819ef5c1b222457d66073659ff80cc84c476c2e6
[xscreensaver] / hacks / glx / endgame.c
1 /*
2  * endgame.c
3  * plays through a chess game ending.  enjoy.
4  *
5  * version 1.0 - June 6, 2002
6  *
7  * Copyright (C) 2002-2008 Blair Tennessy (tennessb@unbc.ca)
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation.  No representations are made about the suitability of this
14  * software for any purpose.  It is provided "as is" without express or
15  * implied warranty.
16  */
17
18 #ifdef STANDALONE
19 #define DEFAULTS       "*delay:     20000 \n" \
20                        "*showFPS:   False \n" \
21                        "*wireframe: False \n" \
22
23 # define refresh_chess 0
24 # include "xlockmore.h"
25
26 #else
27 # include "xlock.h"
28 #endif
29
30 #ifdef USE_GL
31
32 #define BOARDSIZE 8
33 #define WHITES 5
34
35 #include "gltrackball.h"
36 #include "chessmodels.h"
37 #include "chessgames.h"
38
39 #undef countof
40 #define countof(x) (sizeof((x))/sizeof((*x)))
41
42 #define DEF_ROTATE      "True"
43 #define DEF_REFLECTIONS "True"
44 #define DEF_SHADOWS     "True"
45 #define DEF_SMOOTH      "True"
46 #define DEF_CLASSIC     "False"
47
48
49 static XrmOptionDescRec opts[] = {
50   {"+rotate", ".chess.rotate", XrmoptionNoArg, "false" },
51   {"-rotate", ".chess.rotate", XrmoptionNoArg, "true" },
52   {"+reflections", ".chess.reflections", XrmoptionNoArg, "false" },
53   {"-reflections", ".chess.reflections", XrmoptionNoArg, "true" },
54   {"+shadows", ".chess.shadows", XrmoptionNoArg, "false" },
55   {"-shadows", ".chess.shadows", XrmoptionNoArg, "true" },
56   {"+smooth", ".chess.smooth", XrmoptionNoArg, "false" },
57   {"-smooth", ".chess.smooth", XrmoptionNoArg, "true" },
58   {"+classic", ".chess.classic", XrmoptionNoArg, "false" },
59   {"-classic", ".chess.classic", XrmoptionNoArg, "true" },
60 };
61
62 static int rotate, reflections, smooth, shadows, classic;
63
64 static argtype vars[] = {
65   {&rotate,      "rotate",      "Rotate",      DEF_ROTATE, t_Bool},
66   {&reflections, "reflections", "Reflections", DEF_REFLECTIONS, t_Bool},
67   {&shadows,     "shadows",      "Shadows",    DEF_SHADOWS, t_Bool},
68   {&smooth,      "smooth",      "Smooth",      DEF_SMOOTH, t_Bool},
69   {&classic,     "classic",     "Classic",     DEF_CLASSIC, t_Bool},
70 };
71
72 ENTRYPOINT ModeSpecOpt chess_opts = {countof(opts), opts, countof(vars), vars, NULL};
73
74 #ifdef USE_MODULES
75 ModStruct   chess_description =
76 {"chess", "init_chess", "draw_chess", "release_chess",
77  "draw_chess", "init_chess", NULL, &chess_opts,
78  1000, 1, 2, 1, 4, 1.0, "",
79  "Chess", 0, NULL};
80
81 #endif
82
83 #define checkImageWidth 16
84 #define checkImageHeight 16
85
86 typedef struct {
87   GLXContext *glx_context;
88   Window window;
89   trackball_state *trackball;
90   Bool button_down_p;
91
92   ChessGame game;
93   int oldwhite;
94
95   /** definition of white/black (orange/gray) colors */
96   GLfloat colors[2][3];
97
98   GLubyte checkImage[checkImageWidth][checkImageHeight][3];
99   GLuint piecetexture, boardtexture;
100
101   int mpiece, tpiece, steps, done;
102   double from[2], to[2];
103   double dx, dz;
104   int moving, take, mc, count, wire;
105   double theta;
106
107   GLfloat position[4];
108   GLfloat position2[4];
109
110   GLfloat mod;
111
112   GLfloat ground[4];
113
114   int oldgame;
115
116   int poly_counts[PIECES];  /* polygon count of each type of piece */
117
118
119 } Chesscreen;
120
121 static Chesscreen *qs = NULL;
122
123 static const GLfloat MaterialShadow[] =   {0.0, 0.0, 0.0, 0.3};
124
125
126 /* i prefer silvertip */
127 static const GLfloat whites[WHITES][3] = 
128   {
129     {1.0, 0.55, 0.1},
130     {0.8, 0.52, 0.8},
131     {0.43, 0.54, 0.76},
132     {0.2, 0.2, 0.2},
133     {0.35, 0.60, 0.35},
134   };
135
136 static void build_colors(Chesscreen *cs) 
137 {
138
139   /* find new white */
140   int newwhite = cs->oldwhite;
141   while(newwhite == cs->oldwhite)
142     newwhite = random()%WHITES;
143   cs->oldwhite = newwhite;
144
145   cs->colors[0][0] = whites[cs->oldwhite][0];
146   cs->colors[0][1] = whites[cs->oldwhite][1];
147   cs->colors[0][2] = whites[cs->oldwhite][2];
148 }
149
150 /* build piece texture */
151 static void make_piece_texture(Chesscreen *cs) 
152 {
153   int i, j, c;
154
155   for (i = 0; i < checkImageWidth; i++) {
156     for (j = 0; j < checkImageHeight; j++) {
157       c = ((j%2) == 0 || i%2 == 0) ? 240 : 180+random()%16;
158       cs->checkImage[i][j][0] = (GLubyte) c;
159       cs->checkImage[i][j][1] = (GLubyte) c;
160       cs->checkImage[i][j][2] = (GLubyte) c;
161     }
162   }
163
164   glGenTextures(1, &cs->piecetexture);
165   glBindTexture(GL_TEXTURE_2D, cs->piecetexture);
166
167   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
168   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
169   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
170   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
171   glTexImage2D(GL_TEXTURE_2D, 0, 3, checkImageWidth, 
172                checkImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 
173                &cs->checkImage[0][0]);
174 }
175
176 /* build board texture (uniform noise in [180,180+50]) */
177 static void make_board_texture(Chesscreen *cs) 
178 {
179   int i, j, c;
180
181   for (i = 0; i < checkImageWidth; i++) {
182     for (j = 0; j < checkImageHeight; j++) {
183       c = 180 + random()%51;
184       cs->checkImage[i][j][0] = (GLubyte) c;
185       cs->checkImage[i][j][1] = (GLubyte) c;
186       cs->checkImage[i][j][2] = (GLubyte) c;
187     }
188   }
189
190   glGenTextures(1, &cs->boardtexture);
191   glBindTexture(GL_TEXTURE_2D, cs->boardtexture);
192
193   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
194   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
195   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
196   glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
197   glTexImage2D(GL_TEXTURE_2D, 0, 3, checkImageWidth, 
198                checkImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 
199                &cs->checkImage[0][0]);
200 }
201
202 /** handle X event (trackball) */
203 ENTRYPOINT Bool chess_handle_event (ModeInfo *mi, XEvent *event) 
204 {
205   Chesscreen *cs = &qs[MI_SCREEN(mi)];
206
207   if(event->xany.type == ButtonPress && event->xbutton.button == Button1) {
208     cs->button_down_p = True;
209     gltrackball_start (cs->trackball,
210                        event->xbutton.x, event->xbutton.y,
211                        MI_WIDTH (mi), MI_HEIGHT (mi));
212     return True;
213   }
214   else if(event->xany.type == ButtonRelease 
215           && event->xbutton.button == Button1) {
216     cs->button_down_p = False;
217     return True;
218   }
219   else if (event->xany.type == ButtonPress &&
220            (event->xbutton.button == Button4 ||
221             event->xbutton.button == Button5 ||
222             event->xbutton.button == Button6 ||
223             event->xbutton.button == Button7))
224     {
225       gltrackball_mousewheel (cs->trackball, event->xbutton.button, 5,
226                               !event->xbutton.state);
227       return True;
228     }
229   else if(event->xany.type == MotionNotify && cs->button_down_p) {
230     gltrackball_track (cs->trackball,
231                        event->xmotion.x, event->xmotion.y,
232                        MI_WIDTH (mi), MI_HEIGHT (mi));
233     return True;
234   }
235   
236   return False;
237 }
238
239 static const GLfloat diffuse2[] = {1.0, 1.0, 1.0, 1.0};
240 /*static const GLfloat ambient2[] = {0.7, 0.7, 0.7, 1.0};*/
241 static const GLfloat shininess[] = {60.0};
242 static const GLfloat specular[] = {0.4, 0.4, 0.4, 1.0};
243
244 /* configure lighting */
245 static void setup_lights(Chesscreen *cs) 
246 {
247   glEnable(GL_LIGHTING);
248   glLightfv(GL_LIGHT0, GL_POSITION, cs->position);
249   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse2);
250   glEnable(GL_LIGHT0);
251
252 /*   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient2); */
253
254   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
255   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
256
257   glLightfv(GL_LIGHT1, GL_SPECULAR, diffuse2);
258   glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse2);
259   glEnable(GL_LIGHT1);
260 }
261
262 /* draw pieces */
263 static void drawPieces(ModeInfo *mi, Chesscreen *cs) 
264 {
265   int i, j;
266
267   for(i = 0; i < BOARDSIZE; ++i) {
268     for(j = 0; j < BOARDSIZE; ++j) {
269       if(cs->game.board[i][j]) {        
270         int c = cs->game.board[i][j]/PIECES;
271         glColor3fv(cs->colors[c]);
272         glCallList(cs->game.board[i][j]%PIECES);
273         mi->polygon_count += cs->poly_counts[cs->game.board[i][j]%PIECES];
274       }
275       
276       glTranslatef(1.0, 0.0, 0.0);
277     }
278     
279     glTranslatef(-1.0*BOARDSIZE, 0.0, 1.0);
280   }
281
282   glTranslatef(0.0, 0.0, -1.0*BOARDSIZE);
283 }
284
285 /* draw pieces */
286 static void drawPiecesShadow(ModeInfo *mi, Chesscreen *cs) 
287 {
288   int i, j;
289
290   for(i = 0; i < BOARDSIZE; ++i) {
291     for(j = 0; j < BOARDSIZE; ++j) {
292       if(cs->game.board[i][j]) {        
293         glColor4f(0.0, 0.0, 0.0, 0.4);
294         glCallList(cs->game.board[i][j]%PIECES);
295         mi->polygon_count += cs->poly_counts[cs->game.board[i][j]%PIECES];
296       }
297       
298       glTranslatef(1.0, 0.0, 0.0);
299     }
300     
301     glTranslatef(-1.0*BOARDSIZE, 0.0, 1.0);
302   }
303
304   glTranslatef(0.0, 0.0, -1.0*BOARDSIZE);
305 }
306
307 /* draw a moving piece */
308 static void drawMovingPiece(ModeInfo *mi, Chesscreen *cs, int shadow) 
309 {
310   int piece = cs->mpiece % PIECES;
311
312   glPushMatrix();
313
314   if(shadow) glColor4fv(MaterialShadow);
315   else glColor3fv(cs->colors[cs->mpiece/PIECES]);
316
317   /** assume a queening.  should be more general */
318   if((cs->mpiece == PAWN  && fabs(cs->to[0]) < 0.01) || 
319      (cs->mpiece == BPAWN && fabs(cs->to[0]-7.0) < 0.01)) {
320     glTranslatef(cs->from[1]+cs->steps*cs->dx, 0.0, cs->from[0]+cs->steps*cs->dz);
321
322     glColor4f(shadow ? MaterialShadow[0] : cs->colors[cs->mpiece/7][0], 
323               shadow ? MaterialShadow[1] : cs->colors[cs->mpiece/7][1], 
324               shadow ? MaterialShadow[2] : cs->colors[cs->mpiece/7][2],
325               (fabs(50.0-cs->steps))/50.0);
326
327     piece = cs->steps < 50 ? PAWN : QUEEN;
328
329     /* what a kludge */
330     if(cs->steps == 99)
331       cs->mpiece = cs->mpiece == PAWN ? QUEEN : BQUEEN;
332   }
333   else if(cs->mpiece % PIECES == KNIGHT) {
334     GLfloat shine[1];
335     GLfloat spec[4];
336     GLfloat mult;
337     glTranslatef(cs->steps < 50 ? cs->from[1] : cs->to[1], 0.0, 
338                  cs->steps < 50 ? cs->from[0] : cs->to[0]);
339
340     mult = cs->steps < 10 
341       ? (1.0 - cs->steps / 10.0) : 100 - cs->steps < 10 
342       ? (1.0 - (100 - cs->steps) / 10.0) : 0.0;
343
344     shine[0] = mult*shininess[0];
345     spec[0] = mult*specular[0];
346     spec[1] = mult*specular[1];
347     spec[2] = mult*specular[2];
348     spec[3] = 1.0;
349     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shine);
350     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
351
352     glColor4f(shadow ? MaterialShadow[0] : cs->colors[cs->mpiece/7][0], 
353               shadow ? MaterialShadow[1] : cs->colors[cs->mpiece/7][1], 
354               shadow ? MaterialShadow[2] : cs->colors[cs->mpiece/7][2],
355               fabs(49-cs->steps)/49.0);
356
357     glScalef(fabs(49-cs->steps)/49.0, fabs(49-cs->steps)/49.0, fabs(49-cs->steps)/49.0);
358   }
359   else
360     glTranslatef(cs->from[1]+cs->steps*cs->dx, 0.0, cs->from[0]+cs->steps*cs->dz);
361
362   if(!cs->wire)
363     glEnable(GL_BLEND);
364   
365   glCallList(piece);
366   mi->polygon_count += cs->poly_counts[cs->mpiece % PIECES];
367
368   glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
369   glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
370
371   glPopMatrix();
372
373   if(!cs->wire)
374     glDisable(GL_BLEND);
375 }
376
377 /** code to squish a taken piece */
378 static void drawTakePiece(ModeInfo *mi, Chesscreen *cs, int shadow) 
379 {
380   if(!cs->wire)
381     glEnable(GL_BLEND);
382
383   glColor4f(shadow ? MaterialShadow[0] : cs->colors[cs->tpiece/7][0], 
384             shadow ? MaterialShadow[1] : cs->colors[cs->tpiece/7][1], 
385             shadow ? MaterialShadow[2] : cs->colors[cs->tpiece/7][2],
386             (100-1.6*cs->steps)/100.0);
387
388   glTranslatef(cs->to[1], 0.0, cs->to[0]);
389   
390   if(cs->mpiece % PIECES == KNIGHT)
391     glScalef(1.0+cs->steps/100.0, 1.0, 1.0+cs->steps/100.0);
392   else
393     glScalef(1.0, 1 - cs->steps/50.0 > 0.01 ? 1 - cs->steps/50.0 : 0.01, 1.0);
394   glCallList(cs->tpiece % 7);
395   mi->polygon_count += cs->poly_counts[cs->tpiece % PIECES];
396   
397   if(!cs->wire)
398     glDisable(GL_BLEND);
399 }
400
401 /** draw board */
402 static void drawBoard(ModeInfo *mi, Chesscreen *cs) 
403 {
404   int i, j;
405
406   glBegin(GL_QUADS);
407
408   for(i = 0; i < BOARDSIZE; ++i)
409     for(j = 0; j < BOARDSIZE; ++j) {
410       double ma1 = (i+j)%2 == 0 ? cs->mod*i : 0.0;
411       double mb1 = (i+j)%2 == 0 ? cs->mod*j : 0.0;
412       double ma2 = (i+j)%2 == 0 ? cs->mod*(i+1.0) : 0.01;
413       double mb2 = (i+j)%2 == 0 ? cs->mod*(j+1.0) : 0.01;
414
415       /*glColor3fv(colors[(i+j)%2]);*/
416       glColor4f(cs->colors[(i+j)%2][0], cs->colors[(i+j)%2][1],
417                 cs->colors[(i+j)%2][2], 0.65);
418       
419       glNormal3f(0.0, 1.0, 0.0);
420 /*       glTexCoord2f(mod*i, mod*(j+1.0)); */
421       glTexCoord2f(ma1, mb2);
422       glVertex3f(i, 0.0, j + 1.0);
423 /*       glTexCoord2f(mod*(i+1.0), mod*(j+1.0)); */
424       glTexCoord2f(ma2, mb2);
425       glVertex3f(i + 1.0, 0.0, j + 1.0);
426       glTexCoord2f(ma2, mb1);
427 /*       glTexCoord2f(mod*(i+1.0), mod*j); */
428       glVertex3f(i + 1.0, 0.0, j);
429       glTexCoord2f(ma1, mb1);
430 /*       glTexCoord2f(mod*i, mod*j); */
431       glVertex3f(i, 0.0, j);
432
433       mi->polygon_count++;
434     }
435
436   /* chop underneath board */
437 /*   glColor3f(0, 0, 0); */
438 /*   glNormal3f(0, -1, 0); */
439 /*   glVertex3f(0,         0,  BOARDSIZE); */
440 /*   glVertex3f(0,         0,  0); */
441 /*   glVertex3f(BOARDSIZE, 0,  0); */
442 /*   glVertex3f(BOARDSIZE, 0,  BOARDSIZE); */
443   glEnd();
444 }
445
446 static void draw_pieces(ModeInfo *mi, Chesscreen *cs, int wire) 
447 {
448   if (!cs->wire) {
449     glEnable(GL_TEXTURE_2D);
450     glBindTexture(GL_TEXTURE_2D, cs->piecetexture);
451     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
452
453     glColor4f(0.5, 0.5, 0.5, 1.0);
454     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
455   }
456
457   drawPieces(mi, cs);
458   if(cs->moving) drawMovingPiece(mi, cs, 0);
459   if(cs->take) drawTakePiece(mi, cs, 0);
460   glDisable(GL_TEXTURE_2D);
461 }
462
463 static void draw_shadow_pieces(ModeInfo *mi, Chesscreen *cs, int wire) 
464 {
465   if (!cs->wire) {
466     glEnable(GL_TEXTURE_2D);
467     glBindTexture(GL_TEXTURE_2D, cs->piecetexture);
468     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
469   }
470
471   /* use the stencil */
472   glDisable(GL_LIGHTING);
473   glDisable(GL_COLOR_MATERIAL);
474   glDisable(GL_DEPTH_TEST);
475   glDisable(GL_TEXTURE_2D);
476   glDisable(GL_BLEND);
477
478   glClear(GL_STENCIL_BUFFER_BIT);
479   glColorMask(0,0,0,0);
480   glEnable(GL_STENCIL_TEST);
481
482   glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFFL);
483   glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
484   
485
486   glPushMatrix();
487   glTranslatef(0.0, 0.001, 0.0);
488
489   /* draw the pieces */
490   drawPiecesShadow(mi, cs);
491   if(cs->moving) drawMovingPiece(mi, cs, shadows);
492   if(cs->take) drawTakePiece(mi, cs, shadows);
493
494   glPopMatrix();
495
496
497   /* turn on drawing into colour buffer */
498   glColorMask(1,1,1,1);
499
500   /* programming with effect */
501   glDisable(GL_LIGHTING);
502   glDisable(GL_COLOR_MATERIAL);
503   glDisable(GL_TEXTURE_2D);
504
505   /* now draw the union of the shadows */
506
507   /* 
508      <todo>
509      want to keep alpha values (alpha is involved in transition
510      effects of the active pieces).
511      </todo>
512   */
513   glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFFL);
514   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
515
516   glEnable(GL_BLEND);
517
518   glColor4fv(MaterialShadow);
519
520   /* draw the board generously to fill the shadows */
521   glBegin(GL_QUADS);
522
523   glVertex3f(-1.0, 0.0, -1.0);
524   glVertex3f(-1.0, 0.0, BOARDSIZE + 1.0);
525   glVertex3f(1.0 + BOARDSIZE, 0.0, BOARDSIZE + 1.0);
526   glVertex3f(1.0 + BOARDSIZE, 0.0, -1.0);
527
528   glEnd();
529
530   glDisable(GL_STENCIL_TEST);
531
532   /* "pop" attributes */
533   glEnable(GL_TEXTURE_2D);
534   glEnable(GL_COLOR_MATERIAL);
535   glEnable(GL_LIGHTING);
536   glEnable(GL_CULL_FACE);
537
538
539
540 }
541
542 enum {X, Y, Z, W};
543 enum {A, B, C, D};
544
545 /* create a matrix that will project the desired shadow */
546 static void shadowmatrix(GLfloat shadowMat[4][4],
547                   GLfloat groundplane[4],
548                   GLfloat lightpos[4]) 
549 {
550   GLfloat dot;
551
552   /* find dot product between light position vector and ground plane normal */
553   dot = groundplane[X] * lightpos[X] +
554         groundplane[Y] * lightpos[Y] +
555         groundplane[Z] * lightpos[Z] +
556         groundplane[W] * lightpos[W];
557
558   shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
559   shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
560   shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
561   shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
562
563   shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
564   shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
565   shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
566   shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
567
568   shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
569   shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
570   shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
571   shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
572
573   shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
574   shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
575   shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
576   shadowMat[3][3] = dot - lightpos[W] * groundplane[W];
577 }
578
579 /** reflectionboard */
580 static void draw_reflections(ModeInfo *mi, Chesscreen *cs) 
581 {
582   int i, j;
583
584   glEnable(GL_STENCIL_TEST);
585   glStencilFunc(GL_ALWAYS, 1, 1);
586   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
587   glColorMask(0,0,0,0);
588   glDisable(GL_CULL_FACE);
589
590   glDisable(GL_DEPTH_TEST);
591   glBegin(GL_QUADS);
592
593   /* only draw white squares */
594   for(i = 0; i < BOARDSIZE; ++i) {
595     for(j = (BOARDSIZE+i) % 2; j < BOARDSIZE; j += 2) {
596       glVertex3f(i, 0.0, j + 1.0);
597       glVertex3f(i + 1.0, 0.0, j + 1.0);
598       glVertex3f(i + 1.0, 0.0, j);
599       glVertex3f(i, 0.0, j);
600       mi->polygon_count++;
601     }
602   }
603   glEnd();
604   glEnable(GL_DEPTH_TEST);
605
606   glColorMask(1, 1, 1, 1);
607   glStencilFunc(GL_EQUAL, 1, 1);
608   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
609   
610   glPushMatrix(); 
611   glScalef(1.0, -1.0, 1.0);
612   glTranslatef(0.5, 0.0, 0.5);
613
614   glLightfv(GL_LIGHT0, GL_POSITION, cs->position);
615   draw_pieces(mi, cs, cs->wire);
616   glPopMatrix();
617   
618   glDisable(GL_STENCIL_TEST);
619   glLightfv(GL_LIGHT0, GL_POSITION, cs->position);
620
621   glEnable(GL_CULL_FACE);
622   glCullFace(GL_BACK);
623   glColorMask(1,1,1,1);
624 }
625
626 /** draws the scene */
627 static void display(ModeInfo *mi, Chesscreen *cs) 
628 {
629   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
630
631   mi->polygon_count = 0;
632
633   glMatrixMode(GL_MODELVIEW);
634   glLoadIdentity();
635
636   /** setup perspectiv */
637   glTranslatef(0.0, 0.0, -1.5*BOARDSIZE);
638   glRotatef(30.0, 1.0, 0.0, 0.0);
639   gltrackball_rotate (cs->trackball);
640   glRotatef(cs->theta*100, 0.0, 1.0, 0.0);
641   glTranslatef(-0.5*BOARDSIZE, 0.0, -0.5*BOARDSIZE);
642
643 /*   cs->position[0] = 4.0 + 1.0*-sin(cs->theta*100*M_PI/180.0); */
644 /*   cs->position[2] = 4.0 + 1.0* cos(cs->theta*100*M_PI/180.0); */
645 /*   cs->position[1] = 5.0; */
646
647   /* this is the lone light that the shadow matrix is generated from */
648   cs->position[0] = 1.0;
649   cs->position[2] = 1.0;
650   cs->position[1] = 16.0;
651
652   cs->position2[0] = 4.0 + 8.0*-sin(cs->theta*100*M_PI/180.0);
653   cs->position2[2] = 4.0 + 8.0* cos(cs->theta*100*M_PI/180.0);
654
655   if (!cs->wire) {
656     glEnable(GL_LIGHTING);
657     glLightfv(GL_LIGHT0, GL_POSITION, cs->position);
658     glLightfv(GL_LIGHT1, GL_POSITION, cs->position2);
659     glEnable(GL_LIGHT0);
660   }
661
662   /** draw board, pieces */
663   if(!cs->wire) {
664     glEnable(GL_LIGHTING);
665     glEnable(GL_COLOR_MATERIAL);
666
667     if(reflections && !cs->wire) {
668       draw_reflections(mi, cs);
669       glEnable(GL_BLEND);
670     }
671
672     glEnable(GL_TEXTURE_2D);
673     glBindTexture(GL_TEXTURE_2D, cs->boardtexture);
674     drawBoard(mi, cs);
675     glDisable(GL_TEXTURE_2D);
676
677     if(shadows) {
678       /* render shadows */
679       GLfloat m[4][4];
680       shadowmatrix(m, cs->ground, cs->position);
681
682       glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
683       glEnable(GL_BLEND);
684       glDisable(GL_LIGHTING);
685       glDisable(GL_DEPTH_TEST);
686       
687       /* display ant shadow */
688       glPushMatrix();
689       glTranslatef(0.0, 0.001, 0.0);
690       glMultMatrixf(m[0]);
691       glTranslatef(0.5, 0.01, 0.5);
692       draw_shadow_pieces(mi, cs, cs->wire);
693       glPopMatrix();      
694
695       glEnable(GL_LIGHTING);
696       glDisable(GL_BLEND);
697       glEnable(GL_DEPTH_TEST);
698     }
699
700     if(reflections)
701       glDisable(GL_BLEND);
702   }
703   else
704     drawBoard(mi, cs);
705  
706   glTranslatef(0.5, 0.0, 0.5);
707   draw_pieces(mi, cs, cs->wire);
708
709   if(!cs->wire) {
710     glDisable(GL_COLOR_MATERIAL);
711     glDisable(GL_LIGHTING);
712   }
713
714   if (!cs->button_down_p)
715     cs->theta += .002;
716 }
717
718 /** reshape handler */
719 ENTRYPOINT void reshape_chess(ModeInfo *mi, int width, int height) 
720 {
721   GLfloat h = (GLfloat) height / (GLfloat) width;
722   glViewport(0,0, width, height);
723   glMatrixMode(GL_PROJECTION);
724   glLoadIdentity();
725   gluPerspective(45, 1/h, 2.0, 30.0);
726   glMatrixMode(GL_MODELVIEW);
727 }
728
729 /** initialization handler */
730 ENTRYPOINT void init_chess(ModeInfo *mi) 
731 {
732   Chesscreen *cs;
733   int screen = MI_SCREEN(mi);
734
735   if(!qs && 
736      !(qs = (Chesscreen *) calloc(MI_NUM_SCREENS(mi), sizeof(Chesscreen))))
737     return;
738   
739   cs = &qs[screen];
740   cs->window = MI_WINDOW(mi);
741   cs->wire = MI_IS_WIREFRAME(mi);
742   cs->trackball = gltrackball_init ();
743   
744   cs->oldwhite = -1;
745
746   cs->colors[0][0] = 1.0;
747   cs->colors[0][1] = 0.5;
748   cs->colors[0][2] = 0.0;
749
750   cs->colors[1][0] = 0.6;
751   cs->colors[1][1] = 0.6;
752   cs->colors[1][2] = 0.6;
753
754   cs->done = 1;
755   cs->count = 99;
756   cs->mod = 1.4;
757
758 /*   cs->position[0] = 0.0; */
759 /*   cs->position[1] = 5.0; */
760 /*   cs->position[2] = 5.0; */
761 /*   cs->position[3] = 1.0; */
762
763   cs->position[0] = 0.0;
764   cs->position[1] = 24.0;
765   cs->position[2] = 2.0;
766   cs->position[3] = 1.0;
767
768
769   cs->position2[0] = 5.0;
770   cs->position2[1] = 5.0;
771   cs->position2[2] = 5.0;
772   cs->position2[3] = 1.0;
773
774   cs->ground[0] = 0.0;
775   cs->ground[1] = 1.0;
776   cs->ground[2] = 0.0;
777   cs->ground[3] = -0.00001;
778
779   cs->oldgame = -1;
780
781
782   if((cs->glx_context = init_GL(mi)))
783     reshape_chess(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
784   else
785     MI_CLEARWINDOW(mi);
786
787   glClearColor(0.0, 0.0, 0.0, 0.0);
788
789   if (!cs->wire) {
790     glDepthFunc(GL_LEQUAL);
791     glClearStencil(0);
792     glEnable(GL_CULL_FACE);
793     glCullFace(GL_BACK);
794
795     make_piece_texture(cs);
796     make_board_texture(cs);
797   }
798   gen_model_lists( classic, cs->poly_counts);
799
800   if(!cs->wire) {
801     setup_lights(cs);
802     glColorMaterial(GL_FRONT, GL_DIFFUSE);
803     glShadeModel(smooth ? GL_SMOOTH : GL_FLAT);
804     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
805     glEnable(GL_DEPTH_TEST);
806   }
807   else
808     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
809 }
810
811 /** does dirty work drawing scene, moving pieces */
812 ENTRYPOINT void draw_chess(ModeInfo *mi) 
813 {
814   Chesscreen *cs = &qs[MI_SCREEN(mi)];
815   Window w = MI_WINDOW(mi);
816   Display *disp = MI_DISPLAY(mi);
817
818   if(!cs->glx_context)
819     return;
820
821   glXMakeCurrent(disp, w, *(cs->glx_context));
822
823   /** code for moving a piece */
824   if(cs->moving && ++cs->steps == 100) {
825     cs->moving = cs->count = cs->steps = cs->take = 0;
826     cs->game.board[cs->game.moves[cs->mc][2]][cs->game.moves[cs->mc][3]] = cs->mpiece;
827     ++cs->mc;
828     
829     if(cs->mc == cs->game.movecount) {
830       cs->done = 1;
831       cs->mc = 0;
832     }
833   }
834
835   if(++cs->count == 100) {
836     if(!cs->done) {
837       cs->mpiece = cs->game.board[cs->game.moves[cs->mc][0]][cs->game.moves[cs->mc][1]];
838       cs->game.board[cs->game.moves[cs->mc][0]][cs->game.moves[cs->mc][1]] = NONE;
839       
840       if((cs->tpiece = cs->game.board[cs->game.moves[cs->mc][2]][cs->game.moves[cs->mc][3]])) {
841         cs->game.board[cs->game.moves[cs->mc][2]][cs->game.moves[cs->mc][3]] = NONE;
842         cs->take = 1;
843       }
844       
845       cs->from[0] = cs->game.moves[cs->mc][0];
846       cs->from[1] = cs->game.moves[cs->mc][1];
847       cs->to[0] = cs->game.moves[cs->mc][2];
848       cs->to[1] = cs->game.moves[cs->mc][3];
849       
850       cs->dz = (cs->to[0] - cs->from[0]) / 100;
851       cs->dx = (cs->to[1] - cs->from[1]) / 100;
852       cs->steps = 0;
853       cs->moving = 1;
854     }
855     else if(cs->done == 1) {
856       int newgame = cs->oldgame;
857       while(newgame == cs->oldgame)
858         newgame = random()%GAMES;
859
860       /* mod the mod */
861       cs->mod = 0.6 + (random()%20)/10.0;
862
863       /* same old game */
864       cs->oldgame = newgame;
865       cs->game = games[cs->oldgame];
866       build_colors(cs);
867       cs->done = 2;
868       cs->count = 0;
869     }
870     else {
871       cs->done = 0;
872       cs->count = 0;
873     }
874   }
875
876   /* set lighting */
877   if(cs->done) {
878     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 
879              cs->done == 1 ? 1.0+0.1*cs->count : 100.0/cs->count);
880     glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 
881              cs->done == 1 ? 1.0+0.1*cs->count : 100.0/cs->count);
882     glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.14);
883     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.14);
884   }
885
886   display(mi, cs);
887
888   if(mi->fps_p) do_fps(mi);
889   glFinish(); 
890   glXSwapBuffers(disp, w);
891 }
892
893 /** bust it */
894 ENTRYPOINT void release_chess(ModeInfo *mi) 
895 {
896   if(qs)
897     free((void *) qs);
898   qs = 0;
899
900   FreeAllGL(mi);
901 }
902
903 XSCREENSAVER_MODULE_2 ("Endgame", endgame, chess)
904
905 #endif