3ds.h

00001 /* -- 2007-05-07 -- 
00002  *  ardev - an augmented reality library for robot developers
00003  *  Copyright 2005-2007 - Toby Collett (ardev _at_ plan9.net.nz)
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
00018  *
00019  */
00020 #ifndef _3DS_H
00021 #define _3DS_H
00022 
00023 #include <iostream>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <GL/gl.h>                                      // Header File For The OpenGL32 Library
00027 #include <GL/glu.h>                                     // Header File For The GLu32 Library
00028 
00029 # include <string>
00030 # include <vector>
00031 
00032 using namespace std;
00033 
00034 typedef unsigned char BYTE;
00035 
00037 
00038 // This file includes all of the model structures that are needed to load
00039 // in a .3DS file.  If you intend to do animation you will need to add on
00040 // to this.  These structures only support the information that is needed
00041 // to load the objects in the scene and their associative materials.
00042 
00043 //#define MAX_TEXTURES 100                                // The maximum amount of textures to load
00044 
00045 // This is our 3D point class.  This will be used to store the vertices of our model.
00046 class CVector3 
00047 {
00048 public:
00049     float x, y, z;
00050 };
00051 
00052 // This is our 2D point class.  This will be used to store the UV coordinates.
00053 class CVector2 
00054 {
00055 public:
00056     float x, y;
00057 };
00058 
00059 // This is our face structure.  This is is used for indexing into the vertex 
00060 // and texture coordinate arrays.  From this information we know which vertices
00061 // from our vertex array go to which face, along with the correct texture coordinates.
00062 struct tFace
00063 {
00064     int vertIndex[3];           // indicies for the verts that make up this triangle
00065     int coordIndex[3];          // indicies for the tex coords to texture this face
00066 };
00067 
00068 // This holds the information for a material.  It may be a texture map of a color.
00069 // Some of these are not used, but I left them because you will want to eventually
00070 // read in the UV tile ratio and the UV tile offset for some models.
00071 struct tMaterialInfo
00072 {
00073     char  strName[255];         // The texture name
00074     char  strFile[255];         // The texture file name (If this is set it's a texture map)
00075     BYTE  color[3];             // The color of the object (R, G, B)
00076     int   texureId;             // the texture ID
00077     float uTile;                // u tiling of texture  (Currently not used)
00078     float vTile;                // v tiling of texture  (Currently not used)
00079     float uOffset;              // u offset of texture  (Currently not used)
00080     float vOffset;              // v offset of texture  (Currently not used)
00081 } ;
00082 
00083 // This holds all the information for our model/scene. 
00084 // You should eventually turn into a robust class that 
00085 // has loading/drawing/querying functions like:
00086 // LoadModel(...); DrawObject(...); DrawModel(...); DestroyModel(...);
00087 struct t3DObject 
00088 {
00089     int  numOfVerts;            // The number of verts in the model
00090     int  numOfFaces;            // The number of faces in the model
00091     int  numTexVertex;          // The number of texture coordinates
00092     int  materialID;            // The texture ID to use, which is the index into our texture array
00093     bool bHasTexture;           // This is TRUE if there is a texture map for this object
00094     char strName[255];          // The name of the object
00095     CVector3  *pVerts;          // The object's vertices
00096     CVector3  *pNormals;        // The object's normals
00097     CVector2  *pTexVerts;       // The texture's UV coordinates
00098     tFace *pFaces;              // The faces information of the object
00099 };
00100 
00101 // This holds our model information.  This should also turn into a robust class.
00102 // We use STL's (Standard Template Library) vector class to ease our link list burdens. :)
00103 struct t3DModel 
00104 {
00105     int numOfObjects;                   // The number of objects in the model
00106     int numOfMaterials;                 // The number of materials for the model
00107     vector<tMaterialInfo> pMaterials;   // The list of material information (Textures and colors)
00108     vector<t3DObject> pObject;          // The object list for our model
00109 };
00110 
00111 
00113 
00114 
00115 
00116 //>------ Primary Chunk, at the beginning of each file
00117 #define PRIMARY       0x4D4D
00118 
00119 //>------ Main Chunks
00120 #define OBJECTINFO    0x3D3D                // This gives the version of the mesh and is found right before the material and object information
00121 #define VERSION       0x0002                // This gives the version of the .3ds file
00122 #define EDITKEYFRAME  0xB000                // This is the header for all of the key frame info
00123 
00124 //>------ sub defines of OBJECTINFO
00125 #define MATERIAL      0xAFFF                // This stored the texture info
00126 #define OBJECT        0x4000                // This stores the faces, vertices, etc...
00127 
00128 //>------ sub defines of MATERIAL
00129 #define MATNAME       0xA000                // This holds the material name
00130 #define MATDIFFUSE    0xA020                // This holds the color of the object/material
00131 #define MATMAP        0xA200                // This is a header for a new material
00132 #define MATMAPFILE    0xA300                // This holds the file name of the texture
00133 
00134 #define OBJECT_MESH   0x4100                // This lets us know that we are reading a new object
00135 
00136 //>------ sub defines of OBJECT_MESH
00137 #define OBJECT_VERTICES     0x4110          // The objects vertices
00138 #define OBJECT_FACES        0x4120          // The objects faces
00139 #define OBJECT_MATERIAL     0x4130          // This is found if the object has a material, either texture map or color
00140 #define OBJECT_UV           0x4140          // The UV texture coordinates
00141 
00142 
00143 // Here is our structure for our 3DS indicies (since .3DS stores 4 unsigned shorts)
00144 struct tIndices {                           
00145 
00146     unsigned short a, b, c, bVisible;       // This will hold point1, 2, and 3 index's into the vertex array plus a visible flag
00147 };
00148 
00149 // This holds the chunk info
00150 struct tChunk
00151 {
00152     unsigned short int ID;                  // The chunk's ID       
00153     unsigned int length;                    // The length of the chunk
00154     unsigned int bytesRead;                 // The amount of bytes read within that chunk
00155 };
00156 
00157 // This class handles all of the loading code
00158 class CLoad3DS
00159 {
00160 public:
00161     CLoad3DS();                             // This inits the data members
00162 
00163     // This is the function that you call to load the 3DS
00164     bool Import3DS(t3DModel *pModel, const char *strFileName);
00165 
00166 private:
00167     // This reads in a string and saves it in the char array passed in
00168     int GetString(char *);
00169 
00170     // This reads the next chunk
00171     void ReadChunk(tChunk *);
00172 
00173     // This reads the next large chunk
00174     void ProcessNextChunk(t3DModel *pModel, tChunk *);
00175 
00176     // This reads the object chunks
00177     void ProcessNextObjectChunk(t3DModel *pModel, t3DObject *pObject, tChunk *);
00178 
00179     // This reads the material chunks
00180     void ProcessNextMaterialChunk(t3DModel *pModel, tChunk *);
00181 
00182     // This reads the RGB value for the object's color
00183     void ReadColorChunk(tMaterialInfo *pMaterial, tChunk *pChunk);
00184 
00185     // This reads the objects vertices
00186     void ReadVertices(t3DObject *pObject, tChunk *);
00187 
00188     // This reads the objects face information
00189     void ReadVertexIndices(t3DObject *pObject, tChunk *);
00190 
00191     // This reads the texture coodinates of the object
00192     void ReadUVCoordinates(t3DObject *pObject, tChunk *);
00193 
00194     // This reads in the material name assigned to the object and sets the materialID
00195     void ReadObjectMaterial(t3DModel *pModel, t3DObject *pObject, tChunk *pPreviousChunk);
00196     
00197     // This computes the vertex normals for the object (used for lighting)
00198     void ComputeNormals(t3DModel *pModel);
00199 
00200     // This frees memory and closes the file
00201     void CleanUp();
00202     
00203     // The file pointer
00204     FILE *m_FilePointer;
00205     
00206     // These are used through the loading process to hold the chunk information
00207     tChunk *m_CurrentChunk;
00208     tChunk *m_TempChunk;
00209 };
00210 
00211 void CreateTexture(unsigned int textureArray[],char *strFileName,int textureID);
00212 
00213 
00214 #endif
00215 
00216 
00218 //
00219 // * QUICK NOTES * 
00220 // 
00221 // This file is created in the hopes that you can just plug it into your code
00222 // easily.  You will probably want to query more chunks though for animation, etc..
00223 //
00224 // 
00225 // Ben Humphrey (DigiBen)
00226 // Game Programmer
00227 // DigiBen@GameTutorials.com
00228 // Co-Web Host of www.GameTutorials.com
00229 //
00230 //

SourceForge.net Logo Generated on Sat May 12 15:25:43 2007 for ardev by doxygen 1.5.1