2 #include <GL/freeglut.h>
13 #define BEAM_SHELLS 40
14 #define BEAM_RMIN 0.01
15 #define BEAM_RMAX 0.125
16 #define BEAM_ENERGY 0.01
18 #define BEAM_DEF_SPEED 0.1
27 static void cleanup();
29 static void display();
31 static void backdrop();
35 static void reshape(int x, int y);
36 static void keyboard(unsigned char c, int x, int y);
37 static void keyb_special(int key, int x, int y);
38 static void mbutton(int bn, int state, int x, int y);
39 static void mmotion(int x, int y);
41 static int win_width, win_height;
42 static bool freecam = true;
44 static Camera cam = {0, 0, 0, 0, 0, 10};
45 static unsigned int sdr_beam, sdr_sky;
46 static long start_time;
47 static long anim_stop_time;
48 static long tmsec, prev_tmsec;
50 static const float sil_color[] = {0.05, 0.02, 0.1, 1.0};
51 static const float beam_color[] = {0.5, 0.4, 0.2, 1.0};
53 static float beam_angle, beam_speed;
54 static float beam_len;
55 static float xlogo_alpha;
57 static bool show_help;
59 int main(int argc, char **argv)
61 glutInit(&argc, argv);
62 glutInitWindowSize(800, 600);
63 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
65 glutCreateWindow("Faros (press F1 for controls help)");
67 glutDisplayFunc(display);
69 glutReshapeFunc(reshape);
70 glutKeyboardFunc(keyboard);
71 glutSpecialFunc(keyb_special);
72 glutMouseFunc(mbutton);
73 glutMotionFunc(mmotion);
89 glEnable(GL_CULL_FACE);
90 glEnable(GL_DEPTH_TEST);
91 glEnable(GL_MULTISAMPLE);
93 // glEnable(GL_LIGHTING);
96 glEnable(GL_NORMALIZE);
101 if(!(sdr_beam = create_program_load("sdr/beam.v.glsl", "sdr/beam.f.glsl")))
104 if(!(sdr_sky = create_program_load("sdr/sky.v.glsl", "sdr/sky.f.glsl"))) {
111 add_seq_track("beam-speed", INTERP_SIGMOID, EXTRAP_CLAMP, BEAM_DEF_SPEED);
112 add_seq_track("beam-len", INTERP_SIGMOID, EXTRAP_CLAMP, BEAM_LEN);
113 add_seq_track("cam-dist", INTERP_SIGMOID, EXTRAP_CLAMP, 10);
114 add_seq_track("cam-phi", INTERP_SIGMOID, EXTRAP_CLAMP, 0);
115 add_seq_track("cam-theta", INTERP_SIGMOID, EXTRAP_CLAMP, 0);
116 add_seq_track("cam-x", INTERP_SIGMOID, EXTRAP_CLAMP, 0);
117 add_seq_track("cam-y", INTERP_SIGMOID, EXTRAP_CLAMP, 0);
118 add_seq_track("cam-z", INTERP_SIGMOID, EXTRAP_CLAMP, 0);
119 add_seq_track("xlogo", INTERP_SIGMOID, EXTRAP_CLAMP, 0);
122 start_time = glutGet(GLUT_ELAPSED_TIME);
123 prev_tmsec = start_time;
127 static void cleanup()
131 free_program(sdr_beam);
132 free_program(sdr_sky);
135 static void display()
137 tmsec = (long)glutGet(GLUT_ELAPSED_TIME) - start_time;
138 float dt = (tmsec - prev_tmsec) / 1000.0f;
141 if(anim_stop_time) dt = 0.0f;
143 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
146 glMatrixMode(GL_MODELVIEW);
150 cam.dist = get_seq_value("cam-dist", tmsec);
151 cam.phi = get_seq_value("cam-phi", tmsec);
152 cam.theta = get_seq_value("cam-theta", tmsec);
153 cam.x = get_seq_value("cam-x", tmsec);
154 cam.y = get_seq_value("cam-y", tmsec);
155 cam.z = get_seq_value("cam-z", tmsec);
158 glTranslatef(0, -2, -cam.dist);
159 glRotatef(cam.phi, 1, 0, 0);
160 glRotatef(cam.theta, 0, 1, 0);
161 glTranslatef(-cam.x, -cam.y, -cam.z);
163 glColor3fv(sil_color);
167 beam_len = get_seq_value("beam-len", tmsec);
168 beam_speed = get_seq_value("beam-speed", tmsec);
169 beam_angle += beam_speed * 360.0f * dt;
171 xlogo_alpha = get_seq_value("xlogo", tmsec);
172 if(xlogo_alpha > 0.0) {
174 float beam_angle_rad = beam_angle / 180.0 * M_PI;
175 float xlogo_dist = beam_len;
176 float xlogo_pos[3] = {sin(beam_angle_rad), 0, cos(beam_angle_rad)};
177 glTranslatef(xlogo_pos[0] * xlogo_dist, xlogo_pos[1] * xlogo_dist + 5, xlogo_pos[2] * xlogo_dist);
178 glColor4f(0, 0, 0, xlogo_alpha);
184 glRotatef(beam_angle, 0, 1, 0);
197 glPushAttrib(GL_ENABLE_BIT);
198 glDisable(GL_CULL_FACE);
202 glTranslatef(0, 4.65, 0.2);
203 bind_program(sdr_beam);
204 set_uniform_float(sdr_beam, "beam_len", beam_len);
207 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
210 for(int i=0; i<BEAM_SHELLS; i++) {
211 float t = (float)i / (float)(BEAM_SHELLS - 1);
212 float rad = BEAM_RMIN + (BEAM_RMAX - BEAM_RMIN) * t;
214 float alpha = BEAM_ENERGY / (t * t);
217 glColor4f(beam_color[0], beam_color[1], beam_color[2], alpha);
219 glutSolidCylinder(rad, beam_len, 12, 1);
231 static void backdrop()
234 bind_program(sdr_sky);
235 glutSolidSphere(200, 16, 32);
242 static const char *help_lines[] = {
244 " LMB ............ rotate",
245 " MMB drag ....... pan",
246 " RMB drag/wheel . zoom",
247 " c .............. toggle free/animated camera",
248 " v .............. print current view parameters",
251 " <space> ........ pause time",
252 " <backspace> .... restart time",
253 " +/- ............ change beam rotation speed and set keyframe",
254 " 0 .............. clear beam rotation keyframes",
255 " [/] ............ change beam length and set keyframe",
256 " \\ .............. clear beam length keyframes",
257 " <enter> ........ record automatic beam start/stop transition",
258 " K .............. set camera keyframe",
259 " <shift>-L ...... clear all camera keyframes",
260 " X .............. toggle X logo and set keyframe",
261 " <shift>-X ...... clear logo keyframes",
262 " ~ .............. dump all animation keyframes to seq_dump",
266 glPushAttrib(GL_ENABLE_BIT);
267 glDisable(GL_DEPTH_TEST);
269 glMatrixMode(GL_MODELVIEW);
272 glMatrixMode(GL_PROJECTION);
275 glOrtho(0, win_width, win_height, 0, -1, 1);
278 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
280 glColor4f(0, 0, 0, 0.5);
282 glVertex2f(0, win_height);
283 glVertex2f(win_width, win_height);
284 glVertex2f(win_width, 0);
290 for(int i=0; help_lines[i]; i++) {
291 glColor3f(0.05, 0.05, 0.05);
292 glRasterPos2i(xpos + 1, ypos + 2);
293 const char *s = help_lines[i];
295 glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s++);
297 glColor3f(0.7, 1, 0.6);
298 glRasterPos2i(xpos, ypos);
301 glutBitmapCharacter(GLUT_BITMAP_9_BY_15, *s++);
308 glMatrixMode(GL_MODELVIEW);
319 static void reshape(int x, int y)
323 glViewport(0, 0, x, y);
325 glMatrixMode(GL_PROJECTION);
328 gluPerspective(50, (float)x / (float)y, 0.5, 500);
331 static void keyboard(unsigned char c, int x, int y)
334 static float orig_beam_speed;
335 static Camera orig_cam;
336 long anim_time = anim_stop_time ? anim_stop_time - start_time : tmsec;
343 start_time = glutGet(GLUT_ELAPSED_TIME);
350 if(anim_stop_time > 0) {
351 long msec = glutGet(GLUT_ELAPSED_TIME);
352 start_time += msec - anim_stop_time;
353 prev_tmsec = msec - start_time;
356 anim_stop_time = glutGet(GLUT_ELAPSED_TIME);
361 beam_speed = get_seq_value("beam-speed", anim_time);
362 set_seq_value("beam-speed", anim_time, beam_speed + 0.1);
366 beam_speed = get_seq_value("beam-speed", anim_time) - 0.1;
367 if(beam_speed < 0) beam_speed = 0;
368 set_seq_value("beam-speed", anim_time, beam_speed);
372 clear_seq_track("beam-speed");
376 beam_len = get_seq_value("beam-len", anim_time) - 0.5;
377 if(beam_len < 0) beam_len = 0;
378 set_seq_value("beam-len", anim_time, beam_len);
382 beam_len = get_seq_value("beam-len", anim_time);
383 set_seq_value("beam-len", anim_time, beam_len + 0.5);
387 clear_seq_track("beam-len");
392 idx = find_seq_track("beam-speed");
394 if(get_seq_value(idx, anim_time) > 0.0) {
395 clear_seq_track(idx);
396 set_seq_value(idx, anim_time, beam_speed);
397 set_seq_value(idx, anim_time + 3000, 0);
398 orig_beam_speed = beam_speed;
400 clear_seq_track(idx);
401 set_seq_value(idx, anim_time, 0);
402 set_seq_value(idx, anim_time + 3000, orig_beam_speed);
408 printf("camera mode: %s\n", freecam ? "free" : "animated");
417 printf("current view\n");
418 printf(" pos: %f %f %f\n", cam.x, cam.y, cam.z);
419 printf(" theta: %f, phi: %f\n", cam.theta, cam.phi);
420 printf(" dist: %f\n", cam.dist);
424 printf("clearing camera keyframes\n");
425 clear_seq_track("cam-x");
426 clear_seq_track("cam-y");
427 clear_seq_track("cam-z");
428 clear_seq_track("cam-theta");
429 clear_seq_track("cam-phi");
430 clear_seq_track("cam-dist");
434 printf("setting camera keyframe for time: %ld\n", anim_time);
435 set_seq_value("cam-x", anim_time, cam.x);
436 set_seq_value("cam-y", anim_time, cam.y);
437 set_seq_value("cam-z", anim_time, cam.z);
438 set_seq_value("cam-theta", anim_time, cam.theta);
439 set_seq_value("cam-phi", anim_time, cam.phi);
440 set_seq_value("cam-dist", anim_time, cam.dist);
444 set_seq_value("xlogo", anim_time, xlogo_alpha < 0.5 ? 1.0 : 0.0);
448 printf("clearing logo keyframes\n");
449 clear_seq_track("xlogo");
453 printf("dumping animation data to: seq_dump\n");
454 if(!dump_seq("seq_dump")) {
455 fprintf(stderr, "dump failed\n");
464 static void keyb_special(int key, int x, int y)
468 show_help = !show_help;
476 static int prev_x, prev_y;
478 static void mbutton(int bn, int state, int x, int y)
480 int button = bn - GLUT_LEFT_BUTTON;
481 bool pressed = state == GLUT_DOWN;
482 bst[button] = pressed;
491 if(cam.dist < 0) cam.dist = 0;
504 static void mmotion(int x, int y)
512 if (dx == 0 && dy == 0)
516 cam.theta += dx * 0.5;
527 cam.dist += dy * 0.1;
534 float theta = cam.theta / 180.0f * M_PI;
535 float phi = cam.phi / 180.0f * M_PI;
537 float pan_u[3], pan_v[3];
539 pan_u[0] = cos(theta);
541 pan_u[2] = sin(theta);
543 pan_v[0] = sin(phi) * sin(theta);
545 pan_v[2] = -sin(phi) * cos(theta);
547 float pan_x = -dx * 0.002 * cam.dist;
548 float pan_y = dy * 0.002 * cam.dist;
550 cam.x += pan_u[0] * pan_x + pan_v[0] * pan_y;
551 cam.y += pan_u[1] * pan_x + pan_v[1] * pan_y;
552 cam.z += pan_u[2] * pan_x + pan_v[2] * pan_y;