• Advertisement

Archived

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

Texturing not working, at all

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

This has been driving me nuts for a few days now, i've tried sooo many combinations of code, yet opengl just laughs at me every time. I have 2 classes, one for holding a md2 mesh, and one for holding a texture. (both taken from tutorials, so the code *should* work.)
// instantiation code, at the top of the main.cpp file.
Texture DudeTex;
MD2Mesh Dude;
    
both the mesh and texture load fine (according to to the output in my log file.)
// loading code, in the Init() function inside main.cpp
DudeTex.LoadBMP("Media/Players/Male/skin.bmp", GL_LINEAR, GL_LINEAR);
Dude.Load("Media/Players/Male/tris.md2");
Dude.SetState(RUN);
    
everything works ok except for the texture of the mesh. the heightmap(untextured) works, the text works, the camera moves around ok, and apart from the fact that its untextured, the md2 mesh renders and animates fine. // render code.
        
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer

	glLoadIdentity();	

	glColor3f(0.5f, 0.5f, 0.5f);
	glTranslatef(-20.0f,15.0f,-40.0f);
	if(g_Camera.m_vPosition.x < 0 || g_Camera.m_vPosition.z < 0)
		height = 0;
	else
		height = g_Map.Height(g_HeightMap, g_Camera.m_vPosition.x, g_Camera.m_vPosition.y);
	
	myFont.Print("Pos:   x:%f  y:%f  z:%f  height:%d  state:%d  Tex1:%d ", g_Camera.m_vPosition.x, g_Camera.m_vPosition.y, g_Camera.m_vPosition.z, height, Dude.curState, DudeTex.ID);
		
	// Give openGL our camera position, then camera view, then camera up vector

	gluLookAt(g_Camera.m_vPosition.x, g_Camera.m_vPosition.y,  g_Camera.m_vPosition.z,	
			  g_Camera.m_vView.x,	  g_Camera.m_vView.y,      g_Camera.m_vView.z,	
			  g_Camera.m_vUpVector.x, g_Camera.m_vUpVector.y,  g_Camera.m_vUpVector.z);

	
	glColor3f(0.25f, 0.75f, 1.0f);
	glScalef(0.25f, 0.25f, 0.25f);
	g_Map.RenderHeightMap(g_HeightMap);

	glTranslatef(150.0f, 200.0f, -50.0f);

        // This is where my texture ISN'T getting applied (i think...)

	glBindTexture(GL_TEXTURE_2D, DudeTex.ID);
	Dude.Animate(Dude.stateStart, Dude.stateEnd, 0.7f);

	SwapBuffers(g_hDC);	
    
  
