source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp @ 2979

Revision 2979, 8.1 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include <iostream>
2#include "FrameBufferObject.h"
3#include "glInterface.h"
4
5using namespace std;
6
7
8namespace CHCDemoEngine
9{
10
11
12void PrintFBOStatus(GLenum status)
13{
14        switch(status)
15        {
16        case GL_FRAMEBUFFER_COMPLETE_EXT:
17                //cout << "frame buffer object complete" << endl;
18                break;
19        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
20                cerr << "incomplete attachment" << endl;
21                break;
22        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
23                cerr << "missing attachment" << endl;
24                break;
25        case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
26                cerr << "incomplete dimensions" << endl;
27                break;
28        case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
29                cerr << "incomplete formats" << endl;
30                break;
31        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
32                cerr << "incomplete draw buffer" << endl;
33                break;
34        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
35                cerr << "incomplete read buffer" << endl;
36                break;
37        case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
38                cerr << "framebuffer unsupported" << endl;
39                break;
40        default:
41                cerr << "unknown status code " << status << endl;
42        }
43}
44
45
46ColorBufferObject::ColorBufferObject(int w, int h,
47                                                                         int attachment_idx,
48                                                                         FORMAT format,
49                                                                         WRAP_TYPE wrapType,
50                                                                         FILTER_TYPE filterType,
51                                                                         FILTER_TYPE filterTypeMipMap
52                                                                         )
53{
54        Init(w, h, attachment_idx, format, wrapType, filterType, true, filterTypeMipMap);
55}
56
57
58ColorBufferObject::ColorBufferObject(int w, int h,
59                                                                         int attachment_idx,
60                                                                         FORMAT format,
61                                                                         WRAP_TYPE wrapType,
62                                                                         FILTER_TYPE filterType
63                                                                         )
64{
65        Init(w, h, attachment_idx, format, wrapType, filterType, false, FILTER_NEAREST);
66}
67
68
69ColorBufferObject::~ColorBufferObject()
70{
71        glDeleteRenderbuffersEXT(1, &mId);
72        glDeleteTextures(1, &mTexId);
73}
74
75
76void ColorBufferObject::Init(int w, int h,
77                                                         int attachment_idx,
78                                                         FORMAT format,
79                                                         WRAP_TYPE wrapType,
80                                                         FILTER_TYPE filterType,
81                                                         bool useMipMap,
82                                                         FILTER_TYPE filterTypeMipMap
83                                                         )
84{
85        mWidth = w;
86        mHeight = h;
87
88        switch (format)
89        {
90        case BUFFER_UBYTE:
91                mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8; break;
92        case BUFFER_FLOAT_16:
93                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA16F_ARB; break;
94        case BUFFER_FLOAT_32:
95                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA32F_ARB; break;
96        default:
97                mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8;
98                cerr << "should not come here" << endl;
99        }
100
101
102        glGenRenderbuffersEXT(1, &mId);
103        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
104       
105        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, mInternalFormat, w, h);
106        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
107
108
109        glGenTextures(1, &mTexId);
110        glBindTexture(GL_TEXTURE_2D, mTexId);
111        glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, w, h, 0, GL_RGBA, mGlFormat, NULL);
112
113        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
114
115        GLuint minfilterParam;
116        GLuint magfilterParam;
117
118        if (filterType == FILTER_NEAREST)
119                magfilterParam = GL_NEAREST;
120        else // FILTER_LINEAR
121                magfilterParam = GL_LINEAR;
122
123        if (!useMipMap)
124        {
125                if (filterType == FILTER_NEAREST)
126                        minfilterParam = GL_NEAREST;
127                else // FILTER_LINEAR
128                        minfilterParam = GL_LINEAR;
129        }
130        else
131        {
132                if (filterType == FILTER_NEAREST)
133                {
134                        if (filterTypeMipMap == FILTER_NEAREST)
135                                minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
136                        else // FILTER_LINEAR
137                                minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
138                }
139                else // FILTER_LINEAR
140                {
141                        if (filterTypeMipMap == FILTER_NEAREST)
142                                minfilterParam = GL_LINEAR_MIPMAP_NEAREST;
143                        else // FILTER_LINEAR
144                                minfilterParam = GL_LINEAR_MIPMAP_LINEAR;
145                }
146        }
147
148        GLuint wrapParam;
149
150        switch (wrapType)
151        {
152        case WRAP_REPEAT:
153                wrapParam = GL_REPEAT; break;
154        case WRAP_CLAMP_TO_EDGE:
155                wrapParam = GL_CLAMP_TO_EDGE; break;
156        }
157
158        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
159        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
160
161        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
162        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
163
164        if (useMipMap)
165        {
166                glGenerateMipmapEXT(GL_TEXTURE_2D);
167        }
168
169        // print status
170        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
171}
172
173
174
175void *ColorBufferObject::ReadTexture() const
176{
177        int bytes = 0;
178
179        switch (mInternalFormat)
180        {
181        case GL_RGBA8:
182                bytes = 8; break;
183        case GL_RGBA16F_ARB:
184                bytes = 16; break;
185        case GL_RGBA32F_ARB:
186                bytes = 32; break;
187        default:
188                cerr << "should not come here" << endl;
189        }
190
191       
192        glBindTexture(GL_TEXTURE_2D, mTexId);
193       
194        GLint width, height;
195
196        glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
197        glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
198
199        cout << "w: " << width << "h: " << height << endl;
200
201        unsigned char *data = new unsigned char[bytes * 4 * width * height];
202
203        glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);
204
205        glBindTexture(GL_TEXTURE_2D, 0);
206        glDisable(GL_TEXTURE_2D);
207
208        float *d = (float *)data;
209
210        return data;
211}
212
213
214/*****************************************************************/
215/*                 FrameBufferObject implementation              */
216/*****************************************************************/
217
218
219FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
220: mWidth(w), mHeight(h), mDepthTexId(0)
221{
222        glGenFramebuffersEXT(1, &mId);
223       
224        Bind();
225        ///////////
226        //-- create depth buffer
227
228        if (d != DEPTH_NONE)
229        {
230                glGenRenderbuffersEXT(1, &mDepthId);   
231                glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
232
233                GLuint depthFormat;
234
235                switch (d)
236                {
237                case DEPTH_16:
238                        depthFormat = GL_DEPTH_COMPONENT16; break;
239                case DEPTH_24:
240                        depthFormat = GL_DEPTH_COMPONENT24; break;
241                case DEPTH_32:
242                        depthFormat = GL_DEPTH_COMPONENT32; break;
243                default:       
244                        cerr << "should not come here" << endl;
245                }
246
247                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
248               
249
250                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
251
252                if (useDepthTex)
253                {
254                        glGenTextures(1, &mDepthTexId);
255                        glBindTexture(GL_TEXTURE_2D, mDepthTexId);
256
257                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
258                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
259                       
260                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
261                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
262
263                        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
264
265                        glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
266
267                        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
268                }
269        }
270
271        Release();
272}
273
274
275FrameBufferObject::~FrameBufferObject()
276{
277        glDeleteFramebuffersEXT(1, &mId);
278        glDeleteRenderbuffersEXT(1, &mDepthId);
279
280        if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
281
282        CLEAR_CONTAINER(mColorBuffers);
283}
284
285
286int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
287                                                                          ColorBufferObject::WRAP_TYPE wrapType,
288                                                                          ColorBufferObject::FILTER_TYPE filterType,
289                                                                          ColorBufferObject::FILTER_TYPE filterTypeMipMap)
290{
291        Bind();
292
293        const int idx = (int)mColorBuffers.size();
294
295        ColorBufferObject *colorBuf =
296                new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType, filterTypeMipMap);
297
298        mColorBuffers.push_back(colorBuf);
299
300        Release();
301
302        return idx;
303}
304
305
306
307int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
308                                                                          ColorBufferObject::WRAP_TYPE wrapType,
309                                                                          ColorBufferObject::FILTER_TYPE filterType)
310{
311        Bind();
312
313        const int idx = (int)mColorBuffers.size();
314
315        ColorBufferObject *colorBuf =
316                new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType);
317
318        mColorBuffers.push_back(colorBuf);
319
320        Release();
321
322        return idx;
323}
324
325
326void FrameBufferObject::Bind() const
327{
328        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
329}
330
331
332void FrameBufferObject::Release()
333{
334        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
335}
336
337
338} // namespace
Note: See TracBrowser for help on using the repository browser.