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

Revision 2881, 6.0 KB checked in by mattausch, 16 years ago (diff)

working quite well

Line 
1#include <iostream>
2#include "FrameBufferObject.h"
3#include "glInterface.h"
4
5using namespace std;
6
7
8namespace CHCDemoEngine
9{
10
11GLenum color_attachment[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
12
13void PrintFBOStatus(GLenum status)
14{
15        switch(status)
16        {
17        case GL_FRAMEBUFFER_COMPLETE_EXT:
18                //cout << "frame buffer object complete" << endl;
19                break;
20        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
21                cerr << "incomplete attachment" << endl;
22                break;
23        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
24                cerr << "missing attachment" << endl;
25                break;
26        case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
27                cerr << "incomplete dimensions" << endl;
28                break;
29        case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
30                cerr << "incomplete formats" << endl;
31                break;
32        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
33                cerr << "incomplete draw buffer" << endl;
34                break;
35        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
36                cerr << "incomplete read buffer" << endl;
37                break;
38        case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
39                cerr << "framebuffer unsupported" << endl;
40                break;
41        default:
42                cerr << "unknown status code " << status << endl;
43        }
44}
45
46
47ColorBufferObject::ColorBufferObject(int w, int h,
48                                                                         FORMAT format,
49                                                                         WRAP_TYPE wrapType,
50                                                                         FILTER_TYPE filterType,
51                                                                         bool useMipMap,
52                                                                         int attachment_idx)
53{
54        GLuint glformat, internalFormat;
55
56
57        switch (format)
58        {
59        case BUFFER_UBYTE:
60                glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; break;
61        case BUFFER_FLOAT_16:
62                glformat = GL_FLOAT; internalFormat = GL_RGBA16F_ARB; break;
63        case BUFFER_FLOAT_32:
64                glformat = GL_FLOAT; internalFormat = GL_RGBA32F_ARB; break;
65        default:
66                glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8;
67                cerr << "should not come here" << endl;
68        }
69
70
71        glGenRenderbuffersEXT(1, &mId);
72        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
73       
74        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
75        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId);
76
77
78        glGenTextures(1, &mTexId);
79        glBindTexture(GL_TEXTURE_2D, mTexId);
80        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
81
82        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
83
84        GLuint minfilterParam;
85        GLuint magfilterParam;
86
87        switch (filterType)
88        {
89        case FILTER_NEAREST:
90                minfilterParam = GL_NEAREST;
91                magfilterParam = GL_NEAREST; break;
92        case FILTER_LINEAR:
93                minfilterParam = GL_LINEAR;
94                magfilterParam = GL_LINEAR; break;
95        case FILTER_MIPMAP_LINEAR:
96                //minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
97                //magfilterParam = GL_NEAREST;
98
99                minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
100                magfilterParam = GL_LINEAR;
101                break;
102        default:
103                minfilterParam = GL_NEAREST;
104                magfilterParam = GL_NEAREST;
105
106                cerr << "should not come here" << endl;
107        }
108
109       
110        GLuint wrapParam;
111
112        switch (wrapType)
113        {
114        case WRAP_REPEAT:
115                wrapParam = GL_REPEAT; break;
116        case WRAP_CLAMP_TO_EDGE:
117                wrapParam = GL_CLAMP_TO_EDGE; break;
118        }
119
120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
122
123        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
124        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
125
126        if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
127
128        // print status
129        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
130}
131
132
133ColorBufferObject::~ColorBufferObject()
134{
135        glDeleteRenderbuffersEXT(1, &mId);
136        glDeleteTextures(1, &mTexId);
137}
138
139
140FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
141: mWidth(w), mHeight(h), mDepthTexId(0)
142{
143        glGenFramebuffersEXT(1, &mId);
144       
145        Bind();
146
147        ///////////
148        //-- create depth buffer
149
150        if (d != DEPTH_NONE)
151        {
152                glGenRenderbuffersEXT(1, &mDepthId);   
153                glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
154
155                GLuint depthFormat;
156
157                switch (d)
158                {
159                case DEPTH_16:
160                        depthFormat = GL_DEPTH_COMPONENT16; break;
161                case DEPTH_24:
162                        depthFormat = GL_DEPTH_COMPONENT24; break;
163                case DEPTH_32:
164                        depthFormat = GL_DEPTH_COMPONENT32; break;
165                default:       
166                        cerr << "should not come here" << endl;
167                }
168
169                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
170               
171
172                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
173
174                if (useDepthTex)
175                {
176                        glGenTextures(1, &mDepthTexId);
177                        glBindTexture(GL_TEXTURE_2D, mDepthTexId);
178
179                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
180                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
181                       
182                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
183                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
184                        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
185
186                        glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
187
188                        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
189                }
190        }
191
192        Release();
193}
194
195
196FrameBufferObject::~FrameBufferObject()
197{
198        glDeleteFramebuffersEXT(1, &mId);
199        glDeleteRenderbuffersEXT(1, &mDepthId);
200
201        if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
202
203        CLEAR_CONTAINER(mColorBuffers);
204}
205
206
207int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
208                                                                          ColorBufferObject::WRAP_TYPE wrapType,
209                                                                          ColorBufferObject::FILTER_TYPE filterType,
210                                                                          bool useMipMap)
211{
212        Bind();
213
214        const int idx = (int)mColorBuffers.size();
215
216        ColorBufferObject *colorBuf =
217                new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
218
219        mColorBuffers.push_back(colorBuf);
220
221        Release();
222
223        return idx;
224}
225
226
227void FrameBufferObject::Bind() const
228{
229        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
230}
231
232
233void FrameBufferObject::Release()
234{
235        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
236}
237
238
239} // namespace
Note: See TracBrowser for help on using the repository browser.