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

Revision 3204, 9.0 KB checked in by mattausch, 15 years ago (diff)

debug version showing a visualization of the confidence

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