]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Models.cpp
major refactor of texture system
[lugaru.git] / Source / Models.cpp
index b1be7f596ee76cc8d491d8f11559be1f5f086cb7..95b93fe4c489d238a2efe1afbfd6e1b04fdc7885 100644 (file)
@@ -1,6 +1,26 @@
-#include "Models.h"
-//#include "altivec.h"
+/*
+Copyright (C) 2003, 2010 - Wolfire Games
+
+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.
+
+This program 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.
+
+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.
+*/
+
 #include "Game.h"
+#include "Models.h"
 
 extern float multiplier;
 extern float viewdistance;
@@ -8,30 +28,8 @@ extern XYZ viewer;
 extern float fadestart;
 extern float texdetail;
 extern bool decals;
-extern int loadscreencolor;
 
-#include "Game.h"
-extern Game * pgame;
 extern bool visibleloading;
-//Functions
-void *allocate_aligned(size_t pointer_size, size_t byte_alignment)
-{
-       uintptr_t pointer = (uintptr_t)malloc(pointer_size + byte_alignment + 1);
-       uintptr_t aligned_pointer = (pointer + byte_alignment + 1);
-       aligned_pointer -= (aligned_pointer % byte_alignment);
-       *(uint8_t *)(aligned_pointer - 1) = (aligned_pointer - pointer);
-       return (void *)aligned_pointer;
-}
-
-void free_aligned(void *aligned_pointer)
-{
-       free((uint8_t *)(aligned_pointer) - *((uint8_t *)(aligned_pointer) - 1));
-}
-
-void dealloc(void* param){
-       free(param);
-       param=0;
-}
 
 int Model::LineCheck(XYZ *p1,XYZ *p2, XYZ *p, XYZ *move, float *rotate)
 {
@@ -404,7 +402,7 @@ void Model::UpdateVertexArrayNoTexNoNorm(){
        }
 }
 
-bool Model::loadnotex(char *filename )
+bool Model::loadnotex(const char *filename )
 {
        FILE                    *tfile;
        long                            i;
@@ -416,20 +414,13 @@ bool Model::loadnotex(char *filename )
        type=notextype;
        color=0;
 
-       tfile=fopen( filename, "rb" );
+       tfile=fopen( ConvertFileName(filename), "rb" );
        // read model settings
 
        fseek(tfile, 0, SEEK_SET);
        funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
 
        // read the model data
-       /*if(owner)dealloc(owner);
-       if(possible)dealloc(possible);
-       if(vertex)dealloc(vertex);
-       if(normals)dealloc(normals);
-       if(facenormals)dealloc(facenormals);
-       if(Triangles)dealloc(Triangles);
-       if(vArray)dealloc(vArray);*/
        deallocate();
 
        numpossible=0;
@@ -479,7 +470,7 @@ bool Model::loadnotex(char *filename )
 }
 
 
-bool Model::load(char *filename,bool texture )
+bool Model::load(const char *filename,bool texture )
 {
        FILE                    *tfile;
        long                            i;
@@ -488,10 +479,8 @@ bool Model::load(char *filename,bool texture )
 
        LOG(std::string("Loading model...") + filename);
 
-       if(visibleloading){
-               loadscreencolor=2;
-               pgame->LoadingScreen();
-       }
+       if(visibleloading)
+               Game::LoadingScreen();
 
        int oldvertexNum,oldTriangleNum;
        oldvertexNum=vertexNum;
@@ -500,7 +489,7 @@ bool Model::load(char *filename,bool texture )
        type = normaltype;
        color=0;
 
-       tfile=fopen( filename, "rb" );
+       tfile=fopen( ConvertFileName(filename), "rb" );
        // read model settings
 
 
@@ -508,13 +497,6 @@ bool Model::load(char *filename,bool texture )
        funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
 
        // read the model data
-       /*if(owner)dealloc(owner);
-       if(possible)dealloc(possible);
-       if(vertex)dealloc(vertex);
-       if(normals)dealloc(normals);
-       if(facenormals)dealloc(facenormals);
-       if(Triangles)dealloc(Triangles);
-       if(vArray)dealloc(vArray);*/
        deallocate();
 
        numpossible=0;
@@ -542,7 +524,7 @@ bool Model::load(char *filename,bool texture )
                funpackf(tfile, "Bf Bf Bf", &Triangles[i].gy[0], &Triangles[i].gy[1], &Triangles[i].gy[2]);
        }
 
-       Texture.xsz=0;
+       modelTexture.xsz=0;
 
        fclose(tfile);
 
@@ -567,14 +549,17 @@ bool Model::load(char *filename,bool texture )
        return 1;
 }
 
