From 190f9f06c28bf4b7aff2ad0b61e5648ea2b10cc6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=B4me=20Chilliet?= Date: Fri, 23 Dec 2016 02:13:19 +0100 Subject: [PATCH] Using the Decal class in Model as well --- Source/Environment/Terrain.cpp | 2 - Source/GameTick.cpp | 21 +- Source/Graphic/Decal.cpp | 109 ++++++++ Source/Graphic/Decal.hpp | 2 + Source/Graphic/Models.cpp | 493 +++++++++------------------------ Source/Graphic/Models.hpp | 20 +- 6 files changed, 266 insertions(+), 381 deletions(-) diff --git a/Source/Environment/Terrain.cpp b/Source/Environment/Terrain.cpp index 5d74d54..79d31b2 100644 --- a/Source/Environment/Terrain.cpp +++ b/Source/Environment/Terrain.cpp @@ -1393,6 +1393,4 @@ Terrain::Terrain() memset(heightypatch, 0, sizeof(heightypatch)); patch_elements = 0; - - decals.clear(); } diff --git a/Source/GameTick.cpp b/Source/GameTick.cpp index ebf1175..25a6fb0 100644 --- a/Source/GameTick.cpp +++ b/Source/GameTick.cpp @@ -2460,13 +2460,9 @@ void doAttacks() Person::players[k]->animTarget = killanim; terrain.deleteDeadDecals(); for (unsigned int l = 0; l < Object::objects.size(); l++) { - if (Object::objects[l]->model.type == decalstype) - for (int j = 0; j < Object::objects[l]->model.numdecals; j++) { - if ((Object::objects[l]->model.decaltype[j] == blooddecal || - Object::objects[l]->model.decaltype[j] == blooddecalslow) && - Object::objects[l]->model.decalalivetime[j] < 2) - Object::objects[l]->model.DeleteDecal(j); - } + if (Object::objects[l]->model.type == decalstype) { + Object::objects[l]->model.deleteDeadDecals(); + } } } if (!Person::players[i]->dead || musictype != 2) @@ -2481,14 +2477,9 @@ void doAttacks() Person::players[k]->animTarget = dropkickanim; terrain.deleteDeadDecals(); for (unsigned int l = 0; l < Object::objects.size(); l++) { - if (Object::objects[l]->model.type == decalstype) - for (int j = 0; j < Object::objects[l]->model.numdecals; j++) { - if ((Object::objects[l]->model.decaltype[j] == blooddecal || - Object::objects[l]->model.decaltype[j] == blooddecalslow) && - Object::objects[l]->model.decalalivetime[j] < 2) { - Object::objects[l]->model.DeleteDecal(j); - } - } + if (Object::objects[l]->model.type == decalstype) { + Object::objects[l]->model.deleteDeadDecals(); + } } } } diff --git a/Source/Graphic/Decal.cpp b/Source/Graphic/Decal.cpp index de3388f..cb1ee99 100644 --- a/Source/Graphic/Decal.cpp +++ b/Source/Graphic/Decal.cpp @@ -21,6 +21,7 @@ along with Lugaru. If not, see . #include "Graphic/Decal.hpp" #include "Environment/Terrain.hpp" +#include "Graphic/Models.hpp" Decal::Decal() : position(), @@ -93,3 +94,111 @@ Decal::Decal(XYZ _position, decal_type _type, float _opacity, float _rotation, f } } } + +Decal::Decal(XYZ _position, decal_type _type, float _opacity, float _rotation, float size, const Model& model, int i, int which) : + position(_position), + type(_type), + opacity(_opacity), + rotation(_rotation), + alivetime(0), + brightness(0) +{ + float placex, placez; + if (which == 0) { + placex = vertex[model.Triangles[i].vertex[0]].x; + placez = vertex[model.Triangles[i].vertex[0]].z; + + texcoords[0][0] = (placex - position.x) / (size) / 2 + .5; + texcoords[0][1] = (placez - position.z) / (size) / 2 + .5; + + vertex[0].x = placex; + vertex[0].z = placez; + vertex[0].y = vertex[model.Triangles[i].vertex[0]].y; + + + placex = vertex[model.Triangles[i].vertex[1]].x; + placez = vertex[model.Triangles[i].vertex[1]].z; + + texcoords[1][0] = (placex - position.x) / (size) / 2 + .5; + texcoords[1][1] = (placez - position.z) / (size) / 2 + .5; + + vertex[1].x = placex; + vertex[1].z = placez; + vertex[1].y = vertex[model.Triangles[i].vertex[1]].y; + + + placex = vertex[model.Triangles[i].vertex[2]].x; + placez = vertex[model.Triangles[i].vertex[2]].z; + + texcoords[2][0] = (placex - position.x) / (size) / 2 + .5; + texcoords[2][1] = (placez - position.z) / (size) / 2 + .5; + + vertex[2].x = placex; + vertex[2].z = placez; + vertex[2].y = vertex[model.Triangles[i].vertex[2]].y; + } else if (which == 1) { + placex = vertex[model.Triangles[i].vertex[0]].y; + placez = vertex[model.Triangles[i].vertex[0]].z; + + texcoords[0][0] = (placex - position.y) / (size) / 2 + .5; + texcoords[0][1] = (placez - position.z) / (size) / 2 + .5; + + vertex[0].x = vertex[model.Triangles[i].vertex[0]].x; + vertex[0].z = placez; + vertex[0].y = placex; + + + placex = vertex[model.Triangles[i].vertex[1]].y; + placez = vertex[model.Triangles[i].vertex[1]].z; + + texcoords[1][0] = (placex - position.y) / (size) / 2 + .5; + texcoords[1][1] = (placez - position.z) / (size) / 2 + .5; + + vertex[1].x = vertex[model.Triangles[i].vertex[1]].x; + vertex[1].z = placez; + vertex[1].y = placex; + + + placex = vertex[model.Triangles[i].vertex[2]].y; + placez = vertex[model.Triangles[i].vertex[2]].z; + + texcoords[2][0] = (placex - position.y) / (size) / 2 + .5; + texcoords[2][1] = (placez - position.z) / (size) / 2 + .5; + + vertex[2].x = vertex[model.Triangles[i].vertex[2]].x; + vertex[2].z = placez; + vertex[2].y = placex; + } else { + placex = vertex[model.Triangles[i].vertex[0]].x; + placez = vertex[model.Triangles[i].vertex[0]].y; + + texcoords[0][0] = (placex - position.x) / (size) / 2 + .5; + texcoords[0][1] = (placez - position.y) / (size) / 2 + .5; + + vertex[0].x = placex; + vertex[0].z = vertex[model.Triangles[i].vertex[0]].z; + vertex[0].y = placez; + + + placex = vertex[model.Triangles[i].vertex[1]].x; + placez = vertex[model.Triangles[i].vertex[1]].y; + + texcoords[1][0] = (placex - position.x) / (size) / 2 + .5; + texcoords[1][1] = (placez - position.y) / (size) / 2 + .5; + + vertex[1].x = placex; + vertex[1].z = vertex[model.Triangles[i].vertex[1]].z; + vertex[1].y = placez; + + + placex = vertex[model.Triangles[i].vertex[2]].x; + placez = vertex[model.Triangles[i].vertex[2]].y; + + texcoords[2][0] = (placex - position.x) / (size) / 2 + .5; + texcoords[2][1] = (placez - position.y) / (size) / 2 + .5; + + vertex[2].x = placex; + vertex[2].z = vertex[model.Triangles[i].vertex[2]].z; + vertex[2].y = placez; + } +} diff --git a/Source/Graphic/Decal.hpp b/Source/Graphic/Decal.hpp index ae3862f..094d043 100644 --- a/Source/Graphic/Decal.hpp +++ b/Source/Graphic/Decal.hpp @@ -22,6 +22,7 @@ along with Lugaru. If not, see . #define _DECAL_HPP_ class Terrain; +class Model; #include "Math/Quaternions.hpp" @@ -51,6 +52,7 @@ public: Decal(); Decal(XYZ position, decal_type type, float opacity, float rotation, float brightness, int whichx, int whichy, float size, const Terrain& terrain, bool first); + Decal(XYZ position, decal_type type, float opacity, float rotation, float size, const Model& model, int i, int which); }; #endif diff --git a/Source/Graphic/Models.cpp b/Source/Graphic/Models.cpp index ee5add1..ef2b7bb 100644 --- a/Source/Graphic/Models.cpp +++ b/Source/Graphic/Models.cpp @@ -546,7 +546,7 @@ bool Model::loaddecal(const std::string& filename) LOG(std::string("Loading decal...") + Folders::getResourcePath(filename)); type = decalstype; - numdecals = 0; + decals.clear(); color = 0; tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" ); @@ -607,27 +607,6 @@ bool Model::loaddecal(const std::string& filename) } boundingsphereradius = fast_sqrt(boundingsphereradius); - //allow decals - if (!decaltexcoords) { - decaltexcoords = (float***)malloc(sizeof(float**)*max_model_decals); - for (i = 0; i < max_model_decals; i++) { - decaltexcoords[i] = (float**)malloc(sizeof(float*) * 3); - for (j = 0; j < 3; j++) { - decaltexcoords[i][j] = (float*)malloc(sizeof(float) * 2); - } - } - decalvertex = (XYZ**)malloc(sizeof(XYZ*)*max_model_decals); - for (i = 0; i < max_model_decals; i++) { - decalvertex[i] = (XYZ*)malloc(sizeof(XYZ) * 3); - } - - decaltype = (int*)malloc(sizeof(int) * max_model_decals); - decalopacity = (float*)malloc(sizeof(float) * max_model_decals); - decalrotation = (float*)malloc(sizeof(float) * max_model_decals); - decalalivetime = (float*)malloc(sizeof(float) * max_model_decals); - decalposition = (XYZ*)malloc(sizeof(XYZ) * max_model_decals); - } - return true; } @@ -965,7 +944,6 @@ void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloo if (decalstoggle) { if (type != decalstype) return; - static int i; static int lasttype; static bool blend; @@ -977,60 +955,60 @@ void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloo glDisable(GL_CULL_FACE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthMask(0); - if (numdecals > max_model_decals) - numdecals = max_model_decals; - for (i = 0; i < numdecals; i++) { - if (decaltype[i] == blooddecalfast && decalalivetime[i] < 2) - decalalivetime[i] = 2; - - if (decaltype[i] == shadowdecal && decaltype[i] != lasttype) { - shadowtexture.bind(); - if (!blend) { - blend = 1; - glAlphaFunc(GL_GREATER, 0.0001); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + for (unsigned int i = 0; i < decals.size(); i++) { + if (decals[i].type == blooddecalfast && decals[i].alivetime < 2) + decals[i].alivetime = 2; + + if (decals[i].type != lasttype) { + if (decals[i].type == shadowdecal) { + shadowtexture.bind(); + if (!blend) { + blend = 1; + glAlphaFunc(GL_GREATER, 0.0001); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } } - } - if (decaltype[i] == breakdecal && decaltype[i] != lasttype) { - breaktexture.bind(); - if (!blend) { - blend = 1; - glAlphaFunc(GL_GREATER, 0.0001); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + if (decals[i].type == breakdecal) { + breaktexture.bind(); + if (!blend) { + blend = 1; + glAlphaFunc(GL_GREATER, 0.0001); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } } - } - if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalslow) && decaltype[i] != lasttype) { - bloodtexture.bind(); - if (blend) { - blend = 0; - glAlphaFunc(GL_GREATER, 0.15); - glBlendFunc(GL_ONE, GL_ZERO); + if (decals[i].type == blooddecal || decals[i].type == blooddecalslow) { + bloodtexture.bind(); + if (blend) { + blend = 0; + glAlphaFunc(GL_GREATER, 0.15); + glBlendFunc(GL_ONE, GL_ZERO); + } } - } - if ((decaltype[i] == blooddecalfast) && decaltype[i] != lasttype) { - bloodtexture2.bind(); - if (blend) { - blend = 0; - glAlphaFunc(GL_GREATER, 0.15); - glBlendFunc(GL_ONE, GL_ZERO); + if (decals[i].type == blooddecalfast) { + bloodtexture2.bind(); + if (blend) { + blend = 0; + glAlphaFunc(GL_GREATER, 0.15); + glBlendFunc(GL_ONE, GL_ZERO); + } } } - if (decaltype[i] == shadowdecal) { - glColor4f(1, 1, 1, decalopacity[i]); + if (decals[i].type == shadowdecal) { + glColor4f(1, 1, 1, decals[i].opacity); } - if (decaltype[i] == breakdecal) { - glColor4f(1, 1, 1, decalopacity[i]); - if (decalalivetime[i] > 58) - glColor4f(1, 1, 1, decalopacity[i] * (60 - decalalivetime[i]) / 2); + if (decals[i].type == breakdecal) { + glColor4f(1, 1, 1, decals[i].opacity); + if (decals[i].alivetime > 58) + glColor4f(1, 1, 1, decals[i].opacity * (60 - decals[i].alivetime) / 2); } - if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow)) { - glColor4f(1, 1, 1, decalopacity[i]); - if (decalalivetime[i] < 4) - glColor4f(1, 1, 1, decalopacity[i]*decalalivetime[i]*.25); - if (decalalivetime[i] > 58) - glColor4f(1, 1, 1, decalopacity[i] * (60 - decalalivetime[i]) / 2); + if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow)) { + glColor4f(1, 1, 1, decals[i].opacity); + if (decals[i].alivetime < 4) + glColor4f(1, 1, 1, decals[i].opacity*decals[i].alivetime*.25); + if (decals[i].alivetime > 58) + glColor4f(1, 1, 1, decals[i].opacity * (60 - decals[i].alivetime) / 2); } - lasttype = decaltype[i]; + lasttype = decals[i].type; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); @@ -1038,21 +1016,21 @@ void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloo glPushMatrix(); glBegin(GL_TRIANGLES); for (int j = 0; j < 3; j++) { - glTexCoord2f(decaltexcoords[i][j][0], decaltexcoords[i][j][1]); - glVertex3f(decalvertex[i][j].x, decalvertex[i][j].y, decalvertex[i][j].z); + glTexCoord2f(decals[i].texcoords[j][0], decals[i].texcoords[j][1]); + glVertex3f(decals[i].vertex[j].x, decals[i].vertex[j].y, decals[i].vertex[j].z); } glEnd(); glPopMatrix(); } - for (i = numdecals - 1; i >= 0; i--) { - decalalivetime[i] += multiplier; - if (decaltype[i] == blooddecalslow) - decalalivetime[i] -= multiplier * 2 / 3; - if (decaltype[i] == blooddecalfast) - decalalivetime[i] += multiplier * 4; - if (decaltype[i] == shadowdecal) + for (int i = decals.size() - 1; i >= 0; i--) { + decals[i].alivetime += multiplier; + if (decals[i].type == blooddecalslow) + decals[i].alivetime -= multiplier * 2 / 3; + if (decals[i].type == blooddecalfast) + decals[i].alivetime += multiplier * 4; + if (decals[i].type == shadowdecal) DeleteDecal(i); - if ((decaltype[i] == blooddecal || decaltype[i] == blooddecalfast || decaltype[i] == blooddecalslow) && decalalivetime[i] >= 60) + if ((decals[i].type == blooddecal || decals[i].type == blooddecalfast || decals[i].type == blooddecalslow) && decals[i].alivetime >= 60) DeleteDecal(i); } glAlphaFunc(GL_GREATER, 0.0001); @@ -1065,91 +1043,45 @@ void Model::DeleteDecal(int which) if (decalstoggle) { if (type != decalstype) return; - decaltype[which] = decaltype[numdecals - 1]; - decalposition[which] = decalposition[numdecals - 1]; - for (int i = 0; i < 3; i++) { - decalvertex[which][i] = decalvertex[numdecals - 1][i]; - decaltexcoords[which][i][0] = decaltexcoords[numdecals - 1][i][0]; - decaltexcoords[which][i][1] = decaltexcoords[numdecals - 1][i][1]; - } - decalrotation[which] = decalrotation[numdecals - 1]; - decalalivetime[which] = decalalivetime[numdecals - 1]; - decalopacity[which] = decalopacity[numdecals - 1]; - numdecals--; + decals.erase(decals.begin() + which); } } -void Model::MakeDecal(int atype, XYZ *where, float *size, float *opacity, float *rotation) +void Model::MakeDecal(decal_type atype, XYZ *where, float *size, float *opacity, float *rotation) { if (decalstoggle) { if (type != decalstype) return; - static float placex, placez; static XYZ rot; static float distance; - static int i, j; if (*opacity > 0) if (distsq(where, &boundingspherecenter) < (boundingsphereradius + *size) * (boundingsphereradius + *size)) - for (i = 0; i < TriangleNum; i++) { + 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)) { - decalposition[numdecals] = *where; - decaltype[numdecals] = atype; - decalrotation[numdecals] = *rotation; - decalalivetime[numdecals] = 0; 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); - decalopacity[numdecals] = *opacity - distance / 10; - - if (decalopacity[numdecals > 0]) { - placex = vertex[Triangles[i].vertex[0]].x; - placez = vertex[Triangles[i].vertex[0]].z; - - decaltexcoords[numdecals][0][0] = (placex - where->x) / (*size) / 2 + .5; - decaltexcoords[numdecals][0][1] = (placez - where->z) / (*size) / 2 + .5; - - decalvertex[numdecals][0].x = placex; - decalvertex[numdecals][0].z = placez; - decalvertex[numdecals][0].y = vertex[Triangles[i].vertex[0]].y; - - - placex = vertex[Triangles[i].vertex[1]].x; - placez = vertex[Triangles[i].vertex[1]].z; - - decaltexcoords[numdecals][1][0] = (placex - where->x) / (*size) / 2 + .5; - decaltexcoords[numdecals][1][1] = (placez - where->z) / (*size) / 2 + .5; - - decalvertex[numdecals][1].x = placex; - decalvertex[numdecals][1].z = placez; - decalvertex[numdecals][1].y = vertex[Triangles[i].vertex[1]].y; - - placex = vertex[Triangles[i].vertex[2]].x; - placez = vertex[Triangles[i].vertex[2]].z; + if (*opacity - distance / 10 > 0) { + Decal decal(*where, atype, *opacity - distance / 10, *rotation, *size, *this, i, 0); - decaltexcoords[numdecals][2][0] = (placex - where->x) / (*size) / 2 + .5; - decaltexcoords[numdecals][2][1] = (placez - where->z) / (*size) / 2 + .5; - - decalvertex[numdecals][2].x = placex; - decalvertex[numdecals][2].z = placez; - decalvertex[numdecals][2].y = vertex[Triangles[i].vertex[2]].y; - - if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0)) - if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0)) - if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1)) - if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) { - if (decalrotation[numdecals]) { - for (j = 0; j < 3; j++) { + if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0)) + if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0)) + if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1)) + if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) { + if (decal.rotation) { + for (int j = 0; j < 3; j++) { rot.y = 0; - rot.x = decaltexcoords[numdecals][j][0] - .5; - rot.z = decaltexcoords[numdecals][j][1] - .5; - rot = DoRotation(rot, 0, -decalrotation[numdecals], 0); - decaltexcoords[numdecals][j][0] = rot.x + .5; - decaltexcoords[numdecals][j][1] = rot.z + .5; + rot.x = decal.texcoords[j][0] - .5; + rot.z = decal.texcoords[j][1] - .5; + rot = DoRotation(rot, 0, -decal.rotation, 0); + decal.texcoords[j][0] = rot.x + .5; + decal.texcoords[j][1] = rot.z + .5; } } - if (numdecals < max_model_decals - 1) - numdecals++; + if (decals.size() < max_model_decals - 1) { + decals.push_back(decal); + } } } } @@ -1157,193 +1089,86 @@ void Model::MakeDecal(int atype, XYZ *where, float *size, float *opacity, float } } -void Model::MakeDecal(int atype, XYZ where, float size, float opacity, float rotation) +void Model::MakeDecal(decal_type atype, XYZ where, float size, float opacity, float rotation) { if (decalstoggle) { if (type != decalstype) return; - static float placex, placez; static XYZ rot; static float distance; - static int i, j; if (opacity > 0) if (distsq(&where, &boundingspherecenter) < (boundingsphereradius + size) * (boundingsphereradius + size)) - for (i = 0; i < TriangleNum; i++) { + 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)) { - decalposition[numdecals] = where; - decaltype[numdecals] = atype; - decalrotation[numdecals] = rotation; - decalalivetime[numdecals] = 0; - decalopacity[numdecals] = opacity - distance / 10; - - if (decalopacity[numdecals > 0]) { - placex = vertex[Triangles[i].vertex[0]].x; - placez = vertex[Triangles[i].vertex[0]].z; - - decaltexcoords[numdecals][0][0] = (placex - where.x) / (size) / 2 + .5; - decaltexcoords[numdecals][0][1] = (placez - where.z) / (size) / 2 + .5; - - decalvertex[numdecals][0].x = placex; - decalvertex[numdecals][0].z = placez; - decalvertex[numdecals][0].y = vertex[Triangles[i].vertex[0]].y; - - - placex = vertex[Triangles[i].vertex[1]].x; - placez = vertex[Triangles[i].vertex[1]].z; - - decaltexcoords[numdecals][1][0] = (placex - where.x) / (size) / 2 + .5; - decaltexcoords[numdecals][1][1] = (placez - where.z) / (size) / 2 + .5; - - decalvertex[numdecals][1].x = placex; - decalvertex[numdecals][1].z = placez; - decalvertex[numdecals][1].y = vertex[Triangles[i].vertex[1]].y; - - - placex = vertex[Triangles[i].vertex[2]].x; - placez = vertex[Triangles[i].vertex[2]].z; - - decaltexcoords[numdecals][2][0] = (placex - where.x) / (size) / 2 + .5; - decaltexcoords[numdecals][2][1] = (placez - where.z) / (size) / 2 + .5; - - decalvertex[numdecals][2].x = placex; - decalvertex[numdecals][2].z = placez; - decalvertex[numdecals][2].y = vertex[Triangles[i].vertex[2]].y; - - if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0)) - if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0)) - if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1)) - if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) { - if (decalrotation[numdecals]) { - for (j = 0; j < 3; j++) { + if (opacity - distance / 10 > 0) { + Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 0); + + if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0)) + if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0)) + if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1)) + if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) { + if (decal.rotation) { + for (int j = 0; j < 3; j++) { rot.y = 0; - rot.x = decaltexcoords[numdecals][j][0] - .5; - rot.z = decaltexcoords[numdecals][j][1] - .5; - rot = DoRotation(rot, 0, -decalrotation[numdecals], 0); - decaltexcoords[numdecals][j][0] = rot.x + .5; - decaltexcoords[numdecals][j][1] = rot.z + .5; + rot.x = decal.texcoords[j][0] - .5; + rot.z = decal.texcoords[j][1] - .5; + rot = DoRotation(rot, 0, -decal.rotation, 0); + decal.texcoords[j][0] = rot.x + .5; + decal.texcoords[j][1] = rot.z + .5; } } - if (numdecals < max_model_decals - 1) - numdecals++; + if (decals.size() < max_model_decals - 1) { + decals.push_back(decal); + } } } } else if (distance < .02 && abs(facenormals[i].x) > abs(facenormals[i].y) && abs(facenormals[i].x) > abs(facenormals[i].z)) { - decalposition[numdecals] = where; - decaltype[numdecals] = atype; - decalrotation[numdecals] = rotation; - decalalivetime[numdecals] = 0; - decalopacity[numdecals] = opacity - distance / 10; - - if (decalopacity[numdecals > 0]) { - placex = vertex[Triangles[i].vertex[0]].y; - placez = vertex[Triangles[i].vertex[0]].z; - - decaltexcoords[numdecals][0][0] = (placex - where.y) / (size) / 2 + .5; - decaltexcoords[numdecals][0][1] = (placez - where.z) / (size) / 2 + .5; - - decalvertex[numdecals][0].x = vertex[Triangles[i].vertex[0]].x; - decalvertex[numdecals][0].z = placez; - decalvertex[numdecals][0].y = placex; - - - placex = vertex[Triangles[i].vertex[1]].y; - placez = vertex[Triangles[i].vertex[1]].z; - - decaltexcoords[numdecals][1][0] = (placex - where.y) / (size) / 2 + .5; - decaltexcoords[numdecals][1][1] = (placez - where.z) / (size) / 2 + .5; - - decalvertex[numdecals][1].x = vertex[Triangles[i].vertex[1]].x; - decalvertex[numdecals][1].z = placez; - decalvertex[numdecals][1].y = placex; - - - placex = vertex[Triangles[i].vertex[2]].y; - placez = vertex[Triangles[i].vertex[2]].z; - - decaltexcoords[numdecals][2][0] = (placex - where.y) / (size) / 2 + .5; - decaltexcoords[numdecals][2][1] = (placez - where.z) / (size) / 2 + .5; - - decalvertex[numdecals][2].x = vertex[Triangles[i].vertex[2]].x; - decalvertex[numdecals][2].z = placez; - decalvertex[numdecals][2].y = placex; - - if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0)) - if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0)) - if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1)) - if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) { - if (decalrotation[numdecals]) { - for (j = 0; j < 3; j++) { + if (opacity - distance / 10 > 0) { + Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 1); + + if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0)) + if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0)) + if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1)) + if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) { + if (decal.rotation) { + for (int j = 0; j < 3; j++) { rot.y = 0; - rot.x = decaltexcoords[numdecals][j][0] - .5; - rot.z = decaltexcoords[numdecals][j][1] - .5; - rot = DoRotation(rot, 0, -decalrotation[numdecals], 0); - decaltexcoords[numdecals][j][0] = rot.x + .5; - decaltexcoords[numdecals][j][1] = rot.z + .5; + rot.x = decal.texcoords[j][0] - .5; + rot.z = decal.texcoords[j][1] - .5; + rot = DoRotation(rot, 0, -decal.rotation, 0); + decal.texcoords[j][0] = rot.x + .5; + decal.texcoords[j][1] = rot.z + .5; } } - if (numdecals < max_model_decals - 1) - numdecals++; + if (decals.size() < max_model_decals - 1) { + decals.push_back(decal); + } } } } else if (distance < .02 && abs(facenormals[i].z) > abs(facenormals[i].y) && abs(facenormals[i].z) > abs(facenormals[i].x)) { - decalposition[numdecals] = where; - decaltype[numdecals] = atype; - decalrotation[numdecals] = rotation; - decalalivetime[numdecals] = 0; - decalopacity[numdecals] = opacity - distance / 10; - - if (decalopacity[numdecals > 0]) { - placex = vertex[Triangles[i].vertex[0]].x; - placez = vertex[Triangles[i].vertex[0]].y; - - decaltexcoords[numdecals][0][0] = (placex - where.x) / (size) / 2 + .5; - decaltexcoords[numdecals][0][1] = (placez - where.y) / (size) / 2 + .5; - - decalvertex[numdecals][0].x = placex; - decalvertex[numdecals][0].z = vertex[Triangles[i].vertex[0]].z; - decalvertex[numdecals][0].y = placez; - - - placex = vertex[Triangles[i].vertex[1]].x; - placez = vertex[Triangles[i].vertex[1]].y; - - decaltexcoords[numdecals][1][0] = (placex - where.x) / (size) / 2 + .5; - decaltexcoords[numdecals][1][1] = (placez - where.y) / (size) / 2 + .5; - - decalvertex[numdecals][1].x = placex; - decalvertex[numdecals][1].z = vertex[Triangles[i].vertex[1]].z; - decalvertex[numdecals][1].y = placez; - - - placex = vertex[Triangles[i].vertex[2]].x; - placez = vertex[Triangles[i].vertex[2]].y; - - decaltexcoords[numdecals][2][0] = (placex - where.x) / (size) / 2 + .5; - decaltexcoords[numdecals][2][1] = (placez - where.y) / (size) / 2 + .5; - - decalvertex[numdecals][2].x = placex; - decalvertex[numdecals][2].z = vertex[Triangles[i].vertex[2]].z; - decalvertex[numdecals][2].y = placez; - - if (!(decaltexcoords[numdecals][0][0] < 0 && decaltexcoords[numdecals][1][0] < 0 && decaltexcoords[numdecals][2][0] < 0)) - if (!(decaltexcoords[numdecals][0][1] < 0 && decaltexcoords[numdecals][1][1] < 0 && decaltexcoords[numdecals][2][1] < 0)) - if (!(decaltexcoords[numdecals][0][0] > 1 && decaltexcoords[numdecals][1][0] > 1 && decaltexcoords[numdecals][2][0] > 1)) - if (!(decaltexcoords[numdecals][0][1] > 1 && decaltexcoords[numdecals][1][1] > 1 && decaltexcoords[numdecals][2][1] > 1)) { - if (decalrotation[numdecals]) { - for (j = 0; j < 3; j++) { + if (opacity - distance / 10 > 0) { + Decal decal(where, atype, opacity - distance / 10, rotation, size, *this, i, 2); + + if (!(decal.texcoords[0][0] < 0 && decal.texcoords[1][0] < 0 && decal.texcoords[2][0] < 0)) + if (!(decal.texcoords[0][1] < 0 && decal.texcoords[1][1] < 0 && decal.texcoords[2][1] < 0)) + if (!(decal.texcoords[0][0] > 1 && decal.texcoords[1][0] > 1 && decal.texcoords[2][0] > 1)) + if (!(decal.texcoords[0][1] > 1 && decal.texcoords[1][1] > 1 && decal.texcoords[2][1] > 1)) { + if (decal.rotation) { + for (int j = 0; j < 3; j++) { rot.y = 0; - rot.x = decaltexcoords[numdecals][j][0] - .5; - rot.z = decaltexcoords[numdecals][j][1] - .5; - rot = DoRotation(rot, 0, -decalrotation[numdecals], 0); - decaltexcoords[numdecals][j][0] = rot.x + .5; - decaltexcoords[numdecals][j][1] = rot.z + .5; + rot.x = decal.texcoords[j][0] - .5; + rot.z = decal.texcoords[j][1] - .5; + rot = DoRotation(rot, 0, -decal.rotation, 0); + decal.texcoords[j][0] = rot.x + .5; + decal.texcoords[j][1] = rot.z + .5; } } - if (numdecals < max_model_decals - 1) - numdecals++; + if (decals.size() < max_model_decals - 1) { + decals.push_back(decal); + } } } } @@ -1351,6 +1176,15 @@ void Model::MakeDecal(int atype, XYZ where, float size, float opacity, float rot } } +void Model::deleteDeadDecals() +{ + for (int i = decals.size() - 1; i >= 0; i--) { + if ((decals[i].type == blooddecal || decals[i].type == blooddecalslow) && decals[i].alivetime < 2) { + DeleteDecal(i); + } + } +} + Model::~Model() { deallocate(); @@ -1358,8 +1192,6 @@ Model::~Model() void Model::deallocate() { - int i = 0, j = 0; - if (owner) free(owner); owner = 0; @@ -1387,45 +1219,6 @@ void Model::deallocate() if (vArray) free(vArray); vArray = 0; - - - //allow decals - if (decaltexcoords) { - for (i = 0; i < max_model_decals; i++) { - for (j = 0; j < 3; j++) { - free(decaltexcoords[i][j]); - } - free(decaltexcoords[i]); - } - free(decaltexcoords); - } - decaltexcoords = 0; - - - if (decalvertex) { - for (i = 0; i < max_model_decals; i++) { - free(decalvertex[i]); - } - free(decalvertex); - } - decalvertex = 0; - - - free(decaltype); - decaltype = 0; - - free(decalopacity); - decalopacity = 0; - - free(decalrotation); - decalrotation = 0; - - free(decalalivetime); - decalalivetime = 0; - - free(decalposition); - decalposition = 0; - } Model::Model() @@ -1447,16 +1240,6 @@ Model::Model() boundingspherecenter = 0; boundingsphereradius = 0; - decaltexcoords = 0; - decalvertex = 0; - decaltype = 0; - decalopacity = 0; - decalrotation = 0; - decalalivetime = 0; - decalposition = 0; - - numdecals = 0; - flat = 0; type = nothing; diff --git a/Source/Graphic/Models.hpp b/Source/Graphic/Models.hpp index de4526e..5d5e5ec 100644 --- a/Source/Graphic/Models.hpp +++ b/Source/Graphic/Models.hpp @@ -91,13 +91,14 @@ public: XYZ boundingspherecenter; float boundingsphereradius; - float*** decaltexcoords; - XYZ** decalvertex; - int* decaltype; - float* decalopacity; - float* decalrotation; - float* decalalivetime; - XYZ* decalposition; + //~ float*** decaltexcoords; + //~ XYZ** decalvertex; + //~ int* decaltype; + //~ float* decalopacity; + //~ float* decalrotation; + //~ float* decalalivetime; + //~ XYZ* decalposition; + std::vector decals; /*float decaltexcoords[max_model_decals][3][2]; XYZ decalvertex[max_model_decals][3]; @@ -112,8 +113,8 @@ public: bool flat; void DeleteDecal(int which); - void MakeDecal(int atype, XYZ *where, float *size, float *opacity, float *rotation); - void MakeDecal(int atype, XYZ where, float size, float opacity, float rotation); + void MakeDecal(decal_type atype, XYZ *where, float *size, float *opacity, float *rotation); + void MakeDecal(decal_type atype, XYZ where, float size, float opacity, float rotation); void drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloodtexture2, Texture breaktexture); int SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate); int SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate); @@ -139,6 +140,7 @@ public: void drawdifftex(Texture texture); void drawimmediate(); void Rotate(float xang, float yang, float zang); + void deleteDeadDecals(); ~Model(); void deallocate(); Model(); -- 2.39.5