What exacly is GL_ELEMENT_ARRAY_BUFFER

Started by
1 comment, last by JohnnyCode 9 years, 5 months ago

Here's the code:


struct vertexData{
float position[3];
float normal[3];
float tangent[3];
float color[3];
float uv[2];
};


struct vertexID{
unsigned int id[3];
};


vertexData quad[4];


quad[3].position[0] = -1.0f;
quad[3].position[1] =  1.0f;
quad[3].position[2] =  0.0f;
quad[3].normal[0] = -1.0f;
quad[3].normal[1] = -0.0f;
quad[3].normal[2] = 0.0f;
quad[3].uv[0] = 0.0f;
quad[3].uv[1] = 1.0f;


quad[2].position[0] =  1.0f;
quad[2].position[1] =  1.0f;
quad[2].position[2] =  0.0f;
quad[2].normal[0] = -1.0f;
quad[2].normal[1] = -0.0f;
quad[2].normal[2] = 0.0f;
quad[2].uv[0] = 1.0f;
quad[2].uv[1] = 1.0f;


quad[1].position[0] =  1.0f;
quad[1].position[1] = -1.0f;
quad[1].position[2] =  0.0f;
quad[1].normal[0] = 1.0f;
quad[1].normal[1] = 0.0f;
quad[1].normal[2] = 0.0f;
quad[1].uv[0] = 1.0f;
quad[1].uv[1] = 0.0f;


quad[0].position[0] = -1.0f;
quad[0].position[1] = -1.0f;
quad[0].position[2] =  0.0f;
quad[0].normal[0] = -1.0f;
quad[0].normal[1] = -0.0f;
quad[0].normal[2] = 0.0f;
quad[0].uv[0] = 0.0f;
quad[0].uv[1] = 1.0f;


glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,4*sizeof(vertexData), &quad,GL_STATIC_DRAW);
vertexID id[2];
id[0].id[0] = 0;
id[0].id[1] = 1;
id[0].id[2] = 2;
id[1].id[0] = 0;
id[1].id[1] = 2;
id[1].id[2] = 3;


glGenBuffers(1,&vbo2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,vbo2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,6*sizeof(unsigned int),&id,GL_STATIC_DRAW);


glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);

What exacly is that variable i created called id what holds those index numbers? I don't quite get it.
I do understand one thing that its for saving memory, for ... hmm building mesh?

Basically sorting out the vertices what are in same position?

I really dont like these things since im holding normal, color etc... data there and perhaps something would go wrong.
Perhaps normals&smoothing would be wrong but i guess thats up to mesh compiler to care for.

but one question and the reply for it could change my view point of them.

for example if im having complex transparent mesh what triangles i need to sort by nearest to far

basically i can change the triangle draw order in that id variable?

if thats not possible then i have get the idea of that GL_ELEMENT_ARRAY_BUFFER pretty wrong way.

Advertisement
Your element array buffer would have where in your array buffer the vertex it wants is so if you was making a square you would have
-1,0f, 1.0f,
-1.0f, -1.0f,
1.0f, -1.0f,
1.0f, 1.0f

Then in your element array you would have
0, 1, 3, //triangle 1
1, 2, 3 //triangle 2

So you can have a buffer full of shorts rather than loads of duplicated floats

Index buffer is the defintion of triangles (surface) upon verticies. Every triangle is formed by 3 indexes that reference a vertex in vertex buffer (GL_ARRAY_BUFFER) respectively to its very position in vertex buffer. The order that the triangles get drawn by is the very exact order of the indexes in the index buffer (GL_ELEMENT_ARRAY_BUFFER).

You can change order of drawing the geometry triangles by reordering the index buffer indicies (GL_ELEMENT_ARRAY_BUFFER), or, by keeping it as it is and instead make sepparate draw calls per every triangle specifying index offset and index count as desired. It is up to conditions which method would perform faster.

I really dont like these things since im holding normal, color etc... data there and perhaps something would go wrong

Only thing that can be of danger is not keeping unitary winding upon indexes (clockwise or counter-clockwise). See this

id[0].id[0] = 0;
id[0].id[1] = 1;
id[0].id[2] = 2;
id[1].id[0] = 0;
id[1].id[1] = 2;
id[1].id[2] = 3;

You are drawing first trinagle as 0th->1th->2th and then the second one as 0th->2th->3th what means that winding is correctly same for both, the counter-clockwise one. A triangle with opposite winding would be understood of being the surface of opposite direction (inverted)

This topic is closed to new replies.

Advertisement