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
17 #include <X11/Intrinsic.h>
20 #define PROGCLASS "AntSpotlight"
21 #define HACK_INIT init_antspotlight
22 #define HACK_DRAW draw_antspotlight
23 #define HACK_RESHAPE reshape_antspotlight
24 #define HACK_HANDLE_EVENT antspotlight_handle_event
25 #define EVENT_MASK PointerMotionMask
26 #define antspotlight_opts xlockmore_opts
27 #define DEFAULTS "*delay: 20000 \n" \
30 #include "xlockmore.h"
37 #include "gltrackball.h"
39 ModeSpecOpt antspotlight_opts = {
40 0, NULL, 0, NULL, NULL
44 ModStruct antspotlight_description = {
45 "antspotlight", "init_antspotlight", "draw_antspotlight",
46 "release_antspotlight", "draw_antspotlight", "change_antspotlight",
47 (char *) NULL, &antspotlight_opts, 1000, 1, 1, 1, 4, 1.0, "",
48 "draws an ant scoping the screen", 0, NULL
52 #define Scale4Window 0.3
53 #define Scale4Iconic 0.4
55 #define sqr(A) ((A)*(A))
62 int tw, th; /* texture width, height */
64 GLfloat max_tx, max_ty;
67 GLfloat qw = QW, qh = QH; /* for the quad we'll draw */
68 GLfloat qx = -6 , qy = 6;
69 int mono = 0, wire = 0, ticks = 0;
73 GLXContext *glx_context;
75 trackball_state *trackball;
80 #include "grab-ximage.h"
82 static antspotlightstruct *antspotlight = (antspotlightstruct *) NULL;
86 /* draw method for ant */
87 Bool draw_ant(GLfloat *Material, int mono, int shadow,
88 float ant_step, Bool (*sphere)(float), Bool (*cone)(float)) {
90 float cos1 = cos(ant_step);
91 float cos2 = cos(ant_step + 2 * Pi / 3);
92 float cos3 = cos(ant_step + 4 * Pi / 3);
93 float sin1 = sin(ant_step);
94 float sin2 = sin(ant_step + 2 * Pi / 3);
95 float sin3 = sin(ant_step + 4 * Pi / 3);
97 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, 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);
131 glColor3fv(mono ? MaterialGray5 : Material);
132 glVertex3f(0.00, 0.30, 0.00);
133 glColor3fv(MaterialGray);
134 glVertex3f(0.40, 0.70, 0.40);
135 glColor3fv(mono ? MaterialGray5 : Material);
136 glVertex3f(0.00, 0.30, 0.00);
137 glColor3fv(MaterialGray);
138 glVertex3f(0.40, 0.70, -0.40);
143 glColor3fv(mono ? MaterialGray6 : MaterialRed);
144 glVertex3f(0.40, 0.70, 0.40);
145 glVertex3f(0.40, 0.70, -0.40);
150 glBegin(GL_LINE_STRIP);
151 glColor3fv(mono ? MaterialGray5 : Material);
152 glVertex3f(0.00, 0.05, 0.18);
153 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
154 glColor3fv(MaterialGray);
155 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
158 /* LEFT-CENTER ARM */
159 glBegin(GL_LINE_STRIP);
160 glColor3fv(mono ? MaterialGray5 : Material);
161 glVertex3f(0.00, 0.00, 0.18);
162 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
163 glColor3fv(MaterialGray);
164 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
168 glBegin(GL_LINE_STRIP);
169 glColor3fv(mono ? MaterialGray5 : Material);
170 glVertex3f(0.00, -0.05, 0.18);
171 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
172 glColor3fv(MaterialGray);
173 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
176 /* RIGHT-FRONT ARM */
177 glBegin(GL_LINE_STRIP);
178 glColor3fv(mono ? MaterialGray5 : Material);
179 glVertex3f(0.00, 0.05, -0.18);
180 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
181 glColor3fv(MaterialGray);
182 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
185 /* RIGHT-CENTER ARM */
186 glBegin(GL_LINE_STRIP);
187 glColor3fv(mono ? MaterialGray5 : Material);
188 glVertex3f(0.00, 0.00, -0.18);
189 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
190 glColor3fv(MaterialGray);
191 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
195 glBegin(GL_LINE_STRIP);
196 glColor3fv(mono ? MaterialGray5 : Material);
197 glVertex3f(0.00, -0.05, -0.18);
198 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
199 glColor3fv(MaterialGray);
200 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
206 glColor3fv(MaterialGray8);
208 glColor3fv(MaterialMagenta);
209 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
210 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
211 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
212 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
213 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
214 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
218 glEnable(GL_LIGHTING);
224 static Bool mySphere(float radius) {
225 GLUquadricObj *quadObj;
227 if((quadObj = gluNewQuadric()) == 0)
230 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
231 gluSphere(quadObj, radius, 16, 16);
232 gluDeleteQuadric(quadObj);
237 /* silhouette sphere */
238 static Bool mySphere2(float radius) {
239 GLUquadricObj *quadObj;
241 if((quadObj = gluNewQuadric()) == 0)
243 gluQuadricDrawStyle(quadObj, (GLenum) GLU_SILHOUETTE);
244 gluSphere(quadObj, radius, 16, 8);
245 gluDeleteQuadric(quadObj);
251 static Bool myCone2(float radius) { return True; }
255 double boardsize = 8.0;
258 GLfloat spot_direction[3];
260 void draw_board(void) {
262 double cutoff = Pi/3.0;
266 glEnable(GL_TEXTURE_2D);
267 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray6);
271 /* center is roughly spotlight position */
272 center[0] = ant->position[0];/* + cos(ant->direction); */
274 center[2] = ant->position[2];/* - 0.7*sin(ant->direction);*/
276 centertex[0] = (boardsize/2.0+center[0]) * max_tx / boardsize;
277 centertex[1] = max_ty - ((boardsize/2.0+center[2]) * max_ty / 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 = -8; i < 8; ++i) {
292 double theta1, theta2;
294 glBegin(GL_TRIANGLE_STRIP);
295 glNormal3f(0.0, 1.0, 0.0);
296 glTexCoord2f(centertex[0], centertex[1]);
297 glVertex3f(center[0], 0.01, center[2]);
299 /* watch those constants */
300 theta1 = ant->direction + i*(cutoff/8);
301 theta2 = ant->direction + (i+1)*(cutoff/8);
303 for(j = 1; j <= 40; ++j) {
304 double point[3], tex[2];
305 /* double fj = pow(1.05, j) - 1.0;*/
307 point[0] = center[0] + fj*cos(theta1);
309 point[2] = center[2] - fj*sin(theta1);
311 tex[0] = (boardsize/2.0+point[0]) * max_tx / boardsize;
312 tex[1] = max_ty - ((boardsize/2.0+point[2]) * max_ty / boardsize);
314 glTexCoord2f(tex[0], tex[1]);
315 glVertex3f(point[0], point[1], point[2]);
317 point[0] = center[0] + fj*cos(theta2);
319 point[2] = center[2] - fj*sin(theta2);
321 tex[0] = (boardsize/2.0+point[0]) * max_tx / boardsize;
322 tex[1] = max_ty - ((boardsize/2.0+point[2]) * max_ty / boardsize);
324 glTexCoord2f(tex[0], tex[1]);
325 glVertex3f(point[0], point[1], point[2]);
331 glDisable(GL_TEXTURE_2D);
334 /* return euclidean distance between two points */
335 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 void find_goal(void) {
344 ant->goal[0] = random()%((int)(boardsize+0.5)-2) - boardsize/2.0 + 1.0;
346 ant->goal[2] = random()%((int)(boardsize+0.5)-2) - boardsize/2.0 + 1.0;
348 while(distance(ant->position, ant->goal) < 1.0);
351 /* construct our ant */
352 void build_ant(void) {
353 ant = (Ant *) malloc(sizeof (Ant));
354 ant->position[0] = 0.0;
355 ant->position[1] = 0.0;
356 ant->position[2] = 0.0;
357 ant->direction = 0.0;
358 ant->velocity = 0.02;
359 ant->material = MaterialGray5;
365 double sign(double d) {
366 return d < 0.0 ? -1.0 : 1.0;
369 double min(double a, double b) {
370 return a < b ? a : b;
373 double max(double a, double b) {
374 return a > b ? a : b;
377 /* find a new goal and reset steps */
378 void reset_ant(void) {
382 /* draw ant composed of skeleton and glass */
383 void show_ant(void) {
387 /* move into position */
388 glTranslatef(ant->position[0], 0.33, ant->position[2]);
389 glRotatef(180.0 + ant->direction*180.0/Pi, 0.0, 1.0, 0.0);
390 glRotatef(90.0, 0.0, 0.0, 1.0);
393 draw_ant(ant->material, mono, 0, ant->step, mySphere2, myCone2);
398 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGrayB);
399 glColor4fv(MaterialGrayB);
400 draw_ant(MaterialGrayB, mono, 0, ant->step, mySphere, myCone2);
407 void draw_antspotlight_strip(ModeInfo *mi) {
409 /* compute spotlight position and direction */
410 GLfloat light1_position[4];
412 light1_position[0] = ant->position[0] + 0.7*cos(ant->direction);
413 light1_position[1] = 0.5;
414 light1_position[2] = ant->position[2] - 0.7*sin(ant->direction);
415 light1_position[3] = 1.0;
417 spot_direction[0] = cos(ant->direction);
418 spot_direction[1] = -0.5;
419 spot_direction[2] = -sin(ant->direction);
421 glLightfv(GL_LIGHT2, GL_POSITION, light1_position);
422 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, spot_direction);
425 glDisable(GL_LIGHT0);
426 glDisable(GL_LIGHT1);
433 /* glCallList(BOARDLIST); */
435 glDisable(GL_LIGHT2);
442 /* near goal, bend path towards next step */
443 if(distance(ant->position, ant->goal) < 0.2) {
447 /* move toward goal, correct ant direction if required */
451 double dx = ant->goal[0] - ant->position[0];
452 double dz = -(ant->goal[2] - ant->position[2]);
453 double theta, ideal, dt;
455 if(fabs(dx) > EPSILON) {
461 theta = dz > 0.0 ? (1.0/2.0)*Pi : (3.0/2.0)*Pi;
466 ideal = theta - ant->direction;
470 /* compute correction */
471 dt = sign(ideal) * min(fabs(ideal), Pi/100.0);
472 ant->direction += dt;
473 while(ant->direction < 0.0)
474 ant->direction += 2*Pi;
475 while(ant->direction > 2*Pi)
476 ant->direction -= 2*Pi;
479 ant->position[0] += ant->velocity * cos(ant->direction);
480 ant->position[2] += ant->velocity * sin(-ant->direction);
481 ant->step += 10*ant->velocity;
484 void reshape_antspotlight(ModeInfo * mi, int width, int height) {
485 double h = (GLfloat) height / (GLfloat) width;
487 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
489 glViewport(0, 0, mp->WindW = (GLint) width, mp->WindH = (GLint) height);
490 glMatrixMode(GL_PROJECTION);
493 gluPerspective(45, 1/h, 1.0, 25.0);
495 glMatrixMode(GL_MODELVIEW);
500 /* lighting variables */
501 GLfloat front_shininess[] = {60.0};
502 GLfloat front_specular[] = {0.8, 0.8, 0.8, 1.0};
503 GLfloat ambient[] = {0.4, 0.4, 0.4, 1.0};
504 GLfloat ambient2[] = {0.0, 0.0, 0.0, 0.0};
505 GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
506 GLfloat position0[] = {1.0, 5.0, 1.0, 0.0};
507 GLfloat position1[] = {-1.0, -5.0, 1.0, 0.0};
508 GLfloat lmodel_ambient[] = {0.8, 0.8, 0.8, 1.0};
509 GLfloat lmodel_twoside[] = {GL_TRUE};
510 GLfloat spotlight_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
511 GLfloat spotlight_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
513 static void pinit(void) {
515 glClearColor(0.0, 0.0, 0.0, 1.0);
517 /* setup twoside lighting */
518 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
519 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
520 glLightfv(GL_LIGHT0, GL_POSITION, position0);
521 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
522 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
523 glLightfv(GL_LIGHT1, GL_POSITION, position1);
524 /* glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); */
525 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
526 glEnable(GL_LIGHTING);
530 /* setup spotlight */
531 glLightfv(GL_LIGHT2, GL_AMBIENT, spotlight_ambient);
532 glLightfv(GL_LIGHT2, GL_DIFFUSE, spotlight_diffuse);
533 glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.1);
534 glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.05);
535 glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.0);
536 glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 60.0);
537 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 3.0);
539 glEnable(GL_NORMALIZE);
543 /* setup material properties */
544 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
545 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
547 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
548 glShadeModel(GL_SMOOTH);
549 /* glShadeModel(GL_FLAT); */
550 glEnable(GL_DEPTH_TEST);
553 /* cleanup routine */
554 void release_antspotlight(ModeInfo * mi) {
557 free((void *) antspotlight);
558 antspotlight = (antspotlightstruct *) NULL;
565 #define MAX_MAGNIFICATION 10
566 #define max(a, b) a < b ? b : a
567 #define min(a, b) a < b ? a : b
570 Bool antspotlight_handle_event(ModeInfo *mi, XEvent *event) {
571 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
573 switch(event->xany.type) {
576 switch(event->xbutton.button) {
579 mp->button_down_p = True;
580 gltrackball_start(mp->trackball,
581 event->xbutton.x, event->xbutton.y,
582 MI_WIDTH (mi), MI_HEIGHT (mi));
590 mag = min(mag+1, MAX_MAGNIFICATION);
598 switch(event->xbutton.button) {
600 mp->button_down_p = False;
607 if(mp->button_down_p)
608 gltrackball_track(mp->trackball,
609 event->xmotion.x, event->xmotion.y,
610 MI_WIDTH (mi), MI_HEIGHT (mi));
621 void get_snapshot(ModeInfo *modeinfo) {
625 if(MI_IS_WIREFRAME(modeinfo))
628 ximage = screen_to_ximage(modeinfo->xgwa.screen, modeinfo->window);
631 tw = modeinfo->xgwa.width;
632 th = modeinfo->xgwa.height;
634 #if 0 /* jwz: this makes the image start off the bottom right of the screen */
639 qw *= (GLfloat)tw/winw;
640 qh *= (GLfloat)th/winh;
642 max_tx = (GLfloat) tw / (GLfloat) ximage->width;
643 max_ty = (GLfloat) th / (GLfloat) ximage->height;
645 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
646 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
647 GL_LINEAR_MIPMAP_LINEAR);
650 status = gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
651 ximage->width, ximage->height,
652 GL_RGBA, GL_UNSIGNED_BYTE, ximage->data);
654 if (!status && glGetError())
655 /* Some implementations of gluBuild2DMipmaps(), but set a GL error anyway.
656 We could just call check_gl_error(), but that would exit. */
660 const unsigned char *s = gluErrorString(status);
661 fprintf(stderr, "%s: error mipmapping %dx%d texture: %s\n",
662 progname, ximage->width, ximage->height,
663 (s ? s : (unsigned char *) "(unknown)"));
664 fprintf(stderr, "%s: turning on -wireframe.\n", progname);
665 MI_IS_WIREFRAME(modeinfo) = 1;
668 check_gl_error("mipmapping"); /* should get a return code instead of a
669 GL error, but just in case... */
673 XDestroyImage (ximage);
676 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);
700 mono = MI_IS_MONO(mi);
701 wire = MI_IS_WIREFRAME(mi);
703 /* glNewList(BOARDLIST, GL_COMPILE); */
708 void draw_antspotlight(ModeInfo * mi) {
709 antspotlightstruct *mp;
711 Display *display = MI_DISPLAY(mi);
712 Window window = MI_WINDOW(mi);
716 mp = &antspotlight[MI_SCREEN(mi)];
718 MI_IS_DRAWN(mi) = True;
723 glXMakeCurrent(display, window, *(mp->glx_context));
725 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
729 /* position camera */
731 /* follow focused ant */
732 glTranslatef(0.0, 0.0, -10.0 - mag);
733 glRotatef(35.0, 1.0, 0.0, 0.0);
734 gltrackball_rotate(mp->trackball);
735 glTranslatef(-ant->position[0], ant->position[1], -ant->position[2]);
737 /* stable position */
738 /* glTranslatef(0.0, 0.0, -10.0 - mag); */
739 /* gltrackball_rotate(mp->trackball); */
740 /* glRotatef(40.0, 1.0, 0.0, 0.0); */
741 /* glRotatef(20.0, 0.0, 1.0, 0.0); */
743 draw_antspotlight_strip(mi);
749 if (MI_IS_FPS(mi)) do_fps (mi);
752 glXSwapBuffers(display, window);
755 void change_antspotlight(ModeInfo * mi) {
756 antspotlightstruct *mp = &antspotlight[MI_SCREEN(mi)];
758 if (!mp->glx_context)
761 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));