Changeset 5768 in orxonox.OLD for trunk/src/lib/graphics/importer
- Timestamp:
- Nov 24, 2005, 8:24:52 PM (19 years ago)
- Location:
- trunk/src/lib/graphics/importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/importer/texture.cc
r5755 r5768 66 66 { 67 67 if (this->image != NULL) 68 69 70 71 68 { 69 SDL_FreeSurface(this->image); 70 this->image = NULL; 71 } 72 72 if (this->texture != 0) 73 74 75 76 73 { 74 glDeleteTextures(1, &this->texture); 75 this->texture = 0; 76 } 77 77 if (imageName != NULL) 78 78 { … … 85 85 { 86 86 PRINTF(4)("loading Image %s\n", imageName); 87 this->image = this->prepareSurface(tmpSurf); 88 this->texture = loadTexToGL(this->image);87 if (this->prepareSurface(tmpSurf)) 88 loadTexToGL(); 89 89 SDL_FreeSurface(tmpSurf); 90 90 return true; … … 99 99 else 100 100 { 101 101 PRINTF(2)("Image-Name not specified\n"); 102 102 return false; 103 103 } … … 117 117 { 118 118 PRINTF(4)("Reloading Texture %s\n", this->getName()); 119 this-> texture = this->loadTexToGL(this->image);119 this->loadTexToGL(); 120 120 } 121 121 122 122 } 123 123 … … 127 127 * @returns a !!new!! Surface, that is loadable by openGL. 128 128 */ 129 SDL_Surface*Texture::prepareSurface(SDL_Surface* surface)129 bool Texture::prepareSurface(SDL_Surface* surface) 130 130 { 131 131 PRINTF(4)("Loading texture to OpenGL-Environment.\n"); 132 133 SDL_Surface *image;132 133 SDL_Surface* putSurface; 134 134 SDL_Rect area; 135 135 Uint32 saved_flags; 136 136 Uint8 saved_alpha; 137 138 image = SDL_CreateRGBSurface(SDL_SWSURFACE,139 140 137 138 putSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 139 surface->w, surface->h, 140 32, 141 141 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 142 143 144 145 142 0x000000FF, 143 0x0000FF00, 144 0x00FF0000, 145 0xFF000000 146 146 #else 147 148 149 150 147 0xFF000000, 148 0x00FF0000, 149 0x0000FF00, 150 0x000000FF 151 151 #endif 152 ); 153 if ( image == NULL ) 154 return NULL; 155 152 ); 153 if ( putSurface == NULL ) 154 { 155 this->setSurface(NULL); 156 return false; 157 } 158 156 159 /* Save the alpha blending attributes */ 157 160 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); … … 160 163 SDL_SetAlpha(surface, 0, 0); 161 164 } 162 165 163 166 /* Copy the surface into the GL texture image */ 164 167 area.x = 0; … … 166 169 area.w = surface->w; 167 170 area.h = surface->h; 168 SDL_BlitSurface(surface, &area, image, &area);169 171 SDL_BlitSurface(surface, &area, putSurface, &area); 172 170 173 /* Restore the alpha blending attributes */ 171 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) 172 { 173 SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); 174 this->bAlpha = true; 175 } 176 return image; 174 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) 175 { 176 SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); 177 this->bAlpha = true; 178 } 179 180 return (this->setSurface(putSurface)); 181 } 182 183 bool Texture::setSurface(SDL_Surface* newSurface) 184 { 185 if (this->image != NULL) 186 SDL_FreeSurface(this->image); 187 188 this->image = newSurface; 189 190 return (this->image != NULL); 177 191 } 178 192 … … 183 197 * @returns The ID of the texture. 184 198 */ 185 GLuint Texture::loadTexToGL (SDL_Surface* surface) const 186 { 187 GLuint texture = 0; 199 GLuint Texture::loadTexToGL () 200 { 201 if (this->texture != 0 && glIsTexture(this->texture)) 202 glDeleteTextures(1, &this->texture); 203 this->texture = 0; 204 205 if (this->image == NULL) 206 return 0; 207 188 208 /* Create an OpenGL texture for the image */ 189 glGenTextures(1, &t exture);190 glBindTexture(GL_TEXTURE_2D, t exture);209 glGenTextures(1, &this->texture); 210 glBindTexture(GL_TEXTURE_2D, this->texture); 191 211 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 192 212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 193 213 // build the Texture 194 214 glTexImage2D(GL_TEXTURE_2D, 195 196 197 surface->w, surface->h,198 199 200 201 surface->pixels);215 0, 216 GL_RGBA, 217 this->image->w, this->image->h, 218 0, 219 GL_RGBA, 220 GL_UNSIGNED_BYTE, 221 this->image->pixels); 202 222 // build the MipMaps 203 223 gluBuild2DMipmaps(GL_TEXTURE_2D, 204 205 surface->w,206 surface->h,207 208 209 surface->pixels);224 GL_RGBA, 225 this->image->w, 226 this->image->h, 227 GL_RGBA, 228 GL_UNSIGNED_BYTE, 229 this->image->pixels); 210 230 glBindTexture(GL_TEXTURE_2D, 0); 211 return t exture;212 } 231 return this->texture; 232 } -
trunk/src/lib/graphics/importer/texture.h
r5755 r5768 1 1 /*! 2 \file texture.h 3 \brief Contains the texture class, that handles the reading of Images into Texutre-files. 4 5 @todo procedural textures 6 */ 2 * @file texture.h 3 * @brief Contains the texture class, that handles the reading of Images into Texutre-files. 4 */ 7 5 8 6 #ifndef _TEXTURE_H 9 7 #define _TEXTURE_H 10 8 11 #include "glincl.h"12 9 #include "base_object.h" 13 10 14 #include " debug.h"11 #include "glincl.h" 15 12 13 /* Forward Declaration */ 16 14 struct SDL_Surface; 17 15 18 16 //! an enumerator for different procedural texture-types 19 17 typedef enum TEXTURE_TYPE { TEXTURE_RADIAL_ALIAS, 20 18 TEXTURE_NOISE }; 21 19 22 20 //! A Class, that reads in Textures from different fileformats. 23 class Texture : public BaseObject24 {25 public:26 Texture(const char* imageName = NULL);21 class Texture : public BaseObject 22 { 23 public: 24 Texture(const char* imageName = NULL); 27 25 // Texture(TEXTURE_TYPE type, int resolution); 28 ~Texture();26 ~Texture(); 29 27 30 bool loadImage(const char* imageName);31 bool rebuild();28 bool loadImage(const char* imageName); 29 bool rebuild(); 32 30 33 /** @returns The textureID of this texture. */34 inline GLuint getTexture() const { return this->texture; };35 /** @returns true if texture has alpha, false otherwise */36 inline bool hasAlpha() const {return bAlpha;}31 /** @returns The textureID of this texture. */ 32 inline GLuint getTexture() const { return this->texture; }; 33 /** @returns true if texture has alpha, false otherwise */ 34 inline bool hasAlpha() const {return bAlpha;} 37 35 38 private:39 SDL_Surface*prepareSurface(SDL_Surface* input);40 GLuint loadTexToGL (SDL_Surface* surface) const;36 protected: 37 bool prepareSurface(SDL_Surface* input); 38 bool setSurface(SDL_Surface* newSurface); 41 39 42 private: 43 GLuint texture; //!< The Texture-ID of opengl from this Texture. 44 bool bAlpha; //!< if the texture has an alpha channel. 45 SDL_Surface* image; 46 }; 40 GLuint loadTexToGL (); 41 42 private: 43 GLuint texture; //!< The Texture-ID of opengl from this Texture. 44 bool bAlpha; //!< if the texture has an alpha channel. 45 SDL_Surface* image; //!< The SDL_Surfce that stores the Texture on it. 46 }; 47 47 48 48 #endif /* _TEXTURE_H */
Note: See TracChangeset
for help on using the changeset viewer.