- Timestamp:
- Nov 24, 2005, 11:36:49 AM (19 years ago)
- Location:
- trunk/src/lib/graphics/importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/importer/texture.cc
r5306 r5753 37 37 this->bAlpha = false; 38 38 this->texture = 0; 39 this->image = NULL; 39 40 if (imageName != NULL) 40 41 this->loadImage(imageName); … … 50 51 if (this->texture != 0) 51 52 glDeleteTextures(1, &this->texture); 53 if (this->image != NULL) 54 SDL_FreeSurface(this->image); 52 55 } 53 56 54 /**55 * Loads a Texture to the openGL-environment.56 * @param surface the Image to load to openGL57 * @returns The ID of the texture.58 */59 GLuint Texture::loadTexToGL (SDL_Surface* surface)60 {61 if (GraphicsEngine::texturesEnabled)62 {63 PRINTF(4)("Loading texture to OpenGL-Environment.\n");64 65 int w, h;66 SDL_Surface *image;67 SDL_Rect area;68 Uint32 saved_flags;69 Uint8 saved_alpha;70 71 w = surface->w;72 h = surface->h;73 74 image = SDL_CreateRGBSurface(SDL_SWSURFACE,75 w, h,76 32,77 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */78 0x000000FF,79 0x0000FF00,80 0x00FF0000,81 0xFF00000082 #else83 0xFF000000,84 0x00FF0000,85 0x0000FF00,86 0x000000FF87 #endif88 );89 if ( image == NULL ) {90 return 0;91 }92 93 /* Save the alpha blending attributes */94 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);95 saved_alpha = surface->format->alpha;96 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {97 SDL_SetAlpha(surface, 0, 0);98 }99 100 /* Copy the surface into the GL texture image */101 area.x = 0;102 area.y = 0;103 area.w = surface->w;104 area.h = surface->h;105 SDL_BlitSurface(surface, &area, image, &area);106 107 /* Restore the alpha blending attributes */108 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {109 SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);110 this->bAlpha = true;111 }112 113 /* Create an OpenGL texture for the image */114 glGenTextures(1, &this->texture);115 glBindTexture(GL_TEXTURE_2D, this->texture);116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);118 // build the Texture119 glTexImage2D(GL_TEXTURE_2D,120 0,121 GL_RGBA,122 w, h,123 0,124 GL_RGBA,125 GL_UNSIGNED_BYTE,126 image->pixels);127 // build the MipMaps128 gluBuild2DMipmaps(GL_TEXTURE_2D,129 GL_RGBA,130 w,131 h,132 GL_RGBA,133 GL_UNSIGNED_BYTE,134 image->pixels);135 136 SDL_FreeSurface(image); /* No longer needed */137 glBindTexture(GL_TEXTURE_2D, 0);138 }139 }140 57 141 58 /** … … 154 71 // load the new Image to memory 155 72 tmpSurf = IMG_Load(imageName); 156 if(tmpSurf == NULL) 73 if(tmpSurf != NULL) 74 { 75 PRINTF(3)("loading Image %s\n", imageName); 76 this->image = this->prepareSurface(tmpSurf); 77 this->texture = loadTexToGL(this->image); 78 SDL_FreeSurface(tmpSurf); 79 return true; 80 } 81 else 157 82 { 158 83 PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); … … 160 85 return false; 161 86 } 162 else163 {164 PRINTF(3)("loading Image %s\n", imageName);165 loadTexToGL(tmpSurf);166 SDL_FreeSurface(tmpSurf);167 return true;168 }169 87 } 170 88 else … … 175 93 return false; 176 94 } 95 96 SDL_Surface* Texture::prepareSurface(SDL_Surface* surface) 97 { 98 PRINTF(4)("Loading texture to OpenGL-Environment.\n"); 99 100 SDL_Surface *image; 101 SDL_Rect area; 102 Uint32 saved_flags; 103 Uint8 saved_alpha; 104 105 image = SDL_CreateRGBSurface(SDL_SWSURFACE, 106 surface->w, surface->h, 107 32, 108 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 109 0x000000FF, 110 0x0000FF00, 111 0x00FF0000, 112 0xFF000000 113 #else 114 0xFF000000, 115 0x00FF0000, 116 0x0000FF00, 117 0x000000FF 118 #endif 119 ); 120 if ( image == NULL ) { 121 return 0; 122 } 123 124 /* Save the alpha blending attributes */ 125 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); 126 saved_alpha = surface->format->alpha; 127 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 128 SDL_SetAlpha(surface, 0, 0); 129 } 130 131 /* Copy the surface into the GL texture image */ 132 area.x = 0; 133 area.y = 0; 134 area.w = surface->w; 135 area.h = surface->h; 136 SDL_BlitSurface(surface, &area, image, &area); 137 138 /* Restore the alpha blending attributes */ 139 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) 140 { 141 SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); 142 this->bAlpha = true; 143 } 144 return image; 145 } 146 147 148 /** 149 * Loads a Texture to the openGL-environment. 150 * @param surface the Image to load to openGL 151 * @returns The ID of the texture. 152 */ 153 GLuint Texture::loadTexToGL (SDL_Surface* surface) const 154 { 155 GLuint texture = 0; 156 /* Create an OpenGL texture for the image */ 157 glGenTextures(1, &texture); 158 glBindTexture(GL_TEXTURE_2D, texture); 159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 161 // build the Texture 162 glTexImage2D(GL_TEXTURE_2D, 163 0, 164 GL_RGBA, 165 surface->w, surface->h, 166 0, 167 GL_RGBA, 168 GL_UNSIGNED_BYTE, 169 surface->pixels); 170 // build the MipMaps 171 gluBuild2DMipmaps(GL_TEXTURE_2D, 172 GL_RGBA, 173 surface->w, 174 surface->h, 175 GL_RGBA, 176 GL_UNSIGNED_BYTE, 177 surface->pixels); 178 glBindTexture(GL_TEXTURE_2D, 0); 179 return texture; 180 } -
trunk/src/lib/graphics/importer/texture.h
r5308 r5753 26 26 Texture(const char* imageName = NULL); 27 27 // Texture(TEXTURE_TYPE type, int resolution); 28 ~Texture(); 28 29 29 ~Texture(); 30 bool loadImage(const char* imageName); 31 bool reload(); 30 32 31 33 /** @returns The textureID of this texture. */ 32 34 inline GLuint getTexture() const { return this->texture; }; 33 GLuint loadTexToGL (SDL_Surface* surface);34 35 /** @returns true if texture has alpha, false otherwise */ 35 36 inline bool hasAlpha() const {return bAlpha;} 36 37 37 bool loadImage(const char* imageName); 38 38 private: 39 SDL_Surface* prepareSurface(SDL_Surface* input); 40 GLuint loadTexToGL (SDL_Surface* surface) const; 39 41 40 42 private: 41 GLuint texture; //!< The Texture-ID of opengl from this Texture. 42 bool bAlpha; //!< if the texture has an alpha channel. 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; 43 46 }; 44 47
Note: See TracChangeset
for help on using the changeset viewer.