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 | using namespace std; |
---|
24 | |
---|
25 | ///////////// |
---|
26 | /// MODEL /// |
---|
27 | ///////////// |
---|
28 | /** |
---|
29 | * @brief Creates a 3D-VertexArrayModel. |
---|
30 | * |
---|
31 | * assigns it a Name and a Type |
---|
32 | */ |
---|
33 | VertexArrayModel::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 | */ |
---|
47 | VertexArrayModel::~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 | */ |
---|
66 | void VertexArrayModel::draw() const |
---|
67 | { |
---|
68 | PRINTF(4)("drawing the 3D-VertexArrayModels\n"); |
---|
69 | glDisable(GL_LIGHTING); |
---|
70 | |
---|
71 | glEnableClientState(GL_VERTEX_ARRAY ); |
---|
72 | glEnableClientState(GL_TEXTURE_COORD_ARRAY ); |
---|
73 | glEnableClientState(GL_NORMAL_ARRAY ); |
---|
74 | glEnableClientState(GL_COLOR_ARRAY ); |
---|
75 | |
---|
76 | |
---|
77 | glVertexPointer(3, GL_FLOAT, 0, &this->vertices[0]); |
---|
78 | glNormalPointer(GL_FLOAT, 0, &this->normals[0]); |
---|
79 | glTexCoordPointer(2, GL_FLOAT, 0, &this->texCoords[0]); |
---|
80 | glColorPointer(3, GL_FLOAT, 0, &this->colors[0]); |
---|
81 | |
---|
82 | for (GLuint i = 1; ++i < this->stripes.size(); ++i) |
---|
83 | { |
---|
84 | glDrawElements( GL_TRIANGLES, |
---|
85 | this->stripes[i] - this->stripes[i-1], |
---|
86 | GL_UNSIGNED_BYTE, |
---|
87 | &this->indices[this->stripes[i-1]] ); |
---|
88 | } |
---|
89 | glEnable(GL_LIGHTING); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | ////////// |
---|
94 | // MESH // |
---|
95 | ////////// |
---|
96 | /** |
---|
97 | * @brief generates a new Stripe in this Model |
---|
98 | */ |
---|
99 | void VertexArrayModel::newStripe() |
---|
100 | { |
---|
101 | // no stripes of size 0 |
---|
102 | if (this->stripes.empty() || this->indices.size() != this->stripes.back()) |
---|
103 | this->stripes.push_back(this->indices.size()); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * @brief parses a vertex-String |
---|
109 | * @param x the X-coordinate of the Vertex to add. |
---|
110 | * @param y the Y-coordinate of the Vertex to add. |
---|
111 | * @param z the Z-coordinate of the Vertex to add. |
---|
112 | */ |
---|
113 | void VertexArrayModel::addVertex(float x, float y, float z) |
---|
114 | { |
---|
115 | this->vertices.push_back(x); |
---|
116 | this->vertices.push_back(y); |
---|
117 | this->vertices.push_back(z); |
---|
118 | this->pModelInfo.numVertices++; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /** |
---|
123 | * @brief adds a VertexNormal. |
---|
124 | * @param x The x coordinate of the Normal. |
---|
125 | * @param y The y coordinate of the Normal. |
---|
126 | * @param z The z coordinate of the Normal. |
---|
127 | * |
---|
128 | * If a vertexNormal line is found this function will inject it into the vertexNormal-Array |
---|
129 | */ |
---|
130 | void VertexArrayModel::addNormal(float x, float y, float z) |
---|
131 | { |
---|
132 | this->normals.push_back(x); |
---|
133 | this->normals.push_back(y); |
---|
134 | this->normals.push_back(z); |
---|
135 | this->pModelInfo.numNormals++; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief adds a Texture Coordinate |
---|
141 | * @param u The u coordinate of the TextureCoordinate. |
---|
142 | * @param v The y coordinate of the TextureCoordinate. |
---|
143 | * |
---|
144 | * If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array |
---|
145 | */ |
---|
146 | void VertexArrayModel::addTexCoor(float u, float v) |
---|
147 | { |
---|
148 | this->texCoords.push_back(u); |
---|
149 | this->texCoords.push_back(v); |
---|
150 | this->pModelInfo.numTexCoor++; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * @brief adds a new Color |
---|
155 | * @param r the Red Component of the VertexColor to add. |
---|
156 | * @param g the Green Component of the VertexColor to add. |
---|
157 | * @param b the Blue of the VertexColor to add. |
---|
158 | */ |
---|
159 | void VertexArrayModel::addColor(float r, float g, float b) |
---|
160 | { |
---|
161 | this->colors.push_back(r); |
---|
162 | this->colors.push_back(g); |
---|
163 | this->colors.push_back(b); |
---|
164 | // FIXME |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | * adds a new Face |
---|
170 | * @param faceElemCount the number of Vertices to add to the Face. |
---|
171 | * @param type The information Passed with each Vertex |
---|
172 | */ |
---|
173 | void VertexArrayModel::addIndice(GLubyte indice) |
---|
174 | { |
---|
175 | this->indices.push_back(indice); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | /** |
---|
180 | * @brief Finalizes an Object. This can be done outside of the Class. |
---|
181 | */ |
---|
182 | void VertexArrayModel::finalize() |
---|
183 | { |
---|
184 | // finalize the Arrays |
---|
185 | this->newStripe(); |
---|
186 | |
---|
187 | /* |
---|
188 | glEnableClientState(GL_VERTEX_ARRAY | |
---|
189 | GL_TEXTURE_COORD_ARRAY | |
---|
190 | GL_NORMAL_ARRAY); |
---|
191 | */ |
---|
192 | |
---|
193 | this->bFinalized = true; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | ///////////// |
---|
199 | // TESTING // |
---|
200 | ///////////// |
---|
201 | /** |
---|
202 | * @brief Includes a default model |
---|
203 | * |
---|
204 | * This will inject a Cube, because this is the most basic model. |
---|
205 | */ |
---|
206 | void VertexArrayModel::planeModel() |
---|
207 | { |
---|
208 | unsigned int sizeX = 20, sizeY = 20; |
---|
209 | |
---|
210 | unsigned int i, j; |
---|
211 | for (i = 0; i < sizeY; i++) |
---|
212 | { |
---|
213 | for (j = 0; j < sizeX; j++) |
---|
214 | { |
---|
215 | this->addVertex((float)i - (float)sizeY/2.0, sin((float)i/(float)sizeY*5.0)*cos((float)j/(float)sizeX*5.0)*5.0, (float)j - (float)sizeX/2.0); |
---|
216 | this->addNormal(0.0, 1, 0.0); |
---|
217 | this->addTexCoor((float)i/(float)sizeY, (float)j/(float)sizeY); |
---|
218 | this->addColor((float)i/20.0, 0.0, (float)j/20.0); |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | for (i = 0; i < sizeY-1; i++) |
---|
223 | { |
---|
224 | for (j = 0; j < sizeX; j++) |
---|
225 | { |
---|
226 | this->addIndice( sizeY*i + j ); |
---|
227 | this->addIndice( sizeY*(i+1) + j ); |
---|
228 | } |
---|
229 | this->newStripe(); |
---|
230 | this->debug(); |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | #include <cmath> |
---|
235 | |
---|
236 | void VertexArrayModel::spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop) |
---|
237 | { |
---|
238 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
239 | { |
---|
240 | float theta = 0; |
---|
241 | float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop; |
---|
242 | float sinTheta = std::sin(theta); |
---|
243 | float sinPhi = std::sin(phi); |
---|
244 | float cosTheta = std::cos(theta); |
---|
245 | float cosPhi = std::cos(phi); |
---|
246 | this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); |
---|
247 | this->addNormal(1,1,1); |
---|
248 | this->addTexCoor(0,0); |
---|
249 | this->addColor(.125,.436,.246); |
---|
250 | } |
---|
251 | for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber) |
---|
252 | { |
---|
253 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
254 | { |
---|
255 | float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops)); |
---|
256 | if (loopNumber == loops) |
---|
257 | { |
---|
258 | theta = PI; |
---|
259 | } |
---|
260 | float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop; |
---|
261 | float sinTheta = std::sin(theta); |
---|
262 | float sinPhi = std::sin(phi); |
---|
263 | float cosTheta = std::cos(theta); |
---|
264 | float cosPhi = std::cos(phi); |
---|
265 | this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); |
---|
266 | this->addNormal(1,1,1); |
---|
267 | this->addTexCoor(0,0); |
---|
268 | this->addColor(.125,.436,.246); |
---|
269 | |
---|
270 | } |
---|
271 | } |
---|
272 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
273 | { |
---|
274 | this->addIndice(loopSegmentNumber); |
---|
275 | this->addIndice(segmentsPerLoop + loopSegmentNumber); |
---|
276 | } |
---|
277 | for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber) |
---|
278 | { |
---|
279 | for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) |
---|
280 | { |
---|
281 | this->addIndice( ((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber); |
---|
282 | this->addIndice( ((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber); |
---|
283 | } |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | void VertexArrayModel::debug() const |
---|
290 | { |
---|
291 | PRINT(0)("VertexArrayModel (%s): debug\n", this->getName()); |
---|
292 | PRINT(0)("Stripes: %d; Indices: %d; Vertices: %d; Normals %d; TextCoords %d; Colors %d\n", |
---|
293 | this->stripes.size(), |
---|
294 | this->indices.size(), |
---|
295 | this->vertices.size()/3, |
---|
296 | this->normals.size()/3, |
---|
297 | this->texCoords.size()/2, |
---|
298 | this->colors.size() )/3; |
---|
299 | for (GLuint i = 1; i < this->stripes.size(); ++i) |
---|
300 | { |
---|
301 | PRINT(0)("Stripe-%d ::", i); |
---|
302 | for (GLuint j = this->stripes[i-1] ; j < this->stripes[i]; j++) |
---|
303 | { |
---|
304 | PRINT(0)("%d->", this->indices[j]); |
---|
305 | } |
---|
306 | PRINT(0)("\n"); |
---|
307 | } |
---|
308 | } |
---|