Advertisement

08.06.2003 at 06:18PM PDT, ID: 20702231
[x]
Attachment Details

OpenGL line drawing program problems

Asked by dak67 in OpenGL Graphics & Game Programming

Tags: opengl, line

hey there, i hope someone can help. the problems im having with my below code include, storing my lines in a data structure, so i can resize my window and have all lines redrawn (not just the last one), its something with the display and mouse functions i think, check it out
also, can someone implement a clear function, which clears all lines that were drawn, resetting the line number to 0. My big problem tho is implementing Bresenham's algorithm and the SetPixel function. Im sure my code is correct, i need normal lines to be drawn as pixles on the line when the Bresenham option is selected (with the right mouse button), i hope someone can help me ASAP, ive been at it the last few days and hit a brick wall i think, thanks
(by the way, the comments are just code i intended on using but didnt delete yet just in case)
u can email me at dak67@uow.edu.au thanks

//CODE for Line Drawing Program

#include <stdlib.h>
#include <GL/glut.h>

//int ww = 500, wh = 500;
//float colour[7][3] = {1.,1.,1.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,1.,1.,1.,0.,1.,1.,1.,0.};
//int icoord = 0, isline = 0, psize = 1;
//int xp[2],yp[2];

struct LINE
{
       int xp[2];
       int yp[2];
};
/*class LINE
{
      public:
            int xp[2];
            int yp[2];

};
*/
LINE L[100];
int Lnum = 0;      // =0;
int Tnum=100;
int ww = 500, wh = 500;
float colour[7][3] = {1.,1.,1.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,1.,1.,1.,0.,1.,1.,1.,0.};
int icoord = 0, isline = 0, psize = 1;
int x1, y1, x2, y2;

void myinit();
void mouse(int, int, int, int);
void middle_menu(int);
void right_menu(int);
void display();
void reshape(int, int);
void clear();
void Bresenham(int, int, int, int);
void BresLine(int, int);
void SetPixel(int, int);

void myinit()
{
/*      attributes of lines, background, etc */
      glClearColor(0.,0.,0.,1.);            /* black background */
      glColor3f(1.,1.,1.);                  /* draw in white */
      glShadeModel(GL_FLAT);
/*      set up viewing
      500 x 500 window with origin lower left */
      glViewport(0,0,ww,wh);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.,(GLdouble)ww,0.,(GLdouble)wh);      /* ensure aspect ratio */
      glMatrixMode(GL_MODELVIEW);
}
      
void mouse(int btn, int state, int x, int y)
{
    if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
      {
            L[Lnum].xp[icoord] = x/psize;
            L[Lnum].yp[icoord] = (wh-y)/psize;
            if (icoord == 0)
            {
                  icoord++;
                  isline = 0;
            }
            else
            {
                  isline = 1;
                  glBegin(GL_LINES);
                        glVertex2i(L[Lnum].xp[0],L[Lnum].yp[0]);
                        glVertex2i(L[Lnum].xp[1],L[Lnum].yp[1]);
                  glEnd();

                  glFlush();
                  icoord = 0;
            }
      }

}

void middle_menu(int id)
{
      glColor3f(colour[id][0],colour[id][1],colour[id][2]);
}

void right_menu(int id)
{
      switch (id)
      {
            case 1:            // increase pixel size
                  psize++;
                  break;
            case 2:            // decrease pixel size
                  if (psize > 1)
                        psize--;
                  break;
        case 3:
            clear();
            break;
        case 4:
            display();          //GLline
            break;
        case 5:
            Bresenham(x1, y1, x2, y2); //Bresenham(x1, y1, x2, y2);         //Bresenham
            break;
            case 6:      // quit
                  exit(0);
      }
      glPointSize((GLfloat)psize);
      glLineWidth((GLfloat)psize);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.,(GLdouble)ww/psize,0.,(GLdouble)wh/psize); /* ensure aspect ratio */
      glMatrixMode(GL_MODELVIEW);
      glutPostRedisplay();
}

