C ++ / OpenGL - drawing a VBO cube

Recently, I began to study OpenGL, and I am beginning to study its new assets. Some time ago I tried to work with VBOs to draw a simple cube from an array of vertex points. I am having trouble displaying it and understanding some of the arguments in functions. This does not cause errors, but in my opinion is worth nothing.

float cubeVertPoints[72]; //An array containing the vertex points

Here is my VBO init

void loadBufferData()
{
    glGenBuffers(1, &cubeVBO);
    glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertPoints[0])*3, &cubeVertPoints[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(cubeVertPoints[0])*3, (void*)sizeof(GL_FLOAT));
} 

Drawing

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); 
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(cubeVertPoints[0])*3, (void*)sizeof(GL_FLOAT));

    glDrawArrays(GL_TRIANGLES, 0, 1); 

    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
+3
source share
1 answer

You do not transfer all your data to the vertex buffer. The second parameter glBufferDatatakes the size of all your vertex data in bytes. You must install it on sizeof(vertex) * vertex_count.

In addition, the call glEnableVertexAttribArrayand glVertexAttribPointerin loadBufferDatais redundant. You should only call them in your rendering function.

glVertexAttribPointer. "" . , 0. , 0 ( ) sizeof(float) * 3 ( , 3 ).

, . 72/3=24, 24 .

, , , :

struct Vertex
{
  float position[3];
};

, .. (GLvoid*)(&((Vertex*)NULL)->position).

+2

All Articles