Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

extended model again, added mounting point generation procedures now working on a nicer interface

File size: 8.7 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();
[10182]86  this->extractMountPoints();
[5790]87}
88
[9869]89void StaticModel::acquireData(const StaticModelData::Pointer& data)
[2748]90{
[9869]91  this->data = data;
92  this->updateBase();
[2748]93}
[2754]94
[10147]95
96
[2842]97/**
[10147]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
104  // go through all groups and check if they are mounts
105  std::vector<StaticModelData::Group>::const_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
[10183]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      continue;
117
118
119    PRINTF(0)("Found a MountPoint: %s\n", groupName.c_str());
120
121    // now check if it is a mount point identifier
122    if( (*groupIt)._faces.size() != 6)
[10147]123    {
[10183]124      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",
125                groupName.c_str(), (*groupIt)._faces.size());
126    }
[10147]127
[10183]128    // now extract the direction from the length:
129    std::vector<StaticModelData::Face>::const_iterator faceIt = (*groupIt)._faces.begin();
130    for( ; faceIt < (*groupIt)._faces.end(); faceIt++)
131    {
132      // now go through all modelfaceelements
133      std::vector<StaticModelData::FaceElement>::const_iterator faceElementIt = (*faceIt)._elements.begin();
134      for( ; faceElementIt < (*faceIt)._elements.end(); faceElementIt++)
[10147]135      {
[10183]136        int vert = (*faceElementIt).vertexNumber;
137        vertices.push_back(Vector(this->data->getVertices()[vert*3],
138                                  this->data->getVertices()[vert*3 + 1],
139                                  this->data->getVertices()[vert*3 + 2]));
[10147]140      }
[10183]141    }
[10147]142
[10183]143    // vertex with the max surrounding faces is the up-point (pyramid like object)
144    std::vector<Vector>::const_iterator it = vertices.begin();
145    Vector tmpPoint;
146    int tmpCount;
147    Vector up;
148    int maxCount = 0;
149    for( ; it < vertices.end(); it++)
150    {
151      tmpCount = 0;
152      tmpPoint = (*it);
153      //
154      std::vector<Vector>::const_iterator it2 = vertices.begin();
155      for( ; it2 < vertices.end(); it2++)
156        if( tmpPoint == *it2)
157          tmpCount++;
158
159      // if this is the vertex with the most surrounding vertices
160      if( tmpCount > maxCount)
[10147]161      {
[10183]162        up = tmpPoint;
163        maxCount = tmpCount;
[10147]164      }
[10183]165    }
[10147]166
[10181]167
[10183]168    // now get the longest side of the first face, this will be the forward vector
169    Vector forward;
170    Vector side1 = vertices[0] - vertices[1];
171    Vector side2 = vertices[1] - vertices[2];
172    Vector side3 = vertices[2] - vertices[3];
[10181]173
[10183]174    if( fabs(side1.len()) > fabs(side2.len()) && fabs(side1.len()) > fabs(side3.len()))
175      forward = side1;
176    else if( fabs(side2.len()) > fabs(side1.len()) && fabs(side2.len()) > fabs(side3.len()))
177      forward = side2;
178    else if( fabs(side3.len()) > fabs(side1.len()) && fabs(side3.len()) > fabs(side2.len()))
179      forward = side3;
[10181]180
[10183]181    // now get the center of the object
182    Vector center;
183    it = vertices.begin();
184    for( ; it < vertices.end(); it++)
185      center += (*it);
186    // scale
187    center /= vertices.size();
[10147]188
[10183]189    PRINTF(0)("Up Point\n");
190    up.debug();
[10147]191
[10183]192    PRINTF(0)("Center\n");
193    center.debug();
[10165]194
[10183]195    PRINTF(0)("Forward\n");
196    forward.debug();
[10181]197
[10183]198    // now add the mount point
[10184]199    this->addMountPoint( up, forward, center, groupName);
[10147]200  }
201}
202
203
204
205/**
[6031]206 * @brief adds a new Face
[4836]207 * @param faceElemCount the number of Vertices to add to the Face.
208 * @param type The information Passed with each Vertex
[3400]209*/
[6021]210bool StaticModel::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
[3400]211{
[3418]212  va_list itemlist;
213  va_start (itemlist, type);
[9869]214  bool retVal = this->data->addFace(faceElemCount, type, itemlist);
[3418]215  va_end(itemlist);
[9869]216  return retVal;
[3400]217}
218
[9869]219void StaticModel::updateBase()
[3063]220{
[9869]221  // write out the modelInfo data used for the collision detection!
222  this->pModelInfo.pVertices = &this->data->getVertices()[0];
223  this->pModelInfo.numVertices = this->data->getVertices().size();
224  this->pModelInfo.pNormals = &this->data->getNormals()[0];
225  this->pModelInfo.numNormals = this->data->getNormals().size();
226  this->pModelInfo.pTexCoor = &this->data->getTexCoords()[0];
227  this->pModelInfo.numTexCoor = this->data->getTexCoords().size();
[4577]228
[9869]229  this->pModelInfo.pTriangles = this->data->getTrianglesExt();
230  this->pModelInfo.numTriangles = this->data->getTriangles().size();
[3801]231}
232
[4577]233
[3066]234/**
[9869]235 *  Includes a default model
[6031]236 *
[9869]237 * This will inject a Cube, because this is the most basic model.
[6031]238 */
[6021]239void StaticModel::cubeModel()
[2821]240{
[3656]241  this->addVertex (-0.5, -0.5, 0.5);
242  this->addVertex (0.5, -0.5, 0.5);
243  this->addVertex (-0.5, 0.5, 0.5);
244  this->addVertex (0.5, 0.5, 0.5);
245  this->addVertex (-0.5, 0.5, -0.5);
246  this->addVertex (0.5, 0.5, -0.5);
247  this->addVertex (-0.5, -0.5, -0.5);
248  this->addVertex (0.5, -0.5, -0.5);
[2967]249
[3656]250  this->addVertexTexture (0.0, 0.0);
251  this->addVertexTexture (1.0, 0.0);
252  this->addVertexTexture (0.0, 1.0);
253  this->addVertexTexture (1.0, 1.0);
254  this->addVertexTexture (0.0, 2.0);
255  this->addVertexTexture (1.0, 2.0);
256  this->addVertexTexture (0.0, 3.0);
257  this->addVertexTexture (1.0, 3.0);
258  this->addVertexTexture (0.0, 4.0);
259  this->addVertexTexture (1.0, 4.0);
260  this->addVertexTexture (2.0, 0.0);
261  this->addVertexTexture (2.0, 1.0);
262  this->addVertexTexture (-1.0, 0.0);
263  this->addVertexTexture (-1.0, 1.0);
[3081]264
[3656]265  this->addVertexNormal (0.0, 0.0, 1.0);
266  this->addVertexNormal (0.0, 0.0, 1.0);
267  this->addVertexNormal (0.0, 0.0, 1.0);
268  this->addVertexNormal (0.0, 0.0, 1.0);
269  this->addVertexNormal (0.0, 1.0, 0.0);
270  this->addVertexNormal (0.0, 1.0, 0.0);
271  this->addVertexNormal (0.0, 1.0, 0.0);
272  this->addVertexNormal (0.0, 1.0, 0.0);
273  this->addVertexNormal (0.0, 0.0, -1.0);
274  this->addVertexNormal (0.0, 0.0, -1.0);
275  this->addVertexNormal (0.0, 0.0, -1.0);
276  this->addVertexNormal (0.0, 0.0, -1.0);
277  this->addVertexNormal (0.0, -1.0, 0.0);
278  this->addVertexNormal (0.0, -1.0, 0.0);
279  this->addVertexNormal (0.0, -1.0, 0.0);
280  this->addVertexNormal (0.0, -1.0, 0.0);
281  this->addVertexNormal (1.0, 0.0, 0.0);
282  this->addVertexNormal (1.0, 0.0, 0.0);
283  this->addVertexNormal (1.0, 0.0, 0.0);
284  this->addVertexNormal (1.0, 0.0, 0.0);
285  this->addVertexNormal (-1.0, 0.0, 0.0);
286  this->addVertexNormal (-1.0, 0.0, 0.0);
287  this->addVertexNormal (-1.0, 0.0, 0.0);
288  this->addVertexNormal (-1.0, 0.0, 0.0);
[2821]289
[4112]290  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
291  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
292  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
293  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
294  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
295  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
[2821]296}
Note: See TracBrowser for help on using the repository browser.