00001
00002
00003
00004 #ifdef WIN32
00005 #define WIN32_LEAN_AND_MEAN
00006 #include <windows.h>
00007 #endif
00008
00009 #include <GL/glu.h>
00010 #include "image_loader.h"
00011 #include "texture.h"
00012
00014 GLenum gl::texture::s_greyscale_format = GL_LUMINANCE;
00015
00017 gl::texture
00018 gl::texture::load( const std::string& filename, bool build_mipmaps,
00019 GLfloat min_filter, GLfloat max_filter )
00020 {
00021
00022 texture tex;
00023
00024
00025 image::image_t *img = image::load( filename );
00026
00027
00028 if( !img ) {
00029
00030 tex.m_handle = 0;
00031 return tex;
00032 }
00033
00034
00035 img->flip_horizontal( );
00036
00037
00038 tex = load( img, build_mipmaps, min_filter, max_filter );
00039
00040
00041 delete img;
00042
00043
00044 return tex;
00045 }
00046
00048 gl::texture
00049 gl::texture::load( std::istream& stream, bool build_mipmaps,
00050 GLfloat min_filter, GLfloat max_filter )
00051 {
00052
00053 texture tex;
00054
00055
00056 image::image_t *img = image::load( stream );
00057
00058
00059 if( !img ) {
00060
00061 tex.m_handle = 0;
00062 return tex;
00063 }
00064
00065
00066 img->flip_horizontal( );
00067
00068
00069 tex = load( img, build_mipmaps, min_filter, max_filter );
00070
00071
00072 delete img;
00073
00074
00075 return tex;
00076 }
00077
00079
00080 gl::texture
00081 gl::texture::load( image::image_t* img, bool build_mipmaps,
00082 GLfloat min_filter, GLfloat max_filter )
00083 {
00084
00085 texture tex;
00086
00087
00088 if( !img ) {
00089
00090 tex.m_handle = 0;
00091 return tex;
00092 }
00093
00094
00095 GLenum format;
00096 if( img->get_format( ) == image::image_t::RGB ) {
00097 format = GL_RGB;
00098 } else if( img->get_format( ) == image::image_t::RGBA ) {
00099 format = GL_RGBA;
00100 } else if( img->get_format( ) == image::image_t::GREYSCALE ) {
00101 format = s_greyscale_format;
00102 } else {
00103
00104 tex.m_handle = 0;
00105 return tex;
00106 }
00107
00108
00109 GLenum type;
00110 if( img->get_bpc( ) == image::image_t::BYTE ) {
00111 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
00112 type = GL_UNSIGNED_BYTE;
00113 } else {
00114
00115
00116 tex.m_handle = 0;
00117 return tex;
00118 }
00119
00120
00121 GLuint handle;
00122 glGenTextures( 1, &handle );
00123 glBindTexture( GL_TEXTURE_2D, handle );
00124
00125
00126 if( build_mipmaps ) {
00127 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
00128 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter );
00129 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
00130 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
00131 gluBuild2DMipmaps( GL_TEXTURE_2D, format, img->get_width( ),
00132 img->get_height( ), format, type, img->get_pixels( ) );
00133 } else {
00134
00135
00136
00137
00138
00139 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter );
00140 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, max_filter );
00141 glTexImage2D( GL_TEXTURE_2D, 0, format, img->get_width( ),
00142 img->get_height( ), 0, format, type, img->get_pixels( ) );
00143 }
00144
00145
00146 tex.m_aspect = static_cast<real_t>( img->get_width( ) ) /
00147 static_cast<real_t>( img->get_height( ) );
00148
00149
00150 tex.m_handle = handle;
00151 return tex;
00152 }