MPMA Framework 0.4
|
00001 00002 //Luke Lenhart, 2010 00003 //See /docs/License.txt for details on how this code may be used. 00004 00005 #pragma once 00006 00007 #include "../base/Setup.h" 00008 00009 #ifdef MPMA_COMPILE_GFX 00010 00011 #include "../base/Types.h" 00012 #include "../base/ReferenceCount.h" 00013 #include "../base/File.h" 00014 #include "../gfxsetup/GL.h" 00015 #include "../gfxsetup/Extensions.h" 00016 00017 #include <string> 00018 00019 namespace GFX 00020 { 00022 struct TextureDimensions 00023 { 00024 nuint Width; 00025 nuint Height; 00026 //nuint Depth; 00027 00029 inline TextureDimensions(nuint wid=0, nuint hei=1/*, nuint dep=1*/) 00030 { Width=wid; Height=hei; /*Depth=dep;*/ } 00031 }; 00032 00034 struct TextureCreateParameters 00035 { 00037 GLint Format; 00039 bool GenerateMipMaps; 00041 TextureDimensions Dimensions; //Resize source image to this (if non-zero size) 00042 00043 //bool Compress; //TODO 00044 00045 inline TextureCreateParameters(): Format(GL_RGBA8), GenerateMipMaps(true)//, Compress(false) 00046 {} 00047 }; 00048 00049 struct TextureData 00050 { 00051 GLuint object; 00052 TextureDimensions origDimensions; 00053 00054 inline TextureData(): object(0) 00055 {} 00056 }; 00057 00059 class TextureBase: public MPMA::ReferenceCountedData<TextureData> 00060 { 00061 public: 00062 virtual ~TextureBase(); 00063 00065 bool Create(); 00067 void Free(); 00068 00069 //Retrieves the OpenGL texture object. 00070 inline operator GLuint() const { return Data().object; } 00071 00073 inline const nuint& OriginalWidth() const { return Data().origDimensions.Width; } 00075 inline const nuint& OriginalHeight() const { return Data().origDimensions.Height; } 00076 //inline const nuint& OriginalDepth() const { return Data().origDimensions.Depth; } 00077 00078 #ifdef GFX_USES_IMAGEMAGICK 00079 00080 bool CreateFromFile(const MPMA::Filename &filename, const TextureCreateParameters *properties=0); 00081 //bool CreateFromFileInMemory(const unsigned char *memory, nuint byteCount, const TextureCreateParameters *properties=0); //TODO 00082 #endif 00083 00084 bool CreateFromMemory(void *sourceData, const TextureCreateParameters *properties, GLint pixelFormat=GL_RGBA, GLenum pixelSize=GL_UNSIGNED_BYTE, GLenum usage=GL_STATIC_DRAW); 00085 00087 inline void BindAndSetState(int stage) 00088 { 00089 glActiveTextureARB(GL_TEXTURE0_ARB+stage); 00090 glBindTexture(target, *this); 00091 glEnable(target); 00092 } 00093 00095 inline void Unbind(int stage) 00096 { 00097 glActiveTextureARB(GL_TEXTURE0_ARB+stage); 00098 glBindTexture(target, 0); 00099 glDisable(target); 00100 } 00101 00102 protected: 00103 inline TextureBase(GLenum textureTarget): target(textureTarget) 00104 {} 00105 00106 GLenum target; 00107 }; 00108 00110 class Texture2D: public TextureBase 00111 { 00112 public: 00113 inline Texture2D(): TextureBase(GL_TEXTURE_2D) 00114 {} 00115 }; 00116 00118 class AutoBindTexture 00119 { 00120 public: 00121 inline AutoBindTexture(TextureBase &texToBind, int stage=0): tex(texToBind), boundStage(stage) { tex.BindAndSetState(stage); } 00122 inline ~AutoBindTexture() { tex.Unbind(boundStage); } 00123 00124 private: 00125 TextureBase &tex; 00126 int boundStage; 00127 00128 //prevent copying 00129 AutoBindTexture(const AutoBindTexture &o); 00130 bool operator=(const AutoBindTexture &o); 00131 }; 00132 } 00133 00134 #endif //#ifdef MPMA_COMPILE_GFX