Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some new tc-stuff

File size: 6.3 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->bFinalized = false;
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->setClassID(CL_MODEL, "VertexArrayModel");
53  this->bFinalized = false;
54
55  // The acTC object generating this Model. //
56  ACTCData *tc;
57  tc = actcNew();
58  if(tc == NULL) {
59    /* memory allocation failed */
60    /* print error here and exit or whatever */
61  }
62
63  // inputing the data of model to the tc
64  actcBeginInput(tc);
65  for(unsigned int i = 0; i < model.getTriangleCount(); i++)
66      actcAddTriangle(tc, model.getTriangles()[i].indexToVertices[0], model.getTriangles()[i].indexToVertices[1], model.getTriangles()[i].indexToVertices[2]);
67  actcEndInput(tc);
68
69  // importing the data to the new Model.
70  this->newStripe();
71
72  for (unsigned int i = 0; i < model.getVertexCount(); i+=3)
73    this->addVertex(model.getVertexArray()[i], model.getVertexArray()[i+1], model.getVertexArray()[i+2]);
74  for (unsigned int i = 0; i < model.getNormalsCount(); i+=3)
75    this->addNormal(model.getNormalsArray()[i], model.getNormalsArray()[i+1], model.getNormalsArray()[i+2]);
76  for (unsigned int i = 0; i < model.getTexCoordCount(); i+=2)
77    this->addTexCoor(model.getTexCoordArray()[i], model.getTexCoordArray()[i+1]);
78
79
80  int prim;
81  unsigned int v1, v2, v3;
82
83  actcBeginOutput(tc);
84  while((prim = actcStartNextPrim(tc, &v1, &v2) != ACTC_DATABASE_EMPTY))
85  {
86    this->addIndice(v1);
87    this->addIndice(v2);
88    printf("%d\n", v1);
89    printf("%d\n", v2);
90    /* start a primitive of type "prim" with v1 and v2 */
91    while(actcGetNextVert(tc, &v3) != ACTC_PRIM_COMPLETE)
92    {
93      /* continue primitive using v3 */
94      this->addIndice(v3);
95      printf("%d\n", v3);
96    }
97    this->newStripe();
98  }
99  actcEndOutput(tc);
100
101  this->finalize();
102}
103
104
105/**
106 * @brief deletes a VertexArrayModel.
107 *
108 * Looks if any from model allocated space is still in use, and if so deleted it.
109 */
110VertexArrayModel::~VertexArrayModel()
111{
112  PRINTF(4)("Deleting VertexArrayModel ");
113  if (this->getName())
114  {
115    PRINT(4)("%s\n", this->getName());
116  }
117  else
118  {
119    PRINT(4)("\n");
120  }
121}
122
123
124/**
125 * @brief Draws the VertexArrayModels of all Groups.
126 *
127 * It does this by just calling the Lists that must have been created earlier.
128 */
129void VertexArrayModel::draw() const
130{
131  PRINTF(4)("drawing the 3D-VertexArrayModels\n");
132
133  glEnableClientState(GL_VERTEX_ARRAY |
134                      GL_TEXTURE_COORD_ARRAY |
135                      GL_NORMAL_ARRAY);
136  //  glEnableClientState(GL_INDEX_ARRAY);
137
138  glVertexPointer(3, GL_FLOAT, 0, this->vertices.getArray());
139  glNormalPointer(GL_FLOAT, 0, this->normals.getArray());
140  glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords.getArray());
141
142  for (GLuint i = 1; i < this->stripes.size(); ++i)
143    {
144      glDrawRangeElements(GL_TRIANGLE_STRIP,
145                          this->stripes[i-1],
146                          this->stripes[i],
147                          this->indices.getCount(),
148                          GL_UNSIGNED_BYTE,
149                          this->indices.getArray());
150    }
151}
152
153
154//////////
155// MESH //
156//////////
157/**
158 * @brief generates a new Stripe in this Model
159 */
160void VertexArrayModel::newStripe()
161{
162  this->stripes.push_back(this->vertices.getCount()-1);
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.addEntry(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.addEntry(x, y, z);
190  this->pModelInfo.numNormals++;
191}
192
193
194/**
195 *  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.addEntry(u);
204  this->texCoords.addEntry(v);
205  this->pModelInfo.numTexCoor++;
206}
207
208
209/**
210 *  adds a new Face
211 * @param faceElemCount the number of Vertices to add to the Face.
212 * @param type The information Passed with each Vertex
213*/
214void VertexArrayModel::addIndice(GLubyte indice)
215{
216  this->indices.addEntry(indice);
217}
218
219
220/**
221 * @brief Finalizes an Object. This can be done outside of the Class.
222 */
223void VertexArrayModel::finalize()
224{
225  // finalize the Arrays
226  this->vertices.finalizeArray();
227  this->texCoords.finalizeArray();
228  this->normals.finalizeArray();
229  this->indices.finalizeArray();
230
231  this->newStripe();
232
233  /*
234    glEnableClientState(GL_VERTEX_ARRAY |
235    GL_TEXTURE_COORD_ARRAY |
236    GL_NORMAL_ARRAY);
237  */
238
239  this->bFinalized = true;
240}
241
242
243
244/////////////
245// TESTING //
246/////////////
247/**
248 * @brief Includes a default model
249 *
250 * This will inject a Cube, because this is the most basic model.
251 */
252void VertexArrayModel::planeModel()
253{
254  unsigned int i, j;
255  for (i = 0; i < 20; i++)
256    {
257      for (j = 0; j < 20; j++)
258        {
259          this->addVertex(i* 50, .5, (j)*50);
260          this->addNormal(0, 1, 0);
261          this->addTexCoor((float)i/20.0, (float)j/20.0);
262
263        }
264    }
265  for (i = 0; i < 20; i++)
266    {
267      this->addIndice(i);
268      this->addIndice(i+20);
269    }
270}
Note: See TracBrowser for help on using the repository browser.