1 | /* File: sglmodel.h; Mode: C++; Tab-width: 3; Author: Simon Flannery; */ |
---|
2 | |
---|
3 | /* |
---|
4 | The Silicon Graphic Library model format is a light weight, simple and |
---|
5 | flexible format designed for the Open Graphics Library. The format allows |
---|
6 | static models to be streamed promptly within the Open Graphics Library |
---|
7 | framework. This is a high performance implementation and currently only |
---|
8 | supports the GL_V3F, GL_N3F_V3F, GL_T2F_N3F_V3F vertex formats! |
---|
9 | |
---|
10 | The binary file format is described: |
---|
11 | - The number of elements. |
---|
12 | - The format of the elements (for example GL_V3F), which determines the size of |
---|
13 | each element. |
---|
14 | - The mode of the elements (for example GL_TRIANGLES). |
---|
15 | - The array of data. |
---|
16 | |
---|
17 | This Implementation is Copyright (c) 2005 Simon Flannery. |
---|
18 | */ |
---|
19 | |
---|
20 | #ifndef SGLMODEL_H |
---|
21 | #define SGLMODEL_H |
---|
22 | |
---|
23 | #include <stdio.h> |
---|
24 | #include <stdlib.h> |
---|
25 | #include <math.h> |
---|
26 | |
---|
27 | enum {x, y, z, w}; |
---|
28 | |
---|
29 | #pragma pack(push) |
---|
30 | #pragma pack(1) |
---|
31 | |
---|
32 | struct texture |
---|
33 | { |
---|
34 | float u, v; |
---|
35 | }; |
---|
36 | |
---|
37 | struct vertex |
---|
38 | { |
---|
39 | // GL_V3F |
---|
40 | float m[3]; |
---|
41 | |
---|
42 | float& operator[](int i) |
---|
43 | { |
---|
44 | return m[i]; |
---|
45 | } |
---|
46 | |
---|
47 | float operator[](int i) const |
---|
48 | { |
---|
49 | return m[i]; |
---|
50 | } |
---|
51 | }; |
---|
52 | |
---|
53 | struct color_vertex |
---|
54 | { |
---|
55 | // GL_C4UB_V3F |
---|
56 | unsigned char r, g, b, a; |
---|
57 | vertex v; |
---|
58 | }; |
---|
59 | |
---|
60 | struct normal_vertex |
---|
61 | { |
---|
62 | // GL_N3F_V3F |
---|
63 | vertex n; |
---|
64 | vertex v; |
---|
65 | }; |
---|
66 | |
---|
67 | struct texture_vertex |
---|
68 | { |
---|
69 | // GL_T2F_N3F_V3F |
---|
70 | texture t; |
---|
71 | vertex n; |
---|
72 | vertex v; |
---|
73 | }; |
---|
74 | |
---|
75 | struct object_vertex |
---|
76 | { |
---|
77 | vertex position; |
---|
78 | vertex normal; |
---|
79 | texture texcoord; |
---|
80 | }; |
---|
81 | |
---|
82 | #pragma pack(pop) |
---|
83 | |
---|
84 | float my_max(float a, float b) |
---|
85 | { |
---|
86 | return (a > b) ? a : b; |
---|
87 | } |
---|
88 | |
---|
89 | class Sglmodel |
---|
90 | { |
---|
91 | public: |
---|
92 | virtual bool Load(char* szFileName) = 0; |
---|
93 | virtual void GetDimensions(float& width, float& height, float& depth) = 0; |
---|
94 | virtual void GetCenter(float& cx, float& cy, float& cz) = 0; |
---|
95 | virtual void Unitize() = 0; |
---|
96 | virtual void Scale(float scale) = 0; |
---|
97 | virtual void Translate(float tx, float ty, float tz) = 0; |
---|
98 | |
---|
99 | virtual void Render() const = 0; |
---|
100 | |
---|
101 | virtual void Delete() = 0; |
---|
102 | }; |
---|
103 | |
---|
104 | class Sglmodel_sgl : public Sglmodel |
---|
105 | { |
---|
106 | public: |
---|
107 | typedef void (Sglmodel_sgl::*p_Sglmodel_sgl_member)(); |
---|
108 | typedef void (Sglmodel_sgl::*p_Sglmodel_sgl_member_1f)(float); |
---|
109 | typedef void (Sglmodel_sgl::*p_Sglmodel_sgl_member_3f)(float, float, float); |
---|
110 | typedef void (Sglmodel_sgl::*p_Sglmodel_sgl_member_3f_reference)(float&, float&, float&); |
---|
111 | |
---|
112 | virtual bool Load(char* szFileName); |
---|
113 | virtual void GetDimensions(float& width, float& height, float& depth); |
---|
114 | virtual void GetCenter(float& cx, float& cy, float& cz); |
---|
115 | virtual void Unitize(); |
---|
116 | virtual void Scale(float scale); |
---|
117 | virtual void Translate(float tx, float ty, float tz); |
---|
118 | |
---|
119 | virtual void Render() const; |
---|
120 | |
---|
121 | bool Save(char* szFileName) const; |
---|
122 | virtual void Delete(); |
---|
123 | |
---|
124 | void SetTexture(unsigned int t) { id = t; return; } |
---|
125 | unsigned int GetTexture() const { return id; } |
---|
126 | |
---|
127 | private: |
---|
128 | |
---|
129 | void GL_V3F_Dimensions(float& width, float& height, float& depth); |
---|
130 | void GL_N3F_V3F_Dimensions(float& width, float& height, float& depth); |
---|
131 | void GL_T2F_N3F_V3F_Dimensions(float& width, float& height, float& depth); |
---|
132 | void GL_V3F_Center(float& cx, float& cy, float& cz); |
---|
133 | void GL_N3F_V3F_Center(float& cx, float& cy, float& cz); |
---|
134 | void GL_T2F_N3F_V3F_Center(float& cx, float& cy, float& cz); |
---|
135 | void GL_V3F_Unitize(); |
---|
136 | void GL_N3F_V3F_Unitize(); |
---|
137 | void GL_T2F_N3F_V3F_Unitize(); |
---|
138 | void GL_V3F_Scale(float scale); |
---|
139 | void GL_N3F_V3F_Scale(float scale); |
---|
140 | void GL_T2F_N3F_V3F_Scale(float scale); |
---|
141 | void GL_V3F_Translate(float tx, float ty, float tz); |
---|
142 | void GL_N3F_V3F_Translate(float tx, float tt, float tz); |
---|
143 | void GL_T2F_N3F_V3F_Translate(float tx, float ty, float tz); |
---|
144 | |
---|
145 | void* data; |
---|
146 | int format, count, mode; |
---|
147 | unsigned int id; |
---|
148 | |
---|
149 | p_Sglmodel_sgl_member_3f_reference pDimensions; |
---|
150 | p_Sglmodel_sgl_member_3f_reference pCenter; |
---|
151 | p_Sglmodel_sgl_member pUnitize; |
---|
152 | p_Sglmodel_sgl_member_1f pScale; |
---|
153 | p_Sglmodel_sgl_member_3f pTranslate; |
---|
154 | }; |
---|
155 | |
---|
156 | bool Sglmodel_sgl::Load(char* szFileName) |
---|
157 | { |
---|
158 | data = NULL; format = count = mode = 0; id = 0; |
---|
159 | pDimensions = NULL; |
---|
160 | pCenter = NULL; |
---|
161 | pUnitize = NULL; |
---|
162 | pScale = NULL; |
---|
163 | pTranslate = NULL; |
---|
164 | |
---|
165 | FILE* file = fopen(szFileName, "rb"); |
---|
166 | |
---|
167 | if (file != NULL) |
---|
168 | { |
---|
169 | if (fread(&count, sizeof(int), 1, file) > 0 && count > 0) |
---|
170 | { |
---|
171 | if (fread(&format, sizeof(int), 1, file) > 0 && format > 0) |
---|
172 | { |
---|
173 | if (fread(&mode, sizeof(int), 1, file) > 0 && mode > 0) |
---|
174 | { |
---|
175 | size_t size = 0; |
---|
176 | |
---|
177 | switch (format) /* Setup member function pointers. */ |
---|
178 | { |
---|
179 | case GL_V3F: |
---|
180 | size = sizeof(vertex); |
---|
181 | pDimensions = &Sglmodel_sgl::GL_V3F_Dimensions; |
---|
182 | pCenter = &Sglmodel_sgl::GL_V3F_Center; |
---|
183 | pScale = &Sglmodel_sgl::GL_V3F_Scale; |
---|
184 | pUnitize = &Sglmodel_sgl::GL_V3F_Unitize; |
---|
185 | pTranslate = &Sglmodel_sgl::GL_V3F_Translate; |
---|
186 | break; |
---|
187 | |
---|
188 | case GL_N3F_V3F: |
---|
189 | size = sizeof(normal_vertex); |
---|
190 | pDimensions = &Sglmodel_sgl::GL_N3F_V3F_Dimensions; |
---|
191 | pCenter = &Sglmodel_sgl::GL_N3F_V3F_Center; |
---|
192 | pScale = &Sglmodel_sgl::GL_N3F_V3F_Scale; |
---|
193 | pUnitize = &Sglmodel_sgl::GL_N3F_V3F_Unitize; |
---|
194 | pTranslate = &Sglmodel_sgl::GL_N3F_V3F_Translate; |
---|
195 | break; |
---|
196 | |
---|
197 | case GL_T2F_N3F_V3F: |
---|
198 | size = sizeof(texture_vertex); |
---|
199 | pDimensions = &Sglmodel_sgl::GL_T2F_N3F_V3F_Dimensions; |
---|
200 | pCenter = &Sglmodel_sgl::GL_T2F_N3F_V3F_Center; |
---|
201 | pScale = &Sglmodel_sgl::GL_T2F_N3F_V3F_Scale; |
---|
202 | pUnitize = &Sglmodel_sgl::GL_T2F_N3F_V3F_Unitize; |
---|
203 | pTranslate = &Sglmodel_sgl::GL_T2F_N3F_V3F_Translate; |
---|
204 | break; |
---|
205 | } |
---|
206 | |
---|
207 | if (size != 0) |
---|
208 | { |
---|
209 | if ((data = malloc(size * count)) != NULL) |
---|
210 | { |
---|
211 | if (fread(data, size, count, file) != count) |
---|
212 | { |
---|
213 | Delete(); |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | fclose(file); |
---|
222 | } |
---|
223 | |
---|
224 | return data != NULL; |
---|
225 | } |
---|
226 | |
---|
227 | void Sglmodel_sgl::GetDimensions(float& width, float& height, float& depth) |
---|
228 | { |
---|
229 | if (pDimensions != NULL) |
---|
230 | { |
---|
231 | (this->*pDimensions)(width, height, depth); |
---|
232 | } |
---|
233 | |
---|
234 | return; |
---|
235 | } |
---|
236 | |
---|
237 | void Sglmodel_sgl::GetCenter(float& cx, float& cy, float& cz) |
---|
238 | { |
---|
239 | if (pCenter != NULL) |
---|
240 | { |
---|
241 | (this->*pCenter)(cx, cy, cz); |
---|
242 | } |
---|
243 | |
---|
244 | return; |
---|
245 | } |
---|
246 | |
---|
247 | void Sglmodel_sgl::Unitize() |
---|
248 | { |
---|
249 | if (pUnitize != NULL) |
---|
250 | { |
---|
251 | (this->*pUnitize)(); |
---|
252 | } |
---|
253 | |
---|
254 | return; |
---|
255 | } |
---|
256 | |
---|
257 | void Sglmodel_sgl::Scale(float scale) |
---|
258 | { |
---|
259 | if (pScale != NULL) |
---|
260 | { |
---|
261 | (this->*pScale)(scale); |
---|
262 | } |
---|
263 | |
---|
264 | return; |
---|
265 | } |
---|
266 | |
---|
267 | void Sglmodel_sgl::Translate(float tx, float ty, float tz) |
---|
268 | { |
---|
269 | if (pTranslate != NULL) |
---|
270 | { |
---|
271 | (this->*pTranslate)(tx, ty, tz); |
---|
272 | } |
---|
273 | |
---|
274 | return; |
---|
275 | } |
---|
276 | |
---|
277 | void Sglmodel_sgl::Render() const |
---|
278 | { |
---|
279 | if (data != NULL) |
---|
280 | { |
---|
281 | glInterleavedArrays(format, 0, data); |
---|
282 | glDrawArrays(mode, 0, count); |
---|
283 | } |
---|
284 | |
---|
285 | return; |
---|
286 | } |
---|
287 | |
---|
288 | bool Sglmodel_sgl::Save(char* szFileName) const |
---|
289 | { |
---|
290 | if (data != NULL) |
---|
291 | { |
---|
292 | FILE* file = fopen(szFileName, "wb"); |
---|
293 | |
---|
294 | if (file != NULL) |
---|
295 | { |
---|
296 | fwrite(&count, sizeof(int), 1, file); |
---|
297 | fwrite(&format, sizeof(int), 1, file); |
---|
298 | fwrite(&mode, sizeof(int), 1, file); |
---|
299 | |
---|
300 | size_t size = 0; |
---|
301 | |
---|
302 | switch (format) |
---|
303 | { |
---|
304 | case GL_V3F: |
---|
305 | size = sizeof(vertex); |
---|
306 | break; |
---|
307 | |
---|
308 | case GL_N3F_V3F: |
---|
309 | size = sizeof(normal_vertex); |
---|
310 | break; |
---|
311 | |
---|
312 | case GL_T2F_N3F_V3F: |
---|
313 | size = sizeof(texture_vertex); |
---|
314 | break; |
---|
315 | } |
---|
316 | |
---|
317 | fwrite(data, size, count, file); |
---|
318 | |
---|
319 | fclose(file); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | return true; |
---|
324 | } |
---|
325 | |
---|
326 | void Sglmodel_sgl::Delete() |
---|
327 | { |
---|
328 | free(data); data = NULL; format = count = mode = 0; id = 0; |
---|
329 | |
---|
330 | pDimensions = NULL; |
---|
331 | pCenter = NULL; |
---|
332 | pUnitize = NULL; |
---|
333 | pScale = NULL; |
---|
334 | pTranslate = NULL; |
---|
335 | |
---|
336 | return; |
---|
337 | } |
---|
338 | |
---|
339 | void Sglmodel_sgl::GL_V3F_Dimensions(float& width, float& height, float& depth) |
---|
340 | { |
---|
341 | width = height = depth = 0.0f; |
---|
342 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
343 | |
---|
344 | if (data != NULL) |
---|
345 | { |
---|
346 | /* Get the maximum and minimum. */ |
---|
347 | vertex* v = (vertex*) data; |
---|
348 | |
---|
349 | maxx = minx = v[0][x]; |
---|
350 | maxy = miny = v[0][y]; |
---|
351 | maxz = minz = v[0][z]; |
---|
352 | |
---|
353 | for (int i = 0; i < count; ++i) |
---|
354 | { |
---|
355 | if (maxx < v[i][x]) { maxx = v[i][x]; } |
---|
356 | if (minx > v[i][x]) { minx = v[i][x]; } |
---|
357 | if (maxy < v[i][y]) { maxy = v[i][y]; } |
---|
358 | if (miny > v[i][y]) { miny = v[i][y]; } |
---|
359 | if (maxz < v[i][z]) { maxz = v[i][z]; } |
---|
360 | if (minz > v[i][z]) { minz = v[i][z]; } |
---|
361 | } |
---|
362 | |
---|
363 | /* Calculate model width, height, and depth. */ |
---|
364 | width = fabs(maxx) + fabs(minx); |
---|
365 | height = fabs(maxy) + fabs(miny); |
---|
366 | depth = fabs(maxz) + fabs(minz); |
---|
367 | } |
---|
368 | |
---|
369 | return; |
---|
370 | } |
---|
371 | |
---|
372 | void Sglmodel_sgl::GL_N3F_V3F_Dimensions(float& width, float& height, float& depth) |
---|
373 | { |
---|
374 | width = height = depth = 0.0f; |
---|
375 | |
---|
376 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
377 | |
---|
378 | if (data != NULL) |
---|
379 | { |
---|
380 | /* Get the maximum and minimum. */ |
---|
381 | normal_vertex* v = (normal_vertex*) data; |
---|
382 | |
---|
383 | maxx = minx = v[0].v[x]; |
---|
384 | maxy = miny = v[0].v[y]; |
---|
385 | maxz = minz = v[0].v[z]; |
---|
386 | |
---|
387 | for (int i = 0; i < count; ++i) |
---|
388 | { |
---|
389 | if (maxx < v[i].v[x]) { maxx = v[i].v[x]; } |
---|
390 | if (minx > v[i].v[x]) { minx = v[i].v[x]; } |
---|
391 | if (maxy < v[i].v[y]) { maxy = v[i].v[y]; } |
---|
392 | if (miny > v[i].v[y]) { miny = v[i].v[y]; } |
---|
393 | if (maxz < v[i].v[z]) { maxz = v[i].v[z]; } |
---|
394 | if (minz > v[i].v[z]) { minz = v[i].v[z]; } |
---|
395 | } |
---|
396 | |
---|
397 | /* Calculate model width, height, and depth. */ |
---|
398 | width = fabs(maxx) + fabs(minx); |
---|
399 | height = fabs(maxy) + fabs(miny); |
---|
400 | depth = fabs(maxz) + fabs(minz); |
---|
401 | } |
---|
402 | |
---|
403 | return; |
---|
404 | } |
---|
405 | |
---|
406 | void Sglmodel_sgl::GL_T2F_N3F_V3F_Dimensions(float& width, float& height, float& depth) |
---|
407 | { |
---|
408 | width = height = depth = 0.0f; |
---|
409 | |
---|
410 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
411 | |
---|
412 | if (data != NULL) |
---|
413 | { |
---|
414 | /* Get the maximum and minimum. */ |
---|
415 | texture_vertex* v = (texture_vertex*) data; |
---|
416 | |
---|
417 | maxx = minx = v[0].v[x]; |
---|
418 | maxy = miny = v[0].v[y]; |
---|
419 | maxz = minz = v[0].v[z]; |
---|
420 | |
---|
421 | for (int i = 0; i < count; ++i) |
---|
422 | { |
---|
423 | if (maxx < v[i].v[x]) { maxx = v[i].v[x]; } |
---|
424 | if (minx > v[i].v[x]) { minx = v[i].v[x]; } |
---|
425 | if (maxy < v[i].v[y]) { maxy = v[i].v[y]; } |
---|
426 | if (miny > v[i].v[y]) { miny = v[i].v[y]; } |
---|
427 | if (maxz < v[i].v[z]) { maxz = v[i].v[z]; } |
---|
428 | if (minz > v[i].v[z]) { minz = v[i].v[z]; } |
---|
429 | } |
---|
430 | |
---|
431 | /* Calculate model width, height, and depth. */ |
---|
432 | width = fabs(maxx) + fabs(minx); |
---|
433 | height = fabs(maxy) + fabs(miny); |
---|
434 | depth = fabs(maxz) + fabs(minz); |
---|
435 | } |
---|
436 | |
---|
437 | return; |
---|
438 | } |
---|
439 | |
---|
440 | void Sglmodel_sgl::GL_V3F_Center(float& cx, float& cy, float& cz) |
---|
441 | { |
---|
442 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
443 | |
---|
444 | if (data != NULL) |
---|
445 | { |
---|
446 | /* Get the maximum and minimum. */ |
---|
447 | vertex* v = (vertex*) data; |
---|
448 | |
---|
449 | maxx = minx = v[0][x]; |
---|
450 | maxy = miny = v[0][y]; |
---|
451 | maxz = minz = v[0][z]; |
---|
452 | |
---|
453 | for (int i = 0; i < count; ++i) |
---|
454 | { |
---|
455 | if (maxx < v[i][x]) { maxx = v[i][x]; } |
---|
456 | if (minx > v[i][x]) { minx = v[i][x]; } |
---|
457 | if (maxy < v[i][y]) { maxy = v[i][y]; } |
---|
458 | if (miny > v[i][y]) { miny = v[i][y]; } |
---|
459 | if (maxz < v[i][z]) { maxz = v[i][z]; } |
---|
460 | if (minz > v[i][z]) { minz = v[i][z]; } |
---|
461 | } |
---|
462 | |
---|
463 | /* Calculate model width, height, and depth. */ |
---|
464 | float width = fabs(maxx) + fabs(minx); |
---|
465 | float height = fabs(maxy) + fabs(miny); |
---|
466 | float depth = fabs(maxz) + fabs(minz); |
---|
467 | |
---|
468 | /* Calculate center of the model. */ |
---|
469 | cx = (maxx + minx) / 2.0f; |
---|
470 | cy = (maxy + miny) / 2.0f; |
---|
471 | cz = (maxz + minz) / 2.0f; |
---|
472 | } |
---|
473 | |
---|
474 | return; |
---|
475 | } |
---|
476 | |
---|
477 | void Sglmodel_sgl::GL_N3F_V3F_Center(float& cx, float& cy, float& cz) |
---|
478 | { |
---|
479 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
480 | |
---|
481 | if (data != NULL) |
---|
482 | { |
---|
483 | /* Get the maximum and minimum. */ |
---|
484 | normal_vertex* v = (normal_vertex*) data; |
---|
485 | |
---|
486 | maxx = minx = v[0].v[x]; |
---|
487 | maxy = miny = v[0].v[y]; |
---|
488 | maxz = minz = v[0].v[z]; |
---|
489 | |
---|
490 | for (int i = 0; i < count; ++i) |
---|
491 | { |
---|
492 | if (maxx < v[i].v[x]) { maxx = v[i].v[x]; } |
---|
493 | if (minx > v[i].v[x]) { minx = v[i].v[x]; } |
---|
494 | if (maxy < v[i].v[y]) { maxy = v[i].v[y]; } |
---|
495 | if (miny > v[i].v[y]) { miny = v[i].v[y]; } |
---|
496 | if (maxz < v[i].v[z]) { maxz = v[i].v[z]; } |
---|
497 | if (minz > v[i].v[z]) { minz = v[i].v[z]; } |
---|
498 | } |
---|
499 | |
---|
500 | /* Calculate model width, height, and depth. */ |
---|
501 | float width = fabs(maxx) + fabs(minx); |
---|
502 | float height = fabs(maxy) + fabs(miny); |
---|
503 | float depth = fabs(maxz) + fabs(minz); |
---|
504 | |
---|
505 | /* Calculate center of the model. */ |
---|
506 | cx = (maxx + minx) / 2.0f; |
---|
507 | cy = (maxy + miny) / 2.0f; |
---|
508 | cz = (maxz + minz) / 2.0f; |
---|
509 | } |
---|
510 | |
---|
511 | return; |
---|
512 | } |
---|
513 | |
---|
514 | void Sglmodel_sgl::GL_T2F_N3F_V3F_Center(float& cx, float& cy, float& cz) |
---|
515 | { |
---|
516 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
517 | |
---|
518 | if (data != NULL) |
---|
519 | { |
---|
520 | /* Get the maximum and minimum. */ |
---|
521 | texture_vertex* v = (texture_vertex*) data; |
---|
522 | |
---|
523 | maxx = minx = v[0].v[x]; |
---|
524 | maxy = miny = v[0].v[y]; |
---|
525 | maxz = minz = v[0].v[z]; |
---|
526 | |
---|
527 | for (int i = 0; i < count; ++i) |
---|
528 | { |
---|
529 | if (maxx < v[i].v[x]) { maxx = v[i].v[x]; } |
---|
530 | if (minx > v[i].v[x]) { minx = v[i].v[x]; } |
---|
531 | if (maxy < v[i].v[y]) { maxy = v[i].v[y]; } |
---|
532 | if (miny > v[i].v[y]) { miny = v[i].v[y]; } |
---|
533 | if (maxz < v[i].v[z]) { maxz = v[i].v[z]; } |
---|
534 | if (minz > v[i].v[z]) { minz = v[i].v[z]; } |
---|
535 | } |
---|
536 | |
---|
537 | /* Calculate model width, height, and depth. */ |
---|
538 | float width = fabs(maxx) + fabs(minx); |
---|
539 | float height = fabs(maxy) + fabs(miny); |
---|
540 | float depth = fabs(maxz) + fabs(minz); |
---|
541 | |
---|
542 | /* Calculate center of the model. */ |
---|
543 | cx = (maxx + minx) / 2.0f; |
---|
544 | cy = (maxy + miny) / 2.0f; |
---|
545 | cz = (maxz + minz) / 2.0f; |
---|
546 | } |
---|
547 | |
---|
548 | return; |
---|
549 | } |
---|
550 | |
---|
551 | void Sglmodel_sgl::GL_V3F_Unitize() |
---|
552 | { |
---|
553 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
554 | |
---|
555 | if (data != NULL) |
---|
556 | { |
---|
557 | /* Get the maximum and minimum. */ |
---|
558 | vertex* v = (vertex*) data; |
---|
559 | |
---|
560 | maxx = minx = v[0][x]; |
---|
561 | maxy = miny = v[0][y]; |
---|
562 | maxz = minz = v[0][z]; |
---|
563 | |
---|
564 | for (int i = 0; i < count; ++i) |
---|
565 | { |
---|
566 | if (maxx < v[i][x]) { maxx = v[i][x]; } |
---|
567 | if (minx > v[i][x]) { minx = v[i][x]; } |
---|
568 | if (maxy < v[i][y]) { maxy = v[i][y]; } |
---|
569 | if (miny > v[i][y]) { miny = v[i][y]; } |
---|
570 | if (maxz < v[i][z]) { maxz = v[i][z]; } |
---|
571 | if (minz > v[i][z]) { minz = v[i][z]; } |
---|
572 | } |
---|
573 | |
---|
574 | /* Calculate model width, height, and depth. */ |
---|
575 | float width = fabs(maxx) + fabs(minx); |
---|
576 | float height = fabs(maxy) + fabs(miny); |
---|
577 | float depth = fabs(maxz) + fabs(minz); |
---|
578 | |
---|
579 | /* Calculate center of the model. */ |
---|
580 | float cx = (maxx + minx) / 2.0f; |
---|
581 | float cy = (maxy + miny) / 2.0f; |
---|
582 | float cz = (maxz + minz) / 2.0f; |
---|
583 | |
---|
584 | /* Calculate unitizing scale factor. */ |
---|
585 | float scale = 2.0f / my_max(my_max(width, height), depth); |
---|
586 | |
---|
587 | /* Translate around center then scale. */ |
---|
588 | v = (vertex*) data; |
---|
589 | |
---|
590 | for (int i = 0; i < count; ++i) |
---|
591 | { |
---|
592 | v[i][x] -= cx; |
---|
593 | v[i][y] -= cy; |
---|
594 | v[i][z] -= cz; |
---|
595 | v[i][x] *= scale; |
---|
596 | v[i][y] *= scale; |
---|
597 | v[i][z] *= scale; |
---|
598 | } |
---|
599 | } |
---|
600 | |
---|
601 | return; |
---|
602 | } |
---|
603 | |
---|
604 | void Sglmodel_sgl::GL_N3F_V3F_Unitize() |
---|
605 | { |
---|
606 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
607 | |
---|
608 | if (data != NULL) |
---|
609 | { |
---|
610 | /* Get the maximum and minimum. */ |
---|
611 | normal_vertex* v = (normal_vertex*) data; |
---|
612 | |
---|
613 | maxx = minx = v[0].v[x]; |
---|
614 | maxy = miny = v[0].v[y]; |
---|
615 | maxz = minz = v[0].v[z]; |
---|
616 | |
---|
617 | for (int i = 0; i < count; ++i) |
---|
618 | { |
---|
619 | if (maxx < v[i].v[x]) { maxx = v[i].v[x]; } |
---|
620 | if (minx > v[i].v[x]) { minx = v[i].v[x]; } |
---|
621 | if (maxy < v[i].v[y]) { maxy = v[i].v[y]; } |
---|
622 | if (miny > v[i].v[y]) { miny = v[i].v[y]; } |
---|
623 | if (maxz < v[i].v[z]) { maxz = v[i].v[z]; } |
---|
624 | if (minz > v[i].v[z]) { minz = v[i].v[z]; } |
---|
625 | } |
---|
626 | |
---|
627 | /* Calculate model width, height, and depth. */ |
---|
628 | float width = fabs(maxx) + fabs(minx); |
---|
629 | float height = fabs(maxy) + fabs(miny); |
---|
630 | float depth = fabs(maxz) + fabs(minz); |
---|
631 | |
---|
632 | /* Calculate center of the model. */ |
---|
633 | float cx = (maxx + minx) / 2.0f; |
---|
634 | float cy = (maxy + miny) / 2.0f; |
---|
635 | float cz = (maxz + minz) / 2.0f; |
---|
636 | |
---|
637 | /* Calculate unitizing scale factor. */ |
---|
638 | float scale = 2.0f / my_max(my_max(width, height), depth); |
---|
639 | |
---|
640 | /* Translate around center then scale. */ |
---|
641 | v = (normal_vertex*) data; |
---|
642 | |
---|
643 | for (int i = 0; i < count; ++i) |
---|
644 | { |
---|
645 | v[i].v[x] -= cx; |
---|
646 | v[i].v[y] -= cy; |
---|
647 | v[i].v[z] -= cz; |
---|
648 | v[i].v[x] *= scale; |
---|
649 | v[i].v[y] *= scale; |
---|
650 | v[i].v[z] *= scale; |
---|
651 | } |
---|
652 | } |
---|
653 | |
---|
654 | return; |
---|
655 | } |
---|
656 | |
---|
657 | void Sglmodel_sgl::GL_T2F_N3F_V3F_Unitize() |
---|
658 | { |
---|
659 | float maxx = 0.0f, minx = 0.0f, maxy = 0.0f, miny = 0.0f, maxz = 0.0f, minz = 0.0f; |
---|
660 | |
---|
661 | if (data != NULL) |
---|
662 | { |
---|
663 | /* Get the maximum and minimum. */ |
---|
664 | texture_vertex* v = (texture_vertex*) data; |
---|
665 | |
---|
666 | maxx = minx = v[0].v[x]; |
---|
667 | maxy = miny = v[0].v[y]; |
---|
668 | maxz = minz = v[0].v[z]; |
---|
669 | |
---|
670 | for (int i = 0; i < count; ++i) |
---|
671 | { |
---|
672 | if (maxx < v[i].v[x]) { maxx = v[i].v[x]; } |
---|
673 | if (minx > v[i].v[x]) { minx = v[i].v[x]; } |
---|
674 | if (maxy < v[i].v[y]) { maxy = v[i].v[y]; } |
---|
675 | if (miny > v[i].v[y]) { miny = v[i].v[y]; } |
---|
676 | if (maxz < v[i].v[z]) { maxz = v[i].v[z]; } |
---|
677 | if (minz > v[i].v[z]) { minz = v[i].v[z]; } |
---|
678 | } |
---|
679 | |
---|
680 | /* Calculate model width, height, and depth. */ |
---|
681 | float width = fabs(maxx) + fabs(minx); |
---|
682 | float height = fabs(maxy) + fabs(miny); |
---|
683 | float depth = fabs(maxz) + fabs(minz); |
---|
684 | |
---|
685 | /* Calculate center of the model. */ |
---|
686 | float cx = (maxx + minx) / 2.0f; |
---|
687 | float cy = (maxy + miny) / 2.0f; |
---|
688 | float cz = (maxz + minz) / 2.0f; |
---|
689 | |
---|
690 | /* Calculate unitizing scale factor. */ |
---|
691 | float scale = 2.0f / my_max(my_max(width, height), depth); |
---|
692 | |
---|
693 | /* Translate around center then scale. */ |
---|
694 | v = (texture_vertex*) data; |
---|
695 | |
---|
696 | for (int i = 0; i < count; ++i) |
---|
697 | { |
---|
698 | v[i].v[x] -= cx; |
---|
699 | v[i].v[y] -= cy; |
---|
700 | v[i].v[z] -= cz; |
---|
701 | v[i].v[x] *= scale; |
---|
702 | v[i].v[y] *= scale; |
---|
703 | v[i].v[z] *= scale; |
---|
704 | } |
---|
705 | } |
---|
706 | |
---|
707 | return; |
---|
708 | } |
---|
709 | |
---|
710 | void Sglmodel_sgl::GL_V3F_Scale(float scale) |
---|
711 | { |
---|
712 | if (data != NULL) |
---|
713 | { |
---|
714 | vertex* v = (vertex*) data; |
---|
715 | |
---|
716 | for (int i = 0; i < count; ++i) |
---|
717 | { |
---|
718 | v[i][x] *= scale; |
---|
719 | v[i][y] *= scale; |
---|
720 | v[i][z] *= scale; |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | return; |
---|
725 | } |
---|
726 | |
---|
727 | void Sglmodel_sgl::GL_N3F_V3F_Scale(float scale) |
---|
728 | { |
---|
729 | if (data != NULL) |
---|
730 | { |
---|
731 | normal_vertex* v = (normal_vertex*) data; |
---|
732 | |
---|
733 | for (int i = 0; i < count; ++i) |
---|
734 | { |
---|
735 | v[i].v[x] *= scale; |
---|
736 | v[i].v[y] *= scale; |
---|
737 | v[i].v[z] *= scale; |
---|
738 | } |
---|
739 | } |
---|
740 | |
---|
741 | return; |
---|
742 | } |
---|
743 | |
---|
744 | void Sglmodel_sgl::GL_T2F_N3F_V3F_Scale(float scale) |
---|
745 | { |
---|
746 | if (data != NULL) |
---|
747 | { |
---|
748 | texture_vertex* v = (texture_vertex*) data; |
---|
749 | |
---|
750 | for (int i = 0; i < count; ++i) |
---|
751 | { |
---|
752 | v[i].v[x] *= scale; |
---|
753 | v[i].v[y] *= scale; |
---|
754 | v[i].v[z] *= scale; |
---|
755 | } |
---|
756 | } |
---|
757 | |
---|
758 | return; |
---|
759 | } |
---|
760 | |
---|
761 | void Sglmodel_sgl::GL_V3F_Translate(float tx, float ty, float tz) |
---|
762 | { |
---|
763 | if (data != NULL) |
---|
764 | { |
---|
765 | vertex* v = (vertex*) data; |
---|
766 | |
---|
767 | for (int i = 0; i < count; ++i) |
---|
768 | { |
---|
769 | v[i][x] += tx; |
---|
770 | v[i][y] += ty; |
---|
771 | v[i][z] += tz; |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | return; |
---|
776 | } |
---|
777 | |
---|
778 | void Sglmodel_sgl::GL_N3F_V3F_Translate(float tx, float ty, float tz) |
---|
779 | { |
---|
780 | if (data != NULL) |
---|
781 | { |
---|
782 | normal_vertex* v = (normal_vertex*) data; |
---|
783 | |
---|
784 | for (int i = 0; i < count; ++i) |
---|
785 | { |
---|
786 | v[i].v[x] += tx; |
---|
787 | v[i].v[y] += ty; |
---|
788 | v[i].v[z] += tz; |
---|
789 | } |
---|
790 | } |
---|
791 | |
---|
792 | return; |
---|
793 | } |
---|
794 | |
---|
795 | void Sglmodel_sgl::GL_T2F_N3F_V3F_Translate(float tx, float ty, float tz) |
---|
796 | { |
---|
797 | if (data != NULL) |
---|
798 | { |
---|
799 | texture_vertex* v = (texture_vertex*) data; |
---|
800 | |
---|
801 | for (int i = 0; i < count; ++i) |
---|
802 | { |
---|
803 | v[i].v[x] += tx; |
---|
804 | v[i].v[y] += ty; |
---|
805 | v[i].v[z] += tz; |
---|
806 | } |
---|
807 | } |
---|
808 | |
---|
809 | return; |
---|
810 | } |
---|
811 | |
---|
812 | #endif |
---|