istereo
changeset 14:b39d8607f4bb
added textures
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Wed, 07 Sep 2011 09:03:51 +0300 |
parents | fe1cb1c567cc |
children | 32503603eb7d |
files | sdr/test.p.glsl sdr/test.v.glsl src/istereo.c src/tex.c src/tex.h |
diffstat | 5 files changed, 90 insertions(+), 41 deletions(-) [+] |
line diff
1.1 --- a/sdr/test.p.glsl Wed Sep 07 08:33:55 2011 +0300 1.2 +++ b/sdr/test.p.glsl Wed Sep 07 09:03:51 2011 +0300 1.3 @@ -2,9 +2,15 @@ 1.4 precision mediump float; 1.5 #endif 1.6 1.7 +uniform sampler2D tex; 1.8 + 1.9 varying vec4 var_color; 1.10 +varying vec2 var_texcoord; 1.11 1.12 void main() 1.13 { 1.14 - gl_FragColor = var_color; 1.15 + vec4 texel = texture2D(tex, var_texcoord); 1.16 + texel.w = 1.0; 1.17 + 1.18 + gl_FragColor = var_color * texel; 1.19 }
2.1 --- a/sdr/test.v.glsl Wed Sep 07 08:33:55 2011 +0300 2.2 +++ b/sdr/test.v.glsl Wed Sep 07 09:03:51 2011 +0300 2.3 @@ -1,12 +1,15 @@ 2.4 uniform mat4 matrix_modelview, matrix_projection; 2.5 2.6 attribute vec4 attr_vertex, attr_color; 2.7 +attribute vec2 attr_texcoord; 2.8 2.9 varying vec4 var_color; 2.10 +varying vec2 var_texcoord; 2.11 2.12 void main() 2.13 { 2.14 mat4 mvp = matrix_projection * matrix_modelview; 2.15 gl_Position = mvp * attr_vertex; 2.16 var_color = attr_color; 2.17 + var_texcoord = attr_texcoord; 2.18 }
3.1 --- a/src/istereo.c Wed Sep 07 08:33:55 2011 +0300 3.2 +++ b/src/istereo.c Wed Sep 07 09:03:51 2011 +0300 3.3 @@ -6,20 +6,28 @@ 3.4 #include "sanegl.h" 3.5 #include "sdr.h" 3.6 #include "respath.h" 3.7 +#include "tex.h" 3.8 3.9 -void dbg_draw(void); 3.10 static unsigned int get_shader_program(const char *vfile, const char *pfile); 3.11 3.12 unsigned int prog; 3.13 +unsigned int tex; 3.14 3.15 int init(void) 3.16 { 3.17 add_resource_path("sdr"); 3.18 + add_resource_path("data"); 3.19 3.20 if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) { 3.21 fprintf(stderr, "failed to load shader program\n"); 3.22 return -1; 3.23 } 3.24 + 3.25 + if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) { 3.26 + fprintf(stderr, "failed to load texture\n"); 3.27 + return -1; 3.28 + } 3.29 + 3.30 return 0; 3.31 } 3.32 3.33 @@ -39,19 +47,26 @@ 3.34 gl_load_identity(); 3.35 gl_translatef(0, 0, -8); 3.36 3.37 - //dbg_draw(); 3.38 + glEnable(GL_TEXTURE_2D); 3.39 + glBindTexture(GL_TEXTURE_2D, tex); 3.40 3.41 gl_begin(GL_QUADS); 3.42 + gl_texcoord2f(0, 0); 3.43 gl_color3f(1, 0, 0); 3.44 gl_vertex3f(-1, -1, 0); 3.45 + gl_texcoord2f(1, 0); 3.46 gl_color3f(0, 1, 0); 3.47 gl_vertex3f(1, -1, 0); 3.48 + gl_texcoord2f(1, 1); 3.49 gl_color3f(0, 0, 1); 3.50 gl_vertex3f(1, 1, 0); 3.51 + gl_texcoord2f(0, 1); 3.52 gl_color3f(1, 1, 0); 3.53 gl_vertex3f(-1, 1, 0); 3.54 gl_end(); 3.55 3.56 + glDisable(GL_TEXTURE_2D); 3.57 + 3.58 assert(glGetError() == GL_NO_ERROR); 3.59 } 3.60 3.61 @@ -80,41 +95,3 @@ 3.62 } 3.63 return prog; 3.64 } 3.65 - 3.66 -void dbg_draw(void) 3.67 -{ 3.68 - static const GLfloat squareVertices[] = { 3.69 - -0.5f, -0.33f, 3.70 - 0.5f, -0.33f, 3.71 - -0.5f, 0.33f, 3.72 - 0.5f, 0.33f, 3.73 - }; 3.74 - 3.75 - static const GLubyte squareColors[] = { 3.76 - 255, 255, 0, 255, 3.77 - 0, 255, 255, 255, 3.78 - 0, 0, 0, 0, 3.79 - 255, 0, 255, 255, 3.80 - }; 3.81 - 3.82 - int vloc, cloc; 3.83 - 3.84 - glUseProgram(prog); 3.85 - 3.86 - /*gl_apply_xform(prog);*/ 3.87 - 3.88 - 3.89 - vloc = 0;/*glGetAttribLocation(prog, "attr_vertex");*/ 3.90 - cloc = 1;/*glGetAttribLocation(prog, "attr_color");*/ 3.91 - assert(vloc >= 0 && cloc >= 0); 3.92 - 3.93 - glVertexAttribPointer(vloc, 2, GL_FLOAT, 0, 0, squareVertices); 3.94 - glEnableVertexAttribArray(vloc); 3.95 - glVertexAttribPointer(cloc, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors); 3.96 - glEnableVertexAttribArray(cloc); 3.97 - 3.98 - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 3.99 - 3.100 - glDisableVertexAttribArray(vloc); 3.101 - glDisableVertexAttribArray(cloc); 3.102 -}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/tex.c Wed Sep 07 09:03:51 2011 +0300 4.3 @@ -0,0 +1,57 @@ 4.4 +#include <stdio.h> 4.5 +#include <stdlib.h> 4.6 +#include <string.h> 4.7 +#include <errno.h> 4.8 +#include "opengl.h" 4.9 +#include "tex.h" 4.10 + 4.11 +unsigned int load_texture(const char *fname) 4.12 +{ 4.13 + unsigned int tex; 4.14 + FILE *fp; 4.15 + void *pixels; 4.16 + int xsz, ysz, sz; 4.17 + char buf[512]; 4.18 + 4.19 + if(!(fp = fopen(fname, "r"))) { 4.20 + fprintf(stderr, "failed to open texture: %s: %s\n", fname, strerror(errno)); 4.21 + return 0; 4.22 + } 4.23 + 4.24 + if(!fgets(buf, sizeof buf, fp) || buf[0] != 'P' || buf[1] != '6') { 4.25 + fprintf(stderr, "invalid format (1): %s\n", fname); 4.26 + fclose(fp); 4.27 + return 0; 4.28 + } 4.29 + if(!fgets(buf, sizeof buf, fp) || sscanf(buf, "%d %d", &xsz, &ysz) != 2) { 4.30 + fprintf(stderr, "invalid format (2): %s\n", fname); 4.31 + fclose(fp); 4.32 + return 0; 4.33 + } 4.34 + fgets(buf, sizeof buf, fp); 4.35 + 4.36 + sz = xsz * ysz * 3; 4.37 + if(!(pixels = malloc(sz))) { 4.38 + fprintf(stderr, "failed to allocate %d bytes\n", sz); 4.39 + fclose(fp); 4.40 + return 0; 4.41 + } 4.42 + if(fread(pixels, 1, xsz * ysz * 3, fp) < sz) { 4.43 + fprintf(stderr, "partial read: %s\n", fname); 4.44 + free(pixels); 4.45 + fclose(fp); 4.46 + return 0; 4.47 + } 4.48 + fclose(fp); 4.49 + 4.50 + glGenTextures(1, &tex); 4.51 + glBindTexture(GL_TEXTURE_2D, tex); 4.52 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 4.53 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 4.54 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 4.55 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 4.56 + glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); 4.57 + free(pixels); 4.58 + 4.59 + return tex; 4.60 +}