1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/*
* main.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 <GL/glut.h>
#include <math.h>
#include "chess.h"
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define M_TEXTURING 1
#define M_REFLECTION 2
#define M_CHAOS 3
#define M_ANIMATION 4
extern int reflection, texturing, animating, chaos;
GLfloat lightpos[4] = { 2.0, 1.0, 1.0, 0.0 };
GLfloat lightamb[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lightdif[4] = { 1.0, 1.0, 1.0, 1.0 };
float angle = 0.0, a2 = 45.0;
int speed = 0;
GLfloat px = -3.5, py = -16.5, pz = 9.5;
void SetCamera(void)
{
gluLookAt(0.0,2.0,2.0, 0.0,2.0,0.0, 0.0,1.0,0.0);
glRotatef(a2, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glTranslatef(px, -pz, py);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
}
void display(void)
{
glLoadIdentity();
SetCamera();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
do_display();
glutSwapBuffers();
}
void myinit(void)
{
glShadeModel (GL_SMOOTH);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
glClearColor(0.0, 0.0, 0.0, 1.0);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightamb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightdif);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
init_lists();
}
void Animate(void)
{
px -= speed * 0.02 * sin(angle*M_PI/180);
py += speed * 0.02 * cos(angle*M_PI/180);
if (animating)
proceed();
glutPostRedisplay();
}
extern int chaosPieces;
/* ARGSUSED1 */
void parsekey(unsigned char key, int x, int y)
{
switch (key)
{
case 27: exit(0);
case 13: speed = 0; break;
case 'a': a2 += 5; break;
case 'z': a2 -= 5; break;
case 'A': pz += 0.5; break;
case 'Z': pz -= 0.5; break;
default:
return;
}
glutPostRedisplay();
if (animating || (chaosPieces > 0) || (speed != 0))
glutIdleFunc(Animate);
else
glutIdleFunc(NULL);
}
/* ARGSUSED1 */
void parsekey_special(int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_UP: speed += 1; break;
case GLUT_KEY_DOWN: speed -= 1; break;
case GLUT_KEY_RIGHT: angle += 5; break;
case GLUT_KEY_LEFT: angle -= 5; break;
case GLUT_KEY_HOME:
angle = 0.0, a2 = 45.0;
speed = 0;
px = -3.5, py = -16.5, pz = 9.5;
break;
default:
return;
}
glutPostRedisplay();
if (animating || (chaosPieces > 0) || (speed != 0))
glutIdleFunc(Animate);
else
glutIdleFunc(NULL);
}
void handle_main_menu(int item)
{
switch(item) {
case M_REFLECTION:
reflection = !reflection;
glutPostRedisplay();
break;
case M_TEXTURING:
texturing = !texturing;
glutPostRedisplay();
break;
case M_ANIMATION:
animating = !animating;
if (animating || (chaosPieces > 0) || (speed != 0))
glutIdleFunc(Animate);
else
glutIdleFunc(NULL);
break;
case M_CHAOS:
chaos = !chaos;
if (animating || chaos || (speed != 0))
glutIdleFunc(Animate);
break;
}
}
void
Visible(int visible)
{
if (visible == GLUT_VISIBLE) {
if (animating || (chaosPieces > 0) || (speed != 0))
glutIdleFunc(Animate);
} else {
glutIdleFunc(NULL);
}
}
void myReshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum (-0.1, 0.1, -0.1, 0.1, 0.3, 200.0);
glMatrixMode (GL_MODELVIEW);
glViewport(0, 0, w, h);
glLoadIdentity();
SetCamera();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE | GLUT_STENCIL);
glutCreateWindow("Chess");
glutDisplayFunc(display);
glutInitWindowPosition(200, 0);
glutInitWindowSize(300, 300);
glutKeyboardFunc(parsekey);
glutSpecialFunc(parsekey_special);
glutReshapeFunc(myReshape);
glutVisibilityFunc(Visible);
myinit();
glutCreateMenu(handle_main_menu);
glutAddMenuEntry("Toggle texturing", M_TEXTURING);
glutAddMenuEntry("Toggle reflection", M_REFLECTION);
glutAddMenuEntry("-----------------", -1);
glutAddMenuEntry("Toggle animation", M_ANIMATION);
glutAddMenuEntry("Toggle CHAOS!", M_CHAOS);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutSwapBuffers();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
|