-bool Model::loaddecal(char *filename,bool texture )
+bool Model::loaddecal(const char *filename,bool texture )
 {
        FILE                    *tfile;
        long                            i,j;
 
        LOGFUNC;
 
-       LOG(std::string("Loading decal...") + filename);
+       // Changing the filename so that its more os specific
+       char * FixedFN = ConvertFileName(filename);
+
+       LOG(std::string("Loading decal...") + FixedFN);
 
        int oldvertexNum,oldTriangleNum;
        oldvertexNum=vertexNum;
@@ -584,7 +569,7 @@ bool Model::loaddecal(char *filename,bool texture )
        numdecals=0;
        color=0;
 
-       tfile=fopen( filename, "rb" );
+       tfile=fopen( FixedFN, "rb" );
        // read model settings
 
 
@@ -593,13 +578,6 @@ bool Model::loaddecal(char *filename,bool texture )
 
        // read the model data
 
-       /*if(owner)dealloc(owner);
-       if(possible)dealloc(possible);
-       if(vertex)dealloc(vertex);
-       if(normals)dealloc(normals);
-       if(facenormals)dealloc(facenormals);
-       if(Triangles)dealloc(Triangles);
-       if(vArray)dealloc(vArray);*/
        deallocate();
 
        numpossible=0;
@@ -629,7 +607,7 @@ bool Model::loaddecal(char *filename,bool texture )
        }
 
 
-       Texture.xsz=0;
+       modelTexture.xsz=0;
 
        fclose(tfile);
 
@@ -691,7 +669,7 @@ bool Model::loadraw(char *filename )
        type = rawtype;
        color=0;
 
-       tfile=fopen( filename, "rb" );
+       tfile=fopen( ConvertFileName(filename), "rb" );
        // read model settings
 
 
@@ -699,13 +677,6 @@ bool Model::loadraw(char *filename )
        funpackf(tfile, "Bs Bs", &vertexNum, &TriangleNum);
 
        // read the model data
-       /*if(owner)dealloc(owner);
-       if(possible)dealloc(possible);
-       if(vertex)dealloc(vertex);
-       if(normals)dealloc(normals);
-       if(facenormals)dealloc(facenormals);
-       if(Triangles)dealloc(Triangles);
-       if(vArray)dealloc(vArray);*/
        deallocate();
 
        numpossible=0;
