vertex arrays problem

Started by
7 comments, last by V-man 15 years, 4 months ago
Hi I'm trying to draw my models using vertex arrays but i have a problem. Sth does not work. I store my data like this: float vertex[...][3]; // vertices begin from index 1 unsigned int faces[...]; // faces start from 1 too first I have: glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3,GL_FLOAT,0,&vertex[1][0]); and then in the game loop i use glDrawElements(GL_TRIANGLES,facesCount,GL_UNSIGNED_INT,&faces[1]); but as a result it draws only two strange triangles. If I draw my model normally glBegin(GL_TRIANGLES); for(i=1;i<=facesCount;i++) glVertex3fv(vertex[faces]); glEnd(); everything is ok. Thanks 4 response!
Advertisement
You're setting the vertex pointer to the second element in the vertex array, but in the immediate mode example you're indexing it form the first. Thus, the call to glDrawElements is effectively equivalent to this.
glBegin(GL_TRIANGLES);for(i=1;i<=facesCount;i++)glVertex3fv(vertex[faces + 1]);glEnd();

Maybe it should be glVertexPointer(3,GL_FLOAT,0,&vertex[0][0]);

and also, it was suggested that 2 dimentional arrays can cause problems if the compiler decides to do memory padding.
It's better to make it 1D

ps: what language are you using? because if this is C, then you need to starts from 0 to X-1, not 1 to X
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
V-man,
I count my vertices and faces from 1 not from zero but I provede to glVertexPointer() and glDrawElements() pointer to initial vertex and face
(&vertex[1][0] and &faces[1])
I use 2D array because one dimension is a list of vertices and another one stores x,y,z coordinates (4 example vertex[1][0] - x coordinate of the first vertex)
How can it be done with 1D array?

Brother Bob,
"but in the immediate mode example you're indexing it form the first."
As far as I can see in both examples I count from the 1 to x(from second element) but provide to glVertexPointer() and glDrawElements() pointers to those second elements. I tried to add and subtract 1 to those pointers, but no matter what I do my model is still drawn incorrectly :/
Meybe u can possibly show me how should I call those functions properly?
In order to make the vertex array code behave as the immediate mode code, do what V-man said; pass &vertex[0][0] to glVertexPointer. That will base both methods on the same pointer, and given that the indices are the same, the result will be the same.

Unless you have a really good reason not to, I strongly suggest you go for zero-based values instead. I'm fairly sure all your problems here is because you're trying to work with one-based values in an environment that is native zero-based, and you're having problems getting the necessary adjustments between the two correct.
Quote:Original post by arczev
How can it be done with 1D array?


float myarray[9];

myarray[0]=x;
myarray[1]=y;
myarray[2]=z;
myarray[3]=x;
myarray[4]=y;
........
until [8]=z;
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Yea, I've used 1D array and my model works fine.
But how to call glNormalPointer then?
I store normals in array
float normals[...] and their order in unsigned int norord[..] array
if I call
glDrawElements(GL_TRIANGLES,facesCount,GL_UNSIGNED_INT,faces) normals will be defined by vertex order like vertices instead of normal order.
How to define them using norord array?
You cannot have separate indices for some attributes. You must make sure the normal array use the same index array as your vertex data.
It would go something like this

struct MyVertex{float x, y, z;        //Vertexfloat nx, ny, nz;     //Normal};MyVertex vertex[X];glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), &vertex[0].x);glNormalPointer(GL_FLOAT, sizeof(MyVertex), &vertex[0].nx);

Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement