Archived

This topic is now archived and is closed to further replies.

Snale

Problems with texcoords and vertex arrays

Recommended Posts

The way texcoords seem, or rather not seem, to be used when using vertex arrays confuses me. I thought that the coordinates associated with vertex N would be stored in coordarray + N * 3 (assuming that we use 3 values per coordinate) but that doesn''t work for me... Perhaps I have missed some fundamental idea here? It would be strange if it worked another way though since the other array types works like that. I''m probably doing something wierd but I''ve doublechecked everything and it looks very right. Snale +--My humble and superior homepage

Share this post


Link to post
Share on other sites
I extracted the allocation code.. I am loading a milkshape model and the vertex and normal arrays are loaded with values as usual.
each mesh struct corresponds roughly to one group in milkshape the triangles array in each mesh struct holds the indices for the triangles in that group. All triangle arrays refers to the same vertex array though (it is one model where each group works as a subset)
Each group has an array with it's own texcoords (the groups have different textures). Since I thought OGL looked up the texcoords the same way it looks up the normals and vertices I allocate space for two texcoords per vertex in the array. The s[] array contains the u coordinates and the t[] array the v coordinates. Everything except the texcoords works perfectly which leads me to the conclusion that texcoords should be stored in a completely different manner. The question is how...

Not very much to say about the drawing code, I pass the vertex and normal arrays to OGL and then each group pass their triangles and texcoords to OGL.

    
//This is the allocation part


vertices = new float[ nVertices * 3 ];
normals = new float[ nVertices * 3 ];

meshes[i].triangles = new unsigned int[g->numtriangles * 3];
meshes[i].texCoords = new float[nVertices * 2];

meshes[i].triangles[tmpt * 3 + X] = t[j].vertexIndices[X];
meshes[i].triangles[tmpt * 3 + Y] = t[j].vertexIndices[Y];
meshes[i].triangles[tmpt * 3 + Z] = t[j].vertexIndices[Z];

meshes[i].texCoords[t[j].vertexIndices[k] * 2] = t[j].s[k];
meshes[i].texCoords[t[j].vertexIndices[k] * 2 + 1] = t[j].t[k];

// Drawing code


glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer( 3, GL_FLOAT, 0, vertices );
glNormalPointer( GL_FLOAT, 0, normals );
for( int i = 0; i < nMeshes; i++ ) {
glTexCoordPointer( 2, GL_FLOAT, 0, meshes[i].texCoords );
glBindTexture(GL_TEXTURE_2D, materialList[meshes[i].material].getGLId());
glDrawElements( GL_TRIANGLES, meshes[i].nTriangles * 3, GL_UNSIGNED_INT, meshes[i].triangles );
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);



Snale
+--My humble and superior homepage

Edited by - snale on October 31, 2001 1:10:13 PM

Share this post


Link to post
Share on other sites
I''ve got no vertex array experience myself, but you could try to move the vertex and normal pointers inside the loop after the texturecoordinates pointer like this:

for( int i = 0; i < nMeshes; i++ )
{
glBindTexture(GL_TEXTURE_2D, materialList[meshes.material].getGLId());
glTexCoordPointer( 2, GL_FLOAT, 0, meshes[i].texCoords );
glNormalPointer( GL_FLOAT, 0, normals );
glVertexPointer( 3, GL_FLOAT, 0, vertices );
glDrawElements( GL_TRIANGLES, meshes[i].nTriangles * 3, GL_UNSIGNED_INT, meshes[i].triangles );
}

Using the standard OpenGL calls, glVertex must be after the corresponding glNormal and glTexCoord calls. So I thought maybe this is the same for vertex arrays? I can''t possibly think of a reason _why_ though.

Share this post


Link to post
Share on other sites
Tried but no difference...

I think OGL evaluate the vertex array last no matter which order you passed them.

Share this post


Link to post
Share on other sites
quote:
Tried but no difference...

You could also try enabling the vertex array last.
quote:

I think OGL evaluate the vertex array last no matter which order you passed them.

Yeah that would make sense. It was just a thought.

Dirk =[Scarab]= Gerrits

Share this post


Link to post
Share on other sites
I tried just about everything now. The loading code is right and so is the drawing code and nothing happends with the texture coordinates, vertices or anyt other array since the program currently only loads the model and draws it. By checking nehe''s tutorial on ms3d loading I found out that Milkshape uses inverted V-coordinates and by changing the loading code so it would take that into account things got better. But there are still numerous errors. There must be something else that i''ve missed about the ms3d file format. I can''t possibly find any other solution.

Share this post


Link to post
Share on other sites
You could also try changing:
glTexCoordPointer( 2, GL_FLOAT, 0, meshes.texCoords );
into:
glTexCoordPointer( 2, GL_FLOAT, 0, &meshes[i].texCoords[0] );

Again though, I have no idea if it works.


Dirk =[Scarab]= Gerrits

Share this post


Link to post
Share on other sites
draw it without using vertex arrays
ie with glBegin(GL_TRIANGLES)..glEnd() im sure youll find youre supplying the incorrect data to the texture coords array + not that theres a problem with the vertex arrays.

Share this post


Link to post
Share on other sites
Good idea... and it you were right. It is strange though, because I can't really see what it is that is wrong. Perhaps I've looked at the code so much now that I simply doesn't see anything unusal at all.
The problem seems to lie within how I store the texture coordinates in my array. And that probably means there is a problem in my loading code. But as I said, I simply can't find it. Perhaps (as I mentioned in my previous reply) milkshape isn't storing texture coordinates the way I thought.

I think I should mention that most of the model looks correct. The problems are limited to parts of the model where the texturing looks completely messed up (not even slightly correct).


Edited by - snale on November 5, 2001 5:34:28 PM

Share this post


Link to post
Share on other sites
Found the real problem... but no solution

The problem is that milkshape stores three s and t coordinates with each triangle. So one vertex can actually have several texture coordinates. But when using vertex arrays they can only have one(or?).

Anyone who knows a solution to this or do I have to skip vertex arrays?

Share this post


Link to post
Share on other sites
the reason it does this is cause often models need multiple texture coordinate eg the lightmaps will usually use different texture coords than the decal texture.
solution is to duplication vertices, its a pain but it has to be the done

Share this post


Link to post
Share on other sites
Hmm.. ok.
But just duplicating isn''t enough if I want to be sure I can load all milkshape models. Theoretically a vertex could belong to any number of triangles and each triangle could have a different texture coordinate associated with that vertex. Of course I could run through all the triangles and checking their texture coordinates and creating new vertices were it is needed but that seems like it more work than it''s worth. Perhaps in a later stage in development I might consider something like that.

Share this post


Link to post
Share on other sites