Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/mount_points/src/lib/graphics/importer/static_model.cc @ 10310

Last change on this file since 10310 was 10310, checked in by patrick, 17 years ago

found the axis. good

File size: 9.0 KB
RevLine 
[4577]1/*
[2823]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: ...
[4793]14
15   2005-07-06: (Patrick) added new function buildTriangleList()
[2823]16*/
17
[3590]18#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
19
[6021]20#include "static_model.h"
[3427]21
[8362]22#include "debug.h"
[3418]23#include <stdarg.h>
[3398]24
[2776]25
[4022]26
[9406]27
[4022]28/////////////
29/// MODEL ///
30/////////////
[9869]31ObjectListDefinition(StaticModel);
32
[2842]33/**
[6031]34 * @brief Creates a 3D-Model.
35 *
36 * assigns it a Name and a Type
37 */
[7221]38StaticModel::StaticModel(const std::string& modelName)
[9869]39    : data(new StaticModelData(modelName))
[3398]40{
[9869]41  this->registerObject(this, StaticModel::_objectList);
[4577]42  PRINTF(4)("new 3D-Model is being created\n");
[3398]43  this->setName(modelName);
[9869]44}
[3909]45
[9869]46StaticModel::StaticModel(const StaticModel& staticModel)
[10183]47    : data(staticModel.data)
[9869]48{
49  this->registerObject(this, StaticModel::_objectList);
50  this->setName(staticModel.getName());
51  this->updateBase();
52}
[6031]53
[4577]54
[3398]55/**
[6031]56 * @brief deletes an Model.
57 *
58 * Looks if any from model allocated space is still in use, and if so deleted it.
59 */
[6021]60StaticModel::~StaticModel()
[2847]61{
[3548]62  PRINTF(4)("Deleting Model ");
[3396]63
[7732]64  // mark this stuff as beeing deleted
65  this->pModelInfo.pVertices = NULL;
66  this->pModelInfo.pNormals = NULL;
67  this->pModelInfo.pTexCoor = NULL;
[9869]68  this->pModelInfo.pTriangles = NULL;
[2847]69}
70
[9869]71StaticModel& StaticModel::operator=(const StaticModel& model)
[3398]72{
[9869]73  this->data = model.data;
74  this->updateBase();
75  return *this;
76};
[3916]77
[3398]78
[5790]79/**
[9869]80 * @brief Finalizes an Object. This can be done outside of the Class.
[5790]81 */
[9869]82void StaticModel::finalize()
[5790]83{
[9869]84  data->finalize();
85  this->updateBase();
[5790]86}
87
[9869]88void StaticModel::acquireData(const StaticModelData::Pointer& data)
[2748]89{
[9869]90  this->data = data;
91  this->updateBase();
[2748]92}
[2754]93
[10147]94
95
[2842]96/**
[10147]97 * extract the mount points from this file: looking at each group and checking if the group realy is a mountpoint marker
98 * if so get place and orientation
99 */
100void StaticModel::extractMountPoints()
101{
102
103  // go through all groups and check if they are mounts
104  std::vector<StaticModelData::Group>::const_iterator groupIt = this->data->getGroups().begin();
105  for( ; groupIt != this->data->getGroups().end(); groupIt++)
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
[10183]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      continue;
116
117
118    PRINTF(0)("Found a MountPoint: %s\n", groupName.c_str());
119
[10308]120    StaticModelData::Face triangle[3];
121
[10183]122    // now check if it is a mount point identifier
[10307]123    if( (*groupIt)._faces.size() != 9)
[10147]124    {
[10183]125      PRINTF(1)("the face count of %s is wrong, perhaps you missnamed this object or used the wrong mount point object (got %i faces)\n",
126                groupName.c_str(), (*groupIt)._faces.size());
127    }
[10147]128
[10307]129    // now iterate through all faces
[10183]130    std::vector<StaticModelData::Face>::const_iterator faceIt = (*groupIt)._faces.begin();
[10308]131    for( int i = 0; faceIt < (*groupIt)._faces.end(); faceIt++)
[10183]132    {
[10307]133      if( (*faceIt)._elements.size() == 3)
[10147]134      {
[10308]135        triangle[i++] = (*faceIt);
[10307]136        printf("got triangle\n");
[10147]137      }
[10183]138    }
[10147]139
[10310]140
141    Vector center;
142    Vector xAxis;
143    Vector yAxis;
144    Vector zAxis;
[10308]145    // now process all points
146    for( int i = 0; i < 3; i++)
147    {
148      // convert the float vertices to vectors
149      Vector a( this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3],
150                this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3 + 1],
151                this->data->getVertices()[triangle[i]._elements[0].vertexNumber * 3 + 2]);
[10310]152      Vector b( this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3],
153                this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3 + 1],
154                this->data->getVertices()[triangle[i]._elements[1].vertexNumber * 3 + 2]);
155      Vector c( this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3],
156                this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3 + 1],
157                this->data->getVertices()[triangle[i]._elements[2].vertexNumber * 3 + 2]);
[10308]158
159      Vector ab = a - b;
160      Vector ac = a - c;
161      Vector bc = b - c;
162
[10310]163      Vector  axis1;
164      Vector  axis2;
[10308]165      // now find the center point (the one with the 90degree angle)
[10310]166      if( fabs(ab.dot( ac)) < 0.0001)
167      {
168        center = a;
169        axis1 = b - a;
170        axis2 = c - a;
171      }
172      else if( fabs(ab.dot(bc)) < 0.0001)
173      {
174        center = b;
175        axis1 = a - b;
176        axis2 = c - b;
177      }
178      else if( fabs(bc.dot(ac)) < 0.0001)
179      {
180        center = c;
181        axis1 = b - c;
182        axis2 = a - c;
183      }
[10308]184
[10309]185
[10310]186      // get the longest axis (without defining a max() funciton :D
187      if( xAxis.len() < axis1.len() )
188        xAxis = axis1;
189      if( xAxis.len() < axis2.len())
190        xAxis = axis2;
191
192      xAxis.debug();
193
194
195      if( xAxis.len() > axis1.len() && yAxis.len() < axis1.len())
196        yAxis = axis1;
197      if( xAxis.len() > axis2.len() && yAxis.len() < axis2.len())
198        yAxis = axis2;
199
200      yAxis.debug();
201
202      // needed... no explanation here..
203      if( zAxis.len() == 0.)
204        zAxis = yAxis;
205
206      if( yAxis.len() > axis1.len() )
207        zAxis = axis1;
208      if( yAxis.len() > axis2.len() )
209        zAxis = axis2;
210
211      zAxis.debug();
212
[10308]213    }
214
[10183]215    // now add the mount point
[10307]216//     this->addMountPoint( up, forward, center, groupName);
[10147]217  }
218}
219
220
221
222/**
[6031]223 * @brief adds a new Face
[4836]224 * @param faceElemCount the number of Vertices to add to the Face.
225 * @param type The information Passed with each Vertex
[3400]226*/
[6021]227bool StaticModel::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
[3400]228{
[3418]229  va_list itemlist;
230  va_start (itemlist, type);
[9869]231  bool retVal = this->data->addFace(faceElemCount, type, itemlist);
[3418]232  va_end(itemlist);
[9869]233  return retVal;
[3400]234}
235
[9869]236void StaticModel::updateBase()
[3063]237{
[9869]238  // write out the modelInfo data used for the collision detection!
239  this->pModelInfo.pVertices = &this->data->getVertices()[0];
240  this->pModelInfo.numVertices = this->data->getVertices().size();
241  this->pModelInfo.pNormals = &this->data->getNormals()[0];
242  this->pModelInfo.numNormals = this->data->getNormals().size();
243  this->pModelInfo.pTexCoor = &this->data->getTexCoords()[0];
244  this->pModelInfo.numTexCoor = this->data->getTexCoords().size();
[4577]245
[9869]246  this->pModelInfo.pTriangles = this->data->getTrianglesExt();
247  this->pModelInfo.numTriangles = this->data->getTriangles().size();
[3801]248}
249
[4577]250
[3066]251/**
[9869]252 *  Includes a default model
[6031]253 *
[9869]254 * This will inject a Cube, because this is the most basic model.
[6031]255 */
[6021]256void StaticModel::cubeModel()
[2821]257{
[3656]258  this->addVertex (-0.5, -0.5, 0.5);
259  this->addVertex (0.5, -0.5, 0.5);
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);
[2967]266
[3656]267  this->addVertexTexture (0.0, 0.0);
268  this->addVertexTexture (1.0, 0.0);
269  this->addVertexTexture (0.0, 1.0);
270  this->addVertexTexture (1.0, 1.0);
271  this->addVertexTexture (0.0, 2.0);
272  this->addVertexTexture (1.0, 2.0);
273  this->addVertexTexture (0.0, 3.0);
274  this->addVertexTexture (1.0, 3.0);
275  this->addVertexTexture (0.0, 4.0);
276  this->addVertexTexture (1.0, 4.0);
277  this->addVertexTexture (2.0, 0.0);
278  this->addVertexTexture (2.0, 1.0);
279  this->addVertexTexture (-1.0, 0.0);
280  this->addVertexTexture (-1.0, 1.0);
[3081]281
[3656]282  this->addVertexNormal (0.0, 0.0, 1.0);
283  this->addVertexNormal (0.0, 0.0, 1.0);
284  this->addVertexNormal (0.0, 0.0, 1.0);
285  this->addVertexNormal (0.0, 0.0, 1.0);
286  this->addVertexNormal (0.0, 1.0, 0.0);
287  this->addVertexNormal (0.0, 1.0, 0.0);
288  this->addVertexNormal (0.0, 1.0, 0.0);
289  this->addVertexNormal (0.0, 1.0, 0.0);
290  this->addVertexNormal (0.0, 0.0, -1.0);
291  this->addVertexNormal (0.0, 0.0, -1.0);
292  this->addVertexNormal (0.0, 0.0, -1.0);
293  this->addVertexNormal (0.0, 0.0, -1.0);
294  this->addVertexNormal (0.0, -1.0, 0.0);
295  this->addVertexNormal (0.0, -1.0, 0.0);
296  this->addVertexNormal (0.0, -1.0, 0.0);
297  this->addVertexNormal (0.0, -1.0, 0.0);
298  this->addVertexNormal (1.0, 0.0, 0.0);
299  this->addVertexNormal (1.0, 0.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);
[2821]306
[4112]307  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
308  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
309  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
310  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
311  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
312  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
[2821]313}
Note: See TracBrowser for help on using the repository browser.