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

Revision 2965, 7.9 KB checked in by mattausch, 16 years ago (diff)

removed dirttexture stuff. started tonemapping stuff

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) glGenerateMipmapEXT(GL_TEXTURE_2D);
165
166        // print status
167        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
168}
169
170
171
172void *ColorBufferObject::ReadTexture() const
173{
174        int bytes = 0;
175
176        switch (mInternalFormat)
177        {
178        case GL_RGBA8:
179                bytes = 8; break;
180        case GL_RGBA16F_ARB:
181                bytes = 16; break;
182        case GL_RGBA32F_ARB:
183                bytes = 32; break;
184        default:
185                cerr << "should not come here" << endl;
186        }
187
188        unsigned char *data = new unsigned char[bytes * 4 * mWidth * mHeight];
189
190        glEnable(GL_TEXTURE_2D);
191        glBindTexture(GL_TEXTURE_2D, mTexId);
192
193        glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, mGlFormat, data);
194
195        glBindTexture(GL_TEXTURE_2D, 0);
196        glDisable(GL_TEXTURE_2D);
197
198        return data;
199}
200
201
202/*****************************************************************/
203/*                 FrameBufferObject implementation              */
204/*****************************************************************/
205
206
207FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
208: mWidth(w), mHeight(h), mDepthTexId(0)
209{
210        glGenFramebuffersEXT(1, &mId);
211       
212        Bind();
213        ///////////
214        //-- create depth buffer
215
216        if (d != DEPTH_NONE)
217        {
218                glGenRenderbuffersEXT(1, &mDepthId);   
219                glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
220
221                GLuint depthFormat;
222
223                switch (d)
224                {
225                case DEPTH_16:
226                        depthFormat = GL_DEPTH_COMPONENT16; break;
227                case DEPTH_24:
228                        depthFormat = GL_DEPTH_COMPONENT24; break;
229                case DEPTH_32:
230                        depthFormat = GL_DEPTH_COMPONENT32; break;
231                default:       
232                        cerr << "should not come here" << endl;
233                }
234
235                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
236               
237
238                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
239
240                if (useDepthTex)
241                {
242                        glGenTextures(1, &mDepthTexId);
243                        glBindTexture(GL_TEXTURE_2D, mDepthTexId);
244
245                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
246                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
247                       
248                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
249                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
250
251                        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
252
253                        glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
254
255                        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
256                }
257        }
258
259        Release();
260}
261
262
263FrameBufferObject::~FrameBufferObject()
264{
265        glDeleteFramebuffersEXT(1, &mId);
266        glDeleteRenderbuffersEXT(1, &mDepthId);
267
268        if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
269
270        CLEAR_CONTAINER(mColorBuffers);
271}
272
273
274int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
275                                                                          ColorBufferObject::WRAP_TYPE wrapType,
276                                                                          ColorBufferObject::FILTER_TYPE filterType,
277                                                                          ColorBufferObject::FILTER_TYPE filterTypeMipMap)
278{
279        Bind();
280
281        const int idx = (int)mColorBuffers.size();
282
283        ColorBufferObject *colorBuf =
284                new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType, filterTypeMipMap);
285
286        mColorBuffers.push_back(colorBuf);
287
288        Release();
289
290        return idx;
291}
292
293
294
295int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
296                                                                          ColorBufferObject::WRAP_TYPE wrapType,
297                                                                          ColorBufferObject::FILTER_TYPE filterType)
298{
299        Bind();
300
301        const int idx = (int)mColorBuffers.size();
302
303        ColorBufferObject *colorBuf =
304                new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType);
305
306        mColorBuffers.push_back(colorBuf);
307
308        Release();
309
310        return idx;
311}
312
313
314void FrameBufferObject::Bind() const
315{
316        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
317}
318
319
320void FrameBufferObject::Release()
321{
322        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
323}
324
325
326} // namespace
Note: See TracBrowser for help on using the repository browser.