Skip to main content
GameDev.net gamedev.net
🔒 Locked

Array problem...

Started by Bouga Oct 4, 2004 at 7:20 AM 2 replies 1.1k views
Original Post
Bouga
Bouga
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(‖[v1].x);
           glTexCoord2fv(&UV[6*i+2]);
           glNormal3fv(&Norm[v2].x);
           glVertex3fv(‖[v2].x);
           glTexCoord2fv(&UV[6*i+4]);
           glNormal3fv(&Norm[v3].x);
           glVertex3fv(‖[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?
"A screen buffer is worth a thousand char's" - me
Jis
Jis
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...
( Be Side )
iNsAn1tY
iNsAn1tY
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...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.