]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Models.cpp
Oops, committed by mistake a temporary commenting of code
[lugaru.git] / Source / Models.cpp
index 41d5d5cfd05d2b360e95f655fdcdab6cfd3f3be7..2c301689ea38f8cd3695e7ebd5693fc2d838ec0c 100644 (file)
@@ -1,26 +1,26 @@
 /*
 Copyright (C) 2003, 2010 - Wolfire Games
+Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
 
 This file is part of Lugaru.
 
-Lugaru is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
+Lugaru is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
+Lugaru is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-
-See the GNU General Public License for more details.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "Game.h"
 #include "Models.h"
+#include "Utils/Folders.h"
 
 extern float multiplier;
 extern float viewdistance;
@@ -66,43 +66,6 @@ int Model::LineCheck(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
     return firstintersecting;
 }
 
-int Model::LineCheckSlide(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
-{
-    static int j;
-    static float distance;
-    static float olddistance;
-    static int intersecting;
-    static int firstintersecting;
-    static XYZ point;
-
-    *p1 = *p1 - *move;
-    *p2 = *p2 - *move;
-    if (!sphere_line_intersection(p1, p2, &boundingspherecenter, &boundingsphereradius))
-        return -1;
-    firstintersecting = -1;
-    if (*rotate)
-        *p1 = DoRotation(*p1, 0, -*rotate, 0);
-    if (*rotate)
-        *p2 = DoRotation(*p2, 0, -*rotate, 0);
-
-    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);
-        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;
-            firstintersecting = j;
-        }
-    }
-
-    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;
-
-    if (*rotate)
-        *p2 = DoRotation(*p2, 0, *rotate, 0);
-    *p2 = *p2 + *move;
-    return firstintersecting;
-}
-
 int Model::LineCheckPossible(XYZ *p1, XYZ *p2, XYZ *p, XYZ *move, float *rotate)
 {
     static int j;
@@ -192,7 +155,6 @@ int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
     static int firstintersecting;
     static XYZ point;
     static XYZ oldp1;
-    static XYZ start, end;
 
     firstintersecting = -1;
 
@@ -219,12 +181,6 @@ int Model::SphereCheck(XYZ *p1, float radius, XYZ *p, XYZ *move, float *rotate)
                     intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
                 if (intersecting) {
                     *p1 += facenormals[j] * (distance - radius);
-                    /*start=*p1;
-                    end=*p1;
-                    end.y-=radius;
-                    if(LineFacetd(&start,&end,&vertex[Triangles[j].vertex[0]],&vertex[Triangles[j].vertex[1]],&vertex[Triangles[j].vertex[2]],&facenormals[j],&point)){
-                    p1->y=point.y+radius;
-                    }*/
                 }
             }
             if ((distance < olddistance || firstintersecting == -1) && intersecting) {
@@ -252,7 +208,6 @@ int Model::SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate)
     static int firstintersecting;
     static XYZ point;
     static XYZ oldp1;
-    static XYZ start, end;
 
     firstintersecting = -1;
 
@@ -282,7 +237,6 @@ int Model::SphereCheckPossible(XYZ *p1, float radius, XYZ *move, float *rotate)
             if (!intersecting)
                 intersecting = sphere_line_intersection(&vertex[Triangles[j].vertex[0]], &vertex[Triangles[j].vertex[2]], p1, &radius);
             if (intersecting) {
-                //if(j>=0&&j<TriangleNum)
                 possible[numpossible] = j;
                 numpossible++;
             }
@@ -446,19 +400,16 @@ void Model::UpdateVertexArrayNoTexNoNorm()
     }
 }
 
-bool Model::loadnotex(const char *filename )
+bool Model::loadnotex(const std::string& filename )
 {
     FILE *tfile;
     long i;
 
-    //~ int oldvertexNum, oldTriangleNum;
-    //~ oldvertexNum = vertexNum;
-    //~ oldTriangleNum = TriangleNum;
-
     type = notextype;
     color = 0;
 
-    tfile = fopen( ConvertFileName(filename), "rb" );
+    tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
+
     // read model settings
 
     fseek(tfile, 0, SEEK_SET);
@@ -480,7 +431,6 @@ bool Model::loadnotex(const char *filename )
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        //funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
         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];
@@ -510,11 +460,11 @@ bool Model::loadnotex(const char *filename )
     }
     boundingsphereradius = fast_sqrt(boundingsphereradius);
 
-    return 1;
+    return true;
 }
 
 
