Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/graphics/importer/static_model.cc @ 10729

Last change on this file since 10729 was 10729, checked in by nicolasc, 17 years ago

minor fix

File size: 9.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15   2005-07-06: (Patrick) added new function buildTriangleList()
16*/
17
18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
19
20#include "static_model.h"
21
22#include "debug.h"
23#include <stdarg.h>
24
25
26
27
28/////////////
29/// MODEL ///
30/////////////
31ObjectListDefinition(StaticModel);
32
33/**
34 * @brief Creates a 3D-Model.
35 *
36 * assigns it a Name and a Type
37 */
38StaticModel::StaticModel(const std::string& modelName)
39    : data(new StaticModelData(modelName))
40{
41  this->registerObject(this, StaticModel::_objectList);
42  PRINTF(4)("new 3D-Model is being created\n");
43  this->setName(modelName);
44}
45
46StaticModel::StaticModel(const StaticModel& staticModel)
47    : data(staticModel.data)
48{
49  this->registerObject(this, StaticModel::_objectList);
50  this->setName(staticModel.getName());
51  this->updateBase();
52}
53
54
55/**
56 * @brief deletes an Model.
57 *
58 * Looks if any from model allocated space is still in use, and if so deleted it.
59 */
60StaticModel::~StaticModel()
61{
62  PRINTF(4)("Deleting Model ");
63
64  // mark this stuff as beeing deleted
65  this->pModelInfo.pVertices = NULL;
66  this->pModelInfo.pNormals = NULL;
67  this->pModelInfo.pTexCoor = NULL;
68  this->pModelInfo.pTriangles = NULL;
69}
70
71StaticModel& StaticModel::operator=(const StaticModel& model)
72{
73  this->data = model.data;
74  this->updateBase();
75  return *this;
76};
77
78
79/**
80 * @brief Finalizes an Object. This can be done outside of the Class.
81 */
82void StaticModel::finalize()
83{
84  this->extractMountPoints();
85  data->finalize();
86  this->updateBase();
87}
88
89void StaticModel::acquireData(const StaticModelData::Pointer& data)
90{
91  this->data = data;
92  this->updateBase();
93}
94
95
96
97/**
98 * extract the mount points from this file: looking at each group and checking if the group realy is a mountpoint marker
99 * if so get place and orientation
100 */
101void StaticModel::extractMountPoints()
102{
103//   printf("extracting MP...");
104  // go through all groups and check if they are mounts
105  std::vector<StaticModelData::Group>::iterator groupIt = this->data->getGroups().begin();
106  for( ; groupIt != this->data->getGroups().end(); groupIt++)
107  {
108    //PRINTF(0)("Found a MountPoint: %s\n", groupName.c_str());
109
110    // get the name of this group and check if it's a mout point identifier
111    std::string groupName = (*groupIt).name;
112    std::vector<Vector> vertices;
113
114    // check if the name has a "MP" prefix or else it won't work
115    if( groupName.find("MP.", 0) == std::string::npos){
116//       groupIt++;
117//       printf("Found MP:  ");
118      continue;
119    }
120
121    PRINTF(5)("Found a MountPoint: %s\n", groupName.c_str());
122
123    StaticModelData::Face triangle[3];
124
125    // now check if it is a mount point identifier
126    if( (*groupIt)._faces.size() != 11)
127    {
128      PRINTF(4)("the face count of %s is wrong, perhaps you missnamed this object or used the wrong mount point object (got %i faces)\n",
129                groupName.c_str(), (*groupIt)._faces.size());
130//       printf("wrong number of faces\n\n");
131//       groupIt++;
132      continue;
133    }
134
135    // now iterate through all faces
136    std::vector<StaticModelData::Face>::const_iterator faceIt = (*groupIt)._faces.begin();
137    for( int i = 0; faceIt < (*groupIt)._faces.end(); faceIt++)
138    {
139      if( (*faceIt)._elements.size() == 3)
140      {
141        triangle[i++] = (*faceIt);
142      }
143    }
144
145//     printf("confirmed  ");
146
147    Vector center;
148    Vector xAxis;
149    Vector yAxis;
150    Vector zAxis;
151    // now process all points
152//     printf("entering loop: ");
153    for( int i = 0; i < 3; i++)
154    {
155//       printf("%i ", i);
156      // convert the float vertices to vectors
157      Vector a( this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3],
158                this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3 + 1],
159                this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3 + 2]);
160//       printf("a");
161      Vector b( this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3],
162                this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3 + 1],
163                this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3 + 2]);
164//       printf("b");
165      Vector c( this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3],
166                this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3 + 1],
167                this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3 + 2]);
168//       printf("c  ");
169
170      Vector ab = a - b;
171      Vector ac = a - c;
172      Vector bc = b - c;
173
174      Vector  axis1;
175      Vector  axis2;
176      // now find the center point (the one with the 90degree angle)
177      if( fabs(ab.dot( ac)) < 0.0001)
178      {
179        center = a;
180        axis1 = b - a;
181        axis2 = c - a;
182      }
183      else if( fabs(ab.dot(bc)) < 0.0001)
184      {
185        center = b;
186        axis1 = a - b;
187        axis2 = c - b;
188      }
189      else if( fabs(bc.dot(ac)) < 0.0001)
190      {
191        center = c;
192        axis1 = b - c;
193        axis2 = a - c;
194      }
195
196
197      // get the longest axis (without defining a max() funciton :D
198      if( xAxis.len() < axis1.len() )
199        xAxis = axis1;
200      if( xAxis.len() < axis2.len())
201        xAxis = axis2;
202
203
204      // find the 2nd longest axis to be y-axis
205      if( xAxis.len() > axis1.len() && yAxis.len() < axis1.len())
206        yAxis = axis1;
207      if( xAxis.len() > axis2.len() && yAxis.len() < axis2.len())
208        yAxis = axis2;
209
210
211      // needed... no explanation here..
212      if( zAxis.len() == 0.)
213        zAxis = yAxis;
214
215      // find the shortest axis to be z-axis
216      if( yAxis.len() > axis1.len() )
217        zAxis = axis1;
218      if( yAxis.len() > axis2.len() )
219        zAxis = axis2;
220    }
221
222//     printf("adding MP\n");
223    // now add the mount point
224     this->addMountPoint( zAxis, yAxis, center, groupName);
225
226//     printf("removing item...");
227    // remove the group from the model list (mount points do not need to be drawn)
228//      std::vector<StaticModelData::Group>::iterator tmpIt =  groupIt;
229//      groupIt++;
230//     printf("   removing...");
231//      this->data->getGroups().erase(tmpIt);
232//     printf("   REMOVED\n");
233  }
234//   printf("done\n");
235}
236
237
238
239/**
240 * @brief adds a new Face
241 * @param faceElemCount the number of Vertices to add to the Face.
242 * @param type The information Passed with each Vertex
243*/
244bool StaticModel::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
245{
246  va_list itemlist;
247  va_start (itemlist, type);
248  bool retVal = this->data->addFace(faceElemCount, type, itemlist);
249  va_end(itemlist);
250  return retVal;
251}
252
253void StaticModel::updateBase()
254{
255  // write out the modelInfo data used for the collision detection!
256  this->pModelInfo.pVertices = &this->data->getVertices()[0];
257  this->pModelInfo.numVertices = this->data->getVertices().size();
258  this->pModelInfo.pNormals = &this->data->getNormals()[0];
259  this->pModelInfo.numNormals = this->data->getNormals().size();
260  this->pModelInfo.pTexCoor = &this->data->getTexCoords()[0];
261  this->pModelInfo.numTexCoor = this->data->getTexCoords().size();
262
263  this->pModelInfo.pTriangles = this->data->getTrianglesExt();
264  this->pModelInfo.numTriangles = this->data->getTriangles().size();
265}
266
267
268/**
269 *  Includes a default model
270 *
271 * This will inject a Cube, because this is the most basic model.
272 */
273void StaticModel::cubeModel()
274{
275  this->addVertex (-0.5, -0.5, 0.5);
276  this->addVertex (0.5, -0.5, 0.5);
277  this->addVertex (-0.5, 0.5, 0.5);
278  this->addVertex (0.5, 0.5, 0.5);
279  this->addVertex (-0.5, 0.5, -0.5);
280  this->addVertex (0.5, 0.5, -0.5);
281  this->addVertex (-0.5, -0.5, -0.5);
282  this->addVertex (0.5, -0.5, -0.5);
283
284  this->addVertexTexture (0.0, 0.0);
285  this->addVertexTexture (1.0, 0.0);
286  this->addVertexTexture (0.0, 1.0);
287  this->addVertexTexture (1.0, 1.0);
288  this->addVertexTexture (0.0, 2.0);
289  this->addVertexTexture (1.0, 2.0);
290  this->addVertexTexture (0.0, 3.0);
291  this->addVertexTexture (1.0, 3.0);
292  this->addVertexTexture (0.0, 4.0);
293  this->addVertexTexture (1.0, 4.0);
294  this->addVertexTexture (2.0, 0.0);
295  this->addVertexTexture (2.0, 1.0);
296  this->addVertexTexture (-1.0, 0.0);
297  this->addVertexTexture (-1.0, 1.0);
298
299  this->addVertexNormal (0.0, 0.0, 1.0);
300  this->addVertexNormal (0.0, 0.0, 1.0);
301  this->addVertexNormal (0.0, 0.0, 1.0);
302  this->addVertexNormal (0.0, 0.0, 1.0);
303  this->addVertexNormal (0.0, 1.0, 0.0);
304  this->addVertexNormal (0.0, 1.0, 0.0);
305  this->addVertexNormal (0.0, 1.0, 0.0);
306  this->addVertexNormal (0.0, 1.0, 0.0);
307  this->addVertexNormal (0.0, 0.0, -1.0);
308  this->addVertexNormal (0.0, 0.0, -1.0);
309  this->addVertexNormal (0.0, 0.0, -1.0);
310  this->addVertexNormal (0.0, 0.0, -1.0);
311  this->addVertexNormal (0.0, -1.0, 0.0);
312  this->addVertexNormal (0.0, -1.0, 0.0);
313  this->addVertexNormal (0.0, -1.0, 0.0);
314  this->addVertexNormal (0.0, -1.0, 0.0);
315  this->addVertexNormal (1.0, 0.0, 0.0);
316  this->addVertexNormal (1.0, 0.0, 0.0);
317  this->addVertexNormal (1.0, 0.0, 0.0);
318  this->addVertexNormal (1.0, 0.0, 0.0);
319  this->addVertexNormal (-1.0, 0.0, 0.0);
320  this->addVertexNormal (-1.0, 0.0, 0.0);
321  this->addVertexNormal (-1.0, 0.0, 0.0);
322  this->addVertexNormal (-1.0, 0.0, 0.0);
323
324  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
325  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
326  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
327  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
328  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
329  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
330}
Note: See TracBrowser for help on using the repository browser.