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