1 #include "Texture.h"
2 #include "FrameBuffer.h"
3 
4 
5 
6 
Allocate()7 bool GPU::Texture2D::Allocate()
8 {
9     glGenTextures( 1, &m_Id );
10     return m_Id != 0;
11 }
12 
13 
Unallocate()14 bool GPU::Texture2D::Unallocate()
15 {
16     glDeleteTextures( 1, &m_Id );
17     m_Id = 0;
18     return true;
19 }
20 
21 
Create()22 bool GPU::Texture2D::Create()
23 {
24     if( Instantiate() )
25     {
26         glBindTexture( GL_TEXTURE_2D, m_Id );
27         m_Width = m_Height = 0;
28         return true;
29     }
30     else
31         return false;
32 }
33 
34 
Create(GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * data)35 bool GPU::Texture2D::Create( GLint internalFormat,
36                              GLsizei width,
37                              GLsizei height,
38                              GLint border,
39                              GLenum format,
40                              GLenum type,
41                              const GLvoid* data )
42 {
43     if( Instantiate() )
44     {
45         glBindTexture( GL_TEXTURE_2D, m_Id );
46         glTexImage2D(
47             GL_TEXTURE_2D,
48             0,
49             internalFormat,
50             width,
51             height,
52             border,
53             format,
54             type,
55             data
56             );
57         m_Width = width;
58         m_Height = height;
59         return true;
60     }
61     else
62         return false;
63 }
64 
65 
Create(GLint internalFormat,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * data)66 bool GPU::Texture2D::Create( GLint internalFormat,
67                              GLsizei width,
68                              GLsizei height,
69                              GLenum format,
70                              GLenum type,
71                              const GLvoid* data )
72 {
73     if( Instantiate() )
74     {
75         glBindTexture( GL_TEXTURE_2D, m_Id );
76         glTexImage2D(
77             GL_TEXTURE_2D,
78             0,
79             internalFormat,
80             width,
81             height,
82             0,
83             format,
84             type,
85             data
86             );
87         m_Width = width;
88         m_Height = height;
89         return true;
90     }
91     else
92         return false;
93 }
94 
95 
Create(GLint internalFormat,GLsizei width,GLsizei height)96 bool GPU::Texture2D::Create( GLint internalFormat,
97                              GLsizei width,
98                              GLsizei height )
99 {
100     if( Instantiate() )
101     {
102         glBindTexture( GL_TEXTURE_2D, m_Id );
103         glTexImage2D(
104             GL_TEXTURE_2D,
105             0,
106             internalFormat,
107             width,
108             height,
109             0,
110             GL_RGB,
111             GL_FLOAT,
112             NULL
113             );
114         m_Width = width;
115         m_Height = height;
116         return true;
117     }
118     else
119         return false;
120 }
121 
122 
LoadMipMap(GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * data)123 void GPU::Texture2D::LoadMipMap( GLint level,
124                                  GLint internalFormat,
125                                  GLsizei width,
126                                  GLsizei height,
127                                  GLint border,
128                                  GLenum format,
129                                  GLenum type,
130                                  const GLvoid* data )
131 {
132     glBindTexture( GL_TEXTURE_2D, m_Id );
133     glTexImage2D(
134         GL_TEXTURE_2D,
135         level,
136         internalFormat,
137         width,
138         height,
139         border,
140         format,
141         type,
142         data
143         );
144     m_Width = width;
145     m_Height = height;
146 }
147 
148 
LoadMipMap(GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * data)149 void GPU::Texture2D::LoadMipMap( GLint level,
150                                  GLint internalFormat,
151                                  GLsizei width,
152                                  GLsizei height,
153                                  GLenum format,
154                                  GLenum type,
155                                  const GLvoid* data )
156 {
157     glBindTexture( GL_TEXTURE_2D, m_Id );
158     glTexImage2D(
159         GL_TEXTURE_2D,
160         level,
161         internalFormat,
162         width,
163         height,
164         0,
165         format,
166         type,
167         data
168         );
169     m_Width = width;
170     m_Height = height;
171 }
172 
173 
LoadMipMap(GLint level,GLint internalFormat,GLsizei width,GLsizei height)174 void GPU::Texture2D::LoadMipMap( GLint level,
175                                  GLint internalFormat,
176                                  GLsizei width,
177                                  GLsizei height )
178 {
179     glBindTexture( GL_TEXTURE_2D, m_Id );
180     glTexImage2D(
181         GL_TEXTURE_2D,
182         level,
183         internalFormat,
184         width,
185         height,
186         0,
187         GL_RGB,
188         GL_FLOAT,
189         NULL
190         );
191     m_Width = width;
192     m_Height = height;
193 }
194 
195 
Load(GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * data)196 void GPU::Texture2D::Load( GLint internalFormat,
197                            GLsizei width,
198                            GLsizei height,
199                            GLint border,
200                            GLenum format,
201                            GLenum type,
202                            const GLvoid* data )
203 {
204     glBindTexture( GL_TEXTURE_2D, m_Id );
205     glTexImage2D(
206         GL_TEXTURE_2D,
207         0,
208         internalFormat,
209         width,
210         height,
211         border,
212         format,
213         type,
214         data
215         );
216     m_Width = width;
217     m_Height = height;
218 }
219 
220 
Load(GLint internalFormat,GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * data)221 void GPU::Texture2D::Load( GLint internalFormat,
222                            GLsizei width,
223                            GLsizei height,
224                            GLenum format,
225                            GLenum type,
226                            const GLvoid* data )
227 {
228     glBindTexture( GL_TEXTURE_2D, m_Id );
229     glTexImage2D(
230         GL_TEXTURE_2D,
231         0,
232         internalFormat,
233         width,
234         height,
235         0,
236         format,
237         type,
238         data
239         );
240     m_Width = width;
241     m_Height = height;
242 }
243 
244 
Load(GLint internalFormat,GLsizei width,GLsizei height)245 void GPU::Texture2D::Load( GLint internalFormat,
246                            GLsizei width,
247                            GLsizei height )
248 {
249     glBindTexture( GL_TEXTURE_2D, m_Id );
250     glTexImage2D(
251         GL_TEXTURE_2D,
252         0,
253         internalFormat,
254         width,
255         height,
256         0,
257         GL_RGB,
258         GL_FLOAT,
259         NULL
260         );
261     m_Width = width;
262     m_Height = height;
263 }
264 
265 
Bind()266 void GPU::Texture2D::Bind()
267 {
268     glGetIntegerv( GL_ACTIVE_TEXTURE_ARB, &m_TexUnit );
269     glEnable( GL_TEXTURE_2D );
270     glBindTexture( GL_TEXTURE_2D, m_Id );
271 }
272 
273 
Bind(GLuint texUnit)274 void GPU::Texture2D::Bind( GLuint texUnit )
275 {
276     m_TexUnit = GL_TEXTURE0_ARB + texUnit;
277     glActiveTextureARB( m_TexUnit );
278     glEnable( GL_TEXTURE_2D );
279     glBindTexture( GL_TEXTURE_2D, m_Id );
280 }
281 
282 
Unbind() const283 void GPU::Texture2D::Unbind() const
284 {
285     glActiveTextureARB( m_TexUnit );
286     glDisable( GL_TEXTURE_2D );
287     glBindTexture( GL_TEXTURE_2D, 0 );
288 }
289 
290 
RealWidth() const291 unsigned int GPU::Texture2D::RealWidth() const
292 {
293     GLint width = 0;
294     if( m_Id )
295     {
296         glPushAttrib( GL_TEXTURE_BIT );
297         glBindTexture( GL_TEXTURE_2D, m_Id );
298         glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width );
299         glPopAttrib();
300     }
301     return width;
302 }
303 
304 
RealHeight() const305 unsigned int GPU::Texture2D::RealHeight() const
306 {
307     GLint height = 0;
308     if( m_Id )
309     {
310         glPushAttrib( GL_TEXTURE_BIT );
311         glBindTexture( GL_TEXTURE_2D, m_Id );
312         glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height );
313         glPopAttrib();
314     }
315     return height;
316 }
317 
318 
InternalFormat() const319 GLint GPU::Texture2D::InternalFormat() const
320 {
321     GLint format = 0;
322     if( m_Id )
323     {
324         glPushAttrib( GL_TEXTURE_BIT );
325         glBindTexture( GL_TEXTURE_2D, m_Id );
326         glGetTexLevelParameteriv( GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format );
327         glPopAttrib();
328     }
329     return format;
330 }
331 
332 
DumpTo(void * buffer,const GLenum format,const GLenum type,const GLint level) const333 bool GPU::Texture2D::DumpTo( void *buffer,
334                              const GLenum format,
335                              const GLenum type,
336                              const GLint level ) const
337 {
338     if( m_Id )
339     {
340 #if glGetTextureImageEXT    // Extension GL_EXT_direct_state_access is no supported by every common computer...
341         glGetTextureImageEXT( m_Id, GL_TEXTURE_2D, level, format, type, buffer );
342 #else
343         GPU::FrameBuffer fbuffer( m_Width, m_Height );
344         fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this );
345         fbuffer.DumpTo( GL_COLOR_ATTACHMENT0_EXT, buffer, format, type );
346 #endif
347     }
348 
349     return m_Id != 0;
350 }
351 
352 
Clone() const353 GPU::Texture2D GPU::Texture2D::Clone() const
354 {
355     glPushAttrib( GL_TEXTURE_BIT );
356 
357     GPU::Texture2D newTex;
358     newTex.Create();
359     newTex.m_Width = Width();
360     newTex.m_Height = Height();
361 
362     if( m_Id )
363     {
364         GPU::FrameBuffer fbuffer( Width(), Height() );
365         fbuffer.Attach( GL_COLOR_ATTACHMENT0_EXT, this );
366         fbuffer.Bind();
367 
368         newTex.Bind();
369         glCopyTexImage2D( GL_TEXTURE_2D, 0, InternalFormat(), 0, 0, Width(), Height(), 0 );
370         newTex.Unbind();
371 
372         fbuffer.Unbind();
373     }
374 
375     glPopAttrib();
376 
377     return newTex;
378 }
379