Changeset 7785 in orxonox.OLD for trunk/src/lib/graphics/importer/texture.cc
- Timestamp:
- May 24, 2006, 3:17:19 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/importer/texture.cc
r7751 r7785 20 20 #include "debug.h" 21 21 #include "compiler.h" 22 #include <math.h>23 22 24 23 // INCLUDING SDL_Image … … 29 28 #endif 30 29 31 /** 32 * @brief Constructor for a Texture 33 */ 34 Texture::Texture(const std::string& imageName, GLenum target) 35 { 36 this->setClassID(CL_TEXTURE, "Texture"); 37 30 31 TextureData::TextureData() 32 { 38 33 this->bAlpha = false; 39 34 this->texture = 0; 40 35 this->image = NULL; 41 this->priority = 0.5;42 43 if (!imageName.empty())44 {45 this->setName(imageName);46 this->loadImage(imageName, target);47 }48 36 } 49 37 … … 52 40 * @brief Destructor of a Texture 53 41 * 54 * Frees Data, and deletes the textures from GL55 */ 56 Texture ::~Texture()42 * Frees Data, and deletes the textures from GL 43 */ 44 TextureData::~TextureData() 57 45 { 58 46 if (this->texture != 0) … … 64 52 65 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 * @brief Destructor of a Texture 152 * 153 * Frees Data, and deletes the textures from GL 154 */ 155 Texture::~Texture() 156 { 157 } 158 159 160 /** 66 161 * @brief loads an Image from a file to a Texture 67 162 * @param imageName The image to load … … 71 166 if (Texture::texturesEnabled) 72 167 { 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 168 if (!imageName.empty()) 84 169 { 85 170 SDL_Surface* tmpSurf; 86 if (this->texture != 0 && glIsTexture(this->texture)) 87 glDeleteTextures(1, &this->texture); 171 88 172 // load the new Image to memory 89 173 tmpSurf = IMG_Load(imageName.c_str()); 90 174 if(tmpSurf != NULL) 91 175 { 92 PRINTF(4)("loading Image %s\n", imageName.c_str()); 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 176 this->data->loadSurface(tmpSurf, target); 102 177 SDL_FreeSurface(tmpSurf); 103 178 return true; … … 106 181 { 107 182 PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); 108 this-> texture = 0;183 this->setTexture(0); 109 184 return false; 110 185 } … … 119 194 } 120 195 121 122 196 /** 123 197 * @brief rebuilds the texture. … … 127 201 bool Texture::rebuild() 128 202 { 129 if (this->texture != 0) 130 { 131 if (glIsTexture(this->texture)) 132 glDeleteTextures(1,&this->texture); 133 this->setTexture(0); 134 } 135 136 if (this->image != NULL) 203 this->data->setTexture(0); 204 205 if (this->data->getStoredImage() != NULL) 137 206 { 138 207 PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName()); 139 this->setTexture(loadTexToGL(this->image)); 140 } 141 } 142 143 144 /** 145 * @brief set the surface this Texture handles 146 * @param newSurface the new Surface to set as the image for this Texture. 147 * 148 * This deletes the old version of the stored Texture, 149 * and sets the newly given Surface as current. 150 */ 151 bool Texture::setSurface(SDL_Surface* newSurface) 152 { 153 if (this->image != NULL) 154 SDL_FreeSurface(this->image); 155 156 this->image = newSurface; 157 158 return (this->image != NULL); 159 } 160 208 this->setTexture(Texture::loadTexToGL(this->data->getStoredImage())); 209 } 210 } 161 211 162 212 bool Texture::texturesEnabled = true; … … 181 231 * @returns a !!new!! Surface, that is loadable by openGL. 182 232 */ 183 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const233 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) 184 234 { 185 235 assert(surface != NULL); … … 222 272 return NULL; 223 273 224 /* Copy the surface into the GL texture image*/274 /* Copy the surface into the GL texture this->data->getStoredImage() */ 225 275 area.x = 0; 226 276 area.y = 0; … … 245 295 * @returns The ID of the texture. 246 296 */ 247 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const248 { 249 // if (this-> texture != 0 && glIsTexture(this->texture))250 // glDeleteTextures(1, &this-> texture);251 // this-> texture= 0;297 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) 298 { 299 // if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture())) 300 // glDeleteTextures(1, &this->data->getTexture()); 301 // this->data->getTexture() = 0; 252 302 assert(surface != NULL); 253 303 254 304 int errorCode = 0; //!< the error code for the texture loading functions 255 GLuint texture ;//!< the OpenGL texture handle305 GLuint texture = 0; //!< the OpenGL texture handle 256 306 int mipmapLevel = 0; //!< the maximum mipmap level for this texture 257 307 int mipmapWidth = 0; //!< the width of the mipmap 258 308 int mipmapHight = 0; //!< the height of the mipmap 259 309 GLenum format = GL_RGB; 260 if ( this->bAlpha)310 if (surface->format->BitsPerPixel == 32) 261 311 { 262 312 format = GL_RGBA; … … 268 318 } 269 319 270 /* Create an OpenGL texture for the image */ 271 glGenTextures(1, &texture); 272 glBindTexture(target, texture); 320 /* Create an OpenGL texture for the this->data->getStoredImage() */ 321 Texture::generateTexture(texture, target); 273 322 274 323 // glTexImage2D(target, 0, format, … … 277 326 // surface->pixels); 278 327 279 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); 280 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT); 281 282 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/); 283 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 284 285 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority); 286 287 288 /* control the mipmap levels */ 289 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5); 290 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); 328 /// glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); 329 /// glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT); 330 331 /// TODO CHECK THIS BACK in 332 //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority); 291 333 292 334 /* build the Texture OpenGL V >= 1.1 */ … … 306 348 } 307 349 350 void Texture::generateTexture(GLuint& texture, GLenum target) 351 { 352 if (texture == 0 && !glIsTexture(texture)) 353 { 354 glGenTextures(1, &texture); 355 } 356 glBindTexture(target, texture); 357 358 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); 359 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT); 360 361 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 362 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 363 364 /* control the mipmap levels */ 365 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5); 366 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0); 367 }
Note: See TracChangeset
for help on using the changeset viewer.