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(ModeInfo *mi, 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 /* Apparently this is a performance killer on many systems...
94 glEnable(GL_POLYGON_SMOOTH);
97 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
99 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mp->mono ? MaterialGray5 : Material);
100 glEnable(GL_CULL_FACE);
103 if(!((*sphere)(0.18)))
105 glScalef(1, 1 / 1.3, 1);
106 glTranslatef(0.00, 0.30, 0.00);
107 if(!((*sphere)(0.2)))
110 glTranslatef(-0.05, 0.17, 0.05);
111 glRotatef(-90, 1, 0, 0);
112 glRotatef(-25, 0, 1, 0);
115 glTranslatef(0.00, 0.10, 0.00);
118 glRotatef(25, 0, 1, 0);
119 glRotatef(90, 1, 0, 0);
122 glTranslatef(0.15, -0.65, 0.05);
123 if(!((*sphere)(0.25)))
125 glScalef(1, 1 / 1.3, 1);
127 glDisable(GL_CULL_FACE);
129 glDisable(GL_LIGHTING);
132 glEnable(GL_LINE_SMOOTH);
134 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 glColor3fv(mp->mono ? MaterialGray5 : Material);
138 glVertex3f(0.00, 0.30, 0.00);
139 glColor3fv(MaterialGray);
140 glVertex3f(0.40, 0.70, 0.40);
142 glColor3fv(mp->mono ? MaterialGray5 : Material);
143 glVertex3f(0.00, 0.30, 0.00);
144 glColor3fv(MaterialGray);
145 glVertex3f(0.40, 0.70, -0.40);
151 glColor3fv(mp->mono ? MaterialGray6 : MaterialGray5);
152 glVertex3f(0.40, 0.70, 0.40);
154 glVertex3f(0.40, 0.70, -0.40);
160 glBegin(GL_LINE_STRIP);
161 glColor3fv(mp->mono ? MaterialGray5 : Material);
162 glVertex3f(0.00, 0.05, 0.18);
163 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
165 glColor3fv(MaterialGray);
166 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
170 /* LEFT-CENTER ARM */
171 glBegin(GL_LINE_STRIP);
172 glColor3fv(mp->mono ? MaterialGray5 : Material);
173 glVertex3f(0.00, 0.00, 0.18);
174 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
176 glColor3fv(MaterialGray);
177 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
182 glBegin(GL_LINE_STRIP);
183 glColor3fv(mp->mono ? MaterialGray5 : Material);
184 glVertex3f(0.00, -0.05, 0.18);
185 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
187 glColor3fv(MaterialGray);
188 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
192 /* RIGHT-FRONT ARM */
193 glBegin(GL_LINE_STRIP);
194 glColor3fv(mp->mono ? MaterialGray5 : Material);
195 glVertex3f(0.00, 0.05, -0.18);
196 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
198 glColor3fv(MaterialGray);
199 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
203 /* RIGHT-CENTER ARM */
204 glBegin(GL_LINE_STRIP);
205 glColor3fv(mp->mono ? MaterialGray5 : Material);
206 glVertex3f(0.00, 0.00, -0.18);
207 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
209 glColor3fv(MaterialGray);
210 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
215 glBegin(GL_LINE_STRIP);
216 glColor3fv(mp->mono ? MaterialGray5 : Material);
217 glVertex3f(0.00, -0.05, -0.18);
218 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
220 glColor3fv(MaterialGray);
221 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
227 glColor3fv(MaterialGray5);
228 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
229 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
230 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
231 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
232 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
233 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
234 mi->polygon_count += 6;
238 glEnable(GL_LIGHTING);
244 static Bool mySphere(float radius)
246 GLUquadricObj *quadObj;
248 if((quadObj = gluNewQuadric()) == 0)
251 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
252 gluSphere(quadObj, radius, 16, 16);
253 gluDeleteQuadric(quadObj);
258 /* silhouette sphere */
259 static Bool mySphere2(float radius)
261 GLUquadricObj *quadObj;
263 if((quadObj = gluNewQuadric()) == 0)
265 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);
266 gluSphere(quadObj, radius, 16, 8);
267 gluDeleteQuadric(quadObj);
273 static Bool myCone2(float radius) { return True; }
275 static void draw_board(ModeInfo *mi, antspotlightstruct *mp)
278 double cutoff = Pi/3.0;
282 glEnable(GL_TEXTURE_2D);
283 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
284 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
288 /* center is roughly spotlight position */
289 center[0] = mp->ant->position[0];/* + cos(ant->direction); */
291 center[2] = mp->ant->position[2];/* - 0.7*sin(ant->direction);*/
293 centertex[0] = (mp->boardsize/2.0+center[0]) * mp->max_tx / mp->boardsize;
294 centertex[1] = mp->max_ty - ((mp->boardsize/2.0+center[2]) * mp->max_ty / mp->boardsize);
296 /* glPolygonMode(GL_FRONT, GL_LINE); */
297 /* glDisable(GL_TEXTURE_2D); */
300 the vertices determined here should correspond to the illuminated
301 board. ideally the code adapts vertex distribution to the
302 intensity and shape of the light.
304 i should be finding the intersection of the cone of light and
307 for(i = -12; i < 12; ++i) {
309 double theta1, theta2;
311 glBegin(GL_TRIANGLE_STRIP);
312 glNormal3f(0.0, 1.0, 0.0);
314 glTexCoord2f(centertex[0], centertex[1]);
315 glVertex3f(center[0], 0.01, center[2]);
317 /* watch those constants */
318 theta1 = mp->ant->direction + i*(cutoff/8);
319 theta2 = mp->ant->direction + (i+1)*(cutoff/8);
321 for(j = 1; j <= 64; ++j) {
322 double point[3], tex[2];
323 /* double fj = pow(1.05, j) - 1.0;*/
325 point[0] = center[0] + fj*cos(theta1);
327 point[2] = center[2] - fj*sin(theta1);
329 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
330 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
332 glTexCoord2f(tex[0], tex[1]);
333 glVertex3f(point[0], point[1], point[2]);
335 point[0] = center[0] + fj*cos(theta2);
337 point[2] = center[2] - fj*sin(theta2);
339 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
340 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
342 glTexCoord2f(tex[0], tex[1]);
343 glVertex3f(point[0], point[1], point[2]);
350 glDisable(GL_TEXTURE_2D);
353 /* return euclidean distance between two points */
354 static double distance(double x[3], double y[3])
356 double dx = x[0] - y[0];
357 double dz = x[2] - y[2];
358 return sqrt(dx*dx + dz*dz);
361 /* determine a new goal */
362 static void find_goal(antspotlightstruct *mp)
365 mp->ant->goal[0] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
366 mp->ant->goal[1] = 0.0;
367 mp->ant->goal[2] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
369 while(distance(mp->ant->position, mp->ant->goal) < 2.0);
372 /* construct our ant */
373 static void build_ant(antspotlightstruct *mp)
375 mp->ant = (Ant *) malloc(sizeof (Ant));
376 mp->ant->position[0] = 0.0;
377 mp->ant->position[1] = 0.0;
378 mp->ant->position[2] = 0.0;
379 mp->ant->direction = 0.0;
380 mp->ant->velocity = 0.02;
381 mp->ant->material = MaterialGray5;
388 static double sign(double d)
390 return d < 0.0 ? -1.0 : 1.0;
393 static double min(double a, double b)
395 return a < b ? a : b;
399 static double max(double a, double b)
401 return a > b ? a : b;
405 /* find a new goal and reset steps */
406 static void reset_ant(antspotlightstruct *mp)
411 /* draw ant composed of skeleton and glass */
412 static void show_ant(ModeInfo *mi, antspotlightstruct *mp)
417 /* move into position */
418 glTranslatef(mp->ant->position[0], 0.33, mp->ant->position[2]);
419 glRotatef(180.0 + mp->ant->direction*180.0/Pi, 0.0, 1.0, 0.0);
420 glRotatef(90.0, 0.0, 0.0, 1.0);
423 draw_ant(mi, mp, mp->ant->material, mp->mono, 0, mp->ant->step, mySphere2, myCone2);
426 if(!mp->wire && !mp->mono) {
428 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGrayB);
429 glColor4fv(MaterialGrayB);
430 draw_ant(mi, mp, MaterialGrayB, mp->mono, 0, mp->ant->step, mySphere, myCone2);
437 static void draw_antspotlight_strip(ModeInfo *mi)
439 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
441 /* compute spotlight position and direction */
442 GLfloat light1_position[4];
444 light1_position[0] = mp->ant->position[0] + 0.7*cos(mp->ant->direction);
445 light1_position[1] = 0.5;
446 light1_position[2] = mp->ant->position[2] - 0.7*sin(mp->ant->direction);
447 light1_position[3] = 1.0;
449 mp->spot_direction[0] = cos(mp->ant->direction);
450 mp->spot_direction[1] = -0.5;
451 mp->spot_direction[2] = -sin(mp->ant->direction);
453 glLightfv(GL_LIGHT2, GL_POSITION, light1_position);
454 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, mp->spot_direction);
457 glDisable(GL_LIGHT0);
458 glDisable(GL_LIGHT1);
466 glDisable(GL_LIGHT2);
473 /* near goal, bend path towards next step */
474 if(distance(mp->ant->position, mp->ant->goal) < 0.2) {
478 if(random()%100 == 0) {
483 /* move toward goal, correct ant direction if required */
487 double dx = mp->ant->goal[0] - mp->ant->position[0];
488 double dz = -(mp->ant->goal[2] - mp->ant->position[2]);
489 double theta, ideal, dt;
491 if(fabs(dx) > EPSILON) {
497 theta = dz > 0.0 ? (1.0/2.0)*Pi : (3.0/2.0)*Pi;
502 ideal = theta - mp->ant->direction;
506 /* compute correction */
507 dt = sign(ideal) * min(fabs(ideal), Pi/100.0);
508 mp->ant->direction += dt;
509 while(mp->ant->direction < 0.0)
510 mp->ant->direction += 2*Pi;
511 while(mp->ant->direction > 2*Pi)
512 mp->ant->direction -= 2*Pi;
515 mp->ant->position[0] += mp->ant->velocity * cos(mp->ant->direction);
516 mp->ant->position[2] += mp->ant->velocity * sin(-mp->ant->direction);
517 mp->ant->step += 10*mp->ant->velocity;
518 while(mp->ant->step > 2*Pi)
519 mp->ant->step -= 2*Pi;
522 ENTRYPOINT void reshape_antspotlight(ModeInfo * mi, int width, int height)
524 double h = (GLfloat) height / (GLfloat) width;
526 glViewport(0, 0, width, height);
527 glMatrixMode(GL_PROJECTION);
530 gluPerspective(45, 1/h, 1.0, 25.0);
532 glMatrixMode(GL_MODELVIEW);
537 /* lighting variables */
538 static const GLfloat front_shininess[] = {60.0};
539 static const GLfloat front_specular[] = {0.8, 0.8, 0.8, 1.0};
540 static const GLfloat ambient[] = {0.4, 0.4, 0.4, 1.0};
541 /*static const GLfloat ambient2[] = {0.0, 0.0, 0.0, 0.0};*/
542 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
543 static const GLfloat position0[] = {1.0, 5.0, 1.0, 0.0};
544 static const GLfloat position1[] = {-1.0, -5.0, 1.0, 0.0};
545 /*static const GLfloat lmodel_ambient[] = {0.8, 0.8, 0.8, 1.0};*/
546 static const GLfloat lmodel_twoside[] = {GL_TRUE};
547 static const GLfloat spotlight_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
548 static const GLfloat spotlight_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
550 static void pinit(void)
554 /* setup twoside lighting */
555 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
556 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
557 glLightfv(GL_LIGHT0, GL_POSITION, position0);
558 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
559 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
560 glLightfv(GL_LIGHT1, GL_POSITION, position1);
562 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, spotlight_ambient);
563 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
564 glEnable(GL_LIGHTING);
568 /* setup spotlight */
569 glLightfv(GL_LIGHT2, GL_AMBIENT, spotlight_ambient);
570 glLightfv(GL_LIGHT2, GL_DIFFUSE, spotlight_diffuse);
571 glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.1);
572 glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.05);
573 glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0);
574 glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0);
575 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 3.0);
577 glEnable(GL_NORMALIZE);
581 /* setup material properties */
582 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
583 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
585 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
586 glShadeModel(GL_SMOOTH);
587 /* glShadeModel(GL_FLAT); */
588 glEnable(GL_DEPTH_TEST);
591 /* cleanup routine */
592 ENTRYPOINT void release_antspotlight(ModeInfo * mi)
596 free((void *) antspotlight);
597 antspotlight = (antspotlightstruct *) NULL;
603 #define MAX_MAGNIFICATION 10
604 #define max(a, b) a < b ? b : a
605 #define min(a, b) a < b ? a : b
608 ENTRYPOINT Bool antspotlight_handle_event(ModeInfo *mi, XEvent *event)
610 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
612 switch(event->xany.type) {
615 switch(event->xbutton.button) {
618 mp->button_down_p = True;
619 gltrackball_start(mp->trackball,
620 event->xbutton.x, event->xbutton.y,
621 MI_WIDTH (mi), MI_HEIGHT (mi));
625 mp->mag = max(mp->mag-1, 1);
629 mp->mag = min(mp->mag+1, MAX_MAGNIFICATION);
637 switch(event->xbutton.button) {
639 mp->button_down_p = False;
646 if(mp->button_down_p)
647 gltrackball_track(mp->trackball,
648 event->xmotion.x, event->xmotion.y,
649 MI_WIDTH (mi), MI_HEIGHT (mi));
660 image_loaded_cb (const char *filename, XRectangle *geometry,
661 int image_width, int image_height,
662 int texture_width, int texture_height,
665 antspotlightstruct *mp = (antspotlightstruct *) closure;
667 mp->max_tx = (GLfloat) image_width / texture_width;
668 mp->max_ty = (GLfloat) image_height / texture_height;
670 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
671 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
672 (mp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
674 mp->waiting_for_image_p = False;
679 static void get_snapshot(ModeInfo *modeinfo)
681 antspotlightstruct *mp = &antspotlight[MI_SCREEN(modeinfo)];
683 if (MI_IS_WIREFRAME(modeinfo))
686 mp->waiting_for_image_p = True;
688 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
689 *mp->glx_context, 0, 0, mp->mipmap_p,
690 mp->screentexture, image_loaded_cb, mp);
694 ENTRYPOINT void init_antspotlight(ModeInfo *mi)
696 double rot_speed = 0.3;
698 antspotlightstruct *mp;
701 if((antspotlight = (antspotlightstruct *)
702 calloc(MI_NUM_SCREENS(mi), sizeof (antspotlightstruct))) == NULL)
705 mp = &antspotlight[MI_SCREEN(mi)];
706 mp->rot = make_rotator (rot_speed, rot_speed, rot_speed, 1, 0, True);
707 mp->trackball = gltrackball_init ();
709 if((mp->glx_context = init_GL(mi)) != NULL) {
710 reshape_antspotlight(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
711 glDrawBuffer(GL_BACK);
717 glGenTextures(1, &mp->screentexture);
718 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
722 mp->mono = MI_IS_MONO(mi);
723 mp->wire = MI_IS_WIREFRAME(mi);
728 ENTRYPOINT void draw_antspotlight(ModeInfo * mi)
730 antspotlightstruct *mp;
732 Display *display = MI_DISPLAY(mi);
733 Window window = MI_WINDOW(mi);
737 mp = &antspotlight[MI_SCREEN(mi)];
739 MI_IS_DRAWN(mi) = True;
744 mi->polygon_count = 0;
746 /* Just keep running before the texture has come in. */
747 /* if (mp->waiting_for_image_p) return; */
749 glXMakeCurrent(display, window, *(mp->glx_context));
751 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
755 /* position camera */
757 /* follow focused ant */
758 glTranslatef(0.0, 0.0, -6.0 - mp->mag);
759 glRotatef(35.0, 1.0, 0.0, 0.0);
760 gltrackball_rotate(mp->trackball);
761 glTranslatef(-mp->ant->position[0], mp->ant->position[1], -mp->ant->position[2]);
763 /* stable position */
764 /* glTranslatef(0.0, 0.0, -10.0 - mag); */
765 /* gltrackball_rotate(mp->trackball); */
766 /* glRotatef(40.0, 1.0, 0.0, 0.0); */
767 /* glRotatef(20.0, 0.0, 1.0, 0.0); */
769 draw_antspotlight_strip(mi);
775 if (MI_IS_FPS(mi)) do_fps (mi);
778 glXSwapBuffers(display, window);
782 ENTRYPOINT void change_antspotlight(ModeInfo * mi)
784 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
786 if (!mp->glx_context)
789 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
792 #endif /* !STANDALONE */
794 XSCREENSAVER_MODULE ("AntSpotlight", antspotlight)