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

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

improved performance

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_LINEAR_MIPMAP_LINEAR;
97                minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
98                //minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
99                magfilterParam = GL_NEAREST; break;
100        default:
101                minfilterParam = GL_NEAREST;
102                magfilterParam = GL_NEAREST;
103
104                cerr << "should not come here" << endl;
105        }
106
107       
108        GLuint wrapParam;
109
110        switch (wrapType)
111        {
112        case WRAP_REPEAT:
113                wrapParam = GL_REPEAT; break;
114        case WRAP_CLAMP_TO_EDGE:
115                wrapParam = GL_CLAMP_TO_EDGE; break;
116        }
117
118        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
119        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
120
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
122        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
123
124        if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
125
126        // print status
127        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
128}
129
130
131ColorBufferObject::~ColorBufferObject()
132{
133        glDeleteRenderbuffersEXT(1, &mId);
134        glDeleteTextures(1, &mTexId);
135}
136
137
138FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
139: mWidth(w), mHeight(h), mDepthTexId(0)
140{
141        glGenFramebuffersEXT(1, &mId);
142       
143        Bind();
144
145        ///////////
146        //-- create depth buffer
147
148        if (d != DEPTH_NONE)
149        {
150                glGenRenderbuffersEXT(1, &mDepthId);   
151                glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
152
153                GLuint depthFormat;
154
155                switch (d)
156                {
157                case DEPTH_16:
158                        depthFormat = GL_DEPTH_COMPONENT16; break;
159                case DEPTH_24:
160                        depthFormat = GL_DEPTH_COMPONENT24; break;
161                case DEPTH_32:
162                        depthFormat = GL_DEPTH_COMPONENT32; break;
163                default:       
164                        cerr << "should not come here" << endl;
165                }
166
167                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
168               
169
170                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
171
172                if (useDepthTex)
173                {
174                        glGenTextures(1, &mDepthTexId);
175                        glBindTexture(GL_TEXTURE_2D, mDepthTexId);
176
177                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
178                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
179                       
180                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
181                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
182                        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
183
184                        glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
185
186                        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
187                }
188        }
189
190        Release();
191}
192
193
194FrameBufferObject::~FrameBufferObject()
195{
196        glDeleteFramebuffersEXT(1, &mId);
197        glDeleteRenderbuffersEXT(1, &mDepthId);
198
199        if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
200
201        CLEAR_CONTAINER(mColorBuffers);
202}
203
204
205int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
206                                                                          ColorBufferObject::WRAP_TYPE wrapType,
207                                                                          ColorBufferObject::FILTER_TYPE filterType,
208                                                                          bool useMipMap)
209{
210        Bind();
211
212        const int idx = (int)mColorBuffers.size();
213
214        ColorBufferObject *colorBuf =
215                new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
216
217        mColorBuffers.push_back(colorBuf);
218
219        Release();
220
221        return idx;
222}
223
224
225void FrameBufferObject::Bind() const
226{
227        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
228}
229
230
231void FrameBufferObject::Release()
232{
233        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
234}
235
236
237} // namespace
Note: See TracBrowser for help on using the repository browser.