3dphotoshoot
changeset 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 |
files | sdr/android_cam_preview.p.glsl sdr/tex.p.glsl sdr/vertex.glsl src/android/assman.c src/assman.h src/camera.h src/game.cc src/sdr.c src/shader.cc src/shader.h src/texture.h |
diffstat | 11 files changed, 148 insertions(+), 109 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/sdr/android_cam_preview.p.glsl Thu Jun 11 04:56:33 2015 +0300 1.3 @@ -0,0 +1,12 @@ 1.4 +#extension GL_OES_EGL_image_external : require 1.5 +precision mediump float; 1.6 + 1.7 +uniform samplerExternalOES tex; 1.8 + 1.9 +varying vec4 tex_coords; 1.10 + 1.11 +void main() 1.12 +{ 1.13 + vec4 texel = texture2D(tex, tex_coords.xy); 1.14 + gl_FragColor = vec4(texel.xyz, 1.0); 1.15 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/sdr/tex.p.glsl Thu Jun 11 04:56:33 2015 +0300 2.3 @@ -0,0 +1,11 @@ 2.4 +precision mediump float; 2.5 + 2.6 +uniform sampler2D tex; 2.7 + 2.8 +varying vec4 tex_coords; 2.9 + 2.10 +void main() 2.11 +{ 2.12 + vec4 texel = texture2D(tex, tex_coords.xy); 2.13 + gl_FragColor = texel; 2.14 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/sdr/vertex.glsl Thu Jun 11 04:56:33 2015 +0300 3.3 @@ -0,0 +1,12 @@ 3.4 +attribute vec4 attr_vertex, attr_texcoord; 3.5 + 3.6 +uniform mat4 matrix_modelview, matrix_projection, matrix_texture; 3.7 + 3.8 +varying vec4 tex_coords; 3.9 + 3.10 +void main() 3.11 +{ 3.12 + mat4 mvp = matrix_projection * matrix_modelview; 3.13 + gl_Position = mvp * attr_vertex; 3.14 + tex_coords = matrix_texture * attr_texcoord; 3.15 +}
4.1 --- a/src/android/assman.c Thu Jun 11 02:53:43 2015 +0300 4.2 +++ b/src/android/assman.c Thu Jun 11 04:56:33 2015 +0300 4.3 @@ -52,6 +52,11 @@ 4.4 return AAsset_seek((AAsset*)fp, offs, whence); 4.5 } 4.6 4.7 +long ass_ftell(ass_file *fp) 4.8 +{ 4.9 + return AAsset_seek((AAsset*)fp, 0, SEEK_SET); 4.10 +} 4.11 + 4.12 size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp) 4.13 { 4.14 return AAsset_read((AAsset*)fp, buf, size * count) / size;
5.1 --- a/src/assman.h Thu Jun 11 02:53:43 2015 +0300 5.2 +++ b/src/assman.h Thu Jun 11 04:56:33 2015 +0300 5.3 @@ -8,6 +8,7 @@ 5.4 ass_file *ass_fopen(const char *fname, const char *mode); 5.5 void ass_fclose(ass_file *fp); 5.6 long ass_fseek(ass_file *fp, long offs, int whence); 5.7 +long ass_ftell(ass_file *fp); 5.8 5.9 size_t ass_fread(void *buf, size_t size, size_t count, ass_file *fp); 5.10
6.1 --- a/src/camera.h Thu Jun 11 02:53:43 2015 +0300 6.2 +++ b/src/camera.h Thu Jun 11 04:56:33 2015 +0300 6.3 @@ -11,6 +11,10 @@ 6.4 }; 6.5 #endif 6.6 6.7 +#ifdef __cplusplus 6.8 +extern "C" { 6.9 +#endif 6.10 + 6.11 int cam_init(void *platform_data); 6.12 void cam_shutdown(void); 6.13 6.14 @@ -26,4 +30,8 @@ 6.15 6.16 int cam_take_picture(void); 6.17 6.18 +#ifdef __cplusplus 6.19 +} 6.20 +#endif 6.21 + 6.22 #endif /* CAMERA_H_ */
7.1 --- a/src/game.cc Thu Jun 11 02:53:43 2015 +0300 7.2 +++ b/src/game.cc Thu Jun 11 04:56:33 2015 +0300 7.3 @@ -5,53 +5,19 @@ 7.4 #include "opengl.h" 7.5 #include "game.h" 7.6 #include "camera.h" 7.7 -#include "sdr.h" 7.8 #include "sanegl.h" 7.9 #include "texture.h" 7.10 +#include "shader.h" 7.11 7.12 -static void draw_quad(float hsz, float vsz, unsigned int sdr); 7.13 +static void draw_quad(float hsz, float vsz); 7.14 7.15 static int win_width, win_height; 7.16 static float win_aspect; 7.17 static int video_width, video_height; 7.18 static float video_aspect; 7.19 -static unsigned int sdrprog, sdrprog_test; 7.20 -static int uloc_tex, uloc_test_tex; 7.21 static struct texture *test_tex; 7.22 7.23 -enum { ATTR_VERTEX, ATTR_TEXCOORD }; 7.24 - 7.25 -static const char *vsdr_source = 7.26 - "attribute vec4 attr_vertex, attr_texcoord;\n" 7.27 - "uniform mat4 matrix_modelview, matrix_projection, matrix_texture;\n" 7.28 - "varying vec4 tex_coords;\n" 7.29 - "void main()\n" 7.30 - "{\n" 7.31 - "\tmat4 mvp = matrix_projection * matrix_modelview;\n" 7.32 - "\tgl_Position = mvp * attr_vertex;\n" 7.33 - "\ttex_coords = matrix_texture * attr_texcoord;\n" 7.34 - "}\n"; 7.35 - 7.36 -static const char *psdr_cam_source = 7.37 - "#extension GL_OES_EGL_image_external : require\n" 7.38 - "precision mediump float;\n" 7.39 - "uniform samplerExternalOES tex;\n" 7.40 - "varying vec4 tex_coords;\n" 7.41 - "void main()\n" 7.42 - "{\n" 7.43 - "\tvec4 texel = texture2D(tex, tex_coords.xy);\n" 7.44 - "\tgl_FragColor = vec4(texel.xyz, 1.0);\n" 7.45 - "}\n"; 7.46 - 7.47 -static const char *psdr_tex_source = 7.48 - "precision mediump float;\n" 7.49 - "uniform sampler2D tex;\n" 7.50 - "varying vec4 tex_coords;\n" 7.51 - "void main()\n" 7.52 - "{\n" 7.53 - "\tvec4 texel = texture2D(tex, tex_coords.xy);\n" 7.54 - "\tgl_FragColor = texel;\n" 7.55 - "}\n"; 7.56 +static SdrProg *sdr_cam, *sdr_tex; 7.57 7.58 extern "C" int game_init(void) 7.59 { 7.60 @@ -62,36 +28,13 @@ 7.61 7.62 glClearColor(0.4, 0.4, 0.4, 1); 7.63 7.64 - if(!(vsdr = create_vertex_shader(vsdr_source))) 7.65 - return -1; 7.66 - assert(glGetError() == GL_NO_ERROR); 7.67 - if(!(psdr_cam = create_pixel_shader(psdr_cam_source))) 7.68 - return -1; 7.69 - assert(glGetError() == GL_NO_ERROR); 7.70 - if(!(psdr_tex = create_pixel_shader(psdr_tex_source))) 7.71 - return -1; 7.72 - assert(glGetError() == GL_NO_ERROR); 7.73 - if(!(sdrprog = create_program_link(vsdr, psdr_cam, 0))) { 7.74 - fprintf(stderr, "failed to create shader program\n"); 7.75 + if(!(sdr_cam = get_sdrprog("sdr/vertex.glsl", "sdr/android_cam_preview.p.glsl"))) { 7.76 return -1; 7.77 } 7.78 - if(!(sdrprog_test = create_program_link(vsdr, psdr_tex, 0))) { 7.79 - fprintf(stderr, "failed to create test shader program\n"); 7.80 + if(!(sdr_tex = get_sdrprog("sdr/vertex.glsl", "sdr/tex.p.glsl"))) { 7.81 return -1; 7.82 } 7.83 7.84 - glUseProgram(sdrprog); 7.85 - glBindAttribLocation(sdrprog, ATTR_VERTEX, "attr_vertex"); 7.86 - glBindAttribLocation(sdrprog, ATTR_TEXCOORD, "attr_texcoord"); 7.87 - uloc_tex = glGetUniformLocation(sdrprog, "tex"); 7.88 - glLinkProgram(sdrprog); 7.89 - 7.90 - glUseProgram(sdrprog_test); 7.91 - glBindAttribLocation(sdrprog_test, ATTR_VERTEX, "attr_vertex"); 7.92 - glBindAttribLocation(sdrprog_test, ATTR_TEXCOORD, "attr_texcoord"); 7.93 - uloc_test_tex = glGetUniformLocation(sdrprog_test, "tex"); 7.94 - glLinkProgram(sdrprog_test); 7.95 - 7.96 if(!(test_tex = get_texture("data/opengl.png"))) { 7.97 return -1; 7.98 } 7.99 @@ -111,7 +54,8 @@ 7.100 extern "C" void game_shutdown(void) 7.101 { 7.102 cam_shutdown(); 7.103 - free_program(sdrprog); 7.104 + delete sdr_cam; 7.105 + delete sdr_tex; 7.106 } 7.107 7.108 extern "C" void game_display(unsigned long msec) 7.109 @@ -133,11 +77,8 @@ 7.110 gl_matrix_mode(GL_TEXTURE); 7.111 gl_load_matrixf(tex_matrix); 7.112 7.113 - glUseProgram(sdrprog); 7.114 + sdr_cam->bind(); 7.115 glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); 7.116 - if(uloc_tex >= 0) { 7.117 - glUniform1i(uloc_tex, 0); 7.118 - } 7.119 7.120 if(video_aspect > win_aspect) { 7.121 xscale = 1.0; 7.122 @@ -146,7 +87,7 @@ 7.123 xscale = video_aspect; 7.124 yscale = 1.0; 7.125 } 7.126 - draw_quad(xscale, yscale, sdrprog); 7.127 + draw_quad(xscale, yscale); 7.128 7.129 gl_matrix_mode(GL_TEXTURE); 7.130 gl_load_identity(); 7.131 @@ -156,21 +97,18 @@ 7.132 gl_load_identity(); 7.133 gl_scalef((float)test_tex->width / (float)test_tex->height, 1, 1); 7.134 7.135 - glUseProgram(sdrprog_test); 7.136 + sdr_tex->bind(); 7.137 glBindTexture(GL_TEXTURE_2D, test_tex->texid); 7.138 - if(uloc_test_tex >= 0) { 7.139 - glUniform1i(uloc_test_tex, 0); 7.140 - } 7.141 7.142 glEnable(GL_BLEND); 7.143 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 7.144 7.145 - draw_quad(0.5, 0.5, sdrprog_test); 7.146 + draw_quad(0.5, 0.5); 7.147 7.148 glDisable(GL_BLEND); 7.149 } 7.150 7.151 -static void draw_quad(float hsz, float vsz, unsigned int sdr) 7.152 +static void draw_quad(float hsz, float vsz) 7.153 { 7.154 static const float varr[] = {-1, -1, 1, -1, 1, 1, -1, 1}; 7.155 static const float tcarr[] = {0, 0, 1, 0, 1, 1, 0, 1}; 7.156 @@ -179,17 +117,19 @@ 7.157 gl_push_matrix(); 7.158 gl_scalef(hsz, vsz, 1); 7.159 7.160 - gl_apply_xform(sdr); 7.161 + if(SdrProg::active) { 7.162 + gl_apply_xform(SdrProg::active->get_globj()); 7.163 + } 7.164 7.165 - glEnableVertexAttribArray(ATTR_VERTEX); 7.166 - glEnableVertexAttribArray(ATTR_TEXCOORD); 7.167 - glVertexAttribPointer(ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr); 7.168 - glVertexAttribPointer(ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr); 7.169 + glEnableVertexAttribArray(SDR_ATTR_VERTEX); 7.170 + glEnableVertexAttribArray(SDR_ATTR_TEXCOORD); 7.171 + glVertexAttribPointer(SDR_ATTR_VERTEX, 2, GL_FLOAT, 0, 0, varr); 7.172 + glVertexAttribPointer(SDR_ATTR_TEXCOORD, 2, GL_FLOAT, 0, 0, tcarr); 7.173 7.174 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 7.175 7.176 - glDisableVertexAttribArray(ATTR_VERTEX); 7.177 - glDisableVertexAttribArray(ATTR_TEXCOORD); 7.178 + glDisableVertexAttribArray(SDR_ATTR_VERTEX); 7.179 + glDisableVertexAttribArray(SDR_ATTR_TEXCOORD); 7.180 7.181 gl_pop_matrix(); 7.182 }
8.1 --- a/src/sdr.c Thu Jun 11 02:53:43 2015 +0300 8.2 +++ b/src/sdr.c Thu Jun 11 04:56:33 2015 +0300 8.3 @@ -5,6 +5,7 @@ 8.4 #include <stdarg.h> 8.5 #include <assert.h> 8.6 #include "opengl.h" 8.7 +#include "assman.h" 8.8 8.9 #if defined(unix) || defined(__unix__) 8.10 #include <unistd.h> 8.11 @@ -136,35 +137,28 @@ 8.12 8.13 unsigned int load_shader(const char *fname, unsigned int sdr_type) 8.14 { 8.15 -#if defined(unix) || defined(__unix__) 8.16 - struct stat st; 8.17 -#endif 8.18 unsigned int sdr; 8.19 size_t filesize; 8.20 - FILE *fp; 8.21 + ass_file *fp; 8.22 char *src; 8.23 8.24 - if(!(fp = fopen(fname, "rb"))) { 8.25 + if(!(fp = ass_fopen(fname, "rb"))) { 8.26 fprintf(stderr, "failed to open shader %s: %s\n", fname, strerror(errno)); 8.27 return 0; 8.28 } 8.29 8.30 -#if defined(unix) || defined(__unix__) 8.31 - fstat(fileno(fp), &st); 8.32 - filesize = st.st_size; 8.33 -#else 8.34 - fseek(fp, 0, SEEK_END); 8.35 - filesize = ftell(fp); 8.36 - fseek(fp, 0, SEEK_SET); 8.37 -#endif /* unix */ 8.38 + filesize = ass_fseek(fp, 0, SEEK_END); 8.39 + /*filesize = ass_ftell(fp);*/ 8.40 + printf(" filesize: %ld\n", filesize); 8.41 + ass_fseek(fp, 0, SEEK_SET); 8.42 8.43 if(!(src = malloc(filesize + 1))) { 8.44 - fclose(fp); 8.45 + ass_fclose(fp); 8.46 return 0; 8.47 } 8.48 - fread(src, 1, filesize, fp); 8.49 + ass_fread(src, 1, filesize, fp); 8.50 src[filesize] = 0; 8.51 - fclose(fp); 8.52 + ass_fclose(fp); 8.53 8.54 fprintf(stderr, "compiling %s shader: %s... ", sdrtypestr(sdr_type), fname); 8.55 sdr = create_shader(src, sdr_type);
9.1 --- a/src/shader.cc Thu Jun 11 02:53:43 2015 +0300 9.2 +++ b/src/shader.cc Thu Jun 11 04:56:33 2015 +0300 9.3 @@ -7,6 +7,8 @@ 9.4 9.5 std::map<std::string, unsigned int> sdrdb; 9.6 9.7 +const SdrProg *SdrProg::active; 9.8 + 9.9 SdrProg::SdrProg() 9.10 { 9.11 prog = 0; 9.12 @@ -59,10 +61,8 @@ 9.13 return false; 9.14 } 9.15 9.16 - if(!link()) { 9.17 - return false; 9.18 - } 9.19 - return true; 9.20 + bind_default_attribs(); 9.21 + return link(); 9.22 } 9.23 9.24 bool SdrProg::create(const char *vsrc, const char *psrc) 9.25 @@ -91,29 +91,32 @@ 9.26 9.27 bool SdrProg::load(const char *vfname, const char *pfname) 9.28 { 9.29 - unsigned int vs = load_vertex_shader(vfname); 9.30 + unsigned int vs = get_shader(vfname, GL_VERTEX_SHADER); 9.31 if(!vs) { 9.32 return false; 9.33 } 9.34 - sdrdb[vfname] = vs; 9.35 9.36 - unsigned int ps = load_pixel_shader(pfname); 9.37 + unsigned int ps = get_shader(pfname, GL_FRAGMENT_SHADER); 9.38 if(!ps) { 9.39 return false; 9.40 } 9.41 - sdrdb[pfname] = ps; 9.42 9.43 printf("creating shader program (%s, %s)\n", vfname, pfname); 9.44 if(!(prog = create_program_link(vs, ps, 0))) { 9.45 return false; 9.46 } 9.47 - valid = true; 9.48 - return true; 9.49 + 9.50 + bind_default_attribs(); 9.51 + return link(); 9.52 } 9.53 9.54 bool SdrProg::link() const 9.55 { 9.56 - return link_program(prog) != -1; 9.57 + if(link_program(prog) == -1) { 9.58 + return false; 9.59 + } 9.60 + valid = true; 9.61 + return true; 9.62 } 9.63 9.64 int SdrProg::get_uniform(const char *name) const 9.65 @@ -149,6 +152,24 @@ 9.66 return true; 9.67 } 9.68 9.69 +void SdrProg::bind_default_attribs() const 9.70 +{ 9.71 + // XXX must be in sync with SdrDefaultAttrib enums in shader.h 9.72 + static const char *def_names[] = { 9.73 + "attr_vertex", 9.74 + "attr_normal", 9.75 + "attr_texcoord", 9.76 + "attr_color", 9.77 + "attr_tangent", 9.78 + 0 9.79 + }; 9.80 + 9.81 + for(int i=0; def_names[i]; i++) { 9.82 + bind_attrib(def_names[i], i); 9.83 + } 9.84 +} 9.85 + 9.86 + 9.87 bool SdrProg::bind() const 9.88 { 9.89 if(!prog || (!valid && !link())) { 9.90 @@ -161,10 +182,11 @@ 9.91 fprintf(stderr, "failed to bind program %d\n", prog); 9.92 return false; 9.93 } 9.94 + 9.95 + active = this; 9.96 return true; 9.97 } 9.98 9.99 - 9.100 unsigned int get_shader(const char *name, unsigned int type) 9.101 { 9.102 std::map<std::string, unsigned int>::const_iterator it = sdrdb.find(name); 9.103 @@ -180,3 +202,13 @@ 9.104 sdrdb[name] = sdr; 9.105 return sdr; 9.106 } 9.107 + 9.108 +SdrProg *get_sdrprog(const char *vfile, const char *pfile) 9.109 +{ 9.110 + SdrProg *sp = new SdrProg; 9.111 + if(!sp->load(vfile, pfile)) { 9.112 + delete sp; 9.113 + return 0; 9.114 + } 9.115 + return sp; 9.116 +}
10.1 --- a/src/shader.h Thu Jun 11 02:53:43 2015 +0300 10.2 +++ b/src/shader.h Thu Jun 11 04:56:33 2015 +0300 10.3 @@ -3,6 +3,15 @@ 10.4 10.5 #include <vector> 10.6 10.7 +enum SdrDefaultAttrib { 10.8 + SDR_ATTR_VERTEX, 10.9 + SDR_ATTR_NORMAL, 10.10 + SDR_ATTR_TEXCOORD, 10.11 + SDR_ATTR_COLOR, 10.12 + SDR_ATTR_TANGENT 10.13 +}; 10.14 + 10.15 + 10.16 class SdrProg { 10.17 private: 10.18 std::vector<unsigned int> priv_sdr; 10.19 @@ -10,6 +19,8 @@ 10.20 mutable bool valid; 10.21 10.22 public: 10.23 + static const SdrProg *active; 10.24 + 10.25 SdrProg(); 10.26 ~SdrProg(); 10.27 10.28 @@ -27,10 +38,15 @@ 10.29 int get_uniform(const char *name) const; 10.30 int get_attrib(const char *name) const; 10.31 bool bind_attrib(const char *name, int loc) const; 10.32 + void bind_default_attribs() const; 10.33 10.34 bool bind() const; 10.35 + 10.36 + unsigned int get_globj() const { return prog; } 10.37 }; 10.38 10.39 unsigned int get_shader(const char *name, unsigned int type); 10.40 10.41 +SdrProg *get_sdrprog(const char *vfile, const char *pfile); 10.42 + 10.43 #endif /* SHADER_H_ */
11.1 --- a/src/texture.h Thu Jun 11 02:53:43 2015 +0300 11.2 +++ b/src/texture.h Thu Jun 11 04:56:33 2015 +0300 11.3 @@ -7,9 +7,17 @@ 11.4 int img_width, img_height; /* in case we have a smaller image in a pow2 texture */ 11.5 }; 11.6 11.7 +#ifdef __cplusplus 11.8 +extern "C" { 11.9 +#endif 11.10 + 11.11 int load_texture(struct texture *tex, const char *fname); 11.12 struct texture *get_texture(const char *fname); 11.13 11.14 int gen_debug_texture(struct texture *tex); 11.15 11.16 +#ifdef __cplusplus 11.17 +} 11.18 +#endif 11.19 + 11.20 #endif /* TEXTURE_H_ */