Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added Class Tile to HeightMap (allows a primitive LOD Implementation).

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