tavli

diff src/game.cc @ 18:986c0b76513f

shadows, not completed
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Jun 2015 01:29:36 +0300
parents 16a420432aa3
children 37dead56f01e
line diff
     1.1 --- a/src/game.cc	Sun Jun 28 23:04:37 2015 +0300
     1.2 +++ b/src/game.cc	Mon Jun 29 01:29:36 2015 +0300
     1.3 @@ -1,15 +1,20 @@
     1.4  #include <stdio.h>
     1.5 +#include <assert.h>
     1.6  #include "opengl.h"
     1.7  #include "game.h"
     1.8  #include "board.h"
     1.9  #include "scenery.h"
    1.10  #include "sdr.h"
    1.11 +#include "shadow.h"
    1.12 +#include "opt.h"
    1.13  
    1.14 +static void draw_scene();
    1.15  static void draw_backdrop();
    1.16  
    1.17  int win_width, win_height;
    1.18  unsigned long cur_time;
    1.19  unsigned int sdr_phong, sdr_phong_notex;
    1.20 +unsigned int sdr_shadow, sdr_shadow_notex;
    1.21  bool wireframe;
    1.22  
    1.23  static Board board;
    1.24 @@ -48,6 +53,20 @@
    1.25  		if(!(sdr_phong_notex = create_program_load("sdr/phong.v.glsl", "sdr/phong-notex.p.glsl"))) {
    1.26  			return false;
    1.27  		}
    1.28 +
    1.29 +		if(glcaps.fbo) {
    1.30 +			init_shadow(512);
    1.31 +
    1.32 +			if(!(sdr_shadow = create_program_load("sdr/shadow.v.glsl", "sdr/shadow.p.glsl"))) {
    1.33 +				return false;
    1.34 +			}
    1.35 +			set_uniform_int(sdr_shadow, "tex", 0);
    1.36 +			set_uniform_int(sdr_shadow, "shadowmap", 1);
    1.37 +
    1.38 +			if(!(sdr_shadow_notex = create_program_load("sdr/shadow.v.glsl", "sdr/shadow-notex.p.glsl"))) {
    1.39 +				return false;
    1.40 +			}
    1.41 +		}
    1.42  	}
    1.43  
    1.44  	if(!board.init()) {
    1.45 @@ -59,6 +78,7 @@
    1.46  		return false;
    1.47  	}
    1.48  
    1.49 +	assert(glGetError() == GL_NO_ERROR);
    1.50  	return true;
    1.51  }
    1.52  
    1.53 @@ -66,6 +86,7 @@
    1.54  {
    1.55  	board.destroy();
    1.56  	destroy_scenery();
    1.57 +	destroy_shadow();
    1.58  }
    1.59  
    1.60  void game_update(unsigned long time_msec)
    1.61 @@ -86,15 +107,44 @@
    1.62  	float ldir[] = {-10, 20, 10, 1};
    1.63  	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    1.64  
    1.65 -	draw_backdrop();
    1.66 -	draw_scenery();
    1.67 -	board.draw();
    1.68 +	if(opt.shadows && sdr_shadow) {
    1.69 +		printf("shadow pass\n");
    1.70 +
    1.71 +		begin_shadow_pass(Vector3(-10, 20, 10), Vector3(0, 0, 0), 25);
    1.72 +		draw_scene();
    1.73 +		end_shadow_pass();
    1.74 +
    1.75 +		glActiveTexture(GL_TEXTURE1);
    1.76 +		glBindTexture(GL_TEXTURE_2D, get_shadow_tex());
    1.77 +
    1.78 +		glMatrixMode(GL_TEXTURE);
    1.79 +		Matrix4x4 shadow_matrix = get_shadow_matrix();
    1.80 +		glLoadMatrixf(shadow_matrix[0]);
    1.81 +
    1.82 +		glActiveTexture(GL_TEXTURE0);
    1.83 +		glMatrixMode(GL_MODELVIEW);
    1.84 +
    1.85 +		draw_scene();
    1.86 +
    1.87 +		glActiveTexture(GL_TEXTURE1);
    1.88 +		glBindTexture(GL_TEXTURE_2D, 0);
    1.89 +		glActiveTexture(GL_TEXTURE0);
    1.90 +	} else {
    1.91 +		draw_scene();
    1.92 +	}
    1.93  
    1.94  	if(dbg_busyloop) {
    1.95  		redisplay();
    1.96  	}
    1.97  }
    1.98  
    1.99 +static void draw_scene()
   1.100 +{
   1.101 +	draw_backdrop();
   1.102 +	draw_scenery();
   1.103 +	board.draw();
   1.104 +}
   1.105 +
   1.106  static void draw_backdrop()
   1.107  {
   1.108  	glPushAttrib(GL_ENABLE_BIT);
   1.109 @@ -150,6 +200,11 @@
   1.110  			dbg_busyloop = !dbg_busyloop;
   1.111  			redisplay();
   1.112  			break;
   1.113 +
   1.114 +		case 's':
   1.115 +			opt.shadows = !opt.shadows;
   1.116 +			redisplay();
   1.117 +			break;
   1.118  		}
   1.119  	}
   1.120  }