glDrawElements - what i do wrong?

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

Moderators: cheriff, TyRaNiD

Post Reply
LarryTM
Posts: 30
Joined: Tue Jul 04, 2006 4:05 am
Contact:

glDrawElements - what i do wrong?

Post by LarryTM »

Im trying to learn how to use glDrawElements, but i have a problem.

Here is my cube object :

Code: Select all

#define NR_Box01 0
objects[NR_Box01].id = ID_TRIMESH;
objects[NR_Box01].vertex_count = 8;
objects[NR_Box01].faces_count = 12;
objects[NR_Box01].vertex_list = &Box01_vertex_list[0];
objects[NR_Box01].faces_list = &Box01_faces_list[0];
objects[NR_Box01].face_normals_list = &Box01_face_normals_list[0];
objects[NR_Box01].tex_coords_list = &Box01_tex_coords_list[0];
objects[NR_Box01].dim_x1 = -5.000000;
objects[NR_Box01].dim_x2 = 5.000000;
objects[NR_Box01].dim_y1 = -5.000000;
objects[NR_Box01].dim_y2 = 5.000000;
objects[NR_Box01].dim_z1 = -4.981750;
objects[NR_Box01].dim_z2 = 5.018250;

static xyz Box01_vertex_list[] = 
{
{-5.000000, -5.000000, -4.981750},
{5.000000, -5.000000, -4.981750},
{-5.000000, 5.000000, -4.981750},
{5.000000, 5.000000, -4.981750},
{-5.000000, -5.000000, 5.018250},
{5.000000, -5.000000, 5.018250},
{-5.000000, 5.000000, 5.018250},
{5.000000, 5.000000, 5.018250}
};
static face Box01_faces_list[] = 
{
0, 2, 3,
3, 1, 0,
4, 5, 7,
7, 6, 4,
0, 1, 5,
5, 4, 0,
1, 3, 7,
7, 5, 1,
3, 2, 6,
6, 7, 3,
2, 0, 4,
4, 6, 2
};
static xyz Box01_face_normals_list[] = 
{
{0.000000, 0.000000, -1.000000},
{0.000000, 0.000000, -1.000000},
{0.000000, 0.000000, 1.000000},
{0.000000, 0.000000, 1.000000},
{0.000000, -1.000000, 0.000000},
{0.000000, -1.000000, 0.000000},
{1.000000, 0.000000, 0.000000},
{1.000000, 0.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{0.000000, 1.000000, 0.000000},
{-1.000000, 0.000000, 0.000000},
{-1.000000, 0.000000, 0.000000}
};
static coords Box01_tex_coords_list[] = 
{
{0.999500, 0.000500, 0.000499},
{0.000499, 0.000500, 0.999500},
{0.000499, 0.000500, 0.000500},
{0.000499, 0.000500, 0.999500},
{0.999500, 0.999501, 0.000499},
{0.000499, 0.999501, 0.999500},
{0.000499, 0.999501, 0.000500},
{0.000499, 0.999501, 0.999500},
{0.999501, 0.000500, 0.000500},
{0.999500, 0.999501, 0.000500},
{0.000499, 0.999500, 0.000500},
{0.000499, 0.999500, 0.000500},
{0.000500, 0.000499, 0.000500},
{0.999501, 0.000500, 0.000500},
{0.000499, 0.000500, 0.999501},
{0.999500, 0.000499, 0.999501},
{0.999501, 0.999500, 0.999501},
{0.999501, 0.999500, 0.999501},
{0.000500, 0.999501, 0.999501},
{0.000499, 0.000500, 0.999501},
{0.000499, 0.000500, 0.000500},
{0.999500, 0.000500, 0.000499},
{0.999500, 0.999501, 0.000499},
{0.999500, 0.999501, 0.000499},
{0.000499, 0.999501, 0.000500},
{0.000499, 0.000500, 0.000500},
{0.000499, 0.000500, 0.999500},
{0.999500, 0.000500, 0.999501},
{0.999500, 0.999501, 0.999501},
{0.999500, 0.999501, 0.999501},
{0.000499, 0.000500, 0.999500},
{0.999500, 0.000500, 0.999501},
{0.999500, 0.999501, 0.999501},
{0.999500, 0.999501, 0.999501},
{0.000499, 0.000500, 0.000500},
{0.999500, 0.999501, 0.000499}
};
and here is my code :

Code: Select all

unsigned int	m_nVBOVertices;	
unsigned int	m_nVBOTexCoords;	
unsigned int index_buf;

Init :

  glGenBuffersARB( 1, &m_nVBOVertices );	
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices );		
	glBufferDataARB( GL_ARRAY_BUFFER_ARB, objects[0].vertex_count*3*sizeof(float), objects[0].vertex_list, GL_STATIC_DRAW_ARB );
	
	glGenBuffersARB(1, &index_buf);
	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, index_buf);
	glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, objects[0].faces_count*sizeof(GLushort), objects[0].faces_list, GL_STATIC_DRAW_ARB); 

Draw:

glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB( GL_ARRAY_BUFFER_ARB, m_nVBOVertices );
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );      
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, index_buf);	
               
glDrawElements(GL_TRIANGLES, objects[0].faces_count, GL_UNSIGNED_SHORT, 0);
glDisableClientState( GL_VERTEX_ARRAY );	
        
And I have one triangle on screen. Why? What I do wrong?

/lar
Kojima
Posts: 275
Joined: Mon Jun 26, 2006 3:49 am

Post by Kojima »

you're passing the amount of faces to drawelements, but as the name suggests, you actually have to pass how many elements there are.

i.e TriCount * 3
Post Reply