PDA

View Full Version : What in the world am I doing wrong??



dabeav
01-28-2004, 04:32 PM
/*
I am attempting to render a set of triangles, and this is my setup:

I am implementing strips, but cant get that to work, so I am attempting just the triangles for the first run.
*/

enum primitive_type
{
PT_Triangles = 0x0004,
PT_Triangle_Strip = 0x0005
};

struct primitives
{
std::vector<unsigned int> m_Indices;
primitive_type m_Type;
};

/*
A list of strips, each containing a list of indecies, and a type value
*/
std::vector<primitives> Triangles;

/*
I then send a set of parameters to a function, I send a pointer to std::vector<primitives> Triangles; And call it strips inside the function, I also send the verticies, but that isnt important here.
I have the below in a for loop (hence the [B]), and I call it to render my mesh.
*/

glDrawElements(GL_TRIANGLES, (*strips)[B].m_Indices.size(), GL_UNSIGNED_INT, &(*strips)[B].m_Indices);

But it crashs before it renders. I have tried to turn off everything along the way, and it ONLY crashs when it reaches this step. I am realy bad w/pointers, am I possibly doign something wrong? Or what?

endash
01-28-2004, 05:09 PM
Take a look at pages 67-75 of the red book. glDrawElements is for drawing OpenGL Vertex Arrays. It sounds like you are just shoving an array of integers at it. I'm not suprised it's crashing.

That said I havn't used vertex arrays, soo I can't really help you beyond pointing you to the fine manual.

endash
01-28-2004, 05:20 PM
Looking back at your code, I see one level of what you are doing wrong: you are giving glDrawElements a pointer to a std::vector<unsigned int> and not an array at all. (Since the function strictly speaking takes a void*, the compiler won't complain.) Since sizeof(std::vector<unsigned int> is probably much less than the length of the vector, glDrawElements will quickly crash.

dabeav
01-28-2004, 05:34 PM
Ok, so how would I go about fixing that? Should I create an array, and move the data?? but that will be kind of a waiste of time and space... Is there a better way?? Can I pass a pointer??

bunny
01-28-2004, 05:49 PM
vectors are always contiguous in memory. Just pass a pointer to the first element and you'll get the same effect. I don't think this is the problem however.

dabeav
01-28-2004, 06:13 PM
Bunny, YOU ROCK, FIRST ELEMENT!!!!! I was passing a pointer to the vector itself NOT its first element, what a difference [0] makes at teh end of a vector reference. THANK YOU for jogging my memory.

jwatte
01-28-2004, 06:20 PM
Can you take this to the beginner's board? Or, even better, to a beginning C++ programming board?