2
OpenGL/GLUT를 사용하여 Bresenham의 선 그리기 알고리즘을 구현하고 임의적으로 보이는 임의의 이슈와 관련된 문제가 발생했습니다. 다음은 그 예이다 : 여기 OpenGL 선 그리기 결과
내가 관련이있을 수 있습니다 생각하는 일부 코드입니다. 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()
호출과 함께 해당 기능을 추가하기 전에 선 그리기가 정상적으로 작동하는 것처럼 보였습니다.
위의 그림에서 그려지는 선은 왼쪽에있는 선이었습니다. 그것은 효과가 있었지만 다른 유물이 나타났습니다. 그것들은 정점 버퍼에도 있지 않습니다.
어디서 오는 아이디어입니까?
멋진. 매력처럼 일했습니다. – Kyle