source: trunk/src/testing/app/volvis/vUI.cpp @ 4

Revision 4, 12.1 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/********************************************************************************
2 * Volatile - Volume Visualization Software for SAGE
3 * Copyright (C) 2004 Electronic Visualization Laboratory,
4 * University of Illinois at Chicago
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 *  * Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *  * Redistributions in binary form must reproduce the above
14 *    copyright notice, this list of conditions and the following disclaimer
15 *    in the documentation and/or other materials provided with the distribution.
16 *  * Neither the name of the University of Illinois at Chicago nor
17 *    the names of its contributors may be used to endorse or promote
18 *    products derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Direct questions, comments etc about Volatile to www.evl.uic.edu/cavern/forum
33 *********************************************************************************/
34
35//functions to handle UI interaction - if current
36//command is not flushed user cant do anything anymore
37#include "vUI.h"
38#include "global.h"
39#if defined(__APPLE__)
40#include <GLUT/glut.h>
41#else
42#include <GL/glut.h>
43#endif
44#include "MatrixMath.h"
45#include <stdlib.h>
46#include "vGeometry.h"
47#include "vPrimitive.h"
48
49extern vector<vPrimitive*>   renderList;
50
51void mouseToWorld(int x, int y, float world[3]) {
52        float dx = (float)x/(float)global.win.width - 0.5;
53        float dy = (float)y/(float)global.win.height - 0.5;
54        world[0] = dx *(global.env.frustumList[0].right-global.env.frustumList[0].left) * (global.env.eye[2]-world[2])/global.env.clip[0];
55        world[1] = dy *(global.env.frustumList[0].top-global.env.frustumList[0].bottom) * (global.env.eye[2]-world[2])/global.env.clip[0];
56}
57
58void startRotation(int x, int y) {
59        glutSetWindowTitle("Rotate");
60        if (global.ui.cutEnabled)
61                cutTrack.start((2.0 * x - global.win.width) / global.win.width,(2.0 * y - global.win.height) / global.win.height);
62        else {
63                renderList[global.curPrimIdx]->track.start((2.0 * x - global.win.width) / global.win.width,(2.0 * y - global.win.height) / global.win.height);
64        }
65        global.mouse.pos[0] = global.mouse.last[0] = x;
66        global.mouse.pos[1] = global.mouse.last[1] = y;
67        global.volren.lowRes = true;
68#ifdef V_DISTRIBUTED
69        net->setLowRes(true);
70#endif
71}
72
73//when user is rotating typically mouse drag, call this
74void doRotation(int x, int y) {
75        if (global.ui.cutEnabled) {
76                global.cut.update = true;
77                cutTrack.update((2.0 * x - global.win.width) /global.win.width,(2.0 * y - global.win.height) /global.win.height);
78                cutTrack.getAxisAngle(global.cut.angle,global.cut.axis);
79#if defined(V_DISTRIBUTED)
80                net->setCutRotation(global.cut.angle, global.cut.axis);
81#endif
82        }
83        else {
84                renderList[global.curPrimIdx]->track.update((2.0 * x - global.win.width) / global.win.width,(2.0 * y - global.win.height) / global.win.height);
85                renderList[global.curPrimIdx]->track.buildRotMatrix((float (*)[4])renderList[global.curPrimIdx]->xform.rotn);
86#if defined(V_DISTRIBUTED)
87                net->setRotn(renderList[global.curPrimIdx]->xform.rotn);
88#endif
89        }
90        global.mouse.last[0] = x;
91        global.mouse.last[1] = y;
92
93}
94
95//during a button relase
96void endInteract() {
97        glutSetWindowTitle("Vol-a-Tile");
98        global.volren.lowRes = false;
99#ifdef V_DISTRIBUTED
100        net->setLowRes(false);
101#endif
102}
103
104void startZoom(int x, int y) {
105        glutSetWindowTitle("Zoom");
106        global.mouse.pos[0] = global.mouse.last[0] = x;
107        global.mouse.pos[1] = global.mouse.last[1] = y;
108        global.volren.lowRes = true;
109#ifdef V_DISTRIBUTED
110        net->setLowRes(true);
111#endif
112}
113
114void doZoom(int x, int y) {
115        float dx = (x - global.mouse.last[0])/(float)global.win.width;
116        float dy = (y - global.mouse.last[1])/(float)global.win.height;
117        float change;
118        if (fabs(dy) > fabs(dx))
119                change = dy;
120        else
121                change = dx;
122        if (global.ui.cutEnabled) {
123                global.cut.user[3]  += change; //between -1 and 1
124#if defined(V_DISTRIBUTED)
125                net->setCutZoom(change);
126#endif
127        }
128        else {
129//              global.env.eye[2] += dx;
130                renderList[global.curPrimIdx]->xform.scale += change;
131#if defined(V_DISTRIBUTED)
132                net->setZoom(change);
133#endif
134        }
135        global.mouse.last[0] = x;
136        global.mouse.last[1] = y;
137}
138
139//TRANSLATE
140
141//on a mouse-down call this
142void startTranslate(int x, int y) {
143        glutSetWindowTitle("Translate");
144        global.mouse.pos[0] = global.mouse.last[0] = x;
145        global.mouse.pos[1] = global.mouse.last[1] = y;
146        global.volren.lowRes = true;
147#ifdef V_DISTRIBUTED
148        net->setLowRes(true);
149#endif
150}
151
152//when user is translating
153void doTranslate(int x, int y) {
154        float dx = (x - global.mouse.last[0])/(float)global.win.width;
155        float dy = (y - global.mouse.last[1])/(float)global.win.height;
156        renderList[global.curPrimIdx]->xform.trans[0] += dx *(global.env.frustumList[0].right-global.env.frustumList[0].left) * global.env.eye[2]/global.env.clip[0];
157        renderList[global.curPrimIdx]->xform.trans[1] += dy *(global.env.frustumList[0].top-global.env.frustumList[0].bottom) * global.env.eye[2]/global.env.clip[0];
158#if defined(V_DISTRIBUTED)
159        net->setTranslation(renderList[global.curPrimIdx]->xform.trans);
160#endif
161        global.mouse.last[0] = x;
162        global.mouse.last[1] = y;
163
164}
165
166void printUsage() {
167        printf("VolViz\n");
168        printf("\t'c'     - Switch between moving clip plane and object\n");
169        printf("\t'f'     - Toggle printing FPS\n");
170        printf("\t'g'     - Switch between gray and psuedo color\n");
171        printf("\t'h'     - Print usage help\n");
172        printf("\t'o'     - Write the volume to file\n");
173        printf("\t'n'     - Toggle printing number of slices\n");
174        printf("\t'+'     - Increase number of slices\n");
175        printf("\t'-'     - Decrease number of slices\n");
176        printf("\t<PAGE UP>  - Increase Eye Separation\n");
177        printf("\t<PAGE DOWN> - Decrease Eye Separation\n");
178        printf("\t<ESC>   - Quit\n");
179}
180
181void togglePrintFPS() {
182        global.ui.printFPS = !global.ui.printFPS;
183        fprintf(stderr,"print fps %d\n",global.ui.printFPS);
184        if (global.ui.printFPS)
185                glutIdleFunc(idle);
186        else
187                glutIdleFunc(0);
188#if defined(V_DISTRIBUTED)
189        net->togglePrintFPS();
190#endif
191}
192
193void toggleCutPlane() {
194        global.ui.cutEnabled = !global.ui.cutEnabled;
195#if defined(V_DISTRIBUTED)
196        net->toggleCutPlane();
197#endif
198}
199
200void saveGradientVolume() {
201        global.volume->saveGradient();
202#if defined(V_DISTRIBUTED)
203        net->saveGradientVolume();
204#endif
205}
206
207void toggleBoundBox() {
208        global.ui.bboxEnabled = !global.ui.bboxEnabled;
209#if defined(V_DISTRIBUTED)
210        net->toggleBoundBox();
211#endif
212}
213void scaleSampleRate(float factor) {
214        global.volren.goodSamp *= factor;
215        global.volren.interactSamp *= factor;
216        global.volren.sampleRate = global.volren.goodSamp;
217#if defined(V_DISTRIBUTED)
218        net->scaleSampleRate(factor);
219#endif
220}
221
222
223
224void doExit() {
225        deallocateAll();
226#if defined(ADDCG)
227        exitCG():
228#endif
229#if defined(V_DISTRIBUTED)
230        net->deactivateTiles();
231#else
232        exit(0);
233#endif
234}
235
236void doAxisRotation(float angle, char rotaxes) {
237        float axis[] = {0.0f,0.0f,0.0f};
238        switch (rotaxes) {
239        case 'x':
240                axis[0] = 1.0f;
241                break;
242        case 'y':
243                axis[1] = 1.0f;
244                break;
245        case 'z':
246                axis[2] = 1.0f;
247                break;
248        }
249        if (global.ui.cutEnabled) {
250                global.cut.update = true;
251                global.cut.angle = angle; //in degree
252                global.cut.axis[0] = axis[0]; //in degree
253                global.cut.axis[1] = axis[1]; //in degree
254                global.cut.axis[2] = axis[2]; //in degree
255#if defined(V_DISTRIBUTED)
256                net->setCutRotation(global.cut.angle, global.cut.axis);
257#endif
258        }
259        else {
260                computeMatrix(angle, axis, renderList[global.curPrimIdx]->xform.rotn);
261#if defined(V_DISTRIBUTED)
262                net->setRotn(renderList[global.curPrimIdx]->xform.rotn);
263#endif
264        }
265}
266
267//set the lookup table, either from network or locally
268void setLUT(int size, unsigned char* data) {
269        int tfSize = 256*4*sizeof(char);
270        for (int i=0;i<tfSize;i++)
271                global.volren.deptex[i] = data[i];
272        global.volren.loadTLUT = true;
273        global.volren.lowRes = *((int*)&data[tfSize]);
274#if defined(V_DISTRIBUTED)
275        net->setLUT(size,data);
276#endif
277}
278
279//set the low res
280void setLowRes(int lowres) {
281        global.volren.lowRes = lowres;
282#ifdef V_DISTRIBUTED
283        net->setLowRes(global.volren.lowRes);
284#endif
285}
286
287//get isosurface from the optiserver
288//and place it at the mouse ptr
289void genIsosurface(int x, int y) {
290        NetVertexBuffer* theGeom = global.volume->isoSurface();
291        vGeometry* newIso = new vGeometry(theGeom);
292        mouseToWorld(x,y,newIso->xform.trans);
293        newIso->initScale = 1.0f/global.volume->maxDim;
294        fprintf(stderr,"New geom translation %f %f scale %f\n",newIso->xform.trans[0],newIso->xform.trans[1],
295                        newIso->initScale);
296        renderList.push_back(newIso);
297#ifdef V_DISTRIBUTED
298        net->getIsosurface(global.volume->getIsoValue(),newIso->xform.trans[0],newIso->xform.trans[1],newIso->xform.scale);
299#endif
300}
301
302/*
303void genIsopoint(int x, int y) {
304        NetPointBuffer* theGeom = global.volume->isoPoint();
305        vGeometry* newIso = new vGeometry(theGeom);
306        float dx = (float)x/(float)global.win.width - 0.5;
307        float dy = (float)y/(float)global.win.height - 0.5;
308        newIso->xform.trans[0] = dx * (global.env.frustum[1]-global.env.frustum[0]) * global.env.eye[2]/global.env.clip[0];
309        newIso->xform.trans[1] = dy *(global.env.frustum[3]-global.env.frustum[2]) * global.env.eye[2]/global.env.clip[0];
310        newIso->xform.scale = 1.0f/global.volume->maxDim;
311        fprintf(stderr,"New geom translation %f %f scale %f\n",newIso->xform.trans[0],newIso->xform.trans[1],newIso->xform.scale);
312        renderList.push_back(newIso);
313#ifdef V_DISTRIBUTED
314        net->getIsopoint(global.volume->getIsoValue(),newIso->xform.trans[0],newIso->xform.trans[1],newIso->xform.scale);
315#endif
316}
317*/
318void incIsoValue(int value) {
319        global.volume->incIsoValue(value);
320}
321
322void setRoam() {
323        global.ui.navMode= V_ROAM;
324#if defined(V_DISTRIBUTED)
325        net->setRoam();
326#endif
327}
328
329void roamVolume(int roamX, int roamY, int roamZ) {
330        global.volume->roam(roamX, roamY, roamZ);
331        global.volren.loadVolume = true;
332#if defined(V_DISTRIBUTED)
333        net->roamVolume(roamX, roamY, roamZ);
334#endif
335}
336
337void animateVolume() {
338        for (int i=0;i<renderList.size();i++)
339                renderList[i]->toggleAnimate();
340#if defined(V_DISTRIBUTED)
341        net->animateVolume();
342#endif
343}
344
345void updateAnim(int value)
346{
347        for (int i=0;i<renderList.size();i++) {
348                if (renderList[i]->isAnimating()) {
349                        renderList[i]->next();
350                }
351        }
352//if defined(V_DISTRIBUTED)
353//      net->updateAnim();
354//#endif
355        glutTimerFunc(50, updateAnim, 1);
356}
357
358
359
360void setProbe() {
361        global.ui.navMode= V_PROBE;
362#if defined(V_DISTRIBUTED)
363        net->setProbe();
364#endif
365}
366
367void probeVolume(int probeX, int probeY, int probeZ) {
368        global.volume->probe(probeX, probeY, probeZ);
369#if defined(V_DISTRIBUTED)
370        net->probeVolume(probeX, probeY, probeZ);
371#endif
372}
373
374void doSelect(int curIndex) {
375        for (int i=0;i<renderList.size();i++) {
376                if (i==curIndex)
377                        renderList[i]->setSelected(true);
378                else
379                        renderList[i]->setSelected(false);
380        }
381#if defined(V_DISTRIBUTED)
382        net->select(curIndex);
383#endif
384}
385
386void doSelect(int x, int y) {
387        //get the pointer position in world coords
388        mouseToWorld(x, y, global.pointerPos) ;
389        global.curPrimIdx= 0;
390        //select each primitive to see which is selected
391        /*for (int i=0;i< renderList.size();i++) {
392                if (renderList[i]->select()) {
393                        global.curPrimIdx= i;
394                        break;
395                }
396        }*/
397
398#if defined(V_DISTRIBUTED)
399        net->select(global.pointerPos);
400#endif
401}
Note: See TracBrowser for help on using the repository browser.