Jump to content
  • Advertisement

Archived

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

khakionion

MS3D Loading/Textures

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

Okay, sorry for the post, but I've been trying to get this working for two days now, and wasn't able to find anything that answered my question on the forums. I'm loading a MS3D model, using NeHe's game tutorial 4 (Model Mania) as a template. I created a MS3DObject class, with Load() and Render() methods. I can get it to display the correct colors, but when I texture map any of the materials, the model doesn't display the texture map, just an icky color that (sometimes) resembles the dominant color of the texture. Here's my Load() method:
	bool Load(char* Path)
	{
		FILE * Handle;
		MS3DHeader Header;
		if((Handle = fopen(Path, "rb"))==NULL)
		{
			kError("Could not load file correctly");
			return false;
		}
		fread(&Header.IDLine , sizeof(char), 10, Handle);
		fread(&Header.Version, 1, sizeof(int), Handle);

		if(strncmp(Header.IDLine, "MS3D000000", 10)!=0)
		{
			kError("Invalid .ms3d");
			return false;
		}
		if(Header.Version!=3 && Header.Version!=4)
		{
			kError("Invalid .ms3d");
			return false;
		}
		
		//Read, allocate, and assign vertices

		fread(&NumVertices, sizeof(unsigned short), 1, Handle);
		Vertices= new MS3DVertex [NumVertices];
		for(int i=0; i < NumVertices; i++)
		{
			fread(&Vertices[i].Flags, sizeof(BYTE), 1, Handle);
			fread(Vertices[i].Position, sizeof(float), 3, Handle);
			fread(&Vertices[i].BoneID, sizeof(char), 1, Handle);
			fread(&Vertices[i].RefCount, sizeof(BYTE), 1, Handle);
		}

		//Read, allocate, and assign Triangles

		fread(&NumTriangles, sizeof(unsigned short), 1, Handle);
		Triangles = new MS3DTriangle[NumTriangles];
		for(int i=0; i<NumTriangles; i++)
		{
			fread(&Triangles[i].Flags, sizeof(unsigned short), 1, Handle);
			fread(Triangles[i].VertexIndices, sizeof(unsigned short), 3, Handle);
			fread(Triangles[i].VertexNormals[0], sizeof(float), 3, Handle);
			fread(Triangles[i].VertexNormals[1], sizeof(float), 3, Handle);
			fread(Triangles[i].VertexNormals[2], sizeof(float), 3, Handle);
			fread(Triangles[i].UCoords, sizeof(float), 3, Handle);
			fread(Triangles[i].VCoords, sizeof(float), 3, Handle);
			fread(&Triangles[i].SmoothingGroup, sizeof(unsigned char), 1, Handle);
			fread(&Triangles[i].GroupIndex, sizeof(unsigned char), 1, Handle);
		}
		fread(&NumGroups, sizeof(unsigned short), 1, Handle);
		Groups= new MS3DGroup[NumGroups];
		for(int i=0; i<NumGroups; i++)
		{
			fread(&Groups[i].Flags, sizeof(unsigned char), 1, Handle);
			fread(Groups[i].Name, sizeof(char),	32, Handle);
			fread(&Groups[i].NumTriangles,sizeof(unsigned short),1, Handle);
			Groups[i].TriangleIndices=new unsigned short [Groups[i].NumTriangles];
			fread(Groups[i].TriangleIndices, sizeof(unsigned short),Groups[i].NumTriangles,Handle);
			fread(&Groups[i].MaterialIndex, sizeof(char), 1, Handle);
		}
		fread(&NumMaterials, sizeof(unsigned short), 1, Handle);
		Materials = new MS3DMaterial[NumMaterials];
		for(int i=0;i<NumMaterials; i++)
		{
			fread(Materials[i].Name, sizeof(unsigned char), 32, Handle);
			fread(Materials[i].Ambient, sizeof(float), 4, Handle);
			fread(Materials[i].Diffuse, sizeof(float), 4, Handle);
			fread(Materials[i].Specular, sizeof(float), 4, Handle);
			fread(Materials[i].Emissive, sizeof(float), 4, Handle);
			fread(&Materials[i].Shininess, sizeof(float), 1, Handle);
			fread(&Materials[i].Transparency, sizeof(float), 1, Handle);
			fread(&Materials[i].Mode, sizeof(char), 1, Handle);
			fread(Materials[i].Texture, sizeof(char), 128, Handle);
			fread(Materials[i].Alphamap, sizeof(char), 128, Handle);
			if(!strlen(Materials[i].Texture))
			{
				Materials[i].Textured = false;
			}
			else
			{
                AUX_RGBImageRec * NewTexture;
				FILE * File = fopen(Materials[i].Texture,"r");
				if(File)
				{
					fclose(File);
					NewTexture = auxDIBImageLoad(Materials[i].Texture);
					glGenTextures(1, &Materials[i].ID);
					glBindTexture(GL_TEXTURE_2D, Materials[i].ID);
					glTexImage2D(GL_TEXTURE_2D, 0, 3, NewTexture->sizeX, NewTexture->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, NewTexture->data);
					glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
					glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
				}
				else
				{
					kError("Couldn't load texture file for .ms3d model");
				}
				free(NewTexture);
			}
		}
			fclose(Handle);
			return true;
	}
