glDrawElements vs glDrawArrays

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
webjeff
Posts: 66
Joined: Thu May 05, 2005 2:51 am

glDrawElements vs glDrawArrays

Post by webjeff »

OK,

I am using this sample of code to render a box:

Code: Select all


struct MyVertex
{
	float u, v;
	float x,y,z;
};

struct MyVertex __attribute__((aligned(16))) verti[12*3] =
{
	{0, 0, -1,-1, 1}, // 0
	{1, 0, -1, 1, 1}, // 4
	{1, 1,  1, 1, 1}, // 5

	{0, 0, -1,-1, 1}, // 0
	{1, 1,  1, 1, 1}, // 5
	{0, 1,  1,-1, 1}, // 1

	{0, 0, -1,-1,-1}, // 3
	{1, 0,  1,-1,-1}, // 2
	{1, 1,  1, 1,-1}, // 6

	{0, 0, -1,-1,-1}, // 3
	{1, 1,  1, 1,-1}, // 6
	{0, 1, -1, 1,-1}, // 7

	{0, 0,  1,-1,-1}, // 0
	{1, 0,  1,-1, 1}, // 3
	{1, 1,  1, 1, 1}, // 7

	{0, 0,  1,-1,-1}, // 0
	{1, 1,  1, 1, 1}, // 7
	{0, 1,  1, 1,-1}, // 4

	{0, 0, -1,-1,-1}, // 0
	{1, 0, -1, 1,-1}, // 3
	{1, 1, -1, 1, 1}, // 7

	{0, 0, -1,-1,-1}, // 0
	{1, 1, -1, 1, 1}, // 7
	{0, 1, -1,-1, 1}, // 4

	{0, 0, -1, 1,-1}, // 0
	{1, 0,  1, 1,-1}, // 1
	{1, 1,  1, 1, 1}, // 2

	{0, 0, -1, 1,-1}, // 0
	{1, 1,  1, 1, 1}, // 2
	{0, 1, -1, 1, 1}, // 3

	{0, 0, -1,-1,-1}, // 4
	{1, 0, -1,-1, 1}, // 7
	{1, 1,  1,-1, 1}, // 6

	{0, 0, -1,-1,-1}, // 4
	{1, 1,  1,-1, 1}, // 6
	{0, 1,  1,-1,-1}, // 5
};

...

//That sets it up, this renders it:
glInterleavedArrays(GL_T2F_V3F, 0,  verti);
glDrawArrays(GL_TRIANGLES, 0, 12*3);
OK, fine, I see a box, whoohoo. Now I want to use indicies so I am testing glDrawElements, same data, now rendering using:

Code: Select all


//Setup simple index buffer
USHORT buff[12*3];
	buff[0] = 0;
	buff[1] = 1;
	buff[2] = 2;
	buff[3] = 3;
	buff[4] = 4;
	buff[5] = 5;
	buff[6] = 6;
	buff[7] = 7;
	buff[8] = 8;
	buff[9] = 9;
	buff[10] =10;
	buff[11] =11;
	buff[12] =12;
	buff[13] =13;
	buff[14] =14;
	buff[15] =15;
	buff[16] =16;
	buff[17] =17;
	buff[18] =18;
	buff[19] =19;
	buff[20] =20;
	buff[21] =21;
	buff[22] =22;
	buff[23] =23;
	buff[24] =24;
	buff[25] =25;
	buff[26] =26;
	buff[27] =27;
	buff[28] =28;
	buff[29] =29;
	buff[30] =30;
	buff[31] =31;
	buff[32] =32;
	buff[33] =33;
	buff[34] =34;
	buff[35] =35;


	glInterleavedArrays(GL_T2F_V3F, 0,  verti);
	glDrawElements(GL_TRIANGLES, 12*3, GL_UNSIGNED_SHORT, &buff[0]);

I see nothing. I have tried GL_UNSIGNED_BYTE as well and INT, but nothing different, am I missing something obvesious here?

NOTE:
I also know that I should be using 8 verts and 36 indicies but this is just a test to get drawelemets to work cause on my other models it doesn't seem to be rendering or incorrectly rendering.

Thanks a lot
Jeff King.
User avatar
ReKleSS
Posts: 73
Joined: Sat Jun 18, 2005 12:57 pm
Location: Melbourne, Australia

Post by ReKleSS »

Erm... well... your list is defined as USHORT. Doesn't it follow that you should be using GL_UNSIGNED_SHORT?

edit: ...crap, I should have looked more closely at the code instead of just the text... my mistake.

Also, have you checked to see if glGetError() can tell you anything?

-ReK
Last edited by ReKleSS on Sun Aug 21, 2005 10:48 am, edited 2 times in total.
webjeff
Posts: 66
Joined: Thu May 05, 2005 2:51 am

Post by webjeff »

well yes, but I didn't see anything so I figured it might interpret as something else, who knows what could be wrong.

JK
rinco
Posts: 255
Joined: Fri Jan 21, 2005 2:12 pm
Location: Canberra, Australia

Post by rinco »

perhaps you see black triangles on a black background?
ector
Posts: 195
Joined: Thu May 12, 2005 10:22 pm

Post by ector »

Perhaps you forgot to flush the cache after writing the index array to memory, before drawing it?
http://www.dtek.chalmers.se/~tronic/PSPTexTool.zip Free texture converter for PSP with source. More to come.
Post Reply