# HG changeset patch # User John Tsiombikas # Date 1333492653 -10800 # Node ID 0c3874aa717ac7eac7acc30a7915125b0e962107 # Parent 3e53a16d4667f37ff4a71ea808084ef27bc52af5 slice diff -r 3e53a16d4667 -r 0c3874aa717a src/volray.c --- a/src/volray.c Mon Apr 02 17:59:46 2012 +0300 +++ b/src/volray.c Wed Apr 04 01:37:33 2012 +0300 @@ -20,9 +20,16 @@ struct slice_file *next; }; +enum { + UIMODE_DEFAULT, + UIMODE_XFER, + UIMODE_CURSOR +}; + int init(void); void disp(void); void render_volume(void); +void draw_slice(void); void draw_xfer_func(void); void reshape(int x, int y); void keyb(unsigned char key, int x, int y); @@ -42,7 +49,7 @@ vec2_t tex_scale; struct slice_file *flist; int nslices; -unsigned int sdr, vol_tex, ray_tex; +unsigned int vol_sdr, slice_sdr, vol_tex, ray_tex; int win_xsz, win_ysz; int raytex_needs_recalc = 1; @@ -50,7 +57,8 @@ float xfer_mean = 0.5, xfer_sdev = 1.0; int xfertex_needs_recalc = 1; -static int uimode_xfer; +static int uimode; +static float cur_z = 0.5; int main(int argc, char **argv) { @@ -85,12 +93,18 @@ { int i, vol_xsz, vol_ysz; - if(!(sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) { + if(!(vol_sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) { return -1; } - set_uniform_int(sdr, "volume", 0); - set_uniform_int(sdr, "ray_tex", 1); - set_uniform_int(sdr, "xfer_tex", 2); + set_uniform_int(vol_sdr, "volume", 0); + set_uniform_int(vol_sdr, "ray_tex", 1); + set_uniform_int(vol_sdr, "xfer_tex", 2); + + if(!(slice_sdr = create_program_load(0, "slice.p.glsl"))) { + return -1; + } + set_uniform_int(slice_sdr, "volume", 0); + set_uniform_int(slice_sdr, "xfer_tex", 1); glGenTextures(1, &vol_tex); glBindTexture(GL_TEXTURE_3D, vol_tex); @@ -146,6 +160,7 @@ } render_volume(); + draw_slice(); draw_xfer_func(); glutSwapBuffers(); @@ -185,7 +200,7 @@ glBindTexture(GL_TEXTURE_1D, xfer_tex); glEnable(GL_TEXTURE_1D); - bind_program(sdr); + bind_program(vol_sdr); glBegin(GL_QUADS); glColor3f(1, 1, 1); glTexCoord2f(0, 1); glVertex2f(-1, -1); @@ -208,6 +223,41 @@ glPopMatrix(); } +void draw_slice(void) +{ + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(0.9, 0.9, 0); + glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1); + glTranslatef(-1, -1, 0); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_3D, vol_tex); + glEnable(GL_TEXTURE_3D); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_1D, xfer_tex); + glEnable(GL_TEXTURE_1D); + + bind_program(slice_sdr); + + glBegin(GL_QUADS); + glColor3f(1, 1, 1); + glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1); + glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1); + glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1); + glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1); + glEnd(); + + bind_program(0); + + glActiveTexture(GL_TEXTURE1); + glDisable(GL_TEXTURE_1D); + glActiveTexture(GL_TEXTURE0); + glDisable(GL_TEXTURE_3D); + glPopMatrix(); +} + void draw_xfer_func(void) { glMatrixMode(GL_MODELVIEW); @@ -232,7 +282,11 @@ glLineWidth(2.0); glBegin(GL_LINE_LOOP); - glColor3f(uimode_xfer ? 1 : 0, 0, uimode_xfer ? 0 : 1); + if(uimode == UIMODE_XFER) { + glColor3f(1, 0, 0); + } else { + glColor3f(0, 0, 1); + } glVertex2f(0, 0); glVertex2f(1, 0); glVertex2f(1, 1); @@ -260,9 +314,17 @@ exit(0); case 'x': - uimode_xfer = 1; + uimode = UIMODE_XFER; glutPostRedisplay(); break; + + case 'c': + uimode = UIMODE_CURSOR; + glutPostRedisplay(); + break; + + default: + break; } } @@ -270,8 +332,20 @@ { switch(key) { case 'x': - uimode_xfer = 0; - glutPostRedisplay(); + if(uimode == UIMODE_XFER) { + uimode = UIMODE_DEFAULT; + glutPostRedisplay(); + } + break; + + case 'c': + if(uimode == UIMODE_CURSOR) { + uimode = UIMODE_DEFAULT; + glutPostRedisplay(); + } + break; + + default: break; } } @@ -293,7 +367,8 @@ prev_x = x; prev_y = y; - if(uimode_xfer) { + switch(uimode) { + case UIMODE_XFER: if(dx || dy) { xfer_mean += dx / (float)win_xsz; xfer_sdev += 0.5 * dy / (float)win_ysz; @@ -304,8 +379,20 @@ xfertex_needs_recalc = 1; glutPostRedisplay(); } - } else { + break; + case UIMODE_CURSOR: + cur_z += 0.5 * dy / (float)win_ysz; + + if(cur_z < 0.0) + cur_z = 0.0; + if(cur_z > 1.0) + cur_z = 1.0; + glutPostRedisplay(); + break; + + default: + /* view control */ if(bnstate[0]) { cam_theta += dx * 0.5; cam_phi += dy * 0.5; diff -r 3e53a16d4667 -r 0c3874aa717a volray.p.glsl --- a/volray.p.glsl Mon Apr 02 17:59:46 2012 +0300 +++ b/volray.p.glsl Wed Apr 04 01:37:33 2012 +0300 @@ -21,19 +21,19 @@ vec3 ray_march(Ray ray); vec3 shade(Ray ray, ISect isect); Ray get_primary_ray(); -bool intersect_aabb(Ray ray, AABBox aabb, out float t); +ISect intersect_aabb(Ray ray, AABBox aabb); void main() { - const AABBox aabb = AABBox(vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0)); + const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0)); Ray ray = get_primary_ray(); vec3 color = vec3(0.0, 0.0, 0.0); - float start_t; - if(intersect_aabb(ray, aabb, start_t)) { - ray.origin += ray.dir * start_t; - color = vec3(1.0, 0.0, 0.0);//ray_march(ray); + ISect res = intersect_aabb(ray, aabb); + if(res.hit) { + ray.origin += ray.dir * res.t; + color = ray_march(ray); } gl_FragColor = vec4(color, 1.0); @@ -50,7 +50,8 @@ float eval(vec3 pos) { - return texture1D(xfer_tex, texture3D(volume, pos).x).x; + vec3 tc = pos * 0.5 + 0.5; + return texture1D(xfer_tex, texture3D(volume, tc).x).x; } vec3 ray_march(Ray ray) @@ -97,11 +98,16 @@ return ray; } -bool intersect_aabb(Ray ray, AABBox aabb, out float t) +ISect intersect_aabb(Ray ray, AABBox aabb) { + ISect res; + res.hit = false; + if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z && ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) { - return true; + res.t = 0.0; + res.hit = true; + return res; } vec4 bbox[2]; @@ -119,7 +125,7 @@ float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry; if(tmin > tymax || tymin > tmax) { - return false; + return res; } if(tymin > tmin) tmin = tymin; @@ -131,10 +137,31 @@ float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz; if(tmin > tzmax || tzmin > tmax) { - return false; + return res; } - t = tmin; - return tmin < 1.0 && tmax > 0.0; + res.t = tmin; + res.hit = true; + res.pos = ray.origin + ray.dir * res.t; + + /*if(res.pos.x > res.pos.y && res.pos.x > res.pos.z) { + res.normal = vec3(1.0, 0.0, 0.0); + } else if(res.pos.x < res.pos.y && res.pos.x < res.pos.z) { + res.normal = vec3(-1.0, 0.0, 0.0); + } else if(res.pos.y > res.pos.x && res.pos.y > res.pos.z) { + res.normal = vec3(0.0, 1.0, 0.0); + } else if(res.pos.y < res.pos.x && res.pos.y < res.pos.z) { + res.normal = vec3(0.0, -1.0, 0.0); + } else if(res.pos.z > res.pos.x && res.pos.z > res.pos.y) { + res.normal = vec3(0.0, 0.0, 1.0); + } else { + res.normal = vec3(0.0, 0.9, -1.0); + } + if(res.pos.x < 0.0) { + res.normal = vec3(1.0, 0.0, 0.0); + } else { + res.normal = vec3(0.0, 0.0, 1.0); + }*/ + return res; }