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