Changeset 3905 in orxonox.OLD for orxonox/trunk/src/lib/graphics/importer/texture.cc
- Timestamp:
- Apr 20, 2005, 12:39:05 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/graphics/importer/texture.cc
r3863 r3905 23 23 \brief Constructor for a Texture 24 24 */ 25 Texture::Texture(void)26 {27 this->pImage = new Image;28 this->pImage->data = NULL;29 this->map = NULL;30 this->texture = 0;31 }32 33 /**34 \brief Constructor for a Texture35 */36 25 Texture::Texture(const char* imageName) 37 26 { 38 this->pImage = new Image;39 this->pImage->data = NULL;40 this->map = NULL;41 27 this->texture = 0; 42 this->loadImage(imageName); 28 if (imageName) 29 this->loadImage(imageName); 43 30 } 44 31 … … 50 37 Texture::~Texture(void) 51 38 { 52 if (this->pImage->data)53 delete []this->pImage->data;54 delete pImage;55 39 if (this->texture) 56 40 glDeleteTextures(1, &this->texture); … … 73 57 /** 74 58 \brief Loads a Texture to the openGL-environment. 75 \param pImage The Image to load to openGL 59 \param surface the Image to load to openGL 60 \returns The ID of the texture. 76 61 */ 77 bool Texture::loadTexToGL (Image* pImage)62 GLuint Texture::loadTexToGL (SDL_Surface* surface) 78 63 { 79 64 if (GraphicsEngine::texturesEnabled) 80 65 { 81 66 PRINTF(4)("Loading texture to OpenGL-Environment.\n"); 82 glGenTextures(1, &this->texture); 83 glBindTexture(GL_TEXTURE_2D, this->texture); 84 /* not Working, and not needed. 85 glTexImage2D( GL_TEXTURE_2D, 0, 3, width, 86 height, 0, GL_BGR, 87 GL_UNSIGNED_BYTE, map->pixels ); 88 */ 89 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, pImage->format, GL_UNSIGNED_BYTE, pImage->data); 67 68 GLuint texture; 69 int w, h; 70 SDL_Surface *image; 71 SDL_Rect area; 72 Uint32 saved_flags; 73 Uint8 saved_alpha; 90 74 91 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 92 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); 75 w = surface->w; 76 h = surface->h; 77 78 image = SDL_CreateRGBSurface(SDL_SWSURFACE, 79 w, h, 80 32, 81 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 82 0x000000FF, 83 0x0000FF00, 84 0x00FF0000, 85 0xFF000000 86 #else 87 0xFF000000, 88 0x00FF0000, 89 0x0000FF00, 90 0x000000FF 91 #endif 92 ); 93 if ( image == NULL ) { 94 return 0; 95 } 96 97 /* Save the alpha blending attributes */ 98 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); 99 saved_alpha = surface->format->alpha; 100 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 101 SDL_SetAlpha(surface, 0, 0); 102 } 103 104 /* Copy the surface into the GL texture image */ 105 area.x = 0; 106 area.y = 0; 107 area.w = surface->w; 108 area.h = surface->h; 109 SDL_BlitSurface(surface, &area, image, &area); 110 111 /* Restore the alpha blending attributes */ 112 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 113 SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha); 114 } 115 116 /* Create an OpenGL texture for the image */ 117 glGenTextures(1, &texture); 118 glBindTexture(GL_TEXTURE_2D, texture); 119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 121 /* glTexImage2D(GL_TEXTURE_2D, 122 0, 123 GL_RGBA, 124 w, h, 125 0, 126 GL_RGBA, 127 GL_UNSIGNED_BYTE, 128 image->pixels); 129 */ 130 gluBuild2DMipmaps(GL_TEXTURE_2D, 131 3, 132 w, 133 h, 134 GL_RGBA, 135 GL_UNSIGNED_BYTE, 136 image->pixels); 137 138 SDL_FreeSurface(image); /* No longer needed */ 139 140 return texture; 93 141 } 94 142 } … … 104 152 if (imageName) 105 153 { 106 this->map=IMG_Load(imageName); 107 if(!map) 154 SDL_Surface* tmpSurf; 155 if (this->texture) 156 glDeleteTextures(1, &this->texture); 157 // load the new Image to memory 158 tmpSurf = IMG_Load(imageName); 159 if(!tmpSurf) 108 160 { 109 161 PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); 110 162 return false; 111 163 } 112 pImage->height = map->h;113 pImage->width = map->w;114 pImage->data = (GLubyte*)map->pixels;115 pImage->bpp = map->format->BytesPerPixel;116 if (pImage->bpp == 3)117 pImage->format = GL_RGB;118 else if (pImage->bpp == 4)119 {120 pImage->format = GL_RGBA;121 SDL_SetAlpha(this->map, 0, 0);122 }123 164 124 if( !IMG_isPNG(SDL_RWFromFile(imageName, "rb")) && !IMG_isJPG(SDL_RWFromFile(imageName, "rb"))) 125 for (int i=0;i<map->h * map->w *3;i+=3) 126 { 127 GLuint temp = pImage->data[i]; 128 pImage->data[i] = pImage->data[i+2]; 129 pImage->data[i+2] = temp; 130 } 131 /* this is the real swapping algorithm */ 132 for( int i = 0 ; i < (pImage->height / 2) ; ++i ) 133 for( int j = 0 ; j < pImage->width * pImage->bpp; j += pImage->bpp ) 134 for(int k = 0; k < pImage->bpp; ++k) 135 swap( pImage->data[ (i * pImage->width * pImage->bpp) + j + k], pImage->data[ ( (pImage->height - i - 1) * pImage->width * pImage->bpp ) + j + k]); 165 GLubyte* pixels = (GLubyte*)tmpSurf->pixels; 136 166 137 this->loadTexToGL (this->pImage); 138 SDL_FreeSurface(map); 139 pImage->data = NULL; 167 /* this swaps the Mapping so lowel left will be upper left */ 168 for( int i = 0 ; i < (tmpSurf->h / 2) ; ++i ) 169 for( int j = 0 ; j < tmpSurf->w * tmpSurf->format->BytesPerPixel; j += tmpSurf->format->BytesPerPixel ) 170 for(int k = 0; k < tmpSurf->format->BytesPerPixel; ++k) 171 swap( pixels[(i * tmpSurf->w * tmpSurf->format->BytesPerPixel) + j + k], 172 pixels[ ( (tmpSurf->h - i - 1) * tmpSurf->w * tmpSurf->format->BytesPerPixel) + j + k]); 173 174 PRINTF(3)("loading Image %s\n", imageName); 175 if (tmpSurf) 176 this->texture = loadTexToGL(tmpSurf); 177 178 179 SDL_FreeSurface(tmpSurf); 180 return true; 140 181 } 141 182 else
Note: See TracChangeset
for help on using the changeset viewer.