Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6429 was 6314, checked in by bensch, 19 years ago

orxonox/trunk: less debug output

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