@@ -871,10 +842,8 @@ void Model::Rotate(float xang,float yang,float zang)
 
 void Model::CalculateNormals(bool facenormalise)
 {
-       if(visibleloading){
-               loadscreencolor=3;
-               pgame->LoadingScreen();
-       }
+       if(visibleloading)
+               Game::LoadingScreen();
        static int i;
        if(type!=normaltype&&type!=decalstype)return;
 
@@ -909,7 +878,7 @@ void Model::CalculateNormals(bool facenormalise)
 
 void Model::drawimmediate()
 {
-       glBindTexture(GL_TEXTURE_2D,(unsigned long)textureptr);
+       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){
@@ -950,13 +919,13 @@ void Model::draw()
 
        if(!color)glInterleavedArrays( GL_T2F_N3F_V3F,8*sizeof(GLfloat),&vArray[0]);
        if(color)glInterleavedArrays( GL_T2F_C3F_V3F,8*sizeof(GLfloat),&vArray[0]);
-       glBindTexture(GL_TEXTURE_2D,(unsigned long)textureptr);
+       textureptr.bind();
 
-#ifndef WIN32
+#if PLATFORM_MACOSX
        glLockArraysEXT( 0, TriangleNum*3);
 #endif
        glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
-#ifndef WIN32
+#if PLATFORM_MACOSX
        glUnlockArraysEXT();
 #endif
 
@@ -968,6 +937,7 @@ void Model::draw()
        //drawimmediate();
 }
 
+//TODO: phase out in favor of Texture
 void Model::drawdifftex(GLuint texture)
 {
        glEnableClientState(GL_NORMAL_ARRAY);
@@ -981,6 +951,35 @@ void Model::drawdifftex(GLuint texture)
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
 
 
+#ifndef WIN32
+       glLockArraysEXT( 0, TriangleNum*3);
+#endif
+       glDrawArrays(GL_TRIANGLES, 0, TriangleNum*3);
+#ifndef WIN32
+       glUnlockArraysEXT();
+#endif
+
+
+       if(!color)glDisableClientState(GL_NORMAL_ARRAY);
+       if(color)glDisableClientState(GL_COLOR_ARRAY);
+       glDisableClientState(GL_VERTEX_ARRAY);
+       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+       //drawdiffteximmediate(texture);
+}
+
+void Model::drawdifftex(Texture texture)
+{
+       glEnableClientState(GL_NORMAL_ARRAY);
+       glEnableClientState(GL_VERTEX_ARRAY);
+       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+       if(!color)glInterleavedArrays( GL_T2F_N3F_V3F,8*sizeof(GLfloat),&vArray[0]);
+       if(color)glInterleavedArrays( GL_T2F_C3F_V3F,8*sizeof(GLfloat),&vArray[0]);
+
+       texture.bind();
+       glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
+       glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
+
+
 #ifndef WIN32
        glLockArraysEXT( 0, TriangleNum*3);
 #endif
@@ -1030,7 +1029,7 @@ void Model::drawdiffteximmediate(GLuint texture)
        glEnd();
 }
 
-void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtexture2,GLuint breaktexture)
+void Model::drawdecals(Texture shadowtexture,Texture bloodtexture,Texture bloodtexture2,Texture breaktexture)
 {
        if(decals){
                if(type!=decalstype)return;
@@ -1054,7 +1053,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext
                        if(decaltype[i]==blooddecalfast&&decalalivetime[i]<2)decalalivetime[i]=2;
 
                        if(decaltype[i]==shadowdecal&&decaltype[i]!=lasttype){
-                               glBindTexture( GL_TEXTURE_2D, shadowtexture);
+                               shadowtexture.bind();
                                if(!blend){
                                        blend=1;
                                        glAlphaFunc(GL_GREATER, 0.0001);
@@ -1062,7 +1061,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext
                                }
                        }
                        if(decaltype[i]==breakdecal&&decaltype[i]!=lasttype){
-                               glBindTexture( GL_TEXTURE_2D, breaktexture);
+                               breaktexture.bind();
                                if(!blend){
                                        blend=1;
                                        glAlphaFunc(GL_GREATER, 0.0001);
@@ -1070,7 +1069,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext
                                }
                        }
                        if((decaltype[i]==blooddecal||decaltype[i]==blooddecalslow)&&decaltype[i]!=lasttype){
-                               glBindTexture( GL_TEXTURE_2D, bloodtexture);
+                               bloodtexture.bind();
                                if(blend){
                                        blend=0;
                                        glAlphaFunc(GL_GREATER, 0.15);
@@ -1078,7 +1077,7 @@ void Model::drawdecals(GLuint shadowtexture,GLuint bloodtexture,GLuint bloodtext
                                }
                        }
                        if((decaltype[i]==blooddecalfast)&&decaltype[i]!=lasttype){
-                               glBindTexture( GL_TEXTURE_2D, bloodtexture2);
+                               bloodtexture2.bind();
                                if(blend){
                                        blend=0;
                                        glAlphaFunc(GL_GREATER, 0.15);
@@ -1416,33 +1415,32 @@ void Model::MakeDecal(int atype, XYZ where,float size, float opacity, float rota
 Model::~Model()
 {
        deallocate();
-
-       if(textureptr) glDeleteTextures( 1, &textureptr );
+    textureptr.destroy();
 }
 
 void Model::deallocate()
 {
        int i = 0, j = 0;
 
-       if(owner)dealloc(owner);
+       if(owner)free(owner);
        owner = 0;
 
-       if(possible)dealloc(possible);
+       if(possible)free(possible);
        possible = 0;
 
-       if(vertex)dealloc(vertex);
+       if(vertex)free(vertex);
        vertex = 0;
 
-       if(normals)dealloc(normals);
+       if(normals)free(normals);
        normals = 0;
 
-       if(facenormals)dealloc(facenormals);
+       if(facenormals)free(facenormals);
        facenormals = 0;
 
-       if(Triangles)dealloc(Triangles);
+       if(Triangles)free(Triangles);
        Triangles = 0;
 
-       if(vArray)dealloc(vArray);
+       if(vArray)free(vArray);
        vArray = 0;
 
 
@@ -1453,11 +1451,11 @@ void Model::deallocate()
                {
                        for(j=0;j<3;j++)
                        {
-                               dealloc(decaltexcoords[i][j]);
+                               free(decaltexcoords[i][j]);
                        }
-                       dealloc(decaltexcoords[i]);
+                       free(decaltexcoords[i]);
                }
-               dealloc(decaltexcoords);
+               free(decaltexcoords);
        }
        decaltexcoords = 0;
 
@@ -1466,26 +1464,26 @@ void Model::deallocate()
        {
                for(i=0;i<max_model_decals;i++)
                {
-                       dealloc(decalvertex[i]);
+                       free(decalvertex[i]);
                }
-               dealloc(decalvertex);
+               free(decalvertex);
        }
        decalvertex = 0;
 
 
-       dealloc(decaltype);
+       free(decaltype);
        decaltype = 0;
 
-       dealloc(decalopacity);
+       free(decalopacity);
        decalopacity = 0;
 
-       dealloc(decalrotation);
+       free(decalrotation);
        decalrotation = 0;
 
-       dealloc(decalalivetime);
+       free(decalalivetime);
        decalalivetime = 0;
 
-       dealloc(decalposition);
+       free(decalposition);
        decalposition = 0;
 
 };
@@ -1505,8 +1503,7 @@ Model::Model()
        Triangles=0;
        vArray=0;
 
-       textureptr = 0;
-       memset(&Texture, 0, sizeof(Texture));
+       memset(&modelTexture, 0, sizeof(modelTexture));
        numpossible = 0;
        color = 0;
 
@@ -1527,3 +1524,4 @@ Model::Model()
 
        type=nothing;
 }
+