2021-09-24 10:15:43 -04:00
|
|
|
|
#include "engine.h"
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
2021-09-27 11:30:18 -04:00
|
|
|
|
#include "transformation.h"
|
2021-09-27 13:15:57 -04:00
|
|
|
|
#include "player.h"
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
2021-09-27 11:30:18 -04:00
|
|
|
|
Engine::Engine() { }
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
2021-09-27 11:30:18 -04:00
|
|
|
|
Engine::~Engine() { }
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
|
|
|
|
void Engine::Init()
|
|
|
|
|
{
|
2021-10-11 11:37:58 -04:00
|
|
|
|
GLenum glewErr = glewInit();
|
2021-10-12 15:58:54 -04:00
|
|
|
|
if (glewErr != GLEW_OK) {
|
2021-10-11 11:37:58 -04:00
|
|
|
|
std::cerr << " ERREUR GLEW : " << glewGetErrorString(glewErr) << std::endl;
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
|
glLoadIdentity();
|
|
|
|
|
gluPerspective(45.0f, (float)Width() / (float)Height(), 0.0001f, 1000.0f);
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
|
glEnable(GL_LINE_SMOOTH);
|
2021-10-06 21:32:12 -04:00
|
|
|
|
glEnable(GL_CULL_FACE);
|
2021-10-12 15:58:54 -04:00
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2021-10-06 21:32:12 -04:00
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
// Light
|
|
|
|
|
GLfloat light0Pos[4] = { 0.0f, CHUNK_SIZE_Y, 0.0f, 1.0f };
|
2021-10-25 10:50:08 -04:00
|
|
|
|
GLfloat light0Amb[4] = { 0.2f, 0.2f, 0.2f, 1.f };
|
2021-09-27 14:45:59 -04:00
|
|
|
|
GLfloat light0Diff[4] = { 1.f, 1.f, 1.f, 1.f };
|
2021-09-27 10:20:58 -04:00
|
|
|
|
GLfloat light0Spec[4] = { 0.2f, 0.2f, 0.2f, 1.0f };
|
|
|
|
|
|
|
|
|
|
glEnable(GL_LIGHT0);
|
|
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
|
|
|
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, light0Amb);
|
|
|
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0Diff);
|
|
|
|
|
glLightfv(GL_LIGHT0, GL_SPECULAR, light0Spec);
|
|
|
|
|
|
2021-10-12 15:58:54 -04:00
|
|
|
|
// Objet de skybox avec sa propre texture et son propre shader!
|
|
|
|
|
m_skybox.Init(0.00013f);
|
2021-10-04 12:29:10 -04:00
|
|
|
|
|
2021-10-19 10:27:59 -04:00
|
|
|
|
// Objet de musique!
|
2021-10-25 10:50:08 -04:00
|
|
|
|
m_audio.ToggleMusicState();
|
2021-10-19 10:27:59 -04:00
|
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
|
// Init testChunk
|
|
|
|
|
for (int x = 0; x < CHUNK_SIZE_X; ++x) {
|
|
|
|
|
for (int z = 0; z < CHUNK_SIZE_Z; ++z) {
|
|
|
|
|
for (int y = 0; y < 32; ++y) {
|
|
|
|
|
if (x % 2 == 0 && y % 2 == 0 && z % 2 == 0)
|
|
|
|
|
m_testChunk.SetBlock(x, y, z, BTYPE_DIRT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
CenterMouse();
|
|
|
|
|
HideCursor();
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 11:30:18 -04:00
|
|
|
|
void Engine::DeInit() { }
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
2021-09-27 11:30:18 -04:00
|
|
|
|
void Engine::LoadResource() {
|
2021-10-02 15:00:48 -04:00
|
|
|
|
LoadTexture(m_textureFloor, TEXTURE_PATH "grass.png");
|
2021-10-12 15:58:54 -04:00
|
|
|
|
LoadTexture(m_skybox.GetTexture(), TEXTURE_PATH "skybox.png");
|
2021-10-04 12:29:10 -04:00
|
|
|
|
LoadTexture(m_textureCube1, TEXTURE_PATH "metal1.png");
|
|
|
|
|
LoadTexture(m_textureCube2, TEXTURE_PATH "metal2.png");
|
|
|
|
|
LoadTexture(m_textureCube3, TEXTURE_PATH "metal3.png");
|
|
|
|
|
LoadTexture(m_textureCube4, TEXTURE_PATH "metal4.png");
|
2021-10-11 11:37:58 -04:00
|
|
|
|
|
|
|
|
|
std::cout << " Loading and compiling shaders ..." << std::endl;
|
2021-10-12 15:58:54 -04:00
|
|
|
|
if (!m_shader01.Load(SHADER_PATH "shader01.vert", SHADER_PATH "shader01.frag", true)) {
|
2021-10-11 11:37:58 -04:00
|
|
|
|
std::cout << " Failed to load shader " << std::endl;
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
2021-10-12 15:58:54 -04:00
|
|
|
|
if (!m_skybox.GetShader().Load(SHADER_PATH "skybox.vert", SHADER_PATH "skybox.frag", true)) {
|
|
|
|
|
std::cout << " Failed to load shader " << std::endl;
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 15:58:54 -04:00
|
|
|
|
void Engine::UnloadResource(){}
|
|
|
|
|
|
2021-09-24 10:15:43 -04:00
|
|
|
|
void Engine::Render(float elapsedTime)
|
|
|
|
|
{
|
2021-09-27 10:20:58 -04:00
|
|
|
|
static float gameTime = elapsedTime;
|
|
|
|
|
|
|
|
|
|
gameTime += elapsedTime;
|
|
|
|
|
|
2021-09-27 13:15:57 -04:00
|
|
|
|
Transformation all;
|
2021-09-27 13:56:29 -04:00
|
|
|
|
Transformation skybox;
|
2021-09-27 11:30:18 -04:00
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
|
|
// Transformations initiales
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
2021-10-04 12:29:10 -04:00
|
|
|
|
m_player.Move(m_keyW, m_keyS, m_keyA, m_keyD, m_keySpace, m_keylshift, elapsedTime);
|
2021-09-27 13:15:57 -04:00
|
|
|
|
m_player.ApplyTransformation(all);
|
2021-10-06 21:33:56 -04:00
|
|
|
|
m_player.ApplyTransformation(skybox, false); // Version d'ApplyTransformation qui ne tient compte que de la rotation
|
|
|
|
|
// (donc l'objet ne bouge pas relativement au joueur, ce qui est pratique pour une skybox!).
|
2021-10-25 10:50:08 -04:00
|
|
|
|
glDisable(GL_LIGHT0);
|
2021-10-12 15:58:54 -04:00
|
|
|
|
m_skybox.Render(skybox);
|
2021-10-25 10:50:08 -04:00
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
// Plancher
|
|
|
|
|
// Les vertex doivent etre affiches dans le sens anti-horaire (CCW)
|
2021-10-04 12:29:10 -04:00
|
|
|
|
all.Use();
|
2021-10-25 10:50:08 -04:00
|
|
|
|
glEnable(GL_LIGHT0);
|
2021-10-12 15:58:54 -04:00
|
|
|
|
m_shader01.Use();
|
2021-09-27 10:20:58 -04:00
|
|
|
|
m_textureFloor.Bind();
|
2021-10-19 10:27:59 -04:00
|
|
|
|
float nbRep = 10.f;
|
2021-09-27 10:20:58 -04:00
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
glNormal3f(0, 1, 0); // Normal vector
|
|
|
|
|
glTexCoord2f(0, 0);
|
|
|
|
|
glVertex3f(-100.f, -2.f, 100.f);
|
|
|
|
|
glTexCoord2f(nbRep, 0);
|
|
|
|
|
glVertex3f(100.f, -2.f, 100.f);
|
|
|
|
|
glTexCoord2f(nbRep, nbRep);
|
|
|
|
|
glVertex3f(100.f, -2.f, -100.f);
|
|
|
|
|
glTexCoord2f(0, nbRep);
|
|
|
|
|
glVertex3f(-100.f, -2.f, -100.f);
|
|
|
|
|
glEnd();
|
2021-10-04 12:29:10 -04:00
|
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
|
// Chunk
|
|
|
|
|
switch ((int)(gameTime * 5) % 4) {
|
|
|
|
|
case 0: m_textureCube1.Bind();
|
2021-10-04 12:29:10 -04:00
|
|
|
|
break;
|
2021-10-11 11:37:58 -04:00
|
|
|
|
case 1: m_textureCube2.Bind();
|
2021-10-04 12:29:10 -04:00
|
|
|
|
break;
|
2021-10-11 11:37:58 -04:00
|
|
|
|
case 2: m_textureCube3.Bind();
|
2021-10-04 12:29:10 -04:00
|
|
|
|
break;
|
2021-10-11 11:37:58 -04:00
|
|
|
|
case 3: m_textureCube4.Bind();
|
2021-10-04 12:29:10 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-10-11 11:37:58 -04:00
|
|
|
|
if (m_testChunk.IsDirty())
|
|
|
|
|
m_testChunk.Update();
|
2021-10-12 15:58:54 -04:00
|
|
|
|
|
2021-10-11 11:37:58 -04:00
|
|
|
|
m_testChunk.Render();
|
2021-10-25 10:50:08 -04:00
|
|
|
|
m_shader01.Disable();
|
2021-09-27 10:20:58 -04:00
|
|
|
|
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::KeyPressEvent(unsigned char key)
|
|
|
|
|
{
|
2021-10-11 11:37:58 -04:00
|
|
|
|
switch (key) {
|
2021-09-27 10:20:58 -04:00
|
|
|
|
case 36: // ESC
|
|
|
|
|
Stop();
|
|
|
|
|
break;
|
|
|
|
|
case 94: // F10
|
|
|
|
|
SetFullscreen(!IsFullscreen());
|
|
|
|
|
break;
|
|
|
|
|
case 22: // W
|
|
|
|
|
if (!m_keyW) {
|
|
|
|
|
std::cout << "W " << std::endl;
|
|
|
|
|
m_keyW = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0: // A
|
|
|
|
|
if (!m_keyA) {
|
|
|
|
|
std::cout << "A " << std::endl;
|
|
|
|
|
m_keyA = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 18: // S
|
|
|
|
|
if (!m_keyS) {
|
|
|
|
|
std::cout << "S " << std::endl;
|
|
|
|
|
m_keyS = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3: // D
|
|
|
|
|
if (!m_keyD) {
|
|
|
|
|
std::cout << "D " << std::endl;
|
|
|
|
|
m_keyD = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-10-04 12:29:10 -04:00
|
|
|
|
case 38: // Left Shift
|
|
|
|
|
if (!m_keylshift) {
|
|
|
|
|
std::cout << "Dash!" << std::endl;
|
|
|
|
|
m_keylshift = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-10-01 10:52:33 -04:00
|
|
|
|
case 57: // Space
|
|
|
|
|
if (!m_keySpace) {
|
|
|
|
|
std::cout << "Jump! " << std::endl;
|
|
|
|
|
m_keySpace = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-09-27 10:20:58 -04:00
|
|
|
|
case 24: // Y - Ignorer
|
|
|
|
|
case 255: // Fn - Ignorer
|
2021-10-19 10:27:59 -04:00
|
|
|
|
case 12: // M - Ignorer
|
2021-09-27 10:20:58 -04:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
std::cout << "Unhandled key: " << (int)key << std::endl;
|
|
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::KeyReleaseEvent(unsigned char key)
|
|
|
|
|
{
|
2021-10-11 11:37:58 -04:00
|
|
|
|
switch (key) {
|
2021-10-19 10:27:59 -04:00
|
|
|
|
case 12:
|
2021-10-25 10:50:08 -04:00
|
|
|
|
m_audio.ToggleMusicState();
|
2021-10-19 10:27:59 -04:00
|
|
|
|
break;
|
2021-09-27 10:20:58 -04:00
|
|
|
|
case 24: // Y
|
|
|
|
|
m_wireframe = !m_wireframe;
|
|
|
|
|
if (m_wireframe)
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
|
|
|
else
|
|
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
|
break;
|
|
|
|
|
case 22: // W
|
|
|
|
|
std::cout << "rW " << std::endl;
|
|
|
|
|
m_keyW = false;
|
|
|
|
|
break;
|
|
|
|
|
case 0: // A
|
|
|
|
|
std::cout << "rA " << std::endl;
|
|
|
|
|
m_keyA = false;
|
|
|
|
|
break;
|
|
|
|
|
case 18: // S
|
|
|
|
|
std::cout << "rS " << std::endl;
|
|
|
|
|
m_keyS = false;
|
|
|
|
|
break;
|
|
|
|
|
case 3: // D
|
|
|
|
|
std::cout << "rD " << std::endl;
|
|
|
|
|
m_keyD = false;
|
|
|
|
|
break;
|
2021-10-04 12:29:10 -04:00
|
|
|
|
case 38: // Left Shift
|
|
|
|
|
std::cout << "rLS " << std::endl;
|
|
|
|
|
m_keylshift = false;
|
|
|
|
|
case 57: // Espace
|
2021-10-01 10:52:33 -04:00
|
|
|
|
std::cout << "rSpace " << std::endl;
|
|
|
|
|
m_keySpace = false;
|
|
|
|
|
break;
|
2021-09-27 10:20:58 -04:00
|
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::MouseMoveEvent(int x, int y)
|
|
|
|
|
{
|
2021-10-04 12:29:10 -04:00
|
|
|
|
m_player.TurnLeftRight(x - (Width() / 2));
|
|
|
|
|
m_player.TurnTopBottom(y - (Height() / 2));
|
2021-09-27 13:15:57 -04:00
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
// Centrer la souris seulement si elle n'est pas d<>j<EFBFBD> centr<74>e
|
|
|
|
|
// Il est n<>cessaire de faire la v<>rification pour <20>viter de tomber
|
|
|
|
|
// dans une boucle infinie o<> l'appel <20> CenterMouse g<>n<EFBFBD>re un
|
|
|
|
|
// MouseMoveEvent, qui rapelle CenterMouse qui rapelle un autre
|
|
|
|
|
// MouseMoveEvent, etc
|
|
|
|
|
if (x == (Width() / 2) && y == (Height() / 2))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CenterMouse();
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::MousePressEvent(const MOUSE_BUTTON& button, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::MouseReleaseEvent(const MOUSE_BUTTON& button, int x, int y)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Engine::LoadTexture(Texture& texture, const std::string& filename, bool stopOnError)
|
|
|
|
|
{
|
2021-09-27 10:20:58 -04:00
|
|
|
|
texture.Load(filename);
|
|
|
|
|
if (!texture.IsValid())
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "Unable to load texture (" << filename << ")" << std::endl;
|
|
|
|
|
if (stopOnError)
|
|
|
|
|
Stop();
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-09-24 10:15:43 -04:00
|
|
|
|
|
2021-09-27 10:20:58 -04:00
|
|
|
|
return true;
|
2021-09-24 10:15:43 -04:00
|
|
|
|
}
|