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"
31 # include <X11/Xlib.h>
38 #endif /* HAVE_JWZGLES */
43 #include "gltrackball.h"
45 ENTRYPOINT ModeSpecOpt antspotlight_opts = {
46 0, NULL, 0, NULL, NULL
50 ModStruct antspotlight_description = {
51 "antspotlight", "init_antspotlight", "draw_antspotlight",
52 "release_antspotlight", "draw_antspotlight", "change_antspotlight",
53 (char *) NULL, &antspotlight_opts, 1000, 1, 1, 1, 4, 1.0, "",
54 "draws an ant scoping the screen", 0, NULL
58 #define Scale4Window 0.3
59 #define Scale4Iconic 0.4
61 #define sqr(A) ((A)*(A))
68 #include "grab-ximage.h"
71 GLXContext *glx_context;
73 trackball_state *trackball;
76 GLfloat max_tx, max_ty;
77 int mono, wire, ticks;
82 GLfloat spot_direction[3];
86 Bool waiting_for_image_p;
90 static antspotlightstruct *antspotlight = (antspotlightstruct *) NULL;
94 /* draw method for ant */
95 static Bool draw_ant(ModeInfo *mi, antspotlightstruct *mp,
96 const GLfloat *Material, int mono, int shadow,
97 float ant_step, Bool (*sphere)(float), Bool (*cone)(float))
100 float cos1 = cos(ant_step);
101 float cos2 = cos(ant_step + 2 * Pi / 3);
102 float cos3 = cos(ant_step + 4 * Pi / 3);
103 float sin1 = sin(ant_step);
104 float sin2 = sin(ant_step + 2 * Pi / 3);
105 float sin3 = sin(ant_step + 4 * Pi / 3);
107 /* Apparently this is a performance killer on many systems...
108 glEnable(GL_POLYGON_SMOOTH);
111 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
113 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mp->mono ? MaterialGray5 : Material);
114 glEnable(GL_CULL_FACE);
117 if(!((*sphere)(0.18)))
119 glScalef(1, 1 / 1.3, 1);
120 glTranslatef(0.00, 0.30, 0.00);
121 if(!((*sphere)(0.2)))
124 glTranslatef(-0.05, 0.17, 0.05);
125 glRotatef(-90, 1, 0, 0);
126 glRotatef(-25, 0, 1, 0);
129 glTranslatef(0.00, 0.10, 0.00);
132 glRotatef(25, 0, 1, 0);
133 glRotatef(90, 1, 0, 0);
136 glTranslatef(0.15, -0.65, 0.05);
137 if(!((*sphere)(0.25)))
139 glScalef(1, 1 / 1.3, 1);
141 glDisable(GL_CULL_FACE);
143 glDisable(GL_LIGHTING);
146 glEnable(GL_LINE_SMOOTH);
148 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
151 glColor3fv(mp->mono ? MaterialGray5 : Material);
152 glVertex3f(0.00, 0.30, 0.00);
153 glColor3fv(MaterialGray);
154 glVertex3f(0.40, 0.70, 0.40);
156 glColor3fv(mp->mono ? MaterialGray5 : Material);
157 glVertex3f(0.00, 0.30, 0.00);
158 glColor3fv(MaterialGray);
159 glVertex3f(0.40, 0.70, -0.40);
165 glColor3fv(mp->mono ? MaterialGray6 : MaterialGray5);
166 glVertex3f(0.40, 0.70, 0.40);
168 glVertex3f(0.40, 0.70, -0.40);
174 glBegin(GL_LINE_STRIP);
175 glColor3fv(mp->mono ? MaterialGray5 : Material);
176 glVertex3f(0.00, 0.05, 0.18);
177 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
179 glColor3fv(MaterialGray);
180 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
184 /* LEFT-CENTER ARM */
185 glBegin(GL_LINE_STRIP);
186 glColor3fv(mp->mono ? MaterialGray5 : Material);
187 glVertex3f(0.00, 0.00, 0.18);
188 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
190 glColor3fv(MaterialGray);
191 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
196 glBegin(GL_LINE_STRIP);
197 glColor3fv(mp->mono ? MaterialGray5 : Material);
198 glVertex3f(0.00, -0.05, 0.18);
199 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
201 glColor3fv(MaterialGray);
202 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
206 /* RIGHT-FRONT ARM */
207 glBegin(GL_LINE_STRIP);
208 glColor3fv(mp->mono ? MaterialGray5 : Material);
209 glVertex3f(0.00, 0.05, -0.18);
210 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
212 glColor3fv(MaterialGray);
213 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
217 /* RIGHT-CENTER ARM */
218 glBegin(GL_LINE_STRIP);
219 glColor3fv(mp->mono ? MaterialGray5 : Material);
220 glVertex3f(0.00, 0.00, -0.18);
221 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
223 glColor3fv(MaterialGray);
224 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
229 glBegin(GL_LINE_STRIP);
230 glColor3fv(mp->mono ? MaterialGray5 : Material);
231 glVertex3f(0.00, -0.05, -0.18);
232 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
234 glColor3fv(MaterialGray);
235 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
241 glColor3fv(MaterialGray5);
242 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
243 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
244 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
245 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
246 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
247 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
248 mi->polygon_count += 6;
252 glEnable(GL_LIGHTING);
258 static Bool mySphere(float radius)
261 GLUquadricObj *quadObj;
263 if((quadObj = gluNewQuadric()) == 0)
266 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
267 gluSphere(quadObj, radius, 16, 16);
268 gluDeleteQuadric(quadObj);
271 glScalef (radius, radius, radius);
272 glRotatef (90, 1, 0, 0);
273 unit_sphere (16, 16, False);
279 /* silhouette sphere */
280 static Bool mySphere2(float radius)
283 GLUquadricObj *quadObj;
285 if((quadObj = gluNewQuadric()) == 0)
287 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);
288 gluSphere(quadObj, radius, 16, 8);
289 gluDeleteQuadric(quadObj);
291 /* #### no GLU_LINE */
293 glScalef (radius, radius, radius);
294 glRotatef (90, 1, 0, 0);
295 unit_sphere (16, 16, True);
302 static Bool myCone2(float radius) { return True; }
304 static void draw_board(ModeInfo *mi, antspotlightstruct *mp)
307 double cutoff = Pi/3.0;
311 glEnable(GL_TEXTURE_2D);
312 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
313 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
317 /* center is roughly spotlight position */
318 center[0] = mp->ant->position[0];/* + cos(ant->direction); */
320 center[2] = mp->ant->position[2];/* - 0.7*sin(ant->direction);*/
322 centertex[0] = (mp->boardsize/2.0+center[0]) * mp->max_tx / mp->boardsize;
323 centertex[1] = mp->max_ty - ((mp->boardsize/2.0+center[2]) * mp->max_ty / mp->boardsize);
325 /* glPolygonMode(GL_FRONT, GL_LINE); */
326 /* glDisable(GL_TEXTURE_2D); */
329 the vertices determined here should correspond to the illuminated
330 board. ideally the code adapts vertex distribution to the
331 intensity and shape of the light.
333 i should be finding the intersection of the cone of light and
336 for(i = -12; i < 12; ++i) {
338 double theta1, theta2;
340 glBegin(GL_TRIANGLE_STRIP);
341 glNormal3f(0.0, 1.0, 0.0);
343 glTexCoord2f(centertex[0], centertex[1]);
344 glVertex3f(center[0], 0.01, center[2]);
346 /* watch those constants */
347 theta1 = mp->ant->direction + i*(cutoff/8);
348 theta2 = mp->ant->direction + (i+1)*(cutoff/8);
350 for(j = 1; j <= 64; ++j) {
351 double point[3], tex[2];
352 /* double fj = pow(1.05, j) - 1.0;*/
354 point[0] = center[0] + fj*cos(theta1);
356 point[2] = center[2] - fj*sin(theta1);
358 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
359 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
361 glTexCoord2f(tex[0], tex[1]);
362 glVertex3f(point[0], point[1], point[2]);
364 point[0] = center[0] + fj*cos(theta2);
366 point[2] = center[2] - fj*sin(theta2);
368 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
369 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
371 glTexCoord2f(tex[0], tex[1]);
372 glVertex3f(point[0], point[1], point[2]);
379 glDisable(GL_TEXTURE_2D);
382 /* return euclidean distance between two points */
383 static double distance(double x[3], double y[3])
385 double dx = x[0] - y[0];
386 double dz = x[2] - y[2];
387 return sqrt(dx*dx + dz*dz);
390 /* determine a new goal */
391 static void find_goal(antspotlightstruct *mp)
394 mp->ant->goal[0] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
395 mp->ant->goal[1] = 0.0;
396 mp->ant->goal[2] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
398 while(distance(mp->ant->position, mp->ant->goal) < 2.0);
401 /* construct our ant */
402 static void build_ant(antspotlightstruct *mp)
404 mp->ant = (Ant *) malloc(sizeof (Ant));
405 mp->ant->position[0] = 0.0;
406 mp->ant->position[1] = 0.0;
407 mp->ant->position[2] = 0.0;
408 mp->ant->direction = 0.0;
409 mp->ant->velocity = 0.02;
410 mp->ant->material = MaterialGray5;
417 static double sign(double d)
419 return d < 0.0 ? -1.0 : 1.0;
422 static double min(double a, double b)
424 return a < b ? a : b;
428 static double max(double a, double b)
430 return a > b ? a : b;
434 /* find a new goal and reset steps */
435 static void reset_ant(antspotlightstruct *mp)
440 /* draw ant composed of skeleton and glass */
441 static void show_ant(ModeInfo *mi, antspotlightstruct *mp)
446 /* move into position */
447 glTranslatef(mp->ant->position[0], 0.33, mp->ant->position[2]);
448 glRotatef(180.0 + mp->ant->direction*180.0/Pi, 0.0, 1.0, 0.0);
449 glRotatef(90.0, 0.0, 0.0, 1.0);
452 draw_ant(mi, mp, mp->ant->material, mp->mono, 0, mp->ant->step, mySphere2, myCone2);
455 if(!mp->wire && !mp->mono) {
457 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGrayB);
458 glColor4fv(MaterialGrayB);
459 draw_ant(mi, mp, MaterialGrayB, mp->mono, 0, mp->ant->step, mySphere, myCone2);
466 static void draw_antspotlight_strip(ModeInfo *mi)
468 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
470 /* compute spotlight position and direction */
471 GLfloat light1_position[4];
473 light1_position[0] = mp->ant->position[0] + 0.7*cos(mp->ant->direction);
474 light1_position[1] = 0.5;
475 light1_position[2] = mp->ant->position[2] - 0.7*sin(mp->ant->direction);
476 light1_position[3] = 1.0;
478 mp->spot_direction[0] = cos(mp->ant->direction);
479 mp->spot_direction[1] = -0.5;
480 mp->spot_direction[2] = -sin(mp->ant->direction);
482 glLightfv(GL_LIGHT2, GL_POSITION, light1_position);
483 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, mp->spot_direction);
486 glDisable(GL_LIGHT0);
487 glDisable(GL_LIGHT1);
495 glDisable(GL_LIGHT2);
502 /* near goal, bend path towards next step */
503 if(distance(mp->ant->position, mp->ant->goal) < 0.2) {
507 if(random()%100 == 0) {
512 /* move toward goal, correct ant direction if required */
516 double dx = mp->ant->goal[0] - mp->ant->position[0];
517 double dz = -(mp->ant->goal[2] - mp->ant->position[2]);
518 double theta, ideal, dt;
520 if(fabs(dx) > EPSILON) {
526 theta = dz > 0.0 ? (1.0/2.0)*Pi : (3.0/2.0)*Pi;
531 ideal = theta - mp->ant->direction;
535 /* compute correction */
536 dt = sign(ideal) * min(fabs(ideal), Pi/100.0);
537 mp->ant->direction += dt;
538 while(mp->ant->direction < 0.0)
539 mp->ant->direction += 2*Pi;
540 while(mp->ant->direction > 2*Pi)
541 mp->ant->direction -= 2*Pi;
544 mp->ant->position[0] += mp->ant->velocity * cos(mp->ant->direction);
545 mp->ant->position[2] += mp->ant->velocity * sin(-mp->ant->direction);
546 mp->ant->step += 10*mp->ant->velocity;
547 while(mp->ant->step > 2*Pi)
548 mp->ant->step -= 2*Pi;
551 ENTRYPOINT void reshape_antspotlight(ModeInfo * mi, int width, int height)
553 double h = (GLfloat) height / (GLfloat) width;
555 glViewport(0, 0, width, height);
556 glMatrixMode(GL_PROJECTION);
559 gluPerspective(45, 1/h, 1.0, 25.0);
561 glMatrixMode(GL_MODELVIEW);
566 /* lighting variables */
567 static const GLfloat front_shininess[] = {60.0};
568 static const GLfloat front_specular[] = {0.8, 0.8, 0.8, 1.0};
569 static const GLfloat ambient[] = {0.4, 0.4, 0.4, 1.0};
570 /*static const GLfloat ambient2[] = {0.0, 0.0, 0.0, 0.0};*/
571 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
572 static const GLfloat position0[] = {1.0, 5.0, 1.0, 0.0};
573 static const GLfloat position1[] = {-1.0, -5.0, 1.0, 0.0};
574 /*static const GLfloat lmodel_ambient[] = {0.8, 0.8, 0.8, 1.0};*/
575 static const GLfloat lmodel_twoside[] = {GL_TRUE};
576 static const GLfloat spotlight_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
577 static const GLfloat spotlight_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
579 static void pinit(void)
583 /* setup twoside lighting */
584 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
585 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
586 glLightfv(GL_LIGHT0, GL_POSITION, position0);
587 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
588 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
589 glLightfv(GL_LIGHT1, GL_POSITION, position1);
591 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, spotlight_ambient);
592 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
593 glEnable(GL_LIGHTING);
597 /* setup spotlight */
598 glLightfv(GL_LIGHT2, GL_AMBIENT, spotlight_ambient);
599 glLightfv(GL_LIGHT2, GL_DIFFUSE, spotlight_diffuse);
600 glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.1);
601 glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.05);
602 glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0);
603 glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0);
604 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 3.0);
606 glEnable(GL_NORMALIZE);
610 /* setup material properties */
611 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
612 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
614 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
615 glShadeModel(GL_SMOOTH);
616 /* glShadeModel(GL_FLAT); */
617 glEnable(GL_DEPTH_TEST);
620 /* cleanup routine */
621 ENTRYPOINT void release_antspotlight(ModeInfo * mi)
625 free((void *) antspotlight);
626 antspotlight = (antspotlightstruct *) NULL;
632 #define MAX_MAGNIFICATION 10
633 #define max(a, b) a < b ? b : a
634 #define min(a, b) a < b ? a : b
637 ENTRYPOINT Bool antspotlight_handle_event(ModeInfo *mi, XEvent *event)
639 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
641 switch(event->xany.type) {
644 switch(event->xbutton.button) {
647 mp->button_down_p = True;
648 gltrackball_start(mp->trackball,
649 event->xbutton.x, event->xbutton.y,
650 MI_WIDTH (mi), MI_HEIGHT (mi));
654 mp->mag = max(mp->mag-1, 1);
658 mp->mag = min(mp->mag+1, MAX_MAGNIFICATION);
666 switch(event->xbutton.button) {
668 mp->button_down_p = False;
675 if(mp->button_down_p)
676 gltrackball_track(mp->trackball,
677 event->xmotion.x, event->xmotion.y,
678 MI_WIDTH (mi), MI_HEIGHT (mi));
689 image_loaded_cb (const char *filename, XRectangle *geometry,
690 int image_width, int image_height,
691 int texture_width, int texture_height,
694 antspotlightstruct *mp = (antspotlightstruct *) closure;
696 mp->max_tx = (GLfloat) image_width / texture_width;
697 mp->max_ty = (GLfloat) image_height / texture_height;
699 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
700 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
701 (mp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
703 mp->waiting_for_image_p = False;
708 static void get_snapshot(ModeInfo *modeinfo)
710 antspotlightstruct *mp = &antspotlight[MI_SCREEN(modeinfo)];
712 if (MI_IS_WIREFRAME(modeinfo))
715 mp->waiting_for_image_p = True;
717 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
718 *mp->glx_context, 0, 0, mp->mipmap_p,
719 mp->screentexture, image_loaded_cb, mp);
723 ENTRYPOINT void init_antspotlight(ModeInfo *mi)
725 double rot_speed = 0.3;
727 antspotlightstruct *mp;
730 if((antspotlight = (antspotlightstruct *)
731 calloc(MI_NUM_SCREENS(mi), sizeof (antspotlightstruct))) == NULL)
734 mp = &antspotlight[MI_SCREEN(mi)];
735 mp->rot = make_rotator (rot_speed, rot_speed, rot_speed, 1, 0, True);
736 mp->trackball = gltrackball_init ();
738 if((mp->glx_context = init_GL(mi)) != NULL) {
739 reshape_antspotlight(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
740 glDrawBuffer(GL_BACK);
746 glGenTextures(1, &mp->screentexture);
747 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
751 mp->mono = MI_IS_MONO(mi);
752 mp->wire = MI_IS_WIREFRAME(mi);
757 ENTRYPOINT void draw_antspotlight(ModeInfo * mi)
759 antspotlightstruct *mp;
761 Display *display = MI_DISPLAY(mi);
762 Window window = MI_WINDOW(mi);
766 mp = &antspotlight[MI_SCREEN(mi)];
768 MI_IS_DRAWN(mi) = True;
773 mi->polygon_count = 0;
775 /* Just keep running before the texture has come in. */
776 /* if (mp->waiting_for_image_p) return; */
778 glXMakeCurrent(display, window, *(mp->glx_context));
780 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
783 glRotatef(current_device_rotation(), 0, 0, 1);
785 /* position camera */
787 /* follow focused ant */
788 glTranslatef(0.0, 0.0, -6.0 - mp->mag);
789 glRotatef(35.0, 1.0, 0.0, 0.0);
790 gltrackball_rotate(mp->trackball);
791 glTranslatef(-mp->ant->position[0], mp->ant->position[1], -mp->ant->position[2]);
793 /* stable position */
794 /* glTranslatef(0.0, 0.0, -10.0 - mag); */
795 /* gltrackball_rotate(mp->trackball); */
796 /* glRotatef(40.0, 1.0, 0.0, 0.0); */
797 /* glRotatef(20.0, 0.0, 1.0, 0.0); */
799 draw_antspotlight_strip(mi);
805 if (MI_IS_FPS(mi)) do_fps (mi);
808 glXSwapBuffers(display, window);
812 ENTRYPOINT void change_antspotlight(ModeInfo * mi)
814 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
816 if (!mp->glx_context)
819 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
822 #endif /* !STANDALONE */
824 XSCREENSAVER_MODULE ("AntSpotlight", antspotlight)