GLTexture::GLTexture(unsigned int width, unsigned int height) : handle(), texture_width(), texture_height(), image_width(), image_height() { #ifdef GL_VERSION_ES_CM_1_0 assert(is_power_of_2(width)); assert(is_power_of_2(height)); #endif texture_width = width; texture_height = height; image_width = width; image_height = height; assert_gl("before creating texture"); glGenTextures(1, &handle); try { glBindTexture(GL_TEXTURE_2D, handle); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); set_texture_params(); } catch(...) { glDeleteTextures(1, &handle); throw; } }
Texture::Texture(unsigned int w, unsigned int h, GLenum glformat) { assert(is_power_of_2(w)); assert(is_power_of_2(h)); this->width = w; this->height = h; assert_gl("before creating texture"); glGenTextures(1, &handle); try { glBindTexture(GL_TEXTURE_2D, handle); glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); set_texture_params(); } catch(...) { glDeleteTextures(1, &handle); throw; } }
Texture::Texture(SDL_Surface* image, GLenum glformat) { const SDL_PixelFormat* format = image->format; if(!is_power_of_2(image->w) || !is_power_of_2(image->h)) throw std::runtime_error("image has no power of 2 size"); if(format->BitsPerPixel != 24 && format->BitsPerPixel != 32) throw std::runtime_error("image has no 24 or 32 bit color depth"); this->width = image->w; this->height = image->h; assert_gl("before creating texture"); glGenTextures(1, &handle); try { GLenum sdl_format; if(format->BytesPerPixel == 3) sdl_format = GL_RGB; else if(format->BytesPerPixel == 4) sdl_format = GL_RGBA; else assert(false); glBindTexture(GL_TEXTURE_2D, handle); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel); glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, sdl_format, GL_UNSIGNED_BYTE, image->pixels); assert_gl("creating texture"); set_texture_params(); } catch(...) { glDeleteTextures(1, &handle); throw; } }
GLTexture::GLTexture(SDL_Surface* image) : handle(), texture_width(), texture_height(), image_width(), image_height() { #ifdef GL_VERSION_ES_CM_1_0 texture_width = next_power_of_two(image->w); texture_height = next_power_of_two(image->h); #else if (GLEW_ARB_texture_non_power_of_two) { texture_width = image->w; texture_height = image->h; } else { texture_width = next_power_of_two(image->w); texture_height = next_power_of_two(image->h); } #endif image_width = image->w; image_height = image->h; #if SDL_BYTEORDER == SDL_BIG_ENDIAN SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE, texture_width, texture_height, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff); #else SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE, texture_width, texture_height, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000); #endif if(convert == 0) { throw std::runtime_error("Couldn't create texture: out of memory"); } SDL_SetAlpha(image, 0, 0); SDL_BlitSurface(image, 0, convert, 0); assert_gl("before creating texture"); glGenTextures(1, &handle); try { GLenum sdl_format; if(convert->format->BytesPerPixel == 3) sdl_format = GL_RGB; else if(convert->format->BytesPerPixel == 4) sdl_format = GL_RGBA; else { sdl_format = GL_RGBA; assert(false); } glBindTexture(GL_TEXTURE_2D, handle); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); #ifdef GL_UNPACK_ROW_LENGTH glPixelStorei(GL_UNPACK_ROW_LENGTH, convert->pitch/convert->format->BytesPerPixel); #else /* OpenGL ES doesn't support UNPACK_ROW_LENGTH, let's hope SDL didn't add * padding bytes, otherwise we need some extra code here... */ assert(convert->pitch == texture_width * convert->format->BytesPerPixel); #endif if(SDL_MUSTLOCK(convert)) { SDL_LockSurface(convert); } if (true) { // no not use mipmaps glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0, sdl_format, GL_UNSIGNED_BYTE, convert->pixels); } else { // build mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texture_width, texture_height, sdl_format, GL_UNSIGNED_BYTE, convert->pixels); } if(SDL_MUSTLOCK(convert)) { SDL_UnlockSurface(convert); } assert_gl("creating texture"); set_texture_params(); } catch(...) { glDeleteTextures(1, &handle); SDL_FreeSurface(convert); throw; } SDL_FreeSurface(convert); }