Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10749 was 10368, checked in by patrick, 18 years ago

merged the branche playability into the trunk

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