# HG changeset patch # User John Tsiombikas # Date 1333321895 -10800 # Node ID 57072295eb83568b84f7f2742a9ddb37b4927110 # Parent b050ce167ff1805a55ba347cfdf80d61b586189d progress diff -r b050ce167ff1 -r 57072295eb83 src/volray.c --- a/src/volray.c Sat Mar 31 02:08:42 2012 +0300 +++ b/src/volray.c Mon Apr 02 02:11:35 2012 +0300 @@ -26,7 +26,7 @@ void motion(int x, int y); int parse_args(int argc, char **argv); -unsigned int create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale); +static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale); static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg); static int round_pow2(int x); @@ -38,6 +38,7 @@ int nslices; unsigned int sdr, vol_tex, ray_tex; int win_xsz, win_ysz; +int raytex_needs_recalc = 1; int main(int argc, char **argv) { @@ -72,16 +73,18 @@ int i, vol_xsz, vol_ysz; if(!(sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) { - return 1; + return -1; } + set_uniform_int(sdr, "volume", 0); + set_uniform_int(sdr, "ray_tex", 1); glGenTextures(1, &vol_tex); glBindTexture(GL_TEXTURE_3D, vol_tex); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP); for(i=0; i 90) cam_phi = 90; + if(cam_phi <= -90) cam_phi = -89; + if(cam_phi >= 90) cam_phi = 89; glutPostRedisplay(); } if(bnstate[1]) { - cam_y += dy * 0.1; + cam_x += dx * 0.025; + cam_y += dy * 0.025; glutPostRedisplay(); } if(bnstate[2]) { - cam_dist += dy * 0.1; + cam_dist += dy * 0.025; if(cam_dist < 0.0) cam_dist = 0.0; glutPostRedisplay(); } @@ -257,49 +263,52 @@ } -unsigned int create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale) +static void create_ray_texture(int xsz, int ysz, float vfov, vec2_t *tex_scale) { int i, j; - unsigned int tex; + int cur_tex_xsz, cur_tex_ysz; int tex_xsz = round_pow2(xsz); int tex_ysz = round_pow2(ysz); float *teximg, *dir; - if(!(teximg = malloc(3 * tex_xsz * tex_ysz * sizeof *teximg))) { - return 0; + if(!(teximg = malloc(3 * xsz * ysz * sizeof *teximg))) { + return; } dir = teximg; - for(i=0; i cur_tex_xsz || tex_ysz > cur_tex_ysz) { + glBindTexture(GL_TEXTURE_2D, ray_tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F_ARB, tex_xsz, tex_ysz, 0, GL_RGB, GL_FLOAT, 0); + } + + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, xsz, ysz, GL_RGB, GL_FLOAT, teximg); free(teximg); if(tex_scale) { tex_scale->x = (float)xsz / (float)tex_xsz; tex_scale->y = (float)ysz / (float)tex_ysz; } - return tex; + raytex_needs_recalc = 0; } static vec3_t get_primary_ray_dir(int x, int y, int w, int h, float vfov_deg) diff -r b050ce167ff1 -r 57072295eb83 volray.p.glsl --- a/volray.p.glsl Sat Mar 31 02:08:42 2012 +0300 +++ b/volray.p.glsl Mon Apr 02 02:11:35 2012 +0300 @@ -12,6 +12,7 @@ vec3 normal; }; +vec3 sky(Ray ray); vec3 ray_march(Ray ray); vec3 shade(Ray ray, ISect isect); Ray get_primary_ray(); @@ -21,30 +22,37 @@ Ray ray = get_primary_ray(); gl_FragColor = vec4(ray_march(ray), 1.0); + //gl_FragColor = vec4(sky(ray), 1.0); } -/*vec3 sky(Ray ray) +vec3 sky(Ray ray) { vec3 col1 = vec3(0.75, 0.78, 0.8); vec3 col2 = vec3(0.56, 0.7, 1.0); float t = max(ray.dir.y, -0.5); return mix(col1, col2, t); -}*/ +} vec3 ray_march(Ray ray) { - const float ray_step = 0.05; + const float ray_step = 0.1; float energy = 1.0; vec3 pos = ray.origin; + float col = 0.0; - // assuming view space - while(pos.z < 1.0) { - energy -= texture3D(volume, pos).x; + for(int i=0; i<40; i++) { + float val = texture3D(volume, pos).x; + val *= energy; + col += val; + energy -= val; + if(energy < 0.001) { + break; + } pos += ray.dir * ray_step; } - return vec3(energy, energy, energy); + return vec3(col, col, col); } vec3 shade(Ray ray, ISect isect)