dungeon_crawler

view prototype/src/renderer.cc @ 29:2fc004802739

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 26 Aug 2012 03:39:32 +0300
parents fa8f89d06f6f
children 938a6a155c94
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.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_debug, "tex_scale", tex_scale_x, tex_scale_y);
123 }
125 void render_deferred(const Level *level)
126 {
127 // render into the MRT buffers
128 glUseProgram(mrt_prog);
129 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132 level->draw();
134 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
137 // post-process lighting
138 glPushAttrib(GL_ENABLE_BIT);
140 glDisable(GL_LIGHTING);
142 glUseProgram(deferred_omni);
143 for(int i=0; i<MRT_COUNT; i++) {
144 glActiveTexture(GL_TEXTURE0 + i);
145 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
146 glEnable(GL_TEXTURE_2D);
147 }
149 glDepthMask(0);
150 level->draw_lights();
151 glDepthMask(1);
153 for(int i=0; i<MRT_COUNT; i++) {
154 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
155 glDisable(GL_TEXTURE_2D);
156 }
158 glUseProgram(0);
159 glPopAttrib();
160 }
162 static bool create_fbo(int xsz, int ysz)
163 {
164 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
166 tex_xsz = round_pow2(xsz);
167 tex_ysz = round_pow2(ysz);
168 fb_xsz = xsz;
169 fb_ysz = ysz;
171 if(!glGenFramebuffersEXT) {
172 fprintf(stderr, "FBO support missing\n");
173 return false;
174 }
175 glGenFramebuffersEXT(1, &fbo);
176 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
178 glGenTextures(MRT_COUNT, mrt_tex);
179 for(int i=0; i<MRT_COUNT; i++) {
180 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
185 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
187 // attach to fbo
188 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
189 GL_TEXTURE_2D, mrt_tex[i], 0);
190 CHECKGLERR;
191 }
193 static GLenum draw_bufs[] = {
194 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
195 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
196 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
197 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
198 };
199 glDrawBuffersARB(MRT_COUNT, draw_bufs);
201 glGenRenderbuffersEXT(1, &rbuf_depth);
202 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
203 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
204 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
206 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
207 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
208 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
209 return false;
210 }
211 CHECKGLERR;
213 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
214 return true;
215 }
217 static unsigned int load_sdr(const char *vfname, const char *pfname)
218 {
219 char vsfile[PATH_MAX], psfile[PATH_MAX];
220 const char *fname;
221 unsigned int prog;
223 if((fname = datafile_path(vfname))) {
224 strcpy(vsfile, fname);
225 } else {
226 vsfile[0] = 0;
227 }
228 if((fname = datafile_path(pfname))) {
229 strcpy(psfile, fname);
230 } else {
231 psfile[0] = 0;
232 }
233 if(!(prog = create_program_load(vsfile, psfile))) {
234 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
235 return 0;
236 }
237 return prog;
238 }
240 static int round_pow2(int x)
241 {
242 x--;
243 x = (x >> 1) | x;
244 x = (x >> 2) | x;
245 x = (x >> 4) | x;
246 x = (x >> 8) | x;
247 x = (x >> 16) | x;
248 return x + 1;
249 }