tavli

diff src/game.cc @ 19:37dead56f01e

fixed shadows
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jun 2015 06:18:45 +0300
parents 986c0b76513f
children 111c1a9bca29
line diff
     1.1 --- a/src/game.cc	Mon Jun 29 01:29:36 2015 +0300
     1.2 +++ b/src/game.cc	Mon Jun 29 06:18:45 2015 +0300
     1.3 @@ -15,6 +15,7 @@
     1.4  unsigned long cur_time;
     1.5  unsigned int sdr_phong, sdr_phong_notex;
     1.6  unsigned int sdr_shadow, sdr_shadow_notex;
     1.7 +unsigned int sdr_unlit;
     1.8  bool wireframe;
     1.9  
    1.10  static Board board;
    1.11 @@ -23,7 +24,7 @@
    1.12  static bool bnstate[8];
    1.13  static int prev_x, prev_y;
    1.14  
    1.15 -static bool dbg_busyloop;
    1.16 +static bool dbg_busyloop, dbg_show_shadowmap;
    1.17  
    1.18  bool game_init()
    1.19  {
    1.20 @@ -37,6 +38,9 @@
    1.21  	glEnable(GL_LIGHTING);
    1.22  	glEnable(GL_LIGHT0);
    1.23  
    1.24 +	float amb[] = {0.3, 0.3, 0.3, 1.0};
    1.25 +	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
    1.26 +
    1.27  	if(glcaps.sep_spec) {
    1.28  		glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
    1.29  	}
    1.30 @@ -55,7 +59,7 @@
    1.31  		}
    1.32  
    1.33  		if(glcaps.fbo) {
    1.34 -			init_shadow(512);
    1.35 +			init_shadow(2048);
    1.36  
    1.37  			if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
    1.38  				return false;
    1.39 @@ -66,6 +70,7 @@
    1.40  			if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
    1.41  				return false;
    1.42  			}
    1.43 +			set_uniform_int(sdr_shadow_notex, "shadowmap", 1);
    1.44  		}
    1.45  	}
    1.46  
    1.47 @@ -104,13 +109,11 @@
    1.48  	glRotatef(cam_phi, 1, 0, 0);
    1.49  	glRotatef(cam_theta, 0, 1, 0);
    1.50  
    1.51 -	float ldir[] = {-10, 20, 10, 1};
    1.52 -	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    1.53 +	float lpos[] = {-10, 20, 10, 1};
    1.54 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
    1.55  
    1.56  	if(opt.shadows && sdr_shadow) {
    1.57 -		printf("shadow pass\n");
    1.58 -
    1.59 -		begin_shadow_pass(Vector3(-10, 20, 10), Vector3(0, 0, 0), 25);
    1.60 +		begin_shadow_pass(Vector3(lpos[0], lpos[1], lpos[2]), Vector3(0, 0, 0), 4.5);
    1.61  		draw_scene();
    1.62  		end_shadow_pass();
    1.63  
    1.64 @@ -119,7 +122,7 @@
    1.65  
    1.66  		glMatrixMode(GL_TEXTURE);
    1.67  		Matrix4x4 shadow_matrix = get_shadow_matrix();
    1.68 -		glLoadMatrixf(shadow_matrix[0]);
    1.69 +		glLoadTransposeMatrixf(shadow_matrix[0]);
    1.70  
    1.71  		glActiveTexture(GL_TEXTURE0);
    1.72  		glMatrixMode(GL_MODELVIEW);
    1.73 @@ -129,10 +132,49 @@
    1.74  		glActiveTexture(GL_TEXTURE1);
    1.75  		glBindTexture(GL_TEXTURE_2D, 0);
    1.76  		glActiveTexture(GL_TEXTURE0);
    1.77 +		glBindTexture(GL_TEXTURE_2D, 0);
    1.78  	} else {
    1.79  		draw_scene();
    1.80  	}
    1.81  
    1.82 +	if(dbg_show_shadowmap) {
    1.83 +		glMatrixMode(GL_PROJECTION);
    1.84 +		glPushMatrix();
    1.85 +		glLoadIdentity();
    1.86 +		glMatrixMode(GL_MODELVIEW);
    1.87 +		glPushMatrix();
    1.88 +		glLoadIdentity();
    1.89 +		glMatrixMode(GL_TEXTURE);
    1.90 +		glLoadIdentity();
    1.91 +
    1.92 +		glPushAttrib(GL_ENABLE_BIT);
    1.93 +		glUseProgram(0);
    1.94 +		glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
    1.95 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
    1.96 +		glEnable(GL_TEXTURE_2D);
    1.97 +		glDisable(GL_DEPTH_TEST);
    1.98 +		glDisable(GL_LIGHTING);
    1.99 +		glDisable(GL_BLEND);
   1.100 +
   1.101 +		glBegin(GL_QUADS);
   1.102 +		glColor4f(1, 1, 1, 1);
   1.103 +		glTexCoord2f(0, 0); glVertex2f(-1, -1);
   1.104 +		glTexCoord2f(1, 0); glVertex2f(1, -1);
   1.105 +		glTexCoord2f(1, 1); glVertex2f(1, 1);
   1.106 +		glTexCoord2f(0, 1); glVertex2f(-1, 1);
   1.107 +		glEnd();
   1.108 +
   1.109 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
   1.110 +		glBindTexture(GL_TEXTURE_2D, 0);
   1.111 +
   1.112 +		glPopAttrib();
   1.113 +
   1.114 +		glMatrixMode(GL_PROJECTION);
   1.115 +		glPopMatrix();
   1.116 +		glMatrixMode(GL_MODELVIEW);
   1.117 +		glPopMatrix();
   1.118 +	}
   1.119 +
   1.120  	if(dbg_busyloop) {
   1.121  		redisplay();
   1.122  	}
   1.123 @@ -201,6 +243,11 @@
   1.124  			redisplay();
   1.125  			break;
   1.126  
   1.127 +		case 'D':
   1.128 +			dbg_show_shadowmap = !dbg_show_shadowmap;
   1.129 +			redisplay();
   1.130 +			break;
   1.131 +
   1.132  		case 's':
   1.133  			opt.shadows = !opt.shadows;
   1.134  			redisplay();