00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "texture.h"
00024
00025 #include "glheaders.h"
00026
00027 #include <QtCore/QUrl>
00028 #include <QtGui/QImage>
00029 #include <QtOpenGL/QGLContext>
00030
00031 #include "math.h"
00032
00033 using namespace GluonGraphics;
00034
00035 class Texture::TexturePrivate
00036 {
00037 public:
00038 TexturePrivate()
00039 {
00040 glTexture = 0;
00041 }
00042 uint glTexture;
00043 };
00044
00045 Texture::Texture( QObject* parent )
00046 : QObject( parent ),
00047 d( new TexturePrivate )
00048 {
00049
00050 }
00051
00052 Texture::~Texture()
00053 {
00054 glDeleteTextures( 1, &d->glTexture );
00055 delete d;
00056 }
00057
00058 bool Texture::load( const QUrl& url )
00059 {
00060 #ifdef __GNUC__
00061 #warning Todo: Add support for non-2D textures and non-RGBA colour formats. Also, find a way \
00062 around the nasty const_cast .
00063 #endif
00064
00065 if( d->glTexture )
00066 return true;
00067
00068 if( !QGLContext::currentContext() || !QGLContext::currentContext()->isValid() )
00069 return false;
00070
00071 QImage image;
00072 image.load( url.toLocalFile() );
00073
00074 QGLContext* context = const_cast<QGLContext*>( QGLContext::currentContext() );
00075 d->glTexture = context->bindTexture( image );
00076
00077 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
00078 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
00079 #ifndef GLUON_GRAPHICS_GLES
00080 glTexParameterf( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE );
00081 #endif
00082
00083 if( d->glTexture != 0 )
00084 return true;
00085
00086 return false;
00087 }
00088
00089 uint Texture::glTexture()
00090 {
00091 return d->glTexture;
00092 }
00093
00094 #include "texture.moc"