Advertisement

08.06.2003 at 06:18PM PDT, ID: 20702231
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

OpenGL line drawing program problems

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 your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: dak67
Solution Provided By: fl0yd
Participating Experts: 2
Solution Grade: A
Views: 185
Translate:
Loading Advertisement...
08.07.2003 at 12:03AM PDT, ID: 9097457

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.01.2003 at 10:00AM PST, ID: 9663396

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
11.27.2003 at 10:53AM PST, ID: 9833196

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29