私はOpenGL/GLUTを使用してブレゼンハムの線描画アルゴリズムを実装していますが、一見任意のアーティファクトが表示されるという問題があります。次に例を示します。
これが私が関連すると思ういくつかのコードです。頂点バッファーにデータを取り込むコードは含めませんでした。これは、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()
、線画はうまく機能しているようでした。
上の写真では、描く予定の線が左側の線です。それは機能しましたが、他のアーティファクトが現れました。それらは頂点バッファにもありません、私はチェックしました。
彼らがどこから来ているのかアイデアはありますか?