#include "engine.h" #include #include #include #include "transformation.h" #include "player.h" Engine::Engine() { } Engine::~Engine() { } void Engine::Init() { 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); // Light GLfloat light0Pos[4] = { 0.0f, CHUNK_SIZE_Y, 0.0f, 1.0f }; GLfloat light0Amb[4] = { 0.9f, 0.9f, 0.9f, 1.f }; GLfloat light0Diff[4] = { 1.f, 1.f, 1.f, 1.f }; 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); // Lumière de Skybox. GLfloat light1Pos[4] = { 0.0f, 0.0f, 0.0f, 2.0f }; GLfloat light1Amb[4] = { 1.5f, 1.5f, 1.5f, 1.5f }; GLfloat light1Diff[4] = { 1.f, 1.f, 1.f, 1.f }; GLfloat light1Spec[4] = { 1.f, 1.f, 1.f, 1.0f }; glLightfv(GL_LIGHT1, GL_POSITION, light1Pos); glLightfv(GL_LIGHT1, GL_AMBIENT, light1Amb); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1Diff); glLightfv(GL_LIGHT1, GL_SPECULAR, light1Spec); // Blend glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); CenterMouse(); HideCursor(); } void Engine::DeInit() { } void Engine::LoadResource() { LoadTexture(m_textureFloor, TEXTURE_PATH "grass.png"); LoadTexture(m_textureSkybox, TEXTURE_PATH "skybox.png"); 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"); } void Engine::UnloadResource() { } void Engine::Render(float elapsedTime) { static float gameTime = elapsedTime; gameTime += elapsedTime; Transformation all; Transformation skybox; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Transformations initiales glMatrixMode(GL_MODELVIEW); glLoadIdentity(); m_player.Move(m_keyW, m_keyS, m_keyA, m_keyD, m_keySpace, m_keylshift, elapsedTime); m_player.ApplyTransformation(all); 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!). // Skybox - Pas de normals pour avoir un éclairage flat? skybox.Use(); m_textureSkybox.Bind(); glDisable(GL_DEPTH_TEST); // Pour que tout ce qui est dessiné après soit dessiné par-dessus la skybox. glDisable(GL_LIGHT0); glEnable(GL_LIGHT1); glBegin(GL_QUADS); glTexCoord2f(0, .75f); glVertex3f(5.f, 5.f, 5.f); glTexCoord2f(.25f, .75f); glVertex3f(-5.f, 5.f, 5.f); glTexCoord2f(.25f, .50f); glVertex3f(-5.f, -5.f, 5.f); glTexCoord2f(0, .50f); glVertex3f(5.f, -5.f, 5.f); glTexCoord2f(.25f, .75f); glVertex3f(-5.f, 5.f, 5.f); glTexCoord2f(.50f, .75f); glVertex3f(-5.f, 5.f, -5.f); glTexCoord2f(.50f, .50f); glVertex3f(-5.f, -5.f, -5.f); glTexCoord2f(.25f, .50f); glVertex3f(-5.f, -5.f, 5.f); glTexCoord2f(.50f, .75f); glVertex3f(-5.f, 5.f, -5.f); glTexCoord2f(.75f, .75f); glVertex3f(5.f, 5.f, -5.f); glTexCoord2f(.75f, .50f); glVertex3f(5.f, -5.f, -5.f); glTexCoord2f(.50f, .50f); glVertex3f(-5.f, -5.f, -5.f); glTexCoord2f(.75f, .75f); glVertex3f(5.f, 5.f, -5.f); glTexCoord2f(1, .75f); glVertex3f(5.f, 5.f, 5.f); glTexCoord2f(1, .50f); glVertex3f(5.f, -5.f, 5.f); glTexCoord2f(.75f, .50f); glVertex3f(5.f, -5.f, -5.f); glTexCoord2f(.25f, 1); glVertex3f(5.f, 5.f, 5.f); glTexCoord2f(.50f, 1); glVertex3f(5.f, 5.f, -5.f); glTexCoord2f(.50f, .75f); glVertex3f(-5.f, 5.f, -5.f); glTexCoord2f(.25f, .75f); glVertex3f(-5.f, 5.f, 5.f); glTexCoord2f(.50f, .50f); glVertex3f(-5.f, -5.f, -5.f); glTexCoord2f(.75f, .50f); glVertex3f(5.f, -5.f, -5.f); glTexCoord2f(.75f, .25f); glVertex3f(5.f, -5.f, 5.f); glTexCoord2f(.50f, .25f); glVertex3f(-5.f, -5.f, 5.f); glEnd(); glEnable(GL_DEPTH_TEST); glDisable(GL_LIGHT1); glEnable(GL_LIGHT0); // Plancher // Les vertex doivent etre affiches dans le sens anti-horaire (CCW) all.Use(); m_textureFloor.Bind(); float nbRep = 5.f; 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(); // Cube all.ApplyTranslation(0.f, 0.f, -10.f); all.ApplyRotation(gameTime * 100.f, 0.f, 1.f, 0.f); all.ApplyRotation(gameTime * 100.f, 0.f, 0.f, 1.f); all.ApplyRotation(gameTime * 100.f, 1.f, 0.f, 0.f); all.Use(); switch ((int)(gameTime*5) % 4) { case 0: m_textureCube1.Bind(); break; case 1: m_textureCube2.Bind(); break; case 2: m_textureCube3.Bind(); break; case 3: m_textureCube4.Bind(); break; } glEnable(GL_BLEND); glBegin(GL_QUADS); glNormal3f(0, 0, 1); glTexCoord2f(0, 1); glVertex3f(0.5f, 0.5f, 0.5f); glTexCoord2f(1, 1); glVertex3f(-0.5f, 0.5f, 0.5f); glTexCoord2f(1, 0); glVertex3f(-0.5f, -0.5f, 0.5f); glTexCoord2f(0, 0); glVertex3f(0.5f, -0.5f, 0.5f); glNormal3f(0, 0, -1); glTexCoord2f(1, 0); glVertex3f(0.5f, -0.5f, -0.5f); glTexCoord2f(0, 0); glVertex3f(-0.5f, -0.5f, -0.5f); glTexCoord2f(0, 1); glVertex3f(-0.5f, 0.5f, -0.5f); glTexCoord2f(1, 1); glVertex3f(0.5f, 0.5f, -0.5f); glNormal3f(-1, 0, 0); glTexCoord2f(0, 1); glVertex3f(-0.5f, 0.5f, 0.5f); glTexCoord2f(1, 1); glVertex3f(-0.5f, 0.5f, -0.5f); glTexCoord2f(1, 0); glVertex3f(-0.5f, -0.5f, -0.5f); glTexCoord2f(0, 0); glVertex3f(-0.5f, -0.5f, 0.5f); glNormal3f(1, 0, 0); glTexCoord2f(0, 1); glVertex3f(0.5f, 0.5f, -0.5f); glTexCoord2f(1, 1); glVertex3f(0.5f, 0.5f, 0.5f); glTexCoord2f(1, 0); glVertex3f(0.5f, -0.5f, 0.5f); glTexCoord2f(0, 0); glVertex3f(0.5f, -0.5f, -0.5f); glNormal3f(0, 1, 0); glTexCoord2f(0, 0); glVertex3f(-0.5f, 0.5f, -0.5f); glTexCoord2f(0, 1); glVertex3f(-0.5f, 0.5f, 0.5f); glTexCoord2f(1, 1); glVertex3f(0.5f, 0.5f, 0.5f); glTexCoord2f(1, 0); glVertex3f(0.5f, 0.5f, -0.5f); glNormal3f(0, -1, 0); glTexCoord2f(0, 0); glVertex3f(-0.5f, -0.5f, -0.5f); glTexCoord2f(0, 1); glVertex3f(-0.5f, -0.5f, 0.5f); glTexCoord2f(1, 1); glVertex3f(0.5f, -0.5f, 0.5f); glTexCoord2f(1, 0); glVertex3f(0.5f, -0.5f, -0.5f); glDisable(GL_BLEND); glEnd(); } void Engine::KeyPressEvent(unsigned char key) { switch (key) { 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; case 38: // Left Shift if (!m_keylshift) { std::cout << "Dash!" << std::endl; m_keylshift = true; } break; case 57: // Space if (!m_keySpace) { std::cout << "Jump! " << std::endl; m_keySpace = true; } break; case 24: // Y - Ignorer case 255: // Fn - Ignorer break; default: std::cout << "Unhandled key: " << (int)key << std::endl; } } void Engine::KeyReleaseEvent(unsigned char key) { switch (key) { 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; case 38: // Left Shift std::cout << "rLS " << std::endl; m_keylshift = false; case 57: // Espace std::cout << "rSpace " << std::endl; m_keySpace = false; break; } } void Engine::MouseMoveEvent(int x, int y) { m_player.TurnLeftRight(x - (Width() / 2)); m_player.TurnTopBottom(y - (Height() / 2)); // Centrer la souris seulement si elle n'est pas déjà centrée // Il est nécessaire de faire la vérification pour éviter de tomber // dans une boucle infinie où l'appel à CenterMouse génère un // MouseMoveEvent, qui rapelle CenterMouse qui rapelle un autre // MouseMoveEvent, etc if (x == (Width() / 2) && y == (Height() / 2)) return; CenterMouse(); } 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) { texture.Load(filename); if (!texture.IsValid()) { std::cerr << "Unable to load texture (" << filename << ")" << std::endl; if (stopOnError) Stop(); return false; } return true; }