Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/static_model.cc @ 10147

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

merged the mount_point branche back to trunk to use the new std::* based obj file importer

File size: 7.8 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  data->finalize();
85  this->updateBase();
86}
87
88void StaticModel::acquireData(const StaticModelData::Pointer& data)
89{
90  this->data = data;
91  this->updateBase();
92}
93
94
95
96/**
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
113    if( groupName.find("MP.", 0) != std::string::npos)
114    {
115      PRINTF(0)("Found a MountPoint: %s\n", groupName.c_str());
116
117      // now check if it is a mount point identifier
118      if( this->data->getGroups().size() != 5) {
119        PRINTF(1)("the face count of %s is wrong, perhaps you missnamed this object or used the wrong mount point object\n", groupName.c_str());
120      }
121
122      // now extract the direction from the length:
123      std::vector<StaticModelData::Face>::const_iterator faceIt = (*groupIt)._faces.begin();
124      for( ; faceIt < (*groupIt)._faces.end(); faceIt++)
125      {
126        // now go through all modelfaceelements
127        std::vector<StaticModelData::FaceElement>::const_iterator faceElementIt = (*faceIt)._elements.begin();
128        for( ; faceElementIt < (*faceIt)._elements.end(); faceElementIt++)
129        {
130          int vert = (*faceElementIt).vertexNumber;
131          vertices.push_back(Vector(this->data->getVertices()[vert*3],
132                             this->data->getVertices()[vert*3 + 1],
133                             this->data->getVertices()[vert*3 + 2]));
134        }
135      }
136
137      // vertex with the max count is the up-point
138      std::vector<Vector>::const_iterator it = vertices.begin();
139      Vector tmpPoint;
140      int tmpCount;
141      Vector upPoint;
142      int maxCount = 0;
143      for( ; it < vertices.end(); it++)
144      {
145        tmpCount = 0;
146        tmpPoint = (*it);
147        //
148        std::vector<Vector>::const_iterator it2 = vertices.begin();
149        for( ; it2 < vertices.end(); it2++)
150          if( tmpPoint == *it2)
151            tmpCount++;
152
153        // if this is the vertex with the most surrounding vertices
154        if( tmpCount > maxCount)
155        {
156          upPoint = tmpPoint;
157          maxCount = tmpCount;
158        }
159      }
160
161      // now get the center of the object
162      Vector center;
163      it = vertices.begin();
164      for( ; it < vertices.end(); it++)
165        center += (*it);
166      // scale
167      center /= vertices.size();
168
169
170    }
171
172  }
173
174
175}
176
177
178
179/**
180 * @brief adds a new Face
181 * @param faceElemCount the number of Vertices to add to the Face.
182 * @param type The information Passed with each Vertex
183*/
184bool StaticModel::addFace(int faceElemCount, VERTEX_FORMAT type, ...)
185{
186  va_list itemlist;
187  va_start (itemlist, type);
188  bool retVal = this->data->addFace(faceElemCount, type, itemlist);
189  va_end(itemlist);
190  return retVal;
191}
192
193void StaticModel::updateBase()
194{
195  // write out the modelInfo data used for the collision detection!
196  this->pModelInfo.pVertices = &this->data->getVertices()[0];
197  this->pModelInfo.numVertices = this->data->getVertices().size();
198  this->pModelInfo.pNormals = &this->data->getNormals()[0];
199  this->pModelInfo.numNormals = this->data->getNormals().size();
200  this->pModelInfo.pTexCoor = &this->data->getTexCoords()[0];
201  this->pModelInfo.numTexCoor = this->data->getTexCoords().size();
202
203  this->pModelInfo.pTriangles = this->data->getTrianglesExt();
204  this->pModelInfo.numTriangles = this->data->getTriangles().size();
205}
206
207
208/**
209 *  Includes a default model
210 *
211 * This will inject a Cube, because this is the most basic model.
212 */
213void StaticModel::cubeModel()
214{
215  this->addVertex (-0.5, -0.5, 0.5);
216  this->addVertex (0.5, -0.5, 0.5);
217  this->addVertex (-0.5, 0.5, 0.5);
218  this->addVertex (0.5, 0.5, 0.5);
219  this->addVertex (-0.5, 0.5, -0.5);
220  this->addVertex (0.5, 0.5, -0.5);
221  this->addVertex (-0.5, -0.5, -0.5);
222  this->addVertex (0.5, -0.5, -0.5);
223
224  this->addVertexTexture (0.0, 0.0);
225  this->addVertexTexture (1.0, 0.0);
226  this->addVertexTexture (0.0, 1.0);
227  this->addVertexTexture (1.0, 1.0);
228  this->addVertexTexture (0.0, 2.0);
229  this->addVertexTexture (1.0, 2.0);
230  this->addVertexTexture (0.0, 3.0);
231  this->addVertexTexture (1.0, 3.0);
232  this->addVertexTexture (0.0, 4.0);
233  this->addVertexTexture (1.0, 4.0);
234  this->addVertexTexture (2.0, 0.0);
235  this->addVertexTexture (2.0, 1.0);
236  this->addVertexTexture (-1.0, 0.0);
237  this->addVertexTexture (-1.0, 1.0);
238
239  this->addVertexNormal (0.0, 0.0, 1.0);
240  this->addVertexNormal (0.0, 0.0, 1.0);
241  this->addVertexNormal (0.0, 0.0, 1.0);
242  this->addVertexNormal (0.0, 0.0, 1.0);
243  this->addVertexNormal (0.0, 1.0, 0.0);
244  this->addVertexNormal (0.0, 1.0, 0.0);
245  this->addVertexNormal (0.0, 1.0, 0.0);
246  this->addVertexNormal (0.0, 1.0, 0.0);
247  this->addVertexNormal (0.0, 0.0, -1.0);
248  this->addVertexNormal (0.0, 0.0, -1.0);
249  this->addVertexNormal (0.0, 0.0, -1.0);
250  this->addVertexNormal (0.0, 0.0, -1.0);
251  this->addVertexNormal (0.0, -1.0, 0.0);
252  this->addVertexNormal (0.0, -1.0, 0.0);
253  this->addVertexNormal (0.0, -1.0, 0.0);
254  this->addVertexNormal (0.0, -1.0, 0.0);
255  this->addVertexNormal (1.0, 0.0, 0.0);
256  this->addVertexNormal (1.0, 0.0, 0.0);
257  this->addVertexNormal (1.0, 0.0, 0.0);
258  this->addVertexNormal (1.0, 0.0, 0.0);
259  this->addVertexNormal (-1.0, 0.0, 0.0);
260  this->addVertexNormal (-1.0, 0.0, 0.0);
261  this->addVertexNormal (-1.0, 0.0, 0.0);
262  this->addVertexNormal (-1.0, 0.0, 0.0);
263
264  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 0,0,0, 1,1,1, 3,3,2, 2,2,3);
265  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 2,2,4, 3,3,5, 5,5,6, 4,4,7);
266  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 4,4,8, 5,5,9, 7,7,10, 6,6,11);
267  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,6,12, 7,7,13, 1,9,14, 0,8,15);
268  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 1,1,16, 7,10,17, 5,11,18, 3,3,19);
269  this->addFace (4, VERTEX_TEXCOORD_NORMAL, 6,12,20, 0,0,21, 2,2,22, 4,13,23);
270}
Note: See TracBrowser for help on using the repository browser.