glviewvol

view src/viewer.cc @ 15:2a67ea257ac0

makefile fixes for macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 06 Feb 2015 23:47:28 +0200
parents 773f89037a35
children
line source
1 /*
2 glviewvol is an OpenGL 3D volume data viewer
3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "opengl.h"
21 #include "viewer.h"
22 #include "rend_fast.h"
23 #include "opt.h"
24 #include "volume.h"
25 #include "xfer_view.h"
27 static int win_width, win_height;
28 static float cam_theta, cam_phi, cam_dist = 4;
29 static float pre_rot = -90;
30 static int splitter_y = -1;
32 #define SPLITTER_WIDTH 5
34 static Renderer *rend;
35 static Volume *vol;
36 static TransferFunc *xfer;
38 extern "C" {
40 int init()
41 {
42 if(!opt.fname) {
43 fprintf(stderr, "you must specify the volume data filename\n");
44 return -1;
45 }
47 switch(opt.rend_type) {
48 case REND_FAST:
49 rend = new RendererFast;
50 break;
51 default:
52 return -1;
53 }
55 if(!rend->init()) {
56 fprintf(stderr, "renderer initialization failed\n");
57 return -1;
58 }
60 VoxelVolume *voxvol = new VoxelVolume;
61 if(!voxvol->load(opt.fname)) {
62 fprintf(stderr, "failed to load volume data from: %s\n", opt.fname);
63 return -1;
64 }
65 vol = voxvol;
66 rend->set_volume(vol);
68 xfer = new TransferWindow;
69 rend->set_transfer_function(xfer);
71 if(!xfview_init(xfer)) {
72 return -1;
73 }
74 xfview_set_volume(vol);
76 return 0;
77 }
79 void cleanup()
80 {
81 xfview_destroy();
83 rend->destroy();
84 delete rend;
85 delete vol;
86 delete xfer;
87 }
89 void ev_display()
90 {
91 glClear(GL_COLOR_BUFFER_BIT);
93 // render the main view
94 glViewport(0, win_height - splitter_y, win_width, splitter_y);
96 glMatrixMode(GL_PROJECTION);
97 glLoadIdentity();
98 gluPerspective(50.0, (float)win_width / (float)splitter_y, 0.1, 100.0);
100 glMatrixMode(GL_MODELVIEW);
101 glLoadIdentity();
102 glTranslatef(0, 0, -cam_dist);
103 glRotatef(cam_phi + pre_rot, 1, 0, 0);
104 glRotatef(cam_theta, 0, 1, 0);
106 rend->update(0);
107 rend->render();
109 // draw the transfer function view
110 glViewport(0, 0, win_width, win_height - splitter_y);
112 xfview_draw();
114 // draw the GUI
115 glViewport(0, 0, win_width, win_height);
117 glMatrixMode(GL_PROJECTION);
118 glLoadIdentity();
119 glOrtho(0, win_width, win_height, 0, -1, 1);
121 glMatrixMode(GL_MODELVIEW);
122 glLoadIdentity();
124 glBegin(GL_QUADS);
125 glColor3f(1, 1, 1);
126 glVertex2f(0, splitter_y + SPLITTER_WIDTH / 2);
127 glVertex2f(win_width, splitter_y + SPLITTER_WIDTH / 2);
128 glVertex2f(win_width, splitter_y - SPLITTER_WIDTH / 2);
129 glVertex2f(0, splitter_y - SPLITTER_WIDTH / 2);
130 glEnd();
132 swap_buffers();
133 }
135 void ev_reshape(int x, int y)
136 {
137 if(splitter_y < 0) { // not initialized yet
138 splitter_y = (int)(y * 0.85);
139 } else {
140 // calculate where the splitter was relative to the window height
141 // and based on that, it's new position
142 float split = (float)splitter_y / (float)win_height;
143 splitter_y = (int)(y * split);
144 }
146 win_width = x;
147 win_height = y;
149 glViewport(0, 0, x, y);
150 if(rend) {
151 rend->reshape(x, y);
152 }
153 }
155 static bool zscaling;
157 void ev_keyboard(int key, int press, int x, int y)
158 {
159 RendererFast *fr;
161 switch(key) {
162 case 27:
163 if(press) {
164 quit();
165 }
166 break;
168 case 'z':
169 case 'Z':
170 zscaling = press;
171 break;
173 case '=':
174 if(press && (fr = dynamic_cast<RendererFast*>(rend))) {
175 int n = fr->get_proxy_count();
176 int add = n / 4;
177 n += add < 1 ? 1 : add;
178 printf("proxy count: %d\n", n);
179 fr->set_proxy_count(n);
180 redisplay();
181 }
182 break;
184 case '-':
185 if(press && (fr = dynamic_cast<RendererFast*>(rend))) {
186 int n = fr->get_proxy_count();
187 int sub = n / 4;
188 n -= sub < 1 ? 1 : sub;
190 if(n < 1) n = 1;
192 printf("proxy count: %d\n", n);
193 fr->set_proxy_count(n);
194 redisplay();
195 }
196 break;
198 default:
199 break;
200 }
201 }
203 static bool bnstate[8];
204 static int prev_x, prev_y;
206 #define ON_SPLITTER(y) (abs(y - splitter_y) <= SPLITTER_WIDTH / 2)
207 static bool splitter_dragging;
209 void ev_mouse_button(int bn, int press, int x, int y)
210 {
211 bnstate[bn] = press != 0;
212 prev_x = x;
213 prev_y = y;
215 splitter_dragging = bn == 0 && press && ON_SPLITTER(y);
217 if(!splitter_dragging && y > splitter_y) {
218 xfview_button(bn, press, x, y);
219 }
220 }
222 void ev_mouse_motion(int x, int y)
223 {
224 int dx = x - prev_x;
225 int dy = y - prev_y;
226 prev_x = x;
227 prev_y = y;
229 if((dx | dy) == 0) return;
231 if(bnstate[0] && zscaling) {
232 float s = rend->get_zscale() + (float)dy / (float)win_height;
233 rend->set_zscale(s < 0.0 ? 0.0 : s);
234 redisplay();
235 return;
236 }
238 if(splitter_dragging) {
239 splitter_y += dy;
240 redisplay();
241 return;
242 }
244 if(y > splitter_y) {
245 xfview_motion(x, y);
246 return;
247 }
249 // main view motion handling
250 if(bnstate[0]) {
251 cam_theta += dx * 0.5;
252 cam_phi += dy * 0.5;
254 if(cam_phi < -90) cam_phi = -90;
255 if(cam_phi > 90) cam_phi = 90;
256 redisplay();
257 }
258 if(bnstate[2]) {
259 cam_dist += dy * 0.1;
261 if(cam_dist < 0.0) cam_dist = 0.0;
262 redisplay();
263 }
264 }
266 } // extern "C"