dungeon_crawler

changeset 19:8a0ae6b4aa9b

trying to make MRT work
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 04:29:49 +0300
parents 5c41e6fcb300
children 48cfd351a9be
files prototype/sdr/deferred.p.glsl prototype/sdr/mrt.p.glsl prototype/src/renderer.cc
diffstat 3 files changed, 57 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/prototype/sdr/deferred.p.glsl	Tue Aug 21 03:17:48 2012 +0300
     1.2 +++ b/prototype/sdr/deferred.p.glsl	Tue Aug 21 04:29:49 2012 +0300
     1.3 @@ -1,8 +1,21 @@
     1.4 -uniform sampler2D mrt0;
     1.5 +uniform sampler2D mrt0, mrt1, mrt2, mrt3;
     1.6  uniform vec2 tex_scale;
     1.7  
     1.8  void main()
     1.9  {
    1.10 -	vec4 texel0 = texture2D(mrt0, gl_TexCoord[0].st * tex_scale);
    1.11 -	gl_FragColor = texel0;
    1.12 +	vec2 tc = gl_TexCoord[0].st;
    1.13 +	//vec4 texel0 = texture2D(mrt0, gl_TexCoord[0].st * s);
    1.14 +
    1.15 +	vec4 texel;
    1.16 +	if(tc.x < 0.25) {
    1.17 +		texel = texture2D(mrt0, tc * vec2(4.0, 1.0) * tex_scale);
    1.18 +	} else if(tc.x < 0.5) {
    1.19 +		texel = texture2D(mrt0, (tc - vec2(0.25, 0.0)) * vec2(4.0, 1.0) * tex_scale);
    1.20 +	} else if(tc.x < 0.75) {
    1.21 +		texel = texture2D(mrt0, (tc - vec2(0.5, 0.0)) * vec2(4.0, 1.0) * tex_scale);
    1.22 +	} else {
    1.23 +		texel = texture2D(mrt0, (tc - vec2(0.75, 0.0)) * vec2(4.0, 1.0) * tex_scale);
    1.24 +	}
    1.25 +
    1.26 +	gl_FragColor = texel;
    1.27  }
     2.1 --- a/prototype/sdr/mrt.p.glsl	Tue Aug 21 03:17:48 2012 +0300
     2.2 +++ b/prototype/sdr/mrt.p.glsl	Tue Aug 21 04:29:49 2012 +0300
     2.3 @@ -3,5 +3,8 @@
     2.4  void main()
     2.5  {
     2.6  	vec4 texel = texture2D(tex_dif, gl_TexCoord[0].st);
     2.7 -	gl_FragColor = texel;
     2.8 +	gl_FragData[0] = texel * vec4(1.0, 0.0, 0.0, 1.0);
     2.9 +	gl_FragData[1] = texel * vec4(0.0, 1.0, 0.0, 1.0);
    2.10 +	gl_FragData[2] = texel * vec4(0.0, 0.0, 1.0, 1.0);
    2.11 +	gl_FragData[3] = texel * vec4(1.0, 0.0, 0.0, 1.0);
    2.12  }
     3.1 --- a/prototype/src/renderer.cc	Tue Aug 21 03:17:48 2012 +0300
     3.2 +++ b/prototype/src/renderer.cc	Tue Aug 21 04:29:49 2012 +0300
     3.3 @@ -14,7 +14,7 @@
     3.4  static int round_pow2(int x);
     3.5  
     3.6  
     3.7 -#define MRT_COUNT	1
     3.8 +#define MRT_COUNT	4
     3.9  static unsigned int mrt_tex[MRT_COUNT];
    3.10  
    3.11  static unsigned int mrt_prog;
    3.12 @@ -37,9 +37,20 @@
    3.13  
    3.14  bool init_renderer(int xsz, int ysz)
    3.15  {
    3.16 +	int max_draw_buf;
    3.17 +
    3.18 +	glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
    3.19 +	CHECKGLERR;
    3.20 +	printf("max draw buffers: %d\n", max_draw_buf);
    3.21 +	if(max_draw_buf < MRT_COUNT) {
    3.22 +		fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
    3.23 +		return false;
    3.24 +	}
    3.25 +
    3.26  	if(!create_fbo(xsz, ysz)) {
    3.27  		return false;
    3.28  	}
    3.29 +	CHECKGLERR;
    3.30  
    3.31  	if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
    3.32  		return false;
    3.33 @@ -63,8 +74,17 @@
    3.34  {
    3.35  	int loc;
    3.36  
    3.37 +	static GLenum draw_bufs[] = {
    3.38 +		GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
    3.39 +		GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
    3.40 +		GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
    3.41 +		GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
    3.42 +	};
    3.43 +
    3.44  	// render into the MRT buffers
    3.45  	glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
    3.46 +	glDrawBuffersARB(MRT_COUNT, draw_bufs);
    3.47 +
    3.48  	glUseProgram(mrt_prog);
    3.49  	draw_func();
    3.50  	glUseProgram(0);
    3.51 @@ -76,12 +96,19 @@
    3.52  
    3.53  	glDisable(GL_LIGHTING);
    3.54  	glDisable(GL_DEPTH_TEST);
    3.55 -	glBindTexture(GL_TEXTURE_2D, mrt_tex[0]);
    3.56 -	glEnable(GL_TEXTURE_2D);
    3.57  
    3.58  	glUseProgram(deferred_debug);
    3.59 -	if((loc = glGetUniformLocation(deferred_debug, "tex0")) != -1) {
    3.60 -		glUniform1i(loc, 0);
    3.61 +	for(int i=0; i<MRT_COUNT; i++) {
    3.62 +		char uname[32];
    3.63 +
    3.64 +		sprintf(uname, "mrt%d", i);
    3.65 +		if((loc = glGetUniformLocation(deferred_debug, uname)) != -1) {
    3.66 +			glUniform1i(loc, i);
    3.67 +		}
    3.68 +
    3.69 +		glActiveTexture(GL_TEXTURE0 + i);
    3.70 +		glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
    3.71 +		glEnable(GL_TEXTURE_2D);
    3.72  	}
    3.73  	if((loc = glGetUniformLocation(deferred_debug, "tex_scale")) != -1) {
    3.74  		glUniform2f(loc, (float)fb_xsz / tex_xsz, (float)fb_ysz / tex_ysz);
    3.75 @@ -98,6 +125,11 @@
    3.76  	glVertex2f(-1, 1);
    3.77  	glEnd();
    3.78  
    3.79 +	for(int i=0; i<MRT_COUNT; i++) {
    3.80 +		glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
    3.81 +		glDisable(GL_TEXTURE_2D);
    3.82 +	}
    3.83 +
    3.84  	glUseProgram(0);
    3.85  	glPopAttrib();
    3.86  }