Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: vertexArrayModel minor functionality update

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