[2855] | 1 | #include <iostream>
|
---|
| 2 | #include "FrameBufferObject.h"
|
---|
| 3 | #include "glInterface.h"
|
---|
| 4 |
|
---|
| 5 | using namespace std;
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | namespace CHCDemoEngine
|
---|
| 9 | {
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | void PrintFBOStatus(GLenum status)
|
---|
| 13 | {
|
---|
| 14 | switch(status)
|
---|
| 15 | {
|
---|
| 16 | case GL_FRAMEBUFFER_COMPLETE_EXT:
|
---|
[2879] | 17 | //cout << "frame buffer object complete" << endl;
|
---|
[2855] | 18 | break;
|
---|
| 19 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
---|
| 20 | cerr << "incomplete attachment" << endl;
|
---|
| 21 | break;
|
---|
| 22 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
|
---|
| 23 | cerr << "missing attachment" << endl;
|
---|
| 24 | break;
|
---|
| 25 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
|
---|
| 26 | cerr << "incomplete dimensions" << endl;
|
---|
| 27 | break;
|
---|
| 28 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
|
---|
| 29 | cerr << "incomplete formats" << endl;
|
---|
| 30 | break;
|
---|
| 31 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
|
---|
| 32 | cerr << "incomplete draw buffer" << endl;
|
---|
| 33 | break;
|
---|
| 34 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
|
---|
| 35 | cerr << "incomplete read buffer" << endl;
|
---|
| 36 | break;
|
---|
| 37 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
---|
| 38 | cerr << "framebuffer unsupported" << endl;
|
---|
| 39 | break;
|
---|
| 40 | default:
|
---|
| 41 | cerr << "unknown status code " << status << endl;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | ColorBufferObject::ColorBufferObject(int w, int h,
|
---|
[2861] | 47 | FORMAT format,
|
---|
[2856] | 48 | WRAP_TYPE wrapType,
|
---|
| 49 | FILTER_TYPE filterType,
|
---|
[2855] | 50 | bool useMipMap,
|
---|
[2861] | 51 | int attachment_idx)
|
---|
[2855] | 52 | {
|
---|
[2861] | 53 | GLuint glformat, internalFormat;
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | switch (format)
|
---|
| 57 | {
|
---|
| 58 | case BUFFER_UBYTE:
|
---|
| 59 | glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; break;
|
---|
| 60 | case BUFFER_FLOAT_16:
|
---|
| 61 | glformat = GL_FLOAT; internalFormat = GL_RGBA16F_ARB; break;
|
---|
| 62 | case BUFFER_FLOAT_32:
|
---|
| 63 | glformat = GL_FLOAT; internalFormat = GL_RGBA32F_ARB; break;
|
---|
| 64 | default:
|
---|
| 65 | glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8;
|
---|
| 66 | cerr << "should not come here" << endl;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[2879] | 69 |
|
---|
[2856] | 70 | glGenRenderbuffersEXT(1, &mId);
|
---|
| 71 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
|
---|
[2855] | 72 |
|
---|
[2879] | 73 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
|
---|
[2899] | 74 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
|
---|
[2855] | 75 |
|
---|
[2879] | 76 |
|
---|
[2856] | 77 | glGenTextures(1, &mTexId);
|
---|
| 78 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
[2861] | 79 | glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
|
---|
[2862] | 80 |
|
---|
[2899] | 81 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
|
---|
[2855] | 82 |
|
---|
[2867] | 83 | GLuint minfilterParam;
|
---|
| 84 | GLuint magfilterParam;
|
---|
[2855] | 85 |
|
---|
[2856] | 86 | switch (filterType)
|
---|
| 87 | {
|
---|
[2857] | 88 | case FILTER_NEAREST:
|
---|
[2867] | 89 | minfilterParam = GL_NEAREST;
|
---|
| 90 | magfilterParam = GL_NEAREST; break;
|
---|
[2857] | 91 | case FILTER_LINEAR:
|
---|
[2867] | 92 | minfilterParam = GL_LINEAR;
|
---|
| 93 | magfilterParam = GL_LINEAR; break;
|
---|
[2857] | 94 | case FILTER_MIPMAP_LINEAR:
|
---|
[2884] | 95 | minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
|
---|
| 96 | magfilterParam = GL_NEAREST;
|
---|
[2881] | 97 |
|
---|
[2884] | 98 | //minfilterParam = GL_LINEAR_MIPMAP_NEAREST;
|
---|
| 99 | //magfilterParam = GL_LINEAR;
|
---|
[2881] | 100 | break;
|
---|
[2861] | 101 | default:
|
---|
[2867] | 102 | minfilterParam = GL_NEAREST;
|
---|
| 103 | magfilterParam = GL_NEAREST;
|
---|
| 104 |
|
---|
[2861] | 105 | cerr << "should not come here" << endl;
|
---|
[2856] | 106 | }
|
---|
[2855] | 107 |
|
---|
[2856] | 108 |
|
---|
| 109 | GLuint wrapParam;
|
---|
| 110 |
|
---|
| 111 | switch (wrapType)
|
---|
| 112 | {
|
---|
[2857] | 113 | case WRAP_REPEAT:
|
---|
| 114 | wrapParam = GL_REPEAT; break;
|
---|
| 115 | case WRAP_CLAMP_TO_EDGE:
|
---|
| 116 | wrapParam = GL_CLAMP_TO_EDGE; break;
|
---|
[2856] | 117 | }
|
---|
| 118 |
|
---|
[2867] | 119 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
|
---|
| 120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
|
---|
[2856] | 121 |
|
---|
| 122 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
|
---|
| 123 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
|
---|
| 124 |
|
---|
[2867] | 125 | if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
| 126 |
|
---|
[2857] | 127 | // print status
|
---|
[2855] | 128 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
[2878] | 132 | ColorBufferObject::~ColorBufferObject()
|
---|
| 133 | {
|
---|
| 134 | glDeleteRenderbuffersEXT(1, &mId);
|
---|
| 135 | glDeleteTextures(1, &mTexId);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
[2862] | 139 | FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
|
---|
| 140 | : mWidth(w), mHeight(h), mDepthTexId(0)
|
---|
[2855] | 141 | {
|
---|
| 142 | glGenFramebuffersEXT(1, &mId);
|
---|
[2862] | 143 |
|
---|
| 144 | Bind();
|
---|
[2855] | 145 |
|
---|
| 146 | ///////////
|
---|
| 147 | //-- create depth buffer
|
---|
| 148 |
|
---|
[2861] | 149 | if (d != DEPTH_NONE)
|
---|
[2855] | 150 | {
|
---|
| 151 | glGenRenderbuffersEXT(1, &mDepthId);
|
---|
| 152 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
|
---|
| 153 |
|
---|
| 154 | GLuint depthFormat;
|
---|
| 155 |
|
---|
| 156 | switch (d)
|
---|
| 157 | {
|
---|
| 158 | case DEPTH_16:
|
---|
| 159 | depthFormat = GL_DEPTH_COMPONENT16; break;
|
---|
| 160 | case DEPTH_24:
|
---|
| 161 | depthFormat = GL_DEPTH_COMPONENT24; break;
|
---|
| 162 | case DEPTH_32:
|
---|
| 163 | depthFormat = GL_DEPTH_COMPONENT32; break;
|
---|
[2861] | 164 | default:
|
---|
| 165 | cerr << "should not come here" << endl;
|
---|
[2855] | 166 | }
|
---|
| 167 |
|
---|
[2879] | 168 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
|
---|
| 169 |
|
---|
| 170 |
|
---|
[2855] | 171 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
[2862] | 172 |
|
---|
| 173 | if (useDepthTex)
|
---|
| 174 | {
|
---|
| 175 | glGenTextures(1, &mDepthTexId);
|
---|
| 176 | glBindTexture(GL_TEXTURE_2D, mDepthTexId);
|
---|
| 177 |
|
---|
| 178 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
| 179 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
| 180 |
|
---|
| 181 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
| 182 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
| 183 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
| 184 |
|
---|
[2866] | 185 | glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
---|
[2862] | 186 |
|
---|
| 187 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
|
---|
| 188 | }
|
---|
[2857] | 189 | }
|
---|
[2862] | 190 |
|
---|
| 191 | Release();
|
---|
[2855] | 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
[2878] | 195 | FrameBufferObject::~FrameBufferObject()
|
---|
| 196 | {
|
---|
| 197 | glDeleteFramebuffersEXT(1, &mId);
|
---|
| 198 | glDeleteRenderbuffersEXT(1, &mDepthId);
|
---|
| 199 |
|
---|
| 200 | if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
|
---|
| 201 |
|
---|
| 202 | CLEAR_CONTAINER(mColorBuffers);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
[2867] | 206 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
[2859] | 207 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
| 208 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
[2867] | 209 | bool useMipMap)
|
---|
[2857] | 210 | {
|
---|
[2862] | 211 | Bind();
|
---|
[2861] | 212 |
|
---|
[2867] | 213 | const int idx = (int)mColorBuffers.size();
|
---|
| 214 |
|
---|
[2857] | 215 | ColorBufferObject *colorBuf =
|
---|
[2867] | 216 | new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
|
---|
[2861] | 217 |
|
---|
[2857] | 218 | mColorBuffers.push_back(colorBuf);
|
---|
| 219 |
|
---|
[2862] | 220 | Release();
|
---|
[2861] | 221 |
|
---|
| 222 | return idx;
|
---|
[2857] | 223 | }
|
---|
| 224 |
|
---|
[2859] | 225 |
|
---|
[2861] | 226 | void FrameBufferObject::Bind() const
|
---|
| 227 | {
|
---|
| 228 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | void FrameBufferObject::Release()
|
---|
| 233 | {
|
---|
| 234 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 |
|
---|
[2855] | 238 | } // namespace
|
---|