3D model slow down me game!

Started by
6 comments, last by BlimeyOReilly 20 years, 1 month ago
Ive just adapted the .3ds loader tutorial from: http://www.gametutorials.com/Tutorials/opengl/OpenGL_Pg4.htm to get a more interesting world going. Ive obtained the following model to demo the loader: http://www.3dmodelz.com/Free/downloads/downloads.php?action=file&id=80 Problem is the frame rate has dropped from 85fps (with a cube instead of a model) to just 22fps and thats just with one model! I load the model in a load() function which gets called with once with the constructor. The rest is in my Draw() function which gets constantly updated shown in the code below. Is there anything i can do about this?? or any reasons why this might be happening? Thanks for any help!!

// Since we know how many objects our model has, go through each of them.

		  for(int i = 0; i < g_3DModel.numOfObjects; i++)
		  {
		     // Make sure we have valid objects just in case. (size() is in the vector class)

			 if(g_3DModel.pObject.size() <= 0) break;

			 // Get the current object that we are displaying

			 t3DObject *pObject = &g_3DModel.pObject[i];
			
			 // Check to see if this object has a texture map, if so bind the texture to it.

			 if(pObject->bHasTexture) {

				 // Turn on texture mapping and turn off color

				 glEnable(GL_TEXTURE_2D);

				 // Reset the color to normal again

				 glColor3ub(255, 255, 255);

				 // Bind the texture map to the object by it''s materialID

				 glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
			 }
		     else {

				// Turn off texture mapping and turn on color

				glDisable(GL_TEXTURE_2D);
	
				// Reset the color to normal again

				glColor3ub(255, 255, 255);
		 	}

			// This determines if we are in wireframe or normal mode

			glBegin(GL_TRIANGLES);					// Begin drawing with our selected mode (triangles or lines)


			// Go through all of the faces (polygons) of the object and draw them

			for(int j = 0; j < pObject->numOfFaces; j++)
			{
				// Go through each corner of the triangle and draw it.

				for(int whichVertex = 0; whichVertex < 3; whichVertex++)
				{
					// Get the index for each point of the face

					int index = pObject->pFaces[j].vertIndex[whichVertex];
			
					// Give OpenGL the normal for this vertex.

					glNormal3f(pObject->pNormals[ index ].x, pObject->pNormals[ index ].y, pObject->pNormals[ index ].z);
				
					// If the object has a texture associated with it, give it a texture coordinate.

					if(pObject->bHasTexture) {

						// Make sure there was a UVW map applied to the object or else it won''t have tex coords.

						if(pObject->pTexVerts) {
							glTexCoord2f(pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
						}
					} else {

						// Make sure there is a valid material/color assigned to this object.

						// You should always at least assign a material color to an object, 

						// but just in case we want to check the size of the material list.

						// if the size is at least one, and the material ID != -1,

						// then we have a valid material.

						if(g_3DModel.pMaterials.size() && pObject->materialID >= 0) 
						{
							// Get and set the color that the object is, since it must not have a texture

							BYTE *pColor = g_3DModel.pMaterials[pObject->materialID].color;

							// Assign the current color to this model

							glColor3ub(pColor[0], pColor[1], pColor[2]);
						}
					}

					// Pass in the current vertex of the object (Corner of current face)

					glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
				}
			}

		glEnd();								// End the drawing

	}
Advertisement
How many textures does it use? And how many objects does the model contain? (Perhaps these are always equal; 3D is not my strong point.) Perhaps calling glEnable(GL_TEXTURE_2D) a few times is slow, and you should do all your textured rendering in one pass, then all your non-textured stuff afterwards. (Or the reverse.) I''d be tempted to split it into 2 functions, one for textured objects, one for non-textured ones.

You could try asking in the OpenGL forum too.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Do some culling of faces in your model rendering.

You can cull backfaces (faces you can''t see from the camera''s position) and this will probably cut the number of faces you have to render in half.

Go a google.com search on "backface culling".

botman
The problem is that you''re going through and setting your textures and drawing your faces one by one. I''m not very familiar with OpenGL, but you need to collect all triangles that use that same texture together and put then in a display list or vertex buffer or someting and only change the texture once before drawing each type.

tj963
tj963
Don''t use glBegin()/glEnd().

Switch to display lists. Alternatively if you know extensions, you can use VBOs.
My first question would be how many vertexes and faces does the model have and how old is your video card? The 3DS file is over 1MB so a drop in the frame rate might be expected.

Since the model doesn't change shape, you can speed up rendering significantly by using display lists or vertex arrays, etc. like GamerSg said.

tj963: The model is sorted by texture. Each object has 0 or 1 textures.

[edited by - JohnBolton on February 29, 2004 3:23:24 AM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
i had a quick look at the model. the 3ds file itself has almost 2mb and the archive contains 16 textures.

vertex count: ~42k
face count: ~80k
object count: 26
material count: 20 (including 16 textures)

now do the math

just for comparison: a quake model has about 1000-1500 faces.
Cheers for the help guys! i will look into your advice!

This topic is closed to new replies.

Advertisement