-bool Model::load(const char *filename, bool texture )
+bool Model::load(const std::string& filename, bool texture )
 {
     FILE *tfile;
     long i;
@@ -526,16 +476,12 @@ bool Model::load(const char *filename, bool texture )
     if (visibleloading)
         Game::LoadingScreen();
 
-    //~ int oldvertexNum, oldTriangleNum;
-    //~ oldvertexNum = vertexNum;
-    //~ oldTriangleNum = TriangleNum;
-
     type = normaltype;
     color = 0;
 
-    tfile = fopen( ConvertFileName(filename), "rb" );
-    // read model settings
+    tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
 
+    // read model settings
 
     fseek(tfile, 0, SEEK_SET);
     funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
@@ -558,7 +504,6 @@ bool Model::load(const char *filename, bool texture )
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        //funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
         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];
@@ -590,32 +535,25 @@ bool Model::load(const char *filename, bool texture )
     }
     boundingsphereradius = fast_sqrt(boundingsphereradius);
 
-    return 1;
+    return true;
 }
 
-bool Model::loaddecal(const char *filename, bool texture )
+bool Model::loaddecal(const std::string& filename, bool texture )
 {
     FILE *tfile;
     long i, j;
 
     LOGFUNC;
 
-    // Changing the filename so that its more os specific
-    char * FixedFN = ConvertFileName(filename);
-
-    LOG(std::string("Loading decal...") + FixedFN);
-
-    //~ int oldvertexNum, oldTriangleNum;
-    //~ oldvertexNum = vertexNum;
-    //~ oldTriangleNum = TriangleNum;
+    LOG(std::string("Loading decal...") + Folders::getResourcePath(filename));
 
     type = decalstype;
     numdecals = 0;
     color = 0;
 
-    tfile = fopen( FixedFN, "rb" );
-    // read model settings
+    tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
 
+    // read model settings
 
     fseek(tfile, 0, SEEK_SET);
     funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
@@ -640,7 +578,6 @@ bool Model::loaddecal(const char *filename, bool texture )
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        //funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
         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];
@@ -681,7 +618,6 @@ bool Model::loaddecal(const char *filename, bool texture )
                 decaltexcoords[i][j] = (float*)malloc(sizeof(float) * 2);
             }
         }
-        //if(decalvertex)free(decalvertex);
         decalvertex = (XYZ**)malloc(sizeof(XYZ*)*max_model_decals);
         for (i = 0; i < max_model_decals; i++) {
             decalvertex[i] = (XYZ*)malloc(sizeof(XYZ) * 3);
@@ -694,10 +630,10 @@ bool Model::loaddecal(const char *filename, bool texture )
         decalposition = (XYZ*)malloc(sizeof(XYZ) * max_model_decals);
     }
 
-    return 1;
+    return true;
 }
 
-bool Model::loadraw(char *filename )
+bool Model::loadraw(const std::string& filename)
 {
     FILE *tfile;
     long i;
@@ -706,16 +642,12 @@ bool Model::loadraw(char *filename )
 
     LOG(std::string("Loading raw...") + filename);
 
-    //~ int oldvertexNum, oldTriangleNum;
-    //~ oldvertexNum = vertexNum;
-    //~ oldTriangleNum = TriangleNum;
-
     type = rawtype;
     color = 0;
 
-    tfile = fopen( ConvertFileName(filename), "rb" );
-    // read model settings
+    tfile = Folders::openMandatoryFile( Folders::getResourcePath(filename), "rb" );
 
+    // read model settings
 
     fseek(tfile, 0, SEEK_SET);
     funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
@@ -737,7 +669,6 @@ bool Model::loadraw(char *filename )
     }
 
     for (i = 0; i < TriangleNum; i++) {
-        //funpackf(tfile, "Bi Bi Bi", &Triangles[i].vertex[0], &Triangles[i].vertex[1], &Triangles[i].vertex[2]);
         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];
@@ -754,7 +685,7 @@ bool Model::loadraw(char *filename )
         owner[i] = -1;
     }
 
-    return 1;
+    return true;
 }
 
 
@@ -928,11 +859,6 @@ void Model::drawimmediate()
     textureptr.bind();
     glBegin(GL_TRIANGLES);
     for (int i = 0; i < TriangleNum; i++) {
-        /*if(Triangles[i].vertex[0]<vertexNum&&Triangles[i].vertex[1]<vertexNum&&Triangles[i].vertex[2]<vertexNum&&Triangles[i].vertex[0]>=0&&Triangles[i].vertex[1]>=0&&Triangles[i].vertex[2]>=0){
-        if(isnormal(vertex[Triangles[i].vertex[0]].x)&&isnormal(vertex[Triangles[i].vertex[0]].y)&&isnormal(vertex[Triangles[i].vertex[0]].z)
-        &&isnormal(vertex[Triangles[i].vertex[1]].x)&&isnormal(vertex[Triangles[i].vertex[1]].y)&&isnormal(vertex[Triangles[i].vertex[1]].z)
-        &&isnormal(vertex[Triangles[i].vertex[2]].x)&&isnormal(vertex[Triangles[i].vertex[2]].y)&&isnormal(vertex[Triangles[i].vertex[2]].z)){
-        */
         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);
