00001 00005 /* Copyright © 2009, 2010 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 #include "Texture.h" 00012 00013 #include <SDL_image.h> 00014 #include <GL/glu.h> 00015 00016 #include <Debug.h> 00017 #include "stream_loader.h" 00018 00019 namespace Track 00020 { 00021 00022 Texture::Texture(std::istream & source) 00023 : m_filename(string_from_stream(source)) 00024 , m_texture_name(0) 00025 { 00026 DEBUG_MESSAGE("Loading Track::TextureBase"); 00027 } 00028 00029 Texture::Texture(std::string filename) 00030 : m_filename(filename) 00031 , m_texture_name(0) 00032 { 00033 DEBUG_MESSAGE("Loading Track::TextureBase from texture filename"); 00034 } 00035 00036 Texture::Texture() 00037 : m_texture_name(0) 00038 { 00039 DEBUG_MESSAGE("Track::TextureBase default constructor"); 00040 } 00041 00042 Texture::~Texture() 00043 { 00044 DEBUG_MESSAGE("Track::TextureBase destructor"); 00045 } 00046 00047 const std::string & Texture::get_filename() const 00048 { 00049 return m_filename; 00050 } 00051 00052 void Texture::bind() const 00053 { 00054 if (!m_texture_name) 00055 { 00056 m_texture_name_ref = boost::shared_ptr<GLuint>(load_gl_texture(), delete_gl_texture); 00057 } 00058 glBindTexture(m_target, m_texture_name); 00059 } 00060 00061 void Texture::make_cache() const 00062 { 00063 if (!m_texture_name) 00064 { 00065 m_texture_name_ref = boost::shared_ptr<GLuint>(load_gl_texture(), delete_gl_texture); 00066 } 00067 } 00068 00069 void Texture::delete_gl_texture(GLuint * texture) 00070 { 00071 glDeleteTextures(1, texture); 00072 } 00073 00074 GLuint * Texture::load_gl_texture() const 00075 { 00076 m_target = GL_TEXTURE_2D; 00077 glGenTextures(1, &m_texture_name); 00078 glBindTexture(m_target, m_texture_name); 00079 00080 load_from_file(); 00081 00082 return &m_texture_name; 00083 } 00084 00085 void Texture::load_from_file() const 00086 { 00087 SDL_Surface * image (IMG_Load(m_filename.c_str())); // load the image file 00088 // check file has loaded OK 00089 if (!image) 00090 { 00091 std::cerr << "Cannot load image file " << m_filename << std::endl; 00092 throw; 00093 } 00094 00095 // get the format of the image. 00096 m_width = image->w; 00097 m_height = image->h; 00098 00099 SDL_PixelFormat & format = *(image->format); 00100 SDL_LockSurface(image); 00101 if (format.BitsPerPixel == 8) 00102 { 00104 PRINT_STUB_MESSAGE; 00105 } else { 00106 // Check if we have an alpha component. 00107 m_has_alpha = format.Amask; 00108 // check if we need to swizzle the colour components 00109 bool swizzle; 00110 if (m_has_alpha) 00111 { 00112 swizzle = format.Rmask != 0x000000ff 00113 || format.Gmask != 0x0000ff00 00114 || format.Bmask != 0x00ff0000 00115 || format.Amask != 0xff000000; 00116 } else { 00117 swizzle = format.Rmask != 0x0000ff 00118 || format.Gmask != 0x00ff00 00119 || format.Bmask != 0xff0000; 00120 } 00122 if (swizzle) 00123 { 00124 PRINT_STUB_MESSAGE; 00125 } 00126 } 00127 00128 // pass to OpenGL. 00129 gluBuild2DMipmaps(m_target, 00130 m_has_alpha ? GL_RGBA8 : GL_RGB8, 00131 m_width, m_height, 00132 m_has_alpha ? GL_RGBA : GL_RGB, 00133 GL_UNSIGNED_BYTE, 00134 image->pixels); 00135 00136 // clean up 00137 SDL_UnlockSurface(image); 00138 SDL_FreeSurface(image); 00139 00140 // make prettier 00141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 00142 } 00143 00144 }
Generated at Mon Sep 6 00:41:12 2010 by Doxygen version 1.4.7 for Racer version svn335.