PDA

View Full Version : displaying array of vertexs



killercow
08-06-2002, 07:39 AM
I'm trying to displaying a 3ds file. I can read all the vertices just fine and I have them stored in a C++ vector. But when I try to display them using a glDrawArray (I think this is the one) I get a seg. fault. I'm not sure where my problem is, and I'm really not sure on the correct way to display these vertices. Can any one help me out here?
Thanks http://www.opengl.org/discussion_boards/ubb/biggrin.gif

kc

Deiussum
08-06-2002, 07:46 AM
Seg faults are usually caused by touching memory you have no right to touch. If the error appears to be in yourf glDrawArray call, you are probably telling glDrawArray to access an element in your array that is beyond the bounds of that array.

killercow
08-06-2002, 07:57 AM
Yes that's what i was thinking also. I was using the vectors' size() function to get the number of elements, and I had also tried a small portion of vertices to see if it was the problem. I have no problem simply going through the vector and displaying the values. Using the same values that I was passing to glDrawArrays.
I don't know where my problem is. :-(

This is what I have going on I have 501 vertices each with 3 points. So I have a C++ stl vector with 501*3 elements. So then I go
glClientStateEnable(GL_VERTEX_ARRAY)
glVertexPointer(..iforgetwhatigothere..)
glDrawArray(GL_TRIANGLES,0,list.size())

I can't remember the exacts of the code but it's something close to that. I wanted to see if I was accessing something out of my vector so I did this:
for(i = 0; i < list.size(); i++)
printf("%f\n", list[i]);
and it worked fine. Any one got any ideas?

[This message has been edited by killercow (edited 08-06-2002).]

Deiussum
08-06-2002, 08:08 AM
Maybe it would be helpful if you post the line of code you use for glDrawArrays, as well as how you create your array. If you are dynamically creating a multi-dimensional array you can run into problems as well, because the memory is not contiguous.

killercow
08-06-2002, 08:25 AM
I don't have my code here at work but I do remember how I created the array. Since I'm using a c++ vector I just find out the number of elements i need, 501 vertices so I need 501*3 then

list.reserve(501*3)
then I set each element to the values i'm getting from the file.
How would you handle this?

[This message has been edited by killercow (edited 08-06-2002).]

coredump
08-06-2002, 09:23 AM
i'd maybe use a simple array instead of c++ vectors for this. i'm not too sure on the interior workings of the stl vector. is the vector continuous in memory? i know when you create a vector, a certain amount of memory is allocated. once you push_back enough times to fill up that space, do the existing entries in the vector get copied to a bigger memory space? or are the new entries just referenced? not sure...that's why i would use arrays.

b

killercow
08-06-2002, 09:34 AM
Alright thanks for the help guys. I'll try simple arrays. I think I was more worried about whether or not I was actually calling the right functions. For example in glDrawArrays does it matter if the array is a single array, or does it have to be multi-dimenisonal array (#vertices,3)?

Deiussum
08-06-2002, 10:17 AM
An stl vector can occasionally re-allocate memory. So for instance if you do a push_back and it needs to grow the size of the vector something like this goes on..

float *pNew = new float[new size];

memcpy(pOld, pNew, sizeof(float)*pOld.count());

delete [] pOld;
pOld = pNew;


So... if you are pushing new stuff onto there and not re-setting the array that glDrawArrays uses you could be running into a dangling pointer situation.

So far as single vs multi-dimensional array... A single array is generally best. Multi-dimensional arrays that have a static size are contiguous in memory, but that's not the case when you dynamically allocate them, since you need to allocate individual dimensions.

killercow
08-06-2002, 11:37 AM
Ok well i'm gonna try it with a simple GL_float array. I'm not quite sure why I'm using vectors, but oh well...
Thanks for all the help guys http://www.opengl.org/discussion_boards/ubb/biggrin.gif