glDrawElements and Face indices

Apprentice
Posts: 6
Joined: 2009.11
Post: #1
hiSmile
i was working on a program that dynamically allocates a multidimensional array in the form of a pointer-to-pointer (**) to store the face indices for glDrawElements(), however when I try to use the pointer -to-pointer for the fourth parameter in glDrawElements() nothing shows up on the screen. I have also tried statically allocating a multidimensional array (not a pointer-to-pointer) to store the face indices and it worksHuh, and both the dynamically allocated array and the statically allocated array are the exactly same, so i'm very confused as to what to do.
So can someone please help me?

here is a sample of the code that doesn't work:
Quote:/*Model is the name of a class which reads the mesh file and allocates the
necessary memory*/
Model mesh_file;

//Get_Faces() is a member function of Model that returns an unsigned char** of indices
unsigned char** faces = mesh_file.Get_Faces();

//Get_Vertices is a member function of Model that returns a float* of vertex coordinates
float* vertices = mesh_file.Get_Vertices();
int main()
{

//set up the vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);

//mechanism for drawing the object
for(int i=0; i<mesh_file.Get_Face_Count(); i++)
{
glDrawElements(GL_POLYGON,4,GL_UNSIGNED_BYTE,faces[i]);
}
return 0;
}

Here is a sample of the code that does work:
Quote:here is a sample of the code that does work:
[quote]
/*Model is the name of a class which reads the mesh file and allocates the
necessary memory*/
Model mesh_file;

//In this case faces contains the exact same element as in the previous case
unsigned char faces[][4] =
{
{1,2,3,4},
{5,8,7,6},
{1,5,6,2},
{2,6,7,3},
{3,7,8,4},
{5,1,4,8}
};

//Get_Vertices is a member function of Model that returns a float* of vertex coordinates
float* vertices = mesh_file.Get_Vertices();

//mechanism for drawing the object
for(int i=0; i<mesh_file.Get_Face_Count(); i++)
{
glDrawElements(GL_POLYGON,4,GL_UNSIGNED_BYTE,faces[i]);
}

And also I am quite aware of the fact that arrays start counting at 0 and that the array of face indices start counting from 1
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #2
I have a feeling I may be getting mixed up here, but....
shouldn't
Code:
unsigned char** faces
just be
Code:
unsigned char* faces
?

That's probably not it, but sticking a a call to glGetError() and seeing what you get might hint at what's wrong Smile

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
You can't use pointer-to-pointer multidimensional arrays for glDrawElements, it needs to a flat block of memory. Statically allocated multidimensional arrays in C are nothing like pointer-to-pointer arrays and far more like unidimensional arrays.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
If you do end up using a flat array, and if all your faces are indeed composed of 4 vertices as in the sample code, you could do something like this:
Code:
Model mesh_file;

//Get_Faces() now returns a one-dimensional array, of size numFaces*4
unsigned char* faces = mesh_file.Get_Faces();

float* vertices = mesh_file.Get_Vertices();
int main()
{
    //set up the vertex array
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,0,vertices);

    //you don't need a for here
    glDrawElements(GL_QUADS,4*mesh_file.Get_Face_Count(),GL_UNSIGNED_BYTE,faces​);
    return 0;
}
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2009.11
Post: #5
hey,
me again,
I tried all three suggestions and I inched a little closer to getting it to work but I am still having one small problem. I decided to use integers instead of unsigned characters (since I would probably be using more than 11 vertices in my program and unsigned characters can't store values greater than 10) and I tried using glGetError() to shed some light on the situation, but instead of helping me, it actually crashes the program.
As for my problem, nothing shows up on screen when I try to use GL_INT for the type parameter of glDrawElements() and I dont quite know why this happens and its not just with glDrawElements(). I also noticed that anytime I try using integers with any OpenGL routine, it doesn't seem to work. For example, when I try changing the color with glColor3i() it doesn't work, so I was wondering if anyone can help me with this problem or at least offer some suggestions

So this is what the code looks like currently:
Code:
/*Model is the name of a class that reads the mesh file and allocates the necessary memory*/
Model mesh_file;

//Get_Faces() is a member function of Model that returns an int* of indices
int* faces = mesh_file.Get_Faces();

//Get_Vertices() is a member function of Model that returns a float* of vertices
float* vertices = mesh_file.Get_Vertices();

int main()
{
//setup vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,vertices);

//suppose to draw mesh
/*Get_Face_Element_Count() is a member function of Model that returns the total number of indices*/
glDrawElements(GL_POLYGON,mesh_file.Get_Face_Element_Count(),GL_INT,faces);
return 0;
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
You can't make OpenGL calls without an OpenGL context.

GL_INT is not a valid type for indices.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2009.11
Post: #7
OK,
so is there any way to call glDrawElements() with integer values?
And by the way I actually tried using unsigned characters instead of integers with the new piece of code and it still doesn't work Huh, so actually I am still lost.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #8
GL_UNSIGNED_SHORT works, but it sounds like you have some other issue going on here.
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2009.11
Post: #9
Thanks,
I actually got to work with GL_UNSIGNED_INT yesterday and the mesh actually showed up on the screen, so everythings fine now. Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Calculate face direction from bvh or 3d skeleton data harisz 3 3,191 May 29, 2013 10:50 AM
Last Post: OneSadCookie
  OpenGL ES : Dynamic calculation of triangle indices akademiks 0 4,275 Sep 13, 2011 07:58 AM
Last Post: akademiks
  Simple ray-face intersect optimization NYGhost 8 6,520 Aug 17, 2007 12:01 PM
Last Post: NYGhost
  Indexed Face Sets (meshes) wyrmmage 4 4,233 Dec 15, 2006 11:18 AM
Last Post: wyrmmage
  Agh! glDrawElements kills my artwork ferum 2 3,648 Nov 23, 2006 09:05 AM
Last Post: ferum