Jump to content
  • Advertisement
Sign in to follow this  
arczev

vertex arrays problem

This topic is 3587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
You cannot have separate indices for some attributes. You must make sure the normal array use the same index array as your vertex data.

Share this post


Link to post
Share on other sites
It would go something like this


struct MyVertex
{
float x, y, z; //Vertex
float nx, ny, nz; //Normal
};

MyVertex vertex[X];

glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), &vertex[0].x);
glNormalPointer(GL_FLOAT, sizeof(MyVertex), &vertex[0].nx);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!