dungeon_crawler

view prototype/src/renderer.cc @ 30:938a6a155c94

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 27 Aug 2012 04:03:22 +0300
parents 2fc004802739
children 0cd55268eaf4
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_draw_buffers) {
44 fprintf(stderr, "OpenGL implementation doesn't support multiple render targets\n");
45 return false;
46 }
48 int max_draw_buf;
49 glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
50 printf("max draw buffers: %d\n", max_draw_buf);
51 if(max_draw_buf < MRT_COUNT) {
52 fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
53 return false;
54 }
56 if(!create_fbo(xsz, ysz)) {
57 return false;
58 }
59 CHECKGLERR;
61 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
62 return false;
63 }
64 set_uniform_int(mrt_prog, "tex_dif", 0);
65 set_uniform_int(mrt_prog, "tex_norm", 1);
67 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
68 return false;
69 }
70 for(int i=0; i<MRT_COUNT; i++) {
71 char uname[32];
72 sprintf(uname, "mrt%d", i);
73 set_uniform_int(deferred_debug, uname, i);
74 }
76 if(!(deferred_omni = load_sdr("deferred_omni.v.glsl", "deferred_omni.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_omni, uname, i);
83 }
84 return true;
85 }
87 void destroy_renderer()
88 {
89 free_program(mrt_prog);
90 free_program(deferred_omni);
92 glDeleteTextures(MRT_COUNT, mrt_tex);
93 glDeleteFramebuffersEXT(1, &fbo);
94 }
96 void resize_renderer(int xsz, int ysz)
97 {
98 fb_xsz = xsz;
99 fb_ysz = ysz;
101 // if we need a bigger rendertarget resize all colorbuffer textures and the depth buffer
102 if(xsz > tex_xsz || ysz > tex_ysz) {
103 tex_xsz = round_pow2(xsz);
104 tex_ysz = round_pow2(ysz);
106 for(int i=0; i<MRT_COUNT; i++) {
107 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
109 GL_UNSIGNED_BYTE, 0);
110 }
112 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
113 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
114 }
117 // update the texture coordinate scaling factors
118 float tex_scale_x = (float)fb_xsz / tex_xsz;
119 float tex_scale_y = (float)fb_ysz / tex_ysz;
121 set_uniform_float2(deferred_omni, "tex_scale", tex_scale_x, tex_scale_y);
122 set_uniform_float2(deferred_omni, "fb_size", fb_xsz, fb_ysz);
124 set_uniform_float2(deferred_debug, "tex_scale", tex_scale_x, tex_scale_y);
125 }
127 void render_deferred(const Level *level)
128 {
129 // render into the MRT buffers
130 glUseProgram(mrt_prog);
131 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
133 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134 level->draw();
136 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
139 // post-process lighting
140 glPushAttrib(GL_ENABLE_BIT);
142 glEnable(GL_BLEND);
143 glBlendFunc(GL_ONE, GL_ONE);
145 glDisable(GL_LIGHTING);
146 glDisable(GL_CULL_FACE);
147 glDisable(GL_DEPTH_TEST);
149 glUseProgram(deferred_omni);
150 for(int i=0; i<MRT_COUNT; i++) {
151 glActiveTexture(GL_TEXTURE0 + i);
152 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
153 glEnable(GL_TEXTURE_2D);
154 }
156 glDepthMask(0);
157 level->draw_lights();
158 glDepthMask(1);
160 for(int i=0; i<MRT_COUNT; i++) {
161 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
162 glDisable(GL_TEXTURE_2D);
163 }
165 glUseProgram(0);
166 glPopAttrib();
167 }
169 static bool create_fbo(int xsz, int ysz)
170 {
171 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
173 tex_xsz = round_pow2(xsz);
174 tex_ysz = round_pow2(ysz);
175 fb_xsz = xsz;
176 fb_ysz = ysz;
178 if(!glGenFramebuffersEXT) {
179 fprintf(stderr, "FBO support missing\n");
180 return false;
181 }
182 glGenFramebuffersEXT(1, &fbo);
183 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
185 glGenTextures(MRT_COUNT, mrt_tex);
186 for(int i=0; i<MRT_COUNT; i++) {
187 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
189 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
194 // attach to fbo
195 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
196 GL_TEXTURE_2D, mrt_tex[i], 0);
197 CHECKGLERR;
198 }
200 static GLenum draw_bufs[] = {
201 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
202 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
203 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
204 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
205 };
206 glDrawBuffersARB(MRT_COUNT, draw_bufs);
208 glGenRenderbuffersEXT(1, &rbuf_depth);
209 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
210 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
211 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
213 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
214 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
215 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
216 return false;
217 }
218 CHECKGLERR;
220 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
221 return true;
222 }
224 static unsigned int load_sdr(const char *vfname, const char *pfname)
225 {
226 char vsfile[PATH_MAX], psfile[PATH_MAX];
227 const char *fname;
228 unsigned int prog;
230 if((fname = datafile_path(vfname))) {
231 strcpy(vsfile, fname);
232 } else {
233 vsfile[0] = 0;
234 }
235 if((fname = datafile_path(pfname))) {
236 strcpy(psfile, fname);
237 } else {
238 psfile[0] = 0;
239 }
240 if(!(prog = create_program_load(vsfile, psfile))) {
241 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
242 return 0;
243 }
244 return prog;
245 }
247 static int round_pow2(int x)
248 {
249 x--;
250 x = (x >> 1) | x;
251 x = (x >> 2) | x;
252 x = (x >> 4) | x;
253 x = (x >> 8) | x;
254 x = (x >> 16) | x;
255 return x + 1;
256 }