dungeon_crawler
diff prototype/src/renderer.cc @ 18:5c41e6fcb300
- commandline arguments
- stereoscopic rendering
- FBO fixed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 21 Aug 2012 03:17:48 +0300 |
parents | d98240a13793 |
children | 8a0ae6b4aa9b |
line diff
1.1 --- a/prototype/src/renderer.cc Mon Aug 20 06:11:58 2012 +0300 1.2 +++ b/prototype/src/renderer.cc Tue Aug 21 03:17:48 2012 +0300 1.3 @@ -2,21 +2,23 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 #include <limits.h> 1.7 +#include <assert.h> 1.8 #include "opengl.h" 1.9 #include "renderer.h" 1.10 #include "sdr.h" 1.11 #include "datapath.h" 1.12 1.13 + 1.14 static bool create_fbo(int xsz, int ysz); 1.15 -static bool load_shaders(); 1.16 +static unsigned int load_sdr(const char *vfname, const char *pfname); 1.17 static int round_pow2(int x); 1.18 1.19 1.20 -#define MRT_COUNT 4 1.21 +#define MRT_COUNT 1 1.22 static unsigned int mrt_tex[MRT_COUNT]; 1.23 1.24 static unsigned int mrt_prog; 1.25 -static unsigned int deferred_omni; 1.26 +static unsigned int deferred_omni, deferred_debug; 1.27 1.28 static unsigned int fbo, rbuf_depth; 1.29 static const char *fbstname[] = { 1.30 @@ -38,7 +40,11 @@ 1.31 if(!create_fbo(xsz, ysz)) { 1.32 return false; 1.33 } 1.34 - if(!load_shaders()) { 1.35 + 1.36 + if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) { 1.37 + return false; 1.38 + } 1.39 + if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) { 1.40 return false; 1.41 } 1.42 return true; 1.43 @@ -55,9 +61,45 @@ 1.44 1.45 void render_deferred(void (*draw_func)()) 1.46 { 1.47 - bind_program(mrt_prog); 1.48 + int loc; 1.49 + 1.50 + // render into the MRT buffers 1.51 + glBindFramebufferEXT(GL_FRAMEBUFFER, fbo); 1.52 + glUseProgram(mrt_prog); 1.53 draw_func(); 1.54 - bind_program(0); 1.55 + glUseProgram(0); 1.56 + glBindFramebufferEXT(GL_FRAMEBUFFER, 0); 1.57 + 1.58 + 1.59 + // post-process lighting 1.60 + glPushAttrib(GL_ENABLE_BIT); 1.61 + 1.62 + glDisable(GL_LIGHTING); 1.63 + glDisable(GL_DEPTH_TEST); 1.64 + glBindTexture(GL_TEXTURE_2D, mrt_tex[0]); 1.65 + glEnable(GL_TEXTURE_2D); 1.66 + 1.67 + glUseProgram(deferred_debug); 1.68 + if((loc = glGetUniformLocation(deferred_debug, "tex0")) != -1) { 1.69 + glUniform1i(loc, 0); 1.70 + } 1.71 + if((loc = glGetUniformLocation(deferred_debug, "tex_scale")) != -1) { 1.72 + glUniform2f(loc, (float)fb_xsz / tex_xsz, (float)fb_ysz / tex_ysz); 1.73 + } 1.74 + 1.75 + glBegin(GL_QUADS); 1.76 + glTexCoord2f(0, 0); 1.77 + glVertex2f(-1, -1); 1.78 + glTexCoord2f(1, 0); 1.79 + glVertex2f(1, -1); 1.80 + glTexCoord2f(1, 1); 1.81 + glVertex2f(1, 1); 1.82 + glTexCoord2f(0, 1); 1.83 + glVertex2f(-1, 1); 1.84 + glEnd(); 1.85 + 1.86 + glUseProgram(0); 1.87 + glPopAttrib(); 1.88 } 1.89 1.90 static bool create_fbo(int xsz, int ysz) 1.91 @@ -74,11 +116,7 @@ 1.92 return false; 1.93 } 1.94 glGenFramebuffersEXT(1, &fbo); 1.95 - 1.96 - glGenRenderbuffersEXT(1, &rbuf_depth); 1.97 - glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth); 1.98 - glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 1.99 - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth); 1.100 + glBindFramebufferEXT(GL_FRAMEBUFFER, fbo); 1.101 1.102 glGenTextures(MRT_COUNT, mrt_tex); 1.103 for(int i=0; i<MRT_COUNT; i++) { 1.104 @@ -92,52 +130,46 @@ 1.105 // attach to fbo 1.106 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 1.107 mrt_tex[i], 0); 1.108 + CHECKGLERR; 1.109 } 1.110 1.111 + glGenRenderbuffersEXT(1, &rbuf_depth); 1.112 + glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth); 1.113 + glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 1.114 + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth); 1.115 + 1.116 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); 1.117 if(fbst != GL_FRAMEBUFFER_COMPLETE) { 1.118 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]); 1.119 return false; 1.120 } 1.121 + CHECKGLERR; 1.122 + 1.123 + glBindFramebufferEXT(GL_FRAMEBUFFER, 0); 1.124 return true; 1.125 } 1.126 1.127 -static bool load_shaders() 1.128 +static unsigned int load_sdr(const char *vfname, const char *pfname) 1.129 { 1.130 char vsfile[PATH_MAX], psfile[PATH_MAX]; 1.131 const char *fname; 1.132 + unsigned int prog; 1.133 1.134 - if((fname = datafile_path("mrt.v.glsl"))) { 1.135 + if((fname = datafile_path(vfname))) { 1.136 strcpy(vsfile, fname); 1.137 } else { 1.138 vsfile[0] = 0; 1.139 } 1.140 - if((fname = datafile_path("mrt.p.glsl"))) { 1.141 + if((fname = datafile_path(pfname))) { 1.142 strcpy(psfile, fname); 1.143 } else { 1.144 psfile[0] = 0; 1.145 } 1.146 - if(!(mrt_prog = create_program_load(vsfile, psfile))) { 1.147 - fprintf(stderr, "failed to load MRT program\n"); 1.148 - return false; 1.149 + if(!(prog = create_program_load(vsfile, psfile))) { 1.150 + fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile); 1.151 + return 0; 1.152 } 1.153 - 1.154 - if((fname = datafile_path("deferred.v.glsl"))) { 1.155 - strcpy(vsfile, fname); 1.156 - } else { 1.157 - vsfile[0] = 0; 1.158 - } 1.159 - if((fname = datafile_path("deferred.p.glsl"))) { 1.160 - strcpy(psfile, fname); 1.161 - } else { 1.162 - psfile[0] = 0; 1.163 - } 1.164 - 1.165 - if(!(deferred_omni = create_program_load(vsfile, psfile))) { 1.166 - fprintf(stderr, "failed to load deferred shader program\n"); 1.167 - return false; 1.168 - } 1.169 - return true; 1.170 + return prog; 1.171 } 1.172 1.173 static int round_pow2(int x)