Cylinder VBO OpenGL:我做错了什么?

时间:2015-11-22 13:57:29

标签: opengl vbo

我尝试通过将 std :: vector 传递给顶点缓冲区而不是 static const GLfloat variableName [] = {some data}; 来创建圆柱体VBO。 。但是,我的窗口中没有任何内容。

我的代码出了什么问题?

我的VBO:

glGenBuffers(1, &vertexbuffer2);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2);
std::vector<float> bufferData = CreateCylinder();
glBufferData(
    GL_ARRAY_BUFFER,
    sizeof(bufferData),
    &bufferData.front(),
    GL_STATIC_DRAW);

CreateCylinder方法:

std::vector<float> CreateCylinder(){
    std::vector<float> outputArray;
    int i, j, k;
    for (j = 0; j <= 360; j += precision_deg){
        outputArray.push_back(float(cos((pi / 180 * j))));
        outputArray.push_back(1.0);
        outputArray.push_back(float(sin((pi / 180 * j))));
        outputArray.push_back(float(cos((pi / 180 * j))));
        outputArray.push_back(-1.0);
        outputArray.push_back(float(sin((pi / 180 * j))));

    }
    for (i = 1; i >= -1; i -= 2){
        outputArray.push_back(0.0);
        outputArray.push_back(float(i));
        outputArray.push_back(0.0);
        for (k = 0; k <= 360; k += precision_deg){
            outputArray.push_back(i*float(cos((pi / 180 * k))));
            outputArray.push_back(float(i));
            outputArray.push_back(float(sin((pi / 180 * k))));
    }
}
return outputArray;

}

渲染方法:

void RenderScene6(){

    glDisable(GL_DEPTH_TEST);
    Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    View = glm::lookAt(
    glm::vec3(5.63757, 1.7351, -2.19067), 
    glm::vec3(0, 0, 0), 
    glm::vec3(0, 1, 0)  
    );

    Model = glm::mat4(1.0f);

    MVP = Projection * View * Model; 
    glUseProgram(programID_1); 
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2);
    glVertexAttribPointer(
        0,                  
        3,                  
        GL_FLOAT,           
        GL_FALSE,           
        0,                  
        (void*)0            
        );

glDrawArrays(GL_QUAD_STRIP, 0, 437);
glDrawArrays(GL_TRIANGLE_FAN, 438, 888); //THESE ARE HARDCODED ATM.
glDisableVertexAttribArray(0);

}

1 个答案:

答案 0 :(得分:0)

问题在于您传递给glBufferData的尺寸:sizeof(bufferData)std::vector<float>相同,与矢量数据的大小无关。

您需要做的是传递存储在矢量中的数据的实际大小,如下所示:

glBufferData(
    GL_ARRAY_BUFFER,
    bufferData.size() * sizeof(float),
    &bufferData.front(),
    GL_STATIC_DRAW);