]> git.jsancho.org Git - lugaru.git/commitdiff
Using a vector for Model triangles, moved facenormal inside triangles
authorCôme Chilliet <come@chilliet.eu>
Fri, 23 Dec 2016 01:46:01 +0000 (02:46 +0100)
committerCôme Chilliet <come@chilliet.eu>
Fri, 23 Dec 2016 01:46:01 +0000 (02:46 +0100)
Source/Animation/Skeleton.cpp
Source/GameTick.cpp
Source/Graphic/Models.cpp
Source/Graphic/Models.hpp
Source/Objects/Object.cpp
Source/Objects/Person.cpp
Source/Objects/Weapons.cpp

index 8e5cb0c9a8e7336350703c1077fb983465f9c949..1c882558a5a9504d5a76f7bf428efedd55542234 100644 (file)
@@ -348,7 +348,7 @@ float Skeleton::DoConstraints(XYZ *coords, float *scale)
                                         }
                                     }
 
-                                    terrainnormal = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * -1;
+                                    terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
                                     if (terrainnormal.y > .8)
                                         freefall = 0;
                                     bounceness = terrainnormal * findLength(&joints[i].velocity) * (abs(normaldotproduct(joints[i].velocity, terrainnormal)));
index 25a6fb0556ed6b3a71a96e8b1182964f01539984..1f971e70f8f7869b67d0232493d7e1e6b9b74094 100644 (file)
@@ -1775,13 +1775,13 @@ void doAerialAcrobatics()
                             lowpointtarget = lowpoint + DoRotation(Person::players[k]->facing, 0, -90, 0) * 1.5;
                             XYZ tempcoords1 = lowpoint;
                             whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
