Changeset 7687 in orxonox.OLD for branches/water/src/lib/graphics
- Timestamp:
- May 18, 2006, 3:16:46 PM (19 years ago)
- Location:
- branches/water/src/lib/graphics/importer
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/water/src/lib/graphics/importer/material.cc
r7221 r7687 45 45 this->setTransparency(1.0); 46 46 47 this->diffuseTexture = NULL;48 47 this->ambientTexture = NULL; 49 48 this->specularTexture = NULL; … … 61 60 PRINTF(5)("delete Material %s.\n", this->getName()); 62 61 63 if (this->diffuseTexture != NULL) 64 { 65 ResourceManager::getInstance()->unload(this->diffuseTexture); 66 } 62 while(!this->textures.empty()) 63 { 64 if (this->textures.back() != NULL) 65 ResourceManager::getInstance()->unload(this->textures.back()); 66 this->textures.pop_back(); 67 } 68 67 69 if (this->ambientTexture != NULL) 68 70 ResourceManager::getInstance()->unload(this->ambientTexture); 69 71 if (this->specularTexture != NULL) 70 72 ResourceManager::getInstance()->unload(this->specularTexture); 71 } 72 73 Material& Material::operator=(const Material& m) 74 { 75 this->setIllum(m.illumModel); 76 this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]); 77 this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]); 78 this->setSpecular(m.specular[0],m.specular[1],m.specular[2]); 79 this->setShininess(m.shininess); 80 this->setTransparency(m.transparency); 81 82 if (this->diffuseTexture != NULL) 83 ResourceManager::getInstance()->unload(this->diffuseTexture); 84 if (m.diffuseTexture != NULL) 85 this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture); 86 this->ambientTexture = NULL; /// FIXME 87 this->specularTexture = NULL; /// FIXME 88 89 this->setName(m.getName()); 90 } 73 74 if (this == Material::selectedMaterial) 75 Material::selectedMaterial = NULL; 76 } 77 78 79 const Material* Material::selectedMaterial = NULL; 80 81 const GLenum Material::glTextureArbs[] = 82 { 83 GL_TEXTURE0, 84 GL_TEXTURE1, 85 GL_TEXTURE2, 86 GL_TEXTURE3, 87 GL_TEXTURE4, 88 GL_TEXTURE5, 89 GL_TEXTURE6, 90 GL_TEXTURE7 91 }; 92 93 94 /// TODO FIX THIS 95 // Material& Material::operator=(const Material& m) 96 // { 97 // this->setIllum(m.illumModel); 98 // this->setDiffuse(m.diffuse[0],m.diffuse[1],m.diffuse[2]); 99 // this->setAmbient(m.ambient[0],m.ambient[1],m.ambient[2]); 100 // this->setSpecular(m.specular[0],m.specular[1],m.specular[2]); 101 // this->setShininess(m.shininess); 102 // this->setTransparency(m.transparency); 103 // 104 // if (this->diffuseTexture != NULL) 105 // ResourceManager::getInstance()->unload(this->diffuseTexture); 106 // if (m.diffuseTexture != NULL) 107 // this->diffuseTexture = (Texture*)ResourceManager::getInstance()->copy(m.diffuseTexture); 108 // this->ambientTexture = NULL; /// FIXME 109 // this->specularTexture = NULL; /// FIXME 110 // 111 // this->setName(m.getName()); 112 // } 113 91 114 92 115 … … 94 117 * sets the material with which the following Faces will be painted 95 118 */ 96 bool Material::select () const 97 { 119 bool Material::select() const 120 { 121 if (unlikely(this == Material::selectedMaterial)) 122 return true; 123 124 98 125 // setting diffuse color 99 126 glColor4f (diffuse[0], diffuse[1], diffuse[2], this->transparency); 100 // glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse);101 102 127 // setting ambient color 103 128 glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); 104 105 129 // setting up Sprecular 106 130 glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); 107 108 131 // setting up Shininess 109 132 glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); 110 133 134 111 135 // setting the transparency 112 136 if (this->transparency < 1.0 || /* This allows alpha blending of 2D textures with the scene */ 113 ( this->diffuseTexture && this->diffuseTexture->hasAlpha()))137 (likely(!this->textures.empty() && this->textures[0] != NULL) && this->textures[0]->hasAlpha())) 114 138 { 115 139 glEnable(GL_BLEND); … … 117 141 } 118 142 else 119 { 120 glDisable(GL_BLEND); 121 //glColor4f(*(this->diffuse), *(this->diffuse+1), *(this->diffuse+2), 1); 122 } 143 { 144 glDisable(GL_BLEND); 145 } 123 146 124 147 … … 129 152 glShadeModel(GL_SMOOTH); 130 153 131 if (this->diffuseTexture != NULL) 154 if (likely(Material::selectedMaterial != NULL)) 155 { 156 for(unsigned int i = 0; i < Material::selectedMaterial->textures.size(); ++i) 132 157 { 158 glActiveTexture(Material::glTextureArbs[i]); 159 glBindTexture(GL_TEXTURE_2D, 0); 160 glDisable(GL_TEXTURE_2D); 161 } 162 } 163 164 for(unsigned int i = 0; i < this->textures.size(); ++i) 165 { 166 if (likely(this->textures[i] != NULL)) 167 { 168 glActiveTexture(Material::glTextureArbs[i]); 133 169 glEnable(GL_TEXTURE_2D); 134 glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); 170 if(this->textures[i]->hasAlpha()) 171 { 172 glEnable(GL_BLEND); 173 } 174 glBindTexture(GL_TEXTURE_2D, this->textures[i]->getTexture()); 135 175 } 136 else 137 { 138 glDisable(GL_TEXTURE_2D); 139 glBindTexture(GL_TEXTURE_2D, 0); 140 } 176 } 177 Material::selectedMaterial = this; 178 179 /* if (this->diffuseTexture != NULL) 180 { 181 glEnable(GL_TEXTURE_2D); 182 glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); 183 } 184 else 185 { 186 glDisable(GL_TEXTURE_2D); 187 glBindTexture(GL_TEXTURE_2D, 0); 188 }*/ 141 189 } 142 190 … … 162 210 /** 163 211 * Sets the Material Diffuse Color. 164 * @param r Red Color Channel. 212 * @param r Red Color Channel.a 165 213 * @param g Green Color Channel. 166 214 * @param b Blue Color Channel. … … 226 274 this->specular[2] = b; 227 275 this->specular[3] = 1.0; 228 276 } 229 277 230 278 /** … … 289 337 * @param dMap the Name of the Image to Use 290 338 */ 291 void Material::setDiffuseMap(const std::string& dMap, GLenum target) 292 { 339 void Material::setDiffuseMap(const std::string& dMap, GLenum target, unsigned int textureNumber) 340 { 341 assert(textureNumber < Material::getMaxTextureUnits()); 342 293 343 PRINTF(5)("setting Diffuse Map %s\n", dMap); 294 if (this->diffuseTexture != NULL) 295 ResourceManager::getInstance()->unload(this->diffuseTexture); 344 if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL) 345 ResourceManager::getInstance()->unload(this->textures[textureNumber]); 346 347 if (this->textures.size() <= textureNumber) 348 this->textures.resize(textureNumber+1, NULL); 296 349 297 350 //! @todo check if RESOURCE MANAGER is availiable 298 //! @todo Textures from .mtl-file need special care.299 351 if (!dMap.empty()) 300 this->diffuseTexture = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target); 352 { 353 354 this->textures[textureNumber] = (Texture*)ResourceManager::getInstance()->load(dMap, IMAGE, RP_GAME, (int)target); 355 } 301 356 else 302 this->diffuseTexture = NULL; 303 } 357 { 358 this->textures[textureNumber] = NULL; 359 } 360 } 361 362 /** 363 * Sets the Materials Diffuse Map 364 * @param surface pointer to SDL_Surface which shall be used as texture 365 */ 366 void Material::setSDLDiffuseMap(SDL_Surface *surface, GLenum target, unsigned int textureNumber) 367 { 368 assert(textureNumber < Material::getMaxTextureUnits()); 369 370 371 if (this->textures.size() > textureNumber && this->textures[textureNumber] != NULL) 372 ResourceManager::getInstance()->unload(this->textures[textureNumber]); 373 374 if (this->textures.size() <= textureNumber) 375 this->textures.resize(textureNumber+1, NULL); 376 377 if(surface != NULL) 378 { 379 this->textures[textureNumber] = new Texture(surface, GL_TEXTURE_2D); 380 } 381 else 382 { 383 this->textures[textureNumber] = NULL; 384 } 385 386 } 387 304 388 305 389 /** … … 332 416 void Material::setBump(const std::string& bump) 333 417 { 334 335 } 418 } 419 420 421 422 int Material::getMaxTextureUnits() 423 { 424 int maxTexUnits = 0; 425 glGetIntegerv(GL_MAX_TEXTURE_UNITS, &maxTexUnits); 426 return maxTexUnits; 427 } -
branches/water/src/lib/graphics/importer/material.h
r7221 r7687 10 10 #include "base_object.h" 11 11 12 12 13 #if HAVE_CONFIG_H 13 14 #include <config.h> 14 15 #endif /* HAVE_CONFIG_H */ 15 16 16 #ifndef NULL 17 #define NULL 0 //!< a pointer to NULL 18 #endif 17 #include <vector> 18 #include "SDL_image.h" 19 19 20 20 #include "texture.h" … … 25 25 class Material : public BaseObject 26 26 { 27 public:28 Material (const std::string& mtlName = "");29 virtual ~Material ();27 public: 28 Material (const std::string& mtlName = ""); 29 virtual ~Material (); 30 30 31 Material& operator=(const Material& material);31 Material& operator=(const Material& material); 32 32 33 bool select () const;33 bool select () const; 34 34 35 void setIllum (int illum); 36 void setIllum (char* illum); 37 int getIllumModel() const { return this->illumModel; }; 38 void setDiffuse (float r, float g, float b); 39 void setDiffuse (char* rgb); 40 void setAmbient (float r, float g, float b); 41 void setAmbient (char* rgb); 42 void setSpecular (float r, float g, float b); 43 void setSpecular (char* rgb); 44 void setShininess (float shini); 45 void setShininess (char* shini); 46 void setTransparency (float trans); 47 void setTransparency (char* trans); 48 void setBlendFunc(GLenum sFactor, GLenum tFactor) { this->sFactor = sFactor; this->tFactor = tFactor; }; 35 void setIllum (int illum); 36 int getIllumModel() const { return this->illumModel; }; 37 void setDiffuse (float r, float g, float b); 38 void setAmbient (float r, float g, float b); 39 void setSpecular (float r, float g, float b); 40 void setShininess (float shini); 41 void setTransparency (float trans); 42 void setBlendFunc(GLenum sFactor, GLenum tFactor) { this->sFactor = sFactor; this->tFactor = tFactor; }; 49 43 50 // MAPPING //51 void setDiffuseMap(const std::string& dMap, GLenum target = GL_TEXTURE_2D);52 void setAmbientMap(const std::string& aMap, GLenum target = GL_TEXTURE_2D);53 void setSpecularMap(const std::string& sMap, GLenum target = GL_TEXTURE_2D);54 void setBump(const std::string& bump);55 GLuint getDiffuseTexture() const { return (this->diffuseTexture)? this->diffuseTexture->getTexture() : 0; };56 44 57 static void addTexturePath(const std::string& pathName); 45 // TODO Move them out of here 46 void setIllum (char* illum); 47 void setDiffuse (char* rgb); 48 void setAmbient (char* rgb); 49 void setSpecular (char* rgb); 50 void setShininess (char* shini); 51 void setTransparency (char* trans); 52 53 54 // MAPPING // 55 void setDiffuseMap(const std::string& dMap, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0); 56 void setSDLDiffuseMap(SDL_Surface *surface, GLenum target = GL_TEXTURE_2D, unsigned int textureNumber = 0); 57 58 void setAmbientMap(const std::string& aMap, GLenum target = GL_TEXTURE_2D); 59 void setSpecularMap(const std::string& sMap, GLenum target = GL_TEXTURE_2D); 60 void setBump(const std::string& bump); 61 GLuint getDiffuseTexture(unsigned int i = 0) const { return (this->textures.size() > i)? this->textures[i]->getTexture() : 0; }; 62 63 static void addTexturePath(const std::string& pathName); 64 65 public: 66 static const GLenum glTextureArbs[]; //!< The Texture ARB's 67 68 static int getMaxTextureUnits(); 58 69 59 70 private: 60 int illumModel; //!< The IlluminationModel is either flat or smooth. 61 float diffuse [4]; //!< The diffuse color of the Material. 62 float ambient [4]; //!< The ambient color of the Material. 63 float specular [4]; //!< The specular color of the Material. 64 float shininess; //!< The shininess of the Material. 65 float transparency; //!< The transperency of the Material. 66 GLenum sFactor; 67 GLenum tFactor; 71 static const Material* selectedMaterial; //!< The currently selected material. 68 72 69 Texture* diffuseTexture; //!< The diffuse texture of the Material. 70 Texture* ambientTexture; //!< The ambient texture of the Material. 71 Texture* specularTexture; //!< The specular texture of the Material. 73 int illumModel; //!< The IlluminationModel is either flat or smooth. 74 float diffuse [4]; //!< The diffuse color of the Material. 75 float ambient [4]; //!< The ambient color of the Material. 76 float specular [4]; //!< The specular color of the Material. 77 float shininess; //!< The shininess of the Material. 78 float transparency; //!< The transperency of the Material. 79 GLenum sFactor; 80 GLenum tFactor; 81 82 std::vector<Texture*> textures; //!< An Array of Textures. 83 84 Texture* ambientTexture; //!< The ambient texture of the Material. 85 Texture* specularTexture; //!< The specular texture of the Material. 86 87 72 88 }; 73 89 #endif -
branches/water/src/lib/graphics/importer/texture.cc
r7221 r7687 34 34 Texture::Texture(const std::string& imageName, GLenum target) 35 35 { 36 this->init(); 37 38 if (!imageName.empty()) 39 { 40 this->setName(imageName); 41 this->loadImage(imageName, target); 42 } 43 } 44 45 Texture::Texture(SDL_Surface* surface, GLenum target) 46 { 47 this->init(); 48 49 if(surface != NULL) 50 { 51 this->loadSurface(surface, target); 52 53 } 54 } 55 56 void Texture::init() 57 { 36 58 this->setClassID(CL_TEXTURE, "Texture"); 37 59 … … 40 62 this->image = NULL; 41 63 this->priority = 0.5; 42 43 if (!imageName.empty()) 44 { 45 this->setName(imageName); 46 this->loadImage(imageName, target); 47 } 48 } 49 64 } 50 65 51 66 /** … … 64 79 65 80 /** 66 * loads an Image from a file to a Texture81 * @brief loads an Image from a file to a Texture 67 82 * @param imageName The image to load 68 83 */ … … 71 86 if (Texture::texturesEnabled) 72 87 { 73 if (this->image != NULL)74 {75 SDL_FreeSurface(this->image);76 this->image = NULL;77 }78 if (this->texture != 0)79 {80 glDeleteTextures(1, &this->texture);81 this->texture = 0;82 }83 88 if (!imageName.empty()) 84 89 { 85 90 SDL_Surface* tmpSurf; 86 if (this->texture != 0 && glIsTexture(this->texture)) 87 glDeleteTextures(1, &this->texture); 91 88 92 // load the new Image to memory 89 93 tmpSurf = IMG_Load(imageName.c_str()); 90 94 if(tmpSurf != NULL) 91 95 { 92 PRINTF(4)("loading Image %s\n", imageName); 93 bool hasAlpha; 94 SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha); 95 if (newSurf != NULL) 96 { 97 this->setSurface(newSurf); 98 this->setAlpha(hasAlpha); 99 this->setTexture(Texture::loadTexToGL(newSurf, target)); 100 } 101 96 this->loadSurface(tmpSurf, target); 102 97 SDL_FreeSurface(tmpSurf); 103 98 return true; … … 114 109 PRINTF(2)("Image-Name not specified\n"); 115 110 return false; 111 } 112 } 113 return false; 114 } 115 116 /** 117 * @brief Loads an SDL_Surface. 118 */ 119 bool Texture::loadSurface(SDL_Surface* surface, GLenum target) 120 { 121 if (Texture::texturesEnabled) 122 { 123 // some image was loaded before 124 if (this->image != NULL) 125 { 126 SDL_FreeSurface(this->image); 127 this->image = NULL; 128 } 129 130 // unload the old Texture. 131 if (this->texture != 0 && glIsTexture(this->texture)) 132 { 133 glDeleteTextures(1, &this->texture); 134 this->texture = 0; 135 } 136 137 bool hasAlpha; 138 SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha); 139 if (newSurf != NULL) 140 { 141 this->setSurface(newSurf); 142 this->setAlpha(hasAlpha); 143 this->setTexture(Texture::loadTexToGL(newSurf, target)); 144 return true; 116 145 } 117 146 } … … 264 293 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT); 265 294 266 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR /*_MIPMAP_LINEAR*/);295 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 267 296 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 268 297 -
branches/water/src/lib/graphics/importer/texture.h
r7221 r7687 19 19 public: 20 20 Texture(const std::string& imageName = "", GLenum target = GL_TEXTURE_2D); 21 // Texture(TEXTURE_TYPE type, int resolution);21 Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 22 22 virtual ~Texture(); 23 23 24 24 bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D); 25 bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 25 26 virtual bool rebuild(); 26 27 … … 28 29 inline GLuint getTexture() const { return this->texture; }; 29 30 /** @returns true if texture has alpha, false otherwise */ 30 inline bool hasAlpha() const {return bAlpha;}31 inline bool hasAlpha() const {return bAlpha; } 31 32 /** @returns the stored image of this Texture */ 32 33 const SDL_Surface* const getStoredImage() const { return this->image; }; … … 43 44 44 45 protected: 45 46 46 bool setSurface(SDL_Surface* newSurface); 47 47 bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; }; 48 48 bool setTexture(GLuint texture) { this->texture = texture; }; 49 49 50 private: 51 void init(); 50 52 51 53 private:
Note: See TracChangeset
for help on using the changeset viewer.