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

Revision 2979, 8.1 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[2855]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:
[2879]17                //cout << "frame buffer object complete" << endl;
[2855]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,
[2965]47                                                                         int attachment_idx,
[2861]48                                                                         FORMAT format,
[2856]49                                                                         WRAP_TYPE wrapType,
50                                                                         FILTER_TYPE filterType,
[2965]51                                                                         FILTER_TYPE filterTypeMipMap
52                                                                         )
[2855]53{
[2965]54        Init(w, h, attachment_idx, format, wrapType, filterType, true, filterTypeMipMap);
55}
[2861]56
57
[2965]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
[2861]88        switch (format)
89        {
90        case BUFFER_UBYTE:
[2965]91                mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8; break;
[2861]92        case BUFFER_FLOAT_16:
[2965]93                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA16F_ARB; break;
[2861]94        case BUFFER_FLOAT_32:
[2965]95                mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA32F_ARB; break;
[2861]96        default:
[2965]97                mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8;
[2861]98                cerr << "should not come here" << endl;
99        }
100
[2879]101
[2856]102        glGenRenderbuffersEXT(1, &mId);
103        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
[2855]104       
[2965]105        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, mInternalFormat, w, h);
[2899]106        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
[2855]107
[2879]108
[2856]109        glGenTextures(1, &mTexId);
110        glBindTexture(GL_TEXTURE_2D, mTexId);
[2965]111        glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, w, h, 0, GL_RGBA, mGlFormat, NULL);
[2862]112
[2899]113        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
[2855]114
[2867]115        GLuint minfilterParam;
116        GLuint magfilterParam;
[2855]117
[2965]118        if (filterType == FILTER_NEAREST)
[2867]119                magfilterParam = GL_NEAREST;
[2965]120        else // FILTER_LINEAR
121                magfilterParam = GL_LINEAR;
[2867]122
[2965]123        if (!useMipMap)
124        {
125                if (filterType == FILTER_NEAREST)
126                        minfilterParam = GL_NEAREST;
127                else // FILTER_LINEAR
128                        minfilterParam = GL_LINEAR;
[2856]129        }
[2965]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        }
[2855]147
[2856]148        GLuint wrapParam;
149
150        switch (wrapType)
151        {
[2857]152        case WRAP_REPEAT:
153                wrapParam = GL_REPEAT; break;
154        case WRAP_CLAMP_TO_EDGE:
155                wrapParam = GL_CLAMP_TO_EDGE; break;
[2856]156        }
157
[2867]158        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
159        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
[2856]160
161        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
162        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
163
[2976]164        if (useMipMap)
165        {
166                glGenerateMipmapEXT(GL_TEXTURE_2D);
167        }
[2867]168
[2857]169        // print status
[2855]170        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
171}
172
173
[2965]174
175void *ColorBufferObject::ReadTexture() const
[2878]176{
[2965]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
[2979]191       
[2965]192        glBindTexture(GL_TEXTURE_2D, mTexId);
[2979]193       
[2976]194        GLint width, height;
[2965]195
[2979]196        glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
[2976]197        glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
198
199        cout << "w: " << width << "h: " << height << endl;
[2979]200
[2976]201        unsigned char *data = new unsigned char[bytes * 4 * width * height];
202
[2979]203        glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);
[2976]204
[2965]205        glBindTexture(GL_TEXTURE_2D, 0);
206        glDisable(GL_TEXTURE_2D);
207
[2976]208        float *d = (float *)data;
209
[2965]210        return data;
[2878]211}
212
213
[2965]214/*****************************************************************/
215/*                 FrameBufferObject implementation              */
216/*****************************************************************/
217
218
[2862]219FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
220: mWidth(w), mHeight(h), mDepthTexId(0)
[2855]221{
222        glGenFramebuffersEXT(1, &mId);
[2862]223       
224        Bind();
[2855]225        ///////////
226        //-- create depth buffer
227
[2861]228        if (d != DEPTH_NONE)
[2855]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;
[2861]243                default:       
244                        cerr << "should not come here" << endl;
[2855]245                }
246
[2879]247                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
248               
249
[2855]250                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
[2862]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);
[2965]262
[2862]263                        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
264
[2866]265                        glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
[2862]266
267                        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
268                }
[2857]269        }
[2862]270
271        Release();
[2855]272}
273
274
[2878]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
[2867]286int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
[2859]287                                                                          ColorBufferObject::WRAP_TYPE wrapType,
288                                                                          ColorBufferObject::FILTER_TYPE filterType,
[2965]289                                                                          ColorBufferObject::FILTER_TYPE filterTypeMipMap)
[2857]290{
[2862]291        Bind();
[2861]292
[2867]293        const int idx = (int)mColorBuffers.size();
294
[2857]295        ColorBufferObject *colorBuf =
[2965]296                new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType, filterTypeMipMap);
[2861]297
[2857]298        mColorBuffers.push_back(colorBuf);
299
[2862]300        Release();
[2861]301
302        return idx;
[2857]303}
304
[2859]305
[2965]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
[2861]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
[2855]338} // namespace
Note: See TracBrowser for help on using the repository browser.