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 2004 Blair Tennessy
19 #define DEFAULTS "*delay: 20000 \n" \
22 # define refresh_antinspect 0
23 #include "xlockmore.h"
31 # include <X11/Xlib.h>
38 #endif /* HAVE_JWZGLES */
41 #include "gltrackball.h"
43 #define DEF_SHADOWS "True"
47 static XrmOptionDescRec opts[] = {
48 {"-shadows", ".antinspect.shadows", XrmoptionNoArg, "on"},
49 {"+shadows", ".antinspect.shadows", XrmoptionNoArg, "off"}
52 static argtype vars[] = {
53 {&shadows, "shadows", "Shadows", DEF_SHADOWS, t_Bool}
56 static OptionStruct desc[] = {
57 {"-/+shadows", "turn on/off ant shadows"}
60 ENTRYPOINT ModeSpecOpt antinspect_opts = {sizeof opts / sizeof opts[0],
62 sizeof vars / sizeof vars[0],
67 ModStruct antinspect_description =
68 {"antinspect", "init_antinspect", "draw_antinspect", "release_antinspect",
69 "draw_antinspect", "change_antinspect", (char *) NULL, &antinspect_opts,
70 1000, 1, 1, 1, 4, 1.0, "",
71 "draws some ants", 0, NULL};
74 #define Scale4Window 0.3
75 #define Scale4Iconic 0.4
77 #define sqr(A) ((A)*(A))
83 #define ObjAntinspectStrip 0
87 /*************************************************************************/
93 GLXContext *glx_context;
94 trackball_state *trackball;
101 static const float front_shininess[] = {60.0};
102 static const float front_specular[] = {0.7, 0.7, 0.7, 1.0};
103 static const float ambient[] = {0.0, 0.0, 0.0, 1.0};
104 static const float diffuse[] = {1.0, 1.0, 1.0, 1.0};
105 static float position0[] = {0.0, 3.0, 0.0, 1.0};
106 static const float position1[] = {-1.0, -3.0, 1.0, 0.0};
107 static const float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
108 static const float lmodel_twoside[] = {GL_TRUE};
110 static const float MaterialRed[] = {0.6, 0.0, 0.0, 1.0};
111 static const float MaterialOrange[] = {1.0, 0.69, 0.00, 1.0};
112 static const float MaterialGray[] = {0.2, 0.2, 0.2, 1.0};
113 static const float MaterialBlack[] = {0.1, 0.1, 0.1, 0.4};
114 static const float MaterialShadow[] = {0.3, 0.3, 0.3, 0.3};
115 static const float MaterialGray5[] = {0.5, 0.5, 0.5, 0.3};
116 static const float MaterialGray6[] = {0.6, 0.6, 0.6, 1.0};
118 static antinspectstruct *antinspect = (antinspectstruct *) NULL;
125 /* create a matrix that will project the desired shadow */
126 static void shadowmatrix(GLfloat shadowMat[4][4],
127 const GLfloat groundplane[4],
128 const GLfloat lightpos[4])
132 /* find dot product between light position vector and ground plane normal */
133 dot = groundplane[X] * lightpos[X] +
134 groundplane[Y] * lightpos[Y] +
135 groundplane[Z] * lightpos[Z] +
136 groundplane[W] * lightpos[W];
138 shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
139 shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
140 shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
141 shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
143 shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
144 shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
145 shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
146 shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
148 shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
149 shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
150 shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
151 shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
153 shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
154 shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
155 shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
156 shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
159 static const GLfloat ground[4] = {0.0, 1.0, 0.0, -0.00001};
161 /* simple filled sphere */
162 static Bool mySphere(float radius)
165 GLUquadricObj *quadObj;
167 if((quadObj = gluNewQuadric()) == 0)
169 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
170 gluSphere(quadObj, radius, 16, 16);
171 gluDeleteQuadric(quadObj);
174 glScalef (radius, radius, radius);
175 glRotatef (90, 1, 0, 0);
176 unit_sphere (16, 16, False);
183 static Bool mySphere2(float radius)
186 GLUquadricObj *quadObj;
188 if((quadObj = gluNewQuadric()) == 0)
190 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);/*GLU_SILHOUETTE);*/
191 gluSphere(quadObj, radius, 16, 8);
192 gluDeleteQuadric(quadObj);
195 glScalef (radius, radius, radius);
196 glRotatef (90, 1, 0, 0);
197 unit_sphere (16, 8, True);
205 static Bool myCone2(float radius)
211 static Bool draw_antinspect_ant(ModeInfo *mi, antinspectstruct * mp,
212 const float *Material, int mono,
213 Bool (*sphere)(float), Bool (*cone)(float))
215 float cos1 = cos(mp->ant_step);
216 float cos2 = cos(mp->ant_step + 2 * Pi / 3);
217 float cos3 = cos(mp->ant_step + 4 * Pi / 3);
218 float sin1 = sin(mp->ant_step);
219 float sin2 = sin(mp->ant_step + 2 * Pi / 3);
220 float sin3 = sin(mp->ant_step + 4 * Pi / 3);
223 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
225 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Material);
226 glEnable(GL_CULL_FACE);
229 if (!((*sphere)(0.18)))
231 glScalef(1, 1 / 1.3, 1);
232 glTranslatef(0.00, 0.30, 0.00);
233 if (!((*sphere)(0.2)))
236 glTranslatef(-0.05, 0.17, 0.05);
237 glRotatef(-90, 1, 0, 0);
238 glRotatef(-25, 0, 1, 0);
239 if (!((*cone)(0.05)))
241 glTranslatef(0.00, 0.10, 0.00);
242 if (!((*cone)(0.05)))
244 glRotatef(25, 0, 1, 0);
245 glRotatef(90, 1, 0, 0);
248 glTranslatef(0.15, -0.65, 0.05);
249 if (!((*sphere)(0.25)))
251 glScalef(1, 1 / 1.3, 1);
253 glDisable(GL_CULL_FACE);
255 glDisable(GL_LIGHTING);
260 glColor3fv(MaterialGray5);
262 glColor3fv(Material);
263 glVertex3f(0.00, 0.30, 0.00);
264 glColor3fv(MaterialGray);
265 glVertex3f(0.40, 0.70, 0.40);
268 glColor3fv(MaterialGray5);
270 glColor3fv(Material);
271 glVertex3f(0.00, 0.30, 0.00);
272 glColor3fv(MaterialGray);
273 glVertex3f(0.40, 0.70, -0.40);
278 glColor3fv(MaterialGray6);
280 glColor3fv(Material);
281 glVertex3f(0.40, 0.70, 0.40);
283 glVertex3f(0.40, 0.70, -0.40);
288 glBegin(GL_LINE_STRIP);
290 glColor3fv(MaterialGray5);
292 glColor3fv(Material);
293 glVertex3f(0.00, 0.05, 0.18);
294 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
296 glColor3fv(MaterialGray);
297 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
301 /* LEFT-CENTER ARM */
302 glBegin(GL_LINE_STRIP);
304 glColor3fv(MaterialGray5);
306 glColor3fv(Material);
307 glVertex3f(0.00, 0.00, 0.18);
308 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
310 glColor3fv(MaterialGray);
311 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
317 glBegin(GL_LINE_STRIP);
319 glColor3fv(MaterialGray5);
321 glColor3fv(Material);
322 glVertex3f(0.00, -0.05, 0.18);
323 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
325 glColor3fv(MaterialGray);
326 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
330 /* RIGHT-FRONT ARM */
331 glBegin(GL_LINE_STRIP);
333 glColor3fv(MaterialGray5);
335 glColor3fv(Material);
336 glVertex3f(0.00, 0.05, -0.18);
337 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
339 glColor3fv(MaterialGray);
340 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
344 /* RIGHT-CENTER ARM */
345 glBegin(GL_LINE_STRIP);
347 glColor3fv(MaterialGray5);
349 glColor3fv(Material);
350 glVertex3f(0.00, 0.00, -0.18);
351 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
353 glColor3fv(MaterialGray);
354 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
359 glBegin(GL_LINE_STRIP);
361 glColor3fv(MaterialGray5);
363 glColor3fv(Material);
364 glVertex3f(0.00, -0.05, -0.18);
365 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
367 glColor3fv(MaterialGray);
368 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
372 glEnable(GL_LIGHTING);
377 /* only works with 3 right now */
380 static const float MaterialBen[4] = {0.25, 0.30, 0.46, 1.0};
382 static const float* antmaterial[ANTCOUNT] =
383 {MaterialRed, MaterialBen, MaterialOrange};
384 static double antposition[ANTCOUNT] = {0.0, 120.0, 240.0};
385 static const double antvelocity[ANTCOUNT] = {0.3, 0.3, 0.3};
386 static const double antsphere[ANTCOUNT] = {1.2, 1.2, 1.2};
389 static const double antorder[6][ANTCOUNT] = {{0, 1, 2},
397 static Bool draw_antinspect_strip(ModeInfo * mi)
399 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
401 int mono = MI_IS_MONO(mi);
403 int ro = (((int)antposition[1])/(360/(2*ANTCOUNT))) % (2*ANTCOUNT);
405 glEnable(GL_TEXTURE_2D);
407 glLightfv(GL_LIGHT0, GL_POSITION, position0);
409 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
410 glRotatef(-30.0, 0.0, 1.0, 0.0);
412 glDisable(GL_TEXTURE_2D);
415 /* render ground plane */
416 glBegin(GL_TRIANGLES);
417 glColor4fv(MaterialShadow);
418 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlack);
419 glNormal3f(0.0, 1.0, 0.0);
422 glVertex3f(0.0, 0.0, -1.0);
423 glVertex3f(-sqrt(3.0)/2.0, 0.0, 0.5);
424 glVertex3f(sqrt(3.0)/2.0, 0.0, 0.5);
429 for(i = 0; i < 3; ++i) {
430 glRotatef(120.0, 0.0, 1.0, 0.0);
431 glBegin(GL_TRIANGLES);
432 glVertex3f(0.0, 0.0, 1.0 + 3.0);
433 glVertex3f(sqrt(3.0)/2.0, 0.0, -0.5 + 3.0);
434 glVertex3f(-sqrt(3.0)/2.0, 0.0, -0.5 + 3.0);
439 /* first render shadows -- no depth required */
442 shadowmatrix(m, ground, position0);
444 glColor4fv(MaterialShadow);
445 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
448 glDisable(GL_LIGHTING);
450 /* display ant shadow */
452 glTranslatef(0.0, 0.001, 0.0);
455 for(i = 0; i < ANTCOUNT; ++i) {
461 glRotatef(antposition[i], 0.0, 1.0, 0.0);
462 glTranslatef(2.4, 0.0, 0.0);
463 glTranslatef(0.0, antsphere[i], 0.0);
464 glRotatef(90.0, 0.0, 1.0, 0.0);
467 glRotatef(10.0, 0.0, 1.0, 0.0);
468 glRotatef(40.0, 0.0, 0.0, 1.0);
469 glTranslatef(0.0, -0.8, 0.0);
470 glRotatef(180.0, 0.0, 1.0, 0.0);
471 glRotatef(90.0, 0.0, 0.0, 1.0);
474 glColor4fv(MaterialShadow);
475 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
477 if(antposition[i] > 360.0)
478 antposition[i] = 0.0;
479 draw_antinspect_ant(mi, mp, MaterialShadow, mono, mySphere2, myCone2);
482 glDisable(GL_LIGHTING);
485 glRotatef(-20.0, 1.0, 0.0, 0.0);
486 glRotatef(-mp->ant_step*2, 0.0, 0.0, 1.0);
487 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
496 glEnable(GL_LIGHTING);
499 for(j = 0; j < ANTCOUNT; ++j) {
500 /* determine rendering order */
506 glRotatef(antposition[i], 0.0, 1.0, 0.0);
507 glTranslatef(2.4, 0.0, 0.0);
508 glTranslatef(0.0, antsphere[i], 0.0);
509 glRotatef(90.0, 0.0, 1.0, 0.0);
513 glRotatef(10.0, 0.0, 1.0, 0.0);
514 glRotatef(40.0, 0.0, 0.0, 1.0);
515 glTranslatef(0.0, -0.8, 0.0);
516 glRotatef(180.0, 0.0, 1.0, 0.0);
517 glRotatef(90.0, 0.0, 0.0, 1.0);
518 if(antposition[i] > 360.0)
519 antposition[i] = 0.0;
521 draw_antinspect_ant(mi, mp, antmaterial[i], mono, mySphere2, myCone2);
526 glRotatef(-20.0, 1.0, 0.0, 0.0);
527 glRotatef(-mp->ant_step*2, 0.0, 0.0, 1.0);
528 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mono ? MaterialGray5 : antmaterial[i]);
531 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlack);
537 /* finally, evolve */
538 antposition[i] += antvelocity[i];
541 /* but the step size is the same! */
544 mp->ant_position += 1;
548 ENTRYPOINT void reshape_antinspect(ModeInfo * mi, int width, int height)
550 double h = (GLfloat) height / (GLfloat) width;
551 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
552 mp->linewidth = (width / 512) + 1;
554 glViewport(0, 0, mp->WindW = (GLint) width, mp->WindH = (GLint) height);
555 glMatrixMode(GL_PROJECTION);
558 gluPerspective(45, 1/h, 7.0, 20.0);
560 glMatrixMode(GL_MODELVIEW);
561 glLineWidth(mp->linewidth);
562 glPointSize(mp->linewidth);
565 static void pinit(void)
568 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
569 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
570 glLightfv(GL_LIGHT0, GL_POSITION, position0);
571 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
572 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
573 glLightfv(GL_LIGHT1, GL_POSITION, position1);
574 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
575 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
576 glEnable(GL_LIGHTING);
579 glEnable(GL_NORMALIZE);
583 glShadeModel(GL_SMOOTH);
584 glEnable(GL_DEPTH_TEST);
585 glDisable(GL_TEXTURE_2D);
587 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
588 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
591 ENTRYPOINT void release_antinspect(ModeInfo * mi)
594 free((void *) antinspect);
595 antinspect = (antinspectstruct *) NULL;
600 ENTRYPOINT Bool antinspect_handle_event (ModeInfo *mi, XEvent *event)
602 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
604 if(event->xany.type == ButtonPress && event->xbutton.button == Button1) {
605 mp->button_down_p = True;
606 gltrackball_start(mp->trackball,
607 event->xbutton.x, event->xbutton.y,
608 MI_WIDTH (mi), MI_HEIGHT (mi));
611 else if(event->xany.type == ButtonRelease &&
612 event->xbutton.button == Button1) {
613 mp->button_down_p = False;
616 else if (event->xany.type == ButtonPress &&
617 (event->xbutton.button == Button4 ||
618 event->xbutton.button == Button5 ||
619 event->xbutton.button == Button6 ||
620 event->xbutton.button == Button7))
622 gltrackball_mousewheel (mp->trackball, event->xbutton.button, 5,
623 !event->xbutton.state);
626 else if(event->xany.type == MotionNotify && mp->button_down_p) {
627 gltrackball_track (mp->trackball,
628 event->xmotion.x, event->xmotion.y,
629 MI_WIDTH (mi), MI_HEIGHT (mi));
636 ENTRYPOINT void init_antinspect(ModeInfo * mi)
638 antinspectstruct *mp;
640 if(antinspect == NULL) {
641 if((antinspect = (antinspectstruct *) calloc(MI_NUM_SCREENS(mi),
642 sizeof (antinspectstruct))) == NULL)
645 mp = &antinspect[MI_SCREEN(mi)];
646 mp->step = NRAND(90);
647 mp->ant_position = NRAND(90);
648 mp->trackball = gltrackball_init ();
650 if ((mp->glx_context = init_GL(mi)) != NULL) {
651 reshape_antinspect(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
652 glDrawBuffer(GL_BACK);
658 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
661 ENTRYPOINT void draw_antinspect(ModeInfo * mi)
663 antinspectstruct *mp;
665 Display *display = MI_DISPLAY(mi);
666 Window window = MI_WINDOW(mi);
670 mp = &antinspect[MI_SCREEN(mi)];
672 MI_IS_DRAWN(mi) = True;
677 glXMakeCurrent(display, window, *(mp->glx_context));
679 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
682 glRotatef(current_device_rotation(), 0, 0, 1);
684 mi->polygon_count = 0;
686 /* position camera --- this works well, we can peer inside
688 glTranslatef(0.0, 0.0, -10.0);
689 gltrackball_rotate(mp->trackball);
690 glRotatef((15.0/2.0 + 15.0*sin(mp->ant_step/100.0)), 1.0, 0.0, 0.0);
691 glRotatef(30.0, 1.0, 0.0, 0.0);
692 glRotatef(180.0, 0.0, 1.0, 0.0);
694 if (!draw_antinspect_strip(mi)) {
695 release_antinspect(mi);
701 if (MI_IS_FPS(mi)) do_fps (mi);
704 glXSwapBuffers(display, window);
710 ENTRYPOINT void change_antinspect(ModeInfo * mi)
712 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
714 if (!mp->glx_context)
717 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));
720 #endif /* !STANDALONE */
723 XSCREENSAVER_MODULE ("AntInspect", antinspect)