2012-02-01 11 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()の3番目のパラメータは、ポイントの数にする必要があります。おそらく浮動小数点数を渡していますか?

(バッファ内の各頂点は、2つのfloat値を持っているので、これは、あなたが意図したとおり、あなたが2倍のポイントを描画する原因となる。余分なポイントは、ジャンク値を持っているでしょう。)

+0

ワンダフル。魅力のように働いた。 – Kyle