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:
|
---|
17 | //cout << "frame buffer object complete" << endl;
|
---|
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,
|
---|
47 | FORMAT format,
|
---|
48 | WRAP_TYPE wrapType,
|
---|
49 | FILTER_TYPE filterType,
|
---|
50 | bool useMipMap,
|
---|
51 | int attachment_idx)
|
---|
52 | {
|
---|
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 |
|
---|
69 |
|
---|
70 | glGenRenderbuffersEXT(1, &mId);
|
---|
71 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
|
---|
72 |
|
---|
73 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
|
---|
74 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
|
---|
75 |
|
---|
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, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
|
---|
82 |
|
---|
83 | GLuint minfilterParam;
|
---|
84 | GLuint magfilterParam;
|
---|
85 |
|
---|
86 | switch (filterType)
|
---|
87 | {
|
---|
88 | case FILTER_NEAREST:
|
---|
89 | minfilterParam = GL_NEAREST;
|
---|
90 | magfilterParam = GL_NEAREST; break;
|
---|
91 | case FILTER_LINEAR:
|
---|
92 | minfilterParam = GL_LINEAR;
|
---|
93 | magfilterParam = GL_LINEAR; break;
|
---|
94 | case FILTER_MIPMAP_LINEAR:
|
---|
95 | minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
|
---|
96 | magfilterParam = GL_NEAREST;
|
---|
97 |
|
---|
98 | //minfilterParam = GL_LINEAR_MIPMAP_NEAREST;
|
---|
99 | //magfilterParam = GL_LINEAR;
|
---|
100 | break;
|
---|
101 | default:
|
---|
102 | minfilterParam = GL_NEAREST;
|
---|
103 | magfilterParam = GL_NEAREST;
|
---|
104 |
|
---|
105 | cerr << "should not come here" << endl;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | GLuint wrapParam;
|
---|
110 |
|
---|
111 | switch (wrapType)
|
---|
112 | {
|
---|
113 | case WRAP_REPEAT:
|
---|
114 | wrapParam = GL_REPEAT; break;
|
---|
115 | case WRAP_CLAMP_TO_EDGE:
|
---|
116 | wrapParam = GL_CLAMP_TO_EDGE; break;
|
---|
117 | }
|
---|
118 |
|
---|
119 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
|
---|
120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
|
---|
121 |
|
---|
122 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
|
---|
123 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
|
---|
124 |
|
---|
125 | if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
126 |
|
---|
127 | // print status
|
---|
128 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | ColorBufferObject::~ColorBufferObject()
|
---|
133 | {
|
---|
134 | glDeleteRenderbuffersEXT(1, &mId);
|
---|
135 | glDeleteTextures(1, &mTexId);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
|
---|
140 | : mWidth(w), mHeight(h), mDepthTexId(0)
|
---|
141 | {
|
---|
142 | glGenFramebuffersEXT(1, &mId);
|
---|
143 |
|
---|
144 | Bind();
|
---|
145 |
|
---|
146 | ///////////
|
---|
147 | //-- create depth buffer
|
---|
148 |
|
---|
149 | if (d != DEPTH_NONE)
|
---|
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;
|
---|
164 | default:
|
---|
165 | cerr << "should not come here" << endl;
|
---|
166 | }
|
---|
167 |
|
---|
168 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
|
---|
169 |
|
---|
170 |
|
---|
171 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
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 |
|
---|
185 | glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
---|
186 |
|
---|
187 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | Release();
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
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 |
|
---|
206 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
207 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
208 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
209 | bool useMipMap)
|
---|
210 | {
|
---|
211 | Bind();
|
---|
212 |
|
---|
213 | const int idx = (int)mColorBuffers.size();
|
---|
214 |
|
---|
215 | ColorBufferObject *colorBuf =
|
---|
216 | new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
|
---|
217 |
|
---|
218 | mColorBuffers.push_back(colorBuf);
|
---|
219 |
|
---|
220 | Release();
|
---|
221 |
|
---|
222 | return idx;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
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 |
|
---|
238 | } // namespace
|
---|