PDA

View Full Version : Rendering md2 mesh via vertex array, texturing problem



dopeflow
05-06-2003, 07:56 AM
Basically I can render the geometry perfectly, and its textured too, but the texture is just badly applied to it. It was working fine before I switched to vertex arrays, so I assume that im just loading the textureArray in a wrong way.

This is what I do :

I load the md2 header and get the number of st's from that.

Then create a "buffer" to the texture info in the file.

stIndex_t *stPtr;
stPtr = (stIndex_t*)&buffer[modelHeader->offsetST];
texVB= new float[2*m_numST];

and load up the st's into my array which is the array i pass
to glTexCoordPointer(2, GL_FLOAT, 0, texVB);

for (t = 0; t < m_numST; t++)
{
texVB[2*t+0]= (float)stPtr[t].s / (float)m_modelTex->width;
texVB[2*t+1]= (float)stPtr[t].t / (float)m_modelTex->height;
}

And after that I just render using

glTexCoordPointer(2, GL_FLOAT, 0, texVB);
and
glDrawElements... which works since my mesh shows up fine http://www.opengl.org/discussion_boards/ubb/smile.gif

Any help is greatly appreciated, ive been stuck on this problem for a few days :P

Thanks.

Mezz
05-06-2003, 10:15 AM
OK, the problem I think is that the geomtric vertices and the number of texture coordinates don't match one for one in MD2 files. Also, their indices don't either. Vertex arrays require a one to one mapping for all vertex attributes.
Basically, your DrawElements() takes a pointer to some indices, and those indices are used for both geometry and texture coordinate lookups - but with MD2s the geometry indices aren't the same as the texture coordinate indices, hence your problem.

A solution that might be viable is to duplicate coordinates where the indices don't match.

-Mezz

dopeflow
05-06-2003, 10:40 AM
hmm yea this is the probably my problem.
Im not sure what you mean though, I would do this during initialization? (while loading my texture array?)

dopeflow
05-06-2003, 03:47 PM
ok i figure something out, mostly that I will have 6 indices per triangle (1 st value per vertice).

So after I load up my triangle and texture index, which I know is right because when I was doing the rendering vertex per vertex it worked fine, I load up the array like such:

texVB= new float[6*m_numTriangles];
for(i = 0; i < m_numTriangles; i++)
{
texVB[6*i+0]= m_st[m_triIndex[i].stIndex[0]].s;
texVB[6*i+1]= m_st[m_triIndex[i].stIndex[0]].t;
texVB[6*i+2]= m_st[m_triIndex[i].stIndex[2]].s;
texVB[6*i+3]= m_st[m_triIndex[i].stIndex[2]].t;
texVB[6*i+4]= m_st[m_triIndex[i].stIndex[1]].s;
texVB[6*i+5]= m_st[m_triIndex[i].stIndex[1]].t;
}

im doing 0 - 2 - 1 order because thats how it was in the book i have, but i also tried with 0 - 1 - 2 and it didnt worked more.


Now when I render I do :

glActiveTextureARB(GL_TEXTURE0_ARB);
glTexCoordPointer(6, GL_FLOAT, 0, texVB);
glBindTexture(GL_TEXTURE_2D, m_modelTex->texID);
glEnable(GL_TEXTURE_2D);
glDrawElements(GL_TRIANGLES, m_numTriangles*3, GL_UNSIGNED_SHORT, m_testTriIndex);

6 for texCoordPointer since there is 6 values per triangle now, but I also tried with 4 and 2 and it doesnt work more...im out of idea here!

Need help thanks! :P

rgpc
05-06-2003, 07:41 PM
A screen shot is always very useful with a problem like this.

However, that being said...


From MSDN
size
The number of coordinates per array element. The value of size must be 1, 2, 3, or 4.


From your code



glTexCoordPointer(6, GL_FLOAT, 0, texVB);


I would recommend you change your "6" to "2"

dopeflow
05-06-2003, 09:03 PM
yup i fixed that, doesnt work more though http://www.opengl.org/discussion_boards/ubb/frown.gif

rgpc
05-06-2003, 11:49 PM
Originally posted by dopeflow:
yup i fixed that, doesnt work more though http://www.opengl.org/discussion_boards/ubb/frown.gif

