glviewvol
view src/dicomview.cc @ 6:f22be47a3572
moved to TransferFuncs completely
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 30 Dec 2014 06:22:54 +0200 |
parents | 04330eb80b36 |
children | fb6d93471352 |
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;
18 static TransferFunc *xfer;
20 extern "C" {
22 int init()
23 {
24 if(!opt.fname) {
25 fprintf(stderr, "you must specify the volume data filename\n");
26 return -1;
27 }
29 switch(opt.rend_type) {
30 case REND_FAST:
31 rend = new RendererFast;
32 break;
33 default:
34 return -1;
35 }
37 if(!rend->init()) {
38 fprintf(stderr, "renderer initialization failed\n");
39 return -1;
40 }
42 VoxelVolume *voxvol = new VoxelVolume;
43 if(!voxvol->load(opt.fname)) {
44 fprintf(stderr, "failed to load volume data from: %s\n", opt.fname);
45 return -1;
46 }
47 vol = voxvol;
48 rend->set_volume(vol);
50 xfer = new TransferWindow;
51 rend->set_transfer_function(xfer);
53 if(!xfview_init(xfer)) {
54 return -1;
55 }
57 return 0;
58 }
60 void cleanup()
61 {
62 xfview_destroy();
64 rend->destroy();
65 delete rend;
66 delete vol;
67 delete xfer;
68 }
70 void ev_display()
71 {
72 glClear(GL_COLOR_BUFFER_BIT);
74 // render the main view
75 glViewport(0, win_height - splitter_y, win_width, splitter_y);
77 glMatrixMode(GL_PROJECTION);
78 glLoadIdentity();
79 gluPerspective(50.0, (float)win_width / (float)splitter_y, 0.1, 100.0);
81 glMatrixMode(GL_MODELVIEW);
82 glLoadIdentity();
83 glTranslatef(0, 0, -cam_dist);
84 glRotatef(cam_phi, 1, 0, 0);
85 glRotatef(cam_theta, 0, 1, 0);
87 rend->update(0);
88 rend->render();
90 // draw the transfer function view
91 glViewport(0, 0, win_width, win_height - splitter_y);
93 xfview_draw();
95 // draw the GUI
96 glViewport(0, 0, win_width, win_height);
98 glMatrixMode(GL_PROJECTION);
99 glLoadIdentity();
100 glOrtho(0, win_width, win_height, 0, -1, 1);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
105 glBegin(GL_QUADS);
106 glColor3f(1, 1, 1);
107 glVertex2f(0, splitter_y + SPLITTER_WIDTH / 2);
108 glVertex2f(win_width, splitter_y + SPLITTER_WIDTH / 2);
109 glVertex2f(win_width, splitter_y - SPLITTER_WIDTH / 2);
110 glVertex2f(0, splitter_y - SPLITTER_WIDTH / 2);
111 glEnd();
113 swap_buffers();
114 }
116 void ev_reshape(int x, int y)
117 {
118 if(splitter_y < 0) { // not initialized yet
119 splitter_y = (int)(y * 0.85);
120 } else {
121 // calculate where the splitter was relative to the window height
122 // and based on that, it's new position
123 float split = (float)splitter_y / (float)win_height;
124 splitter_y = (int)(y * split);
125 }
127 win_width = x;
128 win_height = y;
130 glViewport(0, 0, x, y);
131 if(rend) {
132 rend->reshape(x, y);
133 }
134 }
136 void ev_keyboard(int key, int press, int x, int y)
137 {
138 if(press) {
139 switch(key) {
140 case 27:
141 quit();
142 }
143 }
144 }
146 static bool bnstate[8];
147 static int prev_x, prev_y;
149 #define ON_SPLITTER(y) (abs(y - splitter_y) <= SPLITTER_WIDTH / 2)
150 static bool splitter_dragging;
152 void ev_mouse_button(int bn, int press, int x, int y)
153 {
154 bnstate[bn] = press != 0;
155 prev_x = x;
156 prev_y = y;
158 splitter_dragging = bn == 0 && press && ON_SPLITTER(y);
160 if(!splitter_dragging && y > splitter_y) {
161 xfview_button(bn, press, x, y);
162 }
163 }
165 void ev_mouse_motion(int x, int y)
166 {
167 int dx = x - prev_x;
168 int dy = y - prev_y;
169 prev_x = x;
170 prev_y = y;
172 if((dx | dy) == 0) return;
174 if(splitter_dragging) {
175 splitter_y += dy;
176 redisplay();
177 return;
178 }
180 if(y > splitter_y) {
181 xfview_motion(x, y);
182 return;
183 }
185 // main view motion handling
186 if(bnstate[0]) {
187 cam_theta += dx * 0.5;
188 cam_phi += dy * 0.5;
190 if(cam_phi < -90) cam_phi = -90;
191 if(cam_phi > 90) cam_phi = 90;
192 redisplay();
193 }
194 if(bnstate[2]) {
195 cam_dist += dy * 0.1;
197 if(cam_dist < 0.0) cam_dist = 0.0;
198 redisplay();
199 }
200 }
202 } // extern "C"