PDA

View Full Version : Vertex Array Problem



TheBlob
05-28-2002, 09:26 AM
Im trying to convert my *working* code for a heightfield from ImmidiateMode to Vertex Arrays, but I encounter problems (would be crazy if it worked without problems *g*).
All data is stored and precalculated in one of my classes.
Then I wanted to copy all data out of the classes into Arrays, so I can use Vertex Arrays:
Heres my code:




float **pointData;
pointData = new float *[MAP_DIMENSION*MAP_DIMENSION];

for(int y=0; y<MAP_DIMENSION; y++)
{
for(int x=0; x<MAP_DIMENSION; x++)
{

pointData[((y)*field_dim)+(x)] = new float [3];

pointData[((y)*field_dim)+(x)][0] = heightfield[((y)*field_dim)+(x)].point.x;
pointData[((y)*field_dim)+(x)][1] = heightfield[((y)*field_dim)+(x)].point.y;
pointData[((y)*field_dim)+(x)][2] = heightfield[((y)*field_dim)+(x)].point.z;


}
}

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, pointData);


I think its quite clear, what Im doing....
When Im rendering the scene using no Vertex Arrays Im just looping through each point with two for-loops, as I have done above.
But the problem for the Vertex Arrays is maybe that OpenGL accesses the indices in a diffrent way.
So nothing is rendered (but the performance goes down, so I guess, you just cant see the triangles)

Has anybody an idea?? Please!!
Thanks in Advance!

Lev
05-28-2002, 09:52 AM
Are you using glDrawElements or glDrawArrays?

-Lev

TheBlob
05-28-2002, 09:55 AM
Yes!!! *g*

velco
05-28-2002, 10:53 AM
Your pointData array must be contiguous. Allocate it with

pointData = new float [dim*dim*3];

and initialize with

pointData [y * dim * 3 + x] = x_coord;
pointData [y * dim * 3 + x + 1] = y_coord;
pointData [y * dim * 3 + x + 2] = z_coord;

Regards,
-velco


[This message has been edited by velco (edited 05-28-2002).]

TheBlob
05-28-2002, 11:12 AM
It sounds logical and I tried your code, but now my program crashes...*g*

Ive started to calculate a little and heres my result:
Lets imagine a 4*4*3 array (dim=4).
So we allocate an array with the size 48.

And now access the 3rd parameter of the last entry with coordinates x=3, y=3.
Teh index would be: (3 * 4 * 3) + 3 + 2 = 41...so 41 is the biggest reachable index with this formula. Somethings wrong here, or am I wrong?

[This message has been edited by TheBlob (edited 05-28-2002).]

velco
05-28-2002, 11:29 AM
Uh, oh, it should be

pointData[ y * dim * 3 + x * 3 + 0] = x;
pointData[ y * dim * 3 + x * 3 + 1] = y;
pointData[ y * dim * 3 + x * 3 + 2] = z;

Lev
05-28-2002, 11:50 AM
Originally posted by TheBlob:
Yes!!! *g*

I was actually asking which one of the two, but well http://www.opengl.org/discussion_boards/ubb/smile.gif

-Lev

TheBlob
05-28-2002, 08:37 PM
Oh....Im trying glDrawArrays.