qvolray
diff src/volray.c @ 5:0c3874aa717a
slice
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 04 Apr 2012 01:37:33 +0300 |
parents | 3e53a16d4667 |
children | f40e4edfee7e |
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;