简体   繁体   中英

Opengl vao with ebo

I'm currently learning OpenGL in my free time and lately I have been facing an "error" I don't understand.

The thing is, I have no errors, only nothing appear on my screen. I'm using OpenGL with SFML.

Here is my code. Here is my method:

void CreateObjet(GLuint& vao, GLuint& vbo, GLuint& ebo, GLuint& textureLocation)

//I create my arrays here.. Don't worry they are fine.

CreateTexture(textureLocation);

glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);

glGenVertexArrays (1, &vao);
glBindVertexArray (vao); //On travaille dans le VAO

    glBindBuffer(GL_ARRAY_BUFFER, vbo);

        glBufferData (GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
        glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), NULL);
        glEnableVertexAttribArray(0);

        glBufferData (GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
        glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3* sizeof(GLfloat)));
        glEnableVertexAttribArray(1);

        glBufferData (GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);
        glVertexAttribPointer (2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6* sizeof(GLfloat)));
        glEnableVertexAttribArray(2);

        //EBO
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indiceFinal), indiceFinal, GL_STATIC_DRAW); 

glBindVertexArray(0);

I know my problem is not with my shaders because I receive no errors in my console with GlshaderRiv().

I would like to know if I'm doing the order properly.

  1. I Create a VBO and a EBO
  2. I Create a VAO
  3. I bind the current VAO to modify it
  4. I bind the current VBO inside the VAO
  5. I bind my first array (Vertex Position vector3f) in my VBO and put them in the first pointer with the correct offset and stride.
  6. I bind my second array (Color Position vector3f) in my VBO and put them in the first pointer with the correct offset and stride.
  7. I bind my third array (Texture Position vector2f) in my VBO and put them in the first pointer with the correct offset and stride.
  8. I bind a EBO within the VAO
  9. I bind the EBO with my element position (Vector 3u).
  10. I unbind the VAO from the memory because my drawing loop is quite later in the code and so, I don't want to use memory space for nothing. Don't worry, Before I draw I put glBindVertexArray(&vao);

That definitely does not look right. You're writing the values for all attributes to the same buffer, with each one overwriting the previous one:

glBufferData (GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), NULL);
glEnableVertexAttribArray(0);

glBufferData (GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
...

When you make the second glBufferData() call, it will overwrite the points data that you previously stored in the buffer with the colors data.

The misunderstanding is probably about what glVertexAttribPointer() does. It specifies which buffer the given attribute is sourced from, as well as the data layout (component count, type, etc). But it does not create a copy of the buffer data, or anything like that. The attribute data you want to use must still be stored in the buffer at the time of the draw call.

To fix this, you either have to use a different buffer for each attribute, or arrange the attribute data so that the values for all 3 attributes can be stored in the same buffer. The arguments of your glVertexAttribPointer() calls actually suggest that you were intending to store all attribute values interleaved in the same buffer. To get this working, you have to arrange the attribute values accordingly, and then store them in the buffer with a single glBufferData() call.

The memory arrangement you will need for this will have all the attribute values for the first vertex in sequence, followed by the values for the second vertex, etc. With pi the position of vertex i , ci the color, and ti the texture coordinates, the correct memory layout is:

p0x p0y p0z c0r c0g c0b t0s t0t
p1x p1y p1z c1r c1g c1b t1s t1t
p2x p2y p2z c2r c2g c2b t2s t2t
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM