dungeon_crawler

diff prototype/src/renderer.cc @ 23:fa8f89d06f6f

progress with light rendering
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Aug 2012 00:10:10 +0300
parents 0588f8a1a351
children 2fc004802739
line diff
     1.1 --- a/prototype/src/renderer.cc	Tue Aug 21 06:34:14 2012 +0300
     1.2 +++ b/prototype/src/renderer.cc	Thu Aug 23 00:10:10 2012 +0300
     1.3 @@ -5,6 +5,7 @@
     1.4  #include <assert.h>
     1.5  #include "opengl.h"
     1.6  #include "renderer.h"
     1.7 +#include "level.h"
     1.8  #include "sdr.h"
     1.9  #include "datapath.h"
    1.10  
    1.11 @@ -60,9 +61,26 @@
    1.12  	if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
    1.13  		return false;
    1.14  	}
    1.15 +	set_uniform_int(mrt_prog, "tex_dif", 0);
    1.16 +	set_uniform_int(mrt_prog, "tex_norm", 1);
    1.17 +
    1.18  	if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
    1.19  		return false;
    1.20  	}
    1.21 +	for(int i=0; i<MRT_COUNT; i++) {
    1.22 +		char uname[32];
    1.23 +		sprintf(uname, "mrt%d", i);
    1.24 +		set_uniform_int(deferred_debug, uname, i);
    1.25 +	}
    1.26 +
    1.27 +	if(!(deferred_omni = load_sdr("deferred.v.glsl", "deferred_omni.p.glsl"))) {
    1.28 +		return false;
    1.29 +	}
    1.30 +	for(int i=0; i<MRT_COUNT; i++) {
    1.31 +		char uname[32];
    1.32 +		sprintf(uname, "mrt%d", i);
    1.33 +		set_uniform_int(deferred_omni, uname, i);
    1.34 +	}
    1.35  	return true;
    1.36  }
    1.37  
    1.38 @@ -75,23 +93,51 @@
    1.39  	glDeleteFramebuffersEXT(1, &fbo);
    1.40  }
    1.41  
    1.42 -void render_deferred(void (*draw_func)())
    1.43 +void resize_renderer(int xsz, int ysz)
    1.44  {
    1.45 -	int loc;
    1.46 +	fb_xsz = xsz;
    1.47 +	fb_ysz = ysz;
    1.48  
    1.49 +	// if we need a bigger rendertarget resize all colorbuffer textures and the depth buffer
    1.50 +	if(xsz > tex_xsz || ysz > tex_ysz) {
    1.51 +		tex_xsz = round_pow2(xsz);
    1.52 +		tex_ysz = round_pow2(ysz);
    1.53 +
    1.54 +		for(int i=0; i<MRT_COUNT; i++) {
    1.55 +			glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
    1.56 +			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
    1.57 +					GL_UNSIGNED_BYTE, 0);
    1.58 +		}
    1.59 +
    1.60 +		glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
    1.61 +		glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
    1.62 +	}
    1.63 +
    1.64 +
    1.65 +	// update the texture coordinate scaling factors
    1.66 +	float tex_scale_x = (float)fb_xsz / tex_xsz;
    1.67 +	float tex_scale_y = (float)fb_ysz / tex_ysz;
    1.68 +
    1.69 +	set_uniform_float2(deferred_omni, "tex_scale", tex_scale_x, tex_scale_y);
    1.70 +	set_uniform_float2(deferred_debug, "tex_scale", tex_scale_x, tex_scale_y);
    1.71 +}
    1.72 +
    1.73 +void render_deferred(const Level *level)
    1.74 +{
    1.75 +	glClearColor(0.4, 0.2, 0.1, 0.0);
    1.76 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.77 +	glPushAttrib(GL_ENABLE_BIT);
    1.78 +	glDisable(GL_LIGHTING);
    1.79 +	level->draw_lights();
    1.80 +	glPopAttrib();
    1.81 +#if 0
    1.82  	// render into the MRT buffers
    1.83 +	glUseProgram(mrt_prog);
    1.84  	glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
    1.85  
    1.86 -	glUseProgram(mrt_prog);
    1.87 -	if((loc = glGetUniformLocation(mrt_prog, "tex_dif")) != -1) {
    1.88 -		glUniform1i(loc, 0);
    1.89 -	}
    1.90 -	if((loc = glGetUniformLocation(mrt_prog, "tex_norm")) != -1) {
    1.91 -		glUniform1i(loc, 1);
    1.92 -	}
    1.93 -	draw_func();
    1.94 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.95 +	level->draw();
    1.96  
    1.97 -	glUseProgram(0);
    1.98  	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
    1.99  
   1.100  
   1.101 @@ -101,33 +147,16 @@
   1.102  	glDisable(GL_LIGHTING);
   1.103  	glDisable(GL_DEPTH_TEST);
   1.104  
   1.105 -	glUseProgram(deferred_debug);
   1.106 +	glUseProgram(deferred_omni);
   1.107  	for(int i=0; i<MRT_COUNT; i++) {
   1.108 -		char uname[32];
   1.109 -
   1.110 -		sprintf(uname, "mrt%d", i);
   1.111 -		if((loc = glGetUniformLocation(deferred_debug, uname)) != -1) {
   1.112 -			glUniform1i(loc, i);
   1.113 -		}
   1.114 -
   1.115  		glActiveTexture(GL_TEXTURE0 + i);
   1.116  		glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
   1.117  		glEnable(GL_TEXTURE_2D);
   1.118  	}
   1.119 -	if((loc = glGetUniformLocation(deferred_debug, "tex_scale")) != -1) {
   1.120 -		glUniform2f(loc, (float)fb_xsz / tex_xsz, (float)fb_ysz / tex_ysz);
   1.121 -	}
   1.122  
   1.123 -	glBegin(GL_QUADS);
   1.124 -	glTexCoord2f(0, 0);
   1.125 -	glVertex2f(-1, -1);
   1.126 -	glTexCoord2f(1, 0);
   1.127 -	glVertex2f(1, -1);
   1.128 -	glTexCoord2f(1, 1);
   1.129 -	glVertex2f(1, 1);
   1.130 -	glTexCoord2f(0, 1);
   1.131 -	glVertex2f(-1, 1);
   1.132 -	glEnd();
   1.133 +	glDepthMask(0);
   1.134 +	level->draw_lights();
   1.135 +	glDepthMask(1);
   1.136  
   1.137  	for(int i=0; i<MRT_COUNT; i++) {
   1.138  		glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
   1.139 @@ -136,6 +165,7 @@
   1.140  
   1.141  	glUseProgram(0);
   1.142  	glPopAttrib();
   1.143 +#endif
   1.144  }
   1.145  
   1.146  static bool create_fbo(int xsz, int ysz)