2012-03-02 52 views
1

私は、軸と共に余弦曲線の折れ線グラフを作成しようとしています。私が抱えている問題は、line_stripは、線を描画した後に軸を描画し続けることです(つまり、線の描画を期待し、停止し、軸が別々に描画を開始することを期待しています。軸はすべて1つのline_stripとして)。私は完全に軸を描く止めるだろうと思ったOpenGLで2つの配列バッファから描画する

//Draw axes 
glBindBuffer(GL_ARRAY_BUFFER, axesBufferObject); 
glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); 

glDrawArrays(GL_LINE_STRIP, 0, 2); // x axis 
glDrawArrays(GL_LINE_STRIP, 2, 2); // y axis 
glDrawArrays(GL_LINE_STRIP, 4, 2); // z axis 

glDisableVertexAttribArray(0); 

が、彼らはまだ描かれている!!:私は理解してdown'tさえ知らない人の事は、私は行を削除しても、ということです

関連するコードを以下に示します。

//Vertices for the axes in 3 dimesions 
const float axesPositions[] = { 
    -1.0f, 0.0f, 0.0f, 1.0f, 
    1.0f, 0.0f, 0.0f, 1.0f, 
    0.0f, -1.0, 0.0f, 1.0f, 
    0.0f, 1.0f, 0.0f, 1.0f, 
    0.0f, 0.0f, -1.0f, 1.0f, 
    0.0f, 0.0f, 1.0f, 1.0f, 
}; 


//calculates cosine line vertices 
std::vector<float> fillPositions() 
{ 
    std::vector<float> arr; 
    for (float x = -1.0f; x < 1.0f; x += 0.01f) 
    { 
     float y; 
     if (x == 0) y = 1; //divide by zero check 
     y = cos(x); 

     arr.push_back(x); 
     arr.push_back(y); 
     arr.push_back(0.0f); 
     arr.push_back(1.0f); 
     } 

    return arr; 
}  

GLuint positionBufferObject; 
GLuint axesBufferObject; 
GLuint vao; 

std::vector<float> linePositions; 

void InitializeVertexBuffers() 
{ 
    //genereate line graph vertex buffer 
    glGenBuffers(1, &positionBufferObject); 

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * linePositions.size(), &linePositions[0], GL_STATIC_DRAW); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 

    //generate axes vertex buffer 
    glGenBuffers(1, &axesBufferObject); 

    glBindBuffer(GL_ARRAY_BUFFER, axesBufferObject); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(axesPositions), axesPositions, GL_STATIC_DRAW); 
    glBindBuffer(GL_ARRAY_BUFFER, 0); 
} 


//Called after the window and OpenGL are initialized. Called exactly once, before the main loop. 
void init() 
{ 
    InitializeProgram(); 

    linePositions = fillPositions(); 

    InitializeVertexBuffers(); 


    glGenVertexArrays(1, &vao); 
    glBindVertexArray(vao); 
} 



//Called to update the display. 
void display() 
{ 

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 

    glUseProgram(theProgram); 

    // Draw line 
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); 

    glDrawArrays(GL_LINE_STRIP, 0, linePositions.size()); 

    glDisableVertexAttribArray(0); 

    //Draw axes 
    glBindBuffer(GL_ARRAY_BUFFER, axesBufferObject); 
    glEnableVertexAttribArray(0); 
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0); 

    glDrawArrays(GL_LINE_STRIP, 0, 2); // x axis 
    glDrawArrays(GL_LINE_STRIP, 2, 2); // y axis 
    glDrawArrays(GL_LINE_STRIP, 4, 2); // z axis 

    glDisableVertexAttribArray(0); 

    glUseProgram(0); 

    glutSwapBuffers(); 
    glutPostRedisplay(); 
} 

答えて

3

linePositions.sizeは()あなたの配列の山車の数ではなく、あなたが描きたい頂点の数です。

変更この行:これまで

glDrawArrays(GL_LINE_STRIP, 0, linePositions.size()); 

glDrawArrays(GL_LINE_STRIP, 0, linePositions.size()/4); 
+0

ああ!あなたは紳士で学者です。この修正の前に、別のバッファに入っていても軸を描画する理由を教えてください。 – bananamana

+0

@bananamana:GPUメモリ内で2つのバッファが互いに近くにあったので、 –

+0

ああありがとう。私はそれが事実かもしれないと思ったが、確かではなかった。 – bananamana

関連する問題