Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map/src/lib/graphics/importer/vertex_array_model.cc @ 6529

Last change on this file since 6529 was 6529, checked in by bottac, 18 years ago

Some bugs fixed

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