简体   繁体   English

带有 ebo 的 Opengl vao

[英]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.我目前正在空闲时间学习 OpenGL,最近我遇到了一个我不明白的“错误”。

The thing is, I have no errors, only nothing appear on my screen.问题是,我没有错误,屏幕上什么也没有出现。 I'm using OpenGL with SFML.我将 OpenGL 与 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().我知道我的问题不在于我的着色器,因为我在使用 GlshaderRiv() 的控制台中没有收到任何错误。

I would like to know if I'm doing the order properly.我想知道我的订单是否正确。

  1. I Create a VBO and a EBO我创建了一个 VBO 和一个 EBO
  2. I Create a VAO我创建了一个 VAO
  3. I bind the current VAO to modify it我绑定当前的VAO来修改它
  4. I bind the current VBO inside the VAO我在 VAO 内绑定当前的 VBO
  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.我将我的第一个数组(顶点位置 vector3f)绑定到我的 VBO 中,并将它们放在第一个具有正确偏移量和步幅的指针中。
  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.我将我的第二个数组(颜色位置 vector3f)绑定到我的 VBO 中,并将它们放在第一个具有正确偏移量和步幅的指针中。
  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.我在我的 VBO 中绑定了我的第三个数组(纹理位置 vector2f),并将它们放在第一个具有正确偏移量和步幅的指针中。
  8. I bind a EBO within the VAO我在 VAO 内绑定了一个 EBO
  9. I bind the EBO with my element position (Vector 3u).我将 EBO 与我的元素位置(矢量 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.我从内存中解除了 VAO 的绑定,因为我的绘图循环在代码中相当晚,所以,我不想白白使用内存空间。 Don't worry, Before I draw I put glBindVertexArray(&vao);别担心,在我画之前我放了 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.当你第二glBufferData()调用,它将覆盖points ,您以前存储在与缓冲区数据colors数据。

The misunderstanding is probably about what glVertexAttribPointer() does.误解可能是关于glVertexAttribPointer()作用。 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.要解决此问题,您必须为每个属性使用不同的缓冲区,或者安排属性数据,以便所有 3 个属性的值都可以存储在同一个缓冲区中。 The arguments of your glVertexAttribPointer() calls actually suggest that you were intending to store all attribute values interleaved in the same buffer.您的glVertexAttribPointer()调用的参数实际上表明您打算将所有属性值交错存储在同一缓冲区中。 To get this working, you have to arrange the attribute values accordingly, and then store them in the buffer with a single glBufferData() call.要使其正常工作,您必须相应地安排属性值,然后使用单个glBufferData()调用将它们存储在缓冲区中。

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:为此,您需要的内存安排将依次包含第一个顶点的所有属性值,然后是第二个顶点的值, cipi是顶点i的位置, ci是颜色, ti是纹理坐标,正确的内存布局是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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