here is the code for the Texture and MD2Mesh classes. (i've left out the animation defines, cause the animation works )
     
// MD2Mesh.h

class MD2Mesh
{
private:	
		int numGlCommands;
		long* glCommands;

		int numTriangles;

public:
		int stateStart;
		int stateEnd;
		int curState;

		int frameSize;
		int numFrames;
		char* frames;

		int currentFrame;
		int nextFrame;
		int endFrame;
		float interpolation;
	
	bool Load(char* filename);
	void Render(int numFrame);
	void Animate(int startFrame, int EndFrame, float Interpolation);
	void SetState(int state);
	void NextState(bool forward);

	MD2Mesh() : stateStart(IDLE1_START), stateEnd(IDLE1_END),
			numGlCommands(0), frameSize(0), numFrames(0), 
			currentFrame(IDLE1_START), nextFrame(currentFrame+1),
			endFrame(IDLE1_END), interpolation(0.0f), curState(IDLE1)
		{	}

	~MD2Mesh()
		{	
		if(glCommands)
			delete [] glCommands;
		if(frames)
			delete [] frames;
		}
};





// MD2Mesh.cpp


bool MD2Mesh::
	Load(char* filename)
	{
	FILE* file;
	MD2_HEADER header;

	//Open the .md2 model file

	if((file= fopen(filename, "rb"))==NULL)
		{
		g_Log.Output("Could not load %s correctly", filename);
		return false;
		}
	
	//Read in the header

	fread(&header, sizeof(MD2_HEADER), 1, file);

	if(header.magic!= 844121161)
		{
		g_Log.Output("%s is not a valid .md2", filename);
		return false;
		}
	if(header.version!=8)
		{
		g_Log.Output("%s is not a valid .md2", filename);
		return false;
		}

	//Create space for the frame information

	frames= new char[header.frameSize*header.numFrames];
	
	//Check to see if it allocated correctly

	if(frames==NULL)
		return false;

	//Zoom to the correct spot in the file

	fseek(file, header.offsetFrames, SEEK_SET);
	fread(frames, header.frameSize*header.numFrames, 1, file);

	//Create space for the GL command information (whether or not to use tri strips, or tri fans)

	glCommands= new long [header.numGlCommands*sizeof(long)];

	//Check to see if it allocated correctly

	if(glCommands==NULL)
		return false;

	//Zoom to the correct spot in the file

	fseek(file,		  header.offsetGlCommands, SEEK_SET);
	fread(glCommands, header.numGlCommands*sizeof(long), 1, file);

	//Move the important information from the header, to the permanent class info.

	numFrames	 = header.numFrames;
	numGlCommands= header.numGlCommands;
	frameSize	 = header.frameSize;
	numTriangles = header.numTriangles;

	//Close 'da friggin file mon

	fclose(file);	

	//Lets get outta here

	g_Log.Output("Loaded %s correctly", filename);
	return true;
	}

void MD2Mesh::
	Render(int numFrame)
	{
	static MD2_MODELVERTEX vertList[100];

	MD2_FRAME_PTR	currentFrame;
	VERTEX			v1;
	long*			command;
	float			texcoord[2];
	int				loop;
	int				vertIndex;
	int				type;
	int				numVertex;
	int				index;

	//Get the current frame and gl command information

	currentFrame= (MD2_FRAME*) ((char*)frames+frameSize*numFrame);
	command		= glCommands;

	//Make sure that the command doesn't equal 0, and if it doesn't lets start rendering!

	while((*command)!=0)
		{
		if(*command>0)	//This is a triangle strip

			{
			numVertex= *command; 
			command++; 
			type= 0;
			}
		else			//This is a triangle fan

			{
			numVertex= - *command; 
			command++; 
			type= 1;
			}

		if(numVertex<0)
			numVertex= -numVertex;

		index= 0;
		//Fill the vertex list information

		for(loop=0; loop<numVertex; loop++)
			{
			vertList[index].u= *((float*)command); 
			command++;
			vertList[index].v= *((float*)command); 
			command++;

			vertIndex= *command; 
			command++;

			vertList[index].x= ( (currentFrame->vertices[vertIndex].vertex[0]* 
								  currentFrame->scale[0])+ 
								  currentFrame->translate[0]);
			vertList[index].z= -((currentFrame->vertices[vertIndex].vertex[1]* 
								  currentFrame->scale[1])+ 
								  currentFrame->translate[1]);
			vertList[index].y= ( (currentFrame->vertices[vertIndex].vertex[2]* 
								  currentFrame->scale[2])+ 
								  currentFrame->translate[2]);
			index++;
			}

		//If the .md2 was optimized for use with triangle strips...

		if(type==0)
			{
			glBegin(GL_TRIANGLE_STRIP);
			for(loop=0; loop<index; loop++)
				{
				v1.vertex[0]=(vertList[loop].x);
				v1.vertex[1]=(vertList[loop].y);
				v1.vertex[2]=(vertList[loop].z);

				texcoord[0]= vertList[loop].u;
				texcoord[1]= vertList[loop].v;

				glTexCoord2fv(texcoord);
				glVertex3fv(v1.vertex);
				}
			glEnd();
			}

		//If the .md2 was made for use with triangle fans...

		else
			{
			glBegin(GL_TRIANGLE_FAN);
			for(loop=0; loop<index; loop++)
				{
				v1.vertex[0]=(vertList[loop].x);
				v1.vertex[1]=(vertList[loop].y);
				v1.vertex[2]=(vertList[loop].z);

				texcoord[0]= vertList[loop].u;
				texcoord[1]= vertList[loop].v;

				glTexCoord2fv(texcoord);
				//v1.SendToOGL();

				glVertex3fv(v1.vertex);
				}
			glEnd();
			}
		}
	
	}

  
          

  
      
// Texture.h

#define BITMAP_ID 0x4D42
class Texture
{

public:
		GLubyte* data;			//Image data (up to 32 bits)

		unsigned int bpp;		//Image color depth in bits per pixel

		unsigned int width;		//Image width

		unsigned int height;	//Image height

		GLuint ID;//unsigned int ID;		//Texture ID used to select a texture


	bool LoadTGA(char* filename, GLfloat minFilter, GLfloat maxFilter);
	bool LoadBMP(char* filename, GLfloat minFilter, GLfloat maxFilter);

	Texture()
		{	}

	~Texture()
		{	
		if(data)
			delete data;	
		}
};

// Texture.cpp


bool Texture::
	LoadTGA(char* filename, GLfloat minFilter, GLfloat maxFilter)	
	{    
	GLubyte		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};	//Uncompressed TGA header

	GLubyte		TGAcompare[12];					//Used to compare TGA header

	GLubyte		header[6];						//The first six useful bytes from the header

	GLuint		bytesPerPixel;					//Holds the bpp of the TGA

	GLuint		imageSize;						//Used to store image size while in RAM

	GLuint		temp;							//Temp variable

	GLuint		type=GL_RGBA;					//Set the default OpenGL mode to RBGA (32 BPP)


	FILE* file = fopen(filename, "rb");			// Open The TGA File


	if(file==NULL													   ||	// Does File Even Exist?

	   fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||	// Are There 12 Bytes To Read?

	   memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0			   ||	// Does The Header Match What We Want?

	   fread(header,1,sizeof(header),file)!=sizeof(header))					// If So Read Next 6 Header Bytes

		{
		if(file==NULL)							// Did The File Even Exist? *Added Jim Strong*

			{
			g_Log.Output("%s does not exist.", filename);
			return false;							
			}
		else
			{
			fclose(file);						// If anything failed, close the file

			g_Log.Output("Could not load %s correctly, general failure.", filename);
			return false;						
			}
		}

	width = header[1] * 256 + header[0];		// Determine The TGA Width	(highbyte*256+lowbyte)

	height= header[3] * 256 + header[2];		// Determine The TGA Height	(highbyte*256+lowbyte)

    
 	if(width	<=0	||							// Is The Width Less Than Or Equal To Zero

	   height<=0	||							// Is The Height Less Than Or Equal To Zero

		(header[4]!=24 && header[4]!=32))		// Is The TGA 24 or 32 Bit?

		{
		fclose(file);							// If Anything Failed, Close The File

		g_Log.Output("%s's height or width is less than zero, or the TGA is not 24 or 32 bits.", filename);
		return false;							
		}

	bpp	 = header[4];							// Grab The TGA's Bits Per Pixel (24 or 32)

	bytesPerPixel= bpp/8;						// Divide By 8 To Get The Bytes Per Pixel

	imageSize	 = width*height*bytesPerPixel;	// Calculate The Memory Required For The TGA Data


	data= new GLubyte [imageSize];				// Reserve Memory To Hold The TGA Data


	if(data==NULL ||							// Does The Storage Memory Exist?

	   fread(data, 1, imageSize, file)!=imageSize)	// Does The Image Size Match The Memory Reserved?

		{
		if(data!=NULL)							// Was Image Data Loaded

			free(data);							// If So, Release The Image Data

		
		g_Log.Output("Storate memory for %s does not exist or is corrupted.", filename);
		
		fclose(file);							// Close The File

		return false;							// Return False

		}

	for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)		// Loop Through The Image Data

		{										// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)

		temp	 =data[i];						// Temporarily Store The Value At Image Data 'i'

		data[i]	 = data[i + 2];					// Set The 1st Byte To The Value Of The 3rd Byte

		data[i+2]= temp;						// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)

		}

	fclose (file);								//Close the file


	// Build A Texture From The Data

	glGenTextures(1, &ID);						//Generate OpenGL texture IDs


	glBindTexture(GL_TEXTURE_2D, ID);			//Bind the texture to a texture object 

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);	//Filtering for if texture is bigger than should be

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);	//Filtering for if texture is smaller than it should be

	
	if(bpp==24)									//Was the TGA 24 bpp?

		type=GL_RGB;							

	glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, data);

	g_Log.Output("Loaded %s correctly.", filename);
	return true;
	}

