volray
changeset 5:0c3874aa717a
slice
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 04 Apr 2012 01:37:33 +0300 |
parents | 3e53a16d4667 |
children | 04316e9e95dc |
files | src/volray.c volray.p.glsl |
diffstat | 2 files changed, 140 insertions(+), 26 deletions(-) [+] |
line diff
1.1 --- a/src/volray.c Mon Apr 02 17:59:46 2012 +0300 1.2 +++ b/src/volray.c Wed Apr 04 01:37:33 2012 +0300 1.3 @@ -20,9 +20,16 @@ 1.4 struct slice_file *next; 1.5 }; 1.6 1.7 +enum { 1.8 + UIMODE_DEFAULT, 1.9 + UIMODE_XFER, 1.10 + UIMODE_CURSOR 1.11 +}; 1.12 + 1.13 int init(void); 1.14 void disp(void); 1.15 void render_volume(void); 1.16 +void draw_slice(void); 1.17 void draw_xfer_func(void); 1.18 void reshape(int x, int y); 1.19 void keyb(unsigned char key, int x, int y); 1.20 @@ -42,7 +49,7 @@ 1.21 vec2_t tex_scale; 1.22 struct slice_file *flist; 1.23 int nslices; 1.24 -unsigned int sdr, vol_tex, ray_tex; 1.25 +unsigned int vol_sdr, slice_sdr, vol_tex, ray_tex; 1.26 int win_xsz, win_ysz; 1.27 int raytex_needs_recalc = 1; 1.28 1.29 @@ -50,7 +57,8 @@ 1.30 float xfer_mean = 0.5, xfer_sdev = 1.0; 1.31 int xfertex_needs_recalc = 1; 1.32 1.33 -static int uimode_xfer; 1.34 +static int uimode; 1.35 +static float cur_z = 0.5; 1.36 1.37 int main(int argc, char **argv) 1.38 { 1.39 @@ -85,12 +93,18 @@ 1.40 { 1.41 int i, vol_xsz, vol_ysz; 1.42 1.43 - if(!(sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) { 1.44 + if(!(vol_sdr = create_program_load("volray.v.glsl", "volray.p.glsl"))) { 1.45 return -1; 1.46 } 1.47 - set_uniform_int(sdr, "volume", 0); 1.48 - set_uniform_int(sdr, "ray_tex", 1); 1.49 - set_uniform_int(sdr, "xfer_tex", 2); 1.50 + set_uniform_int(vol_sdr, "volume", 0); 1.51 + set_uniform_int(vol_sdr, "ray_tex", 1); 1.52 + set_uniform_int(vol_sdr, "xfer_tex", 2); 1.53 + 1.54 + if(!(slice_sdr = create_program_load(0, "slice.p.glsl"))) { 1.55 + return -1; 1.56 + } 1.57 + set_uniform_int(slice_sdr, "volume", 0); 1.58 + set_uniform_int(slice_sdr, "xfer_tex", 1); 1.59 1.60 glGenTextures(1, &vol_tex); 1.61 glBindTexture(GL_TEXTURE_3D, vol_tex); 1.62 @@ -146,6 +160,7 @@ 1.63 } 1.64 1.65 render_volume(); 1.66 + draw_slice(); 1.67 draw_xfer_func(); 1.68 1.69 glutSwapBuffers(); 1.70 @@ -185,7 +200,7 @@ 1.71 glBindTexture(GL_TEXTURE_1D, xfer_tex); 1.72 glEnable(GL_TEXTURE_1D); 1.73 1.74 - bind_program(sdr); 1.75 + bind_program(vol_sdr); 1.76 glBegin(GL_QUADS); 1.77 glColor3f(1, 1, 1); 1.78 glTexCoord2f(0, 1); glVertex2f(-1, -1); 1.79 @@ -208,6 +223,41 @@ 1.80 glPopMatrix(); 1.81 } 1.82 1.83 +void draw_slice(void) 1.84 +{ 1.85 + glMatrixMode(GL_MODELVIEW); 1.86 + glPushMatrix(); 1.87 + glTranslatef(0.9, 0.9, 0); 1.88 + glScalef(0.3, 0.3 * ((float)win_xsz / win_ysz), 1); 1.89 + glTranslatef(-1, -1, 0); 1.90 + 1.91 + glActiveTexture(GL_TEXTURE0); 1.92 + glBindTexture(GL_TEXTURE_3D, vol_tex); 1.93 + glEnable(GL_TEXTURE_3D); 1.94 + 1.95 + glActiveTexture(GL_TEXTURE1); 1.96 + glBindTexture(GL_TEXTURE_1D, xfer_tex); 1.97 + glEnable(GL_TEXTURE_1D); 1.98 + 1.99 + bind_program(slice_sdr); 1.100 + 1.101 + glBegin(GL_QUADS); 1.102 + glColor3f(1, 1, 1); 1.103 + glTexCoord3f(0, 1, cur_z); glVertex2f(-1, -1); 1.104 + glTexCoord3f(1, 1, cur_z); glVertex2f(1, -1); 1.105 + glTexCoord3f(1, 0, cur_z); glVertex2f(1, 1); 1.106 + glTexCoord3f(0, 0, cur_z); glVertex2f(-1, 1); 1.107 + glEnd(); 1.108 + 1.109 + bind_program(0); 1.110 + 1.111 + glActiveTexture(GL_TEXTURE1); 1.112 + glDisable(GL_TEXTURE_1D); 1.113 + glActiveTexture(GL_TEXTURE0); 1.114 + glDisable(GL_TEXTURE_3D); 1.115 + glPopMatrix(); 1.116 +} 1.117 + 1.118 void draw_xfer_func(void) 1.119 { 1.120 glMatrixMode(GL_MODELVIEW); 1.121 @@ -232,7 +282,11 @@ 1.122 1.123 glLineWidth(2.0); 1.124 glBegin(GL_LINE_LOOP); 1.125 - glColor3f(uimode_xfer ? 1 : 0, 0, uimode_xfer ? 0 : 1); 1.126 + if(uimode == UIMODE_XFER) { 1.127 + glColor3f(1, 0, 0); 1.128 + } else { 1.129 + glColor3f(0, 0, 1); 1.130 + } 1.131 glVertex2f(0, 0); 1.132 glVertex2f(1, 0); 1.133 glVertex2f(1, 1); 1.134 @@ -260,9 +314,17 @@ 1.135 exit(0); 1.136 1.137 case 'x': 1.138 - uimode_xfer = 1; 1.139 + uimode = UIMODE_XFER; 1.140 glutPostRedisplay(); 1.141 break; 1.142 + 1.143 + case 'c': 1.144 + uimode = UIMODE_CURSOR; 1.145 + glutPostRedisplay(); 1.146 + break; 1.147 + 1.148 + default: 1.149 + break; 1.150 } 1.151 } 1.152 1.153 @@ -270,8 +332,20 @@ 1.154 { 1.155 switch(key) { 1.156 case 'x': 1.157 - uimode_xfer = 0; 1.158 - glutPostRedisplay(); 1.159 + if(uimode == UIMODE_XFER) { 1.160 + uimode = UIMODE_DEFAULT; 1.161 + glutPostRedisplay(); 1.162 + } 1.163 + break; 1.164 + 1.165 + case 'c': 1.166 + if(uimode == UIMODE_CURSOR) { 1.167 + uimode = UIMODE_DEFAULT; 1.168 + glutPostRedisplay(); 1.169 + } 1.170 + break; 1.171 + 1.172 + default: 1.173 break; 1.174 } 1.175 } 1.176 @@ -293,7 +367,8 @@ 1.177 prev_x = x; 1.178 prev_y = y; 1.179 1.180 - if(uimode_xfer) { 1.181 + switch(uimode) { 1.182 + case UIMODE_XFER: 1.183 if(dx || dy) { 1.184 xfer_mean += dx / (float)win_xsz; 1.185 xfer_sdev += 0.5 * dy / (float)win_ysz; 1.186 @@ -304,8 +379,20 @@ 1.187 xfertex_needs_recalc = 1; 1.188 glutPostRedisplay(); 1.189 } 1.190 - } else { 1.191 + break; 1.192 1.193 + case UIMODE_CURSOR: 1.194 + cur_z += 0.5 * dy / (float)win_ysz; 1.195 + 1.196 + if(cur_z < 0.0) 1.197 + cur_z = 0.0; 1.198 + if(cur_z > 1.0) 1.199 + cur_z = 1.0; 1.200 + glutPostRedisplay(); 1.201 + break; 1.202 + 1.203 + default: 1.204 + /* view control */ 1.205 if(bnstate[0]) { 1.206 cam_theta += dx * 0.5; 1.207 cam_phi += dy * 0.5;
2.1 --- a/volray.p.glsl Mon Apr 02 17:59:46 2012 +0300 2.2 +++ b/volray.p.glsl Wed Apr 04 01:37:33 2012 +0300 2.3 @@ -21,19 +21,19 @@ 2.4 vec3 ray_march(Ray ray); 2.5 vec3 shade(Ray ray, ISect isect); 2.6 Ray get_primary_ray(); 2.7 -bool intersect_aabb(Ray ray, AABBox aabb, out float t); 2.8 +ISect intersect_aabb(Ray ray, AABBox aabb); 2.9 2.10 void main() 2.11 { 2.12 - const AABBox aabb = AABBox(vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0)); 2.13 + const AABBox aabb = AABBox(vec3(-1.0, -1.0, -1.0), vec3(1.0, 1.0, 1.0)); 2.14 Ray ray = get_primary_ray(); 2.15 2.16 vec3 color = vec3(0.0, 0.0, 0.0); 2.17 2.18 - float start_t; 2.19 - if(intersect_aabb(ray, aabb, start_t)) { 2.20 - ray.origin += ray.dir * start_t; 2.21 - color = vec3(1.0, 0.0, 0.0);//ray_march(ray); 2.22 + ISect res = intersect_aabb(ray, aabb); 2.23 + if(res.hit) { 2.24 + ray.origin += ray.dir * res.t; 2.25 + color = ray_march(ray); 2.26 } 2.27 2.28 gl_FragColor = vec4(color, 1.0); 2.29 @@ -50,7 +50,8 @@ 2.30 2.31 float eval(vec3 pos) 2.32 { 2.33 - return texture1D(xfer_tex, texture3D(volume, pos).x).x; 2.34 + vec3 tc = pos * 0.5 + 0.5; 2.35 + return texture1D(xfer_tex, texture3D(volume, tc).x).x; 2.36 } 2.37 2.38 vec3 ray_march(Ray ray) 2.39 @@ -97,11 +98,16 @@ 2.40 return ray; 2.41 } 2.42 2.43 -bool intersect_aabb(Ray ray, AABBox aabb, out float t) 2.44 +ISect intersect_aabb(Ray ray, AABBox aabb) 2.45 { 2.46 + ISect res; 2.47 + res.hit = false; 2.48 + 2.49 if(ray.origin.x >= aabb.min.x && ray.origin.y >= aabb.min.y && ray.origin.z >= aabb.min.z && 2.50 ray.origin.x < aabb.max.x && ray.origin.y < aabb.max.y && ray.origin.z < aabb.max.z) { 2.51 - return true; 2.52 + res.t = 0.0; 2.53 + res.hit = true; 2.54 + return res; 2.55 } 2.56 2.57 vec4 bbox[2]; 2.58 @@ -119,7 +125,7 @@ 2.59 float tymax = (bbox[1 - ysign].y - ray.origin.y) * invdiry; 2.60 2.61 if(tmin > tymax || tymin > tmax) { 2.62 - return false; 2.63 + return res; 2.64 } 2.65 2.66 if(tymin > tmin) tmin = tymin; 2.67 @@ -131,10 +137,31 @@ 2.68 float tzmax = (bbox[1 - zsign].z - ray.origin.z) * invdirz; 2.69 2.70 if(tmin > tzmax || tzmin > tmax) { 2.71 - return false; 2.72 + return res; 2.73 } 2.74 2.75 - t = tmin; 2.76 - return tmin < 1.0 && tmax > 0.0; 2.77 + res.t = tmin; 2.78 + res.hit = true; 2.79 + res.pos = ray.origin + ray.dir * res.t; 2.80 + 2.81 + /*if(res.pos.x > res.pos.y && res.pos.x > res.pos.z) { 2.82 + res.normal = vec3(1.0, 0.0, 0.0); 2.83 + } else if(res.pos.x < res.pos.y && res.pos.x < res.pos.z) { 2.84 + res.normal = vec3(-1.0, 0.0, 0.0); 2.85 + } else if(res.pos.y > res.pos.x && res.pos.y > res.pos.z) { 2.86 + res.normal = vec3(0.0, 1.0, 0.0); 2.87 + } else if(res.pos.y < res.pos.x && res.pos.y < res.pos.z) { 2.88 + res.normal = vec3(0.0, -1.0, 0.0); 2.89 + } else if(res.pos.z > res.pos.x && res.pos.z > res.pos.y) { 2.90 + res.normal = vec3(0.0, 0.0, 1.0); 2.91 + } else { 2.92 + res.normal = vec3(0.0, 0.9, -1.0); 2.93 + } 2.94 + if(res.pos.x < 0.0) { 2.95 + res.normal = vec3(1.0, 0.0, 0.0); 2.96 + } else { 2.97 + res.normal = vec3(0.0, 0.0, 1.0); 2.98 + }*/ 2.99 + return res; 2.100 } 2.101