14 |
#include "util.h" |
#include "util.h" |
15 |
#include "vr.h" |
#include "vr.h" |
16 |
#include <unistd.h> |
#include <unistd.h> |
17 |
|
#include "teem/air.h" |
18 |
|
|
19 |
/* minimum and maximum values from */ |
/* minimum and maximum values from */ |
20 |
#define MIN_VALUE 0 |
#define MIN_VALUE 0 |
26 |
#define MIN_OPAC_MID ((float)(MIN_VALUE+OPAC_HALF_WID)) |
#define MIN_OPAC_MID ((float)(MIN_VALUE+OPAC_HALF_WID)) |
27 |
#define MAX_OPAC_MID ((float)(MAX_VALUE-OPAC_HALF_WID)) |
#define MAX_OPAC_MID ((float)(MAX_VALUE-OPAC_HALF_WID)) |
28 |
|
|
29 |
#define OPAC_DELTA 100 |
#define OPAC_DELTA 25 |
30 |
|
|
31 |
/* pixel dimensions of output */ |
/* reduced-size inputs */ |
32 |
#define WIDTH 480 |
#define REDUCED_WID 256 |
33 |
#define HEIGHT 345 |
#define REDUCED_HT 184 |
34 |
|
#define REDUCED_STEP 1 |
35 |
/* world bounds */ |
|
36 |
#define WRLD_MIN_X 0.0 |
|
37 |
#define WRLD_MIN_Y 0.0 |
/* full-size inputs */ |
38 |
#define WRLD_MAX_X 1.0 |
#define FULL_WID 512 |
39 |
#define WRLD_MAX_Y 1.0 |
#define FULL_HT 368 |
40 |
#define WRLD_CENTER_X 0.5 |
#define FULL_STEP 0.5 |
41 |
#define WRLD_CENTER_Y 0.5 |
|
42 |
|
#define WIDTH (2*FULL_WID) |
43 |
|
#define HEIGHT (2*FULL_HT) |
44 |
|
|
45 |
static inline void CheckError () |
static inline void CheckError () |
46 |
{ |
{ |
53 |
|
|
54 |
/***** Globals for viewing, etc. *****/ |
/***** Globals for viewing, etc. *****/ |
55 |
|
|
56 |
|
typedef struct { |
57 |
|
bool init; |
58 |
|
GLuint wid; |
59 |
|
GLuint ht; |
60 |
|
GLuint id; |
61 |
|
} TexInfo_t; |
62 |
|
|
63 |
VR_World_t *World; |
VR_World_t *World; |
64 |
unsigned int Width; // view window width |
unsigned int Width; // view window width |
65 |
unsigned int Height; // view window height |
unsigned int Height; // view window height |
66 |
|
TexInfo_t ReducedTexure = { // Texture ID of reduced image |
67 |
|
false, REDUCED_WID, REDUCED_HT, 0 |
68 |
|
}; |
69 |
|
TexInfo_t FullTexture = { // Texture ID of full image |
70 |
|
false, FULL_WID, FULL_HT, 0 |
71 |
|
}; |
72 |
bool NeedsRedraw; |
bool NeedsRedraw; |
73 |
bool NeedsRecompute; |
bool NeedsRecompute; |
74 |
float OpacMid = 550.0; |
float OpacMid = 550.0; |
75 |
|
int Dir = 0; /* -1, 0, 1 */ |
76 |
|
|
77 |
|
|
78 |
|
// initialize a texture from a grayscale Nrrd file |
79 |
|
void InitTexture (TexInfo_t *txt, Nrrd *img) |
80 |
|
{ |
81 |
|
if (! txt->init) { |
82 |
|
// generate the TeXID |
83 |
|
glGenTextures (1, &(txt->id)); |
84 |
|
|
85 |
|
// load the texture data |
86 |
|
glBindTexture (GL_TEXTURE_2D, txt->id); |
87 |
|
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, txt->wid, txt->ht, 0, GL_RGBA, GL_FLOAT, img->data); |
88 |
|
CheckError(); |
89 |
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
90 |
|
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
91 |
|
CheckError(); |
92 |
|
} |
93 |
|
else { |
94 |
|
// reload the texture data |
95 |
|
glBindTexture (GL_TEXTURE_2D, txt->id); |
96 |
|
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, txt->wid, txt->ht, GL_RGBA, GL_FLOAT, img->data); |
97 |
|
CheckError(); |
98 |
|
} |
99 |
|
} |
100 |
|
|
101 |
// Callback function called by GLUT to render screen |
// Callback function called by GLUT to render screen |
102 |
void Draw (Nrrd *img) |
void Draw (GLuint txtId) |
103 |
{ |
{ |
104 |
// Clear frame buffer |
// Clear frame buffer |
105 |
glClearColor (0, 0, 0, 1); |
glClearColor (0, 0, 0, 1); |
109 |
glDisable(GL_CULL_FACE); |
glDisable(GL_CULL_FACE); |
110 |
|
|
111 |
// Draw image |
// Draw image |
112 |
glDrawPixels (Width, Height, GL_RGBA, GL_FLOAT, img->data); |
glEnable (GL_TEXTURE_2D); |
113 |
|
glActiveTexture (GL_TEXTURE0); |
114 |
|
glBindTexture (GL_TEXTURE_2D, txtId); |
115 |
|
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
116 |
|
glBegin (GL_QUADS); |
117 |
|
glTexCoord2f (0.0, 0.0); |
118 |
|
glVertex2f (0.0, 0.0); |
119 |
|
glTexCoord2f (1.0, 0.0); |
120 |
|
glVertex2f (1.0, 0.0); |
121 |
|
glTexCoord2f (1.0, 1.0); |
122 |
|
glVertex2f (1.0, 1.0); |
123 |
|
glTexCoord2f (0.0, 1.0); |
124 |
|
glVertex2f (0.0, 1.0); |
125 |
|
glEnd(); |
126 |
|
glDisable (GL_TEXTURE_2D); |
127 |
CheckError (); |
CheckError (); |
128 |
|
|
129 |
// Present frame buffer |
// Present frame buffer |
136 |
// Set OpenGL viewport and camera |
// Set OpenGL viewport and camera |
137 |
glViewport(0, 0, width, height); |
glViewport(0, 0, width, height); |
138 |
|
|
139 |
|
glMatrixMode (GL_PROJECTION); |
140 |
|
glLoadIdentity (); |
141 |
|
gluOrtho2D (0.0, 1.0, 0.0, 1.0); |
142 |
|
|
143 |
|
glMatrixMode (GL_MODELVIEW); |
144 |
|
glLoadIdentity (); |
145 |
|
gluLookAt ( |
146 |
|
0.0, 0.0, 1.0, |
147 |
|
0.0, 0.0, 0.0, |
148 |
|
0.0, 1.0, 0.0); |
149 |
|
|
150 |
// remember width and height |
// remember width and height |
151 |
Width = width; |
Width = width; |
152 |
Height = height; |
Height = height; |
155 |
|
|
156 |
void GLFWCALL KeyCB (int key, int action) |
void GLFWCALL KeyCB (int key, int action) |
157 |
{ |
{ |
|
if (action == GLFW_PRESS) { |
|
158 |
switch (key) { |
switch (key) { |
159 |
case '+': { |
case '+': |
160 |
float mid = OpacMid + OPAC_DELTA; |
if ((action == GLFW_PRESS) && (Dir >= 0)) |
161 |
if (mid <= MAX_OPAC_MID) { |
Dir++; |
162 |
OpacMid = mid; |
break; |
163 |
VR_InVarSet_valOpacMid(World, mid); |
case '-': |
164 |
NeedsRecompute = true; |
if ((action == GLFW_PRESS) && (Dir <= 0)) |
165 |
} |
Dir--; |
166 |
} break; |
break; |
|
case '-': { |
|
|
float mid = OpacMid - OPAC_DELTA; |
|
|
if (mid >= MIN_OPAC_MID) { |
|
|
OpacMid = mid; |
|
|
VR_InVarSet_valOpacMid(World, mid); |
|
|
NeedsRecompute = true; |
|
|
} |
|
|
} break; |
|
167 |
default: |
default: |
168 |
break; |
break; |
169 |
} |
} |
170 |
} |
} |
|
} |
|
171 |
|
|
172 |
void GetData (VR_World_t *wrld, Nrrd *nRGB) |
void Compute (VR_World_t *wrld, bool fullSize, Nrrd *nRGB) |
173 |
{ |
{ |
174 |
|
printf("computing %s... ", fullSize ? "" : "(fast) "); fflush(stdout); |
175 |
|
double t0 = airTime(); |
176 |
|
// setup raycast parameters |
177 |
|
if (fullSize) { |
178 |
|
VR_InVarSet_imgResU (wrld, FULL_WID); |
179 |
|
VR_InVarSet_imgResV (wrld, FULL_HT); |
180 |
|
VR_InVarSet_rayStep (wrld, FULL_STEP); |
181 |
|
} |
182 |
|
else { |
183 |
|
VR_InVarSet_imgResU (wrld, REDUCED_WID); |
184 |
|
VR_InVarSet_imgResV (wrld, REDUCED_HT); |
185 |
|
VR_InVarSet_rayStep (wrld, REDUCED_STEP); |
186 |
|
} |
187 |
|
// recompute |
188 |
|
if (VR_Initially (wrld)) { |
189 |
|
// error |
190 |
|
fprintf(stderr, "Error initializing world: %s", VR_GetErrors(wrld)); |
191 |
|
exit(1); |
192 |
|
} |
193 |
|
int nSteps = VR_Run (wrld, 0); |
194 |
|
printf(" %d steps in %5.3f seconds\n", nSteps, airTime() - t0); |
195 |
|
// get output image |
196 |
if (VR_OutputGet_outRGBA (wrld, nRGB)) { |
if (VR_OutputGet_outRGBA (wrld, nRGB)) { |
197 |
// error |
// error |
198 |
fprintf(stderr, "Error getting nrrd data\n"); |
fprintf(stderr, "Error getting nrrd data: %s", VR_GetErrors(wrld)); |
199 |
exit(1); |
exit(1); |
200 |
} |
} |
201 |
} |
} |
230 |
|
|
231 |
// initialize inputs |
// initialize inputs |
232 |
VR_InVarSet_valOpacMid(wrld, OpacMid); |
VR_InVarSet_valOpacMid(wrld, OpacMid); |
233 |
Nrrd *nin = nrrdNew(); |
VR_InVarSetByName_hand (wrld, "data/vfrhand-nohip.nhdr"); |
|
/* read in the nrrd from file */ |
|
|
if (nrrdLoad(nin, "data/vfrhand-nohip.nhdr", NULL) != 0) { |
|
|
char *msg = biffGetDone(NRRD); |
|
|
fprintf (stderr, "Error loading data: %s", msg); |
|
|
exit (1); |
|
|
} |
|
|
VR_InVarSet_hand (wrld, nin); |
|
|
// nrrdNix (nin); |
|
234 |
|
|
235 |
// nrrd for getting computational state |
// nrrd for getting computational state |
236 |
Nrrd *nRGB = nrrdNew(); |
Nrrd *nRGB = nrrdNew(); |
237 |
|
|
238 |
// Main loop (repeated while window is not closed and [ESC] is not pressed) |
// Main loop (repeated while window is not closed and [ESC] is not pressed) |
239 |
NeedsRecompute = true; |
NeedsRecompute = true; |
240 |
|
GLuint txtId; |
241 |
while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) { |
while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) { |
242 |
if (NeedsRecompute) { |
if (Dir < 0) { |
243 |
printf("computing ... "); fflush(stdout); |
Dir++; |
244 |
// recompute |
float mid = OpacMid - OPAC_DELTA; |
245 |
if (VR_Initially (wrld)) { |
if (mid >= MIN_OPAC_MID) { |
246 |
// error |
OpacMid = mid; |
247 |
fprintf(stderr, "Error initializing world: %s", VR_GetErrors(wrld)); |
VR_InVarSet_valOpacMid(World, mid); |
248 |
exit(1); |
NeedsRecompute = true; |
249 |
|
} |
250 |
|
} |
251 |
|
else if (Dir > 0) { |
252 |
|
Dir--; |
253 |
|
float mid = OpacMid + OPAC_DELTA; |
254 |
|
if (mid <= MAX_OPAC_MID) { |
255 |
|
OpacMid = mid; |
256 |
|
VR_InVarSet_valOpacMid(World, mid); |
257 |
|
NeedsRecompute = true; |
258 |
} |
} |
|
int nSteps = VR_Run (wrld, 0); |
|
|
printf(" %d steps\n", nSteps); |
|
|
// get output image |
|
|
if (VR_OutputGet_outRGBA (wrld, nRGB)) { |
|
|
// error |
|
|
fprintf(stderr, "Error getting nrrd data: %s", VR_GetErrors(wrld)); |
|
|
exit(1); |
|
259 |
} |
} |
260 |
|
if (NeedsRecompute) { |
261 |
|
if (Dir != 0) { |
262 |
|
Compute(wrld, false, nRGB); |
263 |
|
InitTexture(&ReducedTexure, nRGB); |
264 |
|
txtId = ReducedTexure.id; |
265 |
|
} |
266 |
|
else { |
267 |
|
Compute (wrld, true, nRGB); |
268 |
|
InitTexture(&FullTexture, nRGB); |
269 |
NeedsRecompute = false; |
NeedsRecompute = false; |
270 |
|
txtId = FullTexture.id; |
271 |
|
} |
272 |
NeedsRedraw = true; |
NeedsRedraw = true; |
273 |
} |
} |
274 |
if (NeedsRedraw) { |
if (NeedsRedraw) { |
275 |
Draw (nRGB); |
Draw (txtId); |
276 |
NeedsRedraw = false; |
NeedsRedraw = false; |
277 |
} |
} |
278 |
|
if (NeedsRecompute) |
279 |
|
glfwPollEvents(); |
280 |
|
else |
281 |
glfwWaitEvents(); |
glfwWaitEvents(); |
282 |
} |
} |
283 |
|
|