[英]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.我想知道我的订单是否正确。
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:为此,您需要的内存安排将依次包含第一个顶点的所有属性值,然后是第二个顶点的值,
ci
。 pi
是顶点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.