I'm an enginnering student, our lecturer has ask us to create an opengl app tat shows the bersenham
so far i have derive the algo and the pixels...but i don't know how to draw a line between those pixels..
this is what i have so far

void init(void)
{
  glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white
  glColor3f(0, 0, 0);
  glMatrixMode(GL_PROJECTION);      // set projection parameters
  glLoadIdentity();
  gluOrtho2D(0.0,400.0,0.0,400.0);
}
void setPixel(int px, int py)
{
    glBegin(GL_POINTS);
    glColor3f(1, 1, 1);
        glVertex2i(px, py);
    glEnd();
}
void swap(int i, int j) {
   int t = i;
   i = j;
   j = t;
}
void lineBresenham(int p1x, int p1y, int p2x, int p2y)
{
    int F, x, y;

   if (p1x > p2x)  // Swap points if p1 is on the right of p2
    {
       swap(p1x, p2x);
        swap(p1y, p2y);
    }

    // Handle trivial cases separately for algorithm speed up.
    // Trivial case 1: m = +/-INF (Vertical line)
    if (p1x == p2x)
    {
        if (p1y > p2y)  // Swap y-coordinates if p1 is above p2
        {
            swap(p1y, p2y);
       }

        x = p1x;
        y = p1y;
        while (y <= p2y)
        {
            setPixel(x, y);
            y++;
        }
        return;
    }    // Trivial case 2: m = 0 (Horizontal line)
    else if (p1y == p2y)
    {
        x = p1x;
        y = p1y;

        while (x <= p2x)
        {
            setPixel(x, y);
            x++;
        }
        return;
    }


    int dy            = p2y - p1y;  // y-increment from p1 to p2int dx
    int dx            = p2x - p1x;  // x-increment from p1 to p2
    int dy2           = (dy << 1);  // dy << 1 == 2*dy
    int dx2           = (dx << 1);
    int dy2_minus_dx2 = dy2 - dx2;  // precompute constant for speed up
    int dy2_plus_dx2  = dy2 + dx2;


      if (dy >= 0)    // m >= 0
    {
         // Case 1: 0 <= m <= 1 (Original case)
        if (dy <= dx)
        {
             F = dy2 - dx;    // initial F
                 x = p1x;
             y = p1y;
             while (x <= p2x)
             {
                 setPixel(x, y);
                 if (F <= 0)
                 {
                       F += dy2;
                 }
                 else
                {
                    y++;
                    F += dy2_minus_dx2;
                 }
                 x++;
             }
        }
           // replace all dy by dx and dx by dy)
         else
        {
            F = dx2 - dy;    // initial F

            y = p1y;
             x = p1x;
             while (y <= p2y)
           {
                setPixel(x, y);
                 if (F <= 0)
                 {
                    F += dx2;
                 }
                else
              {
                    x++;
                    F -= dy2_minus_dx2;
                }
                y++;
            }
        }
    }
    else    // m < 0
    {
        // Case 3: -1 <= m < 0 (Mirror about x-axis, replace all dy by -dy)
        if (dx >= -dy)
        {
            F = -dy2 - dx;    // initial F

            x = p1x;
            y = p1y;
            while (x <= p2x)
            {
                setPixel(x, y);
                if (F <= 0)
                {
                    F -= dy2;
                }
                else
                {
                    F -= dy2_plus_dx2;
                }
                x++;
            }
        }
        // Case 4: -INF < m < -1 (Mirror about x-axis and mirror
        // about y=x line, replace all dx by -dy and dy by dx)
        else
        {
            F = dx2 + dy;    // initial F

            y = p1y;
            x = p1x;
            while (y >= p2y)
            {
                setPixel(x, y);
                if (F <= 0)
                {
                    F += dx2;
                }
                else
                {
                   x++;
                    F += dy2_plus_dx2;
                }
               y--;
            }
       }
    }
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    // Draws a line from (100,100) to (400,300).
    lineBresenham(100, 100, 400, 300);

    glFlush();
}
int main( int argc, char **argv)
{
  glutInit(&argc, argv);                        // Initialize GLUT
  glutInitWindowPosition(50,100);       // Set top-left display window position
  glutInitWindowSize(400, 400);     // Set display window width and height
  glutCreateWindow("An Example OpenGL Program");// Create display window

  init();                       // Execute initialization procedure
  glutDisplayFunc(display);     // Send graphics to display window
  glutMainLoop();                   // Display everything and wait
}

can someone guide me onto the correct path..thank you

so far i have derive the algo and the pixels...but i don't know how to draw a line between those pixels

Hi,

OpenGL doesn't directly deal with pixel space. What i mean to say is, the API doesn't have functions to directly draw a line between two pixels.

However, what you can do is this:
1) Setup an orthographic viewing volume.
2) Set the parameters as:

glOrtho(0, winW, 0, winH, -1, 1); // Here, (-1, 1) because anyway you are drawing lines.
winW and winH are your window's width and height, as set by glutInitWindowSize()

3) Now, what you have is, when your ModelView Matrix is identity, all points you address would be window coordinates. i.e

glBegin(GL_LINES);
 glVertex2d(2.0, 2.0);
 glVertex2d(5.0, 5.0);
glEnd();

would actually draw from 2,2 to 5,5 in window coordinates.

Is this what you're looking for?

PS: Did you write the entire code on your own or was it provided by the professor? Because i see that you are setting up the projection matrix right. What exactly are you getting wrong? Uploading a pic may illustrate better.

actually its a code i add from various sources...so i manage to do till the mouse clicking but the lines are drawing in a totally diffrent places

void onMouse(int button, int state, int x, int y) {
    int count=0;
    int x1=0,y1=0,x2=0,y2=0;
  if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  {
    switch(gState)
    {

        case waitingforclick:
        printf("First Click");
        ++numLines;
        x1=lines[numLines][0].x=x;
        y1=lines[numLines][0].y=y;
        x2=lines[numLines][1].x=x;
        y2=lines[numLines][1].y=y;
        gState++;
        break;
        case clickedonce:
        printf("Second Click");
        x2=lines[numLines][1].x=x;
        y2=lines[numLines][1].y=y;
        gState=waitingforclick;
        break;
    }
    printf("%d X1: ",lines[numLines][0].x);
    printf("%d Y1: ",lines[numLines][0].y);
    printf("%d X2:",lines[numLines][1].x);
    printf("%d Y2:",lines[numLines][1].y);
    lineBresenham(lines[numLines][0].x,lines[numLines][0].y,lines[numLines][1].x,lines[numLines][1].y);
  }
}
}

this is my code to take mouse coordinate and return it to the lineBresenham function

so i manage to do till the mouse clicking but the lines are drawing in a totally diffrent places

Hi,

I hope you are aware that the window's origin would be the top left corner. So, when you click on the window, the coordinates returned in your mouse callback would be w.r.t the top left corner. Maybe this is causing the problem?

In any case, can you paste the entire code here?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.