Jump to content
  • Advertisement

Archived

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

Promit

MD2 textures are scrambled

This topic is 5221 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

I'm trying to load MD2 files, and geometry-wise everything is fine. Textures, however, are completely scrambled when I apply them; I think something's wrong in the calculations. Here's the relevant code; it's pretty close to the method used in OpenGL Game Programming.
//this section loads the coords and texture

	m_Tex = new CTexture;
	if( m_Tex->LoadBMP( TexName, false, 0 ) == true )
	{
		m_TexWidth = m_Tex->Width();
		m_TexHeight = m_Tex->Height();
	}

	//modified parameters for model textures

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

	m_TexCoords = new ModelTexCoord[Header->NumST];
	m_NumST = Header->NumST;

	//set texture pointer and calculate tex coords for all points

	St = (StIndex*) &Buffer[Header->OffsetST];
	St->Endian();

	for( i = 0; i < Header->NumST; i++ )
	{
		m_TexCoords[i].s = (float) St[i].s / (float) m_TexWidth;
		m_TexCoords[i].t = (float) St[i].t / (float) m_TexHeight;
	}
	TriIndex = new SubMesh[Header->NumTris];

	m_NumTriangles = Header->NumTris;
	m_TriIndex = TriIndex;

	BufIndex = (SubMesh*) &Buffer[Header->OffsetTris];

	//create a tri list by going through each tri in each frame

	//amend to above: remind me to not use i and j as indices, and to NOT WRITE THEM BACKWARDS

	//OF THE EXAMPLE CODE ON ****ING PURPOSE!

	for( i = 0; i < m_NumFrames; i++ )
	{
		for( j = 0; j < m_NumTriangles; j++ )
		{
			BufIndex[j].Endian();

			TriIndex[j].MeshIndex[0] = BufIndex[j].MeshIndex[0];
			TriIndex[j].MeshIndex[1] = BufIndex[j].MeshIndex[1];
			TriIndex[j].MeshIndex[2] = BufIndex[j].MeshIndex[2];

			TriIndex[j].StIndex[0] = BufIndex[j].StIndex[0];
			TriIndex[j].StIndex[1] = BufIndex[j].StIndex[1];
			TriIndex[j].StIndex[2] = BufIndex[j].StIndex[2];
		}
	}

And the rendering code:
	for( i = 0; i < m_NumTriangles; i++ )
	{
            //Vertex is determined by calculations here

		//render tri

		glTexCoord2fv( &m_TexCoords[m_TriIndex[i].StIndex[0]].s );
		glVertex3fv( Vertex[0].fp );

		glTexCoord2fv( &m_TexCoords[m_TriIndex[i].StIndex[1]].s );
		glVertex3fv( Vertex[1].fp );

		glTexCoord2fv( &m_TexCoords[m_TriIndex[i].StIndex[2]].s );
		glVertex3fv( Vertex[2].fp );
        }
Things to note: CTexture and all the texturing code is seperate from this, and is guaranteed to work perfectly. Geometry for all the frames is absolutely perfect, so there's no problems there. I've changed the names of a couple data structures her, but I think it's fairly clear; if not, feel free to ask for clarification. The problem is simple: The texture coordinates are wrong, and the model's texture is scrambled as a result. Help, please? [EDIT] Here's a Screenshot. [edited by - Promit on February 29, 2004 3:33:04 PM] [edited by - Promit on February 29, 2004 3:37:21 PM] [edited by - Promit on February 29, 2004 3:38:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
As I understand
m_TexCoords[m_TriIndex.StIndex[0]].s 
is a float and not an array, so your rendering code should look like this:


for( i = 0; i < m_NumTriangles; i++ )
{ glTexCoord2f( m_TexCoords[m_TriIndex[i].StIndex[0]].s,m_TexCoords[m_TriIndex[i].StIndex[0]].t ); glVertex3fv( Vertex[0].fp ); glTexCoord2f( m_TexCoords[m_TriIndex[i].StIndex[1]].s,m_TexCoords[m_TriIndex[i].StIndex[1]].t ); glVertex3fv( Vertex[1].fp ); glTexCoord2f( m_TexCoords[m_TriIndex[i].StIndex[2]].s,m_TexCoords[m_TriIndex[i].StIndex[2]].t ); glVertex3fv( Vertex[2].fp );
}




"C lets you shoot yourself in the foot rather easily. C++ allows you to reuse the bullet!"



[edited by - CPPMaster Poppet on February 29, 2004 4:10:19 PM]

Share this post


Link to post
Share on other sites
It is a float, but I took advantage of the fact that s and t are tightly packed together and so I can treat them like an array.

More importantly, doing things your way didn''t help.

Share this post


Link to post
Share on other sites
Ok, most recent research on my part suggests that the problem is in loading rather than rendering.

I switched to using GL command based rendering, but textures are still screwed up in exactly the same way--it follows that rendering is not the problem, but rather, the original calculations for texture coords.

Share this post


Link to post
Share on other sites
More accurately is that the t texture coordinate is upside down, which is as you found out, not necessarily documented very well and can be annoying.

Share this post


Link to post
Share on other sites
quote:
Original post by Cor
More accurately is that the t texture coordinate is upside down, which is as you found out, not necessarily documented very well and can be annoying.


So the list of things wrong with MD2s as of now:
t coord for textures is upside down
They switched Y and Z (same with BSPs)
MD2s are rotated around the Y axis 90 deg as well
Too many bloody levels of indices
Double size file because keeps geometry in both strip/fan form and tri list form.

Such a simple format, yet so convoluted...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!