home *** CD-ROM | disk | FTP | other *** search
- #include "Material.h"
-
- #include "log.h"
- #include "vectormath.h"
- //#include "Light.h"
- #include "RendererInfo.h"
- #include "Renderer.h"
- #include "ShaderHandler.h"
- #include "TextureHandler.h"
-
- Material::Material(){
-
- vectorInit4d(1.0, 1.0, 1.0, 1.0, color);
-
- texture=NULL;
- lightmap=NULL;
- shader=NULL;
-
- isTransparent=false;
- hasTexture=false;
- hasLightmap=false;
- hasShader=false;
- isTwoSided=false;
-
- collisionFlags = COLLISION_FLAG_NONE;
- surfaceType = MATERIAL_SURFACE_TYPE_METAL;
- }
-
- Material::Material(File* f){
-
- vectorInit4d(1.0, 1.0, 1.0, 1.0, color);
-
- texture=NULL;
- lightmap=NULL;
- shader=NULL;
-
- isTransparent=false;
- hasTexture=false;
- hasLightmap=false;
- hasShader=false;
- isTwoSided=false;
-
- collisionFlags = COLLISION_FLAG_NONE;
- surfaceType = MATERIAL_SURFACE_TYPE_METAL;
-
- if( !readFromFile(f) )
- error("(in Material::Material()): readFromFile() returned false.\n\n");
-
- }
-
- Material::~Material(){
- clearMaterial();
- }
-
- void Material::clearMaterial(){
-
- if(hasTexture){
- //TextureHandler::releaseTexture(texture);
- texture=NULL;
- }
- if(hasLightmap){
- //TextureHandler::releaseTexture(lightmap);
- lightmap=NULL;
- }
- if(hasShader){
- ShaderHandler::releaseShader(shader);
- shader=NULL;
- }
- }
-
- void Material::setup(){
- if(!hasShader){
- glColor4fv(color);
-
- if(isTwoSided)
- glDisable(GL_CULL_FACE);
-
- if(isTransparent){
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_BLEND);
- }
-
- if(hasTexture){
- // use only lightmaps when hasTexture
- if(Renderer::info.var.lightingMode==LIGHTING_MODE_LIGHTMAP && hasLightmap && _glActiveTextureARB!=NULL){
- _glActiveTextureARB(GL_TEXTURE1_ARB);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, lightmap->texName);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- _glActiveTextureARB(GL_TEXTURE0_ARB);
- }
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, texture->texName);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- }
-
- }else{
- shader->setup(SDL_GetTicks());
- // FIXME: lightmap!!
-
- }
- }
-
- void Material::setdown(){
- if(!hasShader){
- if(isTwoSided)
- glEnable(GL_CULL_FACE);
-
- if(hasTexture){
- // use only lightmaps when hasTexture
- if(Renderer::info.var.lightingMode==LIGHTING_MODE_LIGHTMAP && hasLightmap && _glActiveTextureARB!=NULL){
- _glActiveTextureARB(GL_TEXTURE1_ARB);
- glDisable(GL_TEXTURE_2D);
- _glActiveTextureARB(GL_TEXTURE0_ARB);
- }
- glDisable(GL_TEXTURE_2D);
- }
- if(isTransparent){
- glDisable(GL_BLEND);
- }
- }else{
- shader->setdown();
- }
-
- }
-
- int Material::addTexture(Texture* tex){
- texture=tex;
- return 0;
- }
- int Material::addLightmap(Texture* lm){
- lightmap=lm;
- return 0;
- }
-
- int Material::addShader(Shader* sha){
- shader=sha;
- return 0;
- }
-
- void Material::setAttribs(){
-
- isTransparent=false;
- hasTexture=false;
- hasLightmap=false;
- hasShader=false;
-
- if(color[3]<0.99f){
- isTransparent=true;
- // printf("ambient[3]: %f diffuse[3]: %f specular[3]: %f emissive[3]: %f\n", ambient[3], diffuse[3], specular[3], emissive[3]);
- }
-
-
- if(texture!=NULL){
- hasTexture=true;
- if(texture->hasAlpha){
- isTransparent=true;
- }
- }
- if(lightmap!=NULL){
- hasLightmap=true;
- if(lightmap->hasAlpha){
- isTransparent=true;
- }
- }
-
- if(shader!=NULL){
- hasShader=true;
- // alles nochmal setzen
- if(shader->base!=NULL){
- if(shader->base->flags & SHADER_FLAG_BLEND){
- isTransparent=true;
- }
-
- collisionFlags = shader->base->collisionFlags;
- surfaceType = shader->base->surfaceType;
- }
- if(shader->base!=NULL && shader->base->flags & SHADER_FLAG_TWO_SIDED)
- isTwoSided=true;
- }
-
- }
-
- bool Material::readFromFile(File* f){
- char buff[256];
- Tokenizer t(" =\t\n\r\"|", "\"");
-
- while(f->readLine(256, buff, true) != -1){
-
- //printf("Model: line: %s\n", buff);
- t.tokenize(buff);
-
- if(t.tokc==0)
- continue;
-
- if(streq(t.tokv[0], "NAME")){
- // ignore name
- }else if(streq(t.tokv[0], "SURFACE_TYPE")){
- if( streq(t.tokv[1], "METAL") ){
- this->surfaceType = MATERIAL_SURFACE_TYPE_METAL;
- }else if( streq(t.tokv[1], "STONE") ){
- this->surfaceType = MATERIAL_SURFACE_TYPE_STONE;
- }else if( streq(t.tokv[1], "WOOD") ){
- this->surfaceType = MATERIAL_SURFACE_TYPE_WOOD;
- }else if( streq(t.tokv[1], "DIRT") ){
- this->surfaceType = MATERIAL_SURFACE_TYPE_DIRT;
- }else{
- warn("(in Material::readFromFile() (%s, line %i)): Unknown surface type '%s'.\n\n", f->filename, f->line, t.tokv[1]);
- }
- }else if(streq(t.tokv[0], "COLLISION_FLAGS")){
- for(int i=1;i<t.tokc;i++){
- if(streq(t.tokv[i], "WALK_THROUGH")){
- this->collisionFlags |= COLLISION_FLAG_WALK_THROUGH;
- }else if(streq(t.tokv[i], "SHOOT_THROUGH")){
- this->collisionFlags |= COLLISION_FLAG_SHOOT_THROUGH;
- }else if(streq(t.tokv[i], "NONE")){
- this->collisionFlags = COLLISION_FLAG_NONE;
- }else{
- warn("(in Materials::readFromFile() (%s, line %i)): Unknown collisionFlag '%s'.\n\n", f->filename, f->line, t.tokv[i]);
- }
- }
-
- }else if(streq(t.tokv[0], "COLOR")){
- sscanf(t.tokv[1], "%f %f %f %f", &this->color[0], &this->color[1], &this->color[2], &this->color[3]);
- }else if(streq(t.tokv[0], "TEXTURE")){
- char p[256];
- char* path = File::extractPath(f->filename);
- strcpy(p, path);
- delete[] path;
-
- strcat(p, t.tokv[1]);
- if(ShaderHandler::hasShaderForImageFile(p))
- this->addShader(ShaderHandler::getShader(p));
- else
- this->addTexture(TextureHandler::getTexture(p));
- }else if(streq(t.tokv[0], "LIGHTMAP")){
- char p[256];
- char* path = File::extractPath(f->filename);
- strcpy(p, path);
- delete[] path;
-
- strcat(p, t.tokv[1]);
- this->addLightmap(TextureHandler::getTexture(p));
-
- }else if(streq(t.tokv[0], "{")){
- continue;
- }else if(streq(t.tokv[0], "}")){
- break;//return true;
- }else{
- warn("(in Material::readFromFile() (%s, line %i)): Unknown token '%s'.\n\n", f->filename, f->line, t.tokv[0]);
- }
- }
-
- this->setAttribs();
-
- return true;
- }
-
- void Material::dump(){
- // printf("Material: numTextures: %i, numShaders: %i\n", numTextures, numShaders);
- printf(" color:[%f %f %f %f]\n", color[0], color[1], color[2], color[3]);
- }
-
-
-