Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/vertex_array_model.cc @ 6313

Last change on this file since 6313 was 6313, checked in by bensch, 18 years ago

orxonox/trunk: any-3D-model to VertexArrayModel works perfectly

File size: 10.5 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
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
18#include "vertex_array_model.h"
19
20#include "stdlibincl.h"
21#include <stdarg.h>
22
23#include "tc.h"
24
25using namespace std;
26
27/////////////
28/// MODEL ///
29/////////////
30/**
31 * @brief Creates a 3D-VertexArrayModel.
32 *
33 * assigns it a Name and a Type
34 */
35VertexArrayModel::VertexArrayModel()
36{
37  this->setClassID(CL_MODEL, "VertexArrayModel");
38
39  this->bFinalized = false;
40  this->newStripe();
41}
42
43/**
44 * @brief special copy constructor for converting Models to VertexArray-Stripes
45 * @param model the Model to produce a VertexArray model from.
46 *
47 * Code that uses Brad Granthams
48 * excelent TC-code for generating stripes out of a mix of ModelCoordinates.
49 */
50VertexArrayModel::VertexArrayModel(const Model& model)
51{
52  this->setClassID(CL_MODEL, "VertexArrayModel");
53  this->bFinalized = false;
54
55    // importing the data to the new Model.
56  this->newStripe();
57
58  for (unsigned int i = 0; i < model.getVertexCount()*3; i+=3)
59  {
60    this->addVertex(model.getVertexArray()[i], model.getVertexArray()[i+1], model.getVertexArray()[i+2]);
61  }
62  for (unsigned int i = 0; i < model.getVertexCount()*3; i+=3)
63    this->addColor(model.getVertexArray()[i], model.getVertexArray()[i+1], model.getVertexArray()[i+2]);
64  for (unsigned int i = 0; i < model.getNormalsCount()*3; i+=3)
65    this->addNormal(model.getNormalsArray()[i], model.getNormalsArray()[i+1], model.getNormalsArray()[i+2]);
66//   for (unsigned int i = 0; i < model.getTexCoordCount(); i+=2)
67//     this->addTexCoor(model.getTexCoordArray()[i], model.getTexCoordArray()[i+1]);
68  for (unsigned int i = 0; i < model.getVertexCount()*2; i+=2)
69    this->addTexCoor(1,2);
70
71
72  // The acTC object generating this Model. //
73  ACTCData *tc;
74  tc = actcNew();
75  if(tc == NULL) {
76    /* memory allocation failed */
77    /* print error here and exit or whatever */
78  }
79
80  // inputing the data of model to the tc
81  actcBeginInput(tc);
82  for(unsigned int i = 0; i < model.getTriangleCount(); i++)
83  {
84      actcAddTriangle(tc,
85                      model.getTriangles()[i].indexToVertices[0],
86                      model.getTriangles()[i].indexToVertices[1],
87                      model.getTriangles()[i].indexToVertices[2]);
88  }
89  actcEndInput(tc);
90
91
92
93  int prim;
94  uint v1, v2, v3;
95
96  actcBeginOutput(tc);
97  while((prim = actcStartNextPrim(tc, &v1, &v2) != ACTC_DATABASE_EMPTY))
98  {
99    this->newStripe();
100
101    this->addIndice(v1);
102    this->addIndice(v2);
103    printf("%d\n", v1);
104    printf("%d\n", v2);
105    /* start a primitive of type "prim" with v1 and v2 */
106    while(actcGetNextVert(tc, &v3) != ACTC_PRIM_COMPLETE)
107    {
108      /* continue primitive using v3 */
109      this->addIndice(v3);
110      printf("%d\n", v3);
111    }
112  }
113  actcEndOutput(tc);
114
115  this->finalize();
116  this->debug();
117}
118
119
120/**
121 * @brief deletes a VertexArrayModel.
122 *
123 * Looks if any from model allocated space is still in use, and if so deleted it.
124 */
125VertexArrayModel::~VertexArrayModel()
126{
127  PRINTF(4)("Deleting VertexArrayModel ");
128  if (this->getName())
129  {
130    PRINT(4)("%s\n", this->getName());
131  }
132  else
133  {
134    PRINT(4)("\n");
135  }
136}
137
138
139/**
140 * @brief Draws the VertexArrayModels of all Groups.
141 *
142 * It does this by just calling the Lists that must have been created earlier.
143 */
144void VertexArrayModel::draw() const
145{
146  PRINTF(4)("drawing 3D-VertexArrayModel %s\n", this->getName());
147  glEnableClientState(GL_VERTEX_ARRAY );
148  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
149  glEnableClientState(GL_NORMAL_ARRAY );
150  glEnableClientState(GL_COLOR_ARRAY );
151
152
153  glVertexPointer(3, GL_FLOAT, 0, &this->vertices[0]);
154  glNormalPointer(GL_FLOAT, 0, &this->normals[0]);
155  glTexCoordPointer(2, GL_FLOAT, 0, &this->texCoords[0]);
156  glColorPointer(3, GL_FLOAT, 0, &this->colors[0]);
157
158  for (GLuint i = 1; i < this->stripes.size(); ++i)
159    {
160      glDrawElements( GL_TRIANGLE_STRIP,
161                      this->stripes[i] - this->stripes[i-1],
162                      GL_UNSIGNED_INT,
163                      &this->indices[this->stripes[i-1]] );
164    }
165}
166
167
168//////////
169// MESH //
170//////////
171/**
172 * @brief generates a new Stripe in this Model
173 */
174void VertexArrayModel::newStripe()
175{
176  // no stripes of size 0
177  if (this->stripes.empty() || this->indices.size() != this->stripes.back())
178    this->stripes.push_back(this->indices.size());
179}
180
181
182/**
183 * @brief parses a vertex-String
184 * @param x the X-coordinate of the Vertex to add.
185 * @param y the Y-coordinate of the Vertex to add.
186 * @param z the Z-coordinate of the Vertex to add.
187 */
188void VertexArrayModel::addVertex(float x, float y, float z)
189{
190  this->vertices.push_back(x);
191  this->vertices.push_back(y);
192  this->vertices.push_back(z);
193  this->pModelInfo.numVertices++;
194}
195
196
197/**
198 * @brief adds a VertexNormal.
199 * @param x The x coordinate of the Normal.
200 * @param y The y coordinate of the Normal.
201 * @param z The z coordinate of the Normal.
202 *
203 * If a vertexNormal line is found this function will inject it into the vertexNormal-Array
204 */
205void VertexArrayModel::addNormal(float x, float y, float z)
206{
207  this->normals.push_back(x);
208  this->normals.push_back(y);
209  this->normals.push_back(z);
210  this->pModelInfo.numNormals++;
211}
212
213
214/**
215 * @brief adds a Texture Coordinate
216 * @param u The u coordinate of the TextureCoordinate.
217 * @param v The y coordinate of the TextureCoordinate.
218 *
219 *  If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
220 */
221void VertexArrayModel::addTexCoor(float u, float v)
222{
223  this->texCoords.push_back(u);
224  this->texCoords.push_back(v);
225  this->pModelInfo.numTexCoor++;
226}
227
228/**
229 * @brief adds a new Color
230 * @param r the Red Component of the VertexColor to add.
231 * @param g the Green Component of the VertexColor to add.
232 * @param b the Blue of the VertexColor to add.
233 */
234void VertexArrayModel::addColor(float r, float g, float b)
235{
236  this->colors.push_back(r);
237  this->colors.push_back(g);
238  this->colors.push_back(b);
239  // FIXME
240}
241
242
243/**
244 *  adds a new Face
245 * @param faceElemCount the number of Vertices to add to the Face.
246 * @param type The information Passed with each Vertex
247*/
248void VertexArrayModel::addIndice(GLuint indice)
249{
250  this->indices.push_back(indice);
251}
252
253
254/**
255 * @brief Finalizes an Object. This can be done outside of the Class.
256 */
257void VertexArrayModel::finalize()
258{
259  // finalize the Arrays
260  this->newStripe();
261  this->bFinalized = true;
262}
263
264
265
266
267/////////////
268// TESTING //
269/////////////
270/**
271* @brief Includes a default model
272*
273* This will inject a Cube, because this is the most basic model.
274*/
275void VertexArrayModel::planeModel(float sizeX, float sizeY, unsigned int resolutionX, unsigned int resolutionY)
276{
277  GLuint i, j;
278  for (i = 0; i < resolutionY; i++)
279    {
280      for (j = 0; j < resolutionX; j++)
281        {
282          this->addVertex((float)i - (float)sizeY/2.0, 0.0, (float)j - (float)sizeX/2.0);
283          this->addNormal(0.0, 1, 0.0);
284          this->addTexCoor((float)i/(float)resolutionY, (float)j/(float)resolutionY);
285          this->addColor((float)i/20.0, 0.0, (float)j/20.0);
286        }
287    }
288
289  for (i = 0; i < resolutionY-1; i++)
290  {
291    for (j = 0; j < resolutionX; j++)
292    {
293      this->addIndice( resolutionY*i + j );
294      this->addIndice( resolutionY*(i+1) + j );
295    }
296    this->newStripe();
297  }
298}
299
300#include <cmath>
301
302/**
303 * @brief builds a Triangle Stripped sphere
304 * @param radius: radius
305 * @param loops: the count of loops
306 * @param segmentsPerLoop how many Segments per loop
307 */
308void VertexArrayModel::spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop)
309{
310  for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
311  {
312    float theta = 0;
313    float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
314    float sinTheta = std::sin(theta);
315    float sinPhi = std::sin(phi);
316    float cosTheta = std::cos(theta);
317    float cosPhi = std::cos(phi);
318    this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
319    this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
320    this->addTexCoor(0,0); /// FIXME
321    this->addColor(.125,.436,.246); ///FIXME
322  }
323  for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber)
324  {
325    for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
326    {
327      float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops));
328      if (loopNumber == loops)
329      {
330        theta = PI;
331      }
332      float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
333      float sinTheta = std::sin(theta);
334      float sinPhi = std::sin(phi);
335      float cosTheta = std::cos(theta);
336      float cosPhi = std::cos(phi);
337      this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
338      this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
339      this->addTexCoor(0,0); //FIXME
340      this->addColor(.125,.436,.246);
341
342    }
343  }
344  for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
345  {
346    this->addIndice(loopSegmentNumber);
347    this->addIndice(segmentsPerLoop + loopSegmentNumber);
348  }
349  for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber)
350  {
351    for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
352    {
353      this->addIndice( ((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber);
354      this->addIndice( ((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber);
355    }
356  }
357}
358
359
360/**
361 * @brief print out some nice debug information about this VertexArrayModel.
362 */
363void VertexArrayModel::debug() const
364{
365  PRINT(0)("VertexArrayModel (%s): debug\n", this->getName());
366  PRINT(0)("Stripes: %d; Indices: %d; Vertices: %d; Normals %d; TextCoords %d; Colors %d\n",
367            this->stripes.size(),
368            this->indices.size(),
369            this->vertices.size()/3,
370            this->normals.size()/3,
371            this->texCoords.size()/2,
372            this->colors.size() )/3;
373  for (GLuint i = 1; i < this->stripes.size(); ++i)
374  {
375    PRINT(0)("Stripe-%d (s:%d:e:%d):: ", i, this->stripes[i-1], this->stripes[i]);
376    for (GLuint j = this->stripes[i-1] ; j < this->stripes[i]; j++)
377    {
378      PRINT(0)("%d->", this->indices[j]);
379    }
380    PRINT(0)("\n");
381  }
382}
Note: See TracBrowser for help on using the repository browser.