3dphotoshoot
diff src/game.c @ 11:ad49e1f9b627
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 06:02:08 +0300 |
parents | c71c477521ca |
children | aef7f51f6397 |
line diff
1.1 --- a/src/game.c Sun May 31 00:40:26 2015 +0300 1.2 +++ b/src/game.c Sun May 31 06:02:08 2015 +0300 1.3 @@ -1,10 +1,12 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 #include <math.h> 1.7 +#include <assert.h> 1.8 #include "opengl.h" 1.9 #include "game.h" 1.10 #include "camera.h" 1.11 #include "sdr.h" 1.12 +#include "sanegl.h" 1.13 1.14 static void draw_quad(float hsz, float vsz); 1.15 1.16 @@ -13,22 +15,22 @@ 1.17 static int video_width, video_height; 1.18 static float video_aspect; 1.19 static unsigned int sdrprog; 1.20 -static int aloc_vertex, aloc_texcoord, uloc_texmat, uloc_wmat, uloc_vmat, uloc_pmat; 1.21 +static int aloc_vertex, aloc_texcoord, uloc_tex; 1.22 1.23 static const char *vsdr_source = 1.24 - "attribute vec4 attr_pos, attr_tex;\n" 1.25 - "uniform mat4 world_matrix, proj_matrix, tex_matrix;\n" 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 = proj_matrix * world_matrix;\n" 1.32 - "\tgl_Position = mvp * attr_pos;\n" 1.33 - "\ttex_coords = tex_matrix * attr_tex;\n" 1.34 + "\tmat4 mvp = matrix_projection * matrix_modelview;\n" 1.35 + "\tgl_Position = mvp * attr_vertex;\n" 1.36 + "\ttex_coords = matrix_texture * attr_texcoord;\n" 1.37 "}\n"; 1.38 1.39 static const char *psdr_source = 1.40 "#extension GL_OES_EGL_image_external : require\n" 1.41 - "precision lowp float;\n" 1.42 + "precision mediump float;\n" 1.43 "uniform samplerExternalOES tex;\n" 1.44 "varying vec4 tex_coords;\n" 1.45 "void main()\n" 1.46 @@ -49,19 +51,18 @@ 1.47 1.48 if(!(vsdr = create_vertex_shader(vsdr_source))) 1.49 return -1; 1.50 + assert(glGetError() == GL_NO_ERROR); 1.51 if(!(psdr = create_pixel_shader(psdr_source))) 1.52 return -1; 1.53 + assert(glGetError() == GL_NO_ERROR); 1.54 if(!(sdrprog = create_program_link(vsdr, psdr))) { 1.55 fprintf(stderr, "failed to create shader program\n"); 1.56 return -1; 1.57 } 1.58 glUseProgram(sdrprog); 1.59 - aloc_vertex = glGetAttribLocation(sdrprog, "attr_pos"); 1.60 - aloc_texcoord = glGetAttribLocation(sdrprog, "attr_tex"); 1.61 - uloc_texmat = glGetUniformLocation(sdrprog, "tex_matrix"); 1.62 - uloc_wmat = glGetUniformLocation(sdrprog, "world_matrix"); 1.63 - uloc_vmat = glGetUniformLocation(sdrprog, "view_matrix"); 1.64 - uloc_pmat = glGetUniformLocation(sdrprog, "proj_matrix"); 1.65 + aloc_vertex = glGetAttribLocation(sdrprog, "attr_vertex"); 1.66 + aloc_texcoord = glGetAttribLocation(sdrprog, "attr_texcoord"); 1.67 + uloc_tex = glGetUniformLocation(sdrprog, "tex"); 1.68 1.69 cam_start_video(); 1.70 cam_video_size(&video_width, &video_height); 1.71 @@ -96,9 +97,16 @@ 1.72 //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0); 1.73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.74 1.75 + gl_matrix_mode(GL_MODELVIEW); 1.76 + gl_load_identity(); 1.77 + gl_matrix_mode(GL_TEXTURE); 1.78 + gl_load_matrixf(tex_matrix); 1.79 + 1.80 glUseProgram(sdrprog); 1.81 - glUniformMatrix4fv(uloc_texmat, 1, 0, tex_matrix); 1.82 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); 1.83 + if(uloc_tex >= 0) { 1.84 + glUniform1i(uloc_tex, 0); 1.85 + } 1.86 1.87 if(video_aspect > win_aspect) { 1.88 xscale = 1.0; 1.89 @@ -114,19 +122,20 @@ 1.90 { 1.91 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1}; 1.92 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1}; 1.93 - float xform[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; 1.94 1.95 if(aloc_vertex == -1) { 1.96 return; 1.97 } 1.98 1.99 - xform[0] = hsz; 1.100 - xform[5] = vsz; 1.101 - glUniformMatrix4fv(uloc_wmat, 1, 0, xform); 1.102 + gl_matrix_mode(GL_MODELVIEW); 1.103 + gl_push_matrix(); 1.104 + gl_scalef(hsz, vsz, 1); 1.105 + 1.106 + gl_apply_xform(sdrprog); 1.107 1.108 glEnableVertexAttribArray(aloc_vertex); 1.109 glVertexAttribPointer(aloc_vertex, 2, GL_FLOAT, 0, 0, varr); 1.110 - if(aloc_texcoord) { 1.111 + if(aloc_texcoord != -1) { 1.112 glEnableVertexAttribArray(aloc_texcoord); 1.113 glVertexAttribPointer(aloc_texcoord, 2, GL_FLOAT, 0, 0, tcarr); 1.114 } 1.115 @@ -134,9 +143,11 @@ 1.116 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1.117 1.118 glDisableVertexAttribArray(aloc_vertex); 1.119 - if(aloc_texcoord) { 1.120 + if(aloc_texcoord != -1) { 1.121 glDisableVertexAttribArray(aloc_texcoord); 1.122 } 1.123 + 1.124 + gl_pop_matrix(); 1.125 } 1.126 1.127 void game_reshape(int x, int y) 1.128 @@ -146,9 +157,9 @@ 1.129 win_aspect = y ? (float)x / (float)y : 1.0; 1.130 glViewport(0, 0, x, y); 1.131 1.132 - /*glMatrixMode(GL_PROJECTION); 1.133 - glLoadIdentity(); 1.134 - glScalef((float)win_height / (float)win_width, 1.0, 1.0);*/ 1.135 + gl_matrix_mode(GL_PROJECTION); 1.136 + gl_load_identity(); 1.137 + gl_scalef((float)win_height / (float)win_width, 1, 1); 1.138 } 1.139 1.140 void game_keyboard(int key, int pressed)