# HG changeset patch # User John Tsiombikas # Date 1315459800 -10800 # Node ID fd39c01989350558de1408dc52e7df47ca8cc5a6 # Parent 862a3329a8f0162fb2c4d0dbec46834474319395 normal mapped tunnel diff -r 862a3329a8f0 -r fd39c0198935 libs/Makefile --- a/libs/Makefile Thu Sep 08 06:28:38 2011 +0300 +++ b/libs/Makefile Thu Sep 08 08:30:00 2011 +0300 @@ -1,6 +1,7 @@ src += $(wildcard libs/zlib/*.c) \ $(wildcard libs/libpng/*.c) \ $(wildcard libs/libjpeg/*.c) \ - $(wildcard libs/imago2/*.c) + $(wildcard libs/imago2/*.c) \ + $(wildcard libs/vmath/*.c) -incdir += -Ilibs/imago2 -Ilibs/zlib -Ilibs/libpng -Ilibs/libjpeg +incdir += -Ilibs/imago2 -Ilibs/zlib -Ilibs/libpng -Ilibs/libjpeg -Ilibs/vmath diff -r 862a3329a8f0 -r fd39c0198935 src/istereo.c --- a/src/istereo.c Thu Sep 08 06:28:38 2011 +0300 +++ b/src/istereo.c Thu Sep 08 08:30:00 2011 +0300 @@ -9,16 +9,19 @@ #include "respath.h" #include "tex.h" #include "cam.h" +#include "vmath.h" #include "config.h" static void render(float t); static void draw_tunnel(float t); -static void tunnel_vertex(float u, float v, float du, float dv, float t); +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t); +static vec3_t calc_text_pos(float sec); +static void draw_text(float idx, vec3_t tpos, float alpha); static void worm(float t, float z, float *tx, float *ty); static unsigned int get_shader_program(const char *vfile, const char *pfile); static float get_sec(void); -unsigned int prog, prog_tunnel, prog_text; +unsigned int prog, prog_simple, prog_tunnel, prog_text; unsigned int tex, tex_stones, tex_normal, tex_text; int view_xsz, view_ysz; @@ -28,11 +31,15 @@ #else int stereo = 0; #endif +int use_bump = 1; /* construction parameters */ int sides = 24; int segm = 20; +float tunnel_speed = 0.75; float ring_height = 0.5; +float text_period = 13.0; +float text_speed = 2.2; float split = 0.53; @@ -41,17 +48,33 @@ 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"); + if(!(prog_simple = get_shader_program("test.v.glsl", "test.p.glsl"))) { + return -1; + } + if(!(prog_tunnel = get_shader_program("tunnel.v.glsl", "tunnel.p.glsl"))) { + return -1; + } + if(!(prog_text = get_shader_program("text.v.glsl", "text.p.glsl"))) { return -1; } if(!(tex = load_texture(find_resource("tiles.jpg", 0, 0)))) { - fprintf(stderr, "failed to load texture\n"); + return -1; + } + if(!(tex_stones = load_texture(find_resource("stonewall.jpg", 0, 0)))) { + return -1; + } + if(!(tex_normal = load_texture(find_resource("stonewall_normal.jpg", 0, 0)))) { + return -1; + } + if(!(tex_text = load_texture(find_resource("text.png", 0, 0)))) { return -1; } - cam_fov(42.0); + glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); + + cam_fov(45.0); return 0; } @@ -69,8 +92,14 @@ z = ring_height * segm; worm(tsec, z, &pan_x, &pan_y); - glClearColor(0.6, 0.6, 0.6, 1.0); - glClear(GL_COLOR_BUFFER_BIT); + if(use_bump) { + glClearColor(0.01, 0.01, 0.01, 1.0); + tunnel_speed = 0.5; + } else { + glClearColor(0.6, 0.6, 0.6, 1.0); + tunnel_speed = 0.75; + } + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(stereo) { int split_pt = (int)((float)view_ysz * split); @@ -117,60 +146,52 @@ render(tsec); } - - /*glClearColor(0.4, 0.6, 1.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - bind_program(prog); - - gl_matrix_mode(GL_MODELVIEW); - gl_load_identity(); - gl_translatef(0, 0, -8); - gl_rotatef(t * 100.0, 0, 0, 1); - - bind_texture(tex, 0); - set_uniform_int(prog, "tex", 0); - - 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(); - - bind_texture(0, 0);*/ - assert(glGetError() == GL_NO_ERROR); } static void render(float t) { + int i; + float text_line; + draw_tunnel(t); + + glDepthMask(0); + text_line = floor((text_speed * t) / text_period); + for(i=0; i<15; i++) { + vec3_t tpos = calc_text_pos(t - (float)i * 0.01); + draw_text(text_line, tpos, 1.5 / (float)i); + } + glDepthMask(1); } static void draw_tunnel(float t) { static const float uoffs[] = {0.0, 0.0, 1.0, 1.0}; static const float voffs[] = {0.0, 1.0, 1.0, 0.0}; - int i, j, k; + int i, j, k, tang_loc = -1; float du, dv; + prog = use_bump ? prog_tunnel : prog_simple; + bind_program(prog); set_uniform_float(prog, "t", t); + set_uniform_int(prog, "tex", 0); + + if(use_bump) { + vec3_t ltpos = calc_text_pos(t); + + set_uniform_int(prog, "tex_norm", 1); + set_uniform_float4(prog, "light_pos", ltpos.x, ltpos.y, ltpos.z, 1.0); + tang_loc = get_attrib_loc(prog, "attr_tangent"); + bind_texture(tex_normal, 1); + } bind_texture(tex, 0); gl_matrix_mode(GL_TEXTURE); gl_load_identity(); - gl_translatef(0, -t * 0.75, 0); + gl_translatef(0, -t * tunnel_speed, 0); gl_begin(GL_QUADS); gl_color3f(1.0, 1.0, 1.0); @@ -197,26 +218,20 @@ float u = (j + uoffs[k]) * du; float v = (i + voffs[k]) * dv; - tunnel_vertex(u, v, du, dv, t); + tunnel_vertex(u, v, du, dv, tang_loc, t); } } } gl_end(); - /*gl_begin(GL_QUADS); - gl_color3f(1.0, 0.3, 0.2); - gl_vertex2f(-100, -100); - gl_vertex2f(100, -100); - gl_vertex2f(100, 0); - gl_vertex2f(-100, 0); - gl_end();*/ - + bind_texture(0, 1); bind_texture(0, 0); } -static void tunnel_vertex(float u, float v, float du, float dv, float t) +static void tunnel_vertex(float u, float v, float du, float dv, int tang_loc, float t) { - float pos[3]; + vec3_t pos, norm; + vec3_t dfdu, dfdv, pos_du, pos_dv; float theta = 2.0 * M_PI * u; float theta1 = 2.0 * M_PI * (u + du); @@ -233,24 +248,65 @@ worm(t, z, trans_z, trans_z + 1); worm(t, z1, trans_z1, trans_z1 + 1); - pos[0] = x + trans_z[0]; - pos[1] = y + trans_z[1]; - pos[2] = z; + pos = v3_cons(x + trans_z[0], y + trans_z[1], z); + pos_du = v3_cons(x1 + trans_z[0], y1 + trans_z[1], z); + pos_dv = v3_cons(x + trans_z1[0], y + trans_z1[1], z1); - /*v3_cons(pos, x + trans_z[0], y + trans_z[1], z); - v3_cons(pos_du, x1 + trans_z[0], y1 + trans_z[1], z); - v3_cons(pos_dv, x + trans_z1[0], y + trans_z1[1], z1);*/ + dfdu = v3_sub(pos_du, pos); + dfdv = v3_sub(pos_dv, pos); + norm = v3_cross(dfdv, dfdu); - /*v3_sub(dfdu, pos_du, pos); - v3_sub(dfdv, pos_dv, pos); - v3_cross(norm, dfdv, dfdu);*/ - - /*glVertexAttrib3f(tloc, dfdu[0], dfdu[1], dfdu[2]); - glNormal3f(norm[0], norm[1], norm[2]);*/ + gl_vertex_attrib3f(tang_loc, dfdu.x, dfdu.y, dfdu.z); + gl_normal3f(norm.x, norm.y, norm.z); gl_texcoord2f(u * 2.0, v * 4.0); - gl_vertex3f(pos[0], pos[1], pos[2]); + gl_vertex3f(pos.x, pos.y, pos.z); } +static vec3_t calc_text_pos(float sec) +{ + float t = text_speed * sec; + float z = fmod(t, text_period); + float pan[2]; + + worm(sec, z, pan, pan + 1); + return v3_cons(pan[0], pan[1], z + ring_height); +} + +static void draw_text(float idx, vec3_t tpos, float alpha) +{ + gl_matrix_mode(GL_MODELVIEW); + gl_push_matrix(); + gl_translatef(tpos.x, tpos.y, tpos.z); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + + bind_program(prog_text); + set_uniform_float(prog, "idx", idx); + + bind_texture(tex_text, 0); + + gl_begin(GL_QUADS); + gl_color4f(1.0, 1.0, 1.0, alpha > 1.0 ? 1.0 : alpha); + + gl_texcoord2f(0, 1); + gl_vertex3f(-1, -0.2, 0); + + gl_texcoord2f(1, 1); + gl_vertex3f(1, -0.2, 0); + + gl_texcoord2f(1, 0); + gl_vertex3f(1, 0.2, 0); + + gl_texcoord2f(0, 0); + gl_vertex3f(-1, 0.2, 0); + gl_end(); + + bind_texture(0, 0); + glDisable(GL_BLEND); + + gl_pop_matrix(); +} static void worm(float t, float z, float *tx, float *ty) @@ -281,14 +337,14 @@ unsigned int prog, vs, ps; if(!(vs = get_vertex_shader(find_resource(vfile, 0, 0)))) { - return -1; + return 0; } if(!(ps = get_pixel_shader(find_resource(pfile, 0, 0)))) { - return -1; + return 0; } if(!(prog = create_program_link(vs, ps))) { - return -1; + return 0; } return prog; } diff -r 862a3329a8f0 -r fd39c0198935 src/respath.c --- a/src/respath.c Thu Sep 08 06:28:38 2011 +0300 +++ b/src/respath.c Thu Sep 08 08:30:00 2011 +0300 @@ -51,6 +51,8 @@ } node = node->next; } + + fprintf(stderr, "can't find resource: %s\n", fname); return 0; } diff -r 862a3329a8f0 -r fd39c0198935 src/sanegl.c --- a/src/sanegl.c Thu Sep 08 06:28:38 2011 +0300 +++ b/src/sanegl.c Thu Sep 08 08:30:00 2011 +0300 @@ -325,7 +325,7 @@ } if(cloc != -1) { - glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr); + glVertexAttribPointer(cloc, 4, GL_FLOAT, 1, 0, col_arr); glEnableVertexAttribArray(cloc); } diff -r 862a3329a8f0 -r fd39c0198935 src/tex.c --- a/src/tex.c Thu Sep 08 06:28:38 2011 +0300 +++ b/src/tex.c Thu Sep 08 08:30:00 2011 +0300 @@ -13,6 +13,9 @@ unsigned int tex; void *pixels; + if(!fname) { + return 0; + } if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) { fprintf(stderr, "failed to load image: %s\n", fname); return 0;