dungeon_crawler

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