3dphotoshoot
diff src/game.c @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | d1b456d08713 |
children | ad49e1f9b627 |
line diff
1.1 --- a/src/game.c Mon May 25 05:27:26 2015 +0300 1.2 +++ b/src/game.c Sun May 31 00:40:26 2015 +0300 1.3 @@ -4,6 +4,7 @@ 1.4 #include "opengl.h" 1.5 #include "game.h" 1.6 #include "camera.h" 1.7 +#include "sdr.h" 1.8 1.9 static void draw_quad(float hsz, float vsz); 1.10 1.11 @@ -11,16 +12,57 @@ 1.12 static float win_aspect; 1.13 static int video_width, video_height; 1.14 static float video_aspect; 1.15 +static unsigned int sdrprog; 1.16 +static int aloc_vertex, aloc_texcoord, uloc_texmat, uloc_wmat, uloc_vmat, uloc_pmat; 1.17 + 1.18 +static const char *vsdr_source = 1.19 + "attribute vec4 attr_pos, attr_tex;\n" 1.20 + "uniform mat4 world_matrix, proj_matrix, tex_matrix;\n" 1.21 + "varying vec4 tex_coords;\n" 1.22 + "void main()\n" 1.23 + "{\n" 1.24 + "\tmat4 mvp = proj_matrix * world_matrix;\n" 1.25 + "\tgl_Position = mvp * attr_pos;\n" 1.26 + "\ttex_coords = tex_matrix * attr_tex;\n" 1.27 + "}\n"; 1.28 + 1.29 +static const char *psdr_source = 1.30 + "#extension GL_OES_EGL_image_external : require\n" 1.31 + "precision lowp float;\n" 1.32 + "uniform samplerExternalOES tex;\n" 1.33 + "varying vec4 tex_coords;\n" 1.34 + "void main()\n" 1.35 + "{\n" 1.36 + "\tvec4 texel = texture2D(tex, tex_coords.xy);\n" 1.37 + "\tgl_FragColor = vec4(texel.xyz, 1.0);\n" 1.38 + "}\n"; 1.39 1.40 1.41 int game_init(void) 1.42 { 1.43 + unsigned int vsdr, psdr; 1.44 + 1.45 glEnable(GL_DEPTH_TEST); 1.46 glEnable(GL_CULL_FACE); 1.47 - //glEnable(GL_LIGHTING); 1.48 1.49 glClearColor(0.4, 0.4, 0.4, 1); 1.50 1.51 + if(!(vsdr = create_vertex_shader(vsdr_source))) 1.52 + return -1; 1.53 + if(!(psdr = create_pixel_shader(psdr_source))) 1.54 + return -1; 1.55 + if(!(sdrprog = create_program_link(vsdr, psdr))) { 1.56 + fprintf(stderr, "failed to create shader program\n"); 1.57 + return -1; 1.58 + } 1.59 + glUseProgram(sdrprog); 1.60 + aloc_vertex = glGetAttribLocation(sdrprog, "attr_pos"); 1.61 + aloc_texcoord = glGetAttribLocation(sdrprog, "attr_tex"); 1.62 + uloc_texmat = glGetUniformLocation(sdrprog, "tex_matrix"); 1.63 + uloc_wmat = glGetUniformLocation(sdrprog, "world_matrix"); 1.64 + uloc_vmat = glGetUniformLocation(sdrprog, "view_matrix"); 1.65 + uloc_pmat = glGetUniformLocation(sdrprog, "proj_matrix"); 1.66 + 1.67 cam_start_video(); 1.68 cam_video_size(&video_width, &video_height); 1.69 if(video_height) { 1.70 @@ -36,6 +78,7 @@ 1.71 void game_shutdown(void) 1.72 { 1.73 cam_shutdown(); 1.74 + free_program(sdrprog); 1.75 } 1.76 1.77 void game_display(unsigned long msec) 1.78 @@ -53,17 +96,9 @@ 1.79 //glClearColor(sin(tsec * 10.0), cos(tsec * 10.0), -sin(tsec * 10.0), 1.0); 1.80 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.81 1.82 - glMatrixMode(GL_MODELVIEW); 1.83 - glLoadIdentity(); 1.84 - 1.85 - if(tex) { 1.86 - glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); 1.87 - glEnable(GL_TEXTURE_EXTERNAL_OES); 1.88 - 1.89 - glMatrixMode(GL_TEXTURE); 1.90 - glLoadMatrixf(tex_matrix); 1.91 - glMatrixMode(GL_MODELVIEW); 1.92 - } 1.93 + glUseProgram(sdrprog); 1.94 + glUniformMatrix4fv(uloc_texmat, 1, 0, tex_matrix); 1.95 + glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); 1.96 1.97 if(video_aspect > win_aspect) { 1.98 xscale = 1.0; 1.99 @@ -73,40 +108,35 @@ 1.100 yscale = 1.0; 1.101 } 1.102 draw_quad(xscale, yscale); 1.103 - 1.104 - if(tex) { 1.105 - glDisable(GL_TEXTURE_EXTERNAL_OES); 1.106 - 1.107 - glMatrixMode(GL_TEXTURE); 1.108 - glLoadIdentity(); 1.109 - glMatrixMode(GL_MODELVIEW); 1.110 - } 1.111 } 1.112 1.113 static void draw_quad(float hsz, float vsz) 1.114 { 1.115 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1}; 1.116 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1}; 1.117 - static const float colarr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 1.118 + float xform[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; 1.119 1.120 - glPushMatrix(); 1.121 - glScalef(hsz, vsz, 1); 1.122 + if(aloc_vertex == -1) { 1.123 + return; 1.124 + } 1.125 1.126 - glEnableClientState(GL_VERTEX_ARRAY); 1.127 - glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1.128 - glEnableClientState(GL_COLOR_ARRAY); 1.129 + xform[0] = hsz; 1.130 + xform[5] = vsz; 1.131 + glUniformMatrix4fv(uloc_wmat, 1, 0, xform); 1.132 1.133 - glVertexPointer(2, GL_FLOAT, 0, varr); 1.134 - glTexCoordPointer(2, GL_FLOAT, 0, tcarr); 1.135 - glColorPointer(4, GL_FLOAT, 0, colarr); 1.136 + glEnableVertexAttribArray(aloc_vertex); 1.137 + glVertexAttribPointer(aloc_vertex, 2, GL_FLOAT, 0, 0, varr); 1.138 + if(aloc_texcoord) { 1.139 + glEnableVertexAttribArray(aloc_texcoord); 1.140 + glVertexAttribPointer(aloc_texcoord, 2, GL_FLOAT, 0, 0, tcarr); 1.141 + } 1.142 1.143 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 1.144 1.145 - glDisableClientState(GL_VERTEX_ARRAY); 1.146 - glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1.147 - glDisableClientState(GL_COLOR_ARRAY); 1.148 - 1.149 - glPopMatrix(); 1.150 + glDisableVertexAttribArray(aloc_vertex); 1.151 + if(aloc_texcoord) { 1.152 + glDisableVertexAttribArray(aloc_texcoord); 1.153 + } 1.154 } 1.155 1.156 void game_reshape(int x, int y) 1.157 @@ -116,9 +146,9 @@ 1.158 win_aspect = y ? (float)x / (float)y : 1.0; 1.159 glViewport(0, 0, x, y); 1.160 1.161 - glMatrixMode(GL_PROJECTION); 1.162 + /*glMatrixMode(GL_PROJECTION); 1.163 glLoadIdentity(); 1.164 - glScalef((float)win_height / (float)win_width, 1.0, 1.0); 1.165 + glScalef((float)win_height / (float)win_width, 1.0, 1.0);*/ 1.166 } 1.167 1.168 void game_keyboard(int key, int pressed)