Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: better smoother water

File size: 10.1 KB
RevLine 
[4577]1/*
[2823]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
[3590]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
17
[6010]18#include "vertex_array_model.h"
[3427]19
[5427]20#include "stdlibincl.h"
[3418]21#include <stdarg.h>
[3398]22
[6308]23#include "tc.h"
24
[3140]25using namespace std;
[2776]26
[4022]27/////////////
28/// MODEL ///
29/////////////
[2842]30/**
[6010]31 * @brief Creates a 3D-VertexArrayModel.
32 *
33 * assigns it a Name and a Type
34 */
35VertexArrayModel::VertexArrayModel()
[3398]36{
[6010]37  this->setClassID(CL_MODEL, "VertexArrayModel");
[3909]38
[6037]39  this->newStripe();
[6010]40}
[4577]41
[6308]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");
[3909]52
[6452]53  // importing the data to the new Model.
[6313]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)
[6314]59    this->addColor((float)i / (float)model.getVertexCount(), 0, 0);
60
[6313]61  for (unsigned int i = 0; i < model.getNormalsCount()*3; i+=3)
[6314]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]);
[6313]65
66
[6308]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++)
[6313]78  {
[6310]79      actcAddTriangle(tc,
80                      model.getTriangles()[i].indexToVertices[0],
81                      model.getTriangles()[i].indexToVertices[1],
82                      model.getTriangles()[i].indexToVertices[2]);
[6313]83  }
[6308]84  actcEndInput(tc);
85
[6309]86
87
[6308]88  int prim;
[6477]89  unsigned int v1, v2, v3;
[6308]90
91  actcBeginOutput(tc);
92  while((prim = actcStartNextPrim(tc, &v1, &v2) != ACTC_DATABASE_EMPTY))
93  {
[6310]94    this->newStripe();
95
[6308]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
[6309]110
[3398]111/**
[6308]112 * @brief deletes a VertexArrayModel.
[6010]113 *
114 * Looks if any from model allocated space is still in use, and if so deleted it.
115 */
116VertexArrayModel::~VertexArrayModel()
[2847]117{
[6010]118  PRINTF(4)("Deleting VertexArrayModel ");
[4577]119  if (this->getName())
[5790]120  {
121    PRINT(4)("%s\n", this->getName());
122  }
[3396]123  else
[5790]124  {
125    PRINT(4)("\n");
126  }
[6010]127}
[3396]128
[3140]129
[2842]130/**
[6037]131 * @brief Draws the VertexArrayModels of all Groups.
132 *
133 * It does this by just calling the Lists that must have been created earlier.
[6010]134 */
[6037]135void VertexArrayModel::draw() const
[3398]136{
[6310]137  PRINTF(4)("drawing 3D-VertexArrayModel %s\n", this->getName());
138  glEnableClientState(GL_VERTEX_ARRAY );
[6519]139  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
[6310]140  glEnableClientState(GL_NORMAL_ARRAY );
141  glEnableClientState(GL_COLOR_ARRAY );
[4803]142
[4799]143
[6310]144  glVertexPointer(3, GL_FLOAT, 0, &this->vertices[0]);
145  glNormalPointer(GL_FLOAT, 0, &this->normals[0]);
[6519]146  glTexCoordPointer(2, GL_FLOAT, 0, &this->texCoords[0]);
[6310]147  glColorPointer(3, GL_FLOAT, 0, &this->colors[0]);
[4803]148
[6038]149  for (GLuint i = 1; i < this->stripes.size(); ++i)
[6037]150    {
[6310]151      glDrawElements( GL_TRIANGLE_STRIP,
152                      this->stripes[i] - this->stripes[i-1],
153                      GL_UNSIGNED_INT,
154                      &this->indices[this->stripes[i-1]] );
[6037]155    }
[2748]156}
[2754]157
[3186]158
[3912]159//////////
160// MESH //
161//////////
[3068]162/**
[6037]163 * @brief generates a new Stripe in this Model
164 */
165void VertexArrayModel::newStripe()
166{
[6310]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());
[6037]170}
171
172
173/**
[6010]174 * @brief parses a vertex-String
[4836]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.
[6010]178 */
179void VertexArrayModel::addVertex(float x, float y, float z)
[3400]180{
[6453]181  this->vertices.push_back(Vector(x,y,z));
[6010]182  this->pModelInfo.numVertices++;
[3400]183}
184
[3912]185
186/**
[6010]187 * @brief adds a VertexNormal.
[4836]188 * @param x The x coordinate of the Normal.
189 * @param y The y coordinate of the Normal.
190 * @param z The z coordinate of the Normal.
[6010]191 *
192 * If a vertexNormal line is found this function will inject it into the vertexNormal-Array
193 */
194void VertexArrayModel::addNormal(float x, float y, float z)
[3912]195{
[6453]196  this->normals.push_back(Vector(x,y,z));
[6010]197  this->pModelInfo.numNormals++;
[3912]198}
199
200
201/**
[6310]202 * @brief adds a Texture Coordinate
[4836]203 * @param u The u coordinate of the TextureCoordinate.
204 * @param v The y coordinate of the TextureCoordinate.
[6010]205 *
206 *  If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
207 */
208void VertexArrayModel::addTexCoor(float u, float v)
[3912]209{
[6310]210  this->texCoords.push_back(u);
211  this->texCoords.push_back(v);
[6010]212  this->pModelInfo.numTexCoor++;
[3912]213}
214
[6310]215/**
216 * @brief adds a new Color
217 * @param r the Red Component of the VertexColor to add.
218 * @param g the Green Component of the VertexColor to add.
219 * @param b the Blue of the VertexColor to add.
220 */
221void VertexArrayModel::addColor(float r, float g, float b)
222{
[6453]223  this->colors.push_back(Vector(r,g,b));
[6310]224  // FIXME
225}
[3186]226
[6310]227
[2842]228/**
[4836]229 *  adds a new Face
230 * @param faceElemCount the number of Vertices to add to the Face.
231 * @param type The information Passed with each Vertex
[3400]232*/
[6310]233void VertexArrayModel::addIndice(GLuint indice)
[3400]234{
[6310]235  this->indices.push_back(indice);
[3400]236}
237
[4577]238
[3912]239/**
[6037]240 * @brief Finalizes an Object. This can be done outside of the Class.
241 */
242void VertexArrayModel::finalize()
[3063]243{
244  // finalize the Arrays
[6037]245  this->newStripe();
[3063]246}
247
[3916]248
249
[6310]250
[6010]251/////////////
252// TESTING //
253/////////////
[3916]254/**
[6310]255* @brief Includes a default model
256*
257* This will inject a Cube, because this is the most basic model.
258*/
259void VertexArrayModel::planeModel(float sizeX, float sizeY, unsigned int resolutionX, unsigned int resolutionY)
260{
261  GLuint i, j;
262  for (i = 0; i < resolutionY; i++)
263    {
264      for (j = 0; j < resolutionX; j++)
265        {
[6456]266          this->addVertex(((float)i - (float)resolutionY/2.0)*sizeY, 0.0, ((float)j - (float)resolutionX/2.0)*sizeX);
[6310]267          this->addNormal(0.0, 1, 0.0);
268          this->addTexCoor((float)i/(float)resolutionY, (float)j/(float)resolutionY);
269          this->addColor((float)i/20.0, 0.0, (float)j/20.0);
270        }
271    }
272
273  for (i = 0; i < resolutionY-1; i++)
274  {
275    for (j = 0; j < resolutionX; j++)
276    {
277      this->addIndice( resolutionY*i + j );
278      this->addIndice( resolutionY*(i+1) + j );
279    }
280    this->newStripe();
281  }
282}
283
284#include <cmath>
285
286/**
287 * @brief builds a Triangle Stripped sphere
288 * @param radius: radius
289 * @param loops: the count of loops
290 * @param segmentsPerLoop how many Segments per loop
[4791]291 */
[6310]292void VertexArrayModel::spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop)
[4791]293{
[6310]294  for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
295  {
296    float theta = 0;
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); ///FIXME
306  }
307  for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber)
308  {
309    for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
[4796]310    {
[6310]311      float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops));
312      if (loopNumber == loops)
313      {
314        theta = PI;
315      }
316      float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop;
317      float sinTheta = std::sin(theta);
318      float sinPhi = std::sin(phi);
319      float cosTheta = std::cos(theta);
320      float cosPhi = std::cos(phi);
321      this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
322      this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta);
323      this->addTexCoor(0,0); //FIXME
324      this->addColor(.125,.436,.246);
[4796]325
326    }
[6310]327  }
328  for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
329  {
330    this->addIndice(loopSegmentNumber);
331    this->addIndice(segmentsPerLoop + loopSegmentNumber);
332  }
333  for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber)
334  {
335    for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber)
[4793]336    {
[6310]337      this->addIndice( ((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber);
338      this->addIndice( ((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber);
[4793]339    }
[6310]340  }
[2821]341}
[6310]342
343
344/**
345 * @brief print out some nice debug information about this VertexArrayModel.
346 */
347void VertexArrayModel::debug() const
348{
349  PRINT(0)("VertexArrayModel (%s): debug\n", this->getName());
350  PRINT(0)("Stripes: %d; Indices: %d; Vertices: %d; Normals %d; TextCoords %d; Colors %d\n",
351            this->stripes.size(),
352            this->indices.size(),
[6453]353            this->vertices.size(),
354            this->normals.size(),
[6310]355            this->texCoords.size()/2,
[6453]356            this->colors.size() );
[6310]357  for (GLuint i = 1; i < this->stripes.size(); ++i)
358  {
359    PRINT(0)("Stripe-%d (s:%d:e:%d):: ", i, this->stripes[i-1], this->stripes[i]);
360    for (GLuint j = this->stripes[i-1] ; j < this->stripes[i]; j++)
361    {
[6314]362      PRINT(0)("->%d", this->indices[j]);
[6310]363    }
364    PRINT(0)("\n");
365  }
366}
Note: See TracBrowser for help on using the repository browser.