私は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