-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcGame.cpp
executable file
·142 lines (126 loc) · 3.63 KB
/
cGame.cpp
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
#include "cGame.h"
cGame::cGame(void)
{
drawGlut = false;
}
cGame::~cGame(void){
}
bool cGame::Init()
{
fd = fopen("debug.txt","w+");
Point eye = Point(0, SCENE_HEIGHT+1.0, 0);
Point center = Point(1.0, SCENE_HEIGHT+1.0, 1.0);
Point up = Point(0.0f, 1.0f, 0.0f);
bool basaur;
/***********************************************************************************/
//Graphics initialization
//CAMERA
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
cam = Camera(eye, center, up);
glMatrixMode(GL_MODELVIEW);
glAlphaFunc(GL_GREATER, 0.05f);
glEnable(GL_ALPHA_TEST);
//MATERIAL
glEnable(GL_COLOR_MATERIAL);
GLfloat matAmbient[] = {0.4, 0.4, 0.4, 1.0};
GLfloat matDiffuse[] = {1.0, 1.0, 0.0, 1.0};
GLfloat matSpecular[] = {0.0, 0.0, 1.0, 1.0};
GLfloat matEmission[] = {0.0, 0.0, 0.0, 1.0};
GLfloat matShininnes = 20;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matSpecular);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, matEmission);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &matShininnes);
//LIGHTNING
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat lightPos[] = { SCENE_WIDTH/2, SCENE_HEIGHT*2, SCENE_DEPTH/2};
GLfloat specLight[] = { 1.0, 1.0, 1.0, 1.0};
GLfloat ambiLight[] = { 0, 0, 0, 1.0};
GLfloat diffLight[] = { 1.0, 1.0, 1.0, 1.0};
GLfloat ambientModel[] = {.5, .5, .5, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambiLight);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientModel);
//glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0);
/**********************************************************************************/
int start = glutGet(GLUT_ELAPSED_TIME);
basaur = Scene.Init(fd);
int end = glutGet(GLUT_ELAPSED_TIME);
fprintf(fd,"%d",end-start);
return basaur;
}
bool cGame::Loop() {
bool res = Process();
if(res) Render();
return res;
}
void cGame::Finalize() {
fclose(fd);
exit(0);
}
//Input
void cGame::ReadKeyboard(unsigned char key, bool press) {
keys[key] = press;
}
void cGame::ReadSpecialKeyboard(unsigned char specialkey, bool press) {
specialKeys[specialkey] = press;
}
void cGame::ReadMouse(int button, int state, int x, int y) {
}
void cGame::MouseMove(int x, int y,bool pressed){
}
//Process
bool cGame::Process() {
if(keys[27])return false;
if(keys['W'] || keys['w'] || specialKeys[GLUT_KEY_UP]) {
Point camEye = cam.getCameraEye();
Point camCenter = cam.getCameraCenter();
camEye.z=(camEye.z+1);
camEye.x=(camEye.x+1);
camCenter.z = camCenter.z+1;
camCenter.x = camCenter.x+1;
cam.setCamera(camEye, camCenter);
return true;
}
if(keys['A'] || keys['a'] || specialKeys[GLUT_KEY_LEFT]) {
/*Point camEye = cam.getCameraEye();
camEye.x=(camEye.x-1);
cam.setCamera(camEye, cam.getCameraCenter());*/
return true;
}
if(keys['S'] || keys['s'] || specialKeys[GLUT_KEY_DOWN]) {
Point camEye = cam.getCameraEye();
Point camCenter = cam.getCameraCenter();
camEye.z=(camEye.z-1);
camEye.x=(camEye.x-1);
camCenter.z = camCenter.z-1;
camCenter.x = camCenter.x-1;
cam.setCamera(camEye, camCenter);
return true;
}
if(keys['D'] || keys['d'] || specialKeys[GLUT_KEY_RIGHT]) {
/*Point camEye = cam.getCameraEye();
camEye.x=(camEye.x+1);
cam.setCamera(camEye, cam.getCameraCenter());*/
return true;
}
return true;
}
//Output
void cGame::Render()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glColor3f(1,1,1);
Scene.Draw();
glPopMatrix();
cam.updateCamera();
glutSwapBuffers();
}