Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Problems with texcoords and vertex arrays


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 29 October 2001 - 08:31 PM

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

Sponsor:

#2 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 29 October 2001 - 09:43 PM

Post a snippet of code (the declarations/allocations of the arrays and the code use with gl*Pointer and glDrawArrays).

[Resist Windows XP''s Invasive Production Activation Technology!]

#3 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 2001 - 06:00 AM

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

#4 Scarab0   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 2001 - 07:33 AM

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.


#5 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 2001 - 07:48 AM

Tried but no difference...

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

#6 Scarab0   Members   -  Reputation: 122

Like
Likes
Like

Posted 01 November 2001 - 04:15 AM

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

#7 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 04 November 2001 - 09:33 AM

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.

#8 Scarab0   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 November 2001 - 04:04 AM

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

#9 zedzeek   Members   -  Reputation: 528

Like
Likes
Like

Posted 05 November 2001 - 07:25 AM

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.


#10 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 November 2001 - 10:31 AM

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

#11 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 November 2001 - 07:58 AM

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?

#12 zedzeek   Members   -  Reputation: 528

Like
Likes
Like

Posted 06 November 2001 - 09:37 AM

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

#13 Snale   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 November 2001 - 10:10 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS