2012-02-01 12 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