X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=Source%2FModels.cpp;h=9f92cb531d2f5eeabc43b4396ba7e83eb5905f49;hb=3d9b1d366f1b3607c40eb5bcbbe631794ed899de;hp=e573423ba9eb674cc49b743c26a267cfe217fcd3;hpb=f250ee8330a466b35a7ba2975d3d3fed508068c2;p=lugaru.git diff --git a/Source/Models.cpp b/Source/Models.cpp index e573423..9f92cb5 100644 --- a/Source/Models.cpp +++ b/Source/Models.cpp @@ -21,7 +21,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Game.h" #include "Models.h" -//#include "altivec.h" extern float multiplier; extern float viewdistance; @@ -29,29 +28,8 @@ extern XYZ viewer; extern float fadestart; extern float texdetail; extern bool decals; -extern int loadscreencolor; -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) { @@ -195,7 +173,7 @@ int Model::SphereCheck(XYZ *p1,float radius, XYZ *p, XYZ *move, float *rotate) oldp1=*p1; *p1=*p1-*move; if(*rotate)*p1=DoRotation(*p1,0,-*rotate,0); - if(findDistancefast(p1,&boundingspherecenter)>radius*radius+boundingsphereradius*boundingsphereradius)return -1; + if(distsq(p1,&boundingspherecenter)>radius*radius+boundingsphereradius*boundingsphereradius)return -1; for(i=0;i<4;i++){ for (j=0;jradius*radius+boundingsphereradius*boundingsphereradius){*p1=oldp1; return -1;} + if(distsq(p1,&boundingspherecenter)>radius*radius+boundingsphereradius*boundingsphereradius){*p1=oldp1; return -1;} for (j=0;jboundingsphereradius){ - boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2; + if(j!=i&&distsq(&vertex[j],&vertex[i])/2>boundingsphereradius){ + boundingsphereradius=distsq(&vertex[j],&vertex[i])/2; boundingspherecenter=(vertex[i]+vertex[j])/2; } } @@ -508,10 +479,8 @@ bool Model::load(const 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; @@ -528,13 +497,6 @@ bool Model::load(const 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; @@ -562,7 +524,7 @@ bool Model::load(const 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); @@ -576,8 +538,8 @@ bool Model::load(const char *filename,bool texture ) boundingsphereradius=0; for(i=0;iboundingsphereradius){ - boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2; + if(j!=i&&distsq(&vertex[j],&vertex[i])/2>boundingsphereradius){ + boundingsphereradius=distsq(&vertex[j],&vertex[i])/2; boundingspherecenter=(vertex[i]+vertex[j])/2; } } @@ -616,13 +578,6 @@ bool Model::loaddecal(const 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; @@ -652,7 +607,7 @@ bool Model::loaddecal(const char *filename,bool texture ) } - Texture.xsz=0; + modelTexture.xsz=0; fclose(tfile); @@ -665,8 +620,8 @@ bool Model::loaddecal(const char *filename,bool texture ) boundingsphereradius=0; for(i=0;iboundingsphereradius){ - boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2; + if(j!=i&&distsq(&vertex[j],&vertex[i])/2>boundingsphereradius){ + boundingsphereradius=distsq(&vertex[j],&vertex[i])/2; boundingspherecenter=(vertex[i]+vertex[j])/2; } } @@ -722,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; @@ -821,8 +769,8 @@ void Model::Scale(float xscale,float yscale,float zscale) boundingsphereradius=0; for(i=0;iboundingsphereradius){ - boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2; + if(j!=i&&distsq(&vertex[j],&vertex[i])/2>boundingsphereradius){ + boundingsphereradius=distsq(&vertex[j],&vertex[i])/2; boundingspherecenter=(vertex[i]+vertex[j])/2; } } @@ -861,8 +809,8 @@ void Model::Translate(float xtrans,float ytrans,float ztrans) boundingsphereradius=0; for(i=0;iboundingsphereradius){ - boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2; + if(j!=i&&distsq(&vertex[j],&vertex[i])/2>boundingsphereradius){ + boundingsphereradius=distsq(&vertex[j],&vertex[i])/2; boundingspherecenter=(vertex[i]+vertex[j])/2; } } @@ -882,8 +830,8 @@ void Model::Rotate(float xang,float yang,float zang) boundingsphereradius=0; for(i=0;iboundingsphereradius){ - boundingsphereradius=findDistancefast(&vertex[j],&vertex[i])/2; + if(j!=i&&distsq(&vertex[j],&vertex[i])/2>boundingsphereradius){ + boundingsphereradius=distsq(&vertex[j],&vertex[i])/2; boundingspherecenter=(vertex[i]+vertex[j])/2; } } @@ -894,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; @@ -932,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=0&&Triangles[i].vertex[1]>=0&&Triangles[i].vertex[2]>=0){ @@ -973,7 +919,7 @@ 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(); #if PLATFORM_MACOSX glLockArraysEXT( 0, TriangleNum*3); @@ -991,6 +937,7 @@ void Model::draw() //drawimmediate(); } +//TODO: phase out in favor of Texture void Model::drawdifftex(GLuint texture) { glEnableClientState(GL_NORMAL_ARRAY); @@ -1004,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 @@ -1053,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; @@ -1077,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); @@ -1085,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); @@ -1093,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); @@ -1101,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); @@ -1175,7 +1151,7 @@ void Model::MakeDecal(int atype, XYZ *where,float *size, float *opacity, float * static int i,j; if(*opacity>0) - if(findDistancefast(where,&boundingspherecenter)<(boundingsphereradius+*size)*(boundingsphereradius+*size)) + if(distsq(where,&boundingspherecenter)<(boundingsphereradius+*size)*(boundingsphereradius+*size)) for(i=0;iy||vertex[Triangles[i].vertex[1]].yy||vertex[Triangles[i].vertex[2]].yy)){ decalposition[numdecals]=*where; @@ -1252,7 +1228,7 @@ void Model::MakeDecal(int atype, XYZ where,float size, float opacity, float rota static int i,j; if(opacity>0) - if(findDistancefast(&where,&boundingspherecenter)<(boundingsphereradius+size)*(boundingsphereradius+size)) + if(distsq(&where,&boundingspherecenter)<(boundingsphereradius+size)*(boundingsphereradius+size)) for(i=0;iabs(facenormals[i].x)&&abs(facenormals[i].y)>abs(facenormals[i].z)){ @@ -1439,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; @@ -1476,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; @@ -1489,26 +1464,26 @@ void Model::deallocate() { for(i=0;i