void display()
{
      glClear(GL_COLOR_BUFFER_BIT);
    for (int i=0;i < Tnum; i++)
    {
        if (isline == 1)
        {
            glBegin(GL_LINES);
                  glVertex2i(L[i].xp[0],L[i].yp[0]);
                  glVertex2i(L[i].xp[1],L[i].yp[1]);
            glEnd();
        }
    }
      glFlush();

}

void reshape(int neww, int newh)
{
      wh = newh;
      ww = neww;
      glViewport(0,0,ww,wh);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.,(GLdouble)ww/psize,0.,(GLdouble)wh/psize); /* ensure aspect ratio */
      glMatrixMode(GL_MODELVIEW);
      display();
}

void clear()
{
/*     for (Lnum=0; Lnum < Tnum; Lnum++)
     {
            L[Lnum] = L[Lnum-1];
            Lnum--;
     }
*/
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     Lnum = 0;
}
void Bresenham(int x1, int y1, int x2, int y2)
{
     int Ax, Ay, x, y, x_end;

     Ax = abs(x1-x2);
     Ay = abs(y1-y2);

     if (x1 > x2)
     {
        x = x2;
        y = y2;
        x_end = x1;
     }
     else
     {
         x = x1;
         y = y1;
         x_end = x2;
     }
     SetPixel(x,y);
     BresLine(x,y);
}

void BresLine(int x, int y)
{
     int Ax, Ay, x_end, p, const1, const2;
     p = 2*Ay-Ax;
     const1 = 2*Ay;
     const2 = 2*(Ay-Ax);

     while (x < x_end)
     {
           x = x+1;
           if (p < 0)
                 p = p+const1;
           else
               y = y+1;
               p = p+const2;
           SetPixel(x,y);
      }
}
void SetPixel(int x, int y)
{
//     glRasterPos2i(x,y);
//     glDrawPixels(x,y,GL_RGB,GL_FLOAT,colour);
     glClear(GL_COLOR_BUFFER_BIT);

     glBegin(GL_POINTS);
           glVertex2i(x, y);
       glEnd();
//  for(int x=0; x < Tnum; x++)
//  {
//    for(int y=0; y < Tnum; y++)
//    {
//      if (isline == 1)
//        {

//            glBegin(GL_POINTS);
//            glVertex2i(L[x].xp[0],L[y].yp[0]);
//            glVertex2i(L[x].xp[1],L[y].yp[1]);
//            glEnd();
//      }
//    }
//   }
   glFlush();

}
int main(int argc, char** argv)
{
      glutInit(&argc,argv);
      glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(ww,wh);
      glutCreateWindow("demo");
      glutDisplayFunc(display);
      glutCreateMenu(middle_menu);
      glutAddMenuEntry("White",0);
      glutAddMenuEntry("Red",1);
      glutAddMenuEntry("Green",2);
      glutAddMenuEntry("Blue",3);
      glutAddMenuEntry("Cyan",4);
      glutAddMenuEntry("Magenta",5);
      glutAddMenuEntry("Yellow",6);
      glutAttachMenu(GLUT_MIDDLE_BUTTON);
      glutCreateMenu(right_menu);
      glutAddMenuEntry("increase pixel size", 1);
      glutAddMenuEntry("decrease pixel size", 2);
                glutAddMenuEntry("Clear",3);
                glutAddMenuEntry("GLline",4);
                glutAddMenuEntry("Bresenham",5);
      glutAddMenuEntry("exit",6);
      glutAttachMenu(GLUT_RIGHT_BUTTON);
      myinit();
      glutReshapeFunc(reshape);
      glutMouseFunc(mouse);
      glutMainLoop();
      
      return 0;
}


Start Free Trial
[+][-]08.07.2003 at 12:03AM PDT, ID: 9097457

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: OpenGL Graphics & Game Programming
Tags: opengl, line
Sign Up Now!
Solution Provided By: fl0yd
Participating Experts: 2
Solution Grade: A
 
 
[+][-]11.01.2003 at 10:00AM PST, ID: 9663396

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11.27.2003 at 10:53AM PST, ID: 9833196

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32