-                            if (whichhit != -1 && fabs(Object::objects[i]->model.facenormals[whichhit].y) < .3) {
+                            if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
                                 Person::players[k]->setTargetAnimation(walljumpleftanim);
                                 emit_sound_at(movewhooshsound, Person::players[k]->coords);
                                 if (k == 0)
                                     pause_sound(whooshsound);
 
-                                lowpointtarget = DoRotation(Object::objects[i]->model.facenormals[whichhit], 0, Object::objects[i]->yaw, 0);
+                                lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
                                 Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
                                 if (lowpointtarget.z < 0)
                                     Person::players[k]->yaw = 180 - Person::players[k]->yaw;
@@ -1793,13 +1793,13 @@ void doAerialAcrobatics()
                                 lowpoint = tempcoords1;
                                 lowpointtarget = lowpoint + DoRotation(Person::players[k]->facing, 0, 90, 0) * 1.5;
                                 whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
-                                if (whichhit != -1 && fabs(Object::objects[i]->model.facenormals[whichhit].y) < .3) {
+                                if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
                                     Person::players[k]->setTargetAnimation(walljumprightanim);
                                     emit_sound_at(movewhooshsound, Person::players[k]->coords);
                                     if (k == 0)
                                         pause_sound(whooshsound);
 
-                                    lowpointtarget = DoRotation(Object::objects[i]->model.facenormals[whichhit], 0, Object::objects[i]->yaw, 0);
+                                    lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
                                     Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
                                     if (lowpointtarget.z < 0)
                                         Person::players[k]->yaw = 180 - Person::players[k]->yaw;
@@ -1811,13 +1811,13 @@ void doAerialAcrobatics()
                                     lowpoint = tempcoords1;
                                     lowpointtarget = lowpoint + Person::players[k]->facing * 2;
                                     whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
-                                    if (whichhit != -1 && fabs(Object::objects[i]->model.facenormals[whichhit].y) < .3) {
+                                    if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
                                         Person::players[k]->setTargetAnimation(walljumpbackanim);
                                         emit_sound_at(movewhooshsound, Person::players[k]->coords);
                                         if (k == 0)
                                             pause_sound(whooshsound);
 
-                                        lowpointtarget = DoRotation(Object::objects[i]->model.facenormals[whichhit], 0, Object::objects[i]->yaw, 0);
+                                        lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
                                         Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
                                         if (lowpointtarget.z < 0)
                                             Person::players[k]->yaw = 180 - Person::players[k]->yaw;
@@ -1829,13 +1829,13 @@ void doAerialAcrobatics()
                                         lowpoint = tempcoords1;
                                         lowpointtarget = lowpoint - Person::players[k]->facing * 2;
                                         whichhit = Object::objects[i]->model.LineCheck(&lowpoint, &lowpointtarget, &colpoint, &Object::objects[i]->position, &Object::objects[i]->yaw);
-                                        if (whichhit != -1 && fabs(Object::objects[i]->model.facenormals[whichhit].y) < .3) {
+                                        if (whichhit != -1 && fabs(Object::objects[i]->model.Triangles[whichhit].facenormal.y) < .3) {
                                             Person::players[k]->setTargetAnimation(walljumpfrontanim);
                                             emit_sound_at(movewhooshsound, Person::players[k]->coords);
                                             if (k == 0)
                                                 pause_sound(whooshsound);
 
-                                            lowpointtarget = DoRotation(Object::objects[i]->model.facenormals[whichhit], 0, Object::objects[i]->yaw, 0);
+                                            lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
                                             Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
                                             if (lowpointtarget.z < 0)
                                                 Person::players[k]->yaw = 180 - Person::players[k]->yaw;
@@ -1978,7 +1978,7 @@ void doAerialAcrobatics()
                                                                     lowpoint.y += (float)j / 13;
                                                                     lowpointtarget = lowpoint + facing * 1.3;
                                                                     flatfacing = Person::players[k]->coords;
-                                                                    Person::players[k]->coords = colpoint - DoRotation(Object::objects[i]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * .01;
+                                                                    Person::players[k]->coords = colpoint - DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * .01;
                                                                     Person::players[k]->coords.y = lowpointtarget.y - .07;
                                                                     Person::players[k]->currentoffset = (flatfacing - Person::players[k]->coords) / Person::players[k]->scale;
 
@@ -1989,7 +1989,7 @@ void doAerialAcrobatics()
                                                                         }
                                                                         emit_sound_at(jumpsound, Person::players[k]->coords, 128.);
 
-                                                                        lowpointtarget = DoRotation(Object::objects[i]->model.facenormals[whichhit], 0, Object::objects[i]->yaw, 0);
+                                                                        lowpointtarget = DoRotation(Object::objects[i]->model.Triangles[whichhit].facenormal, 0, Object::objects[i]->yaw, 0);
                                                                         Person::players[k]->yaw = -asin(0 - lowpointtarget.x) * 180 / M_PI;
                                                                         if (lowpointtarget.z < 0)
                                                                             Person::players[k]->yaw = 180 - Person::players[k]->yaw;
index ef2b7bba114b29d27065cf105b0e72955a5e64c6..9cb88673f7a6235f2a976bcb16a675d3ba3e7ffd 100644 (file)
@@ -32,7 +32,6 @@ extern bool decalstoggle;
 
 int Model::LineCheck(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
 {
-    static int j;
     static float distance;
     static float olddistance;
     static int intersecting;
@@ -49,8 +48,8 @@ int Model::LineCheck(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
         return -1;
     firstintersecting = -1;
 
-    for (j = 0; j < TriangleNum; j++) {
-        intersecting = LineFacetd(p1, p2, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], &facenormals[j], &point);
+    for (unsigned int j = 0; j < Triangles.size(); j++) {
+        intersecting = LineFacetd(p1, p2, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]], &Triangles[j].facenormal, &point);
         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);
         if ((distance < olddistance || firstintersecting == -1) && intersecting) {
             olddistance = distance;
@@ -67,7 +66,6 @@ int Model::LineCheck(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
 
 int Model::LineCheckPossible(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
 {
-    static int j;
     static float distance;
     static float olddistance;
     static int intersecting;
@@ -84,10 +82,10 @@ int Model::LineCheckPossible(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
     if (*rotate)
         *p2 = DoRotation(*p2, 0, -*rotate, 0);
 
-    if (numpossible > 0 && numpossible < TriangleNum)
-        for (j = 0; j < numpossible; j++) {
-            if (possible[j] >= 0 && possible[j] < TriangleNum) {
-                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);
+    if (numpossible > 0 && numpossible < int(Triangles.size())) {
+        for (unsigned int j = 0; int(j) < numpossible; j++) {
+            if ((possible[j] >= 0) && possible[j] < int(Triangles.size())) {
+                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);
                 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);
                 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
                     olddistance = distance;
@@ -96,6 +94,7 @@ int Model::LineCheckPossible(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
                 }
             }
         }
+    }
 
     if (*rotate)
         *p = DoRotation(*p, 0, *rotate, 0);
@@ -124,8 +123,8 @@ int Model::LineCheckSlidePossible(XYZ *p1, XYZ *p2, XYZ *move, float *rotate)
 
     if (numpossible)
         for (j = 0; j < numpossible; j++) {
-            if (possible[j] >= 0 && possible[j] < TriangleNum) {
-                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);
+            if (possible[j] >= 0 && possible[j] < int(Triangles.size())) {
+                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);
                 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);
                 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
                     olddistance = distance;
@@ -135,8 +134,8 @@ int Model::LineCheckSlidePossible(XYZ *p1, XYZ *p2, XYZ *move, float *rotate)
         }
 
     if (firstintersecting > 0) {
-        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)));
-        *p2 -= facenormals[firstintersecting] * distance;
+        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)));
+        *p2 -= Triangles[firstintersecting].facenormal * distance;
     }
 
     if (*rotate)
@@ -147,7 +146,7 @@ int Model::LineCheckSlidePossible(XYZ *p1, XYZ *p2, XYZ *move, float *rotate)
 
 int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
 {
-    static int i, j;
+    static int i;
     static float distance;
     static float olddistance;
     static int intersecting;
@@ -165,12 +164,12 @@ int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
         return -1;
 
     for (i = 0; i < 4; i++) {
-        for (j = 0; j < TriangleNum; j++) {
+        for (unsigned int j = 0; j < Triangles.size(); j++) {
             intersecting = 0;
-            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)));
+            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)));
             if (distance < radius) {
-                point = *p1 - facenormals[j] * distance;
-                if (PointInTriangle( &point, facenormals[j], &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
+                point = *p1 - Triangles[j].facenormal * distance;
+                if (PointInTriangle( &point, Triangles[j].facenormal, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
                     intersecting = 1;
                 if (!intersecting)
                     intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], p1, &radius);
@@ -179,7 +178,7 @@ int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
                 if (!intersecting)
                     intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
                 if (intersecting) {
-                    *p1 += facenormals[j] * (distance - radius);
+                    *p1 += Triangles[j].facenormal * (distance - radius);
                 }
             }
             if ((distance < olddistance || firstintersecting == -1) && intersecting) {
@@ -200,7 +199,6 @@ int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
 
 int Model::SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate)
 {
-    static int j;
     static float distance;
     static float olddistance;
     static int intersecting;
@@ -222,12 +220,12 @@ int Model::SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate)
         return -1;
     }
 
-    for (j = 0; j < TriangleNum; j++) {
+    for (unsigned int j = 0; j < Triangles.size(); j++) {
         intersecting = 0;
-        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)));
+        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)));
         if (distance < radius) {
-            point = *p1 - facenormals[j] * distance;
-            if (PointInTriangle( &point, facenormals[j], &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
+            point = *p1 - Triangles[j].facenormal * distance;
+            if (PointInTriangle( &point, Triangles[j].facenormal, &vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], &vertex[Triangles[j].vertex[2]]))
                 intersecting = 1;
             if (!intersecting)
                 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[1]], p1, &radius);
