glviewvol

view src/dicomview.cc @ 5:5417c25cb238

moving to a simpler transfer function
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 29 Dec 2014 15:59:55 +0200
parents 32c4a7160350
children f22be47a3572
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 = 6;
12 static int splitter_y = -1;
14 #define SPLITTER_WIDTH 5
16 static Renderer *rend;
17 static Volume *vol;
19 extern "C" {
21 int init()
22 {
23 if(!opt.fname) {
24 fprintf(stderr, "you must specify the volume data filename\n");
25 return -1;
26 }
28 switch(opt.rend_type) {
29 case REND_FAST:
30 rend = new RendererFast;
31 break;
32 default:
33 return -1;
34 }
36 if(!rend->init()) {
37 fprintf(stderr, "renderer initialization failed\n");
38 return -1;
39 }
41 VoxelVolume *voxvol = new VoxelVolume;
42 if(!voxvol->load(opt.fname)) {
43 fprintf(stderr, "failed to load volume data from: %s\n", opt.fname);
44 return -1;
45 }
46 vol = voxvol;
47 rend->set_volume(vol);
49 if(!xfview_init(rend)) {
50 return -1;
51 }
53 return 0;
54 }
56 void cleanup()
57 {
58 xfview_destroy();
60 rend->destroy();
61 delete rend;
62 delete vol;
63 }
65 void ev_display()
66 {
67 glClear(GL_COLOR_BUFFER_BIT);
69 // render the main view
70 glViewport(0, win_height - splitter_y, win_width, splitter_y);
72 glMatrixMode(GL_PROJECTION);
73 glLoadIdentity();
74 gluPerspective(50.0, (float)win_width / (float)splitter_y, 0.1, 100.0);
76 glMatrixMode(GL_MODELVIEW);
77 glLoadIdentity();
78 glTranslatef(0, 0, -cam_dist);
79 glRotatef(cam_phi, 1, 0, 0);
80 glRotatef(cam_theta, 0, 1, 0);
82 rend->update(0);
83 rend->render();
85 // draw the transfer function view
86 glViewport(0, 0, win_width, win_height - splitter_y);
88 xfview_draw();
90 // draw the GUI
91 glViewport(0, 0, win_width, win_height);
93 glMatrixMode(GL_PROJECTION);
94 glLoadIdentity();
95 glOrtho(0, win_width, win_height, 0, -1, 1);
97 glMatrixMode(GL_MODELVIEW);
98 glLoadIdentity();
100 glBegin(GL_QUADS);
101 glColor3f(1, 1, 1);
102 glVertex2f(0, splitter_y + SPLITTER_WIDTH / 2);
103 glVertex2f(win_width, splitter_y + SPLITTER_WIDTH / 2);
104 glVertex2f(win_width, splitter_y - SPLITTER_WIDTH / 2);
105 glVertex2f(0, splitter_y - SPLITTER_WIDTH / 2);
106 glEnd();
108 swap_buffers();
109 }
111 void ev_reshape(int x, int y)
112 {
113 if(splitter_y < 0) { // not initialized yet
114 splitter_y = (int)(y * 0.85);
115 } else {
116 // calculate where the splitter was relative to the window height
117 // and based on that, it's new position
118 float split = (float)splitter_y / (float)win_height;
119 splitter_y = (int)(y * split);
120 }
122 win_width = x;
123 win_height = y;
125 glViewport(0, 0, x, y);
126 if(rend) {
127 rend->reshape(x, y);
128 }
129 }
131 void ev_keyboard(int key, int press, int x, int y)
132 {
133 if(press) {
134 switch(key) {
135 case 27:
136 quit();
137 }
138 }
139 }
141 static bool bnstate[8];
142 static int prev_x, prev_y;
144 #define ON_SPLITTER(y) (abs(y - splitter_y) <= SPLITTER_WIDTH / 2)
145 static bool splitter_dragging;
147 void ev_mouse_button(int bn, int press, int x, int y)
148 {
149 bnstate[bn] = press != 0;
150 prev_x = x;
151 prev_y = y;
153 splitter_dragging = bn == 0 && press && ON_SPLITTER(y);
155 if(!splitter_dragging && y > splitter_y) {
156 xfview_button(bn, press, x, y);
157 }
158 }
160 void ev_mouse_motion(int x, int y)
161 {
162 int dx = x - prev_x;
163 int dy = y - prev_y;
164 prev_x = x;
165 prev_y = y;
167 if((dx | dy) == 0) return;
169 if(splitter_dragging) {
170 splitter_y += dy;
171 redisplay();
172 return;
173 }
175 if(y > splitter_y) {
176 xfview_motion(x, y);
177 return;
178 }
180 // main view motion handling
181 if(bnstate[0]) {
182 cam_theta += dx * 0.5;
183 cam_phi += dy * 0.5;
185 if(cam_phi < -90) cam_phi = -90;
186 if(cam_phi > 90) cam_phi = 90;
187 redisplay();
188 }
189 if(bnstate[2]) {
190 cam_dist += dy * 0.1;
192 if(cam_dist < 0.0) cam_dist = 0.0;
193 redisplay();
194 }
195 }
197 } // extern "C"