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

Revision 2878, 6.2 KB checked in by mattausch, 16 years ago (diff)

for some reasons very slow even in small streets on my home computer!!

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        glGenRenderbuffersEXT(1, &mId);
71        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
72        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
73       
74        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId);
75
76        glGenTextures(1, &mTexId);
77        glBindTexture(GL_TEXTURE_2D, mTexId);
78        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
79
80        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
81
82        GLuint minfilterParam;
83        GLuint magfilterParam;
84
85        switch (filterType)
86        {
87        case FILTER_NEAREST:
88                minfilterParam = GL_NEAREST;
89                magfilterParam = GL_NEAREST; break;
90        case FILTER_LINEAR:
91                minfilterParam = GL_LINEAR;
92                magfilterParam = GL_LINEAR; break;
93        case FILTER_MIPMAP_LINEAR:
94                //minfilterParam = GL_LINEAR_MIPMAP_LINEAR;
95                minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
96                //minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
97                magfilterParam = GL_NEAREST; break;
98        default:
99                minfilterParam = GL_NEAREST;
100                magfilterParam = GL_NEAREST;
101
102                cerr << "should not come here" << endl;
103        }
104
105       
106        GLuint wrapParam;
107
108        switch (wrapType)
109        {
110        case WRAP_REPEAT:
111                wrapParam = GL_REPEAT; break;
112        case WRAP_CLAMP_TO_EDGE:
113                wrapParam = GL_CLAMP_TO_EDGE; break;
114        }
115
116        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
117        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
118
119        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
121
122        if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
123
124        // print status
125        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
126}
127
128
129ColorBufferObject::~ColorBufferObject()
130{
131        glDeleteRenderbuffersEXT(1, &mId);
132        glDeleteTextures(1, &mTexId);
133}
134
135
136FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
137: mWidth(w), mHeight(h), mDepthTexId(0)
138{
139        glGenFramebuffersEXT(1, &mId);
140       
141        Bind();
142
143        cout << "creating fbo" << endl;
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                cout << "adding depth buffer" << endl;
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, mWidth, mHeight);
170                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
171
172                if (useDepthTex)
173                {
174                        //cout << "adding depth texture" << endl;
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        cout << "adding color buffer" << endl;
213
214        Bind();
215
216        const int idx = (int)mColorBuffers.size();
217
218        ColorBufferObject *colorBuf =
219                new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
220
221        mColorBuffers.push_back(colorBuf);
222
223        Release();
224
225        return idx;
226}
227
228
229void FrameBufferObject::Bind() const
230{
231        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
232}
233
234
235void FrameBufferObject::Release()
236{
237        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
238}
239
240
241} // namespace
Note: See TracBrowser for help on using the repository browser.