00001 #ifndef GLW_TEXTURE2D_H 00002 #define GLW_TEXTURE2D_H 00003 00004 #include "./texture.h" 00005 00006 namespace glw 00007 { 00008 00009 class Texture2DArguments : public TextureArguments 00010 { 00011 public: 00012 00013 typedef TextureArguments BaseType; 00014 typedef Texture2DArguments ThisType; 00015 00016 GLsizei width; 00017 GLsizei height; 00018 GLenum dataFormat; 00019 GLenum dataType; 00020 const void * data; 00021 TextureSampleMode sampler; 00022 00023 Texture2DArguments(void) 00024 { 00025 this->clear(); 00026 } 00027 00028 void clear(void) 00029 { 00030 BaseType::clear(); 00031 this->width = 0; 00032 this->height = 0; 00033 this->dataFormat = GL_NONE; 00034 this->dataType = GL_NONE; 00035 this->data = 0; 00036 this->sampler.clear(); 00037 } 00038 }; 00039 00040 class Texture2D : public Texture 00041 { 00042 friend class Context; 00043 00044 public: 00045 00046 typedef Texture BaseType; 00047 typedef Texture2D ThisType; 00048 00049 virtual Type type(void) const 00050 { 00051 return Texture2DType; 00052 } 00053 00054 virtual int imageDimensions(void) const 00055 { 00056 return 2; 00057 } 00058 00059 virtual bool isArray(void) const 00060 { 00061 return false; 00062 } 00063 00064 GLsizei width(void) const 00065 { 00066 return this->m_width; 00067 } 00068 00069 GLsizei height(void) const 00070 { 00071 return this->m_height; 00072 } 00073 00074 void setImage(GLenum target, GLint unit, GLint level, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) 00075 { 00076 (void)unit; 00077 GLW_ASSERT(this->isValid()); 00078 if (level == 0) 00079 { 00080 this->m_width = width; 00081 this->m_height = height; 00082 } 00083 glTexImage2D(target, level, this->m_format, width, height, 0, dataFormat, dataType, data); 00084 } 00085 00086 void getImage(GLenum target, GLint unit, GLint level, GLenum dataFormat, GLenum dataType, void * data) 00087 { 00088 (void)unit; 00089 GLW_ASSERT(this->isValid()); 00090 glGetTexImage(target, level, dataFormat, dataType, data); 00091 } 00092 00093 void setSubImage(GLenum target, GLint unit, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) 00094 { 00095 (void)unit; 00096 GLW_ASSERT(this->isValid()); 00097 glTexSubImage2D(target, level, xoffset, yoffset, width, height, dataFormat, dataType, data); 00098 } 00099 00100 void generateMipmap(GLenum target, GLint unit) 00101 { 00102 (void)unit; 00103 GLW_ASSERT(this->isValid()); 00104 glGenerateMipmap(target); 00105 } 00106 00107 void setSampleMode(GLenum target, GLint unit, const TextureSampleMode & sampler) 00108 { 00109 (void)unit; 00110 GLW_ASSERT(this->isValid()); 00111 if (GLW_CARE_OF(sampler.minFilter)) glTexParameteri(target, GL_TEXTURE_MIN_FILTER, sampler.minFilter); 00112 if (GLW_CARE_OF(sampler.magFilter)) glTexParameteri(target, GL_TEXTURE_MAG_FILTER, sampler.magFilter); 00113 if (GLW_CARE_OF(sampler.wrapS )) glTexParameteri(target, GL_TEXTURE_WRAP_S, sampler.wrapS ); 00114 if (GLW_CARE_OF(sampler.wrapT )) glTexParameteri(target, GL_TEXTURE_WRAP_T, sampler.wrapT ); 00115 } 00116 00117 protected: 00118 00119 GLsizei m_width; 00120 GLsizei m_height; 00121 00122 Texture2D(Context * ctx) 00123 : BaseType (ctx) 00124 , m_width (0) 00125 , m_height (0) 00126 { 00127 ; 00128 } 00129 00130 bool create(const Texture2DArguments & args) 00131 { 00132 this->destroy(); 00133 GLint boundName = 0; 00134 glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundName); 00135 glGenTextures(1, &(this->m_name)); 00136 glBindTexture(GL_TEXTURE_2D, this->m_name); 00137 glTexImage2D(GL_TEXTURE_2D, 0, args.format, args.width, args.height, 0, args.dataFormat, args.dataType, args.data); 00138 this->m_format = args.format; 00139 this->m_width = args.width; 00140 this->m_height = args.height; 00141 this->setSampleMode(GL_TEXTURE_2D, 0, args.sampler); 00142 glBindTexture(GL_TEXTURE_2D, boundName); 00143 return true; 00144 } 00145 00146 virtual void doDestroy(void) 00147 { 00148 BaseType::doDestroy(); 00149 this->m_format = GL_NONE; 00150 this->m_width = 0; 00151 this->m_height = 0; 00152 } 00153 00154 virtual bool doIsValid(void) const 00155 { 00156 return ((this->m_format != GL_NONE) && (this->m_width > 0) && (this->m_height > 0)); 00157 } 00158 }; 00159 00160 namespace detail { template <> struct BaseOf <Texture2D> { typedef Texture Type; }; }; 00161 typedef detail::ObjectSharedPointerTraits <Texture2D> ::Type Texture2DPtr; 00162 00163 class SafeTexture2D : public SafeTexture 00164 { 00165 friend class Context; 00166 friend class BoundTexture2D; 00167 00168 public: 00169 00170 typedef SafeTexture BaseType; 00171 typedef SafeTexture2D ThisType; 00172 00173 SafeTexture2D(void) 00174 : BaseType() 00175 { 00176 ; 00177 } 00178 00179 GLsizei width(void) const 00180 { 00181 return this->object()->width(); 00182 } 00183 00184 GLsizei height(void) const 00185 { 00186 return this->object()->height(); 00187 } 00188 00189 protected: 00190 00191 SafeTexture2D(const Texture2DPtr & texture2D) 00192 : BaseType(texture2D) 00193 { 00194 ; 00195 } 00196 00197 const Texture2DPtr & object(void) const 00198 { 00199 return static_cast<const Texture2DPtr &>(BaseType::object()); 00200 } 00201 00202 Texture2DPtr & object(void) 00203 { 00204 return static_cast<Texture2DPtr &>(BaseType::object()); 00205 } 00206 }; 00207 00208 namespace detail { template <> struct BaseOf <SafeTexture2D> { typedef SafeTexture Type; }; }; 00209 namespace detail { template <> struct ObjectBase <SafeTexture2D> { typedef Texture2D Type; }; }; 00210 namespace detail { template <> struct ObjectSafe <Texture2D > { typedef SafeTexture2D Type; }; }; 00211 typedef detail::ObjectSharedPointerTraits <SafeTexture2D> ::Type Texture2DHandle; 00212 00213 class Texture2DBindingParams : public TextureBindingParams 00214 { 00215 public: 00216 00217 typedef TextureBindingParams BaseType; 00218 typedef Texture2DBindingParams ThisType; 00219 00220 Texture2DBindingParams(void) 00221 : BaseType() 00222 { 00223 ; 00224 } 00225 00226 Texture2DBindingParams(GLenum aUnit) 00227 : BaseType(GL_TEXTURE_2D, aUnit) 00228 { 00229 ; 00230 } 00231 }; 00232 00233 class BoundTexture2D : public BoundTexture 00234 { 00235 friend class Context; 00236 00237 public: 00238 00239 typedef BoundTexture BaseType; 00240 typedef BoundTexture2D ThisType; 00241 00242 BoundTexture2D(void) 00243 : BaseType() 00244 { 00245 ; 00246 } 00247 00248 const Texture2DHandle & handle(void) const 00249 { 00250 return static_cast<const Texture2DHandle &>(BaseType::handle()); 00251 } 00252 00253 Texture2DHandle & handle(void) 00254 { 00255 return static_cast<Texture2DHandle &>(BaseType::handle()); 00256 } 00257 00258 void setSampleMode(const TextureSampleMode & sampler) 00259 { 00260 this->object()->setSampleMode(this->m_target, this->m_unit, sampler); 00261 } 00262 00263 void setImage(GLint level, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) 00264 { 00265 this->object()->setImage(this->m_target, this->m_unit, level, width, height, dataFormat, dataType, data); 00266 } 00267 00268 void getImage(GLint level, GLenum dataFormat, GLenum dataType, void * data) 00269 { 00270 this->object()->getImage(this->m_target, this->m_unit, level, dataFormat, dataType, data); 00271 } 00272 00273 void setSubImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum dataFormat, GLenum dataType, const void * data) 00274 { 00275 this->object()->setSubImage(this->m_target, this->m_unit, level, xoffset, yoffset, width, height, dataFormat, dataType, data); 00276 } 00277 00278 void generateMipmap(void) 00279 { 00280 this->object()->generateMipmap(this->m_target, this->m_unit); 00281 } 00282 00283 protected: 00284 00285 BoundTexture2D(const Texture2DHandle & handle, const Texture2DBindingParams & params) 00286 : BaseType(handle, params) 00287 { 00288 ; 00289 } 00290 00291 const Texture2DPtr & object(void) const 00292 { 00293 return this->handle()->object(); 00294 } 00295 00296 Texture2DPtr & object(void) 00297 { 00298 return this->handle()->object(); 00299 } 00300 }; 00301 00302 namespace detail { template <> struct ParamsOf <BoundTexture2D> { typedef Texture2DBindingParams Type; }; }; 00303 namespace detail { template <> struct BaseOf <BoundTexture2D> { typedef BoundObject Type; }; }; 00304 namespace detail { template <> struct ObjectBase <BoundTexture2D> { typedef Texture2D Type; }; }; 00305 namespace detail { template <> struct ObjectBound <Texture2D > { typedef BoundTexture2D Type; }; }; 00306 typedef detail::ObjectSharedPointerTraits <BoundTexture2D> ::Type BoundTexture2DHandle; 00307 00308 }; 00309 00310 #endif // GLW_TEXTURE2D_H