# HG changeset patch # User John Tsiombikas # Date 1315375431 -10800 # Node ID b39d8607f4bbd59afae3bc4995a692e649af854b # Parent fe1cb1c567cc846b10d329567a1cada2a46834ed added textures diff -r fe1cb1c567cc -r b39d8607f4bb sdr/test.p.glsl --- a/sdr/test.p.glsl Wed Sep 07 08:33:55 2011 +0300 +++ b/sdr/test.p.glsl Wed Sep 07 09:03:51 2011 +0300 @@ -2,9 +2,15 @@ precision mediump float; #endif +uniform sampler2D tex; + varying vec4 var_color; +varying vec2 var_texcoord; void main() { - gl_FragColor = var_color; + vec4 texel = texture2D(tex, var_texcoord); + texel.w = 1.0; + + gl_FragColor = var_color * texel; } diff -r fe1cb1c567cc -r b39d8607f4bb sdr/test.v.glsl --- a/sdr/test.v.glsl Wed Sep 07 08:33:55 2011 +0300 +++ b/sdr/test.v.glsl Wed Sep 07 09:03:51 2011 +0300 @@ -1,12 +1,15 @@ uniform mat4 matrix_modelview, matrix_projection; attribute vec4 attr_vertex, attr_color; +attribute vec2 attr_texcoord; varying vec4 var_color; +varying vec2 var_texcoord; void main() { mat4 mvp = matrix_projection * matrix_modelview; gl_Position = mvp * attr_vertex; var_color = attr_color; + var_texcoord = attr_texcoord; } diff -r fe1cb1c567cc -r b39d8607f4bb src/istereo.c --- a/src/istereo.c Wed Sep 07 08:33:55 2011 +0300 +++ b/src/istereo.c Wed Sep 07 09:03:51 2011 +0300 @@ -6,20 +6,28 @@ #include "sanegl.h" #include "sdr.h" #include "respath.h" +#include "tex.h" -void dbg_draw(void); static unsigned int get_shader_program(const char *vfile, const char *pfile); unsigned int prog; +unsigned int tex; int init(void) { add_resource_path("sdr"); + add_resource_path("data"); if(!(prog = get_shader_program("test.v.glsl", "test.p.glsl"))) { fprintf(stderr, "failed to load shader program\n"); return -1; } + + if(!(tex = load_texture(find_resource("tiles.ppm", 0, 0)))) { + fprintf(stderr, "failed to load texture\n"); + return -1; + } + return 0; } @@ -39,19 +47,26 @@ gl_load_identity(); gl_translatef(0, 0, -8); - //dbg_draw(); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, tex); gl_begin(GL_QUADS); + gl_texcoord2f(0, 0); gl_color3f(1, 0, 0); gl_vertex3f(-1, -1, 0); + gl_texcoord2f(1, 0); gl_color3f(0, 1, 0); gl_vertex3f(1, -1, 0); + gl_texcoord2f(1, 1); gl_color3f(0, 0, 1); gl_vertex3f(1, 1, 0); + gl_texcoord2f(0, 1); gl_color3f(1, 1, 0); gl_vertex3f(-1, 1, 0); gl_end(); + glDisable(GL_TEXTURE_2D); + assert(glGetError() == GL_NO_ERROR); } @@ -80,41 +95,3 @@ } return prog; } - -void dbg_draw(void) -{ - static const GLfloat squareVertices[] = { - -0.5f, -0.33f, - 0.5f, -0.33f, - -0.5f, 0.33f, - 0.5f, 0.33f, - }; - - static const GLubyte squareColors[] = { - 255, 255, 0, 255, - 0, 255, 255, 255, - 0, 0, 0, 0, - 255, 0, 255, 255, - }; - - int vloc, cloc; - - glUseProgram(prog); - - /*gl_apply_xform(prog);*/ - - - vloc = 0;/*glGetAttribLocation(prog, "attr_vertex");*/ - cloc = 1;/*glGetAttribLocation(prog, "attr_color");*/ - assert(vloc >= 0 && cloc >= 0); - - glVertexAttribPointer(vloc, 2, GL_FLOAT, 0, 0, squareVertices); - glEnableVertexAttribArray(vloc); - glVertexAttribPointer(cloc, 4, GL_UNSIGNED_BYTE, 1, 0, squareColors); - glEnableVertexAttribArray(cloc); - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - - glDisableVertexAttribArray(vloc); - glDisableVertexAttribArray(cloc); -} diff -r fe1cb1c567cc -r b39d8607f4bb src/tex.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tex.c Wed Sep 07 09:03:51 2011 +0300 @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include "opengl.h" +#include "tex.h" + +unsigned int load_texture(const char *fname) +{ + unsigned int tex; + FILE *fp; + void *pixels; + int xsz, ysz, sz; + char buf[512]; + + if(!(fp = fopen(fname, "r"))) { + fprintf(stderr, "failed to open texture: %s: %s\n", fname, strerror(errno)); + return 0; + } + + if(!fgets(buf, sizeof buf, fp) || buf[0] != 'P' || buf[1] != '6') { + fprintf(stderr, "invalid format (1): %s\n", fname); + fclose(fp); + return 0; + } + if(!fgets(buf, sizeof buf, fp) || sscanf(buf, "%d %d", &xsz, &ysz) != 2) { + fprintf(stderr, "invalid format (2): %s\n", fname); + fclose(fp); + return 0; + } + fgets(buf, sizeof buf, fp); + + sz = xsz * ysz * 3; + if(!(pixels = malloc(sz))) { + fprintf(stderr, "failed to allocate %d bytes\n", sz); + fclose(fp); + return 0; + } + if(fread(pixels, 1, xsz * ysz * 3, fp) < sz) { + fprintf(stderr, "partial read: %s\n", fname); + free(pixels); + fclose(fp); + return 0; + } + fclose(fp); + + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, 4, xsz, ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels); + free(pixels); + + return tex; +} diff -r fe1cb1c567cc -r b39d8607f4bb src/tex.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tex.h Wed Sep 07 09:03:51 2011 +0300 @@ -0,0 +1,6 @@ +#ifndef TEX_H_ +#define TEX_H_ + +unsigned int load_texture(const char *fname); + +#endif /* TEX_H_ */