3dphotoshoot

diff src/game.cc @ 21:4ca4e3c5a754

port to C++ completed, shader programs now use the SdrProg class
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 11 Jun 2015 04:56:33 +0300
parents c14613d27a3a
children d7fe157c402d
line diff
     1.1 --- a/src/game.cc	Thu Jun 11 02:53:43 2015 +0300
     1.2 +++ b/src/game.cc	Thu Jun 11 04:56:33 2015 +0300
     1.3 @@ -5,53 +5,19 @@
     1.4  #include "opengl.h"
     1.5  #include "game.h"
     1.6  #include "camera.h"
     1.7 -#include "sdr.h"
     1.8  #include "sanegl.h"
     1.9  #include "texture.h"
    1.10 +#include "shader.h"
    1.11  
    1.12 -static void draw_quad(float hsz, float vsz, unsigned int sdr);
    1.13 +static void draw_quad(float hsz, float vsz);
    1.14  
    1.15  static int win_width, win_height;
    1.16  static float win_aspect;
    1.17  static int video_width, video_height;
    1.18  static float video_aspect;
    1.19 -static unsigned int sdrprog, sdrprog_test;
    1.20 -static int uloc_tex, uloc_test_tex;
    1.21  static struct texture *test_tex;
    1.22  
    1.23 -enum { ATTR_VERTEX, ATTR_TEXCOORD };
    1.24 -
    1.25 -static const char *vsdr_source =
    1.26 -	"attribute vec4 attr_vertex, attr_texcoord;\n"
    1.27 -	"uniform mat4 matrix_modelview, matrix_projection, matrix_texture;\n"
    1.28 -	"varying vec4 tex_coords;\n"
    1.29 -	"void main()\n"
    1.30 -	"{\n"
    1.31 -	"\tmat4 mvp = matrix_projection * matrix_modelview;\n"
    1.32 -	"\tgl_Position = mvp * attr_vertex;\n"
    1.33 -	"\ttex_coords = matrix_texture * attr_texcoord;\n"
    1.34 -	"}\n";
    1.35 -
    1.36 -static const char *psdr_cam_source =
    1.37 -	"#extension GL_OES_EGL_image_external : require\n"
    1.38 -	"precision mediump float;\n"
    1.39 -	"uniform samplerExternalOES tex;\n"
    1.40 -	"varying vec4 tex_coords;\n"
    1.41 -	"void main()\n"
    1.42 -	"{\n"
    1.43 -	"\tvec4 texel = texture2D(tex, tex_coords.xy);\n"
    1.44 -	"\tgl_FragColor = vec4(texel.xyz, 1.0);\n"
    1.45 -	"}\n";
    1.46 -
    1.47 -static const char *psdr_tex_source =
    1.48 -	"precision mediump float;\n"
    1.49 -	"uniform sampler2D tex;\n"
    1.50 -	"varying vec4 tex_coords;\n"
    1.51 -	"void main()\n"
    1.52 -	"{\n"
    1.53 -	"\tvec4 texel = texture2D(tex, tex_coords.xy);\n"
    1.54 -	"\tgl_FragColor = texel;\n"
    1.55 -	"}\n";
    1.56 +static SdrProg *sdr_cam, *sdr_tex;
    1.57  
    1.58  extern "C" int game_init(void)
    1.59  {
    1.60 @@ -62,36 +28,13 @@
    1.61  
    1.62  	glClearColor(0.4, 0.4, 0.4, 1);
    1.63  
    1.64 -	if(!(vsdr = create_vertex_shader(vsdr_source)))
    1.65 -		return -1;
    1.66 -	assert(glGetError() == GL_NO_ERROR);
    1.67 -	if(!(psdr_cam = create_pixel_shader(psdr_cam_source)))
    1.68 -		return -1;
    1.69 -	assert(glGetError() == GL_NO_ERROR);
    1.70 -	if(!(psdr_tex = create_pixel_shader(psdr_tex_source)))
    1.71 -		return -1;
    1.72 -	assert(glGetError() == GL_NO_ERROR);
    1.73 -	if(!(sdrprog = create_program_link(vsdr, psdr_cam, 0))) {
    1.74 -		fprintf(stderr, "failed to create shader program\n");
    1.75 +	if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) {
    1.76  		return -1;
    1.77  	}
    1.78 -	if(!(sdrprog_test = create_program_link(vsdr, psdr_tex, 0))) {
    1.79 -		fprintf(stderr, "failed to create test shader program\n");
    1.80 +	if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) {
    1.81  		return -1;
    1.82  	}
    1.83  
    1.84 -	glUseProgram(sdrprog);
    1.85 -	glBindAttribLocation(sdrprog, ATTR_VERTEX, "attr_vertex");
    1.86 -	glBindAttribLocation(sdrprog, ATTR_TEXCOORD, "attr_texcoord");
    1.87 -	uloc_tex = glGetUniformLocation(sdrprog, "tex");
    1.88 -	glLinkProgram(sdrprog);
    1.89 -
    1.90 -	glUseProgram(sdrprog_test);
    1.91 -	glBindAttribLocation(sdrprog_test, ATTR_VERTEX, "attr_vertex");
    1.92 -	glBindAttribLocation(sdrprog_test, ATTR_TEXCOORD, "attr_texcoord");
    1.93 -	uloc_test_tex = glGetUniformLocation(sdrprog_test, "tex");
    1.94 -	glLinkProgram(sdrprog_test);
    1.95 -
    1.96  	if(!(test_tex = get_texture("data/opengl.png"))) {
    1.97  		return -1;
    1.98  	}
    1.99 @@ -111,7 +54,8 @@
   1.100  extern "C" void game_shutdown(void)
   1.101  {
   1.102  	cam_shutdown();
   1.103 -	free_program(sdrprog);
   1.104 +	delete sdr_cam;
   1.105 +	delete sdr_tex;
   1.106  }
   1.107  
   1.108  extern "C" void game_display(unsigned long msec)
   1.109 @@ -133,11 +77,8 @@
   1.110  	gl_matrix_mode(GL_TEXTURE);
   1.111  	gl_load_matrixf(tex_matrix);
   1.112  
   1.113 -	glUseProgram(sdrprog);
   1.114 +	sdr_cam->bind();
   1.115  	glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
   1.116 -	if(uloc_tex >= 0) {
   1.117 -		glUniform1i(uloc_tex, 0);
   1.118 -	}
   1.119  
   1.120  	if(video_aspect > win_aspect) {
   1.121  		xscale = 1.0;
   1.122 @@ -146,7 +87,7 @@
   1.123  		xscale = video_aspect;
   1.124  		yscale = 1.0;
   1.125  	}
   1.126 -	draw_quad(xscale, yscale, sdrprog);
   1.127 +	draw_quad(xscale, yscale);
   1.128  
   1.129  	gl_matrix_mode(GL_TEXTURE);
   1.130  	gl_load_identity();
   1.131 @@ -156,21 +97,18 @@
   1.132  	gl_load_identity();
   1.133  	gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1);
   1.134  
   1.135 -	glUseProgram(sdrprog_test);
   1.136 +	sdr_tex->bind();
   1.137  	glBindTexture(GL_TEXTURE_2D, test_tex->texid);
   1.138 -	if(uloc_test_tex >= 0) {
   1.139 -		glUniform1i(uloc_test_tex, 0);
   1.140 -	}
   1.141  
   1.142  	glEnable(GL_BLEND);
   1.143  	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.144  
   1.145 -	draw_quad(0.5, 0.5, sdrprog_test);
   1.146 +	draw_quad(0.5, 0.5);
   1.147  
   1.148  	glDisable(GL_BLEND);
   1.149  }
   1.150  
   1.151 -static void draw_quad(float hsz, float vsz, unsigned int sdr)
   1.152 +static void draw_quad(float hsz, float vsz)
   1.153  {
   1.154  	static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1};
   1.155  	static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1};
   1.156 @@ -179,17 +117,19 @@
   1.157  	gl_push_matrix();
   1.158  	gl_scalef(hsz, vsz, 1);
   1.159  
   1.160 -	gl_apply_xform(sdr);
   1.161 +	if(SdrProg::active) {
   1.162 +		gl_apply_xform(SdrProg::active->get_globj());
   1.163 +	}
   1.164  
   1.165 -	glEnableVertexAttribArray(ATTR_VERTEX);
   1.166 -	glEnableVertexAttribArray(ATTR_TEXCOORD);
   1.167 -	glVertexAttribPointer(ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
   1.168 -	glVertexAttribPointer(ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
   1.169 +	glEnableVertexAttribArray(SDR_ATTR_VERTEX);
   1.170 +	glEnableVertexAttribArray(SDR_ATTR_TEXCOORD);
   1.171 +	glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr);
   1.172 +	glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr);
   1.173  
   1.174  	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
   1.175  
   1.176 -	glDisableVertexAttribArray(ATTR_VERTEX);
   1.177 -	glDisableVertexAttribArray(ATTR_TEXCOORD);
   1.178 +	glDisableVertexAttribArray(SDR_ATTR_VERTEX);
   1.179 +	glDisableVertexAttribArray(SDR_ATTR_TEXCOORD);
   1.180  
   1.181  	gl_pop_matrix();
   1.182  }