2012-02-01 167 views
2

我正在使用OpenGL/GLUT来实现Bresenham的线条绘制算法,并且出现了一些看似随意的工件出现问题。这里有一个例子:OpenGL线条绘制工件

This should be one line

下面是一些代码,我认为可能是相关的。我没有包含填充顶点缓冲区的代码,因为我99%确定它是正确的并且已经重写了它。问题出现了,我开始使用GLUT鼠标回调。

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(); 
} 

这个想法是,你点击一个点,线从该点到你释放点。在我将这些功能与glutPostRedisplay()调用一起添加之前,线条绘图似乎工作正常。

在上图中,打算绘制的线是左边的线。它的工作,但其他文物出现。它们也不在顶点缓冲区中,我检查过了。

他们来自哪里的任何想法?

回答

4

glDrawArrays()的第三个参数应该是个点的个数。你可能会传递一些花车吗?

(这会导致你画的两倍多点,你打算,因为在缓冲区中的每个顶点有两个浮点值。加分将有垃圾值。)

+0

精彩。像魅力一样工作。 – Kyle 2012-02-01 23:17:45