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)
35 static float distance;
36 static float olddistance;
37 static int intersecting;
38 static int firstintersecting;
44 *p1 = DoRotation(*p1, 0, -*rotate, 0);
46 *p2 = DoRotation(*p2, 0, -*rotate, 0);
47 if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
49 firstintersecting = -1;
51 for (unsigned int j = 0; j < Triangles.size(); j++) {
52 intersecting = LineFacetd(p1, p2, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], &Triangles[j].facenormal, &point);
53 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);
54 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
55 olddistance = distance;
56 firstintersecting = j;
62 *p = DoRotation(*p, 0, *rotate, 0);
64 return firstintersecting;
67 int Model::LineCheckPossible(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
69 static float distance;
70 static float olddistance;
71 static int intersecting;
72 static int firstintersecting;
77 if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
79 firstintersecting = -1;
81 *p1 = DoRotation(*p1, 0, -*rotate, 0);
83 *p2 = DoRotation(*p2, 0, -*rotate, 0);
85 for (unsigned int j = 0; j < possible.size(); j++) {
86 if (possible[j] < Triangles.size()) {
87 intersecting = LineFacetd(p1, p2, &vertex[Triangles[possible[j]].vertex[0]], &vertex[Triangles[possible[j]].vertex[1]], &vertex[Triangles[possible[j]].vertex[2]], &Triangles[possible[j]].facenormal, &point);
88 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);
89 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
90 olddistance = distance;
91 firstintersecting = possible[j];
98 *p = DoRotation(*p, 0, *rotate, 0);
100 return firstintersecting;
103 int Model::LineCheckSlidePossible(XYZ *p1, XYZ *p2, XYZ *move, float *rotate)
105 static float distance;
106 static float olddistance;
107 static int intersecting;
108 static int firstintersecting;
113 if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
115 firstintersecting = -1;
117 *p1 = DoRotation(*p1, 0, -*rotate, 0);
119 *p2 = DoRotation(*p2, 0, -*rotate, 0);
121 for (unsigned int j = 0; j < possible.size(); j++) {
122 if (possible[j] < Triangles.size()) {
123 intersecting = LineFacetd(p1, p2, &vertex[Triangles[possible[j]].vertex[0]], &vertex[Triangles[possible[j]].vertex[1]], &vertex[Triangles[possible[j]].vertex[2]], &Triangles[possible[j]].facenormal, &point);
124 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);
125 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
126 olddistance = distance;
127 firstintersecting = possible[j];
132 if (firstintersecting > 0) {
133 distance = abs((Triangles[firstintersecting].facenormal.x * p2->x) + (Triangles[firstintersecting].facenormal.y * p2->y) + (Triangles[firstintersecting].facenormal.z * p2->z) - ((Triangles[firstintersecting].facenormal.x * vertex[Triangles[firstintersecting].vertex[0]].x) + (Triangles[firstintersecting].facenormal.y * vertex[Triangles[firstintersecting].vertex[0]].y) + (Triangles[firstintersecting].facenormal.z * vertex[Triangles[firstintersecting].vertex[0]].z)));
134 *p2 -= Triangles[firstintersecting].facenormal * distance;
138 *p2 = DoRotation(*p2, 0, *rotate, 0);
140 return firstintersecting;
143 int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
146 static float distance;
147 static float olddistance;
148 static int intersecting;
149 static int firstintersecting;
153 firstintersecting = -1;
158 *p1 = DoRotation(*p1, 0, -*rotate, 0);
159 if (distsq(p1, &boundingspherecenter) > radius * radius + boundingsphereradius * boundingsphereradius)
162 for (i = 0; i < 4; i++) {
163 for (unsigned int j = 0; j < Triangles.size(); j++) {
165 distance = abs((Triangles[j].facenormal.x * p1->x) + (Triangles[j].facenormal.y * p1->y) + (Triangles[j].facenormal.z * p1->z) - ((Triangles[j].facenormal.x * vertex[Triangles[j].vertex[0]].x) + (Triangles[j].facenormal.y * vertex[Triangles[j].vertex[0]].y) + (Triangles[j].facenormal.z * vertex[Triangles[j].vertex[0]].z)));
166 if (distance < radius) {
167 point = *p1 - Triangles[j].facenormal * distance;
168 if (PointInTriangle( &point, Triangles[j].facenormal, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
171 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], p1, &radius);
173 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], p1, &radius);
175 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
177 *p1 += Triangles[j].facenormal * (distance - radius);
180 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
181 olddistance = distance;
182 firstintersecting = j;
188 *p = DoRotation(*p, 0, *rotate, 0);
191 *p1 = DoRotation(*p1, 0, *rotate, 0);
193 return firstintersecting;
196 int Model::SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate)
198 static float distance;
199 static float olddistance;
200 static int intersecting;
201 static int firstintersecting;
205 firstintersecting = -1;
213 *p1 = DoRotation(*p1, 0, -*rotate, 0);
214 if (distsq(p1, &boundingspherecenter) > radius * radius + boundingsphereradius * boundingsphereradius) {
219 for (unsigned int j = 0; j < Triangles.size(); j++) {
221 distance = abs((Triangles[j].facenormal.x * p1->x) + (Triangles[j].facenormal.y * p1->y) + (Triangles[j].facenormal.z * p1->z) - ((Triangles[j].facenormal.x * vertex[Triangles[j].vertex[0]].x) + (Triangles[j].facenormal.y * vertex[Triangles[j].vertex[0]].y) + (Triangles[j].facenormal.z * vertex[Triangles[j].vertex[0]].z)));
222 if (distance < radius) {
223 point = *p1 - Triangles[j].facenormal * distance;
224 if (PointInTriangle( &point, Triangles[j].facenormal, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
227 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], p1, &radius);
229 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], p1, &radius);
231 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
233 possible.push_back(j);
236 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
237 olddistance = distance;
238 firstintersecting = j;
242 *p1 = DoRotation(*p1, 0, *rotate, 0);
245 return firstintersecting;
249 void Model::UpdateVertexArray()
251 if (type != normaltype && type != decalstype)
255 for (unsigned int i = 0; i < Triangles.size(); i++) {
256 unsigned int j = i * 24;
257 vArray[j + 0] = Triangles[i].gx[0];
258 vArray[j + 1] = Triangles[i].gy[0];
259 vArray[j + 2] = Triangles[i].facenormal.x * -1;
260 vArray[j + 3] = Triangles[i].facenormal.y * -1;
261 vArray[j + 4] = Triangles[i].facenormal.z * -1;
262 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
263 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
264 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
266 vArray[j + 8] = Triangles[i].gx[1];
267 vArray[j + 9] = Triangles[i].gy[1];
268 vArray[j + 10] = Triangles[i].facenormal.x * -1;
269 vArray[j + 11] = Triangles[i].facenormal.y * -1;
270 vArray[j + 12] = Triangles[i].facenormal.z * -1;
271 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
272 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
273 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
275 vArray[j + 16] = Triangles[i].gx[2];
276 vArray[j + 17] = Triangles[i].gy[2];
277 vArray[j + 18] = Triangles[i].facenormal.x * -1;
278 vArray[j + 19] = Triangles[i].facenormal.y * -1;
279 vArray[j + 20] = Triangles[i].facenormal.z * -1;
280 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
281 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
282 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
285 for (unsigned int i = 0; i < Triangles.size(); i++) {
286 unsigned int j = i * 24;
287 vArray[j + 0] = Triangles[i].gx[0];
288 vArray[j + 1] = Triangles[i].gy[0];
289 vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
290 vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
291 vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
292 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
293 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
294 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
296 vArray[j + 8] = Triangles[i].gx[1];
297 vArray[j + 9] = Triangles[i].gy[1];
298 vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
299 vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
300 vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
301 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
302 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
303 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
305 vArray[j + 16] = Triangles[i].gx[2];
306 vArray[j + 17] = Triangles[i].gy[2];
307 vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
308 vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
309 vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
310 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
311 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
312 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
317 void Model::UpdateVertexArrayNoTex()
319 if (type != normaltype && type != decalstype)
323 for (unsigned int i = 0; i < Triangles.size(); i++) {
324 unsigned int j = i * 24;
325 vArray[j + 2] = Triangles[i].facenormal.x * -1;
326 vArray[j + 3] = Triangles[i].facenormal.y * -1;
327 vArray[j + 4] = Triangles[i].facenormal.z * -1;
328 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
329 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
330 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
332 vArray[j + 10] = Triangles[i].facenormal.x * -1;
333 vArray[j + 11] = Triangles[i].facenormal.y * -1;
334 vArray[j + 12] = Triangles[i].facenormal.z * -1;
335 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
336 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
337 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
339 vArray[j + 18] = Triangles[i].facenormal.x * -1;
340 vArray[j + 19] = Triangles[i].facenormal.y * -1;
341 vArray[j + 20] = Triangles[i].facenormal.z * -1;
342 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
343 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
344 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
347 for (unsigned int i = 0; i < Triangles.size(); i++) {
348 unsigned int j = i * 24;
349 vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
350 vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
351 vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
352 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
353 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
354 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
356 vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
357 vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
358 vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
359 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
360 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
361 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
363 vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
364 vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
365 vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
366 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
367 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
368 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
373 void Model::UpdateVertexArrayNoTexNoNorm()
375 if (type != normaltype && type != decalstype)
378 for (unsigned int i = 0; i < Triangles.size(); i++) {
379 unsigned int j = i * 24;
380 vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
381 vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
382 vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
384 vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
385 vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
386 vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
388 vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
389 vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
390 vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
394 bool Model::loadnotex(const std::string& filename)
403 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
405 // read model settings
407 fseek(tfile, 0, SEEK_SET);
408 funpackf(tfile, "Bs Bs", &vertexNum, &triangleNum);
410 // read the model data
415 owner = (int*)malloc(sizeof(int) * vertexNum);
416 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
417 Triangles.resize(triangleNum);
418 vArray = (GLfloat*)malloc(sizeof(GLfloat) * triangleNum * 24);
420 for (i = 0; i < vertexNum; i++) {
421 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
424 for (i = 0; i < triangleNum; i++) {
426 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
427 Triangles[i].vertex[0] = vertex[0];
428 Triangles[i].vertex[1] = vertex[2];
429 Triangles[i].vertex[2] = vertex[4];
430 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
431 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
438 for (i = 0; i < vertexNum; i++) {
442 boundingsphereradius = 0;
443 for (i = 0; i < vertexNum; i++) {
444 for (int j = 0; j < vertexNum; j++) {
445 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
446 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
447 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
451 boundingsphereradius = fast_sqrt(boundingsphereradius);
457 bool Model::load(const std::string& filename)
465 LOG(std::string("Loading model...") + filename);
467 Game::LoadingScreen();
472 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
474 // read model settings
476 fseek(tfile, 0, SEEK_SET);
477 funpackf(tfile, "Bs Bs", &vertexNum, &triangleNum);
479 // read the model data
484 owner = (int*)malloc(sizeof(int) * vertexNum);
485 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
486 normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
487 Triangles.resize(triangleNum);
488 vArray = (GLfloat*)malloc(sizeof(GLfloat) * triangleNum * 24);
490 for (i = 0; i < vertexNum; i++) {
491 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
494 for (i = 0; i < triangleNum; i++) {
496 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
497 Triangles[i].vertex[0] = vertex[0];
498 Triangles[i].vertex[1] = vertex[2];
499 Triangles[i].vertex[2] = vertex[4];
500 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
501 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
504 modelTexture.xsz = 0;
510 for (i = 0; i < vertexNum; i++) {
515 boundingsphereradius = 0;
516 for (i = 0; i < vertexNum; i++) {
517 for (j = 0; j < vertexNum; j++) {
518 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
519 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
520 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
524 boundingsphereradius = fast_sqrt(boundingsphereradius);
529 bool Model::loaddecal(const std::string& filename)
537 LOG(std::string("Loading decal...") + Folders::getResourcePath(filename));
542 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
544 // read model settings
546 fseek(tfile, 0, SEEK_SET);
547 funpackf(tfile, "Bs Bs", &vertexNum, &triangleNum);
549 // read the model data
555 owner = (int*)malloc(sizeof(int) * vertexNum);
556 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
557 normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
558 Triangles.resize(triangleNum);
559 vArray = (GLfloat*)malloc(sizeof(GLfloat) * triangleNum * 24);
561 for (i = 0; i < vertexNum; i++) {
562 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
565 for (i = 0; i < triangleNum; i++) {
567 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
568 Triangles[i].vertex[0] = vertex[0];
569 Triangles[i].vertex[1] = vertex[2];
570 Triangles[i].vertex[2] = vertex[4];
571 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
572 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
576 modelTexture.xsz = 0;
582 for (i = 0; i < vertexNum; i++) {
586 boundingsphereradius = 0;
587 for (i = 0; i < vertexNum; i++) {
588 for (j = 0; j < vertexNum; j++) {
589 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
590 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
591 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
595 boundingsphereradius = fast_sqrt(boundingsphereradius);
600 bool Model::loadraw(const std::string& filename)
608 LOG(std::string("Loading raw...") + filename);
613 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
615 // read model settings
617 fseek(tfile, 0, SEEK_SET);
618 funpackf(tfile, "Bs Bs", &vertexNum, &triangleNum);
620 // read the model data
625 owner = (int*)malloc(sizeof(int) * vertexNum);
626 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
627 Triangles.resize(triangleNum);
628 vArray = (GLfloat*)malloc(sizeof(GLfloat) * triangleNum * 24);
631 for (i = 0; i < vertexNum; i++) {
632 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
635 for (i = 0; i < triangleNum; i++) {
637 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
638 Triangles[i].vertex[0] = vertex[0];
639 Triangles[i].vertex[1] = vertex[2];
640 Triangles[i].vertex[2] = vertex[4];
641 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
642 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
648 for (i = 0; i < vertexNum; i++) {
656 void Model::UniformTexCoords()
658 for (unsigned int i = 0; i < Triangles.size(); i++) {
659 Triangles[i].gy[0] = vertex[Triangles[i].vertex[0]].y;
660 Triangles[i].gy[1] = vertex[Triangles[i].vertex[1]].y;
661 Triangles[i].gy[2] = vertex[Triangles[i].vertex[2]].y;
662 Triangles[i].gx[0] = vertex[Triangles[i].vertex[0]].x;
663 Triangles[i].gx[1] = vertex[Triangles[i].vertex[1]].x;
664 Triangles[i].gx[2] = vertex[Triangles[i].vertex[2]].x;
670 void Model::FlipTexCoords()
672 for (unsigned int i = 0; i < Triangles.size(); i++) {
673 Triangles[i].gy[0] = -Triangles[i].gy[0];
674 Triangles[i].gy[1] = -Triangles[i].gy[1];
675 Triangles[i].gy[2] = -Triangles[i].gy[2];
680 void Model::ScaleTexCoords(float howmuch)
682 for (unsigned int i = 0; i < Triangles.size(); i++) {
683 Triangles[i].gx[0] *= howmuch;
684 Triangles[i].gx[1] *= howmuch;
685 Triangles[i].gx[2] *= howmuch;
686 Triangles[i].gy[0] *= howmuch;
687 Triangles[i].gy[1] *= howmuch;
688 Triangles[i].gy[2] *= howmuch;
693 void Model::Scale(float xscale, float yscale, float zscale)
696 for (i = 0; i < vertexNum; i++) {
697 vertex[i].x *= xscale;
698 vertex[i].y *= yscale;
699 vertex[i].z *= zscale;
705 boundingsphereradius = 0;
706 for (i = 0; i < vertexNum; i++) {
707 for (j = 0; j < vertexNum; j++) {
708 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
709 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
710 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
714 boundingsphereradius = fast_sqrt(boundingsphereradius);
717 void Model::ScaleNormals(float xscale, float yscale, float zscale)
719 if (type != normaltype && type != decalstype)
722 for (int i = 0; i < vertexNum; i++) {
723 normals[i].x *= xscale;
724 normals[i].y *= yscale;
725 normals[i].z *= zscale;
727 for (unsigned int i = 0; i < Triangles.size(); i++) {
728 Triangles[i].facenormal.x *= xscale;
729 Triangles[i].facenormal.y *= yscale;
730 Triangles[i].facenormal.z *= zscale;
735 void Model::Translate(float xtrans, float ytrans, float ztrans)
738 for (i = 0; i < vertexNum; i++) {
739 vertex[i].x += xtrans;
740 vertex[i].y += ytrans;
741 vertex[i].z += ztrans;
746 boundingsphereradius = 0;
747 for (i = 0; i < vertexNum; i++) {
748 for (j = 0; j < vertexNum; j++) {
749 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
750 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
751 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
755 boundingsphereradius = fast_sqrt(boundingsphereradius);
758 void Model::Rotate(float xang, float yang, float zang)
761 for (i = 0; i < vertexNum; i++) {
762 vertex[i] = DoRotation(vertex[i], xang, yang, zang);
767 boundingsphereradius = 0;
768 for (i = 0; i < vertexNum; i++) {
769 for (j = 0; j < vertexNum; j++) {
770 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
771 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
772 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
776 boundingsphereradius = fast_sqrt(boundingsphereradius);
780 void Model::CalculateNormals(bool facenormalise)
782 Game::LoadingScreen();
784 if (type != normaltype && type != decalstype)
787 for (int i = 0; i < vertexNum; i++) {
793 for (unsigned int i = 0; i < Triangles.size(); i++) {
794 CrossProduct(vertex[Triangles[i].vertex[1]] - vertex[Triangles[i].vertex[0]], vertex[Triangles[i].vertex[2]] - vertex[Triangles[i].vertex[0]], &Triangles[i].facenormal);
796 normals[Triangles[i].vertex[0]].x += Triangles[i].facenormal.x;
797 normals[Triangles[i].vertex[0]].y += Triangles[i].facenormal.y;
798 normals[Triangles[i].vertex[0]].z += Triangles[i].facenormal.z;
800 normals[Triangles[i].vertex[1]].x += Triangles[i].facenormal.x;
801 normals[Triangles[i].vertex[1]].y += Triangles[i].facenormal.y;
802 normals[Triangles[i].vertex[1]].z += Triangles[i].facenormal.z;
804 normals[Triangles[i].vertex[2]].x += Triangles[i].facenormal.x;
805 normals[Triangles[i].vertex[2]].y += Triangles[i].facenormal.y;
806 normals[Triangles[i].vertex[2]].z += Triangles[i].facenormal.z;
808 Normalise(&Triangles[i].facenormal);
810 for (int i = 0; i < vertexNum; i++) {
811 Normalise(&normals[i]);
814 UpdateVertexArrayNoTex();
817 void Model::drawimmediate()
820 glBegin(GL_TRIANGLES);
821 for (unsigned int i = 0; i < Triangles.size(); i++) {
822 glTexCoord2f(Triangles[i].gx[0], Triangles[i].gy[0]);
824 glColor3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
826 glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
828 glNormal3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
830 glVertex3f(vertex[Triangles[i].vertex[0]].x, vertex[Triangles[i].vertex[0]].y, vertex[Triangles[i].vertex[0]].z);
832 glTexCoord2f(Triangles[i].gx[1], Triangles[i].gy[1]);
834 glColor3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
836 glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
838 glNormal3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
840 glVertex3f(vertex[Triangles[i].vertex[1]].x, vertex[Triangles[i].vertex[1]].y, vertex[Triangles[i].vertex[1]].z);
842 glTexCoord2f(Triangles[i].gx[2], Triangles[i].gy[2]);
844 glColor3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
846 glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
848 glNormal3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
850 glVertex3f(vertex[Triangles[i].vertex[2]].x, vertex[Triangles[i].vertex[2]].y, vertex[Triangles[i].vertex[2]].z);
857 if (type != normaltype && type != decalstype)
860 glEnableClientState(GL_NORMAL_ARRAY);
861 glEnableClientState(GL_VERTEX_ARRAY);
862 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
865 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
867 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
871 glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
874 glDisableClientState(GL_COLOR_ARRAY);
876 glDisableClientState(GL_NORMAL_ARRAY);
878 glDisableClientState(GL_VERTEX_ARRAY);
879 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
882 void Model::drawdifftex(Texture texture)
884 glEnableClientState(GL_NORMAL_ARRAY);
885 glEnableClientState(GL_VERTEX_ARRAY);
886 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
888 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
890 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
894 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
895 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
897 glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
900 glDisableClientState(GL_COLOR_ARRAY);
902 glDisableClientState(GL_NORMAL_ARRAY);
904 glDisableClientState(GL_VERTEX_ARRAY);
905 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
908 void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloodtexture2, Texture breaktexture)
911 if (type != decalstype) {
919 glDisable(GL_LIGHTING);
920 glDisable(GL_CULL_FACE);
921 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
923 for (unsigned int i = 0; i < decals.size(); i++) {
924 if (decals[i].type == blooddecalfast && decals[i].alivetime < 2)
925 decals[i].alivetime = 2;
927 if (decals[i].type != lasttype) {
928 if (decals[i].type == shadowdecal) {
929 shadowtexture.bind();
932 glAlphaFunc(GL_GREATER, 0.0001);
933 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
936 if (decals[i].type == breakdecal) {
940 glAlphaFunc(GL_GREATER, 0.0001);
941 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
944 if (decals[i].type == blooddecal || decals[i].type == blooddecalslow) {
948 glAlphaFunc(GL_GREATER, 0.15);
949 glBlendFunc(GL_ONE, GL_ZERO);
952 if (decals[i].type == blooddecalfast) {
953 bloodtexture2.bind();
956 glAlphaFunc(GL_GREATER, 0.15);
957 glBlendFunc(GL_ONE, GL_ZERO);
961 if (decals[i].type == shadowdecal) {
962 glColor4f(1, 1, 1, decals[i].opacity);
964 if (decals[i].type == breakdecal) {
965 glColor4f(1, 1, 1, decals[i].opacity);
966 if (decals[i].alivetime > 58)
967 glColor4f(1, 1, 1, decals[i].opacity * (60 - decals[i].alivetime) / 2);
969 if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow)) {
970 glColor4f(1, 1, 1, decals[i].opacity);
971 if (decals[i].alivetime < 4)
972 glColor4f(1, 1, 1, decals[i].opacity*decals[i].alivetime*.25);
973 if (decals[i].alivetime > 58)
974 glColor4f(1, 1, 1, decals[i].opacity * (60 - decals[i].alivetime) / 2);
976 lasttype = decals[i].type;
977 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
978 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
980 glMatrixMode(GL_MODELVIEW);
982 glBegin(GL_TRIANGLES);
983 for (int j = 0; j < 3; j++) {
984 glTexCoord2f(decals[i].texcoords[j][0], decals[i].texcoords[j][1]);
985 glVertex3f(decals[i].vertex[j].x, decals[i].vertex[j].y, decals[i].vertex[j].z);
990 for (int i = decals.size() - 1; i >= 0; i--) {
991 decals[i].alivetime += multiplier;
992 if (decals[i].type == blooddecalslow)
993 decals[i].alivetime -= multiplier * 2 / 3;
994 if (decals[i].type == blooddecalfast)
995 decals[i].alivetime += multiplier * 4;
996 if (decals[i].type == shadowdecal)
998 if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow) && decals[i].alivetime >= 60)
1001 glAlphaFunc(GL_GREATER, 0.0001);
1002 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1006 void Model::DeleteDecal(int which)
1009 if (type != decalstype)
1011 decals.erase(decals.begin() + which);
1015 void Model::MakeDecal(decal_type atype, XYZ *where, float *size, float *opacity, float *rotation)
1018 if (type != decalstype)
1022 static float distance;
1025 if (distsq(where, &boundingspherecenter) < (boundingsphereradius + *size) * (boundingsphereradius + *size))
1026 for (unsigned int i = 0; i < Triangles.size(); i++) {
1027 if (Triangles[i].facenormal.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)) {
1028 distance = abs(((Triangles[i].facenormal.x * where->x) + (Triangles[i].facenormal.y * where->y) + (Triangles[i].facenormal.z * where->z) - ((Triangles[i].facenormal.x * vertex[Triangles[i].vertex[0]].x) + (Triangles[i].facenormal.y * vertex[Triangles[i].vertex[0]].y) + (Triangles[i].facenormal.z * vertex[Triangles[i].vertex[0]].z))) / Triangles[i].facenormal.y);
1030 if ((*opacity - distance / 10) > 0) {
1031 Decal decal(*where, atype, *opacity - distance / 10, *rotation, *size, *this, i, 0);
1033 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1034 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1035 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1036 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1037 if (decal.rotation) {
1038 for (int j = 0; j < 3; j++) {
1040 rot.x = decal.texcoords[j][0] - .5;
1041 rot.z = decal.texcoords[j][1] - .5;
1042 rot = DoRotation(rot, 0, -decal.rotation, 0);
1043 decal.texcoords[j][0] = rot.x + .5;
1044 decal.texcoords[j][1] = rot.z + .5;
1047 if (decals.size() < max_model_decals - 1) {
1048 decals.push_back(decal);
1057 void Model::MakeDecal(decal_type atype, XYZ where, float size, float opacity, float rotation)
1060 if (type != decalstype)
1064 static float distance;
1067 if (distsq(&where, &boundingspherecenter) < (boundingsphereradius + size) * (boundingsphereradius + size))
1068 for (unsigned int i = 0; i < Triangles.size(); i++) {
1069 distance = abs(((Triangles[i].facenormal.x * where.x) + (Triangles[i].facenormal.y * where.y) + (Triangles[i].facenormal.z * where.z) - ((Triangles[i].facenormal.x * vertex[Triangles[i].vertex[0]].x) + (Triangles[i].facenormal.y * vertex[Triangles[i].vertex[0]].y) + (Triangles[i].facenormal.z * vertex[Triangles[i].vertex[0]].z))));
1070 if (distance < .02 && abs(Triangles[i].facenormal.y) > abs(Triangles[i].facenormal.x) && abs(Triangles[i].facenormal.y) > abs(Triangles[i].facenormal.z)) {
1071 if ((opacity - distance / 10) > 0) {
1072 Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 0);
1074 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1075 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1076 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1077 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1078 if (decal.rotation) {
1079 for (int j = 0; j < 3; j++) {
1081 rot.x = decal.texcoords[j][0] - .5;
1082 rot.z = decal.texcoords[j][1] - .5;
1083 rot = DoRotation(rot, 0, -decal.rotation, 0);
1084 decal.texcoords[j][0] = rot.x + .5;
1085 decal.texcoords[j][1] = rot.z + .5;
1088 if (decals.size() < max_model_decals - 1) {
1089 decals.push_back(decal);
1093 } else if (distance < .02 && abs(Triangles[i].facenormal.x) > abs(Triangles[i].facenormal.y) && abs(Triangles[i].facenormal.x) > abs(Triangles[i].facenormal.z)) {
1094 if ((opacity - distance / 10) > 0) {
1095 Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 1);
1097 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1098 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1099 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1100 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1101 if (decal.rotation) {
1102 for (int j = 0; j < 3; j++) {
1104 rot.x = decal.texcoords[j][0] - .5;
1105 rot.z = decal.texcoords[j][1] - .5;
1106 rot = DoRotation(rot, 0, -decal.rotation, 0);
1107 decal.texcoords[j][0] = rot.x + .5;
1108 decal.texcoords[j][1] = rot.z + .5;
1111 if (decals.size() < max_model_decals - 1) {
1112 decals.push_back(decal);
1116 } else if (distance < .02 && abs(Triangles[i].facenormal.z) > abs(Triangles[i].facenormal.y) && abs(Triangles[i].facenormal.z) > abs(Triangles[i].facenormal.x)) {
1117 if ((opacity - distance / 10) > 0) {
1118 Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 2);
1120 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1121 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1122 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1123 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1124 if (decal.rotation) {
1125 for (int j = 0; j < 3; j++) {
1127 rot.x = decal.texcoords[j][0] - .5;
1128 rot.z = decal.texcoords[j][1] - .5;
1129 rot = DoRotation(rot, 0, -decal.rotation, 0);
1130 decal.texcoords[j][0] = rot.x + .5;
1131 decal.texcoords[j][1] = rot.z + .5;
1134 if (decals.size() < max_model_decals - 1) {
1135 decals.push_back(decal);
1144 void Model::deleteDeadDecals()
1146 for (int i = decals.size() - 1; i >= 0; i--) {
1147 if ((decals[i].type == blooddecal || decals[i].type == blooddecalslow) && decals[i].alivetime < 2) {
1158 void Model::deallocate()
1187 boundingspherecenter(),
1188 boundingsphereradius(0),
1191 memset(&modelTexture, 0, sizeof(modelTexture));