summaryrefslogtreecommitdiffstats
path: root/src/Game.cpp
blob: d13a2624a8680cd6769d4bebc3d25cfdd9b61077 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Game.cpp
 *
 * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Game.h"
#include "Collision.h"
#include "Level.h"
#include "Shader.h"
#include "ShadowVolume.h"
#include "Triangle.h"

#include "gl.h"

#include <algorithm>

namespace Zoom {

const float Game::PLAYER_SPEED = 3;
const float Game::PLAYER_RADIUS = 0.3;

Game::Game() : playerPos(vmml::vec3f::ZERO), playerRotY(vmml::mat4f::IDENTITY), playerRotX(0),
    input(0), lightPos(0) {
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_STENCIL_TEST);

  glEnable(GL_BLEND);

  glEnable(GL_MULTISAMPLE_ARB);

  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, vmml::vec4f(0.05, 0.05, 0.05, 1).array);

  glLightfv(GL_LIGHT0, GL_AMBIENT, vmml::vec4f::ZERO.array);
  glLightfv(GL_LIGHT0, GL_DIFFUSE, vmml::vec4f::ONE.array);
  glLightfv(GL_LIGHT0, GL_SPECULAR, vmml::vec4f::ONE.array);
  glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.2);

  glEnable(GL_COLOR_MATERIAL);
  glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vmml::vec4f::ONE.array);
  glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 128);

  glEnable(GL_CULL_FACE);
  glFrontFace(GL_CCW);

  glEnable(GL_STENCIL_TEST);

  loadLevel("level.xml");
  triangles.insert(triangles.end(), level->getRooms().front().walls.begin(), level->getRooms().front().walls.end());
  std::sort(triangles.begin(), triangles.end(), Renderer::TextureSorter());
}

bool Game::loadLevel(const std::string &name) {
  level = Level::loadLevel(name);

  return level;
}

void Game::resize(int width, int height) {
  if(height == 0) {
    height = 1;
  }


  glMatrixMode(GL_PROJECTION);
  glLoadMatrixf(MathUtil::perspective(50.0f, (float)width/(float)height, 0.1).array);

  glMatrixMode(GL_MODELVIEW);

  glViewport(0, 0, width, height);
}

void Game::turn(float x, float y) {
  playerRotY.rotate_y(-x*M_PI/180/10);

  playerRotX -= y*M_PI/180/10;
  if(playerRotX > M_PI_2)
    playerRotX = M_PI_2;
  else if(playerRotX < -M_PI_2)
    playerRotX = -M_PI_2;
}

bool Game::doesCollide(const vmml::vec3f &playerMove) const {
  for(std::vector<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
    if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS-MathUtil::EPSILON, playerMove))
      return true;
  }

  return false;
}

void Game::run(int delta) {
  lightPos += delta*0.5;
  lightPos = std::fmod(lightPos, 26000);

  vmml::vec3f playerMove(vmml::vec3f::ZERO);

  if(input & FORWARD) {
    playerMove -= playerRotY*vmml::vec3f::UNIT_Z;
  }
  if(input & BACKWARD) {
    playerMove += playerRotY*vmml::vec3f::UNIT_Z;
  }
  if(input & LEFT) {
    playerMove -= playerRotY*vmml::vec3f::UNIT_X;
  }
  if(input & RIGHT) {
    playerMove += playerRotY*vmml::vec3f::UNIT_X;
  }

  if(playerMove == vmml::vec3f::ZERO)
    return;

  playerMove.normalize();
  playerMove *= PLAYER_SPEED*delta/1000;

  vmml::vec3f origMove = playerMove;

  bool collision = true;

  while(collision) {
    collision = false;

    float nearestDistance;
    vmml::vec3f nearestNormal;

    for(std::vector<TriangleRecord>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
      float distance;
      vmml::vec3f normal;

      if(Collision::test(t->getTriangle(), playerPos, PLAYER_RADIUS, playerMove, &distance, &normal)) {
        normal.y() = 0;

        if(normal.dot(playerMove) >= 0)
          continue;

        if(!collision || distance < nearestDistance) {
          collision = true;

          nearestDistance = distance;
          nearestNormal = normal;
        }
      }
    }

    if(collision) {
      vmml::vec3f move = playerMove*nearestDistance;

      if(move.dot(origMove) <= 0 && move.squared_length() > 0) {
        return;
      }

      if(doesCollide(move))
        return;

      playerPos += move;

      vmml::vec3f restMove = playerMove - move;

      playerMove = restMove - nearestNormal * (nearestNormal.dot(restMove));
    }
  }

  if(!doesCollide(playerMove))
    playerPos += playerMove;
}

void Game::render() {
  int i;
  vmml::vec3f light(vmml::vec3f::ZERO);

  if(lightPos < 13000) i = lightPos;
  else i = 26000 - lightPos;

  if(i < 500) {
    light.x() = 0.0;
    light.z() = 0.0;
  }
  else if(i < 4500) {
    light.x() = 0.0;
    light.z() = -(i-500) * 0.001;
  }
  else if(i < 8500) {
    light.x() = (i-4500) * 0.001;
    light.z() = -4.0;
  }
  else if(i < 12500) {
    light.x() = 4.0;
    light.z() = -4.0 - (i-8500) * 0.001;
  }
  else {
    light.x() = 4.0;
    light.z() = -8.0;
  }

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  vmml::mat4f transform(playerRotY), inverse;
  transform.rotate_x(playerRotX);
  transform.set_translation(playerPos);
  transform.inverse(inverse);

  glLoadMatrixf(inverse.array);

  renderer.render(triangles, light);

  Shader::disable();
  glDepthFunc(GL_LEQUAL);
  glStencilFunc(GL_ALWAYS, 0, std::numeric_limits<GLuint>::max());
  glBlendFunc(GL_ONE, GL_ZERO);

  glDisable(GL_TEXTURE_2D);
  glColor3f(1, 1, 1);

  vmml::vec3f lightVec = playerPos-light;

  vmml::vec3f axis1 = lightVec.cross(vmml::vec3f(0, 1, 0)), axis2 = lightVec.cross(axis1);
  axis1.normalize();
  axis2.normalize();

  glBegin(GL_POLYGON);
  for(int i = 0; i < 32; ++i) {
    vmml::vec3f pos = light + axis1*std::cos(M_PI*i/16.0)*0.05 + axis2*std::sin(M_PI*i/16.0)*0.05;

    glVertex3fv(pos.array);
  }
  glEnd();

  glEnable(GL_TEXTURE_2D);
}

}