bool Texture::
	LoadBMP(char* filename, GLfloat minFilter, GLfloat maxFilter)
	{
	FILE* file;									//The file pointer

	BITMAPFILEHEADER	file_header;			//The bitmap file header

	BITMAPINFOHEADER	info_header;
	int					imageIdx = 0;			//Image index counter

	unsigned char		tempRGB;				//Swap variable


	//Open filename in read only binary mode

	file= fopen(filename, "rb");
	if(file==NULL)
		{
		g_Log.Output("Could not load %s", filename);
		return false;
		}

	//Read the bitmap file header

	fread(&file_header, sizeof(BITMAPFILEHEADER), 1, file);
	
	//Confirm that this is a bitmap by checking for the universal bitmap id

	if(file_header.bfType != BITMAP_ID)
		{
		fclose(file);
		g_Log.Output("%s is not a .bmp", filename);
		return false;
		}

	//Read the bitmap information header in

	fread(&info_header, sizeof(BITMAPINFOHEADER), 1, file);

	//Advance the file pointer to the beginning of the bitmap data

	fseek(file, file_header.bfOffBits, SEEK_SET);

	//Allocate the bitmap image data

	data= new unsigned char [info_header.biSizeImage];

	//Confirm memory allocation

	if(!data)
		{
		free(data);
		fclose(file);
		g_Log.Output("Could not allocate memory for %s", filename);
		return false;
		}

	//Read in the bitmap image data

	fread(data, 1, info_header.biSizeImage, file);

	//Make sure bitmap image data was read

	if(Texture::data==NULL)
		{
		fclose(file);
		g_Log.Output("Could not read %s", filename);
		return false;
		}
	
	Texture::width = info_header.biWidth;
	Texture::height= info_header.biWidth;
	Texture::bpp	  = info_header.biBitCount;

	//Swap the R and B values to get RGB since the bitmap color format is in BGR

	for(imageIdx = 0; imageIdx<(int)info_header.biSizeImage; imageIdx+=3)
		{
		tempRGB			= data[imageIdx];
		data[imageIdx]  = data[imageIdx + 2];
		data[imageIdx+2]= tempRGB;
		}

	//Close the file and return the bitmap image data

	fclose(file);

	//Build A Texture From The Data

	glGenTextures(1, &ID);						//Generate OpenGL texture IDs


	glBindTexture(GL_TEXTURE_2D, ID);			//Bind the texture to a texture object 


	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);	//Filtering for if texture is bigger than should be

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter);	//Filtering for if texture is smaller than it should be

	
	if(bpp==32)
		bpp=GL_RGBA;

	if(bpp==24)									//Was the TGA 24 bpp?

		bpp=GL_RGB;							

	glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, bpp, GL_UNSIGNED_BYTE, data);

	
	g_Log.Output("Loaded %s correctly", filename);
	g_Log.Output("\n");
	g_Log.Output("bpp=%d  width=%d  height=%d", bpp, width, height);
	g_Log.Output("%d", data);
	
	g_Log.Output("=============================================");
	
	return true;
	}


        
thanks to anyone in advance if they manage to get this far, let alone help [edited by - Smurfwow on April 17, 2002 12:30:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
In the main init section, have you turned ON texturing. Do this with: glEnable(GL_TEXTURE_2D);
I see you borrowed the code from Trent Polack, right?

Dan Fekete

Share this post


Link to post
Share on other sites

  • Advertisement