📄 chess.c
字号:
/*
* chess.c - part of the chess demo in the glut distribution.
*
* (C) Henk Kok (kok@wins.uva.nl)
*
* This file can be freely copied, changed, redistributed, etc. as long as
* this copyright notice stays intact.
*/
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <math.h>
#include "chess.h"
#if 0
/* Uncomment to debug various scenarios. */
#undef GL_VERSION_1_1
#undef GL_EXT_texture_object
#undef GL_EXT_texture
#endif
#ifndef GL_VERSION_1_1
#if defined(GL_EXT_texture_object) && defined(GL_EXT_texture)
#define glGenTextures glGenTexturesEXT
#define glBindTexture glBindTextureEXT
#else
#define USE_DISPLAY_LISTS
#endif
#endif
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
int texturing = 0;
int reflection = 0;
int chaos = 0;
int chaosPieces = 0;
int animating = 1;
static GLuint texName[3];
extern int path[10][10], piece, piece2;
extern GLfloat CX1, CY1, CX2, CY2, CZ2;
#define WIT 0
#define ZWART 16
int board[10][10];
GLubyte white_square[TXSX][TXSY][3];
GLubyte black_square[TXSX][TXSY][3];
GLubyte wood[TXSX][TXSY][3];
extern GLfloat lightpos[];
GLfloat buf[256], phase;
GLfloat transl[48];
int list[48];
GLfloat width[144], height[144];
GLfloat bwidth, bheight;
int cycle[10][10], cyclem, cycle2;
int stunt[10][10], stuntm, stunt2;
GLfloat blackamb[4] = { 0.2, 0.1, 0.1, 0.5 };
GLfloat blackdif[4] = { 0.2, 0.1, 0.0, 0.5 };
GLfloat blackspec[4] = { 0.5, 0.5, 0.5, 0.5 };
GLfloat whiteamb[4] = { 0.7, 0.7, 0.4, 0.5 };
GLfloat whitedif[4] = { 0.8, 0.7, 0.4, 0.5 };
GLfloat whitespec[4] = { 0.8, 0.7, 0.4, 0.5 };
GLfloat copperamb[4] = { 0.24, 0.2, 0.07, 1.0 };
GLfloat copperdif[4] = { 0.75, 0.61, 0.22, 1.0 };
GLfloat copperspec[4] = { 0.32, 0.25, 0.17, 1.0 };
GLfloat darkamb[4] = { 0.10, 0.10, 0.10, 1.0 };
GLfloat darkdif[4] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat darkspec[4] = { 0.25, 0.25, 0.25, 1.0 };
GLdouble ClipPlane[4] = { 0.0, 1.0, 0.0, 0.0 };
void white_texture(void)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, whitedif);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, whiteamb);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, whitespec);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 40.0);
}
void black_texture(void)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blackdif);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, blackamb);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blackspec);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 40.0);
}
void copper_texture(void)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, copperdif);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, copperamb);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, copperspec);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 40.0);
}
void dark_texture(void)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, darkdif);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, darkamb);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, darkspec);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 40.0);
}
void border_texture(void)
{
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, copperdif);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, copperamb);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, copperspec);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 90.0);
}
void init_textures(void)
{
#if !defined(USE_DISPLAY_LISTS)
glGenTextures(3, texName);
#else
texName[0] = 1000;
texName[1] = 1001;
texName[2] = 1002;
#endif
GenerateTextures();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
#if !defined(USE_DISPLAY_LISTS)
glBindTexture(GL_TEXTURE_2D, texName[0]);
#else
glNewList(texName[0], GL_COMPILE);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, 3, TXSX, TXSY, 0, GL_RGB,
GL_UNSIGNED_BYTE, &wood[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
#if defined(USE_DISPLAY_LISTS)
glEndList();
#endif
#if !defined(USE_DISPLAY_LISTS)
glBindTexture(GL_TEXTURE_2D, texName[1]);
#else
glNewList(texName[1], GL_COMPILE);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, 3, TXSX, TXSY, 0, GL_RGB,
GL_UNSIGNED_BYTE, &white_square[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
#if defined(USE_DISPLAY_LISTS)
glEndList();
#endif
#if !defined(USE_DISPLAY_LISTS)
glBindTexture(GL_TEXTURE_2D, texName[2]);
#else
glNewList(texName[2], GL_COMPILE);
#endif
glTexImage2D(GL_TEXTURE_2D, 0, 3, TXSX, TXSY, 0, GL_RGB,
GL_UNSIGNED_BYTE, &black_square[0][0][0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
#if defined(USE_DISPLAY_LISTS)
glEndList();
#endif
}
void do_border(void)
{
glPushMatrix();
glTranslatef(-0.5, 0.0, -0.5);
if (texturing)
{
#if !defined(USE_DISPLAY_LISTS)
glBindTexture(GL_TEXTURE_2D, texName[0]);
#else
glCallList(texName[0]);
#endif
glEnable(GL_TEXTURE_2D);
} else
border_texture();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.0); glVertex3f(8.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.6); glVertex3f(8.5, 0.08, -0.5);
glTexCoord2f(0.0, 0.6); glVertex3f(-0.5, 0.08, -0.5);
glTexCoord2f(0.0, 0.0); glVertex3f(8.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.0); glVertex3f(8.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.6); glVertex3f(8.5, 0.08, 8.5);
glTexCoord2f(0.0, 0.6); glVertex3f(8.5, 0.08, -0.5);
glTexCoord2f(0.0, 0.0); glVertex3f(8.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.0); glVertex3f(0.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.6); glVertex3f(-0.5, 0.08, 8.5);
glTexCoord2f(0.0, 0.6); glVertex3f(8.5, 0.08, 8.5);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.0); glVertex3f(0.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.6); glVertex3f(-0.5, 0.08, -0.5);
glTexCoord2f(0.0, 0.6); glVertex3f(-0.5, 0.08, 8.5);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.0); glVertex3f(8.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.6); glVertex3f(8.0, -0.08, 0.0);
glTexCoord2f(0.0, 0.6); glVertex3f(0.0, -0.08, 0.0);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(8.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.0); glVertex3f(8.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.6); glVertex3f(8.0, -0.08, 8.0);
glTexCoord2f(0.0, 0.6); glVertex3f(8.0, -0.08, 0.0);
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(8.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.0); glVertex3f(0.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.6); glVertex3f(0.0, -0.08, 8.0);
glTexCoord2f(0.0, 0.6); glVertex3f(8.0, -0.08, 8.0);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 0.08, 8.0);
glTexCoord2f(0.6, 0.0); glVertex3f(0.0, 0.08, 0.0);
glTexCoord2f(0.6, 0.6); glVertex3f(0.0, -0.08, 0.0);
glTexCoord2f(0.0, 0.6); glVertex3f(0.0, -0.08, 8.0);
glEnd();
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, 0.08, -0.5);
glTexCoord2f(0.6, 0.0); glVertex3f(8.5, 0.08, -0.5);
glTexCoord2f(0.6, 0.6); glVertex3f(8.5, -0.08, -0.5);
glTexCoord2f(0.0, 0.6); glVertex3f(-0.5, -0.08, -0.5);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(8.5, 0.08, -0.5);
glTexCoord2f(0.6, 0.0); glVertex3f(8.5, 0.08, 8.5);
glTexCoord2f(0.6, 0.6); glVertex3f(8.5, -0.08, 8.5);
glTexCoord2f(0.0, 0.6); glVertex3f(8.5, -0.08, -0.5);
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(8.5, 0.08, 8.5);
glTexCoord2f(0.6, 0.0); glVertex3f(-0.5, 0.08, 8.5);
glTexCoord2f(0.6, 0.6); glVertex3f(-0.5, -0.08, 8.5);
glTexCoord2f(0.0, 0.6); glVertex3f(8.5, -0.08, 8.5);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, 0.08, 8.5);
glTexCoord2f(0.6, 0.0); glVertex3f(-0.5, 0.08, -0.5);
glTexCoord2f(0.6, 0.6); glVertex3f(-0.5, -0.08, -0.5);
glTexCoord2f(0.0, 0.6); glVertex3f(-0.5, -0.08, 8.5);
glEnd();
if (texturing)
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
void do_vlakje(void)
{
glColor4f(1.0, 1.0, 1.0, 1.0);
glDisable(GL_LIGHTING);
glPushMatrix();
glTranslatef(-0.5, 0.0, -0.5);
glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(8.0, 0.0, 0.0);
glVertex3f(8.0, 0.0, 8.0);
glVertex3f(0.0, 0.0, 8.0);
glEnd();
glPopMatrix();
glEnable(GL_LIGHTING);
}
void do_board(void)
{
int x,y;
glPushMatrix();
glTranslatef(-0.5, 0.0, -0.5);
white_texture();
if (texturing)
{
#if !defined(USE_DISPLAY_LISTS)
glBindTexture(GL_TEXTURE_2D, texName[1]);
#else
glCallList(texName[1]);
#endif
glEnable(GL_TEXTURE_2D);
}
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
for (x=0;x<8;x++)
{
for (y=x%2;y<8;y+=2)
{
glTexCoord2f(0.2*x, 0.2*y); glVertex3f(x, 0, y);
glTexCoord2f(0.17+0.2*x, 0.2*y); glVertex3f(x+1, 0, y);
glTexCoord2f(0.17+0.2*x, 0.17+0.2*y); glVertex3f(x+1, 0, y+1);
glTexCoord2f(0.2*x, 0.17+0.2*y); glVertex3f(x, 0, y+1);
}
}
glEnd();
if (texturing)
{
glDisable(GL_TEXTURE_2D);
#if !defined(USE_DISPLAY_LISTS)
glBindTexture(GL_TEXTURE_2D, texName[2]);
#else
glCallList(texName[2]);
#endif
glEnable(GL_TEXTURE_2D);
} else
black_texture();
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
for (x=0;x<8;x++)
{
for (y=1-(x%2);y<8;y+=2)
{
glTexCoord2f(0.2*x, 0.2*y); glVertex3f(x, 0, y);
glTexCoord2f(0.17+0.2*x, 0.2*y); glVertex3f(x+1, 0, y);
glTexCoord2f(0.17+0.2*x, 0.17+0.2*y); glVertex3f(x+1, 0, y+1);
glTexCoord2f(0.2*x, 0.17+0.2*y); glVertex3f(x, 0, y+1);
}
}
glEnd();
if (texturing)
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
void do_solid(GLfloat *f, int sz, GLfloat width)
{
GLfloat nx, ny, s;
GLfloat length;
int i,j;
for (i=0;i<sz;i++)
buf[i] = f[i]/4.2;
for (i=0;i<sz;i+=2)
{
buf[i+1] = buf[i+1] * (1 - cos(phase)/3);
buf[i] = buf[i] * (1+sin(M_PI*buf[i+1]/buf[1])*cos(phase)*0.7);
/*
if (buf[i] > bwidth)
bwidth = buf[i];
*/
if (buf[i+1] > bheight)
bheight = buf[i+1];
}
glBegin(GL_QUAD_STRIP);
for (i=2;i<sz;i+=2)
{
if (buf[i+3] == buf[i+1] && buf[i+2] == buf[i])
continue;
nx = buf[i-1] + buf[i+3];
ny = buf[i+2] - buf[i];
length = sqrt(nx*nx+ny*ny);
nx = nx/length;
ny = ny/length;
s = (1+cos(phase)*0.7);
glNormal3f(0.0, ny, nx);
glVertex3f(width*s, buf[i+1], buf[i]);
glVertex3f(-width*s, buf[i+1], buf[i]);
}
glEnd();
glNormal3f(-1.0, 0.0, 0.0);
i = 2; j = sz-4;
glBegin(GL_TRIANGLE_STRIP);
while (i < j)
{
while (buf[i-1] == buf[i+1] && buf[i-2] == buf[i] && i < j)
i += 2;
if (i < j)
{
s = (1+cos(phase)*0.7);
glVertex3f(-width*s, buf[i+1], buf[i]);
}
i += 2;
while (buf[j-1] == buf[j+1] && buf[j-2] == buf[j] && i < j)
j -= 2;
if (i < j)
{
s = (1+cos(phase)*0.7);
glVertex3f(-width*s, buf[j+1], buf[j]);
}
j -= 2;
}
glEnd();
i = 2; j = sz-4;
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(1.0, 0.0, 0.0);
while (i < j)
{
while (buf[i-1] == buf[i+1] && buf[i-2] == buf[i] && i < j)
i += 2;
if (i < j)
{
s = (1+cos(phase)*0.7);
glVertex3f(width*s, buf[i+1], buf[i]);
}
i += 2;
while (buf[j-1] == buf[j+1] && buf[j-2] == buf[j] && i < j)
j -= 2;
if (i < j)
{
s = (1+cos(phase)*0.7);
glVertex3f(width*s, buf[j+1], buf[j]);
}
j -= 2;
}
glEnd();
}
void do_rotate(GLfloat *f, int sz)
{
GLfloat nx, ny;
GLfloat length;
GLfloat a, c, s;
int i,j;
bheight = 0;
bwidth = 0;
for (i=0;i<sz;i++)
buf[i] = f[i] / 4.2;
for (i=0;i<sz;i+=2)
{
buf[i+1] = buf[i+1] * (1 - cos(phase)/3);
buf[i] = buf[i] * (1+sin(M_PI*buf[i+1]/buf[1])*cos(phase)*0.7);
if (buf[i] > bwidth)
bwidth = buf[i];
if (buf[i+1] > bheight)
bheight = buf[i+1];
}
for (i=2;i<sz-4;i+=2)
{
if (fabs(buf[i+3]+buf[i+1]) +fabs(buf[i+2]+buf[i]) < 0.0001)
continue;
glBegin(GL_QUAD_STRIP);
for (j=0;j<=ACC;j++)
{
a = ((float) j)*M_PI*2/ACC;
c = cos(a);
s = sin(a);
nx = buf[i-1] + buf[i+3];
ny = buf[i+2] - buf[i];
length = sqrt(nx*nx+ny*ny);
nx = nx/length;
ny = ny/length;
glNormal3f(c*nx, ny, s*nx);
glVertex3f(c*buf[i], buf[i+1], s*buf[i]);
nx = buf[i-1] + buf[i+3];
ny = buf[i+2] - buf[i];
length = sqrt(nx*nx+ny*ny);
nx = nx/length;
ny = ny/length;
glNormal3f(c*nx, ny, s*nx);
glVertex3f(c*buf[i+2], buf[i+3], s*buf[i+2]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -