2 * Permission to use, copy, modify, and distribute this software and its
3 * documentation for any purpose and without fee is hereby granted,
4 * provided that the above copyright notice appear in all copies and that
5 * both that copyright notice and this permission notice appear in
6 * supporting documentation.
8 * This file is provided AS IS with no warranties of any kind. The author
9 * shall have no liability with respect to the infringement of copyrights,
10 * trade secrets or any patents by this file or any part thereof. In no
11 * event will the author be liable for any lost revenue or profits or
12 * other special, indirect and consequential damages.
14 * Copyright 2003 Blair Tennessy
18 #define DEFAULTS "*delay: 20000 \n" \
19 "*showFPS: False \n" \
22 # define refresh_antspotlight 0
23 #include "xlockmore.h"
29 #include "gltrackball.h"
31 ENTRYPOINT ModeSpecOpt antspotlight_opts = {
32 0, NULL, 0, NULL, NULL
36 ModStruct antspotlight_description = {
37 "antspotlight", "init_antspotlight", "draw_antspotlight",
38 "release_antspotlight", "draw_antspotlight", "change_antspotlight",
39 (char *) NULL, &antspotlight_opts, 1000, 1, 1, 1, 4, 1.0, "",
40 "draws an ant scoping the screen", 0, NULL
44 #define Scale4Window 0.3
45 #define Scale4Iconic 0.4
47 #define sqr(A) ((A)*(A))
54 #include "grab-ximage.h"
57 GLXContext *glx_context;
59 trackball_state *trackball;
62 GLfloat max_tx, max_ty;
63 int mono, wire, ticks;
68 GLfloat spot_direction[3];
72 Bool waiting_for_image_p;
76 static antspotlightstruct *antspotlight = (antspotlightstruct *) NULL;
80 /* draw method for ant */
81 static Bool draw_ant(antspotlightstruct *mp,
82 const GLfloat *Material, int mono, int shadow,
83 float ant_step, Bool (*sphere)(float), Bool (*cone)(float))
86 float cos1 = cos(ant_step);
87 float cos2 = cos(ant_step + 2 * Pi / 3);
88 float cos3 = cos(ant_step + 4 * Pi / 3);
89 float sin1 = sin(ant_step);
90 float sin2 = sin(ant_step + 2 * Pi / 3);
91 float sin3 = sin(ant_step + 4 * Pi / 3);
93 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mp->mono ? MaterialGray5 : Material);
94 glEnable(GL_CULL_FACE);
97 if(!((*sphere)(0.18)))
99 glScalef(1, 1 / 1.3, 1);
100 glTranslatef(0.00, 0.30, 0.00);
101 if(!((*sphere)(0.2)))
104 glTranslatef(-0.05, 0.17, 0.05);
105 glRotatef(-90, 1, 0, 0);
106 glRotatef(-25, 0, 1, 0);
109 glTranslatef(0.00, 0.10, 0.00);
112 glRotatef(25, 0, 1, 0);
113 glRotatef(90, 1, 0, 0);
116 glTranslatef(0.15, -0.65, 0.05);
117 if(!((*sphere)(0.25)))
119 glScalef(1, 1 / 1.3, 1);
121 glDisable(GL_CULL_FACE);
123 glDisable(GL_LIGHTING);
127 glColor3fv(mp->mono ? MaterialGray5 : Material);
128 glVertex3f(0.00, 0.30, 0.00);
129 glColor3fv(MaterialGray);
130 glVertex3f(0.40, 0.70, 0.40);
131 glColor3fv(mp->mono ? MaterialGray5 : Material);
132 glVertex3f(0.00, 0.30, 0.00);
133 glColor3fv(MaterialGray);
134 glVertex3f(0.40, 0.70, -0.40);
139 glColor3fv(mp->mono ? MaterialGray6 : MaterialGray5);
140 glVertex3f(0.40, 0.70, 0.40);
141 glVertex3f(0.40, 0.70, -0.40);
146 glBegin(GL_LINE_STRIP);
147 glColor3fv(mp->mono ? MaterialGray5 : Material);
148 glVertex3f(0.00, 0.05, 0.18);
149 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
150 glColor3fv(MaterialGray);
151 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
154 /* LEFT-CENTER ARM */
155 glBegin(GL_LINE_STRIP);
156 glColor3fv(mp->mono ? MaterialGray5 : Material);
157 glVertex3f(0.00, 0.00, 0.18);
158 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
159 glColor3fv(MaterialGray);
160 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
164 glBegin(GL_LINE_STRIP);
165 glColor3fv(mp->mono ? MaterialGray5 : Material);
166 glVertex3f(0.00, -0.05, 0.18);
167 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
168 glColor3fv(MaterialGray);
169 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
172 /* RIGHT-FRONT ARM */
173 glBegin(GL_LINE_STRIP);
174 glColor3fv(mp->mono ? MaterialGray5 : Material);
175 glVertex3f(0.00, 0.05, -0.18);
176 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
177 glColor3fv(MaterialGray);
178 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
181 /* RIGHT-CENTER ARM */
182 glBegin(GL_LINE_STRIP);
183 glColor3fv(mp->mono ? MaterialGray5 : Material);
184 glVertex3f(0.00, 0.00, -0.18);
185 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
186 glColor3fv(MaterialGray);
187 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
191 glBegin(GL_LINE_STRIP);
192 glColor3fv(mp->mono ? MaterialGray5 : Material);
193 glVertex3f(0.00, -0.05, -0.18);
194 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
195 glColor3fv(MaterialGray);
196 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
201 glColor3fv(MaterialGray5);
202 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
203 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
204 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
205 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
206 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
207 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
211 glEnable(GL_LIGHTING);
217 static Bool mySphere(float radius)
219 GLUquadricObj *quadObj;
221 if((quadObj = gluNewQuadric()) == 0)
224 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
225 gluSphere(quadObj, radius, 16, 16);
226 gluDeleteQuadric(quadObj);
231 /* silhouette sphere */
232 static Bool mySphere2(float radius)
234 GLUquadricObj *quadObj;
236 if((quadObj = gluNewQuadric()) == 0)
238 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);
239 gluSphere(quadObj, radius, 16, 8);
240 gluDeleteQuadric(quadObj);
246 static Bool myCone2(float radius) { return True; }
248 static void draw_board(antspotlightstruct *mp)
251 double cutoff = Pi/3.0;
255 glEnable(GL_TEXTURE_2D);
256 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
257 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
261 /* center is roughly spotlight position */
262 center[0] = mp->ant->position[0];/* + cos(ant->direction); */
264 center[2] = mp->ant->position[2];/* - 0.7*sin(ant->direction);*/
266 centertex[0] = (mp->boardsize/2.0+center[0]) * mp->max_tx / mp->boardsize;
267 centertex[1] = mp->max_ty - ((mp->boardsize/2.0+center[2]) * mp->max_ty / mp->boardsize);
269 /* glPolygonMode(GL_FRONT, GL_LINE); */
270 /* glDisable(GL_TEXTURE_2D); */
273 the vertices determined here should correspond to the illuminated
274 board. ideally the code adapts vertex distribution to the
275 intensity and shape of the light.
277 i should be finding the intersection of the cone of light and
280 for(i = -8; i < 8; ++i) {
282 double theta1, theta2;
284 glBegin(GL_TRIANGLE_STRIP);
285 glNormal3f(0.0, 1.0, 0.0);
287 glTexCoord2f(centertex[0], centertex[1]);
288 glVertex3f(center[0], 0.01, center[2]);
290 /* watch those constants */
291 theta1 = mp->ant->direction + i*(cutoff/8);
292 theta2 = mp->ant->direction + (i+1)*(cutoff/8);
294 for(j = 1; j <= 40; ++j) {
295 double point[3], tex[2];
296 /* double fj = pow(1.05, j) - 1.0;*/
298 point[0] = center[0] + fj*cos(theta1);
300 point[2] = center[2] - fj*sin(theta1);
302 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
303 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
305 glTexCoord2f(tex[0], tex[1]);
306 glVertex3f(point[0], point[1], point[2]);
308 point[0] = center[0] + fj*cos(theta2);
310 point[2] = center[2] - fj*sin(theta2);
312 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
313 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
315 glTexCoord2f(tex[0], tex[1]);
316 glVertex3f(point[0], point[1], point[2]);
322 glDisable(GL_TEXTURE_2D);
325 /* return euclidean distance between two points */
326 static double distance(double x[3], double y[3])
328 double dx = x[0] - y[0];
329 double dz = x[2] - y[2];
330 return sqrt(dx*dx + dz*dz);
333 /* determine a new goal */
334 static void find_goal(antspotlightstruct *mp)
337 mp->ant->goal[0] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
338 mp->ant->goal[1] = 0.0;
339 mp->ant->goal[2] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
341 while(distance(mp->ant->position, mp->ant->goal) < 2.0);
344 /* construct our ant */
345 static void build_ant(antspotlightstruct *mp)
347 mp->ant = (Ant *) malloc(sizeof (Ant));
348 mp->ant->position[0] = 0.0;
349 mp->ant->position[1] = 0.0;
350 mp->ant->position[2] = 0.0;
351 mp->ant->direction = 0.0;
352 mp->ant->velocity = 0.02;
353 mp->ant->material = MaterialGray5;
360 static double sign(double d)
362 return d < 0.0 ? -1.0 : 1.0;
365 static double min(double a, double b)
367 return a < b ? a : b;
371 static double max(double a, double b)
373 return a > b ? a : b;
377 /* find a new goal and reset steps */
378 static void reset_ant(antspotlightstruct *mp)
383 /* draw ant composed of skeleton and glass */
384 static void show_ant(antspotlightstruct *mp)
389 /* move into position */
390 glTranslatef(mp->ant->position[0], 0.33, mp->ant->position[2]);
391 glRotatef(180.0 + mp->ant->direction*180.0/Pi, 0.0, 1.0, 0.0);
392 glRotatef(90.0, 0.0, 0.0, 1.0);
395 draw_ant(mp, mp->ant->material, mp->mono, 0, mp->ant->step, mySphere2, myCone2);
398 if(!mp->wire && !mp->mono) {
400 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGrayB);
401 glColor4fv(MaterialGrayB);
402 draw_ant(mp, MaterialGrayB, mp->mono, 0, mp->ant->step, mySphere, myCone2);
409 static void draw_antspotlight_strip(ModeInfo *mi)
411 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
413 /* compute spotlight position and direction */
414 GLfloat light1_position[4];
416 light1_position[0] = mp->ant->position[0] + 0.7*cos(mp->ant->direction);
417 light1_position[1] = 0.5;
418 light1_position[2] = mp->ant->position[2] - 0.7*sin(mp->ant->direction);
419 light1_position[3] = 1.0;
421 mp->spot_direction[0] = cos(mp->ant->direction);
422 mp->spot_direction[1] = -0.5;
423 mp->spot_direction[2] = -sin(mp->ant->direction);
425 glLightfv(GL_LIGHT2, GL_POSITION, light1_position);
426 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, mp->spot_direction);
429 glDisable(GL_LIGHT0);
430 glDisable(GL_LIGHT1);
438 glDisable(GL_LIGHT2);
445 /* near goal, bend path towards next step */
446 if(distance(mp->ant->position, mp->ant->goal) < 0.2) {
450 /* move toward goal, correct ant direction if required */
454 double dx = mp->ant->goal[0] - mp->ant->position[0];
455 double dz = -(mp->ant->goal[2] - mp->ant->position[2]);
456 double theta, ideal, dt;
458 if(fabs(dx) > EPSILON) {
464 theta = dz > 0.0 ? (1.0/2.0)*Pi : (3.0/2.0)*Pi;
469 ideal = theta - mp->ant->direction;
473 /* compute correction */
474 dt = sign(ideal) * min(fabs(ideal), Pi/100.0);
475 mp->ant->direction += dt;
476 while(mp->ant->direction < 0.0)
477 mp->ant->direction += 2*Pi;
478 while(mp->ant->direction > 2*Pi)
479 mp->ant->direction -= 2*Pi;
482 mp->ant->position[0] += mp->ant->velocity * cos(mp->ant->direction);
483 mp->ant->position[2] += mp->ant->velocity * sin(-mp->ant->direction);
484 mp->ant->step += 10*mp->ant->velocity;
485 while(mp->ant->step > 2*Pi)
486 mp->ant->step -= 2*Pi;
489 ENTRYPOINT void reshape_antspotlight(ModeInfo * mi, int width, int height)
491 double h = (GLfloat) height / (GLfloat) width;
493 glViewport(0, 0, width, height);
494 glMatrixMode(GL_PROJECTION);
497 gluPerspective(45, 1/h, 1.0, 25.0);
499 glMatrixMode(GL_MODELVIEW);
504 /* lighting variables */
505 static const GLfloat front_shininess[] = {60.0};
506 static const GLfloat front_specular[] = {0.8, 0.8, 0.8, 1.0};
507 static const GLfloat ambient[] = {0.4, 0.4, 0.4, 1.0};
508 /*static const GLfloat ambient2[] = {0.0, 0.0, 0.0, 0.0};*/
509 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
510 static const GLfloat position0[] = {1.0, 5.0, 1.0, 0.0};
511 static const GLfloat position1[] = {-1.0, -5.0, 1.0, 0.0};
512 /*static const GLfloat lmodel_ambient[] = {0.8, 0.8, 0.8, 1.0};*/
513 static const GLfloat lmodel_twoside[] = {GL_TRUE};
514 static const GLfloat spotlight_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
515 static const GLfloat spotlight_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
517 static void pinit(void)
520 glClearColor(0.0, 0.0, 0.0, 1.0);
522 /* setup twoside lighting */
523 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
524 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
525 glLightfv(GL_LIGHT0, GL_POSITION, position0);
526 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
527 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
528 glLightfv(GL_LIGHT1, GL_POSITION, position1);
529 /* glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); */
530 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
531 glEnable(GL_LIGHTING);
535 /* setup spotlight */
536 glLightfv(GL_LIGHT2, GL_AMBIENT, spotlight_ambient);
537 glLightfv(GL_LIGHT2, GL_DIFFUSE, spotlight_diffuse);
538 glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.1);
539 glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.05);
540 glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0);
541 glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0);
542 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 3.0);
544 glEnable(GL_NORMALIZE);
548 /* setup material properties */
549 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
550 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
552 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
553 glShadeModel(GL_SMOOTH);
554 /* glShadeModel(GL_FLAT); */
555 glEnable(GL_DEPTH_TEST);
558 /* cleanup routine */
559 ENTRYPOINT void release_antspotlight(ModeInfo * mi)
563 free((void *) antspotlight);
564 antspotlight = (antspotlightstruct *) NULL;
570 #define MAX_MAGNIFICATION 10
571 #define max(a, b) a < b ? b : a
572 #define min(a, b) a < b ? a : b
575 ENTRYPOINT Bool antspotlight_handle_event(ModeInfo *mi, XEvent *event)
577 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
579 switch(event->xany.type) {
582 switch(event->xbutton.button) {
585 mp->button_down_p = True;
586 gltrackball_start(mp->trackball,
587 event->xbutton.x, event->xbutton.y,
588 MI_WIDTH (mi), MI_HEIGHT (mi));
592 mp->mag = max(mp->mag-1, 1);
596 mp->mag = min(mp->mag+1, MAX_MAGNIFICATION);
604 switch(event->xbutton.button) {
606 mp->button_down_p = False;
613 if(mp->button_down_p)
614 gltrackball_track(mp->trackball,
615 event->xmotion.x, event->xmotion.y,
616 MI_WIDTH (mi), MI_HEIGHT (mi));
627 image_loaded_cb (const char *filename, XRectangle *geometry,
628 int image_width, int image_height,
629 int texture_width, int texture_height,
632 antspotlightstruct *mp = (antspotlightstruct *) closure;
634 mp->max_tx = (GLfloat) image_width / texture_width;
635 mp->max_ty = (GLfloat) image_height / texture_height;
637 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
638 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
639 (mp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
641 mp->waiting_for_image_p = False;
646 static void get_snapshot(ModeInfo *modeinfo)
648 antspotlightstruct *mp = &antspotlight[MI_SCREEN(modeinfo)];
650 if (MI_IS_WIREFRAME(modeinfo))
653 mp->waiting_for_image_p = True;
655 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
656 *mp->glx_context, 0, 0, mp->mipmap_p,
657 mp->screentexture, image_loaded_cb, mp);
661 ENTRYPOINT void init_antspotlight(ModeInfo *mi)
663 double rot_speed = 0.3;
665 antspotlightstruct *mp;
668 if((antspotlight = (antspotlightstruct *)
669 calloc(MI_NUM_SCREENS(mi), sizeof (antspotlightstruct))) == NULL)
672 mp = &antspotlight[MI_SCREEN(mi)];
673 mp->rot = make_rotator (rot_speed, rot_speed, rot_speed, 1, 0, True);
674 mp->trackball = gltrackball_init ();
676 if((mp->glx_context = init_GL(mi)) != NULL) {
677 reshape_antspotlight(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
678 glDrawBuffer(GL_BACK);
684 glGenTextures(1, &mp->screentexture);
685 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
689 mp->mono = MI_IS_MONO(mi);
690 mp->wire = MI_IS_WIREFRAME(mi);
695 ENTRYPOINT void draw_antspotlight(ModeInfo * mi)
697 antspotlightstruct *mp;
699 Display *display = MI_DISPLAY(mi);
700 Window window = MI_WINDOW(mi);
704 mp = &antspotlight[MI_SCREEN(mi)];
706 MI_IS_DRAWN(mi) = True;
711 /* Just keep running before the texture has come in. */
712 /* if (mp->waiting_for_image_p) return; */
714 glXMakeCurrent(display, window, *(mp->glx_context));
716 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
720 /* position camera */
722 /* follow focused ant */
723 glTranslatef(0.0, 0.0, -6.0 - mp->mag);
724 glRotatef(35.0, 1.0, 0.0, 0.0);
725 gltrackball_rotate(mp->trackball);
726 glTranslatef(-mp->ant->position[0], mp->ant->position[1], -mp->ant->position[2]);
728 /* stable position */
729 /* glTranslatef(0.0, 0.0, -10.0 - mag); */
730 /* gltrackball_rotate(mp->trackball); */
731 /* glRotatef(40.0, 1.0, 0.0, 0.0); */
732 /* glRotatef(20.0, 0.0, 1.0, 0.0); */
734 draw_antspotlight_strip(mi);
740 if (MI_IS_FPS(mi)) do_fps (mi);
743 glXSwapBuffers(display, window);
747 ENTRYPOINT void change_antspotlight(ModeInfo * mi)
749 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
751 if (!mp->glx_context)
754 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
757 #endif /* !STANDALONE */
759 XSCREENSAVER_MODULE ("AntSpotlight", antspotlight)