11 |
*/ |
*/ |
12 |
|
|
13 |
#include <AntTweakBar.h> |
#include <AntTweakBar.h> |
14 |
#if defined(__APPLE__) |
#include "GL/glfw.h" |
15 |
# include <GLUT/glut.h> |
#ifdef DIDEROT_CODE |
|
#else |
|
|
# include <GL/glut.h> |
|
|
#endif |
|
16 |
#include "vr-lite-cam.h" |
#include "vr-lite-cam.h" |
17 |
|
#endif |
18 |
|
#include "teem/nrrd.h" |
19 |
|
#include "teem/biff.h" |
20 |
|
|
21 |
/* pixel dimensions of output */ |
/* pixel dimensions of output */ |
22 |
#define WIDTH 512 |
#define WIDTH 512 |
29 |
unsigned int Height; // view window height |
unsigned int Height; // view window height |
30 |
GLuint ImageTexture; // Texture ID of background image |
GLuint ImageTexture; // Texture ID of background image |
31 |
|
|
32 |
|
typedef struct { |
33 |
|
TwBar *ctls; |
34 |
|
unsigned int running; |
35 |
|
} State_t; |
36 |
|
|
37 |
// Callback function called by GLUT to render screen |
// Callback function called by GLUT to render screen |
38 |
void Display(void) |
void Draw () |
39 |
{ |
{ |
40 |
// Clear frame buffer |
// Clear frame buffer |
41 |
glClearColor (0, 0, 0, 1); |
glClearColor (0, 0, 0, 1); |
45 |
glDisable(GL_CULL_FACE); |
glDisable(GL_CULL_FACE); |
46 |
|
|
47 |
// Draw background |
// Draw background |
48 |
|
float halfWid = 0.5 * (float)Width; |
49 |
|
float halfHt = 0.5 * (float)Height; |
50 |
|
glBindTexture (GL_TEXTURE_2D, ImageTexture); |
51 |
|
glBegin (GL_QUADS); |
52 |
|
glTexCoord2f (0.0, 0.0); |
53 |
|
glVertex2f (-halfWid, -halfHt); |
54 |
|
glTexCoord2f (1.0, 0.0); |
55 |
|
glVertex2f (halfWid, -halfHt); |
56 |
|
glTexCoord2f (1.0, 1.0); |
57 |
|
glVertex2f (halfWid, halfHt); |
58 |
|
glTexCoord2f (0.0, 1.0); |
59 |
|
glVertex2f (-halfWid, halfHt); |
60 |
|
glEnd(); |
61 |
|
|
62 |
// Draw current render state |
// Draw current render state |
63 |
|
|
65 |
TwDraw(); |
TwDraw(); |
66 |
|
|
67 |
// Present frame buffer |
// Present frame buffer |
68 |
glutSwapBuffers(); |
glfwSwapBuffers(); |
69 |
} |
} |
70 |
|
|
71 |
// Callback function called by GLUT when window size changes |
// Callback function called by GLFW when window size changes |
72 |
void Reshape (int width, int height) |
void GLFWCALL WindowSizeCB (int width, int height) |
73 |
{ |
{ |
74 |
|
double halfWid = 0.5 * (double)width; |
75 |
|
double halfHt = 0.5 * (double)height; |
76 |
|
|
77 |
// Set OpenGL viewport and camera |
// Set OpenGL viewport and camera |
78 |
glViewport (0, 0, width, height); |
glViewport (0, 0, width, height); |
79 |
glMatrixMode (GL_PROJECTION); |
glMatrixMode (GL_PROJECTION); |
80 |
glLoadIdentity (); |
glLoadIdentity (); |
81 |
/* FIXME */ |
gluOrtho2D (-halfWid, halfWid, -halfHt, halfHt); |
82 |
|
|
83 |
glMatrixMode (GL_MODELVIEW); |
glMatrixMode (GL_MODELVIEW); |
84 |
glLoadIdentity (); |
glLoadIdentity (); |
85 |
/* FIXME */ |
gluLookAt ( |
86 |
|
0.0, 0.0, 1.0, |
87 |
|
0.0, 0.0, 0.0, |
88 |
|
0.0, 1.0, 0.0); |
89 |
|
|
90 |
// Send the new window size to AntTweakBar |
// Send the new window size to AntTweakBar |
91 |
TwWindowSize (width, height); |
TwWindowSize (width, height); |
125 |
glTexImage2D (GL_TEXTURE_2D, /* or GL_TEXTURE_RECTANGLE ? */ |
glTexImage2D (GL_TEXTURE_2D, /* or GL_TEXTURE_RECTANGLE ? */ |
126 |
0, |
0, |
127 |
GL_RED, |
GL_RED, |
128 |
nin->axis[0]->size, nin->axis[1]->size, 0, |
nin->axis[0].size, nin->axis[1].size, 0, |
129 |
GL_RED, type, |
GL_RED, type, |
130 |
nin->data); |
nin->data); |
131 |
/* check for error */ |
/* check for error */ |
133 |
ImageTexture = texId; |
ImageTexture = texId; |
134 |
} |
} |
135 |
|
|
136 |
|
void TW_CALL StartButtonCB (void *data) |
137 |
|
{ |
138 |
|
State_t *st = (State_t *)data; |
139 |
|
|
140 |
|
if (st->running == 0) { |
141 |
|
st->running = 1; |
142 |
|
TwSetParam (st->ctls, "StartButton", "label", TW_PARAM_CSTRING, 1, "Stop"); |
143 |
|
} |
144 |
|
else { |
145 |
|
st->running = 0; |
146 |
|
TwSetParam (st->ctls, "StartButton", "label", TW_PARAM_CSTRING, 1, "Start"); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
int main (int argc, const char **argv) |
int main (int argc, const char **argv) |
151 |
{ |
{ |
152 |
// Initialize GLUT |
State_t state = { .running = 0 }; |
153 |
glutInit (&argc, argv); |
|
154 |
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); |
// Initialize GLFW |
155 |
glutInitWindowSize (WIDTH, HEIGHT); |
if (0 == glfwInit()) { |
156 |
glutCreateWindow ("Diderot Iso2D Animation"); |
// An error occured |
157 |
glutCreateMenu (0); |
fprintf(stderr, "GLFW initialization failed\n"); |
158 |
|
return 1; |
159 |
// Set GLUT callbacks |
} |
160 |
glutDisplayFunc (Display); |
|
161 |
glutReshapeFunc (Reshape); |
GLFWvidmode mode; |
162 |
atexit (TwTerminate); // Called after glutMainLoop ends |
glfwGetDesktopMode (&mode); |
163 |
|
if (0 == glfwOpenWindow(WIDTH, HEIGHT, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 0, 0, GLFW_WINDOW) ) { |
164 |
|
// A fatal error occured |
165 |
|
fprintf(stderr, "Cannot open GLFW window\n"); |
166 |
|
glfwTerminate(); |
167 |
|
return 1; |
168 |
|
} |
169 |
|
glfwEnable(GLFW_MOUSE_CURSOR); |
170 |
|
glfwEnable(GLFW_KEY_REPEAT); |
171 |
|
glfwSetWindowTitle("Diderot Iso2D Animation"); |
172 |
|
|
173 |
// Initialize AntTweakBar |
// Initialize AntTweakBar |
174 |
TwInit (TW_OPENGL, 0); |
TwInit (TW_OPENGL, 0); |
175 |
|
|
176 |
// Set GLUT event callbacks |
// create a tweakbar |
177 |
// - Directly redirect GLUT mouse button events to AntTweakBar |
TwBar *tweakBar = TwNewBar("Controls"); |
178 |
glutMouseFunc ((GLUTmousebuttonfun)TwEventMouseButtonGLUT); |
state.ctls = tweakBar; |
179 |
// - Directly redirect GLUT mouse motion events to AntTweakBar |
unsigned int NumSteps = 0; |
180 |
glutMotionFunc ((GLUTmousemotionfun)TwEventMouseMotionGLUT); |
TwAddVarRO (tweakBar, "NumSteps", TW_TYPE_UINT32, &NumSteps, " label='# of steps' "); |
181 |
// - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion) |
TwAddButton (tweakBar, "StartButton", StartButtonCB, &state, " label='Start' "); |
182 |
glutPassiveMotionFunc ((GLUTmousemotionfun)TwEventMouseMotionGLUT); |
|
183 |
// - Directly redirect GLUT key events to AntTweakBar |
// Set GLFW event callbacks |
184 |
glutKeyboardFunc ((GLUTkeyboardfun)TwEventKeyboardGLUT); |
glfwSetWindowSizeCallback (WindowSizeCB); |
185 |
// - Directly redirect GLUT special key events to AntTweakBar |
glfwSetMouseButtonCallback ((GLFWmousebuttonfun)TwEventMouseButtonGLFW); // redirect to AntTweakBar |
186 |
glutSpecialFunc ((GLUTspecialfun)TwEventSpecialGLUT); |
glfwSetMousePosCallback ((GLFWmouseposfun)TwEventMousePosGLFW); // redirect to AntTweakBar |
187 |
// - Send 'glutGetModifers' function pointer to AntTweakBar; |
glfwSetMouseWheelCallback ((GLFWmousewheelfun)TwEventMouseWheelGLFW); // redirect to AntTweakBar |
188 |
// required because the GLUT key event functions do not report key modifiers states. |
glfwSetKeyCallback ((GLFWkeyfun)TwEventKeyGLFW); // redirect to AntTweakBar |
189 |
TwGLUTModifiersFunc (glutGetModifiers); |
glfwSetCharCallback ((GLFWcharfun)TwEventCharGLFW); // redirect to AntTweakBar |
190 |
|
|
191 |
// load Diderot image as nrrd file |
// load Diderot image as nrrd file |
192 |
Nrrd *Diderot = nrrdNew(); |
Diderot = nrrdNew(); |
193 |
if (nrrdLoad(diderot, "data/ddro-80.nrrd", 0) != 0) { |
if (nrrdLoad(Diderot, "data/ddro-80.nrrd", 0) != 0) { |
194 |
char *msg = biffGetDone(NRRD); |
char *msg = biffGetDone(NRRD); |
195 |
fprintf (stderr, "Error loading nrrd file: %s", msg); |
fprintf (stderr, "Error loading nrrd file: %s", msg); |
196 |
free (msg); |
free (msg); |
197 |
return 0; |
return 0; |
198 |
} |
} |
199 |
|
|
200 |
|
InitTexture (Diderot); |
201 |
|
|
202 |
|
// Main loop (repeated while window is not closed and [ESC] is not pressed) |
203 |
|
while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) { |
204 |
|
Draw(); |
205 |
|
} |
206 |
|
|
207 |
|
#ifdef DIDEROT_CODE |
208 |
ISO_World_t *wrld = ISO_Init (); |
ISO_World_t *wrld = ISO_Init (); |
209 |
|
|
210 |
ISO_Initially (wrld); |
ISO_Initially (wrld); |
226 |
} |
} |
227 |
|
|
228 |
ISO_Shutdown (wrld); |
ISO_Shutdown (wrld); |
229 |
|
#endif |
230 |
|
|
231 |
|
// Terminate AntTweakBar and GLFW |
232 |
|
TwTerminate(); |
233 |
|
glfwTerminate(); |
234 |
|
|
235 |
return 0; |
return 0; |
236 |
} |
} |