@@ -256,135 +254,133 @@ void Model::UpdateVertexArray()
 {
     if (type != normaltype && type != decalstype)
         return;
-    static int i;
-    static int j;
-    if (!flat)
-        for (i = 0; i < TriangleNum; i++) {
-            j = i * 24;
+
+    if (flat) {
+        for (unsigned int i = 0; i < Triangles.size(); i++) {
+            unsigned int j = i * 24;
             vArray[j + 0] = Triangles[i].gx[0];
             vArray[j + 1] = Triangles[i].gy[0];
-            vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
-            vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
-            vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
+            vArray[j + 2] = Triangles[i].facenormal.x * -1;
+            vArray[j + 3] = Triangles[i].facenormal.y * -1;
+            vArray[j + 4] = Triangles[i].facenormal.z * -1;
             vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
             vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
             vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
 
             vArray[j + 8] = Triangles[i].gx[1];
             vArray[j + 9] = Triangles[i].gy[1];
-            vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
-            vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
-            vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
+            vArray[j + 10] = Triangles[i].facenormal.x * -1;
+            vArray[j + 11] = Triangles[i].facenormal.y * -1;
+            vArray[j + 12] = Triangles[i].facenormal.z * -1;
             vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
             vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
             vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
 
             vArray[j + 16] = Triangles[i].gx[2];
             vArray[j + 17] = Triangles[i].gy[2];
-            vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
-            vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
-            vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
+            vArray[j + 18] = Triangles[i].facenormal.x * -1;
+            vArray[j + 19] = Triangles[i].facenormal.y * -1;
+            vArray[j + 20] = Triangles[i].facenormal.z * -1;
             vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
             vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
             vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
         }
-    if (flat)
-        for (i = 0; i < TriangleNum; i++) {
-            j = i * 24;
+    } else {
+        for (unsigned int i = 0; i < Triangles.size(); i++) {
+            unsigned int j = i * 24;
             vArray[j + 0] = Triangles[i].gx[0];
             vArray[j + 1] = Triangles[i].gy[0];
-            vArray[j + 2] = facenormals[i].x * -1;
-            vArray[j + 3] = facenormals[i].y * -1;
-            vArray[j + 4] = facenormals[i].z * -1;
+            vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
+            vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
+            vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
             vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
             vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
             vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
 
             vArray[j + 8] = Triangles[i].gx[1];
             vArray[j + 9] = Triangles[i].gy[1];
-            vArray[j + 10] = facenormals[i].x * -1;
-            vArray[j + 11] = facenormals[i].y * -1;
-            vArray[j + 12] = facenormals[i].z * -1;
+            vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
+            vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
+            vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
             vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
             vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
             vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
 
             vArray[j + 16] = Triangles[i].gx[2];
             vArray[j + 17] = Triangles[i].gy[2];
-            vArray[j + 18] = facenormals[i].x * -1;
-            vArray[j + 19] = facenormals[i].y * -1;
-            vArray[j + 20] = facenormals[i].z * -1;
+            vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
+            vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
+            vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
             vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
             vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
             vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
-
         }
+    }
 }
 
 void Model::UpdateVertexArrayNoTex()
 {
     if (type != normaltype && type != decalstype)
         return;
-    static int i;
-    static int j;
-    if (!flat)
-        for (i = 0; i < TriangleNum; i++) {
-            j = i * 24;
-            vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
-            vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
-            vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
+
+    if (flat) {
+        for (unsigned int i = 0; i < Triangles.size(); i++) {
+            unsigned int j = i * 24;
+            vArray[j + 2] = Triangles[i].facenormal.x * -1;
+            vArray[j + 3] = Triangles[i].facenormal.y * -1;
+            vArray[j + 4] = Triangles[i].facenormal.z * -1;
             vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
             vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
             vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
 
-            vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
-            vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
-            vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
+            vArray[j + 10] = Triangles[i].facenormal.x * -1;
+            vArray[j + 11] = Triangles[i].facenormal.y * -1;
+            vArray[j + 12] = Triangles[i].facenormal.z * -1;
             vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
             vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
             vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
 
-            vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
-            vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
-            vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
+            vArray[j + 18] = Triangles[i].facenormal.x * -1;
+            vArray[j + 19] = Triangles[i].facenormal.y * -1;
+            vArray[j + 20] = Triangles[i].facenormal.z * -1;
             vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
             vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
             vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
         }
-    if (flat)
-        for (i = 0; i < TriangleNum; i++) {
-            j = i * 24;
-            vArray[j + 2] = facenormals[i].x * -1;
-            vArray[j + 3] = facenormals[i].y * -1;
-            vArray[j + 4] = facenormals[i].z * -1;
+    } else {
+        for (unsigned int i = 0; i < Triangles.size(); i++) {
+            unsigned int j = i * 24;
+            vArray[j + 2] = normals[Triangles[i].vertex[0]].x;
+            vArray[j + 3] = normals[Triangles[i].vertex[0]].y;
+            vArray[j + 4] = normals[Triangles[i].vertex[0]].z;
             vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
             vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
             vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
 
-            vArray[j + 10] = facenormals[i].x * -1;
-            vArray[j + 11] = facenormals[i].y * -1;
-            vArray[j + 12] = facenormals[i].z * -1;
+            vArray[j + 10] = normals[Triangles[i].vertex[1]].x;
+            vArray[j + 11] = normals[Triangles[i].vertex[1]].y;
+            vArray[j + 12] = normals[Triangles[i].vertex[1]].z;
             vArray[j + 13] = vertex[Triangles[i].vertex[1]].x;
             vArray[j + 14] = vertex[Triangles[i].vertex[1]].y;
             vArray[j + 15] = vertex[Triangles[i].vertex[1]].z;
 
-            vArray[j + 18] = facenormals[i].x * -1;
-            vArray[j + 19] = facenormals[i].y * -1;
-            vArray[j + 20] = facenormals[i].z * -1;
+            vArray[j + 18] = normals[Triangles[i].vertex[2]].x;
+            vArray[j + 19] = normals[Triangles[i].vertex[2]].y;
+            vArray[j + 20] = normals[Triangles[i].vertex[2]].z;
             vArray[j + 21] = vertex[Triangles[i].vertex[2]].x;
             vArray[j + 22] = vertex[Triangles[i].vertex[2]].y;
             vArray[j + 23] = vertex[Triangles[i].vertex[2]].z;
         }
+    }
 }
 
 void Model::UpdateVertexArrayNoTexNoNorm()
 {
     if (type != normaltype && type != decalstype)
         return;
-    static int i;
-    static int j;
-    for (i = 0; i < TriangleNum; i++) {
-        j = i * 24;
+
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
+        unsigned int j = i * 24;
         vArray[j + 5] = vertex[Triangles[i].vertex[0]].x;
         vArray[j + 6] = vertex[Triangles[i].vertex[0]].y;
         vArray[j + 7] = vertex[Triangles[i].vertex[0]].z;
@@ -403,6 +399,7 @@ bool Model::loadnotex(const std::string& filename)
 {
     FILE *tfile;
     long i;
+    int TriangleNum;
 
     type = notextype;
     color = 0;
@@ -422,7 +419,7 @@ bool Model::loadnotex(const std::string& filename)
     owner = (int*)malloc(sizeof(int) * vertexNum);
     possible = (int*)malloc(sizeof(int) * TriangleNum);
     vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
-    Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
+    Triangles.resize(TriangleNum);
     vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
 
     for (i = 0; i < vertexNum; i++) {
@@ -430,11 +427,11 @@ bool Model::loadnotex(const std::string& filename)
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        short vertex[ 6];
-        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
-        Triangles[i].vertex[ 0] = vertex[ 0];
-        Triangles[i].vertex[ 1] = vertex[ 2];
-        Triangles[i].vertex[ 2] = vertex[ 4];
+        short vertex[6];
+        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
+        Triangles[i].vertex[0] = vertex[0];
+        Triangles[i].vertex[1] = vertex[2];
+        Triangles[i].vertex[2] = vertex[4];
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
     }
@@ -447,10 +444,9 @@ bool Model::loadnotex(const std::string& filename)
         owner[i] = -1;
     }
 
-    static int j;
     boundingsphereradius = 0;
     for (i = 0; i < vertexNum; i++) {
-        for (j = 0; j < vertexNum; j++) {
+        for (int j = 0; j < vertexNum; j++) {
             if (j != i && distsq(&vertex[j], &vertex[i]) / 2 > boundingsphereradius) {
                 boundingsphereradius = distsq(&vertex[j], &vertex[i]) / 2;
                 boundingspherecenter = (vertex[i] + vertex[j]) / 2;
@@ -467,6 +463,7 @@ bool Model::load(const std::string& filename)
 {
     FILE *tfile;
     long i;
+    int TriangleNum;
 
     LOGFUNC;
 
@@ -493,8 +490,7 @@ bool Model::load(const std::string& filename)
     possible = (int*)malloc(sizeof(int) * TriangleNum);
     vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
     normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
-    facenormals = (XYZ*)malloc(sizeof(XYZ) * TriangleNum);
-    Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
+    Triangles.resize(TriangleNum);
     vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
 
     for (i = 0; i < vertexNum; i++) {
@@ -502,11 +498,11 @@ bool Model::load(const std::string& filename)
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        short vertex[ 6];
-        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
-        Triangles[i].vertex[ 0] = vertex[ 0];
-        Triangles[i].vertex[ 1] = vertex[ 2];
-        Triangles[i].vertex[ 2] = vertex[ 4];
+        short vertex[6];
+        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
+        Triangles[i].vertex[0] = vertex[0];
+        Triangles[i].vertex[1] = vertex[2];
+        Triangles[i].vertex[2] = vertex[4];
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
     }
@@ -540,6 +536,7 @@ bool Model::loaddecal(const std::string& filename)
 {
     FILE *tfile;
     long i, j;
+    int TriangleNum;
 
     LOGFUNC;
 
@@ -566,21 +563,19 @@ bool Model::loaddecal(const std::string& filename)
     possible = (int*)malloc(sizeof(int) * TriangleNum);
     vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
     normals = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
-    facenormals = (XYZ*)malloc(sizeof(XYZ) * TriangleNum);
-    Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
+    Triangles.resize(TriangleNum);
     vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
 
-
     for (i = 0; i < vertexNum; i++) {
         funpackf(tfile, "Bf Bf Bf", &vertex[i].x, &vertex[i].y, &vertex[i].z);
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        short vertex[ 6];
-        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
-        Triangles[i].vertex[ 0] = vertex[ 0];
-        Triangles[i].vertex[ 1] = vertex[ 2];
-        Triangles[i].vertex[ 2] = vertex[ 4];
+        short vertex[6];
+        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
+        Triangles[i].vertex[0] = vertex[0];
+        Triangles[i].vertex[1] = vertex[2];
+        Triangles[i].vertex[2] = vertex[4];
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
     }
@@ -614,6 +609,7 @@ bool Model::loadraw(const std::string& filename)
 {
     FILE *tfile;
     long i;
+    int TriangleNum;
 
     LOGFUNC;
 
@@ -637,7 +633,7 @@ bool Model::loadraw(const std::string& filename)
     owner = (int*)malloc(sizeof(int) * vertexNum);
     possible = (int*)malloc(sizeof(int) * TriangleNum);
     vertex = (XYZ*)malloc(sizeof(XYZ) * vertexNum);
-    Triangles = (TexturedTriangle*)malloc(sizeof(TexturedTriangle) * TriangleNum);
+    Triangles.resize(TriangleNum);
     vArray = (GLfloat*)malloc(sizeof(GLfloat) * TriangleNum * 24);
 
 
@@ -646,11 +642,11 @@ bool Model::loadraw(const std::string& filename)
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        short vertex[ 6];
-        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[ 0], &vertex[ 1], &vertex[ 2], &vertex[ 3], &vertex[ 4], &vertex[ 5]);
-        Triangles[i].vertex[ 0] = vertex[ 0];
-        Triangles[i].vertex[ 1] = vertex[ 2];
-        Triangles[i].vertex[ 2] = vertex[ 4];
+        short vertex[6];
+        funpackf(tfile, "Bs Bs Bs Bs Bs Bs", &vertex[0], &vertex[1], &vertex[2], &vertex[3], &vertex[4], &vertex[5]);
+        Triangles[i].vertex[0] = vertex[0];
+        Triangles[i].vertex[1] = vertex[2];
+        Triangles[i].vertex[2] = vertex[4];
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gx[0], &Triangles[i].gx[1], &Triangles[i].gx[2]);
         funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
     }
@@ -668,8 +664,7 @@ bool Model::loadraw(const std::string& filename)
 
 void Model::UniformTexCoords()
 {
-    static int i;
-    for (i = 0; i < TriangleNum; i++) {
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
         Triangles[i].gy[0] = vertex[Triangles[i].vertex[0]].y;
         Triangles[i].gy[1] = vertex[Triangles[i].vertex[1]].y;
         Triangles[i].gy[2] = vertex[Triangles[i].vertex[2]].y;
@@ -683,8 +678,7 @@ void Model::UniformTexCoords()
 
 void Model::FlipTexCoords()
 {
-    static int i;
-    for (i = 0; i < TriangleNum; i++) {
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
         Triangles[i].gy[0] = -Triangles[i].gy[0];
         Triangles[i].gy[1] = -Triangles[i].gy[1];
         Triangles[i].gy[2] = -Triangles[i].gy[2];
@@ -694,8 +688,7 @@ void Model::FlipTexCoords()
 
 void Model::ScaleTexCoords(float howmuch)
 {
-    static int i;
-    for (i = 0; i < TriangleNum; i++) {
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
         Triangles[i].gx[0] *= howmuch;
         Triangles[i].gx[1] *= howmuch;
         Triangles[i].gx[2] *= howmuch;
@@ -734,16 +727,16 @@ void Model::ScaleNormals(float xscale, float yscale, float zscale)
 {
     if (type != normaltype && type != decalstype)
         return;
-    static int i;
-    for (i = 0; i < vertexNum; i++) {
+
+    for (int i = 0; i < vertexNum; i++) {
         normals[i].x *= xscale;
         normals[i].y *= yscale;
         normals[i].z *= zscale;
     }
-    for (i = 0; i < TriangleNum; i++) {
-        facenormals[i].x *= xscale;
-        facenormals[i].y *= yscale;
-        facenormals[i].z *= zscale;
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
+        Triangles[i].facenormal.x *= xscale;
+        Triangles[i].facenormal.y *= yscale;
+        Triangles[i].facenormal.z *= zscale;
     }
     UpdateVertexArray();
 }
@@ -796,34 +789,34 @@ void Model::Rotate(float xang, float yang, float zang)
 void Model::CalculateNormals(bool facenormalise)
 {
     Game::LoadingScreen();
-    static int i;
+
     if (type != normaltype && type != decalstype)
         return;
 
-    for (i = 0; i < vertexNum; i++) {
+    for (int i = 0; i < vertexNum; i++) {
         normals[i].x = 0;
         normals[i].y = 0;
         normals[i].z = 0;
     }
 
-    for (i = 0; i < TriangleNum; i++) {
-        CrossProduct(vertex[Triangles[i].vertex[1]] - vertex[Triangles[i].vertex[0]], vertex[Triangles[i].vertex[2]] - vertex[Triangles[i].vertex[0]], &facenormals[i]);
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
+        CrossProduct(vertex[Triangles[i].vertex[1]] - vertex[Triangles[i].vertex[0]], vertex[Triangles[i].vertex[2]] - vertex[Triangles[i].vertex[0]], &Triangles[i].facenormal);
 
-        normals[Triangles[i].vertex[0]].x += facenormals[i].x;
-        normals[Triangles[i].vertex[0]].y += facenormals[i].y;
-        normals[Triangles[i].vertex[0]].z += facenormals[i].z;
+        normals[Triangles[i].vertex[0]].x += Triangles[i].facenormal.x;
+        normals[Triangles[i].vertex[0]].y += Triangles[i].facenormal.y;
+        normals[Triangles[i].vertex[0]].z += Triangles[i].facenormal.z;
 
-        normals[Triangles[i].vertex[1]].x += facenormals[i].x;
-        normals[Triangles[i].vertex[1]].y += facenormals[i].y;
-        normals[Triangles[i].vertex[1]].z += facenormals[i].z;
+        normals[Triangles[i].vertex[1]].x += Triangles[i].facenormal.x;
+        normals[Triangles[i].vertex[1]].y += Triangles[i].facenormal.y;
+        normals[Triangles[i].vertex[1]].z += Triangles[i].facenormal.z;
 
-        normals[Triangles[i].vertex[2]].x += facenormals[i].x;
-        normals[Triangles[i].vertex[2]].y += facenormals[i].y;
-        normals[Triangles[i].vertex[2]].z += facenormals[i].z;
+        normals[Triangles[i].vertex[2]].x += Triangles[i].facenormal.x;
+        normals[Triangles[i].vertex[2]].y += Triangles[i].facenormal.y;
+        normals[Triangles[i].vertex[2]].z += Triangles[i].facenormal.z;
         if (facenormalise)
-            Normalise(&facenormals[i]);
+            Normalise(&Triangles[i].facenormal);
     }
-    for (i = 0; i < vertexNum; i++) {
+    for (int i = 0; i < vertexNum; i++) {
         Normalise(&normals[i]);
         normals[i] *= -1;
     }
@@ -834,14 +827,14 @@ void Model::drawimmediate()
 {
     textureptr.bind();
     glBegin(GL_TRIANGLES);
-    for (int i = 0; i < TriangleNum; i++) {
+    for (unsigned int i = 0; i < Triangles.size(); i++) {
         glTexCoord2f(Triangles[i].gx[0], Triangles[i].gy[0]);
         if (color)
             glColor3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
         if (!color && !flat)
             glNormal3f(normals[Triangles[i].vertex[0]].x, normals[Triangles[i].vertex[0]].y, normals[Triangles[i].vertex[0]].z);
         if (!color && flat)
-            glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
+            glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
         glVertex3f(vertex[Triangles[i].vertex[0]].x, vertex[Triangles[i].vertex[0]].y, vertex[Triangles[i].vertex[0]].z);
 
         glTexCoord2f(Triangles[i].gx[1], Triangles[i].gy[1]);
@@ -850,7 +843,7 @@ void Model::drawimmediate()
         if (!color && !flat)
             glNormal3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
         if (!color && flat)
-            glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
+            glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
         glVertex3f(vertex[Triangles[i].vertex[1]].x, vertex[Triangles[i].vertex[1]].y, vertex[Triangles[i].vertex[1]].z);
 
         glTexCoord2f(Triangles[i].gx[2], Triangles[i].gy[2]);
@@ -859,7 +852,7 @@ void Model::drawimmediate()
         if (!color && !flat)
             glNormal3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
         if (!color && flat)
-            glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
+            glNormal3f(Triangles[i].facenormal.x, Triangles[i].facenormal.y, Triangles[i].facenormal.y);
         glVertex3f(vertex[Triangles[i].vertex[2]].x, vertex[Triangles[i].vertex[2]].y, vertex[Triangles[i].vertex[2]].z);
     }
     glEnd();
@@ -880,7 +873,7 @@ void Model::draw()
         glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
     textureptr.bind();
 
-    glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
+    glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
 
     if (!color)
         glDisableClientState(GL_NORMAL_ARRAY);
@@ -905,7 +898,7 @@ void Model::drawdifftex(GLuint texture)
     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
 
-    glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
+    glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
 
     if (!color)
         glDisableClientState(GL_NORMAL_ARRAY);
@@ -929,7 +922,7 @@ void Model::drawdifftex(Texture texture)
     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
 
-    glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
+    glDrawArrays(GL_TRIANGLES, 0, Triangles.size() * 3);
 
     if (!color)
         glDisableClientState(GL_NORMAL_ARRAY);
@@ -1058,9 +1051,9 @@ void Model::MakeDecal(decal_type atype, XYZ *where, float *size, float *opacity,
 
         if (*opacity > 0)
             if (distsq(where, &boundingspherecenter) < (boundingsphereradius + *size) * (boundingsphereradius + *size))
-                for (int i = 0; i < TriangleNum; i++) {
-                    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)) {
-                        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);
+                for (unsigned int i = 0; i < Triangles.size(); i++) {
+                    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)) {
+                        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);
 
                         if (*opacity - distance / 10 > 0) {
                             Decal decal(*where, atype, *opacity - distance / 10, *rotation, *size, *this, i, 0);
@@ -1100,9 +1093,9 @@ void Model::MakeDecal(decal_type atype, XYZ where, float size, float opacity, fl
 
         if (opacity > 0)
             if (distsq(&where, &boundingspherecenter) < (boundingsphereradius + size) * (boundingsphereradius + size))
-                for (int i = 0; i < TriangleNum; i++) {
-                    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))));
-                    if (distance < .02 && abs(facenormals[i].y) > abs(facenormals[i].x) && abs(facenormals[i].y) > abs(facenormals[i].z)) {
+                for (unsigned int i = 0; i < Triangles.size(); i++) {
+                    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))));
+                    if (distance < .02 && abs(Triangles[i].facenormal.y) > abs(Triangles[i].facenormal.x) && abs(Triangles[i].facenormal.y) > abs(Triangles[i].facenormal.z)) {
                         if (opacity - distance / 10 > 0) {
                             Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 0);
 
@@ -1125,7 +1118,7 @@ void Model::MakeDecal(decal_type atype, XYZ where, float size, float opacity, fl
                                             }
                                         }
                         }
-                    } else if (distance < .02 && abs(facenormals[i].x) > abs(facenormals[i].y) && abs(facenormals[i].x) > abs(facenormals[i].z)) {
+                    } else if (distance < .02 && abs(Triangles[i].facenormal.x) > abs(Triangles[i].facenormal.y) && abs(Triangles[i].facenormal.x) > abs(Triangles[i].facenormal.z)) {
                         if (opacity - distance / 10 > 0) {
                             Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 1);
 
@@ -1148,7 +1141,7 @@ void Model::MakeDecal(decal_type atype, XYZ where, float size, float opacity, fl
                                             }
                                         }
                         }
-                    } else if (distance < .02 && abs(facenormals[i].z) > abs(facenormals[i].y) && abs(facenormals[i].z) > abs(facenormals[i].x)) {
+                    } else if (distance < .02 && abs(Triangles[i].facenormal.z) > abs(Triangles[i].facenormal.y) && abs(Triangles[i].facenormal.z) > abs(Triangles[i].facenormal.x)) {
                         if (opacity - distance / 10 > 0) {
                             Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 2);
 
@@ -1208,29 +1201,19 @@ void Model::deallocate()
         free(normals);
     normals = 0;
 
-    if (facenormals)
-        free(facenormals);
-    facenormals = 0;
-
-    if (Triangles)
-        free(Triangles);
-    Triangles = 0;
-
     if (vArray)
         free(vArray);
     vArray = 0;
 }
 
 Model::Model()
-  : vertexNum(0), TriangleNum(0),
+  : vertexNum(0),
     hastexture(0),
     type(0), oldtype(0),
     possible(0),
     owner(0),
     vertex(0),
     normals(0),
-    facenormals(0),
-    Triangles(0),
     vArray(0)
 {
     memset(&modelTexture, 0, sizeof(modelTexture));
index 5d5e5ec5e90c006be40d3650a89a219fce20a85b..e642f17d2da82c9dba2f2c5c39c2feb85f193945 100644 (file)
@@ -49,6 +49,7 @@ class TexturedTriangle
 public:
     short vertex[3];
     float gx[3], gy[3];
+    XYZ facenormal;
 };
 
 #define max_model_decals 300
@@ -62,7 +63,7 @@ public:
 class Model
 {
 public:
-    short vertexNum, TriangleNum;
+    short vertexNum;
     bool hastexture;
 
     int type, oldtype;
@@ -71,16 +72,13 @@ public:
     int* owner;
     XYZ* vertex;
     XYZ* normals;
-    XYZ* facenormals;
-    TexturedTriangle* Triangles;
+    std::vector<TexturedTriangle> Triangles;
     GLfloat* vArray;
 
     /*int possible[max_model_vertex];
     int owner[max_textured_triangle];
     XYZ vertex[max_model_vertex];
     XYZ normals[max_model_vertex];
-    XYZ facenormals[max_textured_triangle];
-    TexturedTriangle Triangles[max_textured_triangle];
     GLfloat vArray[max_textured_triangle*24];*/
 
     Texture textureptr;
@@ -91,25 +89,8 @@ public:
     XYZ boundingspherecenter;
     float boundingsphereradius;
 
-    //~ float*** decaltexcoords;
-    //~ XYZ** decalvertex;
-    //~ int* decaltype;
-    //~ float* decalopacity;
-    //~ float* decalrotation;
-    //~ float* decalalivetime;
-    //~ XYZ* decalposition;
     std::vector<Decal> decals;
 
-    /*float decaltexcoords[max_model_decals][3][2];
-    XYZ decalvertex[max_model_decals][3];
-    int decaltype[max_model_decals];
-    float decalopacity[max_model_decals];
-    float decalrotation[max_model_decals];
-    float decalalivetime[max_model_decals];
-    XYZ decalposition[max_model_decals];*/
-
-    int numdecals;
-
     bool flat;
 
     void DeleteDecal(int which);
index 06665de39c645f74bddf07ff155b0a594e4a6121..aff63df4da1efdf2f85b91c1b5ffbb71a818d8fb 100644 (file)
@@ -222,7 +222,7 @@ void Object::doShadows(XYZ lightloc)
             if (shadowed > 0) {
                 col = model.normals[j] - DoRotation(lightloc * shadowed, 0, -yaw, 0);
                 Normalise(&col);
-                for (int k = 0; k < model.TriangleNum; k++) {
+                for (int k = 0; k < model.Triangles.size(); k++) {
                     if (model.Triangles[k].vertex[0] == j) {
                         int l = k * 24;
                         model.vArray[l + 2] = col.x;
index 5dadc8ce697e1c11d260f99bd4ddefc31644c657..7ecd5f59211d09f2e13978152eb53a152070901f 100644 (file)
@@ -6361,7 +6361,7 @@ int Person::DrawSkeleton()
             glBegin(GL_LINES);
 
             if (playerdetail)
-                for (i = 0; i < skeleton.drawmodel.TriangleNum; i++) {
+                for (i = 0; i < skeleton.drawmodel.Triangles.size(); i++) {
                     XYZ &v0 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[i].vertex[0]];
                     XYZ &v1 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[i].vertex[1]];
                     XYZ &v2 = skeleton.drawmodel.vertex[skeleton.drawmodel.Triangles[i].vertex[2]];
@@ -6740,13 +6740,13 @@ int Person::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate,
     if (*rotate)
         *p1 = DoRotation(*p1, 0, -*rotate, 0);
     for (i = 0; i < 4; i++) {
-        for (j = 0; j < model->TriangleNum; j++) {
-            if (model->facenormals[j].y <= slopethreshold) {
+        for (j = 0; j < model->Triangles.size(); j++) {
+            if (model->Triangles[j].facenormal.y <= slopethreshold) {
                 intersecting = 0;
-                distance = abs((model->facenormals[j].x * p1->x) + (model->facenormals[j].y * p1->y) + (model->facenormals[j].z * p1->z) - ((model->facenormals[j].x * model->vertex[model->Triangles[j].vertex[0]].x) + (model->facenormals[j].y * model->vertex[model->Triangles[j].vertex[0]].y) + (model->facenormals[j].z * model->vertex[model->Triangles[j].vertex[0]].z)));
+                distance = abs((model->Triangles[j].facenormal.x * p1->x) + (model->Triangles[j].facenormal.y * p1->y) + (model->Triangles[j].facenormal.z * p1->z) - ((model->Triangles[j].facenormal.x * model->vertex[model->Triangles[j].vertex[0]].x) + (model->Triangles[j].facenormal.y * model->vertex[model->Triangles[j].vertex[0]].y) + (model->Triangles[j].facenormal.z * model->vertex[model->Triangles[j].vertex[0]].z)));
                 if (distance < radius) {
-                    point = *p1 - model->facenormals[j] * distance;
-                    if (PointInTriangle( &point, model->facenormals[j], &model->vertex[model->Triangles[j].vertex[0]], &model->vertex[model->Triangles[j].vertex[1]], &model->vertex[model->Triangles[j].vertex[2]]))
+                    point = *p1 - model->Triangles[j].facenormal * distance;
+                    if (PointInTriangle( &point, model->Triangles[j].facenormal, &model->vertex[model->Triangles[j].vertex[0]], &model->vertex[model->Triangles[j].vertex[1]], &model->vertex[model->Triangles[j].vertex[2]]))
                         intersecting = 1;
                     if (!intersecting)
                         intersecting = sphere_line_intersection(&model->vertex[model->Triangles[j].vertex[0]],
@@ -6761,11 +6761,11 @@ int Person::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate,
                                                                 &model->vertex[model->Triangles[j].vertex[2]],
                                                                 p1, &radius);
                     end = *p1 - point;
-                    if (dotproduct(&model->facenormals[j], &end) > 0 && intersecting) {
+                    if (dotproduct(&model->Triangles[j].facenormal, &end) > 0 && intersecting) {
                         start = *p1;
                         end = *p1;
                         end.y -= radius;
-                        if (LineFacetd(&start, &end, &model->vertex[model->Triangles[j].vertex[0]], &model->vertex[model->Triangles[j].vertex[1]], &model->vertex[model->Triangles[j].vertex[2]], &model->facenormals[j], &point)) {
+                        if (LineFacetd(&start, &end, &model->vertex[model->Triangles[j].vertex[0]], &model->vertex[model->Triangles[j].vertex[1]], &model->vertex[model->Triangles[j].vertex[2]], &model->Triangles[j].facenormal, &point)) {
                             p1->y = point.y + radius;
                             if ((animTarget == jumpdownanim || isFlip())) {
                                 if (isFlip() && (frameTarget < 5 || targetFrame().label == 7 || targetFrame().label == 4))
@@ -6805,23 +6805,23 @@ int Person::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate,
                 }
             }
         }
-        for (j = 0; j < model->TriangleNum; j++) {
-            if (model->facenormals[j].y > slopethreshold) {
+        for (j = 0; j < model->Triangles.size(); j++) {
+            if (model->Triangles[j].facenormal.y > slopethreshold) {
                 intersecting = 0;
                 start = *p1;
                 start.y -= radius / 4;
                 XYZ &v0 = model->vertex[model->Triangles[j].vertex[0]];
                 XYZ &v1 = model->vertex[model->Triangles[j].vertex[1]];
                 XYZ &v2 = model->vertex[model->Triangles[j].vertex[2]];
-                distance = abs((model->facenormals[j].x * start.x)
-                               + (model->facenormals[j].y * start.y)
-                               + (model->facenormals[j].z * start.z)
-                               - ((model->facenormals[j].x * v0.x)
-                                  + (model->facenormals[j].y * v0.y)
-                                  + (model->facenormals[j].z * v0.z)));
+                distance = abs((model->Triangles[j].facenormal.x * start.x)
+                               + (model->Triangles[j].facenormal.y * start.y)
+                               + (model->Triangles[j].facenormal.z * start.z)
+                               - ((model->Triangles[j].facenormal.x * v0.x)
+                                  + (model->Triangles[j].facenormal.y * v0.y)
+                                  + (model->Triangles[j].facenormal.z * v0.z)));
                 if (distance < radius * .5) {
-                    point = start - model->facenormals[j] * distance;
-                    if (PointInTriangle( &point, model->facenormals[j], &v0, &v1, &v2))
+                    point = start - model->Triangles[j].facenormal * distance;
+                    if (PointInTriangle( &point, model->Triangles[j].facenormal, &v0, &v1, &v2))
                         intersecting = 1;
                     if (!intersecting)
                         intersecting = sphere_line_intersection(v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, p1->x, p1->y, p1->z, radius / 2);
@@ -6830,14 +6830,14 @@ int Person::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate,
                     if (!intersecting)
                         intersecting = sphere_line_intersection(v0.x, v0.y, v0.z, v2.x, v2.y, v2.z, p1->x, p1->y, p1->z, radius / 2);
                     end = *p1 - point;
-                    if (dotproduct(&model->facenormals[j], &end) > 0 && intersecting) {
+                    if (dotproduct(&model->Triangles[j].facenormal, &end) > 0 && intersecting) {
                         if ((animTarget == jumpdownanim || animTarget == jumpupanim || isFlip())) {
                             start = velocity;
-                            velocity -= DoRotation(model->facenormals[j], 0, *rotate, 0) * findLength(&velocity) * abs(normaldotproduct(velocity, DoRotation(model->facenormals[j], 0, *rotate, 0))); //(distance-radius*.5)/multiplier;
+                            velocity -= DoRotation(model->Triangles[j].facenormal, 0, *rotate, 0) * findLength(&velocity) * abs(normaldotproduct(velocity, DoRotation(model->Triangles[j].facenormal, 0, *rotate, 0))); //(distance-radius*.5)/multiplier;
                             if (findLengthfast(&start) < findLengthfast(&velocity))
                                 velocity = start;
                         }
-                        *p1 += model->facenormals[j] * (distance - radius * .5);
+                        *p1 += model->Triangles[j].facenormal * (distance - radius * .5);
                     }
                 }
                 if ((distance < olddistance || firstintersecting == -1) && intersecting) {
index 38ee55be357d52d38babdc9b223992e581f3e0a9..5ef40e6ff08e47d17a8963419f2eed0e32c659f9 100644 (file)
@@ -166,7 +166,7 @@ void Weapon::DoStuff(int i)
                     if (whichhit != -1) {
                         if (Object::objects[k]->type == treetrunktype) {
                             Object::objects[k]->model.MakeDecal(breakdecal, DoRotation(colpoint - Object::objects[k]->position, 0, -Object::objects[k]->yaw, 0), .1, 1, Random() % 360);
-                            normalrot = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0);
+                            normalrot = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0);
                             velocity = 0;
                             if (type == knife)
                                 position = colpoint - normalrot * .1;
@@ -451,7 +451,7 @@ void Weapon::DoStuff(int i)
                         if (whichhit != -1) {
                             hitsomething = 1;
                             position = colpoint;
-                            terrainnormal = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * -1;
+                            terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
                             ReflectVector(&velocity, &terrainnormal);
                             position += terrainnormal * .002;
 
@@ -481,7 +481,7 @@ void Weapon::DoStuff(int i)
                         if (whichhit != -1) {
                             hitsomething = 1;
                             tippoint = colpoint;
-                            terrainnormal = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * -1;
+                            terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
                             ReflectVector(&tipvelocity, &terrainnormal);
                             tippoint += terrainnormal * .002;
 
@@ -518,7 +518,7 @@ void Weapon::DoStuff(int i)
                                 if (whichhit != -1) {
                                     hitsomething = 1;
                                     mid = colpoint;
-                                    terrainnormal = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * -1;
+                                    terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
                                     ReflectVector(&velocity, &terrainnormal);
 
                                     bounceness = terrainnormal * findLength(&velocity) * (abs(normaldotproduct(velocity, terrainnormal)));
@@ -553,7 +553,7 @@ void Weapon::DoStuff(int i)
                                 if (whichhit != -1) {
                                     hitsomething = 1;
                                     mid = colpoint;
-                                    terrainnormal = DoRotation(Object::objects[k]->model.facenormals[whichhit], 0, Object::objects[k]->yaw, 0) * -1;
+                                    terrainnormal = DoRotation(Object::objects[k]->model.Triangles[whichhit].facenormal, 0, Object::objects[k]->yaw, 0) * -1;
                                     ReflectVector(&tipvelocity, &terrainnormal);
 
                                     bounceness = terrainnormal * findLength(&tipvelocity) * (abs(normaldotproduct(tipvelocity, terrainnormal)));