+ drawPieces(mi, cs);
+ if(cs->moving) drawMovingPiece(mi, cs, 0);
+ if(cs->take) drawTakePiece(mi, cs, 0);
+ glDisable(GL_TEXTURE_2D);
+}
+
+static void draw_shadow_pieces(ModeInfo *mi, Chesscreen *cs, int wire)
+{
+ if (!cs->wire) {
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D, cs->piecetexture);
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+ }
+
+ /* use the stencil */
+ glDisable(GL_LIGHTING);
+ glDisable(GL_COLOR_MATERIAL);
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_TEXTURE_2D);
+ glDisable(GL_BLEND);
+
+ glClear(GL_STENCIL_BUFFER_BIT);
+ glColorMask(0,0,0,0);
+ glEnable(GL_STENCIL_TEST);
+
+ glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFFL);
+ glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
+
+
+ glPushMatrix();
+ glTranslatef(0.0, 0.001, 0.0);
+
+ /* draw the pieces */
+ drawPiecesShadow(mi, cs);
+ if(cs->moving) drawMovingPiece(mi, cs, shadows);
+ if(cs->take) drawTakePiece(mi, cs, shadows);
+
+ glPopMatrix();
+
+
+ /* turn on drawing into colour buffer */
+ glColorMask(1,1,1,1);
+
+ /* programming with effect */
+ glDisable(GL_LIGHTING);
+ glDisable(GL_COLOR_MATERIAL);
+ glDisable(GL_TEXTURE_2D);
+
+ /* now draw the union of the shadows */
+
+ /*
+ <todo>
+ want to keep alpha values (alpha is involved in transition
+ effects of the active pieces).
+ </todo>
+ */
+ glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFFL);
+ glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
+
+ glEnable(GL_BLEND);
+
+ glColor4fv(MaterialShadow);
+
+ /* draw the board generously to fill the shadows */
+ glBegin(GL_QUADS);
+
+ glVertex3f(-1.0, 0.0, -1.0);
+ glVertex3f(-1.0, 0.0, BOARDSIZE + 1.0);
+ glVertex3f(1.0 + BOARDSIZE, 0.0, BOARDSIZE + 1.0);
+ glVertex3f(1.0 + BOARDSIZE, 0.0, -1.0);
+
+ glEnd();
+
+ glDisable(GL_STENCIL_TEST);
+
+ /* "pop" attributes */
+ glEnable(GL_TEXTURE_2D);
+ glEnable(GL_COLOR_MATERIAL);
+ glEnable(GL_LIGHTING);
+ glEnable(GL_CULL_FACE);
+
+
+
+}
+
+enum {X, Y, Z, W};
+enum {A, B, C, D};
+
+/* create a matrix that will project the desired shadow */
+static void shadowmatrix(GLfloat shadowMat[4][4],
+ GLfloat groundplane[4],
+ GLfloat lightpos[4])
+{
+ GLfloat dot;
+
+ /* find dot product between light position vector and ground plane normal */
+ dot = groundplane[X] * lightpos[X] +
+ groundplane[Y] * lightpos[Y] +
+ groundplane[Z] * lightpos[Z] +
+ groundplane[W] * lightpos[W];
+
+ shadowMat[0][0] = dot - lightpos[X] * groundplane[X];
+ shadowMat[1][0] = 0.f - lightpos[X] * groundplane[Y];
+ shadowMat[2][0] = 0.f - lightpos[X] * groundplane[Z];
+ shadowMat[3][0] = 0.f - lightpos[X] * groundplane[W];
+
+ shadowMat[X][1] = 0.f - lightpos[Y] * groundplane[X];
+ shadowMat[1][1] = dot - lightpos[Y] * groundplane[Y];
+ shadowMat[2][1] = 0.f - lightpos[Y] * groundplane[Z];
+ shadowMat[3][1] = 0.f - lightpos[Y] * groundplane[W];
+
+ shadowMat[X][2] = 0.f - lightpos[Z] * groundplane[X];
+ shadowMat[1][2] = 0.f - lightpos[Z] * groundplane[Y];
+ shadowMat[2][2] = dot - lightpos[Z] * groundplane[Z];
+ shadowMat[3][2] = 0.f - lightpos[Z] * groundplane[W];
+
+ shadowMat[X][3] = 0.f - lightpos[W] * groundplane[X];
+ shadowMat[1][3] = 0.f - lightpos[W] * groundplane[Y];
+ shadowMat[2][3] = 0.f - lightpos[W] * groundplane[Z];
+ shadowMat[3][3] = dot - lightpos[W] * groundplane[W];