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

Revision 2861, 5.1 KB checked in by mattausch, 16 years ago (diff)

cleaned up code

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                                                                         bool useMultiSampling,
53                                                                         int attachment_idx)
54{
55        GLuint glformat, internalFormat;
56
57
58        switch (format)
59        {
60        case BUFFER_UBYTE:
61                glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; break;
62        case BUFFER_FLOAT_16:
63                glformat = GL_FLOAT; internalFormat = GL_RGBA16F_ARB; break;
64        case BUFFER_FLOAT_32:
65                glformat = GL_FLOAT; internalFormat = GL_RGBA32F_ARB; break;
66        default:
67                glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8;
68                cerr << "should not come here" << endl;
69        }
70
71        glGenRenderbuffersEXT(1, &mId);
72        glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
73        glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
74       
75        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId);
76
77        glGenTextures(1, &mTexId);
78        glBindTexture(GL_TEXTURE_2D, mTexId);
79        //glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
80        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
81        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
82
83        GLuint filterParam;
84
85        switch (filterType)
86        {
87        case FILTER_NEAREST:
88                filterParam = GL_NEAREST; break;
89        case FILTER_LINEAR:
90                filterParam = GL_LINEAR; break;
91        case FILTER_MIPMAP_LINEAR:
92                filterParam = GL_LINEAR_MIPMAP_LINEAR; break;
93        default:
94                filterParam = GL_NEAREST;
95                cerr << "should not come here" << endl;
96        }
97
98       
99        GLuint wrapParam;
100
101        switch (wrapType)
102        {
103        case WRAP_REPEAT:
104                wrapParam = GL_REPEAT; break;
105        case WRAP_CLAMP_TO_EDGE:
106                wrapParam = GL_CLAMP_TO_EDGE; break;
107        }
108
109        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterParam);
110        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterParam);
111
112        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
113        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
114
115        // print status
116        PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
117}
118
119
120FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d)
121: mWidth(w), mHeight(h)
122{
123        glGenFramebuffersEXT(1, &mId);
124        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
125
126        cout << "creating fbo" << endl;
127
128        ///////////
129        //-- create depth buffer
130
131        if (d != DEPTH_NONE)
132        {
133                glGenRenderbuffersEXT(1, &mDepthId);   
134                glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
135
136                cout << "adding depth buffer" << endl;
137
138                GLuint depthFormat;
139
140                switch (d)
141                {
142                case DEPTH_16:
143                        depthFormat = GL_DEPTH_COMPONENT16; break;
144                case DEPTH_24:
145                        depthFormat = GL_DEPTH_COMPONENT24; break;
146                case DEPTH_32:
147                        depthFormat = GL_DEPTH_COMPONENT32; break;
148                default:       
149                        cerr << "should not come here" << endl;
150                }
151
152                glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, mWidth, mHeight);
153                glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
154        }
155}
156
157
158int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
159                                                                          ColorBufferObject::WRAP_TYPE wrapType,
160                                                                          ColorBufferObject::FILTER_TYPE filterType,
161                                                                          bool useMipMap,
162                                                                          bool useMultiSampling)
163{
164        cout << "adding color buffer" << endl;
165
166        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
167
168        int idx = (int)mColorBuffers.size();
169        ColorBufferObject *colorBuf =
170                new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, useMultiSampling, idx);
171
172        mColorBuffers.push_back(colorBuf);
173
174        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
175
176        return idx;
177}
178
179
180void FrameBufferObject::Bind() const
181{
182        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
183}
184
185
186void FrameBufferObject::Release()
187{
188        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
189}
190
191
192} // namespace
Note: See TracBrowser for help on using the repository browser.