Jump to content
  • Advertisement
Sign in to follow this  
Bouga

Array problem...

This topic is 5403 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, so heres the problem: I have a model in which each vertex can have different texture coords in any of the triangles that it is part of. In other words I have my vertex positions and normals in arrays that can be rendered with indices (the data is per vertex) but the texture coords are in an array that can be rendered with the usual drawelements - the data is per vertex per triangle. Heres how I render it now:
for(int i=0;i<TriCount;i++)
           {
           v1=Tris.Vert[0];
           v2=Tris.Vert[1];
           v3=Tris.Vert[2];
           glTexCoord2fv(&UV[6*i]);
           glNormal3fv(&Norm[v1].x);
           glVertex3fv(&Vert[v1].x);
           glTexCoord2fv(&UV[6*i+2]);
           glNormal3fv(&Norm[v2].x);
           glVertex3fv(&Vert[v2].x);
           glTexCoord2fv(&UV[6*i+4]);
           glNormal3fv(&Norm[v3].x);
           glVertex3fv(&Vert[v3].x);
           }
But thats quite slow, so how should I optimise it? Is it posiible to somehow draw the normals and vertexes like with glDrawRangeElements and the texcoords like with glDrawElements at the same time?

Share this post


Link to post
Share on other sites
Advertisement
You can set some information about how the opengl should look into the array(how many chars to jump between two values...). If you can't find the solution with these, i think you must create a new array with the same values better organized. I had a problem recently on how to send the face's normals recently, and haven't found yet the solution to this, even with the extensions...

Share this post


Link to post
Share on other sites
Quote:
Original post by Bouga
Is it posiible to somehow draw the normals and vertexes like with glDrawRangeElements and the texcoords like with glDrawElements at the same time?

No, that isn't possible (I'm sure this time! [smile]). What you need to do is create three arrays (one for normals, one for texture coords and one for vertices), and one index array (that you can pass to glDrawElements).

To do this, all your arrays need to be "synchonized", so that if you wanted to access the normals, texture coords and vertices for a triangle's vertex, you'd look in the same place in every array. This is the "better organization" that Jis is talking about. For example, the following snippet would set all the data for index element 59:
glNormal3fv(m_pNormalBuffer[59]);
glTexCoord2fv(m_pTexCoordBuffer[59]);
glVertex3fv(m_pVertexBuffer[59]);
Now, if you're loading your data from a model, then you probably have three arrays (normal, texcoord and vertex), and an index buffer for each. The index buffers will all be the same size. Create new normal, texcoord and vertex arrays. Run through the index buffers, and save the values they index in their respective arrays to the new arrays. Your data will now be in the right order. Some data will be duplicated, but normally, the new arrays will only be about 10-20% bigger than the old ones. The index buffer is simple to create. It's just a list of values 1, 2, 3, 4, 5...n, where n is the number of triangles * 3. Anyway, I answered a similar question a while back, and put some code there. Might help.

@Jis: If you're using vertex arrays, and you want to pass the face normal with each vertex, then you do need a normal array. However, instead of the three normal values for a triangle being different (per-vertex normals), you set them all to be the face normal...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Too bad, I guess I will have to double some vertex data then, thanks.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!