3dphotoshoot
diff src/game.c @ 17:aef7f51f6397
resource loading works
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 10 Jun 2015 06:56:27 +0300 |
parents | ad49e1f9b627 |
children |
line diff
1.1 --- a/src/game.c Tue Jun 09 18:38:33 2015 +0300 1.2 +++ b/src/game.c Wed Jun 10 06:56:27 2015 +0300 1.3 @@ -7,15 +7,19 @@ 1.4 #include "camera.h" 1.5 #include "sdr.h" 1.6 #include "sanegl.h" 1.7 +#include "texture.h" 1.8 1.9 -static void draw_quad(float hsz, float vsz); 1.10 +static void draw_quad(float hsz, float vsz, unsigned int sdr); 1.11 1.12 static int win_width, win_height; 1.13 static float win_aspect; 1.14 static int video_width, video_height; 1.15 static float video_aspect; 1.16 -static unsigned int sdrprog; 1.17 -static int aloc_vertex, aloc_texcoord, uloc_tex; 1.18 +static unsigned int sdrprog, sdrprog_test; 1.19 +static int uloc_tex, uloc_test_tex; 1.20 +static struct texture *test_tex; 1.21 + 1.22 +enum { ATTR_VERTEX, ATTR_TEXCOORD }; 1.23 1.24 static const char *vsdr_source = 1.25 "attribute vec4 attr_vertex, attr_texcoord;\n" 1.26 @@ -28,7 +32,7 @@ 1.27 "\ttex_coords = matrix_texture * attr_texcoord;\n" 1.28 "}\n"; 1.29 1.30 -static const char *psdr_source = 1.31 +static const char *psdr_cam_source = 1.32 "#extension GL_OES_EGL_image_external : require\n" 1.33 "precision mediump float;\n" 1.34 "uniform samplerExternalOES tex;\n" 1.35 @@ -39,12 +43,21 @@ 1.36 "\tgl_FragColor = vec4(texel.xyz, 1.0);\n" 1.37 "}\n"; 1.38 1.39 +static const char *psdr_tex_source = 1.40 + "precision mediump float;\n" 1.41 + "uniform sampler2D tex;\n" 1.42 + "varying vec4 tex_coords;\n" 1.43 + "void main()\n" 1.44 + "{\n" 1.45 + "\tvec4 texel = texture2D(tex, tex_coords.xy);\n" 1.46 + "\tgl_FragColor = texel;\n" 1.47 + "}\n"; 1.48 1.49 int game_init(void) 1.50 { 1.51 - unsigned int vsdr, psdr; 1.52 + unsigned int vsdr, psdr_cam, psdr_tex; 1.53 1.54 - glEnable(GL_DEPTH_TEST); 1.55 + //glEnable(GL_DEPTH_TEST); 1.56 glEnable(GL_CULL_FACE); 1.57 1.58 glClearColor(0.4, 0.4, 0.4, 1); 1.59 @@ -52,17 +65,36 @@ 1.60 if(!(vsdr = create_vertex_shader(vsdr_source))) 1.61 return -1; 1.62 assert(glGetError() == GL_NO_ERROR); 1.63 - if(!(psdr = create_pixel_shader(psdr_source))) 1.64 + if(!(psdr_cam = create_pixel_shader(psdr_cam_source))) 1.65 return -1; 1.66 assert(glGetError() == GL_NO_ERROR); 1.67 - if(!(sdrprog = create_program_link(vsdr, psdr))) { 1.68 + if(!(psdr_tex = create_pixel_shader(psdr_tex_source))) 1.69 + return -1; 1.70 + assert(glGetError() == GL_NO_ERROR); 1.71 + if(!(sdrprog = create_program_link(vsdr, psdr_cam, 0))) { 1.72 fprintf(stderr, "failed to create shader program\n"); 1.73 return -1; 1.74 } 1.75 + if(!(sdrprog_test = create_program_link(vsdr, psdr_tex, 0))) { 1.76 + fprintf(stderr, "failed to create test shader program\n"); 1.77 + return -1; 1.78 + } 1.79 + 1.80 glUseProgram(sdrprog); 1.81 - aloc_vertex = glGetAttribLocation(sdrprog, "attr_vertex"); 1.82 - aloc_texcoord = glGetAttribLocation(sdrprog, "attr_texcoord"); 1.83 + glBindAttribLocation(sdrprog, ATTR_VERTEX, "attr_vertex"); 1.84 + glBindAttribLocation(sdrprog, ATTR_TEXCOORD, "attr_texcoord"); 1.85 uloc_tex = glGetUniformLocation(sdrprog, "tex"); 1.86 + glLinkProgram(sdrprog); 1.87 + 1.88 + glUseProgram(sdrprog_test); 1.89 + glBindAttribLocation(sdrprog_test, ATTR_VERTEX, "attr_vertex"); 1.90 + glBindAttribLocation(sdrprog_test, ATTR_TEXCOORD, "attr_texcoord"); 1.91 + uloc_test_tex = glGetUniformLocation(sdrprog_test, "tex"); 1.92 + glLinkProgram(sdrprog_test); 1.93 + 1.94 + if(!(test_tex = get_texture("data/opengl.png"))) { 1.95 + return -1; 1.96 + } 1.97 1.98 cam_start_video(); 1.99 cam_video_size(&video_width, &video_height); 1.100 @@ -94,7 +126,6 @@ 1.101 1.102 //float tsec = (float)msec / 1000.0f; 1.103 1.104 - //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0); 1.105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.106 1.107 gl_matrix_mode(GL_MODELVIEW); 1.108 @@ -115,37 +146,50 @@ 1.109 xscale = video_aspect; 1.110 yscale = 1.0; 1.111 } 1.112 - draw_quad(xscale, yscale); 1.113 + draw_quad(xscale, yscale, sdrprog); 1.114 + 1.115 + gl_matrix_mode(GL_TEXTURE); 1.116 + gl_load_identity(); 1.117 + gl_translatef(0, 1, 0); 1.118 + gl_scalef(1, -1, 1); 1.119 + gl_matrix_mode(GL_MODELVIEW); 1.120 + gl_load_identity(); 1.121 + gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1); 1.122 + 1.123 + glUseProgram(sdrprog_test); 1.124 + glBindTexture(GL_TEXTURE_2D, test_tex->texid); 1.125 + if(uloc_test_tex >= 0) { 1.126 + glUniform1i(uloc_test_tex, 0); 1.127 + } 1.128 + 1.129 + glEnable(GL_BLEND); 1.130 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.131 + 1.132 + draw_quad(0.5, 0.5, sdrprog_test); 1.133 + 1.134 + glDisable(GL_BLEND); 1.135 } 1.136 1.137 -static void draw_quad(float hsz, float vsz) 1.138 +static void draw_quad(float hsz, float vsz, unsigned int sdr) 1.139 { 1.140 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1}; 1.141 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1}; 1.142 1.143 - if(aloc_vertex == -1) { 1.144 - return; 1.145 - } 1.146 - 1.147 gl_matrix_mode(GL_MODELVIEW); 1.148 gl_push_matrix(); 1.149 gl_scalef(hsz, vsz, 1); 1.150 1.151 - gl_apply_xform(sdrprog); 1.152 + gl_apply_xform(sdr); 1.153 1.154 - glEnableVertexAttribArray(aloc_vertex); 1.155 - glVertexAttribPointer(aloc_vertex, 2, GL_FLOAT, 0, 0, varr); 1.156 - if(aloc_texcoord != -1) { 1.157 - glEnableVertexAttribArray(aloc_texcoord); 1.158 - glVertexAttribPointer(aloc_texcoord, 2, GL_FLOAT, 0, 0, tcarr); 1.159 - } 1.160 + glEnableVertexAttribArray(ATTR_VERTEX); 1.161 + glEnableVertexAttribArray(ATTR_TEXCOORD); 1.162 + glVertexAttribPointer(ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr); 1.163 + glVertexAttribPointer(ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr); 1.164 1.165 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1.166 1.167 - glDisableVertexAttribArray(aloc_vertex); 1.168 - if(aloc_texcoord != -1) { 1.169 - glDisableVertexAttribArray(aloc_texcoord); 1.170 - } 1.171 + glDisableVertexAttribArray(ATTR_VERTEX); 1.172 + glDisableVertexAttribArray(ATTR_TEXCOORD); 1.173 1.174 gl_pop_matrix(); 1.175 }