1 | #include "FrameBufferObject.h"
|
---|
2 | #include "glInterface.h"
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 |
|
---|
6 | #ifdef _CRT_SET
|
---|
7 | #define _CRTDBG_MAP_ALLOC
|
---|
8 | #include <stdlib.h>
|
---|
9 | #include <crtdbg.h>
|
---|
10 |
|
---|
11 | // redefine new operator
|
---|
12 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
13 | #define new DEBUG_NEW
|
---|
14 | #endif
|
---|
15 |
|
---|
16 |
|
---|
17 | using namespace std;
|
---|
18 |
|
---|
19 |
|
---|
20 | namespace CHCDemoEngine
|
---|
21 | {
|
---|
22 |
|
---|
23 | int FrameBufferObject::sCurrentFbo = -1;
|
---|
24 |
|
---|
25 |
|
---|
26 | void PrintFBOStatus(GLenum status)
|
---|
27 | {
|
---|
28 | switch(status)
|
---|
29 | {
|
---|
30 | case GL_FRAMEBUFFER_COMPLETE_EXT:
|
---|
31 | //cout << "frame buffer object complete" << endl;
|
---|
32 | break;
|
---|
33 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
---|
34 | cerr << "incomplete attachment" << endl;
|
---|
35 | break;
|
---|
36 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
|
---|
37 | cerr << "missing attachment" << endl;
|
---|
38 | break;
|
---|
39 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
|
---|
40 | cerr << "incomplete dimensions" << endl;
|
---|
41 | break;
|
---|
42 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
|
---|
43 | cerr << "incomplete formats" << endl;
|
---|
44 | break;
|
---|
45 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
---|
46 | cerr << "incomplete draw buffer" << endl;
|
---|
47 | break;
|
---|
48 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
---|
49 | cerr << "incomplete read buffer" << endl;
|
---|
50 | break;
|
---|
51 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
---|
52 | cerr << "framebuffer unsupported" << endl;
|
---|
53 | break;
|
---|
54 | default:
|
---|
55 | cerr << "unknown status code " << status << endl;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | ColorBufferObject::ColorBufferObject(int w, int h,
|
---|
61 | int attachment_idx,
|
---|
62 | FORMAT format,
|
---|
63 | WRAP_TYPE wrapType,
|
---|
64 | FILTER_TYPE filterType,
|
---|
65 | FILTER_TYPE filterTypeMipMap
|
---|
66 | )
|
---|
67 | {
|
---|
68 | Init(w, h, attachment_idx, format, wrapType, filterType, true, filterTypeMipMap);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | ColorBufferObject::ColorBufferObject(int w, int h,
|
---|
73 | int attachment_idx,
|
---|
74 | FORMAT format,
|
---|
75 | WRAP_TYPE wrapType,
|
---|
76 | FILTER_TYPE filterType
|
---|
77 | )
|
---|
78 | {
|
---|
79 | Init(w, h, attachment_idx, format, wrapType, filterType, false, FILTER_NEAREST);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | ColorBufferObject::~ColorBufferObject()
|
---|
84 | {
|
---|
85 | glDeleteRenderbuffersEXT(1, &mId);
|
---|
86 | glDeleteTextures(1, &mTexId);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | void ColorBufferObject::Init(int w, int h,
|
---|
91 | int attachment_idx,
|
---|
92 | FORMAT format,
|
---|
93 | WRAP_TYPE wrapType,
|
---|
94 | FILTER_TYPE filterType,
|
---|
95 | bool useMipMap,
|
---|
96 | FILTER_TYPE filterTypeMipMap
|
---|
97 | )
|
---|
98 | {
|
---|
99 | mWidth = w;
|
---|
100 | mHeight = h;
|
---|
101 |
|
---|
102 | int components = GL_RGBA;
|
---|
103 |
|
---|
104 | switch (format)
|
---|
105 | {
|
---|
106 | case RGB_UBYTE:
|
---|
107 | mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGB8; components = GL_RGB; break;
|
---|
108 | case RGBA_UBYTE:
|
---|
109 | mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8; break;
|
---|
110 | case RGB_FLOAT_32:
|
---|
111 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB16F_ARB; components = GL_RGB; break;
|
---|
112 | case RGBA_FLOAT_16:
|
---|
113 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA16F_ARB; break;
|
---|
114 | case RGB_FLOAT_16:
|
---|
115 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB32F_ARB; components = GL_RGB; break;
|
---|
116 | case RGBA_FLOAT_32:
|
---|
117 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA32F_ARB; break;
|
---|
118 | default:
|
---|
119 | mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8;
|
---|
120 | cerr << "should not come here" << endl;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | glGenRenderbuffersEXT(1, &mId);
|
---|
125 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
|
---|
126 |
|
---|
127 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, mInternalFormat, w, h);
|
---|
128 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
|
---|
129 |
|
---|
130 |
|
---|
131 | glGenTextures(1, &mTexId);
|
---|
132 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
133 | glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, w, h, 0, components, mGlFormat, NULL);
|
---|
134 |
|
---|
135 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
|
---|
136 |
|
---|
137 | GLuint minfilterParam;
|
---|
138 | GLuint magfilterParam;
|
---|
139 |
|
---|
140 | if (filterType == FILTER_NEAREST)
|
---|
141 | magfilterParam = GL_NEAREST;
|
---|
142 | else // FILTER_LINEAR
|
---|
143 | magfilterParam = GL_LINEAR;
|
---|
144 |
|
---|
145 | if (!useMipMap)
|
---|
146 | {
|
---|
147 | if (filterType == FILTER_NEAREST)
|
---|
148 | minfilterParam = GL_NEAREST;
|
---|
149 | else // FILTER_LINEAR
|
---|
150 | minfilterParam = GL_LINEAR;
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | if (filterType == FILTER_NEAREST)
|
---|
155 | {
|
---|
156 | if (filterTypeMipMap == FILTER_NEAREST)
|
---|
157 | minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
|
---|
158 | else // FILTER_LINEAR
|
---|
159 | minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
|
---|
160 | }
|
---|
161 | else // FILTER_LINEAR
|
---|
162 | {
|
---|
163 | if (filterTypeMipMap == FILTER_NEAREST)
|
---|
164 | minfilterParam = GL_LINEAR_MIPMAP_NEAREST;
|
---|
165 | else // FILTER_LINEAR
|
---|
166 | minfilterParam = GL_LINEAR_MIPMAP_LINEAR;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | GLuint wrapParam;
|
---|
171 |
|
---|
172 | switch (wrapType)
|
---|
173 | {
|
---|
174 | case WRAP_REPEAT:
|
---|
175 | wrapParam = GL_REPEAT; break;
|
---|
176 | case WRAP_CLAMP_TO_EDGE:
|
---|
177 | wrapParam = GL_CLAMP_TO_EDGE; break;
|
---|
178 | }
|
---|
179 |
|
---|
180 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
|
---|
181 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
|
---|
182 |
|
---|
183 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
|
---|
184 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
|
---|
185 |
|
---|
186 | if (useMipMap)
|
---|
187 | {
|
---|
188 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
189 | }
|
---|
190 |
|
---|
191 | // print status
|
---|
192 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 | void *ColorBufferObject::ReadTexture() const
|
---|
198 | {
|
---|
199 | int bytes = 0;
|
---|
200 |
|
---|
201 | switch (mInternalFormat)
|
---|
202 | {
|
---|
203 | case GL_RGB8:
|
---|
204 | bytes = 3; break;
|
---|
205 | case GL_RGBA8:
|
---|
206 | bytes = 4; break;
|
---|
207 | case GL_RGBA16F_ARB:
|
---|
208 | bytes = 16; break;
|
---|
209 | case GL_RGB32F_ARB:
|
---|
210 | bytes = 24; break;
|
---|
211 | case GL_RGBA32F_ARB:
|
---|
212 | bytes = 32; break;
|
---|
213 | default:
|
---|
214 | cerr << "should not come here" << endl;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
219 |
|
---|
220 | /*GLint width, height;
|
---|
221 | glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
|
---|
222 | glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
|
---|
223 | cout << "w: " << width << "h: " << height << endl;
|
---|
224 | */
|
---|
225 |
|
---|
226 | unsigned char *data = new unsigned char[bytes * 4 * mWidth * mHeight];
|
---|
227 |
|
---|
228 | glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);
|
---|
229 |
|
---|
230 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
231 | glDisable(GL_TEXTURE_2D);
|
---|
232 |
|
---|
233 | return data;
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | /*****************************************************************/
|
---|
238 | /* FrameBufferObject implementation */
|
---|
239 | /*****************************************************************/
|
---|
240 |
|
---|
241 |
|
---|
242 | FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
|
---|
243 | : mWidth(w), mHeight(h), mDepthTexId(0)
|
---|
244 | {
|
---|
245 | glGenFramebuffersEXT(1, &mId);
|
---|
246 |
|
---|
247 | Bind();
|
---|
248 | ///////////
|
---|
249 | //-- create depth buffer
|
---|
250 |
|
---|
251 | if (d != DEPTH_NONE)
|
---|
252 | {
|
---|
253 | glGenRenderbuffersEXT(1, &mDepthId);
|
---|
254 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
|
---|
255 |
|
---|
256 | GLuint depthFormat;
|
---|
257 |
|
---|
258 | switch (d)
|
---|
259 | {
|
---|
260 | case DEPTH_16:
|
---|
261 | depthFormat = GL_DEPTH_COMPONENT16; break;
|
---|
262 | case DEPTH_24:
|
---|
263 | depthFormat = GL_DEPTH_COMPONENT24; break;
|
---|
264 | case DEPTH_32:
|
---|
265 | depthFormat = GL_DEPTH_COMPONENT32; break;
|
---|
266 | default:
|
---|
267 | cerr << "should not come here" << endl;
|
---|
268 | }
|
---|
269 |
|
---|
270 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
|
---|
271 |
|
---|
272 |
|
---|
273 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
274 |
|
---|
275 | if (useDepthTex)
|
---|
276 | {
|
---|
277 | glGenTextures(1, &mDepthTexId);
|
---|
278 | glBindTexture(GL_TEXTURE_2D, mDepthTexId);
|
---|
279 |
|
---|
280 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
281 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
282 |
|
---|
283 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
284 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
285 |
|
---|
286 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
287 |
|
---|
288 | glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
---|
289 |
|
---|
290 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | Release();
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | FrameBufferObject::~FrameBufferObject()
|
---|
299 | {
|
---|
300 | glDeleteFramebuffersEXT(1, &mId);
|
---|
301 | glDeleteRenderbuffersEXT(1, &mDepthId);
|
---|
302 |
|
---|
303 | if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
|
---|
304 |
|
---|
305 | CLEAR_CONTAINER(mColorBuffers);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
310 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
311 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
312 | ColorBufferObject::FILTER_TYPE filterTypeMipMap)
|
---|
313 | {
|
---|
314 | Bind();
|
---|
315 |
|
---|
316 | const int idx = (int)mColorBuffers.size();
|
---|
317 |
|
---|
318 | ColorBufferObject *colorBuf =
|
---|
319 | new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType, filterTypeMipMap);
|
---|
320 |
|
---|
321 | mColorBuffers.push_back(colorBuf);
|
---|
322 |
|
---|
323 | Release();
|
---|
324 |
|
---|
325 | return idx;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 |
|
---|
330 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
331 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
332 | ColorBufferObject::FILTER_TYPE filterType)
|
---|
333 | {
|
---|
334 | Bind();
|
---|
335 |
|
---|
336 | const int idx = (int)mColorBuffers.size();
|
---|
337 |
|
---|
338 | ColorBufferObject *colorBuf =
|
---|
339 | new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType);
|
---|
340 |
|
---|
341 | mColorBuffers.push_back(colorBuf);
|
---|
342 |
|
---|
343 | Release();
|
---|
344 |
|
---|
345 | return idx;
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | void FrameBufferObject::Bind() const
|
---|
350 | {
|
---|
351 | if (sCurrentFbo != mId)
|
---|
352 | {
|
---|
353 | sCurrentFbo = mId;
|
---|
354 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 |
|
---|
359 | void FrameBufferObject::Release()
|
---|
360 | {
|
---|
361 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
362 | sCurrentFbo = -1;
|
---|
363 | }
|
---|
364 |
|
---|
365 |
|
---|
366 | } // namespace
|
---|