multiple file loading. so fruturated! help!

This is a discussion on multiple file loading. so fruturated! help! within the Game Programming forums, part of the General Programming Boards category; ok. In my engine, i'm using the MS3D file loading code from a NeHe tutorial. problem is, the loader only ...

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070

    Question multiple file loading. so fruturated! help!

    ok. In my engine, i'm using the MS3D file loading code from a NeHe tutorial. problem is, the loader only loads and renders 1 file. I need to load and render multiple files at once. Iv'e spend the last 2 hours trying to figure out how to modify the loader to handle more than 1 file without much luck. Heres the code:

    model.cpp
    Code:
    /*
    	Model.cpp
    
    		Abstract base class for a model. The specific extended class will render the given model. 
    
    	Author:	Brett Porter
    	Email: brettporter@yahoo.com
    	Website: http://www.geocities.com/brettporter/
    	Copyright (C)2000, Brett Porter. All Rights Reserved.
    
    	This file may be used only as long as this copyright notice remains intact.
    */
    
    #include <windows.h>		// Header File For Windows
    #include <gl\gl.h>			// Header File For The OpenGL32 Library
    
    #include "Model.h"
    #include "Textures.h"
    #include "FrustumCulling.h"
    
    extern fCull _glFrustum;
    
    Model::Model()
    {
    	m_numMeshes = 0;
    	m_pMeshes = NULL;
    	m_numMaterials = 0;
    	m_pMaterials = NULL;
    	m_numTriangles = 0;
    	m_pTriangles = NULL;
    	m_numVertices = 0;
    	m_pVertices = NULL;
    }
    
    Model::~Model()
    {
    	int i;
    	for ( i = 0; i < m_numMeshes; i++ )
    		delete[] m_pMeshes[i].m_pTriangleIndices;
    	for ( i = 0; i < m_numMaterials; i++ )
    		delete[] m_pMaterials[i].m_pTextureFilename;
    
    	m_numMeshes = 0;
    	if ( m_pMeshes != NULL )
    	{
    		delete[] m_pMeshes;
    		m_pMeshes = NULL;
    	}
    
    	m_numMaterials = 0;
    	if ( m_pMaterials != NULL )
    	{
    		delete[] m_pMaterials;
    		m_pMaterials = NULL;
    	}
    
    	m_numTriangles = 0;
    	if ( m_pTriangles != NULL )
    	{
    		delete[] m_pTriangles;
    		m_pTriangles = NULL;
    	}
    
    	m_numVertices = 0;
    	if ( m_pVertices != NULL )
    	{
    		delete[] m_pVertices;
    		m_pVertices = NULL;
    	}
    }
    
    void Model::loadModelTextures()
    {
    	for ( int x = 0; x < m_numMaterials; x++ )
    	{
    		if ( strlen( m_pMaterials[x].m_pTextureFilename ) > 0 )
    			m_pMaterials[x].m_texture = CreateModelTexture( m_pMaterials[x].m_pTextureFilename );
    		else
    			m_pMaterials[x].m_texture = 0;
    	}
    }
    
    void Model::draw() 
    {
    	glActiveTextureARB		= (PFNGLACTIVETEXTUREARBPROC)		wglGetProcAddress("glActiveTextureARB");
    	glMultiTexCoord2fARB	= (PFNGLMULTITEXCOORD2FARBPROC)		wglGetProcAddress("glMultiTexCoord2fARB");
    
    	GLboolean texEnabled = glIsEnabled( GL_TEXTURE_2D );
    	// Draw by group
    	for ( int i = 0; i < m_numMeshes; i++ )
    	{
    		int materialIndex = m_pMeshes[i].m_materialIndex;
    		if ( materialIndex >= 0 )
    		{
    			glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient );
    			glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse );
    			glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular );
    			glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive );
    			glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess );
    
    			if ( m_pMaterials[materialIndex].m_texture > 0 )
    			{
    				glActiveTextureARB(GL_TEXTURE0_ARB);
    				glEnable( GL_TEXTURE_2D );
    				glBindTexture( GL_TEXTURE_2D, m_pMaterials[materialIndex].m_texture );
    			}
    			else
    				glDisable( GL_TEXTURE_2D );
    		}
    		else
    		{
    			// Material properties?
    			glDisable( GL_TEXTURE_2D );
    		}
    
    		glBegin( GL_TRIANGLES );
    		{
    			for ( int j = 0; j < m_pMeshes[i].m_numTriangles; j++ )
    			{
    				int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];
    				const Triangle* pTri = &m_pTriangles[triangleIndex];
    				
    				for ( int k = 0; k < 3; k++ )
    				{
    					int index = pTri->m_vertexIndices[k];
    					
    					glNormal3fv( pTri->m_vertexNormals[k] );
    					glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );
    					glVertex3fv( m_pVertices[index].m_location );
    				}
    			}
    		}
    		glEnd();
    	}
    
    	if ( texEnabled )
    		glEnable( GL_TEXTURE_2D );
    	else
    		glDisable( GL_TEXTURE_2D );
    
    }
    model.h
    Code:
    /*
    	Model.h
    
    		Abstract base class for a model. The specific extended class will render the given model. 
    
    	Author:	Brett Porter
    	Email: brettporter@yahoo.com
    	Website: http://www.geocities.com/brettporter/
    	Copyright (C)2000, Brett Porter. All Rights Reserved.
    
    	This file may be used only as long as this copyright notice remains intact.
    */
    
    #ifndef MODEL_H
    #define MODEL_H
    
    class Model
    {
    	public:
    		//	Mesh
    		struct Mesh
    		{
    			int m_materialIndex;
    			int m_numTriangles;
    			int *m_pTriangleIndices;
    		};
    
    		//	Material properties
    		struct Material
    		{
    			float m_ambient[4], m_diffuse[4], m_specular[4], m_emissive[4];
    			float m_shininess;
    			GLuint m_texture;
    			char *m_pTextureFilename;
    		};
    
    		//	Triangle structure
    		struct Triangle
    		{
    			float m_vertexNormals[3][3];
    			float m_s[3], m_t[3];
    			int m_vertexIndices[3];
    		};
    
    		//	Vertex structure
    		struct Vertex
    		{
    			char m_boneID;	// for skeletal animation
    			float m_location[3];
    		};
    
    	public:
    		Model();
    		virtual ~Model();
    
    		virtual bool loadModelData( const char *filename ) = 0;
    		void loadModelTextures();
    		void draw();
    		void reloadTextures();
    
    	protected:
    		//	Meshes used
    		int m_numMeshes;
    		Mesh *m_pMeshes;
    
    		//	Materials used
    		int m_numMaterials;
    		Material *m_pMaterials;
    
    		//	Triangles used
    		int m_numTriangles;
    		Triangle *m_pTriangles;
    
    		//	Vertices Used
    		int m_numVertices;
    		Vertex *m_pVertices;
    };
    
    #endif // ndef MODEL_H
    ms3d.cpp
    Code:
    /*
    	MilkshapeModel.cpp
    
    		Loads and renders a Milkshape3D model. 
    
    	Author:	Brett Porter
    	Email: brettporter@yahoo.com
    	Website: http://www.geocities.com/brettporter/
    	Copyright (C)2000, Brett Porter. All Rights Reserved.
    
    	This file may be used only as long as this copyright notice remains intact.
    */
    
    #include <windows.h>		// Header File For Windows
    #include <gl\gl.h>			// Header File For The OpenGL32 Library
    
    #include "ms3d.h"
    
    #include <fstream.h>
    
    MilkshapeModel::MilkshapeModel()
    {
    }
    
    MilkshapeModel::~MilkshapeModel()
    {
    }
    
    /* 
    	MS3D STRUCTURES 
    */
    
    // byte-align structures
    #ifdef _MSC_VER
    #	pragma pack( push, packing )
    #	pragma pack( 1 )
    #	define PACK_STRUCT
    #elif defined( __GNUC__ )
    #	define PACK_STRUCT	__attribute__((packed))
    #else
    #	error you must byte-align these structures with the appropriate compiler directives
    #endif
    
    typedef unsigned char byte;
    typedef unsigned short word;
    
    // File header
    struct MS3DHeader
    {
    	char m_ID[10];
    	int m_version;
    } PACK_STRUCT;
    
    // Vertex information
    struct MS3DVertex
    {
    	byte m_flags;
    	float m_vertex[3];
    	char m_boneID;
    	byte m_refCount;
    } PACK_STRUCT;
    
    // Triangle information
    struct MS3DTriangle
    {
    	word m_flags;
    	word m_vertexIndices[3];
    	float m_vertexNormals[3][3];
    	float m_s[3], m_t[3];
    	byte m_smoothingGroup;
    	byte m_groupIndex;
    } PACK_STRUCT;
    
    // Material information
    struct MS3DMaterial
    {
        char m_name[32];
        float m_ambient[4];
        float m_diffuse[4];
        float m_specular[4];
        float m_emissive[4];
        float m_shininess;	// 0.0f - 128.0f
        float m_transparency;	// 0.0f - 1.0f
        byte m_mode;	// 0, 1, 2 is unused now
        char m_texture[128];
        char m_alphamap[128];
    } PACK_STRUCT;
    
    //	Joint information
    struct MS3DJoint
    {
    	byte m_flags;
    	char m_name[32];
    	char m_parentName[32];
    	float m_rotation[3];
    	float m_translation[3];
    	word m_numRotationKeyframes;
    	word m_numTranslationKeyframes;
    } PACK_STRUCT;
    
    // Keyframe data
    struct MS3DKeyframe
    {
    	float m_time;
    	float m_parameter[3];
    } PACK_STRUCT;
    
    // Default alignment
    #ifdef _MSC_VER
    #	pragma pack( pop, packing )
    #endif
    
    #undef PACK_STRUCT
    
    bool MilkshapeModel::loadModelData( const char *filename )
    {
    	ifstream inputFile( filename, ios::in | ios::binary | ios::nocreate );
    	if ( inputFile.fail())
    		return false;	// "Couldn't open the model file."
    
    	inputFile.seekg( 0, ios::end );
    	long fileSize = inputFile.tellg();
    	inputFile.seekg( 0, ios::beg );
    
    	byte *pBuffer = new byte[fileSize];
    	inputFile.read( pBuffer, fileSize );
    	inputFile.close();
    
    	const byte *pPtr = pBuffer;
    	MS3DHeader *pHeader = ( MS3DHeader* )pPtr;
    	pPtr += sizeof( MS3DHeader );
    
    	if ( strncmp( pHeader->m_ID, "MS3D000000", 10 ) != 0 )
    		return false; // "Not a valid Milkshape3D model file."
    
    	if ( pHeader->m_version < 3 || pHeader->m_version > 4 )
    		return false; // "Unhandled file version. Only Milkshape3D Version 1.3 and 1.4 is supported." );
    
    	int nVertices = *( word* )pPtr; 
    	m_numVertices = nVertices;
    	m_pVertices = new Vertex[nVertices];
    	pPtr += sizeof( word );
    
    	int i;
    	for ( i = 0; i < nVertices; i++ )
    	{
    		MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
    		m_pVertices[i].m_boneID = pVertex->m_boneID;
    		memcpy( m_pVertices[i].m_location, pVertex->m_vertex, sizeof( float )*3 );
    		pPtr += sizeof( MS3DVertex );
    	}
    
    	int nTriangles = *( word* )pPtr;
    	m_numTriangles = nTriangles;
    	m_pTriangles = new Triangle[nTriangles];
    	pPtr += sizeof( word );
    
    	for ( i = 0; i < nTriangles; i++ )
    	{
    		MS3DTriangle *pTriangle = ( MS3DTriangle* )pPtr;
    		int vertexIndices[3] = { pTriangle->m_vertexIndices[0], pTriangle->m_vertexIndices[1], pTriangle->m_vertexIndices[2] };
    		float t[3] = { 1.0f-pTriangle->m_t[0], 1.0f-pTriangle->m_t[1], 1.0f-pTriangle->m_t[2] };
    		memcpy( m_pTriangles[i].m_vertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
    		memcpy( m_pTriangles[i].m_s, pTriangle->m_s, sizeof( float )*3 );
    		memcpy( m_pTriangles[i].m_t, t, sizeof( float )*3 );
    		memcpy( m_pTriangles[i].m_vertexIndices, vertexIndices, sizeof( int )*3 );
    		pPtr += sizeof( MS3DTriangle );
    	}
    
    	int nGroups = *( word* )pPtr;
    	m_numMeshes = nGroups;
    	m_pMeshes = new Mesh[nGroups];
    	pPtr += sizeof( word );
    	for ( i = 0; i < nGroups; i++ )
    	{
    		pPtr += sizeof( byte );	// flags
    		pPtr += 32;				// name
    
    		word nTriangles = *( word* )pPtr;
    		pPtr += sizeof( word );
    		int *pTriangleIndices = new int[nTriangles];
    		for ( int j = 0; j < nTriangles; j++ )
    		{
    			pTriangleIndices[j] = *( word* )pPtr;
    			pPtr += sizeof( word );
    		}
    
    		char materialIndex = *( char* )pPtr;
    		pPtr += sizeof( char );
    	
    		m_pMeshes[i].m_materialIndex = materialIndex;
    		m_pMeshes[i].m_numTriangles = nTriangles;
    		m_pMeshes[i].m_pTriangleIndices = pTriangleIndices;
    	}
    
    	int nMaterials = *( word* )pPtr;
    	m_numMaterials = nMaterials;
    	m_pMaterials = new Material[nMaterials];
    	pPtr += sizeof( word );
    	for ( i = 0; i < nMaterials; i++ )
    	{
    		MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
    		memcpy( m_pMaterials[i].m_ambient, pMaterial->m_ambient, sizeof( float )*4 );
    		memcpy( m_pMaterials[i].m_diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
    		memcpy( m_pMaterials[i].m_specular, pMaterial->m_specular, sizeof( float )*4 );
    		memcpy( m_pMaterials[i].m_emissive, pMaterial->m_emissive, sizeof( float )*4 );
    		m_pMaterials[i].m_shininess = pMaterial->m_shininess;
    		m_pMaterials[i].m_pTextureFilename = new char[strlen( pMaterial->m_texture )+1];
    		strcpy( m_pMaterials[i].m_pTextureFilename, pMaterial->m_texture );
    		pPtr += sizeof( MS3DMaterial );
    	}
    
    	delete[] pBuffer;
    
    	return true;
    }
    ms3d.h
    Code:
    /*
    	MilkshapeModel.h
    
    		Loads and renders a Milkshape3D model. 
    
    	Author:	Brett Porter
    	Email: brettporter@yahoo.com
    	Website: http://www.geocities.com/brettporter/
    	Copyright (C)2000, Brett Porter. All Rights Reserved.
    
    	This file may be used only as long as this copyright notice remains intact.
    */
    
    #ifndef MILKSHAPEMODEL_H
    #define MILKSHAPEMODEL_H
    
    #include "Model.h"
    
    class MilkshapeModel : public Model
    {
    	public:
    		/*	Constructor. */
    		MilkshapeModel();
    
    		/*	Destructor. */
    		virtual ~MilkshapeModel();
    
    		/*	
    			Load the model data into the private variables. 
    				filename			Model filename
    		*/
    		virtual bool loadModelData( const char *filename );
    };
    
    #endif // ndef MILKSHAPEMODEL_H
    in my init function:
    Code:
    	_peModel = new MilkshapeModel(); // Memory To Hold The Model
    	if ( _peModel->loadModelData( "LVL1.ms3d" ) == false )		// Loads The Model And Checks For Errors
    	{
    		MessageBox( NULL, "Couldn't load the specified model. File not found or file corrupted.  {OGLrendering.cpp | OGLrender::Init();}", "Error", MB_OK | MB_ICONERROR );
    	}
    	_peModel->loadModelTextures();
    and then to render, I call _peModel->draw();

    I'm completely stuck. Ive tried everything I could think of, and nothing works.
    Please help if you can! Thanx in advance!

    -psychopath
    Last edited by psychopath; 05-08-2005 at 01:38 PM.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Given the little information you give, I can only suggest creating multiple Model objects to allow multiple models to be rendered...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'd try and help, but you know, I already got 3 posts on the subject

  4. #4
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    ah, sorry. I'll try and elabourate a bit more. If it dosn't help, let me know

    Well, basicly, I need a model loading setup similar to OGL texture loading. As in, I need to load models into an array, and then tell the draw function which model from the array to render.

    Pseudo code:
    Code:
    void world::init()
    {
        model->LoadModel(modelArray, "model.ms3d", 12);
        model->LoadModel(modelArray, "model2.ms3d", 13);
    }
    void world::render()
    {
         model->Draw(12);
    }
    The problem is, I don't know how to add this capibility to the loader I have.

    Again, if you need me to elabourate any more, just let me know.

    -psychopath
    Last edited by psychopath; 05-09-2005 at 02:18 PM.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void world::init()
    {
        model1->LoadModel(modelArray, "model.ms3d", 12);
        model2->LoadModel(modelArray, "model2.ms3d", 13);
    }
    void world::render()
    {
         model1->Draw(12);
         model2->Draw(12);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    well, that would work fine, except for my purposes, having hardcoded path names with different model objects isn't going to work (sorry. I knew I forgot to mention somthing in the last post, lol). Thats why I posted the pseudo code the way I did, although your not a mind reader...or are you?? lol. Basicly, I need a model loading system, exactly like the OGL texture loading system.

    -psychopath
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print a File on Multiple Processors
    By Cell in forum Linux Programming
    Replies: 6
    Last Post: 03-25-2009, 10:39 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21