Advertisement

Rendering Problems

Started by August 23, 2003 05:25 AM
0 comments, last by Alura 21 years, 6 months ago
I''m having some problems in the code for my mini game, i''m temporarily using the Games Tutorial 7 code for ms3d and tga loading, the problem is that it loads the files correctly but i can''t make it render the ms3ds or apply textures, any ideas? Here''s part of the code from Main.cpp:

int InitGL()										// All Setup For OpenGL Goes Here

{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);							//Enable two dimensional texture mapping

	glEnable(GL_DEPTH_TEST);							//Enable depth testing

	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);				// Black Background

	glClearDepth(1.0f);									// Depth Buffer Setup

	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA );
	Font1.Init();
	//Font1.Init();

	PlayerSetup();
	GUI.LoadTGA("Art/GUI.tga",GL_LINEAR,GL_LINEAR);
	Inquirer.Load("Art/inquirer.ms3d");
	InquirerSkin.LoadTGA("Art/inquirer.tga",GL_LINEAR,GL_LINEAR);

	Log.Output("System - initialization sucessfull");

	return 0;										// Initialization Went OK

}

int PlayerSetup(GLvoid)
{
	Player.x = 0.0f;
	Player.y = 0.0f;
	Player.z = 0.0f;
	Player.Hull = 30;
	Player.mode = 2;

	PALog.Output("Player - Player initialization sucessfull");

	return 0;
}

int DrawGLScene(GLvoid)									// Here''s Where We Do All The Drawing

{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glFlush();

	glEnable(GL_BLEND);
	glEnable(GL_TEXTURE_2D); 
	glColor4f(1.0f,1.0f,1.0f,1.0f);
	glBindTexture(GL_TEXTURE_2D, GUI.ID); // doesnt do anything :/

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-600.0f, 800.0f, -5.0f);				// Top Left

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f( 600.0f, 800.0f, -5.0f);				// Top Right

		glTexCoord2f(1.0f, 0.0f);
		glVertex3f( 600.0f,-800.0f, -5.0f);				// Bottom Right

		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-600.0f,-800.0f, -5.0f);				// Bottom Left

	glEnd();

	glEnable(GL_TEXTURE_2D);
	glTranslatef(0.0f, 0.0f, -155.0f);
	glShadeModel(GL_SMOOTH);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);	
	glBindTexture(GL_TEXTURE_2D, InquirerSkin.ID);// doesnt do anything :/

	Inquirer.Render(); // doesnt do anything :/


	glDisable(GL_TEXTURE_2D);

	return true;
}

And here''s the Render() and LoadTGA() code:


void MS3D::
	Render(void)
	{
	int loop1;
	int loop2;
	int loop3;

	//Draw by group

	for(loop1=0; loop1<numGroups; loop1++ )
		{
		//Draw as regular triangles, since .ms3d''s aren''t optimized like .md2''s

		glBegin(GL_TRIANGLES);
			for(loop2=0; loop2<groups[loop1].numTriangles; loop2++)
				{
				int triangleIndex		=  groups[loop1].triangleIndices[loop2];
				const MS3D_TRIANGLE* tri= &triangles[triangleIndex];

				//Loop through the triangle''s vertices, and output them!

				for(loop3=0; loop3<3; loop3++)
					{
					int index= tri->vertexIndices[loop3];

					glNormal3fv( tri->vertexNormals[loop3]);
					glTexCoord2f(tri->u[loop3], tri->v[loop3]);
					glVertex3fv(vertices[index].vertex);
					}
				}
		glEnd();
		}
	}

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*

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

			PALog.Output("Texture - 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

		PALog.Output("Texture - %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

		
		PALog.Output("Texture - Storage 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);

	PALog.Output("Texture - Loaded %s correctly.", filename);
	return true;
	}

So, as i said before, the problem is that it all works but the render doesn''t apear on the screen and that i can''t assign textures to a model. Any ideas?
Small update, now it draws the texture on the quad, but it still doesnt draws the ms3d, here''s a code update:

int PlayerSetup(GLvoid){	Player.x = 0.0f;	Player.y = 0.0f;	Player.z = 0.0f;	Player.Hull = 30;	Player.mode = 2;	PALog.Output("Player - Player initialization sucessfull");	return 0;}void DrawGUI(GLvoid){	glEnable(GL_BLEND);	glEnable(GL_TEXTURE_2D);	glColor4f(1.0f,1.0f,1.0f,1.0f);	glBindTexture(GL_TEXTURE_2D, GUI.ID);	glBegin(GL_QUADS);		glTexCoord2f(0, 512);		glVertex3f(- height, width, -5.0f);				// Top Left		glTexCoord2f(512, 512);		glVertex3f( height, width, -5.0f);				// Top Right		glTexCoord2f(512, 0);		glVertex3f( height,- width, -5.0f);				// Bottom Right		glTexCoord2f(0, 0);		glVertex3f(- height,- width, -5.0f);				// Bottom Left	glEnd();	glDisable(GL_BLEND);	glDisable(GL_TEXTURE_2D);}void LoadStuff(GLvoid){	GUI.LoadTGA("Art/GUI.tga",GL_LINEAR,GL_LINEAR);	Inquirer.Load("Art/inquirer.ms3d");	InquirerSkin.LoadTGA("Art/inquirer.tga",GL_LINEAR,GL_LINEAR);	Log.Output("System - All files loaded.");}void DrawFont(GLvoid){	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix	glLoadIdentity();							// Reset The Projection Matrix	glOrtho(0.0f,width,height,0.0f,-1.0f,1.0f);				// Create Ortho 640x480 View (0,0 At Top Left)	glMatrixMode(GL_MODELVIEW);						// Select The Modelview Matrix	glLoadIdentity();							//	glColor4f(1,1,1,1);	glEnable(GL_TEXTURE_2D);	Font1.PrintGL(5,580,"Hull - %d", Player.Hull);	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix	glLoadIdentity();							// Reset The Projection Matrix	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);	glMatrixMode(GL_MODELVIEW);						// Select The Modelview Matrix	glLoadIdentity();}void DrawCritters(GLvoid){	glEnable(GL_TEXTURE_2D);	glTranslatef(0.0f, 0.0f, -155.0f);	glShadeModel(GL_SMOOTH);	glBindTexture(GL_TEXTURE_2D, InquirerSkin.ID);	Inquirer.Render();		glDisable(GL_TEXTURE_2D);}void DrawMenu(GLvoid){}void DrawOther(GLvoid){	glDisable(GL_TEXTURE_2D);	glColor4f(1,0,0,1);	glBegin(GL_TRIANGLES);		glVertex3f( -1.0f, -1.0f, -15.0f);		glVertex3f( 0.0f, 1.0f, -15.0f);		glVertex3f( 1.0f,  -1.0f, -15.0f);	glEnd(); }int InitGL()										// All Setup For OpenGL Goes Here{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);							//Enable two dimensional texture mapping	glEnable(GL_DEPTH_TEST);							//Enable depth testing	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);				// Black Background	glClearDepth(1.0f);									// Depth Buffer Setup	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations    glEnable( GL_BLEND );    glBlendFunc( GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA );	Log.Output("System - GL initialization sucessfull.");		InitGame();	return 0;										// Initialization Went OK}int InitGame(GLvoid){	Font1.Init();	PlayerSetup();	LoadStuff();	Log.Output("System - Game initialization sucessfull.");	return 0;}int DrawGLScene(GLvoid)									// Here''s Where We Do All The Drawing{    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glMatrixMode( GL_MODELVIEW );    glLoadIdentity();    glFlush();	DrawFont();	DrawGUI();	DrawOther();	DrawCritters();			return true;}


Now, basicly DrawGUI works, but it makes about 4 pics of the same texture aplied to the quad, i think it has to do with the ortho view, and the ms3d still doesn''t draws, any ideas? :S

This topic is closed to new replies.

Advertisement