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
18 #include <X11/Intrinsic.h>
21 #define PROGCLASS "AntInspect"
22 #define HACK_INIT init_antinspect
23 #define HACK_DRAW draw_antinspect
24 #define HACK_RESHAPE reshape_antinspect
25 #define HACK_HANDLE_EVENT antinspect_handle_event
26 #define EVENT_MASK PointerMotionMask
27 #define antinspect_opts xlockmore_opts
28 #define DEFAULTS "*delay: 20000 \n" \
29 "*showFPS: False \n" \
30 "*wireframe: False \n"
32 #include "xlockmore.h"
38 #include "gltrackball.h"
40 #define DEF_SHADOWS "True"
44 static XrmOptionDescRec opts[] = {
46 (char *) ".antinspect.shadows", XrmoptionNoArg, (caddr_t) "on"},
49 (char *) ".antinspect.shadows", XrmoptionNoArg, (caddr_t) "off"}
52 static argtype vars[] = {
53 {(caddr_t *) &shadows,
54 (char *) "shadows", (char *) "Shadows", (char *) DEF_SHADOWS, t_Bool}
57 static OptionStruct desc[] = {
58 {(char *) "-/+shadows",
59 (char *) "turn on/off ant shadows"}
62 ModeSpecOpt antinspect_opts = {sizeof opts / sizeof opts[0],
64 sizeof vars / sizeof vars[0],
69 ModStruct antinspect_description =
70 {"antinspect", "init_antinspect", "draw_antinspect", "release_antinspect",
71 "draw_antinspect", "change_antinspect", (char *) NULL, &antinspect_opts,
72 1000, 1, 1, 1, 4, 1.0, "",
73 "draws some ants", 0, NULL};
76 #define Scale4Window 0.3
77 #define Scale4Iconic 0.4
79 #define sqr(A) ((A)*(A))
85 #define ObjAntinspectStrip 0
89 /*************************************************************************/
95 GLXContext *glx_context;
96 trackball_state *trackball;
100 static float front_shininess[] = {60.0};
101 static float front_specular[] = {0.7, 0.7, 0.7, 1.0};
102 static float ambient[] = {0.0, 0.0, 0.0, 1.0};
103 static float diffuse[] = {1.0, 1.0, 1.0, 1.0};
104 static float position0[] = {0.0, 3.0, 0.0, 1.0};
105 static float position1[] = {-1.0, -3.0, 1.0, 0.0};
106 static float lmodel_ambient[] = {0.5, 0.5, 0.5, 1.0};
107 static float lmodel_twoside[] = {GL_TRUE};
109 static float MaterialRed[] = {0.6, 0.0, 0.0, 1.0};
110 static float MaterialOrange[] = {1.0, 0.69, 0.00, 1.0};
111 static float MaterialGray[] = {0.2, 0.2, 0.2, 1.0};
112 static float MaterialBlack[] = {0.1, 0.1, 0.1, 0.4};
113 static float MaterialShadow[] = {0.3, 0.3, 0.3, 0.3};
114 static float MaterialGray5[] = {0.5, 0.5, 0.5, 0.3};
115 static float MaterialGray6[] = {0.6, 0.6, 0.6, 1.0};
117 static antinspectstruct *antinspect = (antinspectstruct *) NULL;
124 /* create a matrix that will project the desired shadow */
125 void shadowmatrix(GLfloat shadowMat[4][4],
126 GLfloat groundplane[4],
127 GLfloat lightpos[4]) {
130 /* find dot product between light position vector and ground plane normal */
131 dot = groundplane[X] * lightpos[X] +
132 groundplane[Y] * lightpos[Y] +
133 groundplane[Z] * lightpos[Z] +
134 groundplane[W] * lightpos[W];
136 shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
137 shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
138 shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
139 shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
141 shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
142 shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
143 shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
144 shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
146 shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
147 shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
148 shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
149 shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
151 shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
152 shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
153 shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
154 shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
157 GLfloat ground[4] = {0.0, 1.0, 0.0, -0.00001};
159 /* simple filled sphere */
160 static Bool mySphere(float radius) {
161 GLUquadricObj *quadObj;
163 if((quadObj = gluNewQuadric()) == 0)
165 gluQuadricDrawStyle(quadObj, (GLenum) GLU_FILL);
166 gluSphere(quadObj, radius, 16, 16);
167 gluDeleteQuadric(quadObj);
173 static Bool mySphere2(float radius) {
174 GLUquadricObj *quadObj;
176 if((quadObj = gluNewQuadric()) == 0)
178 gluQuadricDrawStyle(quadObj, (GLenum) GLU_LINE);/*GLU_SILHOUETTE);*/
179 gluSphere(quadObj, radius, 16, 8);
180 gluDeleteQuadric(quadObj);
186 static Bool myCone2(float radius) {
191 static float ant_step = 0;
194 static Bool draw_antinspect_ant(antinspectstruct * mp, float *Material, int mono,
195 Bool (*sphere)(float), Bool (*cone)(float)) {
196 float cos1 = cos(ant_step);
197 float cos2 = cos(ant_step + 2 * Pi / 3);
198 float cos3 = cos(ant_step + 4 * Pi / 3);
199 float sin1 = sin(ant_step);
200 float sin2 = sin(ant_step + 2 * Pi / 3);
201 float sin3 = sin(ant_step + 4 * Pi / 3);
204 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
206 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Material);
207 glEnable(GL_CULL_FACE);
210 if (!((*sphere)(0.18)))
212 glScalef(1, 1 / 1.3, 1);
213 glTranslatef(0.00, 0.30, 0.00);
214 if (!((*sphere)(0.2)))
217 glTranslatef(-0.05, 0.17, 0.05);
218 glRotatef(-90, 1, 0, 0);
219 glRotatef(-25, 0, 1, 0);
220 if (!((*cone)(0.05)))
222 glTranslatef(0.00, 0.10, 0.00);
223 if (!((*cone)(0.05)))
225 glRotatef(25, 0, 1, 0);
226 glRotatef(90, 1, 0, 0);
229 glTranslatef(0.15, -0.65, 0.05);
230 if (!((*sphere)(0.25)))
232 glScalef(1, 1 / 1.3, 1);
234 glDisable(GL_CULL_FACE);
236 glDisable(GL_LIGHTING);
241 glColor3fv(MaterialGray5);
243 glColor3fv(Material);
244 glVertex3f(0.00, 0.30, 0.00);
245 glColor3fv(MaterialGray);
246 glVertex3f(0.40, 0.70, 0.40);
248 glColor3fv(MaterialGray5);
250 glColor3fv(Material);
251 glVertex3f(0.00, 0.30, 0.00);
252 glColor3fv(MaterialGray);
253 glVertex3f(0.40, 0.70, -0.40);
257 glColor3fv(MaterialGray6);
259 glColor3fv(Material);
260 glVertex3f(0.40, 0.70, 0.40);
261 glVertex3f(0.40, 0.70, -0.40);
265 glBegin(GL_LINE_STRIP);
267 glColor3fv(MaterialGray5);
269 glColor3fv(Material);
270 glVertex3f(0.00, 0.05, 0.18);
271 glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
272 glColor3fv(MaterialGray);
273 glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
276 /* LEFT-CENTER ARM */
277 glBegin(GL_LINE_STRIP);
279 glColor3fv(MaterialGray5);
281 glColor3fv(Material);
282 glVertex3f(0.00, 0.00, 0.18);
283 glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
284 glColor3fv(MaterialGray);
285 glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
289 glBegin(GL_LINE_STRIP);
291 glColor3fv(MaterialGray5);
293 glColor3fv(Material);
294 glVertex3f(0.00, -0.05, 0.18);
295 glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
296 glColor3fv(MaterialGray);
297 glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
300 /* RIGHT-FRONT ARM */
301 glBegin(GL_LINE_STRIP);
303 glColor3fv(MaterialGray5);
305 glColor3fv(Material);
306 glVertex3f(0.00, 0.05, -0.18);
307 glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
308 glColor3fv(MaterialGray);
309 glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
312 /* RIGHT-CENTER ARM */
313 glBegin(GL_LINE_STRIP);
315 glColor3fv(MaterialGray5);
317 glColor3fv(Material);
318 glVertex3f(0.00, 0.00, -0.18);
319 glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
320 glColor3fv(MaterialGray);
321 glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
325 glBegin(GL_LINE_STRIP);
327 glColor3fv(MaterialGray5);
329 glColor3fv(Material);
330 glVertex3f(0.00, -0.05, -0.18);
331 glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
332 glColor3fv(MaterialGray);
333 glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
336 glEnable(GL_LIGHTING);
341 /* only works with 3 right now */
344 float MaterialBen[4] = {0.25, 0.30, 0.46, 1.0};
346 static float* antmaterial[ANTCOUNT] =
347 {MaterialRed, MaterialBen, MaterialOrange};
348 static double antposition[ANTCOUNT] = {0.0, 120.0, 240.0};
349 static double antvelocity[ANTCOUNT] = {0.3, 0.3, 0.3};
350 static double antsphere[ANTCOUNT] = {1.2, 1.2, 1.2};
353 static double antorder[6][ANTCOUNT] = {{0, 1, 2},
361 static Bool draw_antinspect_strip(ModeInfo * mi) {
362 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
364 int mono = MI_IS_MONO(mi);
366 int ro = (((int)antposition[1])/(360/(2*ANTCOUNT))) % (2*ANTCOUNT);
368 glEnable(GL_TEXTURE_2D);
370 glLightfv(GL_LIGHT0, GL_POSITION, position0);
372 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
373 glRotatef(-30.0, 0.0, 1.0, 0.0);
375 glDisable(GL_TEXTURE_2D);
378 /* render ground plane */
379 glBegin(GL_TRIANGLES);
380 glColor4fv(MaterialShadow);
381 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlack);
382 glNormal3f(0.0, 1.0, 0.0);
385 glVertex3f(0.0, 0.0, -1.0);
386 glVertex3f(-sqrt(3.0)/2.0, 0.0, 0.5);
387 glVertex3f(sqrt(3.0)/2.0, 0.0, 0.5);
391 for(i = 0; i < 3; ++i) {
392 glRotatef(120.0, 0.0, 1.0, 0.0);
393 glBegin(GL_TRIANGLES);
394 glVertex3f(0.0, 0.0, 1.0 + 3.0);
395 glVertex3f(sqrt(3.0)/2.0, 0.0, -0.5 + 3.0);
396 glVertex3f(-sqrt(3.0)/2.0, 0.0, -0.5 + 3.0);
400 /* first render shadows -- no depth required */
403 shadowmatrix(m, ground, position0);
405 glColor4fv(MaterialShadow);
406 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
409 glDisable(GL_LIGHTING);
411 /* display ant shadow */
413 glTranslatef(0.0, 0.001, 0.0);
416 for(i = 0; i < ANTCOUNT; ++i) {
422 glRotatef(antposition[i], 0.0, 1.0, 0.0);
423 glTranslatef(2.4, 0.0, 0.0);
424 glTranslatef(0.0, antsphere[i], 0.0);
425 glRotatef(90.0, 0.0, 1.0, 0.0);
428 glRotatef(10.0, 0.0, 1.0, 0.0);
429 glRotatef(40.0, 0.0, 0.0, 1.0);
430 glTranslatef(0.0, -0.8, 0.0);
431 glRotatef(180.0, 0.0, 1.0, 0.0);
432 glRotatef(90.0, 0.0, 0.0, 1.0);
435 glColor4fv(MaterialShadow);
436 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
438 if(antposition[i] > 360.0)
439 antposition[i] = 0.0;
440 draw_antinspect_ant(mp, MaterialShadow, mono, mySphere2, myCone2);
443 glDisable(GL_LIGHTING);
446 glRotatef(-20.0, 1.0, 0.0, 0.0);
447 glRotatef(-ant_step*2, 0.0, 0.0, 1.0);
448 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
457 glEnable(GL_LIGHTING);
460 for(j = 0; j < ANTCOUNT; ++j) {
461 /* determine rendering order */
467 glRotatef(antposition[i], 0.0, 1.0, 0.0);
468 glTranslatef(2.4, 0.0, 0.0);
469 glTranslatef(0.0, antsphere[i], 0.0);
470 glRotatef(90.0, 0.0, 1.0, 0.0);
474 glRotatef(10.0, 0.0, 1.0, 0.0);
475 glRotatef(40.0, 0.0, 0.0, 1.0);
476 glTranslatef(0.0, -0.8, 0.0);
477 glRotatef(180.0, 0.0, 1.0, 0.0);
478 glRotatef(90.0, 0.0, 0.0, 1.0);
479 if(antposition[i] > 360.0)
480 antposition[i] = 0.0;
482 draw_antinspect_ant(mp, antmaterial[i], mono, mySphere2, myCone2);
487 glRotatef(-20.0, 1.0, 0.0, 0.0);
488 glRotatef(-ant_step*2, 0.0, 0.0, 1.0);
489 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mono ? MaterialGray5 : antmaterial[i]);
492 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlack);
498 /* finally, evolve */
499 antposition[i] += antvelocity[i];
502 /* but the step size is the same! */
505 mp->ant_position += 1;
509 void reshape_antinspect(ModeInfo * mi, int width, int height) {
510 double h = (GLfloat) height / (GLfloat) width;
511 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
512 linewidth = (width / 512) + 1;
514 glViewport(0, 0, mp->WindW = (GLint) width, mp->WindH = (GLint) height);
515 glMatrixMode(GL_PROJECTION);
518 gluPerspective(45, 1/h, 7.0, 20.0);
520 glMatrixMode(GL_MODELVIEW);
521 glLineWidth(linewidth);
522 glPointSize(linewidth);
525 static void pinit(void) {
527 glClearColor(0.0, 0.0, 0.0, 1.0);
529 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
530 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
531 glLightfv(GL_LIGHT0, GL_POSITION, position0);
532 glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
533 glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
534 glLightfv(GL_LIGHT1, GL_POSITION, position1);
535 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
536 glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
537 glEnable(GL_LIGHTING);
540 glEnable(GL_NORMALIZE);
544 glShadeModel(GL_SMOOTH);
545 glEnable(GL_DEPTH_TEST);
546 glDisable(GL_TEXTURE_2D);
548 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, front_shininess);
549 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, front_specular);
552 void release_antinspect(ModeInfo * mi) {
554 free((void *) antinspect);
555 antinspect = (antinspectstruct *) NULL;
560 Bool antinspect_handle_event (ModeInfo *mi, XEvent *event) {
561 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
563 if(event->xany.type == ButtonPress && event->xbutton.button & Button1) {
564 mp->button_down_p = True;
565 gltrackball_start(mp->trackball,
566 event->xbutton.x, event->xbutton.y,
567 MI_WIDTH (mi), MI_HEIGHT (mi));
570 else if(event->xany.type == ButtonRelease &&
571 event->xbutton.button & Button1) {
572 mp->button_down_p = False;
575 else if(event->xany.type == MotionNotify && mp->button_down_p) {
576 gltrackball_track (mp->trackball,
577 event->xmotion.x, event->xmotion.y,
578 MI_WIDTH (mi), MI_HEIGHT (mi));
585 void init_antinspect(ModeInfo * mi) {
586 antinspectstruct *mp;
588 if(antinspect == NULL) {
589 if((antinspect = (antinspectstruct *) calloc(MI_NUM_SCREENS(mi),
590 sizeof (antinspectstruct))) == NULL)
593 mp = &antinspect[MI_SCREEN(mi)];
594 mp->step = NRAND(90);
595 mp->ant_position = NRAND(90);
596 mp->trackball = gltrackball_init ();
598 if ((mp->glx_context = init_GL(mi)) != NULL) {
599 reshape_antinspect(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
600 glDrawBuffer(GL_BACK);
606 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
609 void draw_antinspect(ModeInfo * mi) {
610 antinspectstruct *mp;
612 Display *display = MI_DISPLAY(mi);
613 Window window = MI_WINDOW(mi);
617 mp = &antinspect[MI_SCREEN(mi)];
619 MI_IS_DRAWN(mi) = True;
624 glXMakeCurrent(display, window, *(mp->glx_context));
626 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
630 /* position camera --- this works well, we can peer inside
632 glTranslatef(0.0, 0.0, -10.0);
633 gltrackball_rotate(mp->trackball);
634 glRotatef((15.0/2.0 + 15.0*sin(ant_step/100.0)), 1.0, 0.0, 0.0);
635 glRotatef(30.0, 1.0, 0.0, 0.0);
636 glRotatef(180.0, 0.0, 1.0, 0.0);
638 if (!draw_antinspect_strip(mi)) {
639 release_antinspect(mi);
645 if (MI_IS_FPS(mi)) do_fps (mi);
648 glXSwapBuffers(display, window);
653 void change_antinspect(ModeInfo * mi) {
654 antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
656 if (!mp->glx_context)
659 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(mp->glx_context));