Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10765 was 10760, checked in by nicolasc, 18 years ago

Widget Improvements

File size: 9.3 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  // go through all groups and check if they are mounts
104  std::vector<StaticModelData::Group>::iterator groupIt = this->data->getGroups().begin();
105  for( ; groupIt != this->data->getGroups().end();)
106  {
107    //PRINTF(0)("Found a MountPoint: %s\n", groupName.c_str());
108
109    // get the name of this group and check if it's a mout point identifier
110    std::string groupName = (*groupIt).name;
111    std::vector<Vector> vertices;
112
113    // check if the name has a "MP" prefix or else it won't work
114    if( groupName.find("MP.", 0) == std::string::npos){
115      groupIt++;
116      continue;
117    }
118
119    PRINTF(5)("Found a MountPoint: %s\n", groupName.c_str());
120
121    StaticModelData::Face triangle[3];
122
123    // now check if it is a mount point identifier
124    if( (*groupIt)._faces.size() != 11)
125    {
126      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",
127                groupName.c_str(), (*groupIt)._faces.size());
128      groupIt++;
129      continue;
130    }
131
132    // now iterate through all faces
133    std::vector<StaticModelData::Face>::const_iterator faceIt = (*groupIt)._faces.begin();
134    for( int i = 0; faceIt < (*groupIt)._faces.end(); faceIt++)
135    {
136      if( (*faceIt)._elements.size() == 3)
137      {
138        triangle[i++] = (*faceIt);
139      }
140    }
141
142
143    Vector center;
144    Vector xAxis;
145    Vector yAxis;
146    Vector zAxis;
147    // now process all points
148    for( int i = 0; i < 3; i++)
149    {
150      // convert the float vertices to vectors
151      Vector a( this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3],
152                this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3 + 1],
153                this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3 + 2]);
154      Vector b( this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3],
155                this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3 + 1],
156                this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3 + 2]);
157      Vector c( this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3],
158                this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3 + 1],
159                this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3 + 2]);
160
161      Vector ab = a - b;
162      Vector ac = a - c;
163      Vector bc = b - c;
164
165      Vector  axis1;
166      Vector  axis2;
167      // now find the center point (the one with the 90degree angle)
168      if( fabs(ab.dot( ac)) < 0.0001)
169      {
170        center = a;
171        axis1 = b - a;
172        axis2 = c - a;
173      }
174      else if( fabs(ab.dot(bc)) < 0.0001)
175      {
176        center = b;
177        axis1 = a - b;
178        axis2 = c - b;
179      }
180      else if( fabs(bc.dot(ac)) < 0.0001)
181      {
182        center = c;
183        axis1 = b - c;
184        axis2 = a - c;
185      }
186
187
188      // get the longest axis (without defining a max() funciton :D
189      if( xAxis.len() < axis1.len() )
190        xAxis = axis1;
191      if( xAxis.len() < axis2.len())
192        xAxis = axis2;
193
194
195      // find the 2nd longest axis to be y-axis
196      if( xAxis.len() > axis1.len() && yAxis.len() < axis1.len())
197        yAxis = axis1;
198      if( xAxis.len() > axis2.len() && yAxis.len() < axis2.len())
199        yAxis = axis2;
200
201
202      // needed... no explanation here..
203      if( zAxis.len() == 0.)
204        zAxis = yAxis;
205
206      // find the shortest axis to be z-axis
207      if( yAxis.len() > axis1.len() )
208        zAxis = axis1;
209      if( yAxis.len() > axis2.len() )
210        zAxis = axis2;
211    }
212
213//     printf("adding MP\n");
214     this->addMountPoint( zAxis, yAxis, center, groupName);
215      // remove the group from the model list (mount points do not need to be drawn)
216//      std::vector<StaticModelData::Group>::iterator tmpIt =  groupIt;
217     groupIt++; 
218//      this->data->getGroups().erase(tmpIt);
219  }
220}
221
222
223
224/**
225 * @brief adds a new Face
226 * @param faceElemCount the number of Vertices to add to the Face.
227 * @param type The information Passed with each Vertex
228*/
229bool StaticModel::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
230{
231  va_list itemlist;
232  va_start (itemlist, type);
233  bool retVal = this->data->addFace(faceElemCount, type, itemlist);
234  va_end(itemlist);
235  return retVal;
236}
237
238void StaticModel::updateBase()
239{
240  // write out the modelInfo data used for the collision detection!
241  this->pModelInfo.pVertices = &this->data->getVertices()[0];
242  this->pModelInfo.numVertices = this->data->getVertices().size();
243  this->pModelInfo.pNormals = &this->data->getNormals()[0];
244  this->pModelInfo.numNormals = this->data->getNormals().size();
245  this->pModelInfo.pTexCoor = &this->data->getTexCoords()[0];
246  this->pModelInfo.numTexCoor = this->data->getTexCoords().size();
247
248  this->pModelInfo.pTriangles = this->data->getTrianglesExt();
249  this->pModelInfo.numTriangles = this->data->getTriangles().size();
250}
251
252
253/**
254 *  Includes a default model
255 *
256 * This will inject a Cube, because this is the most basic model.
257 */
258void StaticModel::cubeModel()
259{
260  this->addVertex (-0.5, -0.5, 0.5);
261  this->addVertex (0.5, -0.5, 0.5);
262  this->addVertex (-0.5, 0.5, 0.5);
263  this->addVertex (0.5, 0.5, 0.5);
264  this->addVertex (-0.5, 0.5, -0.5);
265  this->addVertex (0.5, 0.5, -0.5);
266  this->addVertex (-0.5, -0.5, -0.5);
267  this->addVertex (0.5, -0.5, -0.5);
268
269  this->addVertexTexture (0.0, 0.0);
270  this->addVertexTexture (1.0, 0.0);
271  this->addVertexTexture (0.0, 1.0);
272  this->addVertexTexture (1.0, 1.0);
273  this->addVertexTexture (0.0, 2.0);
274  this->addVertexTexture (1.0, 2.0);
275  this->addVertexTexture (0.0, 3.0);
276  this->addVertexTexture (1.0, 3.0);
277  this->addVertexTexture (0.0, 4.0);
278  this->addVertexTexture (1.0, 4.0);
279  this->addVertexTexture (2.0, 0.0);
280  this->addVertexTexture (2.0, 1.0);
281  this->addVertexTexture (-1.0, 0.0);
282  this->addVertexTexture (-1.0, 1.0);
283
284  this->addVertexNormal (0.0, 0.0, 1.0);
285  this->addVertexNormal (0.0, 0.0, 1.0);
286  this->addVertexNormal (0.0, 0.0, 1.0);
287  this->addVertexNormal (0.0, 0.0, 1.0);
288  this->addVertexNormal (0.0, 1.0, 0.0);
289  this->addVertexNormal (0.0, 1.0, 0.0);
290  this->addVertexNormal (0.0, 1.0, 0.0);
291  this->addVertexNormal (0.0, 1.0, 0.0);
292  this->addVertexNormal (0.0, 0.0, -1.0);
293  this->addVertexNormal (0.0, 0.0, -1.0);
294  this->addVertexNormal (0.0, 0.0, -1.0);
295  this->addVertexNormal (0.0, 0.0, -1.0);
296  this->addVertexNormal (0.0, -1.0, 0.0);
297  this->addVertexNormal (0.0, -1.0, 0.0);
298  this->addVertexNormal (0.0, -1.0, 0.0);
299  this->addVertexNormal (0.0, -1.0, 0.0);
300  this->addVertexNormal (1.0, 0.0, 0.0);
301  this->addVertexNormal (1.0, 0.0, 0.0);
302  this->addVertexNormal (1.0, 0.0, 0.0);
303  this->addVertexNormal (1.0, 0.0, 0.0);
304  this->addVertexNormal (-1.0, 0.0, 0.0);
305  this->addVertexNormal (-1.0, 0.0, 0.0);
306  this->addVertexNormal (-1.0, 0.0, 0.0);
307  this->addVertexNormal (-1.0, 0.0, 0.0);
308
309  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
310  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
311  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
312  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
313  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
314  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
315}
Note: See TracBrowser for help on using the repository browser.