Archived

This topic is now archived and is closed to further replies.

Sanson

Problem with Vertex Array

Recommended Posts

I'' ve this data from my exporter 3DExploration. static GLubyte face_indicies[2][9] = { {2,3,0 ,0,1,2 ,0,1,2 }, {1,2,0 ,3,4,5 ,3,4,5 } }; static GLfloat vertices [4][3] = { {1.97425f,54.367f,2e-06f},{-1.96275f,54.367f,2e-06f},{-1.96275f,50.43f,2e-06f}, {1.97425f,50.43f,2e-06f} }; static GLfloat normals [6][3] = { {0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f}, {0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f} }; static GLfloat textures [6][2] = { {0.0005f,0.0005f},{0.9995f,0.0005f},{0.999501f,0.9995f}, {0.0005f,0.999501f},{0.0005f,0.0005f},{0.999501f,0.9995f} }; I would like to implement Vertex Array. The first problem is how casting multidimensional array in one dimension (glPointer accept only one dimension); GLubyte g_indexArray[18]; //Cast of GLubyte face_indicies[2][9] GLfloat g_vertexArray[12]; //Cast of GLfloat vertices[4][3] GLfloat g_normalArray[18]; //Cast of GLfloat normals [6][3] GLfloat g_texcoordArray[12]; //Cast of GLfloat textures [6][2] void InitializeArray() { glEnableClientState (GL_VERTEX_ARRAY); glEnableClientState (GL_NORMAL_ARRAY); glEnableClientState (GL_TEXTURE_COORD_ARRAY); glVertexPointer(3,GL_FLOAT,0,g_vertexArray); glNormalPointer(GL_FLOAT,0,g_normalArray); glTexCoordPointer(2,GL_FLOAT,0,g_texcoordArray); } void Render() { .......... glDrawElements(GL_TRIANGLES,sizeof(g_indexArray)/sizeof(GLubyte),GL_UNSIGNED_BYTE,g_indexArray); } Someone help me to casting the arrays ? I hope that the rest of code is allright... Thank you very very much Sanson

Share this post


Link to post
Share on other sites
The problem is that the elements in the array must be stored squentially. This is usually not the case with an multidimensional array since these essentially are an array of pointers, where each pointer points to a new array with elements (or another array with pointers if there is more dimensions.) The solution would be to copy all the elements to a regular array but that eat more performance than you gain by using Vertex Arrays.
Another possible solution could be to place all elements in a single dimensioned array and make an multidimensional array out of it. The code to build the vertex array would look something like this:


float *vertices = new float[12] = {1.97425f,54.367f,2e-06f,-1.96275f,54.367f,2e-06f,-1.96275f,50.43f,2e-06f,
1.97425f,50.43f,2e-06f};
float **multiV = new float*[4];
multiV[0] = vertices;
multiV[1] = vertices + 3;
multiV[2] = vertices + 6;
multiV[3] = vertices + 9;

// you can access array elements in two ways:
multiV[2][2] == vertices[8];


The question is why bothering about this anyway. It would be much easier to just use arrays with a single dimension.

Snale
+--My humble and superior homepage

Edited by - snale on November 1, 2001 6:38:44 PM

Share this post


Link to post
Share on other sites
Thank you
but why use another multidimensional array?
I''ve already a multidimensional array.
Anyway the problem is if I do a Display List with the data from 3DExploration all is OK, even with geometry with 100.000 polygons.
When I do a routine for vertex array WITH THE SAME DATA the object is render not correctly.
The first impression was problems with multidimensional array,
after this I''v tried to remove the brackets of all the array,so to create a one dimension array from a multidimension one, the result was the same, I see the geometry but it is incorrect.
The data in my example is for a plane with two tiangles, in this case I see the plane correctly, the problem is with more complex geometry.
How I must utilize the pointers (glVertexPointer ecc.)in your example ?
What about the index in glDrawElements ?
Thank you very much anyway

Share this post


Link to post
Share on other sites
The point in my reply was how you should allocate your multidimensional arrays. You must pass a single dimensioned array to glVertexPointer. You can't just cast a multidimensional array to a singledimensional.

Look at this example:

12 values stored in an array with one dimension. The values are stored sequentially.

float *array = { 1.0,2.0,2.5,1.12,3.0,4.4,2.8,3.3,5.5,1.1,1.2,6.6 }
And this is how it might look in memory (each v represents a byte in the array an b represents a byte not in the array, the dots represents an undefined number of bs)

...bbbvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvbbbb...

This is what an multidimensional array with two dimension with 4 values in each might look like.

float **array = { 0x00FCD344, 0x00FCE45, 0x00FCF99 };
array[0] = {1.0, 2.0, 2.5, 1.12 };
array[1] = {3.0, 4.4, 2.8, 3.3 };
array[2] = {5.5, 1.1, 1.2, 6.6 };

This is how this array would look physically in the memory:

..bbbvvvvvvvvvvvvvvvvbbbbb...bbbbbbbvvvvvvvvvvvvvvvvbbbbb...vvvvvvvvvvvvvvvvbbbbb...

The values are fragmenten and spread in memory.

When you pass an pointer to OGL it assumes that it is an single dimensional array. When glDrawElements is called OGL looks up the elements that corresponds to the indices you've passed.

OGL will draw three triangles. This is how OGL assumes your values are stored. But as I showed you above that is generally not the case with multidimensional arrays.

// The numbers represents the indices.
1 2 3 4 5 6 7 8 9 10 11 12
...bbbvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvbbb...


An multidimensional array is an array with pointers. These pointers point to arrays with values (or pointers). OGL wants one array with values.

The example in my previous reply was illustrating how you could do to use an single dimensioned array as an multidimensional array.

The problems seems to be that you don't understand the how pointers and arrays really work. I recommend you to read a good book on the C language which explains the basics of memory management, pointers etc.




Edited by - Snale on November 2, 2001 4:55:54 AM

Share this post


Link to post
Share on other sites