- Timestamp:
- May 24, 2006, 2:49:58 AM (19 years ago)
- Location:
- branches/water/src/lib/graphics/importer
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/water/src/lib/graphics/importer/material.h
r7700 r7783 1 1 /*! 2 \file material.h3 \brief Contains the Material Class that handles Material for 3D-Objects.4 @todo free SDL-surface when deleting Material.5 @todo delete imgNameWithPath after use creation.6 */2 * @file material.h 3 * @brief Contains the Material Class that handles Material for 3D-Objects. 4 * @todo free SDL-surface when deleting Material. 5 * @todo delete imgNameWithPath after use creation. 6 */ 7 7 8 8 #ifndef _MATERIAL_H -
branches/water/src/lib/graphics/importer/texture.cc
r7700 r7783 20 20 #include "debug.h" 21 21 #include "compiler.h" 22 #include <math.h>23 22 24 23 // INCLUDING SDL_Image … … 30 29 31 30 32 33 34 Texture::Texture(GLenum target) 35 { 36 this->init(); 37 this->generateTexture(texture, target); 38 } 39 40 /** 41 * Constructor for a Texture 42 */ 43 Texture::Texture(const std::string& imageName, GLenum target) 44 { 45 this->init(); 46 47 if (!imageName.empty()) 48 { 49 this->setName(imageName); 50 this->loadImage(imageName, target); 51 } 52 } 53 54 55 56 Texture::Texture(SDL_Surface* surface, GLenum target) 57 { 58 this->init(); 59 60 if(surface != NULL) 61 { 62 this->loadSurface(surface, target); 63 64 } 65 } 66 67 void Texture::init() 68 { 69 this->setClassID(CL_TEXTURE, "Texture"); 70 31 TextureData::TextureData() 32 { 71 33 this->bAlpha = false; 72 34 this->texture = 0; 73 35 this->image = NULL; 74 this->priority = 0.5; 75 } 76 77 /** 78 * Destructor of a Texture79 80 Frees Data, and deletes the textures from GL81 */82 Texture ::~Texture()36 } 37 38 39 /** 40 * @brief Destructor of a Texture 41 * 42 * Frees Data, and deletes the textures from GL 43 */ 44 TextureData::~TextureData() 83 45 { 84 46 if (this->texture != 0) … … 90 52 91 53 /** 54 * @brief Loads an SDL_Surface. 55 */ 56 bool TextureData::loadSurface(SDL_Surface* surface, GLenum target) 57 { 58 if (Texture::getTextureEnableState()) 59 { 60 SDL_Surface* newSurf = Texture::prepareSurface(surface, this->bAlpha); 61 if (newSurf != NULL) 62 { 63 this->setSurface(newSurf); 64 this->setTexture(Texture::loadTexToGL(newSurf, target)); 65 return true; 66 } 67 } 68 return false; 69 } 70 71 72 73 /** 74 * @brief set the surface this Texture handles 75 * @param newSurface the new Surface to set as the image for this Texture. 76 * 77 * This deletes the old version of the stored Texture, 78 * and sets the newly given Surface as current. 79 */ 80 bool TextureData::setSurface(SDL_Surface* newSurface) 81 { 82 if (this->image != NULL) 83 SDL_FreeSurface(this->image); 84 85 this->image = newSurface; 86 87 return (this->image != NULL); 88 } 89 90 91 92 bool TextureData::setTexture(GLuint texture) 93 { 94 // unload the old Texture. 95 if (this->texture != 0 && glIsTexture(this->getTexture())) 96 { 97 glDeleteTextures(1, &this->texture); 98 } 99 this->texture = texture; 100 return (texture != 0); 101 } 102 103 104 105 106 107 Texture::Texture(GLenum target) 108 { 109 this->init(); 110 GLuint texture; 111 this->generateTexture(texture, target); 112 this->data->setTexture(texture); 113 } 114 115 /** 116 * Constructor for a Texture 117 */ 118 Texture::Texture(const std::string& imageName, GLenum target) 119 { 120 this->init(); 121 122 if (!imageName.empty()) 123 { 124 this->setName(imageName); 125 this->loadImage(imageName, target); 126 } 127 } 128 129 130 131 Texture::Texture(SDL_Surface* surface, GLenum target) 132 { 133 this->init(); 134 135 if(surface != NULL) 136 { 137 this->data->loadSurface(surface, target); 138 } 139 } 140 141 void Texture::init() 142 { 143 this->setClassID(CL_TEXTURE, "Texture"); 144 145 this->data = CountPointer<TextureData>(new TextureData()); 146 147 this->priority = 0.5; 148 } 149 150 /** 151 * Destructor of a Texture 152 153 Frees Data, and deletes the textures from GL 154 */ 155 Texture::~Texture() 156 { 157 } 158 159 160 /** 92 161 * @brief loads an Image from a file to a Texture 93 162 * @param imageName The image to load … … 105 174 if(tmpSurf != NULL) 106 175 { 107 this-> loadSurface(tmpSurf, target);176 this->data->loadSurface(tmpSurf, target); 108 177 SDL_FreeSurface(tmpSurf); 109 178 return true; … … 112 181 { 113 182 PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); 114 this-> texture = 0;183 this->setTexture(0); 115 184 return false; 116 185 } … … 126 195 127 196 /** 128 * @brief Loads an SDL_Surface.129 */130 bool Texture::loadSurface(SDL_Surface* surface, GLenum target)131 {132 if (Texture::texturesEnabled)133 {134 // some image was loaded before135 if (this->image != NULL)136 {137 SDL_FreeSurface(this->image);138 this->image = NULL;139 }140 141 // unload the old Texture.142 if (this->texture != 0 && glIsTexture(this->texture))143 {144 glDeleteTextures(1, &this->texture);145 this->texture = 0;146 }147 148 bool hasAlpha;149 SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);150 if (newSurf != NULL)151 {152 this->setSurface(newSurf);153 this->setAlpha(hasAlpha);154 this->setTexture(Texture::loadTexToGL(newSurf, target));155 return true;156 }157 }158 return false;159 }160 161 162 /**163 197 * @brief rebuilds the texture. 164 198 * reloads the Texture from Memory to OpenGL. … … 166 200 bool Texture::rebuild() 167 201 { 168 if (this->texture != 0) 169 { 170 if (glIsTexture(this->texture)) 171 glDeleteTextures(1,&this->texture); 172 this->setTexture(0); 173 } 174 175 if (this->image != NULL) 202 this->data->setTexture(0); 203 204 if (this->data->getStoredImage() != NULL) 176 205 { 177 206 PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); 178 this->setTexture(loadTexToGL(this->image)); 179 } 180 } 181 182 183 /** 184 * @brief set the surface this Texture handles 185 * @param newSurface the new Surface to set as the image for this Texture. 186 * 187 * This deletes the old version of the stored Texture, 188 * and sets the newly given Surface as current. 189 */ 190 bool Texture::setSurface(SDL_Surface* newSurface) 191 { 192 if (this->image != NULL) 193 SDL_FreeSurface(this->image); 194 195 this->image = newSurface; 196 197 return (this->image != NULL); 198 } 199 207 this->setTexture(Texture::loadTexToGL(this->data->getStoredImage())); 208 } 209 } 200 210 201 211 bool Texture::texturesEnabled = true; … … 220 230 * @returns a !!new!! Surface, that is loadable by openGL. 221 231 */ 222 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const232 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) 223 233 { 224 234 PRINTF(4)("Loading texture to OpenGL-Environment.\n"); … … 258 268 } 259 269 260 /* Copy the surface into the GL texture image*/270 /* Copy the surface into the GL texture this->data->getStoredImage() */ 261 271 area.x = 0; 262 272 area.y = 0; … … 281 291 * @returns The ID of the texture. 282 292 */ 283 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const284 { 285 // if (this-> texture != 0 && glIsTexture(this->texture))286 // glDeleteTextures(1, &this-> texture);287 // this-> texture= 0;293 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) 294 { 295 // if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture())) 296 // glDeleteTextures(1, &this->data->getTexture()); 297 // this->data->getTexture() = 0; 288 298 289 299 int errorCode = 0; //!< the error code for the texture loading functions … … 297 307 return 0; 298 308 299 /* Create an OpenGL texture for the image */ 300 this->generateTexture(texture, target); 301 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority); 309 /* Create an OpenGL texture for the this->data->getStoredImage() */ 310 Texture::generateTexture(texture, target); 311 312 /// TODO CHECK THIS BACK in 313 //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority); 302 314 303 315 /* build the Texture OpenGL V >= 1.1 */ … … 330 342 void Texture::generateTexture(GLuint& texture, GLenum target) 331 343 { 332 if ( !glIsTexture(texture))344 if (texture == 0 && !glIsTexture(texture)) 333 345 { 334 346 glGenTextures(1, &texture); … … 346 358 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); 347 359 } 348 -
branches/water/src/lib/graphics/importer/texture.h
r7700 r7783 10 10 11 11 #include "glincl.h" 12 #include "count_pointer.h" 12 13 13 14 /* Forward Declaration */ 14 15 struct SDL_Surface; 15 16 16 //! A Class, that reads in Textures from different fileformats.17 class Texture : public BaseObject18 {19 public:20 Texture(GLenum target = GL_TEXTURE_2D);21 Texture(const std::string& imageName, GLenum target = GL_TEXTURE_2D);22 Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);23 virtual ~Texture();24 17 25 bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D); 26 bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 27 virtual bool rebuild(); 18 class TextureData 19 { 20 public: 21 TextureData(); 22 ~TextureData(); 28 23 29 /** @returns The textureID of this texture. */ 30 inline GLuint getTexture() const { return this->texture; }; 31 /** @returns true if texture has alpha, false otherwise */ 32 inline bool hasAlpha() const {return bAlpha; } 33 /** @returns the stored image of this Texture */ 34 const SDL_Surface* const getStoredImage() const { return this->image; }; 24 inline GLuint getTexture() const { return this->texture; }; 25 /** @returns true if texture has alpha, false otherwise */ 26 inline bool hasAlpha() const {return this->bAlpha; } 27 /** @returns the stored image of this Texture */ 28 const SDL_Surface* const getStoredImage() const { return this->image; }; 29 30 bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 35 31 36 32 37 33 38 static void setTextureEnableState(bool texturesEnabled); 39 /** @returns true if Textures are enabled */ 40 inline static bool getTextureEnableState() { return Texture::texturesEnabled; }; 34 bool rebuild(); 41 35 42 // Utility functionality:43 SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha) const;44 GLuint loadTexToGL (const SDL_Surface* surface, GLenum target = GL_TEXTURE_2D) const;36 bool setSurface(SDL_Surface* newSurface); 37 bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; }; 38 bool setTexture(GLuint texture); 45 39 46 protected: 47 bool setSurface(SDL_Surface* newSurface); 48 bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; }; 49 bool setTexture(GLuint texture) { this->texture = texture; }; 40 private: 41 GLuint texture; //!< The Texture-ID of opengl from this Texture. 42 bool bAlpha; //!< if the texture has an alpha channel. 43 SDL_Surface* image; //!< The SDL_Surfce that stores the Texture on it. 44 }; 50 45 51 private:52 void init();53 static void generateTexture(GLuint& texture, GLenum target);54 46 55 private: 56 GLuint texture; //!< The Texture-ID of opengl from this Texture. 57 bool bAlpha; //!< if the texture has an alpha channel. 58 SDL_Surface* image; //!< The SDL_Surfce that stores the Texture on it. 59 GLclampf priority; //!< the priority of the current texture (used for garphics cards with limited mem) 47 //! A Class, that reads in Textures from different fileformats. 48 class Texture : public BaseObject 49 { 50 public: 60 51 61 static bool texturesEnabled; //!< If the Textures are enabled. 62 }; 52 public: 53 Texture(GLenum target = GL_TEXTURE_2D); 54 Texture(const std::string& imageName, GLenum target = GL_TEXTURE_2D); 55 Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 56 57 virtual ~Texture(); 58 59 bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D); 60 bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 61 virtual bool rebuild(); 62 63 /** @returns The textureID of this texture. */ 64 inline GLuint getTexture() const { return this->data->getTexture(); }; 65 /** @returns true if texture has alpha, false otherwise */ 66 inline bool hasAlpha() const { return this->data->hasAlpha(); } 67 /** @returns the stored image of this Texture */ 68 const SDL_Surface* const getStoredImage() const { return this->data->getStoredImage(); }; 69 70 71 72 static void setTextureEnableState(bool texturesEnabled); 73 /** @returns true if Textures are enabled */ 74 inline static bool getTextureEnableState() { return Texture::texturesEnabled; }; 75 76 // Utility functionality: 77 static SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha); 78 static GLuint loadTexToGL (const SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 79 80 81 protected: 82 bool setSurface(SDL_Surface* newSurface) { return this->data->setSurface(newSurface); }; 83 bool setAlpha(bool hasAlpha) { return this->data->setAlpha(hasAlpha); }; 84 bool setTexture(GLuint texture) { return this->data->setTexture(texture); }; 85 86 private: 87 void init(); 88 static void generateTexture(GLuint& texture, GLenum target); 89 90 private: 91 CountPointer<TextureData> data; //!< The TextureData 92 GLclampf priority; //!< the priority of the current texture (used for garphics cards with limited mem) 93 94 static bool texturesEnabled; //!< If the Textures are enabled. 95 }; 63 96 64 97 #endif /* _TEXTURE_H */
Note: See TracChangeset
for help on using the changeset viewer.