-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathModel3D.h
224 lines (173 loc) · 7.4 KB
/
Model3D.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Model3D.h: interface for the Model3D class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MODEL3D_H__1B2D0CFD_4ADD_4AC8_83EC_6C5CEA86ABB1__INCLUDED_)
#define AFX_MODEL3D_H__1B2D0CFD_4ADD_4AC8_83EC_6C5CEA86ABB1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <windows.h>
#include <math.h>
#include "GLee.h"
//#include <gl\gl.h> // Header File For The OpenGL32 Library
//#include "libraries\bmp\bmp.h" // replacement for glaux.h
#include <vector>
using namespace std;
// This is our 3D point class. This will be used to store the vertices
// of our model.
struct tVector3 {
float x, y, z;
};
// This is our 2D point class. This will be used to store the UV coordinates.
struct tVector2 {
float x, y;
};
// This is our face structure. This is is used for indexing into the vertex
// and texture coordinate arrays. From this information we know which vertices
// from our vertex array go to which face, along with the correct texture coordinates.
struct tFace
{
int vertIndex[3]; // indicies for the verts that make up this triangle
int coordIndex[3]; // indicies for the tex coords to texture this face
};
// This holds the information for a material. It may be a texture map of a color.
// Some of these are not used, but I left them because you will want to eventually
// read in the UV tile ratio and the UV tile offset for some models.
struct tMaterialInfo
{
char strName[255]; // The texture name
char strFile[255]; // The texture file name (If this is set it's a texture map)
BYTE color[3]; // The color of the object (R, G, B)
int texureId; // the texture ID
float uTile; // u tiling of texture (Currently not used)
float vTile; // v tiling of texture (Currently not used)
float uOffset; // u offset of texture (Currently not used)
float vOffset; // v offset of texture (Currently not used)
};
// This holds all the information for our model/scene.
// You should eventually turn into a robust class that
// has loading/drawing/querying functions like:
// LoadModel(...); DrawObject(...); DrawModel(...); DestroyModel(...);
struct t3DObject
{
int numOfVerts; // The number of verts in the model
int numOfFaces; // The number of faces in the model
int numTexVertex; // The number of texture coordinates
int materialID; // The texture ID to use, which is the index into our texture array
bool bHasTexture; // This is TRUE if there is a texture map for this object
char strName[255]; // The name of the object
tVector3 *pVerts; // The object's vertices
tVector3 *pNormals; // The object's normals
tVector2 *pTexVerts; // The texture's UV coordinates
tFace *pFaces; // The faces information of the object
};
// This holds our model information. This should also turn into a robust class.
// We use STL's (Standard Template Library) vector class to ease our link list burdens. :)
struct t3DModel
{
int numOfObjects; // The number of objects in the model
int numOfMaterials; // The number of materials for the model
vector<tMaterialInfo> pMaterials; // The list of material information (Textures and colors)
vector<t3DObject> pObject; // The object list for our model
};
//>------ Primary Chunk, at the beginning of each file
#define PRIMARY 0x4D4D
//>------ Main Chunks
#define OBJECTINFO 0x3D3D // This gives the version of the mesh and is found right before the material and object information
#define VERSION3DS 0x0002 // This gives the version of the .3ds file
#define EDITKEYFRAME 0xB000 // This is the header for all of the key frame info
//>------ sub defines of OBJECTINFO
#define MATERIAL 0xAFFF // This stored the texture info
#define OBJECT 0x4000 // This stores the faces, vertices, etc...
//>------ sub defines of MATERIAL
#define MATNAME 0xA000 // This holds the material name
#define MATDIFFUSE 0xA020 // This holds the color of the object/material
#define MATMAP 0xA200 // This is a header for a new material
#define MATMAPFILE 0xA300 // This holds the file name of the texture
#define OBJECT_MESH 0x4100 // This lets us know that we are reading a new object
//>------ sub defines of OBJECT_MESH
#define OBJECT_VERTICES 0x4110 // The objects vertices
#define OBJECT_FACES 0x4120 // The objects faces
#define OBJECT_MATERIAL 0x4130 // This is found if the object has a material, either texture map or color
#define OBJECT_UV 0x4140 // The UV texture coordinates
// Here is our structure for our 3DS indicies (since .3DS stores 4 unsigned shorts)
struct tIndices {
unsigned short a, b, c, bVisible; // This will hold point1, 2, and 3 index's into the vertex array plus a visible flag
};
// This holds the chunk info
struct tChunk
{
unsigned short int ID; // The chunk's ID
unsigned int length; // The length of the chunk
unsigned int bytesRead; // The amount of bytes read within that chunk
};
struct t3DModel;
struct t3DObject;
struct tMaterialInfo;
// This class handles all of the loading code
class CLoad3DS
{
public:
CLoad3DS(); // This inits the data members
// This is the function that you call to load the 3DS
bool Import3DS(t3DModel *pModel, char *strFileName);
private:
// This reads in a string and saves it in the char array passed in
int GetString(char *);
// This reads the next chunk
void ReadChunk(tChunk *);
// This reads the next large chunk
void ProcessNextChunk(t3DModel *pModel, tChunk *);
// This reads the object chunks
void ProcessNextObjectChunk(t3DModel *pModel, t3DObject *pObject, tChunk *);
// This reads the material chunks
void ProcessNextMaterialChunk(t3DModel *pModel, tChunk *);
// This reads the RGB value for the object's color
void ReadColorChunk(tMaterialInfo *pMaterial, tChunk *pChunk);
// This reads the objects vertices
void ReadVertices(t3DObject *pObject, tChunk *);
// This reads the objects face information
void ReadVertexIndices(t3DObject *pObject, tChunk *);
// This reads the texture coodinates of the object
void ReadUVCoordinates(t3DObject *pObject, tChunk *);
// This reads in the material name assigned to the object and sets the materialID
void ReadObjectMaterial(t3DModel *pModel, t3DObject *pObject, tChunk *pPreviousChunk);
// This computes the vertex normals for the object (used for lighting)
void ComputeNormals(t3DModel *pModel);
// This frees memory and closes the file
void CleanUp();
// The file pointer
FILE *m_FilePointer;
// These are used through the loading process to hold the chunk information
tChunk *m_CurrentChunk;
tChunk *m_TempChunk;
};
class Model3D
{
public:
Model3D();
virtual ~Model3D();
bool LoadModel(char* strFileName, char* strDirectory = NULL);
// Delete the model from the graphics card
void FreeDisplayList(void);
// Build
//
void ToggleViewMode(void);
private:
GLuint m_Textures[40];
GLuint m_OneTextureID;
GLuint m_OneDisplayID;
t3DModel m_3DModel;
int m_nViewMode;
//AUX_RGBImageRec* _LoadBMP(char* Filename);
//int _LoadFontTexture(char* strFileName, int TextureIDIndex);
// Quickly load an image onto the graphics card (default filter settings)
int _QuickLoadTexture(char* sFileName, int TextureIDIndex);
void _RenderModel(void);
// Build the Display List
void BuildDisplayList(void);
public:
// Render the Model (From a display list)
void RenderModel(void);
};
#endif // !defined(AFX_MODEL3D_H__1B2D0CFD_4ADD_4AC8_83EC_6C5CEA86ABB1__INCLUDED_)