2012-03-02 4 views
1

축과 함께 코사인 곡선의 선 그래프를 만들려고합니다. 내가 겪고있는 문제는 line_strip이 선을 그린 후 축을 그리는 것을 계속한다는 것입니다. 즉 선 그리기, 멈춤, 축이 별도로 그리기를 시작할 것으로 예상됩니다. 이제 선이 선을 그립니다. 축 모두를 하나의 line_strip로). 내가 전부 축 그리기 중지 것이라고 생각OpenGL의 두 배열 버퍼에서 그리기

//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 메모리에서 두 개의 버퍼가 서로 가까이 있었기 때문에? –

+0

아 감사합니다. 나는 그것이 사실일지도 모른다라고 생각했다. 그러나 확실하지 않았다. .. – bananamana