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 /* 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);
141 glColor3fv(mp->mono ? MaterialGray5 : Material);
142 glVertex3f(0.00, 0.30, 0.00);
143 glColor3fv(MaterialGray);
144 glVertex3f(0.40, 0.70, -0.40);
149 glColor3fv(mp->mono ? MaterialGray6 : MaterialGray5);
150 glVertex3f(0.40, 0.70, 0.40);
151 glVertex3f(0.40, 0.70, -0.40);
156 glBegin(GL_LINE_STRIP);
157 glColor3fv(mp->mono ? MaterialGray5 : Material);
158 glVertex3f(0.00, 0.05, 0.18);
159 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
160 glColor3fv(MaterialGray);
161 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
164 /* LEFT-CENTER ARM */
165 glBegin(GL_LINE_STRIP);
166 glColor3fv(mp->mono ? MaterialGray5 : Material);
167 glVertex3f(0.00, 0.00, 0.18);
168 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
169 glColor3fv(MaterialGray);
170 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
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 * cos3, -0.15, 0.25);
178 glColor3fv(MaterialGray);
179 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
182 /* RIGHT-FRONT ARM */
183 glBegin(GL_LINE_STRIP);
184 glColor3fv(mp->mono ? MaterialGray5 : Material);
185 glVertex3f(0.00, 0.05, -0.18);
186 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
187 glColor3fv(MaterialGray);
188 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
191 /* RIGHT-CENTER ARM */
192 glBegin(GL_LINE_STRIP);
193 glColor3fv(mp->mono ? MaterialGray5 : Material);
194 glVertex3f(0.00, 0.00, -0.18);
195 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
196 glColor3fv(MaterialGray);
197 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
201 glBegin(GL_LINE_STRIP);
202 glColor3fv(mp->mono ? MaterialGray5 : Material);
203 glVertex3f(0.00, -0.05, -0.18);
204 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
205 glColor3fv(MaterialGray);
206 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
211 glColor3fv(MaterialGray5);
212 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
213 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
214 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
215 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
216 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
217 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
221 glEnable(GL_LIGHTING);
227 static Bool mySphere(float radius)
229 GLUquadricObj *quadObj;
231 if((quadObj = gluNewQuadric()) == 0)
234 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
235 gluSphere(quadObj, radius, 16, 16);
236 gluDeleteQuadric(quadObj);
241 /* silhouette sphere */
242 static Bool mySphere2(float radius)
244 GLUquadricObj *quadObj;
246 if((quadObj = gluNewQuadric()) == 0)
248 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);
249 gluSphere(quadObj, radius, 16, 8);
250 gluDeleteQuadric(quadObj);
256 static Bool myCone2(float radius) { return True; }
258 static void draw_board(antspotlightstruct *mp)
261 double cutoff = Pi/3.0;
265 glEnable(GL_TEXTURE_2D);
266 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
267 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
271 /* center is roughly spotlight position */
272 center[0] = mp->ant->position[0];/* + cos(ant->direction); */
274 center[2] = mp->ant->position[2];/* - 0.7*sin(ant->direction);*/
276 centertex[0] = (mp->boardsize/2.0+center[0]) * mp->max_tx / mp->boardsize;
277 centertex[1] = mp->max_ty - ((mp->boardsize/2.0+center[2]) * mp->max_ty / mp->boardsize);
279 /* glPolygonMode(GL_FRONT, GL_LINE); */
280 /* glDisable(GL_TEXTURE_2D); */
283 the vertices determined here should correspond to the illuminated
284 board. ideally the code adapts vertex distribution to the
285 intensity and shape of the light.
287 i should be finding the intersection of the cone of light and
290 for(i = -12; i < 12; ++i) {
292 double theta1, theta2;
294 glBegin(GL_TRIANGLE_STRIP);
295 glNormal3f(0.0, 1.0, 0.0);
297 glTexCoord2f(centertex[0], centertex[1]);
298 glVertex3f(center[0], 0.01, center[2]);
300 /* watch those constants */
301 theta1 = mp->ant->direction + i*(cutoff/8);
302 theta2 = mp->ant->direction + (i+1)*(cutoff/8);
304 for(j = 1; j <= 64; ++j) {
305 double point[3], tex[2];
306 /* double fj = pow(1.05, j) - 1.0;*/
308 point[0] = center[0] + fj*cos(theta1);
310 point[2] = center[2] - fj*sin(theta1);
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]);
318 point[0] = center[0] + fj*cos(theta2);
320 point[2] = center[2] - fj*sin(theta2);
322 tex[0] = (mp->boardsize/2.0+point[0]) * mp->max_tx / mp->boardsize;
323 tex[1] = (mp->boardsize/2.0+point[2]) * mp->max_ty / mp->boardsize;
325 glTexCoord2f(tex[0], tex[1]);
326 glVertex3f(point[0], point[1], point[2]);
332 glDisable(GL_TEXTURE_2D);
335 /* return euclidean distance between two points */
336 static double distance(double x[3], double y[3])
338 double dx = x[0] - y[0];
339 double dz = x[2] - y[2];
340 return sqrt(dx*dx + dz*dz);
343 /* determine a new goal */
344 static void find_goal(antspotlightstruct *mp)
347 mp->ant->goal[0] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
348 mp->ant->goal[1] = 0.0;
349 mp->ant->goal[2] = random()%((int)(mp->boardsize+0.5)-2) - mp->boardsize/2.0 + 1.0;
351 while(distance(mp->ant->position, mp->ant->goal) < 2.0);
354 /* construct our ant */
355 static void build_ant(antspotlightstruct *mp)
357 mp->ant = (Ant *) malloc(sizeof (Ant));
358 mp->ant->position[0] = 0.0;
359 mp->ant->position[1] = 0.0;
360 mp->ant->position[2] = 0.0;
361 mp->ant->direction = 0.0;
362 mp->ant->velocity = 0.02;
363 mp->ant->material = MaterialGray5;
370 static double sign(double d)
372 return d < 0.0 ? -1.0 : 1.0;
375 static double min(double a, double b)
377 return a < b ? a : b;
381 static double max(double a, double b)
383 return a > b ? a : b;
387 /* find a new goal and reset steps */
388 static void reset_ant(antspotlightstruct *mp)
393 /* draw ant composed of skeleton and glass */
394 static void show_ant(antspotlightstruct *mp)
399 /* move into position */
400 glTranslatef(mp->ant->position[0], 0.33, mp->ant->position[2]);
401 glRotatef(180.0 + mp->ant->direction*180.0/Pi, 0.0, 1.0, 0.0);
402 glRotatef(90.0, 0.0, 0.0, 1.0);
405 draw_ant(mp, mp->ant->material, mp->mono, 0, mp->ant->step, mySphere2, myCone2);
408 if(!mp->wire && !mp->mono) {
410 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGrayB);
411 glColor4fv(MaterialGrayB);
412 draw_ant(mp, MaterialGrayB, mp->mono, 0, mp->ant->step, mySphere, myCone2);
419 static void draw_antspotlight_strip(ModeInfo *mi)
421 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
423 /* compute spotlight position and direction */
424 GLfloat light1_position[4];
426 light1_position[0] = mp->ant->position[0] + 0.7*cos(mp->ant->direction);
427 light1_position[1] = 0.5;
428 light1_position[2] = mp->ant->position[2] - 0.7*sin(mp->ant->direction);
429 light1_position[3] = 1.0;
431 mp->spot_direction[0] = cos(mp->ant->direction);
432 mp->spot_direction[1] = -0.5;
433 mp->spot_direction[2] = -sin(mp->ant->direction);
435 glLightfv(GL_LIGHT2, GL_POSITION, light1_position);
436 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, mp->spot_direction);
439 glDisable(GL_LIGHT0);
440 glDisable(GL_LIGHT1);
448 glDisable(GL_LIGHT2);
455 /* near goal, bend path towards next step */
456 if(distance(mp->ant->position, mp->ant->goal) < 0.2) {
460 if(random()%100 == 0) {
465 /* move toward goal, correct ant direction if required */
469 double dx = mp->ant->goal[0] - mp->ant->position[0];
470 double dz = -(mp->ant->goal[2] - mp->ant->position[2]);
471 double theta, ideal, dt;
473 if(fabs(dx) > EPSILON) {
479 theta = dz > 0.0 ? (1.0/2.0)*Pi : (3.0/2.0)*Pi;
484 ideal = theta - mp->ant->direction;
488 /* compute correction */
489 dt = sign(ideal) * min(fabs(ideal), Pi/100.0);
490 mp->ant->direction += dt;
491 while(mp->ant->direction < 0.0)
492 mp->ant->direction += 2*Pi;
493 while(mp->ant->direction > 2*Pi)
494 mp->ant->direction -= 2*Pi;
497 mp->ant->position[0] += mp->ant->velocity * cos(mp->ant->direction);
498 mp->ant->position[2] += mp->ant->velocity * sin(-mp->ant->direction);
499 mp->ant->step += 10*mp->ant->velocity;
500 while(mp->ant->step > 2*Pi)
501 mp->ant->step -= 2*Pi;
504 ENTRYPOINT void reshape_antspotlight(ModeInfo * mi, int width, int height)
506 double h = (GLfloat) height / (GLfloat) width;
508 glViewport(0, 0, width, height);
509 glMatrixMode(GL_PROJECTION);
512 gluPerspective(45, 1/h, 1.0, 25.0);
514 glMatrixMode(GL_MODELVIEW);
519 /* lighting variables */
520 static const GLfloat front_shininess[] = {60.0};
521 static const GLfloat front_specular[] = {0.8, 0.8, 0.8, 1.0};
522 static const GLfloat ambient[] = {0.4, 0.4, 0.4, 1.0};
523 /*static const GLfloat ambient2[] = {0.0, 0.0, 0.0, 0.0};*/
524 static const GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
525 static const GLfloat position0[] = {1.0, 5.0, 1.0, 0.0};
526 static const GLfloat position1[] = {-1.0, -5.0, 1.0, 0.0};
527 /*static const GLfloat lmodel_ambient[] = {0.8, 0.8, 0.8, 1.0};*/
528 static const GLfloat lmodel_twoside[] = {GL_TRUE};
529 static const GLfloat spotlight_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
530 static const GLfloat spotlight_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
532 static void pinit(void)
535 glClearColor(0.0, 0.0, 0.0, 1.0);
537 /* setup twoside lighting */
538 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
539 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
540 glLightfv(GL_LIGHT0, GL_POSITION, position0);
541 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
542 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
543 glLightfv(GL_LIGHT1, GL_POSITION, position1);
545 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, spotlight_ambient);
546 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
547 glEnable(GL_LIGHTING);
551 /* setup spotlight */
552 glLightfv(GL_LIGHT2, GL_AMBIENT, spotlight_ambient);
553 glLightfv(GL_LIGHT2, GL_DIFFUSE, spotlight_diffuse);
554 glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.1);
555 glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.05);
556 glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0);
557 glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0);
558 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 3.0);
560 glEnable(GL_NORMALIZE);
564 /* setup material properties */
565 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
566 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
568 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
569 glShadeModel(GL_SMOOTH);
570 /* glShadeModel(GL_FLAT); */
571 glEnable(GL_DEPTH_TEST);
574 /* cleanup routine */
575 ENTRYPOINT void release_antspotlight(ModeInfo * mi)
579 free((void *) antspotlight);
580 antspotlight = (antspotlightstruct *) NULL;
586 #define MAX_MAGNIFICATION 10
587 #define max(a, b) a < b ? b : a
588 #define min(a, b) a < b ? a : b
591 ENTRYPOINT Bool antspotlight_handle_event(ModeInfo *mi, XEvent *event)
593 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
595 switch(event->xany.type) {
598 switch(event->xbutton.button) {
601 mp->button_down_p = True;
602 gltrackball_start(mp->trackball,
603 event->xbutton.x, event->xbutton.y,
604 MI_WIDTH (mi), MI_HEIGHT (mi));
608 mp->mag = max(mp->mag-1, 1);
612 mp->mag = min(mp->mag+1, MAX_MAGNIFICATION);
620 switch(event->xbutton.button) {
622 mp->button_down_p = False;
629 if(mp->button_down_p)
630 gltrackball_track(mp->trackball,
631 event->xmotion.x, event->xmotion.y,
632 MI_WIDTH (mi), MI_HEIGHT (mi));
643 image_loaded_cb (const char *filename, XRectangle *geometry,
644 int image_width, int image_height,
645 int texture_width, int texture_height,
648 antspotlightstruct *mp = (antspotlightstruct *) closure;
650 mp->max_tx = (GLfloat) image_width / texture_width;
651 mp->max_ty = (GLfloat) image_height / texture_height;
653 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
654 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
655 (mp->mipmap_p ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
657 mp->waiting_for_image_p = False;
662 static void get_snapshot(ModeInfo *modeinfo)
664 antspotlightstruct *mp = &antspotlight[MI_SCREEN(modeinfo)];
666 if (MI_IS_WIREFRAME(modeinfo))
669 mp->waiting_for_image_p = True;
671 load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
672 *mp->glx_context, 0, 0, mp->mipmap_p,
673 mp->screentexture, image_loaded_cb, mp);
677 ENTRYPOINT void init_antspotlight(ModeInfo *mi)
679 double rot_speed = 0.3;
681 antspotlightstruct *mp;
684 if((antspotlight = (antspotlightstruct *)
685 calloc(MI_NUM_SCREENS(mi), sizeof (antspotlightstruct))) == NULL)
688 mp = &antspotlight[MI_SCREEN(mi)];
689 mp->rot = make_rotator (rot_speed, rot_speed, rot_speed, 1, 0, True);
690 mp->trackball = gltrackball_init ();
692 if((mp->glx_context = init_GL(mi)) != NULL) {
693 reshape_antspotlight(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
694 glDrawBuffer(GL_BACK);
700 glGenTextures(1, &mp->screentexture);
701 glBindTexture(GL_TEXTURE_2D, mp->screentexture);
705 mp->mono = MI_IS_MONO(mi);
706 mp->wire = MI_IS_WIREFRAME(mi);
711 ENTRYPOINT void draw_antspotlight(ModeInfo * mi)
713 antspotlightstruct *mp;
715 Display *display = MI_DISPLAY(mi);
716 Window window = MI_WINDOW(mi);
720 mp = &antspotlight[MI_SCREEN(mi)];
722 MI_IS_DRAWN(mi) = True;
727 /* Just keep running before the texture has come in. */
728 /* if (mp->waiting_for_image_p) return; */
730 glXMakeCurrent(display, window, *(mp->glx_context));
732 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
736 /* position camera */
738 /* follow focused ant */
739 glTranslatef(0.0, 0.0, -6.0 - mp->mag);
740 glRotatef(35.0, 1.0, 0.0, 0.0);
741 gltrackball_rotate(mp->trackball);
742 glTranslatef(-mp->ant->position[0], mp->ant->position[1], -mp->ant->position[2]);
744 /* stable position */
745 /* glTranslatef(0.0, 0.0, -10.0 - mag); */
746 /* gltrackball_rotate(mp->trackball); */
747 /* glRotatef(40.0, 1.0, 0.0, 0.0); */
748 /* glRotatef(20.0, 0.0, 1.0, 0.0); */
750 draw_antspotlight_strip(mi);
756 if (MI_IS_FPS(mi)) do_fps (mi);
759 glXSwapBuffers(display, window);
763 ENTRYPOINT void change_antspotlight(ModeInfo * mi)
765 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
767 if (!mp->glx_context)
770 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
773 #endif /* !STANDALONE */
775 XSCREENSAVER_MODULE ("AntSpotlight", antspotlight)