Jump to content
  • Advertisement
Sign in to follow this  
dpadam450

OpenGL Loading/Texturing models into OpenGL

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

I am about to start texturing in opengl but I might have my friend start drawing up ideas/models for a game. Is it fairly easy to take model data and texture data from maya/3dsmax and put it into code? Like if there is a 300 polygon model, do I have to go through manually and type the data into opengl? That might not have made the most sense but like if everything face of a say 300 polygon model has been textured in maya, will I have to rewrite the texture data for everyface in opengl or do I just read from a .x file or something? thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster


I assume you mean take a .x file and somehow get all the facets laid out on one texture and then be able to use your tools to paint the texture (which wjen combined with the .x file will allow rendering of a painted model....
(this probably would include building the UV info into a updated .x file)


You want an 'unwrapper' utility which does the above. There are several tools available(but I dont recall their names) Try Googling keywords ( unwrapper UV tool utility ) etc...




Somebody wrote this somewhere (found it in my goodies info file):


wings3d is really an easy modeller to get ahold of (took me 15 min before I could use it well). It also has an unwrapper, .3ds and .x exporter and raytracer (yafray) plug in. Its really easy to learn, powefull enough for any model you want to do and has some great features. Plus its free w/ tons of support and tutorials. Good luck.

Share this post


Link to post
Share on other sites
You should not code any model by hand that is more complex than a cube (what could even be painful if it has enough texture effects attached). Always all people use a modeller application and import its data into the own application. 3ds is somewhat popular in this sense.

OpenGL lacks a file format for models/textures (or anything other), so it does not provide an importer, too. You have to do the job yourself. However, each and every OGL programmer has had this problem once ago, so I assume that it is possible to get some working code. Nevertheless, importing means not only to read the file but to translate its data into the internal data structure in use, so it is presumably that you _have_ to spend some time.

Another possibility is to use a modeller application that supports a scripting language, so you may be able to write an exporter yourself. The benefit may be to have an exported file format that fulfills your desires totally. Nevertheless you have to write an importer by your own, too, but it may become relatively simple (a 3ds importer is not such simple if considering texturing and animation).

Share this post


Link to post
Share on other sites
Essentially, here is the idea. Mind you the IDEA of what you want to do...


Learn about the file type you're dealing with! Learn what data structures it uses, learn how they work together.

Create a class of data structures that mirror that of the file.

Create a load function that memcpy()'s all the files data structures into your new ones...


Basically, what you do is create a new instance of your Model class, and use the load method your create in that class to load a new model for that new class you created...

I can post some snippets for you, this is working with an MS3D Model file, (.ms3d), I find it a very easy to work with Model type.

MS3D.H

#ifndef MS3D_H
#define MS3D_H
#include <windows.h>
#include <gl\glaux.h>
#include <vector>

class MS3DModel
{
public:

MS3DModel();

virtual ~MS3DModel();

struct Vertex
{
char BoneID;
float Location[3];
};

int NumVertices;
Vertex *Vertices;

struct Triangle
{
float VertexNormals[3][3];
float Textures1[3], Textures2[3];
int VertexIndices[3];
};

int NumTriangles;
Triangle *Triangles;

struct Mesh
{
int MaterialIndex;
int NumTriangles;
int *TriangleIndices;
};

int NumMeshes;
Mesh *Meshes;

struct Material
{
float Ambient[4], Diffuse[4], Specular[4], Emissive[4];
float Shininess;
GLuint Texture;
char *TextureFilename;
};

int NumMaterials;
Material *Materials;

bool LoadModelData( const char *filename );
void ReloadTextures();
void DrawMS3DModels();
};

#endif



MS3D.cpp


#include <windows.h> // Header File For Windows
#include <fstream.h>
#include "TextureManager.h"
#include "MS3D.h"

MS3DModel::MS3DModel()
{
NumMeshes = 0;
Meshes = NULL;
NumMaterials = 0;
Materials = NULL;
NumTriangles = 0;
Triangles = NULL;
NumVertices = 0;
Vertices = NULL;
}

MS3DModel::~MS3DModel()
{
int i;
for ( i = 0; i < NumMeshes; i++ )
delete[] Meshes.TriangleIndices;
for ( i = 0; i < NumMaterials; i++ )
delete[] Materials.TextureFilename;

NumMeshes = 0;//
if ( Meshes != NULL )
{
delete[] Meshes;
Meshes = NULL;
}

NumMaterials = 0;
if ( Materials != NULL )
{
delete[] Materials;
Materials = NULL;
}

NumTriangles = 0;
if ( Triangles != NULL )
{
delete[] Triangles;
Triangles = NULL;
}

NumVertices = 0;
if ( Vertices != NULL )
{
delete[] Vertices;
Vertices = NULL;
}
}

/*
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 MS3DModel::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 > 7 )
return false; // "Unhandled file version. Milkshape3D Version 1.3 through 1.7 is supported." :)

int nVertices = *( word* )pPtr;
NumVertices = nVertices;
Vertices = new Vertex[nVertices];
pPtr += sizeof( word );

int i;
for ( i = 0; i < nVertices; i++ )
{
MS3DVertex *pVertex = ( MS3DVertex* )pPtr;
Vertices.BoneID = pVertex->m_boneID;
memcpy( Vertices.Location, pVertex->m_vertex, sizeof( float )*3 );
pPtr += sizeof( MS3DVertex );
}

int nTriangles = *( word* )pPtr;
NumTriangles = nTriangles;
Triangles = 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( Triangles.VertexNormals, pTriangle->m_vertexNormals, sizeof( float )*3*3 );
memcpy( Triangles.Textures1, pTriangle->m_s, sizeof( float )*3 );
memcpy( Triangles.Textures2, t, sizeof( float )*3 );
memcpy( Triangles.VertexIndices, vertexIndices, sizeof( int )*3 );
pPtr += sizeof( MS3DTriangle );
}

int nGroups = *( word* )pPtr;
NumMeshes = nGroups;
Meshes = 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 );

Meshes.MaterialIndex = materialIndex;
Meshes.NumTriangles = nTriangles;
Meshes.TriangleIndices = pTriangleIndices;
}

int nMaterials = *( word* )pPtr;
NumMaterials = nMaterials;
Materials = new Material[nMaterials];
pPtr += sizeof( word );
for ( i = 0; i < nMaterials; i++ )
{
MS3DMaterial *pMaterial = ( MS3DMaterial* )pPtr;
memcpy( Materials.Ambient, pMaterial->m_ambient, sizeof( float )*4 );
memcpy( Materials.Diffuse, pMaterial->m_diffuse, sizeof( float )*4 );
memcpy( Materials.Specular, pMaterial->m_specular, sizeof( float )*4 );
memcpy( Materials.Emissive, pMaterial->m_emissive, sizeof( float )*4 );
Materials.Shininess = pMaterial->m_shininess;
Materials.TextureFilename = new char[strlen( pMaterial->m_texture )+1];
strcpy( Materials.TextureFilename, pMaterial->m_texture );
pPtr += sizeof( MS3DMaterial );
}

ReloadTextures();

delete[] pBuffer;

return true;
}

void MS3DModel::ReloadTextures()
{
for ( int i = 0; i < NumMaterials; i++ )
if ( strlen( Materials.TextureFilename ) > 0 )
Materials.Texture = LoadGLTexture( Materials.TextureFilename );
else
Materials.Texture = 0;
}




Check out nehe.gamedev.net for more info on MS3D Model loading, there is a whole tutorial there. I believe it is lesson 31.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!