Question

I am using OpenGL/GLUT to implement Bresenham's line drawing algorithm and having some issues with seemingly arbitrary artifacts showing up. Here is an example:

This should be one line

Here is some code that I think may be relevant. I didn't include the code that populates the vertex buffer because I'm 99% sure it's right and have rewritten it. The problem came up one I started using the GLUT mouse callbacks.

 void Line::draw()
 {
     // Bind program and buffer
     glUseProgram(program);
     glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

     // Get position attribute location
     GLuint vertexPosLoc = glGetAttribLocation(
                               program,
                               "position");

     // Enable attribute
     glEnableVertexAttribArray(vertexPosLoc);

     // Associate vertex position with attribute
     glVertexAttribPointer(vertexPosLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);

     glDrawArrays(GL_POINTS, 0, vertexDataSize);

     // Reset the program
     glDisableVertexAttribArray(vertexPosLoc);
     glBindBuffer(GL_ARRAY_BUFFER, 0);
     glUseProgram(0);
 }



 void display()
 {
     // Clear the color buffer and the depth buffer
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

     vector<Line*>::iterator it;
     for(it = lines.begin(); it < lines.end(); it++)
     {
         (*it)->draw();
     }

     // Draw the temporary line
     if(line)
     {
         line->draw();
     }

     // Swap buffers
     glutSwapBuffers();
 }

void mouseClick(int button, int state, int x, int y)
{
    int viewVals[4];
    glGetIntegerv(GL_VIEWPORT, viewVals);
    y = viewVals[3] - y;
    if(button != GLUT_LEFT_BUTTON)
    {   
        return;
    }   
    if(state == GLUT_DOWN)
    {   
        x1 = x;
        y1 = y;
    }   
    else
    {   
        lines.push_back(line);
        line = NULL;
    }   

    glutPostRedisplay();
}

void mouseMotion(int x, int y)
{
    int viewVals[4];
    glGetIntegerv(GL_VIEWPORT, viewVals);
    y = viewVals[3] - y;

    // Delete the previous line
    delete line;

    // Create a new line
    line = new Line(x1,y1,x,y);
    line->setProgram(program);

    glutPostRedisplay();
}

The idea is that you click a point and the line goes from that point to the point you release. Before I added that functionality along with the glutPostRedisplay() calls, the line drawing seemed to work fine.

In the above picture, the line that was intended to be drawn was the one on the left. It worked, but other artifacts appeared. They aren't in the vertex buffer either, I've checked.

Any ideas where they are coming from?

Was it helpful?

Solution

The third parameter to glDrawArrays() should be the number of points. Are you perhaps passing the number of floats?

(This would cause you to draw twice as many points as you intend, since each vertex in the buffer has two float values. The extra points would have junk values.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top