Skip to main content
gamedev.net
🔒 Locked

Loading Multiple Objects?

Started by Darkan_Fireblade Apr 23, 2002 at 11:24 AM 0 replies 200+ views
Original Post
Darkan_Fireblade
Darkan_Fireblade
Hi guys i got my 3ds loader working! YAY! but now i was wondering about Loading Multiple Objects? here is the object loading code
  
//Here anre the includes

#include <WinMain.h>
#include <3ds.h>

//Here are the veriabols

HDC			hDC=NULL;		// Private GDI Device Context

HGLRC		hRC=NULL;		// Permanent Rendering Context

HWND		hWnd=NULL;		// Holds Our Window Handle

HINSTANCE	hInstance;		// Holds The Instance Of The Application


bool	keys[256];			// Array Used For The Keyboard Routine

bool	active=TRUE;		// Window Active Flag Set To TRUE By Default

bool	fullscreen=TRUE;	// Fullscreen Flag Set To Fullscreen Mode By Default


#define FILE_NAME  "face.3ds"
#define FILE_NAME2 "ball.3ds"							// This is the 3D file we will load.


UINT g_Texture[MAX_TEXTURES] = {0};						// This holds the texture info, referenced by an ID


CLoad3DS g_Load3ds;										// This is 3DS class.  This should go in a good model class.

t3DModel g_3DModel;										// This holds the 3D Model info that we load in


int   g_ViewMode	  = GL_TRIANGLES;					// We want the default drawing mode to be normal

bool  g_bLighting     = true;							// Turn lighting on initially

float g_RotateX		  = 0.0f;							// This is the current value at which the model is rotated

float g_RotationSpeed = 0.8f;							// This is the speed that our model rotates.  (-speed rotates left)


LRESULT	CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//This is the start of the functions incase you didnt know

void SceneInit(GLvoid)
{
	g_Load3ds.Import3DS(&g_3DModel, FILE_NAME2);		// Load our .3DS file into our model structure


	// Go through all the materials

	for(int i = 0; i < g_3DModel.numOfMaterials; i++)
	{
		// Check to see if there is a file name to load in this material

		if(strlen(g_3DModel.pMaterials[i].strFile) > 0)
		{
			CreateTexture(g_Texture, g_3DModel.pMaterials[i].strFile, i);			
		}

		// Set the texture ID for this material

		g_3DModel.pMaterials[i].texureId = i;
	}

	glEnable(GL_LIGHT0);								// Turn on a light with defaults set

	glEnable(GL_LIGHTING);								// Turn on lighting

	glEnable(GL_COLOR_MATERIAL);						// Allow color

}


WPARAM MainLoop()
{
	MSG msg;

	while(1)											// Do our infinate loop

	{													// Check if there was a message

		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        { 
			if(msg.message == WM_QUIT)					// If the message wasnt to quit

				break;
            TranslateMessage(&msg);						// Find out what the message does

            DispatchMessage(&msg);						// Execute the message

        }
		else											// if there wasn''t a message

		{ 
			DrawGLScene();							// Update the screen	

        } 
	}

	// When we are done, we need to free all the model data

	// We do this by walking through all the objects and freeing their information


	// Go through all the objects in the scene

	for(int i = 0; i < g_3DModel.numOfObjects; i++)
	{
		// Free the faces, normals, vertices, and texture coordinates.

		delete [] g_3DModel.pObject[i].pFaces;
		delete [] g_3DModel.pObject[i].pNormals;
		delete [] g_3DModel.pObject[i].pVerts;
		delete [] g_3DModel.pObject[i].pTexVerts;
	}

	KillGLWindow();											// Clean up and free all allocated memory


	return(msg.wParam);									// Return from the program

}

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

{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

	glLoadIdentity();

	gluLookAt(		0, 1.5f, 8,		0, 0.5f, 0,			0, 1, 0);

	glRotatef(g_RotateX, 0, 1.0f, 0);						// Rotate the object around the Y-Axis

	g_RotateX += g_RotationSpeed;

	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(g_ViewMode);					// 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

	}

	SwapBuffers(hDC);

}

int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance

					HINSTANCE	hPrevInstance,		// Previous Instance

					LPSTR		lpCmdLine,			// Command Line Parameters

					int			nCmdShow)			// Window Show State

{
	BOOL	done=FALSE;								// Bool Variable To Exit Loop


	// Ask The User Which Screen Mode They Prefer

	if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
		fullscreen=FALSE;							// Windowed Mode

	}

	// Create Our OpenGL Window

	if (!CreateGLWindow("Raja''s 3ds Loader",640,480,16,fullscreen))
	{
		return 0;									// Quit If Window Was Not Created

	}

	SceneInit();

	return MainLoop();							// Exit The Program

}
  
any ideas? Thanks in Advance Sir Darkan Fireblade
Sir Darkan Fireblade
Alexandre Brien
Alexandre Brien
easy, use arrays.
ex :

#define NUMBER_OF_MODELS 10 //if you have 10 models.
CLoad3DS g_Load3ds[NUMBER_OF_MODELS];

and then you do exactly the same thing but use
g_Load3ds[0]. .... for the first model, and so on.
In Construction : http://www.gdev.org

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.