Changeset 8363 in orxonox.OLD for trunk/src/lib/graphics
- Timestamp:
- Jun 14, 2006, 10:37:08 AM (19 years ago)
- Location:
- trunk/src/lib/graphics/importer
- Files:
-
- 3 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/importer/Makefile.am
r8186 r8363 11 11 primitive_model.cc \ 12 12 md2Model.cc \ 13 material.cc \14 texture.cc \15 texture_sequence.cc \16 13 height_map.cc \ 17 media_container.cc \18 movie_player.cc \19 \20 14 bsp_manager.cc \ 21 15 bsp_file.cc \ 22 16 bsp_tree_node.cc \ 23 bsp_tree_leaf.cc 17 bsp_tree_leaf.cc \ 18 \ 19 material.cc \ 20 texture.cc \ 21 texture_data.cc \ 22 texture_sequence.cc \ 23 media_container.cc \ 24 movie_player.cc 25 24 26 25 27 … … 37 39 primitive_model.h \ 38 40 md2Model.h \ 41 anorms.h \ 42 anormtab.h \ 43 height_map.h \ 44 \ 39 45 material.h \ 40 46 texture.h \ 47 texture_data.h \ 41 48 texture_sequence.h \ 42 height_map.h \ 43 anorms.h \ 44 anormtab.h \ 49 \ 45 50 media_container.h \ 46 51 movie_player.h\ -
trunk/src/lib/graphics/importer/texture.cc
r8316 r8363 37 37 */ 38 38 Uint32 alphaMask[] = { 39 40 41 42 43 };39 0xFF000000, 40 0x00FF0000, 41 0x0000FF00, 42 0x000000FF, 43 }; 44 44 45 45 Uint32 opaqueMask[] = { 46 47 48 49 50 };46 0x00FF0000, 47 0x0000FF00, 48 0x000000FF, 49 0xFF000000 50 }; 51 51 #else 52 52 /* 53 54 55 53 * On the LIL_ENDIAN architecture everything is fine and easy. The 24 54 * and 32bit bitmaps have the same masks. 55 */ 56 56 Uint32 alphaMask[] = { 57 58 59 60 61 };57 0x000000FF, 58 0x0000FF00, 59 0x00FF0000, 60 0xFF000000, 61 }; 62 62 63 63 Uint32 *opaqueMask = alphaMask; 64 65 64 #endif 66 65 67 TextureData::TextureData() 68 { 69 this->bAlpha = false; 70 this->texture = 0; 71 this->image = NULL; 72 } 73 74 75 /** 76 * @brief Destructor of a Texture 66 67 68 /** 69 * @brief creates an Empty Texture, 77 70 * 78 * Frees Data, and deletes the textures from GL 79 */ 80 TextureData::~TextureData() 81 { 82 if (this->texture != 0) 83 glDeleteTextures(1, &this->texture); 84 if (this->image != NULL) 85 SDL_FreeSurface(this->image); 86 } 87 88 89 /** 90 * @brief Loads an SDL_Surface. 91 */ 92 bool TextureData::loadSurface(SDL_Surface* surface, GLenum target) 93 { 94 if (Texture::getTextureEnableState()) 95 { 96 SDL_Surface* newSurf = Texture::prepareSurface(surface, this->bAlpha); 97 if (newSurf != NULL) 98 { 99 this->setSurface(newSurf); 100 this->setTexture(Texture::loadTexToGL(newSurf, target)); 101 return true; 102 } 103 } 104 return false; 105 } 106 107 108 109 /** 110 * @brief set the surface this Texture handles 111 * @param newSurface the new Surface to set as the image for this Texture. 71 * onto this Texture you can load non-empty textures with the = 72 * operator. 73 */ 74 Texture::Texture() 75 { 76 this->init(); 77 } 78 79 /** 80 * @brief Creates a Texture from another Texture (copy Constructor) 81 * @param texture the Texture to copy. 112 82 * 113 * This deletes the old version of the stored Texture, 114 * and sets the newly given Surface as current. 115 */ 116 bool TextureData::setSurface(SDL_Surface* newSurface) 117 { 118 if (this->image != NULL) 119 SDL_FreeSurface(this->image); 120 121 this->image = newSurface; 122 123 return (this->image != NULL); 124 } 125 126 127 128 bool TextureData::setTexture(GLuint texture) 129 { 130 // unload the old Texture. 131 if (this->texture != 0 && glIsTexture(this->getTexture())) 132 { 133 glDeleteTextures(1, &this->texture); 134 } 135 this->texture = texture; 136 return (texture != 0); 137 } 138 139 Texture::Texture() 140 { 141 this->init(); 142 } 143 144 83 * @note only the Data-Pointer will be shared. 84 */ 145 85 Texture::Texture(const Texture& texture) 146 : data(texture.data)86 : data(texture.data) 147 87 { 148 88 this->setClassID(CL_TEXTURE, "Texture"); … … 150 90 } 151 91 152 92 /** 93 * @brief Creates a new empty Texture with the below properties. 94 * @param target: the GL-Target. 95 * @param width: the Width of the Texture. 96 * @param height: The Hight of the Texture. 97 * @param channels: also known as BitsPerPixel. 98 * @param type the Type of Texture. 99 */ 153 100 Texture::Texture(GLenum target, unsigned int width, unsigned int height, unsigned int channels, GLenum type) 154 101 { 155 102 this->init(); 156 GLuint texture = 0; 157 Texture::generateTexture(texture, target); 158 159 glBindTexture(target, texture); 160 161 unsigned int* pixels = new unsigned int[width * height * channels]; 162 memset(pixels, 0, width * height * channels * sizeof(unsigned int)); 163 164 165 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 166 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); 167 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 168 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 169 170 glTexImage2D(target, 0, channels, width, height, 0, type, GL_UNSIGNED_INT, pixels); 171 172 173 174 delete[] pixels; 175 176 this->data->setTexture(texture); 103 104 SDL_Surface * surface = SDL_CreateRGBSurface(SDL_HWSURFACE, width, height, channels, 105 alphaMask[0], alphaMask[1], alphaMask[2], alphaMask[3]); 106 107 if(surface != NULL) 108 { 109 this->data->loadSurface(surface, target); 110 SDL_FreeSurface(surface); 111 } 177 112 } 178 113 … … 218 153 */ 219 154 Texture::~Texture() 220 { 221 } 155 {} 222 156 223 157 … … 308 242 int pixelDepth = 24; 309 243 310 244 Uint32* mask = opaqueMask; 311 245 312 246 /* Save the alpha blending attributes */ … … 318 252 hasAlpha = true; 319 253 pixelDepth = 32; 320 254 mask = alphaMask; 321 255 } 322 256 … … 324 258 surface->w, surface->h, 325 259 pixelDepth, 326 260 mask[0], mask[1], mask[2], mask[3] ); 327 261 if ( retSurface == NULL ) 328 262 return NULL; … … 377 311 Texture::generateTexture(texture, target); 378 312 379 // glTexImage2D(target, 0, format,380 // surface->w, surface->h,381 // 0, format, GL_UNSIGNED_BYTE,382 // surface->pixels);383 384 /// glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);385 /// glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);313 // glTexImage2D(target, 0, format, 314 // surface->w, surface->h, 315 // 0, format, GL_UNSIGNED_BYTE, 316 // surface->pixels); 317 318 /// glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT); 319 /// glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT); 386 320 387 321 /// TODO CHECK THIS BACK in -
trunk/src/lib/graphics/importer/texture.h
r8312 r8363 11 11 #include "glincl.h" 12 12 #include "count_pointer.h" 13 #include "texture_data.h" 14 13 15 14 16 /* Forward Declaration */ 15 17 struct SDL_Surface; 16 17 18 class TextureData19 {20 public:21 TextureData();22 ~TextureData();23 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);31 32 bool rebuild();33 34 bool setSurface(SDL_Surface* newSurface);35 /** @returns true if the Surface has an Alpha Value. */36 bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; return this->bAlpha; };37 bool setTexture(GLuint texture);38 39 private:40 GLuint texture; //!< The Texture-ID of opengl from this Texture.41 bool bAlpha; //!< if the texture has an alpha channel.42 SDL_Surface* image; //!< The SDL_Surfce that stores the Texture on it.43 };44 45 18 46 19 //! A Class, that reads in Textures from different fileformats. -
trunk/src/lib/graphics/importer/texture_data.cc
r8362 r8363 21 21 #include "compiler.h" 22 22 23 #ifdef HAVE_SDL_SDL_H 24 #include <SDL/SDL_image.h> 25 #include <SDL/SDL_endian.h> 26 #include <SDL/SDL_byteorder.h> 27 #else 28 #include <SDL_endian.h> 29 #include <SDL_image.h> 30 #include <SDL_byteorder.h> 31 #endif 32 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 33 /* 34 * On the BIG_ENDIAN architecture, the 24 and 32bit bitmaps have 35 * different masks. If you don't do this distinction properly, 36 * you will get weird-looking textures. 23 #include "sdlincl.h" 24 25 /** 26 * @brief creates a new Texture Data. 37 27 */ 38 Uint32 alphaMask[] = {39 0xFF000000,40 0x00FF0000,41 0x0000FF00,42 0x000000FF,43 };44 45 Uint32 opaqueMask[] = {46 0x00FF0000,47 0x0000FF00,48 0x000000FF,49 0xFF00000050 };51 #else52 /*53 * On the LIL_ENDIAN architecture everything is fine and easy. The 2454 * and 32bit bitmaps have the same masks.55 */56 Uint32 alphaMask[] = {57 0x000000FF,58 0x0000FF00,59 0x00FF0000,60 0xFF000000,61 };62 63 Uint32 *opaqueMask = alphaMask;64 65 #endif66 67 28 TextureData::TextureData() 68 29 { … … 89 50 /** 90 51 * @brief Loads an SDL_Surface. 52 * @param surface the Surface to Load. 53 * @param target the GL-Target to load the Surface to default GL_TEXTURE_2D 54 * @returns true on success, false otherwise. 91 55 */ 92 56 bool TextureData::loadSurface(SDL_Surface* surface, GLenum target) … … 110 74 * @brief set the surface this Texture handles 111 75 * @param newSurface the new Surface to set as the image for this Texture. 76 * @returns true on success. 112 77 * 113 78 * This deletes the old version of the stored Texture, … … 125 90 126 91 127 92 /** 93 * @brief sets a new Texture to the Data. 94 * @param texture the Texture 95 * @returns true on success. 96 */ 128 97 bool TextureData::setTexture(GLuint texture) 129 98 { … … 136 105 return (texture != 0); 137 106 } 138 139 Texture::Texture()140 {141 this->init();142 }143 144 145 Texture::Texture(const Texture& texture)146 : data(texture.data)147 {148 this->setClassID(CL_TEXTURE, "Texture");149 this->priority = 0.5;150 }151 152 153 Texture::Texture(GLenum target, unsigned int width, unsigned int height, unsigned int channels, GLenum type)154 {155 this->init();156 GLuint texture = 0;157 Texture::generateTexture(texture, target);158 159 glBindTexture(target, texture);160 161 unsigned int* pixels = new unsigned int[width * height * channels];162 memset(pixels, 0, width * height * channels * sizeof(unsigned int));163 164 165 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);166 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);167 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);168 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);169 170 glTexImage2D(target, 0, channels, width, height, 0, type, GL_UNSIGNED_INT, pixels);171 172 173 174 delete[] pixels;175 176 this->data->setTexture(texture);177 }178 179 /**180 * Constructor for a Texture181 */182 Texture::Texture(const std::string& imageName, GLenum target)183 {184 this->init();185 186 if (!imageName.empty())187 {188 this->setName(imageName);189 this->loadImage(imageName, target);190 }191 }192 193 194 195 Texture::Texture(SDL_Surface* surface, GLenum target)196 {197 this->init();198 199 if(surface != NULL)200 {201 this->data->loadSurface(surface, target);202 }203 }204 205 void Texture::init()206 {207 this->setClassID(CL_TEXTURE, "Texture");208 209 this->data = CountPointer<TextureData>(new TextureData());210 211 this->priority = 0.5;212 }213 214 /**215 * @brief Destructor of a Texture216 *217 * Frees Data, and deletes the textures from GL218 */219 Texture::~Texture()220 {221 }222 223 224 /**225 * @brief loads an Image from a file to a Texture226 * @param imageName The image to load227 */228 bool Texture::loadImage(const std::string& imageName, GLenum target)229 {230 if (Texture::texturesEnabled)231 {232 if (!imageName.empty())233 {234 SDL_Surface* tmpSurf;235 236 // load the new Image to memory237 tmpSurf = IMG_Load(imageName.c_str());238 if(tmpSurf != NULL)239 {240 this->data->loadSurface(tmpSurf, target);241 SDL_FreeSurface(tmpSurf);242 return true;243 }244 else245 {246 PRINTF(1)("IMG_Load: %s\n", IMG_GetError());247 this->setTexture(0);248 return false;249 }250 }251 else252 {253 PRINTF(2)("Image-Name not specified\n");254 return false;255 }256 }257 return false;258 }259 260 /**261 * @brief rebuilds the texture.262 *263 * reloads the Texture from Memory to OpenGL.264 */265 bool Texture::rebuild()266 {267 this->data->setTexture(0);268 269 if (this->data->getStoredImage() != NULL)270 {271 PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());272 this->setTexture(Texture::loadTexToGL(this->data->getStoredImage()));273 }274 return true;275 }276 277 bool Texture::texturesEnabled = true;278 279 /**280 * @brief enables, disables textures281 * @param texturesEnabled true if the textures should be enabled282 */283 void Texture::setTextureEnableState(bool texturesEnabled)284 {285 Texture::texturesEnabled = texturesEnabled;286 }287 288 289 //////////////////////////////////////290 // UTILITY FUNCTIONALITY OF TEXTURE //291 //////////////////////////////////////292 /**293 * @brief converts surface to a new SDL_Surface, that is loadable by openGL294 * @param surface the Surface to convert295 * @param hasAlpha if the newly created Surface has an alpha channel, true is returned otherwise false.296 * @returns a !!new!! Surface, that is loadable by openGL.297 */298 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)299 {300 assert(surface != NULL);301 PRINTF(4)("Loading texture to OpenGL-Environment.\n");302 303 SDL_Surface* retSurface;304 SDL_Rect area;305 Uint32 saved_flags;306 Uint8 saved_alpha;307 hasAlpha = false;308 int pixelDepth = 24;309 310 Uint32* mask = opaqueMask;311 312 /* Save the alpha blending attributes */313 saved_flags = surface->flags&(SDL_SRCALPHA | SDL_RLEACCELOK);314 saved_alpha = surface->format->alpha;315 if ( saved_flags & SDL_SRCALPHA )316 {317 SDL_SetAlpha(surface, 0, 0);318 hasAlpha = true;319 pixelDepth = 32;320 mask = alphaMask;321 }322 323 retSurface = SDL_CreateRGBSurface(SDL_HWSURFACE,324 surface->w, surface->h,325 pixelDepth,326 mask[0], mask[1], mask[2], mask[3] );327 if ( retSurface == NULL )328 return NULL;329 330 /* Copy the surface into the GL texture this->data->getStoredImage() */331 area.x = 0;332 area.y = 0;333 area.w = surface->w;334 area.h = surface->h;335 SDL_BlitSurface(surface, &area, retSurface, &area);336 337 /* Restore the alpha blending attributes */338 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )339 {340 SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);341 hasAlpha = true;342 }343 344 return (retSurface);345 }346 347 348 /**349 * @brief Loads a Texture to the openGL-environment.350 * @param surface the Image to load to openGL351 * @returns The ID of the texture.352 */353 GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)354 {355 // if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))356 // glDeleteTextures(1, &this->data->getTexture());357 // this->data->getTexture() = 0;358 assert(surface != NULL);359 360 int errorCode = 0; //!< the error code for the texture loading functions361 GLuint texture = 0; //!< the OpenGL texture handle362 //int mipmapLevel = 0; //!< the maximum mipmap level for this texture363 //int mipmapWidth = 0; //!< the width of the mipmap364 //int mipmapHight = 0; //!< the height of the mipmap365 GLenum format = GL_RGB;366 if (surface->format->BitsPerPixel == 32)367 {368 format = GL_RGBA;369 assert(surface->format->BitsPerPixel == 32);370 }371 else372 {373 assert(surface->format->BitsPerPixel == 24);374 }375 376 /* Create an OpenGL texture for the this->data->getStoredImage() */377 Texture::generateTexture(texture, target);378 379 // glTexImage2D(target, 0, format,380 // surface->w, surface->h,381 // 0, format, GL_UNSIGNED_BYTE,382 // surface->pixels);383 384 /// glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);385 /// glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);386 387 /// TODO CHECK THIS BACK in388 //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);389 390 /* build the Texture OpenGL V >= 1.1 */391 392 // printf("%s, w:%d h:%d, 0x%x\n", this->getName(), surface->w, surface->h, target);393 394 /* control the mipmap levels */395 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);396 glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);397 398 // build the MipMaps automaticaly399 errorCode = gluBuild2DMipmaps(target, format,400 surface->w, surface->h,401 format, GL_UNSIGNED_BYTE,402 surface->pixels403 );404 if(unlikely(errorCode != 0))405 PRINTF(1)("Error while loading texture (mipmap generation), gluBuild2DMipmaps returned %i\n", errorCode);406 407 return texture;408 }409 410 void Texture::generateTexture(GLuint& texture, GLenum target)411 {412 if (texture == 0 && !glIsTexture(texture))413 {414 glGenTextures(1, &texture);415 }416 glBindTexture(target, texture);417 418 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);419 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);420 421 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);422 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);423 424 } -
trunk/src/lib/graphics/importer/texture_data.h
r8362 r8363 4 4 */ 5 5 6 #ifndef _TEXTURE_ H7 #define _TEXTURE_ H6 #ifndef _TEXTURE_DATA_H 7 #define _TEXTURE_DATA_H 8 8 9 9 #include "base_object.h" … … 43 43 }; 44 44 45 46 //! A Class, that reads in Textures from different fileformats. 47 class Texture : public BaseObject 48 { 49 public: 50 Texture(); 51 Texture(const Texture& texture); 52 Texture(GLenum target, unsigned int width, unsigned int height, unsigned int channels, GLenum type); 53 Texture(const std::string& imageName, GLenum target = GL_TEXTURE_2D); 54 Texture(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 55 56 virtual ~Texture(); 57 58 bool loadImage(const std::string& imageName, GLenum target = GL_TEXTURE_2D); 59 bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 60 virtual bool rebuild(); 61 62 /** @returns The textureID of this texture. */ 63 inline GLuint getTexture() const { return this->data->getTexture(); }; 64 /** @returns true if texture has alpha, false otherwise */ 65 inline bool hasAlpha() const { return this->data->hasAlpha(); } 66 /** @returns the stored image of this Texture */ 67 const SDL_Surface* const getStoredImage() const { return this->data->getStoredImage(); }; 68 69 70 71 static void setTextureEnableState(bool texturesEnabled); 72 /** @returns true if Textures are enabled */ 73 inline static bool getTextureEnableState() { return Texture::texturesEnabled; }; 74 // Utility functionality: 75 static SDL_Surface* prepareSurface(SDL_Surface* input, bool& hasAlpha); 76 static GLuint loadTexToGL (const SDL_Surface* surface, GLenum target = GL_TEXTURE_2D); 77 78 protected: 79 bool setSurface(SDL_Surface* newSurface) { return this->data->setSurface(newSurface); }; 80 bool setAlpha(bool hasAlpha) { return this->data->setAlpha(hasAlpha); }; 81 bool setTexture(GLuint texture) { return this->data->setTexture(texture); }; 82 83 private: 84 void init(); 85 static void generateTexture(GLuint& texture, GLenum target); 86 87 private: 88 CountPointer<TextureData> data; //!< The TextureData 89 GLclampf priority; //!< the priority of the current texture (used for garphics cards with limited mem) 90 91 static bool texturesEnabled; //!< If the Textures are enabled. 92 }; 93 94 #endif /* _TEXTURE_H */ 45 #endif /* _TEXTURE_DATA_H */
Note: See TracChangeset
for help on using the changeset viewer.