Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6134 was 6038, checked in by bensch, 19 years ago

orxonox/trunk: small optimisation

File size: 4.4 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
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  this->setClassID(CL_MODEL, "VertexArrayModel");
36
37  this->bFinalized = false;
38  this->newStripe();
39}
40
41
42/**
43 * @brief deletes an VertexArrayModel.
44 *
45 * Looks if any from model allocated space is still in use, and if so deleted it.
46 */
47VertexArrayModel::~VertexArrayModel()
48{
49  PRINTF(4)("Deleting VertexArrayModel ");
50  if (this->getName())
51  {
52    PRINT(4)("%s\n", this->getName());
53  }
54  else
55  {
56    PRINT(4)("\n");
57  }
58}
59
60
61/**
62 * @brief Draws the VertexArrayModels of all Groups.
63 *
64 * It does this by just calling the Lists that must have been created earlier.
65 */
66void VertexArrayModel::draw() const
67{
68  PRINTF(4)("drawing the 3D-VertexArrayModels\n");
69
70  glEnableClientState(GL_VERTEX_ARRAY |
71                      GL_TEXTURE_COORD_ARRAY |
72                      GL_NORMAL_ARRAY);
73  //  glEnableClientState(GL_INDEX_ARRAY);
74
75  glVertexPointer(3, GL_FLOAT, 0, this->vertices.getArray());
76  glNormalPointer(GL_FLOAT, 0, this->normals.getArray());
77  glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords.getArray()); 
78
79  for (GLuint i = 1; i < this->stripes.size(); ++i)
80    {
81      glDrawRangeElements(GL_TRIANGLE_STRIP,
82                          this->stripes[i-1],
83                          this->stripes[i],
84                          this->indices.getCount(),
85                          GL_UNSIGNED_BYTE,
86                          this->indices.getArray());
87    }
88}
89
90
91//////////
92// MESH //
93//////////
94/**
95 * @brief generates a new Stripe in this Model
96 */
97void VertexArrayModel::newStripe()
98{
99  this->stripes.push_back(this->vertices.getCount()-1);
100}
101
102
103/**
104 * @brief parses a vertex-String
105 * @param x the X-coordinate of the Vertex to add.
106 * @param y the Y-coordinate of the Vertex to add.
107 * @param z the Z-coordinate of the Vertex to add.
108 */
109void VertexArrayModel::addVertex(float x, float y, float z)
110{
111  this->vertices.addEntry(x, y, z);
112  this->pModelInfo.numVertices++;
113}
114
115
116/**
117 * @brief adds a VertexNormal.
118 * @param x The x coordinate of the Normal.
119 * @param y The y coordinate of the Normal.
120 * @param z The z coordinate of the Normal.
121 *
122 * If a vertexNormal line is found this function will inject it into the vertexNormal-Array
123 */
124void VertexArrayModel::addNormal(float x, float y, float z)
125{
126  this->normals.addEntry(x, y, z);
127  this->pModelInfo.numNormals++;
128}
129
130
131/**
132 *  adds a Texture Coordinate
133 * @param u The u coordinate of the TextureCoordinate.
134 * @param v The y coordinate of the TextureCoordinate.
135 *
136 *  If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
137 */
138void VertexArrayModel::addTexCoor(float u, float v)
139{
140  this->texCoords.addEntry(u);
141  this->texCoords.addEntry(v);
142  this->pModelInfo.numTexCoor++;
143}
144
145
146/**
147 *  adds a new Face
148 * @param faceElemCount the number of Vertices to add to the Face.
149 * @param type The information Passed with each Vertex
150*/
151void VertexArrayModel::addIndice(GLubyte indice)
152{
153  this->indices.addEntry(indice);
154}
155
156
157/**
158 * @brief Finalizes an Object. This can be done outside of the Class.
159 */
160void VertexArrayModel::finalize()
161{
162  // finalize the Arrays
163  this->vertices.finalizeArray();
164  this->texCoords.finalizeArray();
165  this->normals.finalizeArray();
166  this->indices.finalizeArray();
167
168  this->newStripe();
169
170  /*
171    glEnableClientState(GL_VERTEX_ARRAY |
172    GL_TEXTURE_COORD_ARRAY |
173    GL_NORMAL_ARRAY);
174  */
175
176  this->bFinalized = true;
177}
178
179
180
181/////////////
182// TESTING //
183/////////////
184/**
185 * @brief Includes a default model
186 *
187 * This will inject a Cube, because this is the most basic model.
188 */
189void VertexArrayModel::planeModel()
190{
191  unsigned int i, j;
192  for (i = 0; i < 20; i++)
193    {
194      for (j = 0; j < 20; j++)
195        {
196          this->addVertex(i* 50, .5, (j)*50);
197          this->addNormal(0, 1, 0);
198          this->addTexCoor((float)i/20.0, (float)j/20.0);
199
200        }
201    }
202  for (i = 0; i < 20; i++)
203    {
204      this->addIndice(i);
205      this->addIndice(i+20);
206    }
207}
Note: See TracBrowser for help on using the repository browser.