rev |
line source |
nuclear@15
|
1 #include <stdio.h>
|
nuclear@15
|
2 #include <stdlib.h>
|
nuclear@15
|
3 #include <string.h>
|
nuclear@15
|
4 #include <limits.h>
|
nuclear@18
|
5 #include <assert.h>
|
nuclear@15
|
6 #include "opengl.h"
|
nuclear@15
|
7 #include "renderer.h"
|
nuclear@23
|
8 #include "level.h"
|
nuclear@15
|
9 #include "sdr.h"
|
nuclear@15
|
10 #include "datapath.h"
|
nuclear@15
|
11
|
nuclear@35
|
12 #undef DBG_VIS_MRT
|
nuclear@35
|
13
|
nuclear@35
|
14 #ifdef DBG_VIS_MRT
|
nuclear@35
|
15 static void deferred_debug();
|
nuclear@35
|
16 #endif
|
nuclear@18
|
17
|
nuclear@17
|
18 static bool create_fbo(int xsz, int ysz);
|
nuclear@18
|
19 static unsigned int load_sdr(const char *vfname, const char *pfname);
|
nuclear@17
|
20 static int round_pow2(int x);
|
nuclear@15
|
21
|
nuclear@17
|
22
|
nuclear@35
|
23 #define MRT_COUNT 3
|
nuclear@17
|
24 static unsigned int mrt_tex[MRT_COUNT];
|
nuclear@17
|
25
|
nuclear@17
|
26 static unsigned int mrt_prog;
|
nuclear@18
|
27 static unsigned int deferred_omni, deferred_debug;
|
nuclear@17
|
28
|
nuclear@17
|
29 static unsigned int fbo, rbuf_depth;
|
nuclear@17
|
30 static const char *fbstname[] = {
|
nuclear@17
|
31 "GL_FRAMEBUFFER_COMPLETE",
|
nuclear@17
|
32 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
|
nuclear@17
|
33 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
|
nuclear@17
|
34 "no such fbo error",
|
nuclear@17
|
35 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
|
nuclear@17
|
36 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
|
nuclear@17
|
37 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
|
nuclear@17
|
38 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
|
nuclear@17
|
39 "GL_FRAMEBUFFER_UNSUPPORTED"
|
nuclear@15
|
40 };
|
nuclear@15
|
41
|
nuclear@17
|
42 static int fb_xsz, fb_ysz, tex_xsz, tex_ysz;
|
nuclear@15
|
43
|
nuclear@21
|
44
|
nuclear@21
|
45
|
nuclear@17
|
46 bool init_renderer(int xsz, int ysz)
|
nuclear@17
|
47 {
|
nuclear@32
|
48 if(!GLEW_ARB_texture_float) {
|
nuclear@32
|
49 fprintf(stderr, "Error: OpenGL implementation doesn't support floating point textures\n");
|
nuclear@32
|
50 return false;
|
nuclear@32
|
51 }
|
nuclear@21
|
52 if(!GLEW_ARB_draw_buffers) {
|
nuclear@32
|
53 fprintf(stderr, "Error: OpenGL implementation doesn't support multiple render targets\n");
|
nuclear@21
|
54 return false;
|
nuclear@21
|
55 }
|
nuclear@21
|
56
|
nuclear@19
|
57 int max_draw_buf;
|
nuclear@19
|
58 glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
|
nuclear@19
|
59 printf("max draw buffers: %d\n", max_draw_buf);
|
nuclear@19
|
60 if(max_draw_buf < MRT_COUNT) {
|
nuclear@19
|
61 fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
|
nuclear@19
|
62 return false;
|
nuclear@19
|
63 }
|
nuclear@19
|
64
|
nuclear@17
|
65 if(!create_fbo(xsz, ysz)) {
|
nuclear@17
|
66 return false;
|
nuclear@17
|
67 }
|
nuclear@19
|
68 CHECKGLERR;
|
nuclear@18
|
69
|
nuclear@18
|
70 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
|
nuclear@18
|
71 return false;
|
nuclear@18
|
72 }
|
nuclear@23
|
73 set_uniform_int(mrt_prog, "tex_dif", 0);
|
nuclear@23
|
74 set_uniform_int(mrt_prog, "tex_norm", 1);
|
nuclear@23
|
75
|
nuclear@18
|
76 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
|
nuclear@17
|
77 return false;
|
nuclear@17
|
78 }
|
nuclear@23
|
79 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@23
|
80 char uname[32];
|
nuclear@23
|
81 sprintf(uname, "mrt%d", i);
|
nuclear@23
|
82 set_uniform_int(deferred_debug, uname, i);
|
nuclear@23
|
83 }
|
nuclear@23
|
84
|
nuclear@30
|
85 if(!(deferred_omni = load_sdr("deferred_omni.v.glsl", "deferred_omni.p.glsl"))) {
|
nuclear@23
|
86 return false;
|
nuclear@23
|
87 }
|
nuclear@23
|
88 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@23
|
89 char uname[32];
|
nuclear@23
|
90 sprintf(uname, "mrt%d", i);
|
nuclear@23
|
91 set_uniform_int(deferred_omni, uname, i);
|
nuclear@23
|
92 }
|
nuclear@17
|
93 return true;
|
nuclear@17
|
94 }
|
nuclear@17
|
95
|
nuclear@17
|
96 void destroy_renderer()
|
nuclear@17
|
97 {
|
nuclear@17
|
98 free_program(mrt_prog);
|
nuclear@17
|
99 free_program(deferred_omni);
|
nuclear@17
|
100
|
nuclear@17
|
101 glDeleteTextures(MRT_COUNT, mrt_tex);
|
nuclear@17
|
102 glDeleteFramebuffersEXT(1, &fbo);
|
nuclear@17
|
103 }
|
nuclear@17
|
104
|
nuclear@33
|
105 unsigned int get_deferred_shader(void)
|
nuclear@33
|
106 {
|
nuclear@33
|
107 return deferred_omni;
|
nuclear@33
|
108 }
|
nuclear@33
|
109
|
nuclear@35
|
110 int get_tangent_location(void)
|
nuclear@35
|
111 {
|
nuclear@35
|
112 return get_attrib_loc(mrt_prog, "attr_tangent");
|
nuclear@35
|
113 }
|
nuclear@35
|
114
|
nuclear@23
|
115 void resize_renderer(int xsz, int ysz)
|
nuclear@17
|
116 {
|
nuclear@23
|
117 fb_xsz = xsz;
|
nuclear@23
|
118 fb_ysz = ysz;
|
nuclear@18
|
119
|
nuclear@23
|
120 // if we need a bigger rendertarget resize all colorbuffer textures and the depth buffer
|
nuclear@23
|
121 if(xsz > tex_xsz || ysz > tex_ysz) {
|
nuclear@23
|
122 tex_xsz = round_pow2(xsz);
|
nuclear@23
|
123 tex_ysz = round_pow2(ysz);
|
nuclear@23
|
124
|
nuclear@23
|
125 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@23
|
126 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@32
|
127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
|
nuclear@23
|
128 }
|
nuclear@23
|
129
|
nuclear@23
|
130 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@23
|
131 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
|
nuclear@23
|
132 }
|
nuclear@23
|
133
|
nuclear@23
|
134
|
nuclear@23
|
135 // update the texture coordinate scaling factors
|
nuclear@23
|
136 float tex_scale_x = (float)fb_xsz / tex_xsz;
|
nuclear@23
|
137 float tex_scale_y = (float)fb_ysz / tex_ysz;
|
nuclear@23
|
138
|
nuclear@23
|
139 set_uniform_float2(deferred_omni, "tex_scale", tex_scale_x, tex_scale_y);
|
nuclear@30
|
140 set_uniform_float2(deferred_omni, "fb_size", fb_xsz, fb_ysz);
|
nuclear@30
|
141
|
nuclear@23
|
142 set_uniform_float2(deferred_debug, "tex_scale", tex_scale_x, tex_scale_y);
|
nuclear@23
|
143 }
|
nuclear@23
|
144
|
nuclear@23
|
145 void render_deferred(const Level *level)
|
nuclear@23
|
146 {
|
nuclear@18
|
147 // render into the MRT buffers
|
nuclear@23
|
148 glUseProgram(mrt_prog);
|
nuclear@18
|
149 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
|
nuclear@19
|
150
|
nuclear@23
|
151 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@23
|
152 level->draw();
|
nuclear@20
|
153
|
nuclear@18
|
154 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
|
nuclear@18
|
155
|
nuclear@35
|
156 #ifdef DBG_VIS_MRT
|
nuclear@35
|
157 deferred_debug();
|
nuclear@35
|
158 #else
|
nuclear@18
|
159
|
nuclear@18
|
160 // post-process lighting
|
nuclear@33
|
161 glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
|
nuclear@18
|
162
|
nuclear@30
|
163 glEnable(GL_BLEND);
|
nuclear@30
|
164 glBlendFunc(GL_ONE, GL_ONE);
|
nuclear@30
|
165
|
nuclear@18
|
166 glDisable(GL_LIGHTING);
|
nuclear@30
|
167 glDisable(GL_DEPTH_TEST);
|
nuclear@33
|
168 glCullFace(GL_FRONT);
|
nuclear@18
|
169
|
nuclear@23
|
170 glUseProgram(deferred_omni);
|
nuclear@19
|
171 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@19
|
172 glActiveTexture(GL_TEXTURE0 + i);
|
nuclear@19
|
173 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@19
|
174 glEnable(GL_TEXTURE_2D);
|
nuclear@18
|
175 }
|
nuclear@18
|
176
|
nuclear@23
|
177 glDepthMask(0);
|
nuclear@23
|
178 level->draw_lights();
|
nuclear@23
|
179 glDepthMask(1);
|
nuclear@18
|
180
|
nuclear@19
|
181 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@19
|
182 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
|
nuclear@19
|
183 glDisable(GL_TEXTURE_2D);
|
nuclear@19
|
184 }
|
nuclear@19
|
185
|
nuclear@18
|
186 glUseProgram(0);
|
nuclear@18
|
187 glPopAttrib();
|
nuclear@35
|
188 #endif // DBG_VIS_MRT
|
nuclear@17
|
189 }
|
nuclear@17
|
190
|
nuclear@17
|
191 static bool create_fbo(int xsz, int ysz)
|
nuclear@17
|
192 {
|
nuclear@35
|
193 unsigned int clamp = GLEW_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
|
nuclear@17
|
194
|
nuclear@35
|
195 // round the texture size up to the next power of 2
|
nuclear@17
|
196 tex_xsz = round_pow2(xsz);
|
nuclear@17
|
197 tex_ysz = round_pow2(ysz);
|
nuclear@17
|
198 fb_xsz = xsz;
|
nuclear@17
|
199 fb_ysz = ysz;
|
nuclear@17
|
200
|
nuclear@17
|
201 if(!glGenFramebuffersEXT) {
|
nuclear@17
|
202 fprintf(stderr, "FBO support missing\n");
|
nuclear@17
|
203 return false;
|
nuclear@17
|
204 }
|
nuclear@17
|
205 glGenFramebuffersEXT(1, &fbo);
|
nuclear@18
|
206 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
|
nuclear@17
|
207
|
nuclear@17
|
208 glGenTextures(MRT_COUNT, mrt_tex);
|
nuclear@17
|
209 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@17
|
210 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@17
|
211 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
|
nuclear@17
|
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
|
nuclear@17
|
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@17
|
214 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@32
|
215 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
|
nuclear@17
|
216
|
nuclear@17
|
217 // attach to fbo
|
nuclear@20
|
218 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
|
nuclear@20
|
219 GL_TEXTURE_2D, mrt_tex[i], 0);
|
nuclear@18
|
220 CHECKGLERR;
|
nuclear@17
|
221 }
|
nuclear@17
|
222
|
nuclear@20
|
223 static GLenum draw_bufs[] = {
|
nuclear@20
|
224 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
|
nuclear@20
|
225 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
|
nuclear@20
|
226 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
|
nuclear@20
|
227 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
|
nuclear@20
|
228 };
|
nuclear@20
|
229 glDrawBuffersARB(MRT_COUNT, draw_bufs);
|
nuclear@20
|
230
|
nuclear@18
|
231 glGenRenderbuffersEXT(1, &rbuf_depth);
|
nuclear@18
|
232 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@18
|
233 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
|
nuclear@18
|
234 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
|
nuclear@18
|
235
|
nuclear@17
|
236 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
|
nuclear@17
|
237 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
|
nuclear@17
|
238 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
|
nuclear@17
|
239 return false;
|
nuclear@17
|
240 }
|
nuclear@18
|
241 CHECKGLERR;
|
nuclear@18
|
242
|
nuclear@18
|
243 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
|
nuclear@17
|
244 return true;
|
nuclear@17
|
245 }
|
nuclear@17
|
246
|
nuclear@18
|
247 static unsigned int load_sdr(const char *vfname, const char *pfname)
|
nuclear@15
|
248 {
|
nuclear@15
|
249 char vsfile[PATH_MAX], psfile[PATH_MAX];
|
nuclear@15
|
250 const char *fname;
|
nuclear@18
|
251 unsigned int prog;
|
nuclear@15
|
252
|
nuclear@18
|
253 if((fname = datafile_path(vfname))) {
|
nuclear@17
|
254 strcpy(vsfile, fname);
|
nuclear@17
|
255 } else {
|
nuclear@17
|
256 vsfile[0] = 0;
|
nuclear@17
|
257 }
|
nuclear@18
|
258 if((fname = datafile_path(pfname))) {
|
nuclear@17
|
259 strcpy(psfile, fname);
|
nuclear@17
|
260 } else {
|
nuclear@17
|
261 psfile[0] = 0;
|
nuclear@17
|
262 }
|
nuclear@18
|
263 if(!(prog = create_program_load(vsfile, psfile))) {
|
nuclear@18
|
264 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
|
nuclear@18
|
265 return 0;
|
nuclear@15
|
266 }
|
nuclear@18
|
267 return prog;
|
nuclear@15
|
268 }
|
nuclear@15
|
269
|
nuclear@17
|
270 static int round_pow2(int x)
|
nuclear@15
|
271 {
|
nuclear@17
|
272 x--;
|
nuclear@17
|
273 x = (x >> 1) | x;
|
nuclear@17
|
274 x = (x >> 2) | x;
|
nuclear@17
|
275 x = (x >> 4) | x;
|
nuclear@17
|
276 x = (x >> 8) | x;
|
nuclear@17
|
277 x = (x >> 16) | x;
|
nuclear@17
|
278 return x + 1;
|
nuclear@15
|
279 }
|
nuclear@35
|
280
|
nuclear@35
|
281 #ifdef DBG_VIS_MRT
|
nuclear@35
|
282 // visualize the MRT buffers
|
nuclear@35
|
283 static void deferred_debug()
|
nuclear@35
|
284 {
|
nuclear@35
|
285 glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
|
nuclear@35
|
286 glUseProgram(deferred_debug);
|
nuclear@35
|
287 glDisable(GL_DEPTH_TEST);
|
nuclear@35
|
288
|
nuclear@35
|
289 glMatrixMode(GL_PROJECTION);
|
nuclear@35
|
290 glPushMatrix();
|
nuclear@35
|
291 glLoadIdentity();
|
nuclear@35
|
292 glMatrixMode(GL_MODELVIEW);
|
nuclear@35
|
293 glPushMatrix();
|
nuclear@35
|
294 glLoadIdentity();
|
nuclear@35
|
295
|
nuclear@35
|
296 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@35
|
297 glActiveTexture(GL_TEXTURE0 + i);
|
nuclear@35
|
298 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@35
|
299 glEnable(GL_TEXTURE_2D);
|
nuclear@35
|
300 }
|
nuclear@35
|
301
|
nuclear@35
|
302 glBegin(GL_QUADS);
|
nuclear@35
|
303 glTexCoord2f(0, 0);
|
nuclear@35
|
304 glVertex2f(-1, -1);
|
nuclear@35
|
305 glTexCoord2f(1, 0);
|
nuclear@35
|
306 glVertex2f(1, -1);
|
nuclear@35
|
307 glTexCoord2f(1, 1);
|
nuclear@35
|
308 glVertex2f(1, 1);
|
nuclear@35
|
309 glTexCoord2f(0, 1);
|
nuclear@35
|
310 glVertex2f(-1, 1);
|
nuclear@35
|
311 glEnd();
|
nuclear@35
|
312
|
nuclear@35
|
313 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@35
|
314 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
|
nuclear@35
|
315 glDisable(GL_TEXTURE_2D);
|
nuclear@35
|
316 }
|
nuclear@35
|
317
|
nuclear@35
|
318 glMatrixMode(GL_PROJECTION);
|
nuclear@35
|
319 glPopMatrix();
|
nuclear@35
|
320 glMatrixMode(GL_MODELVIEW);
|
nuclear@35
|
321 glPopMatrix();
|
nuclear@35
|
322
|
nuclear@35
|
323 glUseProgram(0);
|
nuclear@35
|
324 glPopAttrib();
|
nuclear@35
|
325 }
|
nuclear@35
|
326 #endif
|