@@ -959,8 +885,6 @@ void Model::drawimmediate()
         if (!color && flat)
             glNormal3f(facenormals[i].x, facenormals[i].y, facenormals[i].y);
         glVertex3f(vertex[Triangles[i].vertex[2]].x, vertex[Triangles[i].vertex[2]].y, vertex[Triangles[i].vertex[2]].z);
-        //}
-        //}
     }
     glEnd();
 }
@@ -980,14 +904,7 @@ void Model::draw()
         glInterleavedArrays( GL_T2F_C3F_V3F, 8 * sizeof(GLfloat), &vArray[0]);
     textureptr.bind();
 
-#if PLATFORM_MACOSX
-    glLockArraysEXT( 0, TriangleNum * 3);
-#endif
     glDrawArrays(GL_TRIANGLES, 0, TriangleNum * 3);
-#if PLATFORM_MACOSX
-    glUnlockArraysEXT();
-#endif
-
 
     if (!color)
         glDisableClientState(GL_NORMAL_ARRAY);
@@ -995,7 +912,6 @@ void Model::draw()
         glDisableClientState(GL_COLOR_ARRAY);
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-    //drawimmediate();
 }
 
 //TODO: phase out in favor of Texture
@@ -1029,7 +945,6 @@ void Model::drawdifftex(GLuint texture)
         glDisableClientState(GL_COLOR_ARRAY);
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-    //drawdiffteximmediate(texture);
 }
 
 void Model::drawdifftex(Texture texture)
@@ -1062,49 +977,6 @@ void Model::drawdifftex(Texture texture)
         glDisableClientState(GL_COLOR_ARRAY);
     glDisableClientState(GL_VERTEX_ARRAY);
     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-    //drawdiffteximmediate(texture);
-}
-
-void Model::drawdiffteximmediate(GLuint texture)
-{
-    glBindTexture(GL_TEXTURE_2D, (unsigned long)texture);
-
-    glBegin(GL_TRIANGLES);
-    for (int i = 0; i < TriangleNum; i++) {
-        /*if(Triangles[i].vertex[0]<vertexNum&&Triangles[i].vertex[1]<vertexNum&&Triangles[i].vertex[2]<vertexNum&&Triangles[i].vertex[0]>=0&&Triangles[i].vertex[1]>=0&&Triangles[i].vertex[2]>=0){
-        if(isnormal(vertex[Triangles[i].vertex[0]].x)&&isnormal(vertex[Triangles[i].vertex[0]].y)&&isnormal(vertex[Triangles[i].vertex[0]].z)
-        &&isnormal(vertex[Triangles[i].vertex[1]].x)&&isnormal(vertex[Triangles[i].vertex[1]].y)&&isnormal(vertex[Triangles[i].vertex[1]].z)
-        &&isnormal(vertex[Triangles[i].vertex[2]].x)&&isnormal(vertex[Triangles[i].vertex[2]].y)&&isnormal(vertex[Triangles[i].vertex[2]].z)){
-        */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);
-        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]);
-        if (color)
-            glColor3f(normals[Triangles[i].vertex[1]].x, normals[Triangles[i].vertex[1]].y, normals[Triangles[i].vertex[1]].z);
-        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);
-        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]);
-        if (color)
-            glColor3f(normals[Triangles[i].vertex[2]].x, normals[Triangles[i].vertex[2]].y, normals[Triangles[i].vertex[2]].z);
-        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);
-        glVertex3f(vertex[Triangles[i].vertex[2]].x, vertex[Triangles[i].vertex[2]].y, vertex[Triangles[i].vertex[2]].z);
-        //}
-        //}
-    }
-    glEnd();
 }
 
 void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloodtexture2, Texture breaktexture)
@@ -1113,12 +985,9 @@ void Model::drawdecals(Texture shadowtexture, Texture bloodtexture, Texture bloo
         if (type != decalstype)
             return;
         static int i;
-        //~ static float distancemult;
         static int lasttype;
-        //~ static float viewdistsquared;
         static bool blend;
 
-        //~ viewdistsquared = viewdistance * viewdistance;
         blend = 1;
 
         lasttype = -1;
@@ -1237,7 +1106,6 @@ void Model::MakeDecal(int atype, XYZ *where, float *size, float *opacity, float
 
         static float placex, placez;
         static XYZ rot;
-        //static XYZ point,point1,point2;
         static float distance;
         static int i, j;
 
@@ -1316,7 +1184,6 @@ void Model::MakeDecal(int atype, XYZ where, float size, float opacity, float rot
 
         static float placex, placez;
         static XYZ rot;
-        //static XYZ point,point1,point2;
         static float distance;
         static int i, j;