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));
543 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
545 // read model settings
547 fseek(tfile, 0, SEEK_SET);
548 funpackf(tfile, "Bs Bs", &vertexNum, &triangleNum);
550 // read the model data
556 owner = (int*)malloc(sizeof(int) * vertexNum);
557 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
558 normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
559 Triangles.resize(triangleNum);
560 vArray = (GLfloat*)malloc(sizeof(GLfloat) * triangleNum * 24);
562 for (i = 0; i < vertexNum; i++) {
563 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
566 for (i = 0; i < triangleNum; i++) {
568 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
569 Triangles[i].vertex[0] = vertex[0];
570 Triangles[i].vertex[1] = vertex[2];
571 Triangles[i].vertex[2] = vertex[4];
572 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
573 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
577 modelTexture.xsz = 0;
583 for (i = 0; i < vertexNum; i++) {
587 boundingsphereradius = 0;
588 for (i = 0; i < vertexNum; i++) {
589 for (j = 0; j < vertexNum; j++) {
590 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
591 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
592 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
596 boundingsphereradius = fast_sqrt(boundingsphereradius);
601 bool Model::loadraw(const std::string& filename)
609 LOG(std::string("Loading raw...") + filename);
614 tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
616 // read model settings
618 fseek(tfile, 0, SEEK_SET);
619 funpackf(tfile, "Bs Bs", &vertexNum, &triangleNum);
621 // read the model data
626 owner = (int*)malloc(sizeof(int) * vertexNum);
627 vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
628 Triangles.resize(triangleNum);
629 vArray = (GLfloat*)malloc(sizeof(GLfloat) * triangleNum * 24);
632 for (i = 0; i < vertexNum; i++) {
633 funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
636 for (i = 0; i < triangleNum; i++) {
638 funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
639 Triangles[i].vertex[0] = vertex[0];
640 Triangles[i].vertex[1] = vertex[2];
641 Triangles[i].vertex[2] = vertex[4];
642 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
643 funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
649 for (i = 0; i < vertexNum; i++) {
657 void Model::UniformTexCoords()
659 for (unsigned int i = 0; i < Triangles.size(); i++) {
660 Triangles[i].gy[0] = vertex[Triangles[i].vertex[0]].y;
661 Triangles[i].gy[1] = vertex[Triangles[i].vertex[1]].y;
662 Triangles[i].gy[2] = vertex[Triangles[i].vertex[2]].y;
663 Triangles[i].gx[0] = vertex[Triangles[i].vertex[0]].x;
664 Triangles[i].gx[1] = vertex[Triangles[i].vertex[1]].x;
665 Triangles[i].gx[2] = vertex[Triangles[i].vertex[2]].x;
671 void Model::FlipTexCoords()
673 for (unsigned int i = 0; i < Triangles.size(); i++) {
674 Triangles[i].gy[0] = -Triangles[i].gy[0];
675 Triangles[i].gy[1] = -Triangles[i].gy[1];
676 Triangles[i].gy[2] = -Triangles[i].gy[2];
681 void Model::ScaleTexCoords(float howmuch)
683 for (unsigned int i = 0; i < Triangles.size(); i++) {
684 Triangles[i].gx[0] *= howmuch;
685 Triangles[i].gx[1] *= howmuch;
686 Triangles[i].gx[2] *= howmuch;
687 Triangles[i].gy[0] *= howmuch;
688 Triangles[i].gy[1] *= howmuch;
689 Triangles[i].gy[2] *= howmuch;
694 void Model::Scale(float xscale, float yscale, float zscale)
697 for (i = 0; i < vertexNum; i++) {
698 vertex[i].x *= xscale;
699 vertex[i].y *= yscale;
700 vertex[i].z *= zscale;
706 boundingsphereradius = 0;
707 for (i = 0; i < vertexNum; i++) {
708 for (j = 0; j < vertexNum; j++) {
709 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
710 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
711 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
715 boundingsphereradius = fast_sqrt(boundingsphereradius);
718 void Model::ScaleNormals(float xscale, float yscale, float zscale)
720 if (type != normaltype && type != decalstype)
723 for (int i = 0; i < vertexNum; i++) {
724 normals[i].x *= xscale;
725 normals[i].y *= yscale;
726 normals[i].z *= zscale;
728 for (unsigned int i = 0; i < Triangles.size(); i++) {
729 Triangles[i].facenormal.x *= xscale;
730 Triangles[i].facenormal.y *= yscale;
731 Triangles[i].facenormal.z *= zscale;
736 void Model::Translate(float xtrans, float ytrans, float ztrans)
739 for (i = 0; i < vertexNum; i++) {
740 vertex[i].x += xtrans;
741 vertex[i].y += ytrans;
742 vertex[i].z += ztrans;
747 boundingsphereradius = 0;
748 for (i = 0; i < vertexNum; i++) {
749 for (j = 0; j < vertexNum; j++) {
750 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
751 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
752 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
756 boundingsphereradius = fast_sqrt(boundingsphereradius);
759 void Model::Rotate(float xang, float yang, float zang)
762 for (i = 0; i < vertexNum; i++) {
763 vertex[i] = DoRotation(vertex[i], xang, yang, zang);
768 boundingsphereradius = 0;
769 for (i = 0; i < vertexNum; i++) {
770 for (j = 0; j < vertexNum; j++) {
771 if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
772 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
773 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
777 boundingsphereradius = fast_sqrt(boundingsphereradius);
781 void Model::CalculateNormals(bool facenormalise)
783 Game::LoadingScreen();
785 if (type != normaltype && type != decalstype)
788 for (int i = 0; i < vertexNum; i++) {
794 for (unsigned int i = 0; i < Triangles.size(); i++) {
795 CrossProduct(vertex[Triangles[i].vertex[1]] - vertex[Triangles[i].vertex[0]], vertex[Triangles[i].vertex[2]] - vertex[Triangles[i].vertex[0]], &Triangles[i].facenormal);
797 normals[Triangles[i].vertex[0]].x += Triangles[i].facenormal.x;
798 normals[Triangles[i].vertex[0]].y += Triangles[i].facenormal.y;
799 normals[Triangles[i].vertex[0]].z += Triangles[i].facenormal.z;
801 normals[Triangles[i].vertex[1]].x += Triangles[i].facenormal.x;
802 normals[Triangles[i].vertex[1]].y += Triangles[i].facenormal.y;
803 normals[Triangles[i].vertex[1]].z += Triangles[i].facenormal.z;
805 normals[Triangles[i].vertex[2]].x += Triangles[i].facenormal.x;
806 normals[Triangles[i].vertex[2]].y += Triangles[i].facenormal.y;
807 normals[Triangles[i].vertex[2]].z += Triangles[i].facenormal.z;
809 Normalise(&Triangles[i].facenormal);
811 for (int i = 0; i < vertexNum; i++) {
812 Normalise(&normals[i]);
815 UpdateVertexArrayNoTex();
818 void Model::drawimmediate()
821 glBegin(GL_TRIANGLES);
822 for (unsigned int i = 0; i < Triangles.size(); i++) {
823 glTexCoord2f(Triangles[i].gx[0], Triangles[i].gy[0]);
825 glColor3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
827 glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
829 glNormal3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
831 glVertex3f(vertex[Triangles[i].vertex[0]].x, vertex[Triangles[i].vertex[0]].y, vertex[Triangles[i].vertex[0]].z);
833 glTexCoord2f(Triangles[i].gx[1], Triangles[i].gy[1]);
835 glColor3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
837 glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
839 glNormal3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
841 glVertex3f(vertex[Triangles[i].vertex[1]].x, vertex[Triangles[i].vertex[1]].y, vertex[Triangles[i].vertex[1]].z);
843 glTexCoord2f(Triangles[i].gx[2], Triangles[i].gy[2]);
845 glColor3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
847 glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
849 glNormal3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
851 glVertex3f(vertex[Triangles[i].vertex[2]].x, vertex[Triangles[i].vertex[2]].y, vertex[Triangles[i].vertex[2]].z);
858 if (type != normaltype && type != decalstype)
861 glEnableClientState(GL_NORMAL_ARRAY);
862 glEnableClientState(GL_VERTEX_ARRAY);
863 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
866 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
868 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
872 glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
875 glDisableClientState(GL_COLOR_ARRAY);
877 glDisableClientState(GL_NORMAL_ARRAY);
879 glDisableClientState(GL_VERTEX_ARRAY);
880 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
883 void Model::drawdifftex(Texture texture)
885 glEnableClientState(GL_NORMAL_ARRAY);
886 glEnableClientState(GL_VERTEX_ARRAY);
887 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
889 glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
891 glInterleavedArrays( GL_T2F_N3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
895 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
896 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
898 glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
901 glDisableClientState(GL_COLOR_ARRAY);
903 glDisableClientState(GL_NORMAL_ARRAY);
905 glDisableClientState(GL_VERTEX_ARRAY);
906 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
909 void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloodtexture2, Texture breaktexture)
912 if (type != decalstype)
921 glDisable(GL_LIGHTING);
922 glDisable(GL_CULL_FACE);
923 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
925 for (unsigned int i = 0; i < decals.size(); i++) {
926 if (decals[i].type == blooddecalfast && decals[i].alivetime < 2)
927 decals[i].alivetime = 2;
929 if (decals[i].type != lasttype) {
930 if (decals[i].type == shadowdecal) {
931 shadowtexture.bind();
934 glAlphaFunc(GL_GREATER, 0.0001);
935 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
938 if (decals[i].type == breakdecal) {
942 glAlphaFunc(GL_GREATER, 0.0001);
943 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
946 if (decals[i].type == blooddecal || decals[i].type == blooddecalslow) {
950 glAlphaFunc(GL_GREATER, 0.15);
951 glBlendFunc(GL_ONE, GL_ZERO);
954 if (decals[i].type == blooddecalfast) {
955 bloodtexture2.bind();
958 glAlphaFunc(GL_GREATER, 0.15);
959 glBlendFunc(GL_ONE, GL_ZERO);
963 if (decals[i].type == shadowdecal) {
964 glColor4f(1, 1, 1, decals[i].opacity);
966 if (decals[i].type == breakdecal) {
967 glColor4f(1, 1, 1, decals[i].opacity);
968 if (decals[i].alivetime > 58)
969 glColor4f(1, 1, 1, decals[i].opacity * (60 - decals[i].alivetime) / 2);
971 if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow)) {
972 glColor4f(1, 1, 1, decals[i].opacity);
973 if (decals[i].alivetime < 4)
974 glColor4f(1, 1, 1, decals[i].opacity*decals[i].alivetime*.25);
975 if (decals[i].alivetime > 58)
976 glColor4f(1, 1, 1, decals[i].opacity * (60 - decals[i].alivetime) / 2);
978 lasttype = decals[i].type;
979 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
980 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
982 glMatrixMode(GL_MODELVIEW);
984 glBegin(GL_TRIANGLES);
985 for (int j = 0; j < 3; j++) {
986 glTexCoord2f(decals[i].texcoords[j][0], decals[i].texcoords[j][1]);
987 glVertex3f(decals[i].vertex[j].x, decals[i].vertex[j].y, decals[i].vertex[j].z);
992 for (int i = decals.size() - 1; i >= 0; i--) {
993 decals[i].alivetime += multiplier;
994 if (decals[i].type == blooddecalslow)
995 decals[i].alivetime -= multiplier * 2 / 3;
996 if (decals[i].type == blooddecalfast)
997 decals[i].alivetime += multiplier * 4;
998 if (decals[i].type == shadowdecal)
1000 if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow) && decals[i].alivetime >= 60)
1003 glAlphaFunc(GL_GREATER, 0.0001);
1004 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1008 void Model::DeleteDecal(int which)
1011 if (type != decalstype)
1013 decals.erase(decals.begin() + which);
1017 void Model::MakeDecal(decal_type atype, XYZ *where, float *size, float *opacity, float *rotation)
1020 if (type != decalstype)
1024 static float distance;
1027 if (distsq(where, &boundingspherecenter) < (boundingsphereradius + *size) * (boundingsphereradius + *size))
1028 for (unsigned int i = 0; i < Triangles.size(); i++) {
1029 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)) {
1030 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);
1032 if (*opacity - distance / 10 > 0) {
1033 Decal decal(*where, atype, *opacity - distance / 10, *rotation, *size, *this, i, 0);
1035 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1036 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1037 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1038 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1039 if (decal.rotation) {
1040 for (int j = 0; j < 3; j++) {
1042 rot.x = decal.texcoords[j][0] - .5;
1043 rot.z = decal.texcoords[j][1] - .5;
1044 rot = DoRotation(rot, 0, -decal.rotation, 0);
1045 decal.texcoords[j][0] = rot.x + .5;
1046 decal.texcoords[j][1] = rot.z + .5;
1049 if (decals.size() < max_model_decals - 1) {
1050 decals.push_back(decal);
1059 void Model::MakeDecal(decal_type atype, XYZ where, float size, float opacity, float rotation)
1062 if (type != decalstype)
1066 static float distance;
1069 if (distsq(&where, &boundingspherecenter) < (boundingsphereradius + size) * (boundingsphereradius + size))
1070 for (unsigned int i = 0; i < Triangles.size(); i++) {
1071 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))));
1072 if (distance < .02 && abs(Triangles[i].facenormal.y) > abs(Triangles[i].facenormal.x) && abs(Triangles[i].facenormal.y) > abs(Triangles[i].facenormal.z)) {
1073 if (opacity - distance / 10 > 0) {
1074 Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 0);
1076 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1077 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1078 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1079 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1080 if (decal.rotation) {
1081 for (int j = 0; j < 3; j++) {
1083 rot.x = decal.texcoords[j][0] - .5;
1084 rot.z = decal.texcoords[j][1] - .5;
1085 rot = DoRotation(rot, 0, -decal.rotation, 0);
1086 decal.texcoords[j][0] = rot.x + .5;
1087 decal.texcoords[j][1] = rot.z + .5;
1090 if (decals.size() < max_model_decals - 1) {
1091 decals.push_back(decal);
1095 } else if (distance < .02 && abs(Triangles[i].facenormal.x) > abs(Triangles[i].facenormal.y) && abs(Triangles[i].facenormal.x) > abs(Triangles[i].facenormal.z)) {
1096 if (opacity - distance / 10 > 0) {
1097 Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 1);
1099 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1100 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1101 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1102 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1103 if (decal.rotation) {
1104 for (int j = 0; j < 3; j++) {
1106 rot.x = decal.texcoords[j][0] - .5;
1107 rot.z = decal.texcoords[j][1] - .5;
1108 rot = DoRotation(rot, 0, -decal.rotation, 0);
1109 decal.texcoords[j][0] = rot.x + .5;
1110 decal.texcoords[j][1] = rot.z + .5;
1113 if (decals.size() < max_model_decals - 1) {
1114 decals.push_back(decal);
1118 } else if (distance < .02 && abs(Triangles[i].facenormal.z) > abs(Triangles[i].facenormal.y) && abs(Triangles[i].facenormal.z) > abs(Triangles[i].facenormal.x)) {
1119 if (opacity - distance / 10 > 0) {
1120 Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 2);
1122 if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0))
1123 if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0))
1124 if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1))
1125 if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) {
1126 if (decal.rotation) {
1127 for (int j = 0; j < 3; j++) {
1129 rot.x = decal.texcoords[j][0] - .5;
1130 rot.z = decal.texcoords[j][1] - .5;
1131 rot = DoRotation(rot, 0, -decal.rotation, 0);
1132 decal.texcoords[j][0] = rot.x + .5;
1133 decal.texcoords[j][1] = rot.z + .5;
1136 if (decals.size() < max_model_decals - 1) {
1137 decals.push_back(decal);
1146 void Model::deleteDeadDecals()
1148 for (int i = decals.size() - 1; i >= 0; i--) {
1149 if ((decals[i].type == blooddecal || decals[i].type == blooddecalslow) && decals[i].alivetime < 2) {
1160 void Model::deallocate()
1187 boundingspherecenter(),
1188 boundingsphereradius(0),
1191 memset(&modelTexture, 0, sizeof(modelTexture));