home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Material.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  6.2 KB  |  265 lines

  1. #include "Material.h"
  2.  
  3. #include "log.h"
  4. #include "vectormath.h"
  5. //#include "Light.h"
  6. #include "RendererInfo.h"
  7. #include "Renderer.h"
  8. #include "ShaderHandler.h"
  9. #include "TextureHandler.h"
  10.  
  11. Material::Material(){
  12.  
  13.     vectorInit4d(1.0, 1.0, 1.0, 1.0, color);
  14.  
  15.     texture=NULL;
  16.     lightmap=NULL;
  17.     shader=NULL;
  18.  
  19.     isTransparent=false;
  20.     hasTexture=false;
  21.     hasLightmap=false;
  22.     hasShader=false;
  23.     isTwoSided=false;
  24.  
  25.     collisionFlags = COLLISION_FLAG_NONE;
  26.     surfaceType = MATERIAL_SURFACE_TYPE_METAL;
  27. }
  28.  
  29. Material::Material(File* f){
  30.  
  31.     vectorInit4d(1.0, 1.0, 1.0, 1.0, color);
  32.  
  33.     texture=NULL;
  34.     lightmap=NULL;
  35.     shader=NULL;
  36.  
  37.     isTransparent=false;
  38.     hasTexture=false;
  39.     hasLightmap=false;
  40.     hasShader=false;
  41.     isTwoSided=false;
  42.  
  43.     collisionFlags = COLLISION_FLAG_NONE;
  44.     surfaceType = MATERIAL_SURFACE_TYPE_METAL;
  45.  
  46.     if( !readFromFile(f) )
  47.         error("(in Material::Material()): readFromFile() returned false.\n\n");
  48.  
  49. }
  50.  
  51. Material::~Material(){
  52.     clearMaterial();
  53. }
  54.  
  55. void Material::clearMaterial(){
  56.  
  57.     if(hasTexture){
  58.         //TextureHandler::releaseTexture(texture);
  59.         texture=NULL;
  60.     }
  61.     if(hasLightmap){
  62.         //TextureHandler::releaseTexture(lightmap);
  63.         lightmap=NULL;
  64.     }
  65.     if(hasShader){
  66.         ShaderHandler::releaseShader(shader);
  67.         shader=NULL;
  68.     }
  69. }
  70.  
  71. void Material::setup(){
  72.     if(!hasShader){
  73.         glColor4fv(color);
  74.  
  75.         if(isTwoSided)
  76.             glDisable(GL_CULL_FACE);
  77.  
  78.         if(isTransparent){
  79.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  80.             glEnable(GL_BLEND);
  81.         }
  82.  
  83.         if(hasTexture){
  84.             // use only lightmaps when hasTexture
  85.             if(Renderer::info.var.lightingMode==LIGHTING_MODE_LIGHTMAP && hasLightmap && _glActiveTextureARB!=NULL){
  86.                 _glActiveTextureARB(GL_TEXTURE1_ARB);
  87.                 glEnable(GL_TEXTURE_2D);
  88.                 glBindTexture(GL_TEXTURE_2D,  lightmap->texName);
  89.                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  90.                 _glActiveTextureARB(GL_TEXTURE0_ARB);
  91.             }
  92.             glEnable(GL_TEXTURE_2D);
  93.             glBindTexture(GL_TEXTURE_2D, texture->texName);
  94.             glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  95.         }
  96.  
  97.     }else{
  98.         shader->setup(SDL_GetTicks());
  99.         // FIXME: lightmap!!
  100.  
  101.     }
  102. }
  103.  
  104. void Material::setdown(){
  105.     if(!hasShader){
  106.         if(isTwoSided)
  107.             glEnable(GL_CULL_FACE);
  108.  
  109.         if(hasTexture){
  110.             // use only lightmaps when hasTexture
  111.             if(Renderer::info.var.lightingMode==LIGHTING_MODE_LIGHTMAP && hasLightmap && _glActiveTextureARB!=NULL){
  112.                 _glActiveTextureARB(GL_TEXTURE1_ARB);
  113.                 glDisable(GL_TEXTURE_2D);
  114.                 _glActiveTextureARB(GL_TEXTURE0_ARB);
  115.             }
  116.             glDisable(GL_TEXTURE_2D);
  117.         }
  118.         if(isTransparent){
  119.             glDisable(GL_BLEND);
  120.         }
  121.     }else{
  122.         shader->setdown();
  123.     }
  124.  
  125. }
  126.  
  127. int Material::addTexture(Texture* tex){
  128.     texture=tex;
  129.     return 0;
  130. }
  131. int Material::addLightmap(Texture* lm){
  132.     lightmap=lm;
  133.     return 0;
  134. }
  135.  
  136. int Material::addShader(Shader* sha){
  137.     shader=sha;
  138.     return 0;
  139. }
  140.  
  141. void Material::setAttribs(){
  142.  
  143.     isTransparent=false;
  144.     hasTexture=false;
  145.     hasLightmap=false;
  146.     hasShader=false;
  147.  
  148.     if(color[3]<0.99f){
  149.         isTransparent=true;
  150. //        printf("ambient[3]: %f    diffuse[3]: %f      specular[3]: %f    emissive[3]: %f\n", ambient[3], diffuse[3], specular[3], emissive[3]);
  151.     }
  152.  
  153.  
  154.     if(texture!=NULL){
  155.         hasTexture=true;
  156.         if(texture->hasAlpha){
  157.             isTransparent=true;
  158.         }
  159.     }
  160.     if(lightmap!=NULL){
  161.         hasLightmap=true;
  162.         if(lightmap->hasAlpha){
  163.             isTransparent=true;
  164.         }
  165.     }
  166.  
  167.     if(shader!=NULL){
  168.         hasShader=true;
  169.         // alles nochmal setzen
  170.         if(shader->base!=NULL){
  171.             if(shader->base->flags & SHADER_FLAG_BLEND){
  172.                 isTransparent=true;
  173.             }
  174.  
  175.             collisionFlags = shader->base->collisionFlags;
  176.             surfaceType = shader->base->surfaceType;
  177.         }
  178.         if(shader->base!=NULL && shader->base->flags & SHADER_FLAG_TWO_SIDED)
  179.             isTwoSided=true;
  180.     }
  181.  
  182. }
  183.  
  184. bool Material::readFromFile(File* f){
  185.     char buff[256];
  186.     Tokenizer t(" =\t\n\r\"|", "\"");
  187.  
  188.     while(f->readLine(256, buff, true) != -1){
  189.         
  190.         //printf("Model: line: %s\n", buff);
  191.         t.tokenize(buff);
  192.  
  193.         if(t.tokc==0)
  194.             continue;
  195.  
  196.         if(streq(t.tokv[0], "NAME")){
  197.             // ignore name
  198.         }else if(streq(t.tokv[0], "SURFACE_TYPE")){
  199.             if( streq(t.tokv[1], "METAL") ){
  200.                 this->surfaceType = MATERIAL_SURFACE_TYPE_METAL;
  201.             }else if( streq(t.tokv[1], "STONE") ){
  202.                 this->surfaceType = MATERIAL_SURFACE_TYPE_STONE;
  203.             }else if( streq(t.tokv[1], "WOOD") ){
  204.                 this->surfaceType = MATERIAL_SURFACE_TYPE_WOOD;
  205.             }else if( streq(t.tokv[1], "DIRT") ){
  206.                 this->surfaceType = MATERIAL_SURFACE_TYPE_DIRT;
  207.             }else{
  208.                 warn("(in Material::readFromFile() (%s, line %i)): Unknown surface type '%s'.\n\n", f->filename, f->line, t.tokv[1]);
  209.             }
  210.         }else if(streq(t.tokv[0], "COLLISION_FLAGS")){
  211.             for(int i=1;i<t.tokc;i++){
  212.                 if(streq(t.tokv[i], "WALK_THROUGH")){
  213.                     this->collisionFlags |= COLLISION_FLAG_WALK_THROUGH;
  214.                 }else if(streq(t.tokv[i], "SHOOT_THROUGH")){
  215.                     this->collisionFlags |= COLLISION_FLAG_SHOOT_THROUGH;
  216.                 }else if(streq(t.tokv[i], "NONE")){
  217.                     this->collisionFlags = COLLISION_FLAG_NONE;
  218.                 }else{
  219.                     warn("(in Materials::readFromFile() (%s, line %i)): Unknown collisionFlag '%s'.\n\n", f->filename, f->line, t.tokv[i]);
  220.                 }
  221.             }
  222.  
  223.         }else if(streq(t.tokv[0], "COLOR")){
  224.             sscanf(t.tokv[1], "%f %f %f %f", &this->color[0], &this->color[1], &this->color[2], &this->color[3]);
  225.         }else if(streq(t.tokv[0], "TEXTURE")){
  226.             char p[256];
  227.             char* path = File::extractPath(f->filename);
  228.             strcpy(p, path);
  229.             delete[] path;
  230.  
  231.             strcat(p, t.tokv[1]);
  232.             if(ShaderHandler::hasShaderForImageFile(p))
  233.                 this->addShader(ShaderHandler::getShader(p));
  234.             else
  235.                 this->addTexture(TextureHandler::getTexture(p));
  236.         }else if(streq(t.tokv[0], "LIGHTMAP")){
  237.             char p[256];
  238.             char* path = File::extractPath(f->filename);
  239.             strcpy(p, path);
  240.             delete[] path;
  241.  
  242.             strcat(p, t.tokv[1]);
  243.             this->addLightmap(TextureHandler::getTexture(p));
  244.             
  245.         }else if(streq(t.tokv[0], "{")){
  246.             continue;
  247.         }else if(streq(t.tokv[0], "}")){
  248.             break;//return true;
  249.         }else{
  250.             warn("(in Material::readFromFile() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
  251.         }
  252.     }
  253.  
  254.     this->setAttribs();
  255.  
  256.     return true;
  257. }
  258.  
  259. void Material::dump(){
  260. //    printf("Material: numTextures: %i, numShaders: %i\n", numTextures, numShaders);
  261.     printf("          color:[%f %f %f %f]\n", color[0], color[1], color[2], color[3]);
  262. }
  263.  
  264.  
  265.