dungeon_crawler

view prototype/src/renderer.cc @ 33:0357994effe2

ha! fixed deferred
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Aug 2012 03:18:47 +0300
parents 0cd55268eaf4
children d0e93b4d9ec9
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"
13 static bool create_fbo(int xsz, int ysz);
14 static unsigned int load_sdr(const char *vfname, const char *pfname);
15 static int round_pow2(int x);
18 #define MRT_COUNT 4
19 static unsigned int mrt_tex[MRT_COUNT];
21 static unsigned int mrt_prog;
22 static unsigned int deferred_omni, deferred_debug;
24 static unsigned int fbo, rbuf_depth;
25 static const char *fbstname[] = {
26 "GL_FRAMEBUFFER_COMPLETE",
27 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
28 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
29 "no such fbo error",
30 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
31 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
32 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
33 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
34 "GL_FRAMEBUFFER_UNSUPPORTED"
35 };
37 static int fb_xsz, fb_ysz, tex_xsz, tex_ysz;
41 bool init_renderer(int xsz, int ysz)
42 {
43 if(!GLEW_ARB_texture_float) {
44 fprintf(stderr, "Error: OpenGL implementation doesn't support floating point textures\n");
45 return false;
46 }
47 if(!GLEW_ARB_draw_buffers) {
48 fprintf(stderr, "Error: OpenGL implementation doesn't support multiple render targets\n");
49 return false;
50 }
52 int max_draw_buf;
53 glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
54 printf("max draw buffers: %d\n", max_draw_buf);
55 if(max_draw_buf < MRT_COUNT) {
56 fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
57 return false;
58 }
60 if(!create_fbo(xsz, ysz)) {
61 return false;
62 }
63 CHECKGLERR;
65 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
66 return false;
67 }
68 set_uniform_int(mrt_prog, "tex_dif", 0);
69 set_uniform_int(mrt_prog, "tex_norm", 1);
71 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
72 return false;
73 }
74 for(int i=0; i<MRT_COUNT; i++) {
75 char uname[32];
76 sprintf(uname, "mrt%d", i);
77 set_uniform_int(deferred_debug, uname, i);
78 }
80 if(!(deferred_omni = load_sdr("deferred_omni.v.glsl", "deferred_omni.p.glsl"))) {
81 return false;
82 }
83 for(int i=0; i<MRT_COUNT; i++) {
84 char uname[32];
85 sprintf(uname, "mrt%d", i);
86 set_uniform_int(deferred_omni, uname, i);
87 }
88 return true;
89 }
91 void destroy_renderer()
92 {
93 free_program(mrt_prog);
94 free_program(deferred_omni);
96 glDeleteTextures(MRT_COUNT, mrt_tex);
97 glDeleteFramebuffersEXT(1, &fbo);
98 }
100 unsigned int get_deferred_shader(void)
101 {
102 return deferred_omni;
103 }
105 void resize_renderer(int xsz, int ysz)
106 {
107 fb_xsz = xsz;
108 fb_ysz = ysz;
110 // if we need a bigger rendertarget resize all colorbuffer textures and the depth buffer
111 if(xsz > tex_xsz || ysz > tex_ysz) {
112 tex_xsz = round_pow2(xsz);
113 tex_ysz = round_pow2(ysz);
115 for(int i=0; i<MRT_COUNT; i++) {
116 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
117 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
118 }
120 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
121 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
122 }
125 // update the texture coordinate scaling factors
126 float tex_scale_x = (float)fb_xsz / tex_xsz;
127 float tex_scale_y = (float)fb_ysz / tex_ysz;
129 set_uniform_float2(deferred_omni, "tex_scale", tex_scale_x, tex_scale_y);
130 set_uniform_float2(deferred_omni, "fb_size", fb_xsz, fb_ysz);
132 set_uniform_float2(deferred_debug, "tex_scale", tex_scale_x, tex_scale_y);
133 }
135 void render_deferred(const Level *level)
136 {
137 // render into the MRT buffers
138 glUseProgram(mrt_prog);
139 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
141 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
142 level->draw();
144 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
147 // post-process lighting
148 glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
150 glEnable(GL_BLEND);
151 glBlendFunc(GL_ONE, GL_ONE);
153 glDisable(GL_LIGHTING);
154 glDisable(GL_DEPTH_TEST);
155 glCullFace(GL_FRONT);
157 glUseProgram(deferred_omni);
158 for(int i=0; i<MRT_COUNT; i++) {
159 glActiveTexture(GL_TEXTURE0 + i);
160 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
161 glEnable(GL_TEXTURE_2D);
162 }
164 glDepthMask(0);
165 level->draw_lights();
166 glDepthMask(1);
168 for(int i=0; i<MRT_COUNT; i++) {
169 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
170 glDisable(GL_TEXTURE_2D);
171 }
173 glUseProgram(0);
174 glPopAttrib();
175 }
177 static bool create_fbo(int xsz, int ysz)
178 {
179 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
181 tex_xsz = round_pow2(xsz);
182 tex_ysz = round_pow2(ysz);
183 fb_xsz = xsz;
184 fb_ysz = ysz;
186 if(!glGenFramebuffersEXT) {
187 fprintf(stderr, "FBO support missing\n");
188 return false;
189 }
190 glGenFramebuffersEXT(1, &fbo);
191 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
193 glGenTextures(MRT_COUNT, mrt_tex);
194 for(int i=0; i<MRT_COUNT; i++) {
195 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
197 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
200 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, tex_xsz, tex_ysz, 0, GL_RGBA, GL_FLOAT, 0);
202 // attach to fbo
203 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
204 GL_TEXTURE_2D, mrt_tex[i], 0);
205 CHECKGLERR;
206 }
208 static GLenum draw_bufs[] = {
209 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
210 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
211 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
212 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
213 };
214 glDrawBuffersARB(MRT_COUNT, draw_bufs);
216 glGenRenderbuffersEXT(1, &rbuf_depth);
217 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
218 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
219 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
221 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
222 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
223 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
224 return false;
225 }
226 CHECKGLERR;
228 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
229 return true;
230 }
232 static unsigned int load_sdr(const char *vfname, const char *pfname)
233 {
234 char vsfile[PATH_MAX], psfile[PATH_MAX];
235 const char *fname;
236 unsigned int prog;
238 if((fname = datafile_path(vfname))) {
239 strcpy(vsfile, fname);
240 } else {
241 vsfile[0] = 0;
242 }
243 if((fname = datafile_path(pfname))) {
244 strcpy(psfile, fname);
245 } else {
246 psfile[0] = 0;
247 }
248 if(!(prog = create_program_load(vsfile, psfile))) {
249 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
250 return 0;
251 }
252 return prog;
253 }
255 static int round_pow2(int x)
256 {
257 x--;
258 x = (x >> 1) | x;
259 x = (x >> 2) | x;
260 x = (x >> 4) | x;
261 x = (x >> 8) | x;
262 x = (x >> 16) | x;
263 return x + 1;
264 }