mElite  1.0
An Elite clone based on TextElite by Jan-Philipp Kappmeier and Melanie Schmidt.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Objects.cpp
Go to the documentation of this file.
1 
7 #include "Objects.h"
8 #include "LittleHelper.h"
9 #include "World.h"
10 #include "ObjectLoader3ds.h"
11 #include "VectorMath.h"
12 
13 using namespace std;
14 
20 Objects *Objects::m_objects = 0;
21 
30  if( m_objects == 0 )
31  m_objects = new Objects;
32  return m_objects;
33 }
34 
38 Objects::Objects() {
39  reserve(10);
40 }
41 
46  objectList.clear();
47  m_objects = 0;
48 }
49 
56 void Objects::loadObject( unsigned int object, string filename, float factor ) {
57  // if the size of the vector is not big enough to access object, resize it. therefore create enough
58  // space to access objectList[object], that means heighten size to object+1
59  if( object >= objectList.size() )
60  reserve( object + 1 );
61 
62  // free object, if another was assigned
63  if( objectList[object].object != -1 ) {
64  freeObject( object );
65  }
66 
67  // load new object. currently, only 3ds is usable. all files are identified by their ending
68  // the loader should test if the file contains data in the right type
69  if( LittleHelper::stringCmpi( filename.substr(filename.length() - 3, 3), "3ds" ) == 0) {
70  loadObject3DS( object, filename, factor );
71  }
72 }
73 
84 void Objects::loadObject3DS( unsigned int object, string filename, float factor ) {
85  ObjectInfo oi;
86  ObjectLoader3ds *model;
87 
88  oi.filename = filename;
89  oi.object = object;
90  model = new ObjectLoader3ds();
91  model->LoadModel( filename.c_str() );
92  oi.displayList = createDisplayList( model, factor );
93  delete model;
94  oi.view = Vector3( 0, 0, 1 );
95  objectList[object] = oi;
96 }
97 
107 void Objects::freeObject( unsigned int object ) {
108  if( object >= objectList.size() ) // try to access texture that is not in vector
109  return;
110  if( objectList[object].object == -1 ) // try to free object which is assigned to no object
111  return;
112 
113  glDeleteLists( objectList[object].displayList, 1 );
114 
115  objectList[object].object = -1;
116  objectList[object].displayList = 0;
117  objectList[object].view = Vector3( 0, 0, 1 );
118  objectList[object].filename = "";
119 }
120 
122 
130 void Objects::reserve( unsigned int objectCount ) {
131  if( objectCount <= objectList.size() )// new size is not bigger than the old one
132  return;
133 
134  size_t oldSize = objectList.size();
135 
136  objectList.reserve( objectCount );
137  objectList.resize ( objectCount );
138 
139  // initialize new texture space
140  for( size_t i=oldSize ; i < objectList.size(); i++ ) {
141  ObjectInfo oi;
142  objectList[i] = oi;
143  }
144 }
145 
150 GLuint Objects::getDisplayList ( unsigned int object ) {
151  if( (unsigned)object > objectList.size() )
152  return 0;
153 
154  return objectList[object].displayList;
155 }
156 
161 Vector3 Objects::getObjectView ( unsigned int object ) {
162  if( (unsigned)object > objectList.size() )
163  return Vector3( 0, 0, 0 );
164 
165  return objectList[object].view;
166 }
167 
169 
175 GLuint Objects::createDisplayList( ObjectLoader3ds * model, float factor ) {
176  GLuint list = glGenLists(1);
177  glNewList( list, GL_COMPILE ); // New Compiled box Display List
178  glPushAttrib( GL_ALL_ATTRIB_BITS );
179  glEnable( GL_COLOR_MATERIAL );
180  glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
181  // Loop through all meshes that are apart of the file
182  // and render them.
183  //glBindTexture( GL_TEXTURE_2D, m_texture );// textures are at present disabled!
184  glDisable( GL_TEXTURE_2D );
185  for(int i = 0; i < model->totalMeshes; i++) {
186  glBegin(GL_TRIANGLES);
187 
188  // Draw each triangle of this mesh.
189  for(unsigned int f = 0; f < model->meshList[i].totalFaces; f++) {
190  // Get pointers to make the below code cleaner.
191  stMesh *pMesh = &model->meshList[i];
192  stFace *pFace = pMesh->pFaces;
193  stTexCoord *pTexCoord = pMesh->pTexCoords;
194 
195  // If one or both or NULL, we got a problem.
196  if(!pMesh || !pFace) continue;
197 
198  // Draw the triangle.
199  glNormal3f(pFace[f].normal.x, pFace[f].normal.y,
200  pFace[f].normal.z);
201 
202  // set the color
203  int material = pFace[f].matId;
204 
205  stMaterial *pMaterial = &model->materialList[material];
206 
207  float color[] = {1,0,0,1};
208 
209  glColor3f((float) pMaterial->colorDiffuse.r / 255.0, (float) pMaterial->colorDiffuse.g / 255.0, (float) pMaterial->colorDiffuse.b / 255.0);
210  //glMaterialf( GL_FRONT, GLenum pname, TYPE param ) // only for shininess
211 
212  //glMaterialf( GL_FRONT, GL_SHININESS, 0.0);
213  //color[0] = (float) pMaterial->colorAmbient.r / 255.0;
214  //color[1] = (float) pMaterial->colorAmbient.g / 255.0;
215  //color[2] = (float) pMaterial->colorAmbient.b / 255.0;
216  //glMaterialfv( GL_FRONT, GL_AMBIENT, color );
217 
218  //color[0] = (float) pMaterial->colorDiffuse.r / 255.0;
219  //color[1] = (float) pMaterial->colorDiffuse.g / 255.0;
220  //color[2] = (float) pMaterial->colorDiffuse.b / 255.0;
221  //glMaterialfv( GL_FRONT, GL_DIFFUSE, color );
222 
223  //color[0] = (float) pMaterial->colorSpecular.r / 255.0;
224  //color[1] = (float) pMaterial->colorSpecular.g / 255.0;
225  //color[2] = (float) pMaterial->colorSpecular.b / 255.0;
226 
227  //color[0] = 1;
228  //color[1] = 0;
229  //color[2] = 1;
230 
231  // set speculor to black, it doesn't work correctly with other values -> why?
232  glMaterialfv( GL_FRONT, GL_SPECULAR, color );
233 
234 
235  // calculate normal
236  Vector3 p1;
237  p1.x = pMesh->pVertices[pFace[f].indices[0]].x*factor;
238  p1.y = pMesh->pVertices[pFace[f].indices[0]].y*factor;
239  p1.z = pMesh->pVertices[pFace[f].indices[0]].z*factor;
240  Vector3 p2;
241  p2.x = pMesh->pVertices[pFace[f].indices[1]].x*factor;
242  p2.y = pMesh->pVertices[pFace[f].indices[1]].y*factor;
243  p2.z = pMesh->pVertices[pFace[f].indices[1]].z*factor;
244  Vector3 p3;
245  p3.x = pMesh->pVertices[pFace[f].indices[2]].x*factor;
246  p3.y = pMesh->pVertices[pFace[f].indices[2]].y*factor;
247  p3.z = pMesh->pVertices[pFace[f].indices[2]].z*factor;
248 
249  Vector3 normale = VectorMath::normal(p1, p2, p3);
250 
251  glNormal3f(normale.x, normale.y, normale.z);
252 
253  if(pTexCoord) glTexCoord2f(pTexCoord[pFace[f].indices[0]].tu, pTexCoord[pFace[f].indices[0]].tv);
254  glVertex3f(pMesh->pVertices[pFace[f].indices[0]].x * factor,
255  pMesh->pVertices[pFace[f].indices[0]].y *factor,
256  pMesh->pVertices[pFace[f].indices[0]].z *factor);
257 
258  if(pTexCoord) glTexCoord2f(pTexCoord[pFace[f].indices[1]].tu, pTexCoord[pFace[f].indices[1]].tv);
259  glVertex3f(pMesh->pVertices[pFace[f].indices[1]].x * factor,
260  pMesh->pVertices[pFace[f].indices[1]].y * factor,
261  pMesh->pVertices[pFace[f].indices[1]].z * factor);
262 
263  if(pTexCoord) glTexCoord2f(pTexCoord[pFace[f].indices[2]].tu, pTexCoord[pFace[f].indices[2]].tv);
264  glVertex3f(pMesh->pVertices[pFace[f].indices[2]].x *factor,
265  pMesh->pVertices[pFace[f].indices[2]].y *factor,
266  pMesh->pVertices[pFace[f].indices[2]].z *factor);
267  }
268  glEnd();
269  }
270  glPopAttrib();
271  glEndList();
272  //listCreated = true;
273  return list;
274 }