glviewvol

view src/dicomview.cc @ 8:fb6d93471352

main thing done
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 30 Dec 2014 20:03:32 +0200
parents f22be47a3572
children 89efc666105c
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "opengl.h"
4 #include "dicomview.h"
5 #include "rend_fast.h"
6 #include "opt.h"
7 #include "volume.h"
8 #include "xfer_view.h"
10 static int win_width, win_height;
11 static float cam_theta, cam_phi, cam_dist = 4;
12 static float pre_rot = -90;
13 static int splitter_y = -1;
15 #define SPLITTER_WIDTH 5
17 static Renderer *rend;
18 static Volume *vol;
19 static TransferFunc *xfer;
21 extern "C" {
23 int init()
24 {
25 if(!opt.fname) {
26 fprintf(stderr, "you must specify the volume data filename\n");
27 return -1;
28 }
30 switch(opt.rend_type) {
31 case REND_FAST:
32 rend = new RendererFast;
33 break;
34 default:
35 return -1;
36 }
38 if(!rend->init()) {
39 fprintf(stderr, "renderer initialization failed\n");
40 return -1;
41 }
43 VoxelVolume *voxvol = new VoxelVolume;
44 if(!voxvol->load(opt.fname)) {
45 fprintf(stderr, "failed to load volume data from: %s\n", opt.fname);
46 return -1;
47 }
48 vol = voxvol;
49 rend->set_volume(vol);
51 xfer = new TransferWindow;
52 rend->set_transfer_function(xfer);
54 if(!xfview_init(xfer)) {
55 return -1;
56 }
58 return 0;
59 }
61 void cleanup()
62 {
63 xfview_destroy();
65 rend->destroy();
66 delete rend;
67 delete vol;
68 delete xfer;
69 }
71 void ev_display()
72 {
73 glClear(GL_COLOR_BUFFER_BIT);
75 // render the main view
76 glViewport(0, win_height - splitter_y, win_width, splitter_y);
78 glMatrixMode(GL_PROJECTION);
79 glLoadIdentity();
80 gluPerspective(50.0, (float)win_width / (float)splitter_y, 0.1, 100.0);
82 glMatrixMode(GL_MODELVIEW);
83 glLoadIdentity();
84 glTranslatef(0, 0, -cam_dist);
85 glRotatef(cam_phi + pre_rot, 1, 0, 0);
86 glRotatef(cam_theta, 0, 1, 0);
88 rend->update(0);
89 rend->render();
91 // draw the transfer function view
92 glViewport(0, 0, win_width, win_height - splitter_y);
94 xfview_draw();
96 // draw the GUI
97 glViewport(0, 0, win_width, win_height);
99 glMatrixMode(GL_PROJECTION);
100 glLoadIdentity();
101 glOrtho(0, win_width, win_height, 0, -1, 1);
103 glMatrixMode(GL_MODELVIEW);
104 glLoadIdentity();
106 glBegin(GL_QUADS);
107 glColor3f(1, 1, 1);
108 glVertex2f(0, splitter_y + SPLITTER_WIDTH / 2);
109 glVertex2f(win_width, splitter_y + SPLITTER_WIDTH / 2);
110 glVertex2f(win_width, splitter_y - SPLITTER_WIDTH / 2);
111 glVertex2f(0, splitter_y - SPLITTER_WIDTH / 2);
112 glEnd();
114 swap_buffers();
115 }
117 void ev_reshape(int x, int y)
118 {
119 if(splitter_y < 0) { // not initialized yet
120 splitter_y = (int)(y * 0.85);
121 } else {
122 // calculate where the splitter was relative to the window height
123 // and based on that, it's new position
124 float split = (float)splitter_y / (float)win_height;
125 splitter_y = (int)(y * split);
126 }
128 win_width = x;
129 win_height = y;
131 glViewport(0, 0, x, y);
132 if(rend) {
133 rend->reshape(x, y);
134 }
135 }
137 void ev_keyboard(int key, int press, int x, int y)
138 {
139 RendererFast *fr;
141 if(press) {
142 switch(key) {
143 case 27:
144 quit();
146 case '=':
147 if((fr = dynamic_cast<RendererFast*>(rend))) {
148 int n = fr->get_proxy_count();
149 int add = n / 4;
150 n += add < 1 ? 1 : add;
151 printf("proxy count: %d\n", n);
152 fr->set_proxy_count(n);
153 }
154 redisplay();
155 break;
157 case '-':
158 if((fr = dynamic_cast<RendererFast*>(rend))) {
159 int n = fr->get_proxy_count();
160 int sub = n / 4;
161 n -= sub < 1 ? 1 : sub;
163 if(n < 1) n = 1;
165 printf("proxy count: %d\n", n);
166 fr->set_proxy_count(n);
167 }
168 redisplay();
169 break;
171 default:
172 break;
173 }
174 }
175 }
177 static bool bnstate[8];
178 static int prev_x, prev_y;
180 #define ON_SPLITTER(y) (abs(y - splitter_y) <= SPLITTER_WIDTH / 2)
181 static bool splitter_dragging;
183 void ev_mouse_button(int bn, int press, int x, int y)
184 {
185 bnstate[bn] = press != 0;
186 prev_x = x;
187 prev_y = y;
189 splitter_dragging = bn == 0 && press && ON_SPLITTER(y);
191 if(!splitter_dragging && y > splitter_y) {
192 xfview_button(bn, press, x, y);
193 }
194 }
196 void ev_mouse_motion(int x, int y)
197 {
198 int dx = x - prev_x;
199 int dy = y - prev_y;
200 prev_x = x;
201 prev_y = y;
203 if((dx | dy) == 0) return;
205 if(splitter_dragging) {
206 splitter_y += dy;
207 redisplay();
208 return;
209 }
211 if(y > splitter_y) {
212 xfview_motion(x, y);
213 return;
214 }
216 // main view motion handling
217 if(bnstate[0]) {
218 cam_theta += dx * 0.5;
219 cam_phi += dy * 0.5;
221 if(cam_phi < -90) cam_phi = -90;
222 if(cam_phi > 90) cam_phi = 90;
223 redisplay();
224 }
225 if(bnstate[2]) {
226 cam_dist += dy * 0.1;
228 if(cam_dist < 0.0) cam_dist = 0.0;
229 redisplay();
230 }
231 }
233 } // extern "C"