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 glEnable(GL_POLYGON_SMOOTH);
95 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
97 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mp->mono ? MaterialGray5 : Material);
98 glEnable(GL_CULL_FACE);
101 if(!((*sphere)(0.18)))
103 glScalef(1, 1 / 1.3, 1);
104 glTranslatef(0.00, 0.30, 0.00);
105 if(!((*sphere)(0.2)))
108 glTranslatef(-0.05, 0.17, 0.05);
109 glRotatef(-90, 1, 0, 0);
110 glRotatef(-25, 0, 1, 0);
113 glTranslatef(0.00, 0.10, 0.00);
116 glRotatef(25, 0, 1, 0);
117 glRotatef(90, 1, 0, 0);
120 glTranslatef(0.15, -0.65, 0.05);
121 if(!((*sphere)(0.25)))
123 glScalef(1, 1 / 1.3, 1);
125 glDisable(GL_CULL_FACE);
127 glDisable(GL_LIGHTING);
130 glEnable(GL_LINE_SMOOTH);
132 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
135 glColor3fv(mp->mono ? MaterialGray5 : Material);
136 glVertex3f(0.00, 0.30, 0.00);
137 glColor3fv(MaterialGray);
138 glVertex3f(0.40, 0.70, 0.40);
139 glColor3fv(mp->mono ? MaterialGray5 : Material);
140 glVertex3f(0.00, 0.30, 0.00);
141 glColor3fv(MaterialGray);
142 glVertex3f(0.40, 0.70, -0.40);
147 glColor3fv(mp->mono ? MaterialGray6 : MaterialGray5);
148 glVertex3f(0.40, 0.70, 0.40);
149 glVertex3f(0.40, 0.70, -0.40);
154 glBegin(GL_LINE_STRIP);
155 glColor3fv(mp->mono ? MaterialGray5 : Material);
156 glVertex3f(0.00, 0.05, 0.18);
157 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
158 glColor3fv(MaterialGray);
159 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
162 /* LEFT-CENTER ARM */
163 glBegin(GL_LINE_STRIP);
164 glColor3fv(mp->mono ? MaterialGray5 : Material);
165 glVertex3f(0.00, 0.00, 0.18);
166 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
167 glColor3fv(MaterialGray);
168 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
172 glBegin(GL_LINE_STRIP);
173 glColor3fv(mp->mono ? MaterialGray5 : Material);
174 glVertex3f(0.00, -0.05, 0.18);
175 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
176 glColor3fv(MaterialGray);
177 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
180 /* RIGHT-FRONT ARM */
181 glBegin(GL_LINE_STRIP);
182 glColor3fv(mp->mono ? MaterialGray5 : Material);
183 glVertex3f(0.00, 0.05, -0.18);
184 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
185 glColor3fv(MaterialGray);
186 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
189 /* RIGHT-CENTER ARM */
190 glBegin(GL_LINE_STRIP);
191 glColor3fv(mp->mono ? MaterialGray5 : Material);
192 glVertex3f(0.00, 0.00, -0.18);
193 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
194 glColor3fv(MaterialGray);
195 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
199 glBegin(GL_LINE_STRIP);
200 glColor3fv(mp->mono ? MaterialGray5 : Material);
201 glVertex3f(0.00, -0.05, -0.18);
202 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
203 glColor3fv(MaterialGray);
204 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
209 glColor3fv(MaterialGray5);
210 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
211 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
212 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
213 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
214 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
215 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
219 glEnable(GL_LIGHTING);
225 static Bool mySphere(float radius)
227 GLUquadricObj *quadObj;
229 if((quadObj = gluNewQuadric()) == 0)
232 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
233 gluSphere(quadObj, radius, 16, 16);
234 gluDeleteQuadric(quadObj);
239 /* silhouette sphere */
240 static Bool mySphere2(float radius)
242 GLUquadricObj *quadObj;
244 if((quadObj = gluNewQuadric()) == 0)
246 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);
247 gluSphere(quadObj, radius, 16, 8);
248 gluDeleteQuadric(quadObj);
254 static Bool myCone2(float radius) { return True; }
256 static void draw_board(antspotlightstruct *mp)
259 double cutoff = Pi/3.0;
263 glEnable(GL_TEXTURE_2D);
264 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
265 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
269 /* center is roughly spotlight position */
270 center[0] = mp->ant->position[0];/* + cos(ant->direction); */
272 center[2] = mp->ant->position[2];/* - 0.7*sin(ant->direction);*/
274 centertex[0] = (mp->boardsize/2.0+center[0]) * mp->max_tx / mp->boardsize;
275 centertex[1] = mp->max_ty - ((mp->boardsize/2.0+center[2]) * mp->max_ty / mp->boardsize);
277 /* glPolygonMode(GL_FRONT, GL_LINE); */
278 /* glDisable(GL_TEXTURE_2D); */
281 the vertices determined here should correspond to the illuminated
282 board. ideally the code adapts vertex distribution to the
283 intensity and shape of the light.
285 i should be finding the intersection of the cone of light and
288 for(i = -12; i < 12; ++i) {
290 double theta1, theta2;
292 glBegin(GL_TRIANGLE_STRIP);
293 glNormal3f(0.0, 1.0, 0.0);
295 glTexCoord2f(centertex[0], centertex[1]);
296 glVertex3f(center[0], 0.01, center[2]);
298 /* watch those constants */
299 theta1 = mp->ant->direction + i*(cutoff/8);
300 theta2 = mp->ant->direction + (i+1)*(cutoff/8);
302 for(j = 1; j <= 64; ++j) {
303 double point[3], tex[2];
304 /* double fj = pow(1.05, j) - 1.0;*/
306 point[0] = center[0] + fj*cos(theta1);
308 point[2] = center[2] - fj*sin(theta1);
310 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
311 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
313 glTexCoord2f(tex[0], tex[1]);
314 glVertex3f(point[0], point[1], point[2]);
316 point[0] = center[0] + fj*cos(theta2);
318 point[2] = center[2] - fj*sin(theta2);
320 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
321 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
323 glTexCoord2f(tex[0], tex[1]);
324 glVertex3f(point[0], point[1], point[2]);
330 glDisable(GL_TEXTURE_2D);
333 /* return euclidean distance between two points */
334 static double distance(double x[3], double y[3])
336 double dx = x[0] - y[0];
337 double dz = x[2] - y[2];
338 return sqrt(dx*dx + dz*dz);
341 /* determine a new goal */
342 static void find_goal(antspotlightstruct *mp)
345 mp->ant->goal[0] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
346 mp->ant->goal[1] = 0.0;
347 mp->ant->goal[2] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
349 while(distance(mp->ant->position, mp->ant->goal) < 2.0);
352 /* construct our ant */
353 static void build_ant(antspotlightstruct *mp)
355 mp->ant = (Ant *) malloc(sizeof (Ant));
356 mp->ant->position[0] = 0.0;
357 mp->ant->position[1] = 0.0;
358 mp->ant->position[2] = 0.0;
359 mp->ant->direction = 0.0;
360 mp->ant->velocity = 0.02;
361 mp->ant->material = MaterialGray5;
368 static double sign(double d)
370 return d < 0.0 ? -1.0 : 1.0;
373 static double min(double a, double b)
375 return a < b ? a : b;
379 static double max(double a, double b)
381 return a > b ? a : b;
385 /* find a new goal and reset steps */
386 static void reset_ant(antspotlightstruct *mp)
391 /* draw ant composed of skeleton and glass */
392 static void show_ant(antspotlightstruct *mp)
397 /* move into position */
398 glTranslatef(mp->ant->position[0], 0.33, mp->ant->position[2]);
399 glRotatef(180.0 + mp->ant->direction*180.0/Pi, 0.0, 1.0, 0.0);
400 glRotatef(90.0, 0.0, 0.0, 1.0);
403 draw_ant(mp, mp->ant->material, mp->mono, 0, mp->ant->step, mySphere2, myCone2);
406 if(!mp->wire && !mp->mono) {
408 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGrayB);
409 glColor4fv(MaterialGrayB);
410 draw_ant(mp, MaterialGrayB, mp->mono, 0, mp->ant->step, mySphere, myCone2);
417 static void draw_antspotlight_strip(ModeInfo *mi)
419 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
421 /* compute spotlight position and direction */
422 GLfloat light1_position[4];
424 light1_position[0] = mp->ant->position[0] + 0.7*cos(mp->ant->direction);
425 light1_position[1] = 0.5;
426 light1_position[2] = mp->ant->position[2] - 0.7*sin(mp->ant->direction);
427 light1_position[3] = 1.0;
429 mp->spot_direction[0] = cos(mp->ant->direction);
430 mp->spot_direction[1] = -0.5;
431 mp->spot_direction[2] = -sin(mp->ant->direction);
433 glLightfv(GL_LIGHT2, GL_POSITION, light1_position);
434 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, mp->spot_direction);
437 glDisable(GL_LIGHT0);
438 glDisable(GL_LIGHT1);
446 glDisable(GL_LIGHT2);
453 /* near goal, bend path towards next step */
454 if(distance(mp->ant->position, mp->ant->goal) < 0.2) {
458 if(random()%100 == 0) {
463 /* move toward goal, correct ant direction if required */
467 double dx = mp->ant->goal[0] - mp->ant->position[0];
468 double dz = -(mp->ant->goal[2] - mp->ant->position[2]);
469 double theta, ideal, dt;
471 if(fabs(dx) > EPSILON) {
477 theta = dz > 0.0 ? (1.0/2.0)*Pi : (3.0/2.0)*Pi;
482 ideal = theta - mp->ant->direction;
486 /* compute correction */
487 dt = sign(ideal) * min(fabs(ideal), Pi/100.0);
488 mp->ant->direction += dt;
489 while(mp->ant->direction < 0.0)
490 mp->ant->direction += 2*Pi;
491 while(mp->ant->direction > 2*Pi)
492 mp->ant->direction -= 2*Pi;
495 mp->ant->position[0] += mp->ant->velocity * cos(mp->ant->direction);
496 mp->ant->position[2] += mp->ant->velocity * sin(-mp->ant->direction);
497 mp->ant->step += 10*mp->ant->velocity;
498 while(mp->ant->step > 2*Pi)
499 mp->ant->step -= 2*Pi;
502 ENTRYPOINT void reshape_antspotlight(ModeInfo * mi, int width, int height)
504 double h = (GLfloat) height / (GLfloat) width;
506 glViewport(0, 0, width, height);
507 glMatrixMode(GL_PROJECTION);
510 gluPerspective(45, 1/h, 1.0, 25.0);
512 glMatrixMode(GL_MODELVIEW);
517 /* lighting variables */
518 static const GLfloat front_shininess[] = {60.0};
519 static const GLfloat front_specular[] = {0.8, 0.8, 0.8, 1.0};
520 static const GLfloat ambient[] = {0.4, 0.4, 0.4, 1.0};
521 /*static const GLfloat ambient2[] = {0.0, 0.0, 0.0, 0.0};*/
522 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
523 static const GLfloat position0[] = {1.0, 5.0, 1.0, 0.0};
524 static const GLfloat position1[] = {-1.0, -5.0, 1.0, 0.0};
525 /*static const GLfloat lmodel_ambient[] = {0.8, 0.8, 0.8, 1.0};*/
526 static const GLfloat lmodel_twoside[] = {GL_TRUE};
527 static const GLfloat spotlight_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
528 static const GLfloat spotlight_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
530 static void pinit(void)
533 glClearColor(0.0, 0.0, 0.0, 1.0);
535 /* setup twoside lighting */
536 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
537 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
538 glLightfv(GL_LIGHT0, GL_POSITION, position0);
539 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
540 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
541 glLightfv(GL_LIGHT1, GL_POSITION, position1);
543 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, spotlight_ambient);
544 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
545 glEnable(GL_LIGHTING);
549 /* setup spotlight */
550 glLightfv(GL_LIGHT2, GL_AMBIENT, spotlight_ambient);
551 glLightfv(GL_LIGHT2, GL_DIFFUSE, spotlight_diffuse);
552 glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.1);
553 glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.05);
554 glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0);
555 glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0);
556 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 3.0);
558 glEnable(GL_NORMALIZE);
562 /* setup material properties */
563 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
564 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
566 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
567 glShadeModel(GL_SMOOTH);
568 /* glShadeModel(GL_FLAT); */
569 glEnable(GL_DEPTH_TEST);
572 /* cleanup routine */
573 ENTRYPOINT void release_antspotlight(ModeInfo * mi)
577 free((void *) antspotlight);
578 antspotlight = (antspotlightstruct *) NULL;
584 #define MAX_MAGNIFICATION 10
585 #define max(a, b) a < b ? b : a
586 #define min(a, b) a < b ? a : b
589 ENTRYPOINT Bool antspotlight_handle_event(ModeInfo *mi, XEvent *event)
591 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
593 switch(event->xany.type) {
596 switch(event->xbutton.button) {
599 mp->button_down_p = True;
600 gltrackball_start(mp->trackball,
601 event->xbutton.x, event->xbutton.y,
602 MI_WIDTH (mi), MI_HEIGHT (mi));
606 mp->mag = max(mp->mag-1, 1);
610 mp->mag = min(mp->mag+1, MAX_MAGNIFICATION);
618 switch(event->xbutton.button) {
620 mp->button_down_p = False;
627 if(mp->button_down_p)
628 gltrackball_track(mp->trackball,
629 event->xmotion.x, event->xmotion.y,
630 MI_WIDTH (mi), MI_HEIGHT (mi));
641 image_loaded_cb (const char *filename, XRectangle *geometry,
642 int image_width, int image_height,
643 int texture_width, int texture_height,
646 antspotlightstruct *mp = (antspotlightstruct *) closure;
648 mp->max_tx = (GLfloat) image_width / texture_width;
649 mp->max_ty = (GLfloat) image_height / texture_height;
651 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
652 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
653 (mp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
655 mp->waiting_for_image_p = False;
660 static void get_snapshot(ModeInfo *modeinfo)
662 antspotlightstruct *mp = &antspotlight[MI_SCREEN(modeinfo)];
664 if (MI_IS_WIREFRAME(modeinfo))
667 mp->waiting_for_image_p = True;
669 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
670 *mp->glx_context, 0, 0, mp->mipmap_p,
671 mp->screentexture, image_loaded_cb, mp);
675 ENTRYPOINT void init_antspotlight(ModeInfo *mi)
677 double rot_speed = 0.3;
679 antspotlightstruct *mp;
682 if((antspotlight = (antspotlightstruct *)
683 calloc(MI_NUM_SCREENS(mi), sizeof (antspotlightstruct))) == NULL)
686 mp = &antspotlight[MI_SCREEN(mi)];
687 mp->rot = make_rotator (rot_speed, rot_speed, rot_speed, 1, 0, True);
688 mp->trackball = gltrackball_init ();
690 if((mp->glx_context = init_GL(mi)) != NULL) {
691 reshape_antspotlight(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
692 glDrawBuffer(GL_BACK);
698 glGenTextures(1, &mp->screentexture);
699 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
703 mp->mono = MI_IS_MONO(mi);
704 mp->wire = MI_IS_WIREFRAME(mi);
709 ENTRYPOINT void draw_antspotlight(ModeInfo * mi)
711 antspotlightstruct *mp;
713 Display *display = MI_DISPLAY(mi);
714 Window window = MI_WINDOW(mi);
718 mp = &antspotlight[MI_SCREEN(mi)];
720 MI_IS_DRAWN(mi) = True;
725 /* Just keep running before the texture has come in. */
726 /* if (mp->waiting_for_image_p) return; */
728 glXMakeCurrent(display, window, *(mp->glx_context));
730 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
734 /* position camera */
736 /* follow focused ant */
737 glTranslatef(0.0, 0.0, -6.0 - mp->mag);
738 glRotatef(35.0, 1.0, 0.0, 0.0);
739 gltrackball_rotate(mp->trackball);
740 glTranslatef(-mp->ant->position[0], mp->ant->position[1], -mp->ant->position[2]);
742 /* stable position */
743 /* glTranslatef(0.0, 0.0, -10.0 - mag); */
744 /* gltrackball_rotate(mp->trackball); */
745 /* glRotatef(40.0, 1.0, 0.0, 0.0); */
746 /* glRotatef(20.0, 0.0, 1.0, 0.0); */
748 draw_antspotlight_strip(mi);
754 if (MI_IS_FPS(mi)) do_fps (mi);
757 glXSwapBuffers(display, window);
761 ENTRYPOINT void change_antspotlight(ModeInfo * mi)
763 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
765 if (!mp->glx_context)
768 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
771 #endif /* !STANDALONE */
773 XSCREENSAVER_MODULE ("AntSpotlight", antspotlight)