[2855] | 1 | #include "FrameBufferObject.h"
|
---|
| 2 | #include "glInterface.h"
|
---|
[3021] | 3 | #include <iostream>
|
---|
[2855] | 4 |
|
---|
[3021] | 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 |
|
---|
[2855] | 17 | using namespace std;
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | namespace CHCDemoEngine
|
---|
| 21 | {
|
---|
| 22 |
|
---|
[3007] | 23 | int FrameBufferObject::sCurrentFbo = -1;
|
---|
[2855] | 24 |
|
---|
[3007] | 25 |
|
---|
[2855] | 26 | void PrintFBOStatus(GLenum status)
|
---|
| 27 | {
|
---|
| 28 | switch(status)
|
---|
| 29 | {
|
---|
| 30 | case GL_FRAMEBUFFER_COMPLETE_EXT:
|
---|
[2879] | 31 | //cout << "frame buffer object complete" << endl;
|
---|
[2855] | 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,
|
---|
[2965] | 61 | int attachment_idx,
|
---|
[2861] | 62 | FORMAT format,
|
---|
[2856] | 63 | WRAP_TYPE wrapType,
|
---|
| 64 | FILTER_TYPE filterType,
|
---|
[2965] | 65 | FILTER_TYPE filterTypeMipMap
|
---|
| 66 | )
|
---|
[2855] | 67 | {
|
---|
[2965] | 68 | Init(w, h, attachment_idx, format, wrapType, filterType, true, filterTypeMipMap);
|
---|
| 69 | }
|
---|
[2861] | 70 |
|
---|
| 71 |
|
---|
[2965] | 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 |
|
---|
[3005] | 102 | int components = GL_RGBA;
|
---|
[2999] | 103 |
|
---|
[2861] | 104 | switch (format)
|
---|
| 105 | {
|
---|
[3005] | 106 | case RGB_UBYTE:
|
---|
[3017] | 107 | mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGB8; components = GL_RGB; break;
|
---|
[3005] | 108 | case RGBA_UBYTE:
|
---|
[2965] | 109 | mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8; break;
|
---|
[3017] | 110 | case RGB_FLOAT_32:
|
---|
| 111 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB16F_ARB; components = GL_RGB; break;
|
---|
[3005] | 112 | case RGBA_FLOAT_16:
|
---|
[2965] | 113 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA16F_ARB; break;
|
---|
[3017] | 114 | case RGB_FLOAT_16:
|
---|
| 115 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB32F_ARB; components = GL_RGB; break;
|
---|
[3005] | 116 | case RGBA_FLOAT_32:
|
---|
[2965] | 117 | mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA32F_ARB; break;
|
---|
[2861] | 118 | default:
|
---|
[2965] | 119 | mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8;
|
---|
[2861] | 120 | cerr << "should not come here" << endl;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[2879] | 123 |
|
---|
[2856] | 124 | glGenRenderbuffersEXT(1, &mId);
|
---|
| 125 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
|
---|
[2855] | 126 |
|
---|
[2965] | 127 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, mInternalFormat, w, h);
|
---|
[2899] | 128 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
|
---|
[2855] | 129 |
|
---|
[2879] | 130 |
|
---|
[2856] | 131 | glGenTextures(1, &mTexId);
|
---|
| 132 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
[3005] | 133 | glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, w, h, 0, components, mGlFormat, NULL);
|
---|
[2862] | 134 |
|
---|
[2899] | 135 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
|
---|
[2855] | 136 |
|
---|
[2867] | 137 | GLuint minfilterParam;
|
---|
| 138 | GLuint magfilterParam;
|
---|
[2855] | 139 |
|
---|
[2965] | 140 | if (filterType == FILTER_NEAREST)
|
---|
[2867] | 141 | magfilterParam = GL_NEAREST;
|
---|
[2965] | 142 | else // FILTER_LINEAR
|
---|
| 143 | magfilterParam = GL_LINEAR;
|
---|
[2867] | 144 |
|
---|
[2965] | 145 | if (!useMipMap)
|
---|
| 146 | {
|
---|
| 147 | if (filterType == FILTER_NEAREST)
|
---|
| 148 | minfilterParam = GL_NEAREST;
|
---|
| 149 | else // FILTER_LINEAR
|
---|
| 150 | minfilterParam = GL_LINEAR;
|
---|
[2856] | 151 | }
|
---|
[2965] | 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 | }
|
---|
[2855] | 169 |
|
---|
[2856] | 170 | GLuint wrapParam;
|
---|
| 171 |
|
---|
| 172 | switch (wrapType)
|
---|
| 173 | {
|
---|
[2857] | 174 | case WRAP_REPEAT:
|
---|
| 175 | wrapParam = GL_REPEAT; break;
|
---|
| 176 | case WRAP_CLAMP_TO_EDGE:
|
---|
| 177 | wrapParam = GL_CLAMP_TO_EDGE; break;
|
---|
[3204] | 178 | case WRAP_CLAMP_TO_BORDER:
|
---|
| 179 | wrapParam = GL_MIRROR_CLAMP_TO_EDGE_EXT; break;
|
---|
[2856] | 180 | }
|
---|
| 181 |
|
---|
[2867] | 182 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
|
---|
| 183 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
|
---|
[2856] | 184 |
|
---|
| 185 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
|
---|
| 186 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
|
---|
| 187 |
|
---|
[2976] | 188 | if (useMipMap)
|
---|
| 189 | {
|
---|
| 190 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
| 191 | }
|
---|
[2867] | 192 |
|
---|
[2857] | 193 | // print status
|
---|
[2855] | 194 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 |
|
---|
[2965] | 198 |
|
---|
| 199 | void *ColorBufferObject::ReadTexture() const
|
---|
[2878] | 200 | {
|
---|
[2965] | 201 | int bytes = 0;
|
---|
| 202 |
|
---|
| 203 | switch (mInternalFormat)
|
---|
| 204 | {
|
---|
[3005] | 205 | case GL_RGB8:
|
---|
| 206 | bytes = 3; break;
|
---|
[2965] | 207 | case GL_RGBA8:
|
---|
[3005] | 208 | bytes = 4; break;
|
---|
[2965] | 209 | case GL_RGBA16F_ARB:
|
---|
| 210 | bytes = 16; break;
|
---|
[3005] | 211 | case GL_RGB32F_ARB:
|
---|
| 212 | bytes = 24; break;
|
---|
[2965] | 213 | case GL_RGBA32F_ARB:
|
---|
| 214 | bytes = 32; break;
|
---|
| 215 | default:
|
---|
| 216 | cerr << "should not come here" << endl;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[2979] | 219 |
|
---|
[2965] | 220 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
[2976] | 221 |
|
---|
[3005] | 222 | unsigned char *data = new unsigned char[bytes * 4 * mWidth * mHeight];
|
---|
[2979] | 223 |
|
---|
| 224 | glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);
|
---|
[2976] | 225 |
|
---|
[2965] | 226 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 227 | glDisable(GL_TEXTURE_2D);
|
---|
| 228 |
|
---|
| 229 | return data;
|
---|
[2878] | 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
[2965] | 233 | /*****************************************************************/
|
---|
| 234 | /* FrameBufferObject implementation */
|
---|
| 235 | /*****************************************************************/
|
---|
| 236 |
|
---|
| 237 |
|
---|
[2862] | 238 | FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
|
---|
| 239 | : mWidth(w), mHeight(h), mDepthTexId(0)
|
---|
[2855] | 240 | {
|
---|
| 241 | glGenFramebuffersEXT(1, &mId);
|
---|
[2862] | 242 |
|
---|
| 243 | Bind();
|
---|
[2855] | 244 | ///////////
|
---|
| 245 | //-- create depth buffer
|
---|
| 246 |
|
---|
[2861] | 247 | if (d != DEPTH_NONE)
|
---|
[2855] | 248 | {
|
---|
| 249 | glGenRenderbuffersEXT(1, &mDepthId);
|
---|
| 250 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
|
---|
| 251 |
|
---|
| 252 | GLuint depthFormat;
|
---|
| 253 |
|
---|
| 254 | switch (d)
|
---|
| 255 | {
|
---|
| 256 | case DEPTH_16:
|
---|
| 257 | depthFormat = GL_DEPTH_COMPONENT16; break;
|
---|
| 258 | case DEPTH_24:
|
---|
| 259 | depthFormat = GL_DEPTH_COMPONENT24; break;
|
---|
| 260 | case DEPTH_32:
|
---|
| 261 | depthFormat = GL_DEPTH_COMPONENT32; break;
|
---|
[2861] | 262 | default:
|
---|
| 263 | cerr << "should not come here" << endl;
|
---|
[2855] | 264 | }
|
---|
| 265 |
|
---|
[2879] | 266 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
|
---|
| 267 |
|
---|
| 268 |
|
---|
[2855] | 269 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
[2862] | 270 |
|
---|
| 271 | if (useDepthTex)
|
---|
| 272 | {
|
---|
| 273 | glGenTextures(1, &mDepthTexId);
|
---|
| 274 | glBindTexture(GL_TEXTURE_2D, mDepthTexId);
|
---|
| 275 |
|
---|
| 276 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 277 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 278 |
|
---|
| 279 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 280 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
[2965] | 281 |
|
---|
[2862] | 282 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
| 283 |
|
---|
[2866] | 284 | glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
---|
[2862] | 285 |
|
---|
| 286 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
|
---|
| 287 | }
|
---|
[2857] | 288 | }
|
---|
[2862] | 289 |
|
---|
| 290 | Release();
|
---|
[2855] | 291 | }
|
---|
| 292 |
|
---|
| 293 |
|
---|
[2878] | 294 | FrameBufferObject::~FrameBufferObject()
|
---|
| 295 | {
|
---|
| 296 | glDeleteFramebuffersEXT(1, &mId);
|
---|
| 297 | glDeleteRenderbuffersEXT(1, &mDepthId);
|
---|
| 298 |
|
---|
| 299 | if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
|
---|
| 300 |
|
---|
| 301 | CLEAR_CONTAINER(mColorBuffers);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 |
|
---|
[2867] | 305 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
[2859] | 306 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
| 307 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
[2965] | 308 | ColorBufferObject::FILTER_TYPE filterTypeMipMap)
|
---|
[2857] | 309 | {
|
---|
[2862] | 310 | Bind();
|
---|
[2861] | 311 |
|
---|
[2867] | 312 | const int idx = (int)mColorBuffers.size();
|
---|
| 313 |
|
---|
[2857] | 314 | ColorBufferObject *colorBuf =
|
---|
[2965] | 315 | new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType, filterTypeMipMap);
|
---|
[2861] | 316 |
|
---|
[2857] | 317 | mColorBuffers.push_back(colorBuf);
|
---|
| 318 |
|
---|
[2862] | 319 | Release();
|
---|
[2861] | 320 |
|
---|
| 321 | return idx;
|
---|
[2857] | 322 | }
|
---|
| 323 |
|
---|
[2859] | 324 |
|
---|
[2965] | 325 |
|
---|
| 326 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
| 327 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
| 328 | ColorBufferObject::FILTER_TYPE filterType)
|
---|
| 329 | {
|
---|
| 330 | Bind();
|
---|
| 331 |
|
---|
| 332 | const int idx = (int)mColorBuffers.size();
|
---|
| 333 |
|
---|
| 334 | ColorBufferObject *colorBuf =
|
---|
| 335 | new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType);
|
---|
| 336 |
|
---|
| 337 | mColorBuffers.push_back(colorBuf);
|
---|
| 338 |
|
---|
| 339 | Release();
|
---|
| 340 |
|
---|
| 341 | return idx;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 |
|
---|
[2861] | 345 | void FrameBufferObject::Bind() const
|
---|
| 346 | {
|
---|
[3007] | 347 | if (sCurrentFbo != mId)
|
---|
| 348 | {
|
---|
| 349 | sCurrentFbo = mId;
|
---|
| 350 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
|
---|
| 351 | }
|
---|
[2861] | 352 | }
|
---|
| 353 |
|
---|
| 354 |
|
---|
| 355 | void FrameBufferObject::Release()
|
---|
| 356 | {
|
---|
| 357 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
[3007] | 358 | sCurrentFbo = -1;
|
---|
[2861] | 359 | }
|
---|
| 360 |
|
---|
| 361 |
|
---|
[3117] | 362 | void FrameBufferObject::InitBuffer(FrameBufferObject *fbo, int index)
|
---|
| 363 | {
|
---|
| 364 | // read the second buffer, write to the first buffer
|
---|
| 365 | fbo->Bind();
|
---|
| 366 | glDrawBuffers(1, mrt + index);
|
---|
| 367 |
|
---|
| 368 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 369 |
|
---|
| 370 | FrameBufferObject::Release();
|
---|
| 371 | }
|
---|
| 372 |
|
---|
[2855] | 373 | } // namespace
|
---|