Advertisement

Help with GL_ELEMENT_ARRAY_BUFFER

Started by February 21, 2018 07:41 PM
6 comments, last by too_many_stars 6 years, 11 months ago

Hello everyone,

I have having issues understanding the opengl index buffers. Here is what I have, a simple quad...


GLfloat verts[] = {		1.0f , 1.0f , 1.0f ,	//[ 0 ]
						-1.0f , 1.0f , 1.0f ,	//[ 1 ]
						-1.0f , -1.0f , 1.0f ,	//[ 2 ]
						1.0f , -1.0f , 1.0f  }; //[ 3 ]

	GLushort indices[] = { 	0 , 1 , 2 , 
							2 , 1 , 3};

	GLfloat uv[] = {	1.0f , 1.0f ,	//front face
						0.0f , 1.0f ,
						1.0f , 0.0f ,
						1.0f , 0.0f ,
						0.0f , 1.0f , 
						0.0f , 0.0f
						
					};

Bound, filled, and passed into my shaders with a texture.

However, when I make the call


glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , index_id );
glDrawElements( GL_TRIANGLES , 3 * 2 , GL_UNSIGNED_SHORT , (void*) NULL );

The textures uv co-ordinates are all screwed up. My winding is CCW for both the verts and the indices. Every index has a uv coordinate so I am at a loss.

Mike

7 minutes ago, too_many_stars said:

The textures uv co-ordinates are all screwed up

You need to have one UV coordinate per vertex, not per index. 

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement
58 minutes ago, swiftcoder said:

You need to have one UV coordinate per vertex, not per index. 

Thanks for the response swiftcoder, I already tried that with the following code...


GLfloat verts[] = {	1.0f , 1.0f , 1.0f ,	//[ 0 ]
						-1.0f , 1.0f , 1.0f ,	//[ 1 ]
						-1.0f , -1.0f , 1.0f ,	//[ 2 ]
						1.0f , -1.0f , 1.0f  }; //[ 3 ]

	GLushort indices[] = { 0 , 1 , 2 , 
							2 , 1 , 3};

	GLfloat uv[] = {	1.0f , 1.0f ,	
						0.0f , 1.0f ,	
						0.0f , 0.0f ,	
						1.0f , 0.0f };	

And the result is a quad with a slice taken out of it on the left hand side.

I should also note that when I use glDrawArrays ( after changing the number of verts and uv's I don't have any issues.)

 

 

quad.jpg

23 minutes ago, too_many_stars said:

And the result is a quad with a slice taken out of it on the left hand side.

Yep, your indices are ordered incorrectly. Given that your vertices are in clockwise order from the front... Try [0,2,1, 0,3,2].

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thanks so much again Swifcoder, that did the trick. I think I completely confused myself going back and forth. 

One more question if I may. 

1.) Is there a performance increase to using indecies? 

Thanks,

 

Mike



 
28 minutes ago, too_many_stars said:

1.) Is there a performance increase to using indecies?

For the most part, yes. The GPU will cache vertex shader executions, so as long as the repeated use of the same index occur reasonably close together, it can use the cached result instead of running the vertex shader again.

Of course, there are cases where indices don't help. Think of a cube with flat shading - in order to get hard edges/corners, each vertex has to have 3 distinct normals, so you need 3 distinct vertices, so 24 vertices all together. Since all the resulting vertices are unique, indices won't buy you anything...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement

That makes sense, thanks so much!

This topic is closed to new replies.

Advertisement