Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the VertexArrayModel from heightmap and added some simple importer facility.

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