PDA

View Full Version : dynamic 2D arrays



mdog1234
01-13-2003, 12:10 PM
This is not really an opengl question but I im using openGl in this particular program. I am having trouble dynamically allocating a 2d array with c++ this is what i have done
at the top i have done this int **FaceData
FaceData = new int[num_polygons][num_verticies*3];
why doesnt this work?

Bob
01-13-2003, 12:42 PM
It won't work becasue a pointer and an array are different things.

With int **FaceData, FaceData[0] is a pointer to an integer. With int FaceData[5][10] (numbers are just an example), FaceData[0] is an array of five integers.

If you want dynamic 2D arrays, where both dimensions are dynamic, you have to like this.



int **FaceData;

FaceData = new int *[num_polygons];
for(int i = 0; i < num_polygons; ++i)
FaceData[i] = new int[num_vertices*3];

mdog1234
01-13-2003, 12:55 PM
Yeah your right. Thanks that helps

Deiussum
01-13-2003, 01:18 PM
You should also be aware that if you use dynamic 2d arrays, you won't be able to use them with the vertex array functions. That is because the vertex array functions assume the data to all be contiguous, and that won't likely be the case with dynamic 2d arrays. (You allocate memory for an array of pointers first, then allocate memory for each of those pointers.)

mdog1234
01-13-2003, 01:51 PM
so what your saying is. this wont work

lets say i have something like this
FaceData[100][100];

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, VertexData);
glDrawElements(GL_POINTS, 4, GL_INT, FaceData[0]);

GPSnoopy
01-13-2003, 02:03 PM
I think Deiussum meant the dynamic 2d array from Bob

However you can make a memory contiguous dynamic 2d array (which will work with gl functions):



int ** FaceData;
int * FaceData_Memory;

FaceData = new int * [num_polygons];
FaceData_Memory = new int [num_polygons * num_vertices * 3];

for (size_t i = 0; i < num_polygons; ++i)
FaceData[i] = FaceData_Memory + i * (num_vertices * 3);


Don't forget to delete FaceData_Memory and FaceData when you don't need them anymore.

PS: I think that anyone using dynamic multidimensional arrays should always use this method to allocate a contiguous memory block.
Plus it's faster. http://www.opengl.org/discussion_boards/ubb/wink.gif

mdog1234
01-13-2003, 05:33 PM
Where are some good tutorials on vertex arrays. I am able to draw something but it doesnt look right. I dont know what I am doing wrong. If anyone has any experience with 2d dynamic arrays and vertex arrys in opengl some help would be appreciated. Thanks.

Deiussum
01-14-2003, 05:30 AM
Yeah, I was talking about allocating 2d arrays using Bob's example. GPSnoopy's method would work nicely, though. I still tend to just use a single dimension array and calculate the indices as I need them, them. You can also just use a struct with x,y,z coords (and anything else you might want like color, normal, texcoords, etc.) and then just create an array of that.

mdog1234
01-14-2003, 09:22 AM
and the struct will work for vertex arrays?

Deiussum
01-14-2003, 10:44 AM
Yes, structs work nicely. There are some cases with structs where you can get some padding between members due to pack alignment, but generally you are ok if you stick with variables that have a length of 4 bytes like floats.

mdog1234
01-14-2003, 10:50 AM
Ok thanks that helps