1 | #include <iostream>
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "glInterface.h"
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace CHCDemoEngine
|
---|
9 | {
|
---|
10 |
|
---|
11 | GLenum color_attachment[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
|
---|
12 |
|
---|
13 | void 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 |
|
---|
47 | ColorBufferObject::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 |
|
---|
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 |
|
---|
120 | FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
|
---|
121 | : mWidth(w), mHeight(h), mDepthTexId(0)
|
---|
122 | {
|
---|
123 | glGenFramebuffersEXT(1, &mId);
|
---|
124 |
|
---|
125 | Bind();
|
---|
126 |
|
---|
127 | cout << "creating fbo" << endl;
|
---|
128 |
|
---|
129 | ///////////
|
---|
130 | //-- create depth buffer
|
---|
131 |
|
---|
132 | if (d != DEPTH_NONE)
|
---|
133 | {
|
---|
134 | glGenRenderbuffersEXT(1, &mDepthId);
|
---|
135 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
|
---|
136 |
|
---|
137 | cout << "adding depth buffer" << endl;
|
---|
138 |
|
---|
139 | GLuint depthFormat;
|
---|
140 |
|
---|
141 | switch (d)
|
---|
142 | {
|
---|
143 | case DEPTH_16:
|
---|
144 | depthFormat = GL_DEPTH_COMPONENT16; break;
|
---|
145 | case DEPTH_24:
|
---|
146 | depthFormat = GL_DEPTH_COMPONENT24; break;
|
---|
147 | case DEPTH_32:
|
---|
148 | depthFormat = GL_DEPTH_COMPONENT32; break;
|
---|
149 | default:
|
---|
150 | cerr << "should not come here" << endl;
|
---|
151 | }
|
---|
152 |
|
---|
153 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, mWidth, mHeight);
|
---|
154 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
155 |
|
---|
156 | if (useDepthTex)
|
---|
157 | {
|
---|
158 | //cout << "adding depth texture" << endl;
|
---|
159 |
|
---|
160 | glGenTextures(1, &mDepthTexId);
|
---|
161 | glBindTexture(GL_TEXTURE_2D, mDepthTexId);
|
---|
162 |
|
---|
163 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
164 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
165 |
|
---|
166 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
167 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
168 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
169 |
|
---|
170 | glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
---|
171 |
|
---|
172 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | Release();
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | int FrameBufferObject::AddColorBuffer(int w, int h,
|
---|
181 | ColorBufferObject::FORMAT col,
|
---|
182 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
183 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
184 | bool useMipMap,
|
---|
185 | bool useMultiSampling)
|
---|
186 | {
|
---|
187 | cout << "adding color buffer" << endl;
|
---|
188 |
|
---|
189 | Bind();
|
---|
190 |
|
---|
191 | int idx = (int)mColorBuffers.size();
|
---|
192 | ColorBufferObject *colorBuf =
|
---|
193 | new ColorBufferObject(w, h, col, wrapType, filterType, useMipMap, useMultiSampling, idx);
|
---|
194 |
|
---|
195 | mColorBuffers.push_back(colorBuf);
|
---|
196 |
|
---|
197 | Release();
|
---|
198 |
|
---|
199 | return idx;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | void FrameBufferObject::Bind() const
|
---|
204 | {
|
---|
205 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | void FrameBufferObject::Release()
|
---|
210 | {
|
---|
211 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | } // namespace
|
---|