Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

heightMap: vertex-array draws as it should

File size: 5.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
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  glEnableClientState(GL_TEXTURE_COORD_ARRAY );
72  glEnableClientState(GL_NORMAL_ARRAY );
73  glEnableClientState(GL_COLOR_ARRAY );
74
75
76  glVertexPointer(3, GL_FLOAT, 0, this->vertices.getArray());
77  glNormalPointer(GL_FLOAT, 0, this->normals.getArray());
78  glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords.getArray());
79  glColorPointer(3, GL_FLOAT, 0, this->colors.getArray());
80
81//      glDrawElements(GL_TRIANGLE_STRIP, this->indices.getCount(), GL_UNSIGNED_BYTE, this->indices.getArray());
82  for (GLuint i = 1; i < this->stripes.size(); ++i)
83    {
84      glDrawRangeElements(GL_TRIANGLE_STRIP,
85                          this->stripes[i-1],
86                          this->stripes[i],
87                          this->indices.getCount(),
88                          GL_UNSIGNED_BYTE,
89                          this->indices.getArray());
90    }
91}
92
93
94//////////
95// MESH //
96//////////
97/**
98 * @brief generates a new Stripe in this Model
99 */
100void VertexArrayModel::newStripe()
101{
102  this->stripes.push_back(this->vertices.getCount()-1);
103}
104
105
106/**
107 * @brief parses a vertex-String
108 * @param x the X-coordinate of the Vertex to add.
109 * @param y the Y-coordinate of the Vertex to add.
110 * @param z the Z-coordinate of the Vertex to add.
111 */
112void VertexArrayModel::addVertex(float x, float y, float z)
113{
114  this->vertices.addEntry(x, y, z);
115  this->pModelInfo.numVertices++;
116}
117
118
119/**
120 * @brief adds a VertexNormal.
121 * @param x The x coordinate of the Normal.
122 * @param y The y coordinate of the Normal.
123 * @param z The z coordinate of the Normal.
124 *
125 * If a vertexNormal line is found this function will inject it into the vertexNormal-Array
126 */
127void VertexArrayModel::addNormal(float x, float y, float z)
128{
129  this->normals.addEntry(x, y, z);
130  this->pModelInfo.numNormals++;
131}
132
133
134/**
135 * @brief adds a Texture Coordinate
136 * @param u The u coordinate of the TextureCoordinate.
137 * @param v The y coordinate of the TextureCoordinate.
138 *
139 *  If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array
140 */
141void VertexArrayModel::addTexCoor(float u, float v)
142{
143  this->texCoords.addEntry(u);
144  this->texCoords.addEntry(v);
145  this->pModelInfo.numTexCoor++;
146}
147
148/**
149 * @brief adds a new Color
150 * @param r the Red Component of the VertexColor to add.
151 * @param g the Green Component of the VertexColor to add.
152 * @param b the Blue of the VertexColor to add.
153 */
154void VertexArrayModel::addColor(float r, float g, float b)
155{
156  this->colors.addEntry(r, g, b);
157  // FIXME
158}
159
160
161/**
162 *  adds a new Face
163 * @param faceElemCount the number of Vertices to add to the Face.
164 * @param type The information Passed with each Vertex
165*/
166void VertexArrayModel::addIndice(GLubyte indice)
167{
168  this->indices.addEntry(indice);
169}
170
171
172/**
173 * @brief Finalizes an Object. This can be done outside of the Class.
174 */
175void VertexArrayModel::finalize()
176{
177  // finalize the Arrays
178  this->vertices.finalizeArray();
179  this->texCoords.finalizeArray();
180  this->normals.finalizeArray();
181  this->colors.finalizeArray();
182  this->indices.finalizeArray();
183
184  this->newStripe();
185
186  /*
187    glEnableClientState(GL_VERTEX_ARRAY |
188    GL_TEXTURE_COORD_ARRAY |
189    GL_NORMAL_ARRAY);
190  */
191
192  this->bFinalized = true;
193}
194
195
196
197/////////////
198// TESTING //
199/////////////
200/**
201* @brief Includes a default model
202*
203* This will inject a Cube, because this is the most basic model.
204*/
205void VertexArrayModel::planeModel()
206{
207  unsigned int sizeX = 20, sizeY = 10;
208
209  unsigned int i, j;
210  for (i = 0; i < sizeY; i++)
211    {
212      for (j = 0; j < sizeX; j++)
213        {
214          this->addVertex((float)i - sizeY/2.0, -(float)i * (float)j/(float)sizeY/(float)sizeX * 10.0 , (float)(j) - sizeX/2.0);
215          this->addNormal((float)i/(float)sizeY, 1, (float)j/(float)sizeX);
216          this->addTexCoor((float)i/(float)sizeY, (float)j/(float)sizeY);
217          this->addColor((float)i/20.0, 0.0, (float)j/20.0);
218        }
219    }
220
221  for (i = 0; i < sizeY-1; i++)
222  {
223    for (j = 0; j < sizeX; j++)
224    {
225      this->addIndice(sizeX*i + j);
226      this->addIndice(sizeX*(i+1) + j);
227    }
228    if (i < sizeY -1 )
229      this->newStripe();
230  }
231}
Note: See TracBrowser for help on using the repository browser.