The detail is mind boggling.

What do you mean it doesn't work? Can you supply a screen shot? Are you getting any errors from OpenGL? (glGetError()) Is it simply that the textures are not displaying correctly (ie. Is this an Opengl problem at all or are you just unable to load and display MD2 models)?

dopeflow
05-07-2003, 12:36 AM
like i said, the texture is applied to my mesh, but not correctly. Other than that everything runs fine.

sanjo
05-07-2003, 01:21 AM
if the texture is a pcx or the like.
then its y-flipped wrt. to opengl.
so change : t -> 1-t in every texcoord and then it maybe work.

Mezz
05-07-2003, 08:04 AM
Hi again.

Since you said it works OK when using immediate mode, I'm fairly confident the one-one mapping of texture/geometric coordinates is your problem. As I said, duplication of vertices is a solution.
Yes, you would do this during initialisation, when you load in your MD2.

A simple way to do this is to run through your face index arrays and just lift the geometric/texture coordinates they point at and put them in two new arrays. You wind up with a lot of duplication )probably where you don't need it), but it'll get it working for you quickly, and you can always implement a smarter algorithm later.

Hope that helps.

-Mezz

dopeflow
05-07-2003, 05:24 PM
Thanks mezz, im doing exactly that

texVB= new float[6*m_numTriangles];
for(i = 0; i < m_numTriangles; i++)
{
texVB[6*i+0]= m_st[m_triIndex[i].stIndex[0]].s;
texVB[6*i+1]= m_st[m_triIndex[i].stIndex[0]].t;
texVB[6*i+2]= m_st[m_triIndex[i].stIndex[2]].s;
texVB[6*i+3]= m_st[m_triIndex[i].stIndex[2]].t;
texVB[6*i+4]= m_st[m_triIndex[i].stIndex[1]].s;
texVB[6*i+5]= m_st[m_triIndex[i].stIndex[1]].t;
}

But does glTexPointer really expect 6 values for each triangles?

rgpc
05-07-2003, 05:52 PM
Ah... (*clunk*) That's your problem. TexPointer expects 2 values per vertex - It's not linked to the triangles, it's the vertices that count. So if you have 8 vertices and 12 triangles you need 8 st's.

Each VERTEX has it's own st coords...

dopeflow
05-07-2003, 06:58 PM
I think im close to a solution lol :P

Just to be sure

if my vertex array is like such

-----triangle0----- -----triangle1------
float, float, float, float, float, float etc

the texture array would be

-tri0 vert0- -tri0 vert1- -tri0 vert2-
float, float, float, float, float, float

right?
Thing is I use my triangle index to draw the triangles in the correct order, therefore I have to adjust the texture to match the drawing order

[This message has been edited by dopeflow (edited 05-07-2003).]

dopeflow
05-07-2003, 07:26 PM
Also in the md2 format, in the header theres an offset to the St's. So I load up the whole file in a buffer and then get to the beginning of the st's using the offset.

But the number of st's is different than the number of vertices. I dont know why they made it complicated like that in their format :P

rgpc
05-07-2003, 08:23 PM
OK, I am officially starting to get ticked off.

First thing you should do is go off, find a copy of the Red book and read up on DrawElements(), VertexPointer(), TexPointer() etc. as you are showing a lack of knowledge of how these work at their basic level.

Second thing you should get clear in your mind is the difference between indices and vertices. DrawElements takes a list of indices and you seem to be stuck on the idea that if you have a triangle then you will have texture coords for each of the indices (not each of the vertices).

Triangles do NOT have texture coords, vertices have texture coords. The number of elements in the array supplied in glVertexPointer() is the same as the number in glTexPointer() and is (most likely) different to the number in glDrawElements().

Finally, you should go to a site like www.GameTutorials.com (http://www.GameTutorials.com) or nehe.gamedev.net and read up on their tutorials relating to MD2 loading and display.

dopeflow
05-07-2003, 08:44 PM
Yea i know pretty much all that. The only thing im wondering about is that obviously you have to take the st's you load from the file and put them in the right order in your texture array. But Im just not sure how to do that.