2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 #include "Graphic/Models.hpp"
24 #include "Utils/Folders.hpp"
26 extern float multiplier;
27 extern float viewdistance;
29 extern float fadestart;
30 extern float texdetail;
31 extern bool decalstoggle;
33 int Model::LineCheck(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
36 static float distance;
37 static float olddistance;
38 static int intersecting;
39 static int firstintersecting;
45 *p1 = DoRotation(*p1, 0, -*rotate, 0);
47 *p2 = DoRotation(*p2, 0, -*rotate, 0);
48 if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
50 firstintersecting = -1;
52 for (j = 0; j < TriangleNum; j++) {
53 intersecting = LineFacetd(p1, p2, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], &facenormals[j], &point);
54 distance = (point.x - p1->x) * (point.x - p1->x) + (point.y - p1->y) * (point.y - p1->y) + (point.z - p1->z) * (point.z - p1->z);
55 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
56 olddistance = distance;
57 firstintersecting = j;
63 *p = DoRotation(*p, 0, *rotate, 0);
65 return firstintersecting;
68 int Model::LineCheckPossible(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
71 static float distance;
72 static float olddistance;
73 static int intersecting;
74 static int firstintersecting;
79 if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
81 firstintersecting = -1;
83 *p1 = DoRotation(*p1, 0, -*rotate, 0);
85 *p2 = DoRotation(*p2, 0, -*rotate, 0);
87 if (numpossible > 0 && numpossible < TriangleNum)
88 for (j = 0; j < numpossible; j++) {
89 if (possible[j] >= 0 && possible[j] < TriangleNum) {
90 intersecting = LineFacetd(p1, p2, &vertex[Triangles[possible[j]].vertex[0]], &vertex[Triangles[possible[j]].vertex[1]], &vertex[Triangles[possible[j]].vertex[2]], &facenormals[possible[j]], &point);
91 distance = (point.x - p1->x) * (point.x - p1->x) + (point.y - p1->y) * (point.y - p1->y) + (point.z - p1->z) * (point.z - p1->z);
92 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
93 olddistance = distance;
94 firstintersecting = possible[j];
101 *p = DoRotation(*p, 0, *rotate, 0);
103 return firstintersecting;
106 int Model::LineCheckSlidePossible(XYZ *p1, XYZ *p2, XYZ *move, float *rotate)
109 static float distance;
110 static float olddistance;
111 static int intersecting;
112 static int firstintersecting;
117 if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
119 firstintersecting = -1;
121 *p1 = DoRotation(*p1, 0, -*rotate, 0);
123 *p2 = DoRotation(*p2, 0, -*rotate, 0);
126 for (j = 0; j < numpossible; j++) {
127 if (possible[j] >= 0 && possible[j] < TriangleNum) {
128 intersecting = LineFacetd(p1, p2, &vertex[Triangles[possible[j]].vertex[0]], &vertex[Triangles[possible[j]].vertex[1]], &vertex[Triangles[possible[j]].vertex[2]], &facenormals[possible[j]], &point);
129 distance = (point.x - p1->x) * (point.x - p1->x) + (point.y - p1->y) * (point.y - p1->y) + (point.z - p1->z) * (point.z - p1->z);
130 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
131 olddistance = distance;
132 firstintersecting = possible[j];
137 if (firstintersecting > 0) {
138 distance = abs((facenormals[firstintersecting].x * p2->x) + (facenormals[firstintersecting].y * p2->y) + (facenormals[firstintersecting].z * p2->z) - ((facenormals[firstintersecting].x * vertex[Triangles[firstintersecting].vertex[0]].x) + (facenormals[firstintersecting].y * vertex[Triangles[firstintersecting].vertex[0]].y) + (facenormals[firstintersecting].z * vertex[Triangles[firstintersecting].vertex[0]].z)));
139 *p2 -= facenormals[firstintersecting] * distance;
143 *p2 = DoRotation(*p2, 0, *rotate, 0);
145 return firstintersecting;
148 int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
151 static float distance;
152 static float olddistance;
153 static int intersecting;
154 static int firstintersecting;
158 firstintersecting = -1;
163 *p1 = DoRotation(*p1, 0, -*rotate, 0);
164 if (distsq(p1, &boundingspherecenter) > radius * radius + boundingsphereradius * boundingsphereradius)
167 for (i = 0; i < 4; i++) {
168 for (j = 0; j < TriangleNum; j++) {
170 distance = abs((facenormals[j].x * p1->x) + (facenormals[j].y * p1->y) + (facenormals[j].z * p1->z) - ((facenormals[j].x * vertex[Triangles[j].vertex[0]].x) + (facenormals[j].y * vertex[Triangles[j].vertex[0]].y) + (facenormals[j].z * vertex[Triangles[j].vertex[0]].z)));
171 if (distance < radius) {
172 point = *p1 - facenormals[j] * distance;
173 if (PointInTriangle( &point, facenormals[j], &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
176 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], p1, &radius);
178 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], p1, &radius);
180 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
182 *p1 += facenormals[j] * (distance - radius);
185 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
186 olddistance = distance;
187 firstintersecting = j;
193 *p = DoRotation(*p, 0, *rotate, 0);
196 *p1 = DoRotation(*p1, 0, *rotate, 0);
198 return firstintersecting;
201 int Model::SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate)
204 static float distance;
205 static float olddistance;
206 static int intersecting;
207 static int firstintersecting;
211 firstintersecting = -1;
219 *p1 = DoRotation(*p1, 0, -*rotate, 0);
220 if (distsq(p1, &boundingspherecenter) > radius * radius + boundingsphereradius * boundingsphereradius) {
225 for (j = 0; j < TriangleNum; j++) {
227 distance = abs((facenormals[j].x * p1->x) + (facenormals[j].y * p1->y) + (facenormals[j].z * p1->z) - ((facenormals[j].x * vertex[Triangles[j].vertex[0]].x) + (facenormals[j].y * vertex[Triangles[j].vertex[0]].y) + (facenormals[j].z * vertex[Triangles[j].vertex[0]].z)));
228 if (distance < radius) {
229 point = *p1 - facenormals[j] * distance;
230 if (PointInTriangle( &point, facenormals[j], &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
233 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], p1, &radius);
235 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], p1, &radius);
237 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
239 possible[numpossible] = j;
243 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
244 olddistance = distance;
245 firstintersecting = j;
249 *p1 = DoRotation(*p1, 0, *rotate, 0);
251 return firstintersecting;
255 void Model::UpdateVertexArray()
257 if (type != normaltype && type != decalstype)
262 for (i = 0; i < TriangleNum; i++) {
264 vArray[j + 0] = Triangles[i].gx[0];
265 vArray[j + 1] = Triangles[i].gy[0];
266 vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
267 vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
268 vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
269 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
270 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
271 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
273 vArray[j + 8] = Triangles[i].gx[1];
274 vArray[j + 9] = Triangles[i].gy[1];
275 vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
276 vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
277 vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
278 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
279 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
280 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
282 vArray[j + 16] = Triangles[i].gx[2];
283 vArray[j + 17] = Triangles[i].gy[2];
284 vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
285 vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
286 vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
287 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
288 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
289 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
292 for (i = 0; i < TriangleNum; i++) {
294 vArray[j + 0] = Triangles[i].gx[0];
295 vArray[j + 1] = Triangles[i].gy[0];
296 vArray[j + 2] = facenormals[i].x * -1;
297 vArray[j + 3] = facenormals[i].y * -1;
298 vArray[j + 4] = facenormals[i].z * -1;
299 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
300 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
301 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
303 vArray[j + 8] = Triangles[i].gx[1];
304 vArray[j + 9] = Triangles[i].gy[1];
305 vArray[j + 10] = facenormals[i].x * -1;
306 vArray[j + 11] = facenormals[i].y * -1;
307 vArray[j + 12] = facenormals[i].z * -1;
308 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
309 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
310 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
312 vArray[j + 16] = Triangles[i].gx[2];
313 vArray[j + 17] = Triangles[i].gy[2];
314 vArray[j + 18] = facenormals[i].x * -1;
315 vArray[j + 19] = facenormals[i].y * -1;
316 vArray[j + 20] = facenormals[i].z * -1;
317 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
318 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
319 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
324 void Model::UpdateVertexArrayNoTex()
326 if (type != normaltype && type != decalstype)
331 for (i = 0; i < TriangleNum; i++) {
333 vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
334 vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
335 vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
336 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
337 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
338 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
340 vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
341 vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
342 vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
343 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
344 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
345 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
347 vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
348 vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
349 vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
350 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
351 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
352 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
355 for (i = 0; i < TriangleNum; i++) {
357 vArray[j + 2] = facenormals[i].x * -1;
358 vArray[j + 3] = facenormals[i].y * -1;
359 vArray[j + 4] = facenormals[i].z * -1;
360 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
361 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
362 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
364 vArray[j + 10] = facenormals[i].x * -1;
365 vArray[j + 11] = facenormals[i].y * -1;
366 vArray[j + 12] = facenormals[i].z * -1;
367 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
368 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
369 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
371 vArray[j + 18] = facenormals[i].x * -1;
372 vArray[j + 19] = facenormals[i].y * -1;
373 vArray[j + 20] = facenormals[i].z * -1;
374 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
375 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
376 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
380 void Model::UpdateVertexArrayNoTexNoNorm()
382 if (type != normaltype && type != decalstype)
386 for (i = 0; i < TriangleNum; i++) {
388 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
389 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
390 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
392 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
393 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
394 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
396 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
397 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
398 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
402 bool Model::loadnotex(const std::string& filename)
410 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
412 // read model settings
414 fseek(tfile, 0, SEEK_SET);
415 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
417 // read the model data
422 owner = (int*)malloc(sizeof(int) * vertexNum);
423 possible = (int*)malloc(sizeof(int) * TriangleNum);
424 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
425 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
426 vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
428 for (i = 0; i < vertexNum; i++) {
429 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
432 for (i = 0; i < TriangleNum; i++) {
434 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
435 Triangles[i].vertex[ 0] = vertex[ 0];
436 Triangles[i].vertex[ 1] = vertex[ 2];
437 Triangles[i].vertex[ 2] = vertex[ 4];
438 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
439 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
446 for (i = 0; i < vertexNum; i++) {
451 boundingsphereradius = 0;
452 for (i = 0; i < vertexNum; i++) {
453 for (j = 0; j < vertexNum; j++) {
454 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
455 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
456 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
460 boundingsphereradius = fast_sqrt(boundingsphereradius);
466 bool Model::load(const std::string& filename)
473 LOG(std::string("Loading model...") + filename);
475 Game::LoadingScreen();
480 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
482 // read model settings
484 fseek(tfile, 0, SEEK_SET);
485 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
487 // read the model data
492 owner = (int*)malloc(sizeof(int) * vertexNum);
493 possible = (int*)malloc(sizeof(int) * TriangleNum);
494 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
495 normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
496 facenormals = (XYZ*)malloc(sizeof(XYZ) * TriangleNum);
497 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
498 vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
500 for (i = 0; i < vertexNum; i++) {
501 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
504 for (i = 0; i < TriangleNum; i++) {
506 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
507 Triangles[i].vertex[ 0] = vertex[ 0];
508 Triangles[i].vertex[ 1] = vertex[ 2];
509 Triangles[i].vertex[ 2] = vertex[ 4];
510 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
511 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
514 modelTexture.xsz = 0;
520 for (i = 0; i < vertexNum; i++) {
525 boundingsphereradius = 0;
526 for (i = 0; i < vertexNum; i++) {
527 for (j = 0; j < vertexNum; j++) {
528 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
529 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
530 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
534 boundingsphereradius = fast_sqrt(boundingsphereradius);
539 bool Model::loaddecal(const std::string& filename)
546 LOG(std::string("Loading decal...") + Folders::getResourcePath(filename));
552 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
554 // read model settings
556 fseek(tfile, 0, SEEK_SET);
557 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
559 // read the model data
565 owner = (int*)malloc(sizeof(int) * vertexNum);
566 possible = (int*)malloc(sizeof(int) * TriangleNum);
567 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
568 normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
569 facenormals = (XYZ*)malloc(sizeof(XYZ) * TriangleNum);
570 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
571 vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
574 for (i = 0; i < vertexNum; i++) {
575 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
578 for (i = 0; i < TriangleNum; i++) {
580 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
581 Triangles[i].vertex[ 0] = vertex[ 0];
582 Triangles[i].vertex[ 1] = vertex[ 2];
583 Triangles[i].vertex[ 2] = vertex[ 4];
584 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
585 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
589 modelTexture.xsz = 0;
595 for (i = 0; i < vertexNum; i++) {
599 boundingsphereradius = 0;
600 for (i = 0; i < vertexNum; i++) {
601 for (j = 0; j < vertexNum; j++) {
602 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
603 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
604 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
608 boundingsphereradius = fast_sqrt(boundingsphereradius);
611 if (!decaltexcoords) {
612 decaltexcoords = (float***)malloc(sizeof(float**)*max_model_decals);
613 for (i = 0; i < max_model_decals; i++) {
614 decaltexcoords[i] = (float**)malloc(sizeof(float*) * 3);
615 for (j = 0; j < 3; j++) {
616 decaltexcoords[i][j] = (float*)malloc(sizeof(float) * 2);
619 decalvertex = (XYZ**)malloc(sizeof(XYZ*)*max_model_decals);
620 for (i = 0; i < max_model_decals; i++) {
621 decalvertex[i] = (XYZ*)malloc(sizeof(XYZ) * 3);
624 decaltype = (int*)malloc(sizeof(int) * max_model_decals);
625 decalopacity = (float*)malloc(sizeof(float) * max_model_decals);
626 decalrotation = (float*)malloc(sizeof(float) * max_model_decals);
627 decalalivetime = (float*)malloc(sizeof(float) * max_model_decals);
628 decalposition = (XYZ*)malloc(sizeof(XYZ) * max_model_decals);
634 bool Model::loadraw(const std::string& filename)
641 LOG(std::string("Loading raw...") + filename);
646 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
648 // read model settings
650 fseek(tfile, 0, SEEK_SET);
651 funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
653 // read the model data
658 owner = (int*)malloc(sizeof(int) * vertexNum);
659 possible = (int*)malloc(sizeof(int) * TriangleNum);
660 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
661 Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
662 vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
665 for (i = 0; i < vertexNum; i++) {
666 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
669 for (i = 0; i < TriangleNum; i++) {
671 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
672 Triangles[i].vertex[ 0] = vertex[ 0];
673 Triangles[i].vertex[ 1] = vertex[ 2];
674 Triangles[i].vertex[ 2] = vertex[ 4];
675 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
676 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
682 for (i = 0; i < vertexNum; i++) {
690 void Model::UniformTexCoords()
693 for (i = 0; i < TriangleNum; i++) {
694 Triangles[i].gy[0] = vertex[Triangles[i].vertex[0]].y;
695 Triangles[i].gy[1] = vertex[Triangles[i].vertex[1]].y;
696 Triangles[i].gy[2] = vertex[Triangles[i].vertex[2]].y;
697 Triangles[i].gx[0] = vertex[Triangles[i].vertex[0]].x;
698 Triangles[i].gx[1] = vertex[Triangles[i].vertex[1]].x;
699 Triangles[i].gx[2] = vertex[Triangles[i].vertex[2]].x;
705 void Model::FlipTexCoords()
708 for (i = 0; i < TriangleNum; i++) {
709 Triangles[i].gy[0] = -Triangles[i].gy[0];
710 Triangles[i].gy[1] = -Triangles[i].gy[1];
711 Triangles[i].gy[2] = -Triangles[i].gy[2];
716 void Model::ScaleTexCoords(float howmuch)
719 for (i = 0; i < TriangleNum; i++) {
720 Triangles[i].gx[0] *= howmuch;
721 Triangles[i].gx[1] *= howmuch;
722 Triangles[i].gx[2] *= howmuch;
723 Triangles[i].gy[0] *= howmuch;
724 Triangles[i].gy[1] *= howmuch;
725 Triangles[i].gy[2] *= howmuch;
730 void Model::Scale(float xscale, float yscale, float zscale)
733 for (i = 0; i < vertexNum; i++) {
734 vertex[i].x *= xscale;
735 vertex[i].y *= yscale;
736 vertex[i].z *= zscale;
742 boundingsphereradius = 0;
743 for (i = 0; i < vertexNum; i++) {
744 for (j = 0; j < vertexNum; j++) {
745 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
746 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
747 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
751 boundingsphereradius = fast_sqrt(boundingsphereradius);
754 void Model::ScaleNormals(float xscale, float yscale, float zscale)
756 if (type != normaltype && type != decalstype)
759 for (i = 0; i < vertexNum; i++) {
760 normals[i].x *= xscale;
761 normals[i].y *= yscale;
762 normals[i].z *= zscale;
764 for (i = 0; i < TriangleNum; i++) {
765 facenormals[i].x *= xscale;
766 facenormals[i].y *= yscale;
767 facenormals[i].z *= zscale;
772 void Model::Translate(float xtrans, float ytrans, float ztrans)
775 for (i = 0; i < vertexNum; i++) {
776 vertex[i].x += xtrans;
777 vertex[i].y += ytrans;
778 vertex[i].z += ztrans;
783 boundingsphereradius = 0;
784 for (i = 0; i < vertexNum; i++) {
785 for (j = 0; j < vertexNum; j++) {
786 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
787 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
788 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
792 boundingsphereradius = fast_sqrt(boundingsphereradius);
795 void Model::Rotate(float xang, float yang, float zang)
798 for (i = 0; i < vertexNum; i++) {
799 vertex[i] = DoRotation(vertex[i], xang, yang, zang);
804 boundingsphereradius = 0;
805 for (i = 0; i < vertexNum; i++) {
806 for (j = 0; j < vertexNum; j++) {
807 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
808 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
809 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
813 boundingsphereradius = fast_sqrt(boundingsphereradius);
817 void Model::CalculateNormals(bool facenormalise)
819 Game::LoadingScreen();
821 if (type != normaltype && type != decalstype)
824 for (i = 0; i < vertexNum; i++) {
830 for (i = 0; i < TriangleNum; i++) {
831 CrossProduct(vertex[Triangles[i].vertex[1]] - vertex[Triangles[i].vertex[0]], vertex[Triangles[i].vertex[2]] - vertex[Triangles[i].vertex[0]], &facenormals[i]);
833 normals[Triangles[i].vertex[0]].x += facenormals[i].x;
834 normals[Triangles[i].vertex[0]].y += facenormals[i].y;
835 normals[Triangles[i].vertex[0]].z += facenormals[i].z;
837 normals[Triangles[i].vertex[1]].x += facenormals[i].x;
838 normals[Triangles[i].vertex[1]].y += facenormals[i].y;
839 normals[Triangles[i].vertex[1]].z += facenormals[i].z;
841 normals[Triangles[i].vertex[2]].x += facenormals[i].x;
842 normals[Triangles[i].vertex[2]].y += facenormals[i].y;
843 normals[Triangles[i].vertex[2]].z += facenormals[i].z;
845 Normalise(&facenormals[i]);
847 for (i = 0; i < vertexNum; i++) {
848 Normalise(&normals[i]);
851 UpdateVertexArrayNoTex();
854 void Model::drawimmediate()
857 glBegin(GL_TRIANGLES);
858 for (int i = 0; i < TriangleNum; i++) {
859 glTexCoord2f(Triangles[i].gx[0], Triangles[i].gy[0]);
861 glColor3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
863 glNormal3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
865 glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
866 glVertex3f(vertex[Triangles[i].vertex[0]].x, vertex[Triangles[i].vertex[0]].y, vertex[Triangles[i].vertex[0]].z);
868 glTexCoord2f(Triangles[i].gx[1], Triangles[i].gy[1]);
870 glColor3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
872 glNormal3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
874 glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
875 glVertex3f(vertex[Triangles[i].vertex[1]].x, vertex[Triangles[i].vertex[1]].y, vertex[Triangles[i].vertex[1]].z);
877 glTexCoord2f(Triangles[i].gx[2], Triangles[i].gy[2]);
879 glColor3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
881 glNormal3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
883 glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
884 glVertex3f(vertex[Triangles[i].vertex[2]].x, vertex[Triangles[i].vertex[2]].y, vertex[Triangles[i].vertex[2]].z);
891 if (type != normaltype && type != decalstype)
894 glEnableClientState(GL_NORMAL_ARRAY);
895 glEnableClientState(GL_VERTEX_ARRAY);
896 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
899 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
901 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
904 glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
907 glDisableClientState(GL_NORMAL_ARRAY);
909 glDisableClientState(GL_COLOR_ARRAY);
910 glDisableClientState(GL_VERTEX_ARRAY);
911 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
914 //TODO: phase out in favor of Texture
915 void Model::drawdifftex(GLuint texture)
917 glEnableClientState(GL_NORMAL_ARRAY);
918 glEnableClientState(GL_VERTEX_ARRAY);
919 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
921 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
923 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
925 glBindTexture(GL_TEXTURE_2D, (unsigned long)texture);
926 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
927 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
929 glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
932 glDisableClientState(GL_NORMAL_ARRAY);
934 glDisableClientState(GL_COLOR_ARRAY);
935 glDisableClientState(GL_VERTEX_ARRAY);
936 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
939 void Model::drawdifftex(Texture texture)
941 glEnableClientState(GL_NORMAL_ARRAY);
942 glEnableClientState(GL_VERTEX_ARRAY);
943 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
945 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
947 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
950 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
951 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
953 glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
956 glDisableClientState(GL_NORMAL_ARRAY);
958 glDisableClientState(GL_COLOR_ARRAY);
959 glDisableClientState(GL_VERTEX_ARRAY);
960 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
963 void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloodtexture2, Texture breaktexture)
966 if (type != decalstype)
976 glDisable(GL_LIGHTING);
977 glDisable(GL_CULL_FACE);
978 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
980 if (numdecals > max_model_decals)
981 numdecals = max_model_decals;
982 for (i = 0; i < numdecals; i++) {
983 if (decaltype[i] == blooddecalfast && decalalivetime[i] < 2)
984 decalalivetime[i] = 2;
986 if (decaltype[i] == shadowdecal && decaltype[i] != lasttype) {
987 shadowtexture.bind();
990 glAlphaFunc(GL_GREATER, 0.0001);
991 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
994 if (decaltype[i] == breakdecal && decaltype[i] != lasttype) {
998 glAlphaFunc(GL_GREATER, 0.0001);
999 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1002 if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalslow) && decaltype[i] != lasttype) {
1003 bloodtexture.bind();
1006 glAlphaFunc(GL_GREATER, 0.15);
1007 glBlendFunc(GL_ONE, GL_ZERO);
1010 if ((decaltype[i] == blooddecalfast) && decaltype[i] != lasttype) {
1011 bloodtexture2.bind();
1014 glAlphaFunc(GL_GREATER, 0.15);
1015 glBlendFunc(GL_ONE, GL_ZERO);
1018 if (decaltype[i] == shadowdecal) {
1019 glColor4f(1, 1, 1, decalopacity[i]);
1021 if (decaltype[i] == breakdecal) {
1022 glColor4f(1, 1, 1, decalopacity[i]);
1023 if (decalalivetime[i] > 58)
1024 glColor4f(1, 1, 1, decalopacity[i] * (60 - decalalivetime[i]) / 2);
1026 if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow)) {
1027 glColor4f(1, 1, 1, decalopacity[i]);
1028 if (decalalivetime[i] < 4)
1029 glColor4f(1, 1, 1, decalopacity[i]*decalalivetime[i]*.25);
1030 if (decalalivetime[i] > 58)
1031 glColor4f(1, 1, 1, decalopacity[i] * (60 - decalalivetime[i]) / 2);
1033 lasttype = decaltype[i];
1034 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1035 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
1037 glMatrixMode(GL_MODELVIEW);
1039 glBegin(GL_TRIANGLES);
1040 for (int j = 0; j < 3; j++) {
1041 glTexCoord2f(decaltexcoords[i][j][0], decaltexcoords[i][j][1]);
1042 glVertex3f(decalvertex[i][j].x, decalvertex[i][j].y, decalvertex[i][j].z);
1047 for (i = numdecals - 1; i >= 0; i--) {
1048 decalalivetime[i] += multiplier;
1049 if (decaltype[i] == blooddecalslow)
1050 decalalivetime[i] -= multiplier * 2 / 3;
1051 if (decaltype[i] == blooddecalfast)
1052 decalalivetime[i] += multiplier * 4;
1053 if (decaltype[i] == shadowdecal)
1055 if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow) && decalalivetime[i] >= 60)
1058 glAlphaFunc(GL_GREATER, 0.0001);
1059 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1063 void Model::DeleteDecal(int which)
1066 if (type != decalstype)
1068 decaltype[which] = decaltype[numdecals - 1];
1069 decalposition[which] = decalposition[numdecals - 1];
1070 for (int i = 0; i < 3; i++) {
1071 decalvertex[which][i] = decalvertex[numdecals - 1][i];
1072 decaltexcoords[which][i][0] = decaltexcoords[numdecals - 1][i][0];
1073 decaltexcoords[which][i][1] = decaltexcoords[numdecals - 1][i][1];
1075 decalrotation[which] = decalrotation[numdecals - 1];
1076 decalalivetime[which] = decalalivetime[numdecals - 1];
1077 decalopacity[which] = decalopacity[numdecals - 1];
1082 void Model::MakeDecal(int atype, XYZ *where, float *size, float *opacity, float *rotation)
1085 if (type != decalstype)
1088 static float placex, placez;
1090 static float distance;
1094 if (distsq(where, &boundingspherecenter) < (boundingsphereradius + *size) * (boundingsphereradius + *size))
1095 for (i = 0; i < TriangleNum; i++) {
1096 if (facenormals[i].y < -.1 && (vertex[Triangles[i].vertex[0]].y < where->y || vertex[Triangles[i].vertex[1]].y < where->y || vertex[Triangles[i].vertex[2]].y < where->y)) {
1097 decalposition[numdecals] = *where;
1098 decaltype[numdecals] = atype;
1099 decalrotation[numdecals] = *rotation;
1100 decalalivetime[numdecals] = 0;
1101 distance = abs(((facenormals[i].x * where->x) + (facenormals[i].y * where->y) + (facenormals[i].z * where->z) - ((facenormals[i].x * vertex[Triangles[i].vertex[0]].x) + (facenormals[i].y * vertex[Triangles[i].vertex[0]].y) + (facenormals[i].z * vertex[Triangles[i].vertex[0]].z))) / facenormals[i].y);
1102 decalopacity[numdecals] = *opacity - distance / 10;
1104 if (decalopacity[numdecals > 0]) {
1105 placex = vertex[Triangles[i].vertex[0]].x;
1106 placez = vertex[Triangles[i].vertex[0]].z;
1108 decaltexcoords[numdecals][0][0] = (placex - where->x) / (*size) / 2 + .5;
1109 decaltexcoords[numdecals][0][1] = (placez - where->z) / (*size) / 2 + .5;
1111 decalvertex[numdecals][0].x = placex;
1112 decalvertex[numdecals][0].z = placez;
1113 decalvertex[numdecals][0].y = vertex[Triangles[i].vertex[0]].y;
1116 placex = vertex[Triangles[i].vertex[1]].x;
1117 placez = vertex[Triangles[i].vertex[1]].z;
1119 decaltexcoords[numdecals][1][0] = (placex - where->x) / (*size) / 2 + .5;
1120 decaltexcoords[numdecals][1][1] = (placez - where->z) / (*size) / 2 + .5;
1122 decalvertex[numdecals][1].x = placex;
1123 decalvertex[numdecals][1].z = placez;
1124 decalvertex[numdecals][1].y = vertex[Triangles[i].vertex[1]].y;
1127 placex = vertex[Triangles[i].vertex[2]].x;
1128 placez = vertex[Triangles[i].vertex[2]].z;
1130 decaltexcoords[numdecals][2][0] = (placex - where->x) / (*size) / 2 + .5;
1131 decaltexcoords[numdecals][2][1] = (placez - where->z) / (*size) / 2 + .5;
1133 decalvertex[numdecals][2].x = placex;
1134 decalvertex[numdecals][2].z = placez;
1135 decalvertex[numdecals][2].y = vertex[Triangles[i].vertex[2]].y;
1137 if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0))
1138 if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
1139 if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
1140 if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) {
1141 if (decalrotation[numdecals]) {
1142 for (j = 0; j < 3; j++) {
1144 rot.x = decaltexcoords[numdecals][j][0] - .5;
1145 rot.z = decaltexcoords[numdecals][j][1] - .5;
1146 rot = DoRotation(rot, 0, -decalrotation[numdecals], 0);
1147 decaltexcoords[numdecals][j][0] = rot.x + .5;
1148 decaltexcoords[numdecals][j][1] = rot.z + .5;
1151 if (numdecals < max_model_decals - 1)
1160 void Model::MakeDecal(int atype, XYZ where, float size, float opacity, float rotation)
1163 if (type != decalstype)
1166 static float placex, placez;
1168 static float distance;
1172 if (distsq(&where, &boundingspherecenter) < (boundingsphereradius + size) * (boundingsphereradius + size))
1173 for (i = 0; i < TriangleNum; i++) {
1174 distance = abs(((facenormals[i].x * where.x) + (facenormals[i].y * where.y) + (facenormals[i].z * where.z) - ((facenormals[i].x * vertex[Triangles[i].vertex[0]].x) + (facenormals[i].y * vertex[Triangles[i].vertex[0]].y) + (facenormals[i].z * vertex[Triangles[i].vertex[0]].z))));
1175 if (distance < .02 && abs(facenormals[i].y) > abs(facenormals[i].x) && abs(facenormals[i].y) > abs(facenormals[i].z)) {
1176 decalposition[numdecals] = where;
1177 decaltype[numdecals] = atype;
1178 decalrotation[numdecals] = rotation;
1179 decalalivetime[numdecals] = 0;
1180 decalopacity[numdecals] = opacity - distance / 10;
1182 if (decalopacity[numdecals > 0]) {
1183 placex = vertex[Triangles[i].vertex[0]].x;
1184 placez = vertex[Triangles[i].vertex[0]].z;
1186 decaltexcoords[numdecals][0][0] = (placex - where.x) / (size) / 2 + .5;
1187 decaltexcoords[numdecals][0][1] = (placez - where.z) / (size) / 2 + .5;
1189 decalvertex[numdecals][0].x = placex;
1190 decalvertex[numdecals][0].z = placez;
1191 decalvertex[numdecals][0].y = vertex[Triangles[i].vertex[0]].y;
1194 placex = vertex[Triangles[i].vertex[1]].x;
1195 placez = vertex[Triangles[i].vertex[1]].z;
1197 decaltexcoords[numdecals][1][0] = (placex - where.x) / (size) / 2 + .5;
1198 decaltexcoords[numdecals][1][1] = (placez - where.z) / (size) / 2 + .5;
1200 decalvertex[numdecals][1].x = placex;
1201 decalvertex[numdecals][1].z = placez;
1202 decalvertex[numdecals][1].y = vertex[Triangles[i].vertex[1]].y;
1205 placex = vertex[Triangles[i].vertex[2]].x;
1206 placez = vertex[Triangles[i].vertex[2]].z;
1208 decaltexcoords[numdecals][2][0] = (placex - where.x) / (size) / 2 + .5;
1209 decaltexcoords[numdecals][2][1] = (placez - where.z) / (size) / 2 + .5;
1211 decalvertex[numdecals][2].x = placex;
1212 decalvertex[numdecals][2].z = placez;
1213 decalvertex[numdecals][2].y = vertex[Triangles[i].vertex[2]].y;
1215 if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0))
1216 if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
1217 if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
1218 if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) {
1219 if (decalrotation[numdecals]) {
1220 for (j = 0; j < 3; j++) {
1222 rot.x = decaltexcoords[numdecals][j][0] - .5;
1223 rot.z = decaltexcoords[numdecals][j][1] - .5;
1224 rot = DoRotation(rot, 0, -decalrotation[numdecals], 0);
1225 decaltexcoords[numdecals][j][0] = rot.x + .5;
1226 decaltexcoords[numdecals][j][1] = rot.z + .5;
1229 if (numdecals < max_model_decals - 1)
1233 } else if (distance < .02 && abs(facenormals[i].x) > abs(facenormals[i].y) && abs(facenormals[i].x) > abs(facenormals[i].z)) {
1234 decalposition[numdecals] = where;
1235 decaltype[numdecals] = atype;
1236 decalrotation[numdecals] = rotation;
1237 decalalivetime[numdecals] = 0;
1238 decalopacity[numdecals] = opacity - distance / 10;
1240 if (decalopacity[numdecals > 0]) {
1241 placex = vertex[Triangles[i].vertex[0]].y;
1242 placez = vertex[Triangles[i].vertex[0]].z;
1244 decaltexcoords[numdecals][0][0] = (placex - where.y) / (size) / 2 + .5;
1245 decaltexcoords[numdecals][0][1] = (placez - where.z) / (size) / 2 + .5;
1247 decalvertex[numdecals][0].x = vertex[Triangles[i].vertex[0]].x;
1248 decalvertex[numdecals][0].z = placez;
1249 decalvertex[numdecals][0].y = placex;
1252 placex = vertex[Triangles[i].vertex[1]].y;
1253 placez = vertex[Triangles[i].vertex[1]].z;
1255 decaltexcoords[numdecals][1][0] = (placex - where.y) / (size) / 2 + .5;
1256 decaltexcoords[numdecals][1][1] = (placez - where.z) / (size) / 2 + .5;
1258 decalvertex[numdecals][1].x = vertex[Triangles[i].vertex[1]].x;
1259 decalvertex[numdecals][1].z = placez;
1260 decalvertex[numdecals][1].y = placex;
1263 placex = vertex[Triangles[i].vertex[2]].y;
1264 placez = vertex[Triangles[i].vertex[2]].z;
1266 decaltexcoords[numdecals][2][0] = (placex - where.y) / (size) / 2 + .5;
1267 decaltexcoords[numdecals][2][1] = (placez - where.z) / (size) / 2 + .5;
1269 decalvertex[numdecals][2].x = vertex[Triangles[i].vertex[2]].x;
1270 decalvertex[numdecals][2].z = placez;
1271 decalvertex[numdecals][2].y = placex;
1273 if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0))
1274 if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
1275 if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
1276 if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) {
1277 if (decalrotation[numdecals]) {
1278 for (j = 0; j < 3; j++) {
1280 rot.x = decaltexcoords[numdecals][j][0] - .5;
1281 rot.z = decaltexcoords[numdecals][j][1] - .5;
1282 rot = DoRotation(rot, 0, -decalrotation[numdecals], 0);
1283 decaltexcoords[numdecals][j][0] = rot.x + .5;
1284 decaltexcoords[numdecals][j][1] = rot.z + .5;
1287 if (numdecals < max_model_decals - 1)
1291 } else if (distance < .02 && abs(facenormals[i].z) > abs(facenormals[i].y) && abs(facenormals[i].z) > abs(facenormals[i].x)) {
1292 decalposition[numdecals] = where;
1293 decaltype[numdecals] = atype;
1294 decalrotation[numdecals] = rotation;
1295 decalalivetime[numdecals] = 0;
1296 decalopacity[numdecals] = opacity - distance / 10;
1298 if (decalopacity[numdecals > 0]) {
1299 placex = vertex[Triangles[i].vertex[0]].x;
1300 placez = vertex[Triangles[i].vertex[0]].y;
1302 decaltexcoords[numdecals][0][0] = (placex - where.x) / (size) / 2 + .5;
1303 decaltexcoords[numdecals][0][1] = (placez - where.y) / (size) / 2 + .5;
1305 decalvertex[numdecals][0].x = placex;
1306 decalvertex[numdecals][0].z = vertex[Triangles[i].vertex[0]].z;
1307 decalvertex[numdecals][0].y = placez;
1310 placex = vertex[Triangles[i].vertex[1]].x;
1311 placez = vertex[Triangles[i].vertex[1]].y;
1313 decaltexcoords[numdecals][1][0] = (placex - where.x) / (size) / 2 + .5;
1314 decaltexcoords[numdecals][1][1] = (placez - where.y) / (size) / 2 + .5;
1316 decalvertex[numdecals][1].x = placex;
1317 decalvertex[numdecals][1].z = vertex[Triangles[i].vertex[1]].z;
1318 decalvertex[numdecals][1].y = placez;
1321 placex = vertex[Triangles[i].vertex[2]].x;
1322 placez = vertex[Triangles[i].vertex[2]].y;
1324 decaltexcoords[numdecals][2][0] = (placex - where.x) / (size) / 2 + .5;
1325 decaltexcoords[numdecals][2][1] = (placez - where.y) / (size) / 2 + .5;
1327 decalvertex[numdecals][2].x = placex;
1328 decalvertex[numdecals][2].z = vertex[Triangles[i].vertex[2]].z;
1329 decalvertex[numdecals][2].y = placez;
1331 if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0))
1332 if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0))
1333 if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1))
1334 if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) {
1335 if (decalrotation[numdecals]) {
1336 for (j = 0; j < 3; j++) {
1338 rot.x = decaltexcoords[numdecals][j][0] - .5;
1339 rot.z = decaltexcoords[numdecals][j][1] - .5;
1340 rot = DoRotation(rot, 0, -decalrotation[numdecals], 0);
1341 decaltexcoords[numdecals][j][0] = rot.x + .5;
1342 decaltexcoords[numdecals][j][1] = rot.z + .5;
1345 if (numdecals < max_model_decals - 1)
1359 void Model::deallocate()
1393 if (decaltexcoords) {
1394 for (i = 0; i < max_model_decals; i++) {
1395 for (j = 0; j < 3; j++) {
1396 free(decaltexcoords[i][j]);
1398 free(decaltexcoords[i]);
1400 free(decaltexcoords);
1406 for (i = 0; i < max_model_decals; i++) {
1407 free(decalvertex[i]);
1420 free(decalrotation);
1423 free(decalalivetime);
1426 free(decalposition);
1432 : vertexNum(0), TriangleNum(0),
1434 type(0), oldtype(0),
1443 memset(&modelTexture, 0, sizeof(modelTexture));
1447 boundingspherecenter = 0;
1448 boundingsphereradius = 0;