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 | int attachment_idx)
|
---|
53 | {
|
---|
54 | GLuint glformat, internalFormat;
|
---|
55 |
|
---|
56 |
|
---|
57 | switch (format)
|
---|
58 | {
|
---|
59 | case BUFFER_UBYTE:
|
---|
60 | glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8; break;
|
---|
61 | case BUFFER_FLOAT_16:
|
---|
62 | glformat = GL_FLOAT; internalFormat = GL_RGBA16F_ARB; break;
|
---|
63 | case BUFFER_FLOAT_32:
|
---|
64 | glformat = GL_FLOAT; internalFormat = GL_RGBA32F_ARB; break;
|
---|
65 | default:
|
---|
66 | glformat = GL_UNSIGNED_BYTE; internalFormat = GL_RGBA8;
|
---|
67 | cerr << "should not come here" << endl;
|
---|
68 | }
|
---|
69 |
|
---|
70 | glGenRenderbuffersEXT(1, &mId);
|
---|
71 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
|
---|
72 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, w, h);
|
---|
73 |
|
---|
74 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_RENDERBUFFER_EXT, mId);
|
---|
75 |
|
---|
76 | glGenTextures(1, &mTexId);
|
---|
77 | glBindTexture(GL_TEXTURE_2D, mTexId);
|
---|
78 | glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RGBA, glformat, NULL);
|
---|
79 |
|
---|
80 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, color_attachment[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
|
---|
81 |
|
---|
82 | GLuint minfilterParam;
|
---|
83 | GLuint magfilterParam;
|
---|
84 |
|
---|
85 | switch (filterType)
|
---|
86 | {
|
---|
87 | case FILTER_NEAREST:
|
---|
88 | minfilterParam = GL_NEAREST;
|
---|
89 | magfilterParam = GL_NEAREST; break;
|
---|
90 | case FILTER_LINEAR:
|
---|
91 | minfilterParam = GL_LINEAR;
|
---|
92 | magfilterParam = GL_LINEAR; break;
|
---|
93 | case FILTER_MIPMAP_LINEAR:
|
---|
94 | //minfilterParam = GL_LINEAR_MIPMAP_LINEAR;
|
---|
95 | minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
|
---|
96 | //minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
|
---|
97 | magfilterParam = GL_NEAREST; break;
|
---|
98 | default:
|
---|
99 | minfilterParam = GL_NEAREST;
|
---|
100 | magfilterParam = GL_NEAREST;
|
---|
101 |
|
---|
102 | cerr << "should not come here" << endl;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | GLuint wrapParam;
|
---|
107 |
|
---|
108 | switch (wrapType)
|
---|
109 | {
|
---|
110 | case WRAP_REPEAT:
|
---|
111 | wrapParam = GL_REPEAT; break;
|
---|
112 | case WRAP_CLAMP_TO_EDGE:
|
---|
113 | wrapParam = GL_CLAMP_TO_EDGE; break;
|
---|
114 | }
|
---|
115 |
|
---|
116 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
|
---|
117 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
|
---|
118 |
|
---|
119 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
|
---|
120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
|
---|
121 |
|
---|
122 | if (useMipMap) glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
123 |
|
---|
124 | // print status
|
---|
125 | PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
|
---|
130 | : mWidth(w), mHeight(h), mDepthTexId(0)
|
---|
131 | {
|
---|
132 | glGenFramebuffersEXT(1, &mId);
|
---|
133 |
|
---|
134 | Bind();
|
---|
135 |
|
---|
136 | cout << "creating fbo" << endl;
|
---|
137 |
|
---|
138 | ///////////
|
---|
139 | //-- create depth buffer
|
---|
140 |
|
---|
141 | if (d != DEPTH_NONE)
|
---|
142 | {
|
---|
143 | glGenRenderbuffersEXT(1, &mDepthId);
|
---|
144 | glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
|
---|
145 |
|
---|
146 | cout << "adding depth buffer" << endl;
|
---|
147 |
|
---|
148 | GLuint depthFormat;
|
---|
149 |
|
---|
150 | switch (d)
|
---|
151 | {
|
---|
152 | case DEPTH_16:
|
---|
153 | depthFormat = GL_DEPTH_COMPONENT16; break;
|
---|
154 | case DEPTH_24:
|
---|
155 | depthFormat = GL_DEPTH_COMPONENT24; break;
|
---|
156 | case DEPTH_32:
|
---|
157 | depthFormat = GL_DEPTH_COMPONENT32; break;
|
---|
158 | default:
|
---|
159 | cerr << "should not come here" << endl;
|
---|
160 | }
|
---|
161 |
|
---|
162 | glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, mWidth, mHeight);
|
---|
163 | glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
|
---|
164 |
|
---|
165 | if (useDepthTex)
|
---|
166 | {
|
---|
167 | //cout << "adding depth texture" << endl;
|
---|
168 |
|
---|
169 | glGenTextures(1, &mDepthTexId);
|
---|
170 | glBindTexture(GL_TEXTURE_2D, mDepthTexId);
|
---|
171 |
|
---|
172 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
---|
173 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
---|
174 |
|
---|
175 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
176 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
177 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
---|
178 |
|
---|
179 | glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
|
---|
180 |
|
---|
181 | glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | Release();
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col,
|
---|
190 | ColorBufferObject::WRAP_TYPE wrapType,
|
---|
191 | ColorBufferObject::FILTER_TYPE filterType,
|
---|
192 | bool useMipMap)
|
---|
193 | {
|
---|
194 | cout << "adding color buffer" << endl;
|
---|
195 |
|
---|
196 | Bind();
|
---|
197 |
|
---|
198 | const int idx = (int)mColorBuffers.size();
|
---|
199 |
|
---|
200 | ColorBufferObject *colorBuf =
|
---|
201 | new ColorBufferObject(mWidth, mHeight, col, wrapType, filterType, useMipMap, idx);
|
---|
202 |
|
---|
203 | mColorBuffers.push_back(colorBuf);
|
---|
204 |
|
---|
205 | Release();
|
---|
206 |
|
---|
207 | return idx;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | void FrameBufferObject::Bind() const
|
---|
212 | {
|
---|
213 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | void FrameBufferObject::Release()
|
---|
218 | {
|
---|
219 | glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | } // namespace
|
---|