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

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

found bug with lod levels
made env file for shaders

RevLine 
[2855]1#include <iostream>
2#include "FrameBufferObject.h"
3#include "glInterface.h"
4
5using namespace std;
6
7
8namespace CHCDemoEngine
9{
10
[2861]11GLenum color_attachment[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
[2855]12
13void PrintFBOStatus(GLenum status)
14{
15        switch(status)
16        {
17        case GL_FRAMEBUFFER_COMPLETE_EXT:
[2879]18                //cout << "frame buffer object complete" << endl;
[2855]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,
[2861]48                                                                         FORMAT format,
[2856]49                                                                         WRAP_TYPE wrapType,
50                                                                         FILTER_TYPE filterType,
[2855]51                                                                         bool useMipMap,
[2861]52                                                                         int attachment_idx)
[2855]53{
[2861]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
[2879]70
[2856]71        glGenRenderbuffersEXT(1, &mId);
72        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
[2855]73       
[2879]74        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
[2861]75        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId);
[2855]76
[2879]77
[2856]78        glGenTextures(1, &mTexId);
79        glBindTexture(GL_TEXTURE_2D, mTexId);
[2861]80        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
[2862]81
[2861]82        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
[2855]83
[2867]84        GLuint minfilterParam;
85        GLuint magfilterParam;
[2855]86
[2856]87        switch (filterType)
88        {
[2857]89        case FILTER_NEAREST:
[2867]90                minfilterParam = GL_NEAREST;
91                magfilterParam = GL_NEAREST; break;
[2857]92        case FILTER_LINEAR:
[2867]93                minfilterParam = GL_LINEAR;
94                magfilterParam = GL_LINEAR; break;
[2857]95        case FILTER_MIPMAP_LINEAR:
[2884]96                minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
97                magfilterParam = GL_NEAREST;
[2881]98
[2884]99                //minfilterParam = GL_LINEAR_MIPMAP_NEAREST;
100                //magfilterParam = GL_LINEAR;
[2881]101                break;
[2861]102        default:
[2867]103                minfilterParam = GL_NEAREST;
104                magfilterParam = GL_NEAREST;
105
[2861]106                cerr << "should not come here" << endl;
[2856]107        }
[2855]108
[2856]109       
110        GLuint wrapParam;
111
112        switch (wrapType)
113        {
[2857]114        case WRAP_REPEAT:
115                wrapParam = GL_REPEAT; break;
116        case WRAP_CLAMP_TO_EDGE:
117                wrapParam = GL_CLAMP_TO_EDGE; break;
[2856]118        }
119
[2867]120        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
121        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
[2856]122
123        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
124        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
125
[2867]126        if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
127
[2857]128        // print status
[2855]129        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
130}
131
132
[2878]133ColorBufferObject::~ColorBufferObject()
134{
135        glDeleteRenderbuffersEXT(1, &mId);
136        glDeleteTextures(1, &mTexId);
137}
138
139
[2862]140FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
141: mWidth(w), mHeight(h), mDepthTexId(0)
[2855]142{
143        glGenFramebuffersEXT(1, &mId);
[2862]144       
145        Bind();
[2855]146
147        ///////////
148        //-- create depth buffer
149
[2861]150        if (d != DEPTH_NONE)
[2855]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;
[2861]165                default:       
166                        cerr << "should not come here" << endl;
[2855]167                }
168
[2879]169                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
170               
171
[2855]172                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
[2862]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
[2866]186                        glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
[2862]187
188                        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
189                }
[2857]190        }
[2862]191
192        Release();
[2855]193}
194
195
[2878]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
[2867]207int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
[2859]208                                                                          ColorBufferObject::WRAP_TYPE wrapType,
209                                                                          ColorBufferObject::FILTER_TYPE filterType,
[2867]210                                                                          bool useMipMap)
[2857]211{
[2862]212        Bind();
[2861]213
[2867]214        const int idx = (int)mColorBuffers.size();
215
[2857]216        ColorBufferObject *colorBuf =
[2867]217                new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
[2861]218
[2857]219        mColorBuffers.push_back(colorBuf);
220
[2862]221        Release();
[2861]222
223        return idx;
[2857]224}
225
[2859]226
[2861]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
[2855]239} // namespace
Note: See TracBrowser for help on using the repository browser.