dungeon_crawler

view prototype/src/renderer.cc @ 37:84a56fb24850

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