And here's my Render():
virtual void Render()
	{
		glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
		glScalef(0.75f,0.75f,0.75f);
		for(int loop1=0; loop1<NumGroups; loop1++ )
		{
			if(Materials[Groups[loop1].MaterialIndex].Textured)
			{
				glBindTexture(GL_TEXTURE_2D, Materials[Groups[loop1].MaterialIndex].ID);
			}
			// Draw As Regular Triangles, Since .MS3D's Aren't Optimized Like .MD2's

			glBegin(GL_TRIANGLES);
			for(int loop2=0; loop2<Groups[loop1].NumTriangles; loop2++)
			{
				int triangleIndex = Groups[loop1].TriangleIndices[loop2];
				const MS3DTriangle * tri= &Triangles[triangleIndex];
				// Loop Through The Triangle's Vertices, And Output Them!

				for(int loop3=0; loop3<3; loop3++)
				{
					int index= tri->VertexIndices[loop3];
					glNormal3fv( tri->VertexNormals[loop3]);
					if(Groups[loop1].MaterialIndex >= 0)
					{
						if(Materials[Groups[loop1].MaterialIndex].Textured)
						{
							glTexCoord2f(tri->UCoords[loop3], tri->VCoords[loop3]);
						}
						else
						{
							glColor4fv(Materials[Groups[loop1].MaterialIndex].Diffuse);
						}
					}
					else
					{
						glColor3f(rand()/10000,rand()/10000,rand()/10000);
					}
					glVertex3fv(Vertices[index].Position);
				}
			}
			glEnd();
		}
	}
I don't think I quite understand the concepts behind texture mapping in OpenGL. Is this covered in the Red Book? If so, where is that electronic version that is supposed to be readily available? I can't seem to find that, either... :S EDIT BTW, my texture is a 24-bit BMP that has 256x256 dimensions. EDIT2 Source tags got messed up. [edited by - Khakionion on June 16, 2003 7:15:08 PM] [edited by - Khakionion on June 16, 2003 7:17:31 PM]

Share this post


Link to post
Share on other sites
Advertisement
Im only going off the fact that you said there where strage blochs of colors on the model, perhapes your UV coords are bad, or some of the glTexCoords are not being used when they should be.

Share this post


Link to post
Share on other sites
quote:
Original post by Xero-X2
Im only going off the fact that you said there where strage blochs of colors on the model, perhapes your UV coords are bad, or some of the glTexCoords are not being used when they should be.


Well, it''s not blocks of color, just one color per textured model. That''s why I think it''s just not acknowledging the glTexCoords() calls at all...if that''s possible.

-------
Love conquers all....except CANCER

Share this post


Link to post
Share on other sites
Try removing your if statements and FORCE the program to use them even with out matrials or what ever, that will help you get to your answer.

Share this post


Link to post
Share on other sites
Looks like it was because of MS3D''s 3DS importer...I hate to blame it on something other than me, but as soon as I started loading files that had only been created in MS3D, everything worked like a charm. *sigh*

-------
Love conquers all....except CANCER

Share this post


Link to post
Share on other sites
Maybe I just missed it but you''re not inverting the m_t component of the texture coordinates. You supposed to do something like m_t = m_t - 1;

Share this post


Link to post
Share on other sites
Ah, geez. That too, huh? I must have read that a million times, but completely forgot to do it. Thanks, the textures look better now!

-------
Love conquers all....except CANCER

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!