Commit 3bd9c177 authored by Benjamin Castel's avatar Benjamin Castel
Browse files

feat: Added rotations to ships

parent cd24b02b
Loading
Loading
Loading
Loading
+2 −1
Changes for include/GlApplication.hpp: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ class GlApplication : public Application {
        static void usage(std::string & shortDescritpion, std::string & synopsis, std::string & description);

        void setFramebufferSize(int framebufferWidth, int framebufferHeight);
        void quickLog() const;

    private:
        enum ModelKeys {
@@ -41,7 +42,7 @@ class GlApplication : public Application {
        static void resize(GLFWwindow * window, int framebufferWidth, int framebufferHeight);
        static void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mods);
        void continuousKey(float deltaTime);
        void computeView(bool reset = false);
        void computeView(int player);

    private:
        std::map<ModelKeys, std::vector<std::shared_ptr<Entity>>> m_entities;  ///< objects to render
+13 −4
Changes for include/model/Entity.hpp: 13 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -17,17 +17,26 @@ class Entity {
        virtual void update(const double &time);

        glm::mat4 getModelMatrix() const;
        glm::vec3 get_x_axis() const;
        glm::vec3 get_y_axis() const;
        glm::vec3 get_z_axis() const;

        glm::vec3 get_position() const;

        void move(glm::vec3 translation);
        void rotate(glm::vec3 rotation);
        void rotate(glm::quat rotation);
        void setScale(float scale);

    private:
    protected:
        void computeModelMatrix(); 

        glm::mat4 m_modelMatrix;

        glm::vec3 m_position;
        glm::vec3 m_rotation;
        glm::quat m_rotation;
        float m_scale;
};

        glm::vec3 m_localX;
        glm::vec3 m_localY;
        glm::vec3 m_localZ;
};
+3 −0
Changes for include/model/Spaceship.hpp: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -21,4 +21,7 @@ class Spaceship : public Entity {

        void set_acceleration(const glm::vec3 &newAcceleration);
        void set_thrusting(const bool &newThrusting);

        // TODO : REMOVE AND REPLACE
        void thrust(const float &deltaTime);
};
+60 −17
Changes for src/GlApplication.cpp: 60 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -6,12 +6,11 @@ GlApplication::GlApplication(int windowWidth, int windowHeight)
          m_framebufferWidth(windowWidth), 
          m_framebufferHeight(windowHeight),
          m_spaceshipPlayer1(new Spaceship(Player::one, glm::mat4(1))),
          m_spaceshipPlayer2(new Spaceship(Player::two, glm::translate(glm::mat4(1), {0, 2, 0})))
          m_spaceshipPlayer2(new Spaceship(Player::two, glm::translate(glm::mat4(1), {0, 2, 2})))
{
    GLFWwindow * window = glfwGetCurrentContext();
    glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
    resize(window, windowWidth, windowHeight);
    computeView(true);

    glEnable(GL_DEPTH_TEST);

@@ -19,6 +18,12 @@ GlApplication::GlApplication(int windowWidth, int windowHeight)
    createEntities();
}

void GlApplication::quickLog() const {
    std:: cout << "LOG: SpaceshipPlayer2 {x=[" << this->m_spaceshipPlayer2->get_x_axis().x << ", " << this->m_spaceshipPlayer2->get_x_axis().y << ", " << this->m_spaceshipPlayer2->get_x_axis().z << "]; y=[" <<
    this->m_spaceshipPlayer2->get_y_axis().x << ", " << this->m_spaceshipPlayer2->get_y_axis().y << ", " << this->m_spaceshipPlayer2->get_y_axis().z << "]; z=[" <<
    this->m_spaceshipPlayer2->get_z_axis().x << ", " << this->m_spaceshipPlayer2->get_z_axis().y << ", " << this->m_spaceshipPlayer2->get_z_axis().z << "]}";
} 

void GlApplication::loadModels() {
    m_models[ModelKeys::SPACESHIP] = RenderObject::createWavefrontInstance("meshes/fighter/fighter_blue.obj");
    m_models[ModelKeys::PLANKS] = RenderObject::createWavefrontInstance("meshes/Pallet/Bswap_HPBake_Planks.obj");
@@ -36,12 +41,12 @@ void GlApplication::createEntities() {

    std::shared_ptr<Entity> plankEntity(new Entity());
    plankEntity->move({2, 1, -0.1});
    plankEntity->rotate({pi, 0, 0});
    plankEntity->rotate(glm::angleAxis(glm::radians(45.0f), glm::vec3(1.0f, 0.0f, 0.0f)));
    m_entities[ModelKeys::PLANKS].push_back(plankEntity);

    std::shared_ptr<Entity> tronEntity(new Entity());
    tronEntity->move({1., 0, 0});
    tronEntity->rotate({-pi / 2, -5 * pi, 0});
    tronEntity->rotate(glm::angleAxis(glm::radians(60.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
    tronEntity->setScale(0.25);
    m_entities[ModelKeys::TRON].push_back(tronEntity);
}
@@ -70,6 +75,7 @@ void GlApplication::renderFrame()
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(0, 0, m_framebufferWidth, m_framebufferHeight / 2);
    computeView(2);
    for(std::pair<const GlApplication::ModelKeys, std::vector<std::shared_ptr<Entity>>> &keyValue : m_entities) {
        std::unique_ptr<RenderObject> &modelToRender = m_models[keyValue.first];
        for(std::shared_ptr<Entity> &entity : keyValue.second) {
@@ -79,6 +85,7 @@ void GlApplication::renderFrame()
    }

    glViewport(0, m_framebufferHeight / 2, m_framebufferWidth, m_framebufferHeight / 2);
    computeView(1);
    for(std::pair<const GlApplication::ModelKeys, std::vector<std::shared_ptr<Entity>>> &keyValue : m_entities) {
        std::unique_ptr<RenderObject> &modelToRender = m_models[keyValue.first];
        for(std::shared_ptr<Entity> entity : keyValue.second) {
@@ -103,18 +110,19 @@ void GlApplication::update()
    }
}

void GlApplication::computeView(bool reset)
void GlApplication::computeView(int player)
{
    if (reset) {
        const float pi = glm::pi<float>();
        m_eyePhi = pi / 8;
        m_eyeTheta = pi / 2 + pi / 10;
    }

    glm::vec3 center(0, 0, 0);
    glm::vec3 up(0, 0, -1);
    glm::vec3 eyePos = 5.f * glm::vec3(cos(m_eyePhi) * sin(m_eyeTheta), sin(m_eyePhi) * sin(m_eyeTheta), cos(m_eyeTheta));
    if (player == 1) {
        glm::vec3 eyePos = this->m_spaceshipPlayer1->get_position() - normalize(this->m_spaceshipPlayer1->get_y_axis())*2.0f + this->m_spaceshipPlayer1->get_z_axis();
        glm::vec3 up     = this->m_spaceshipPlayer1->get_z_axis();
        glm::vec3 center = this->m_spaceshipPlayer1->get_position();
        m_view = glm::lookAt(eyePos, center, up);
    } else if (player == 2) {
        glm::vec3 eyePos = this->m_spaceshipPlayer2->get_position() - normalize(this->m_spaceshipPlayer2->get_y_axis())*2.0f + this->m_spaceshipPlayer2->get_z_axis();
        glm::vec3 up     = this->m_spaceshipPlayer2->get_z_axis();
        glm::vec3 center = this->m_spaceshipPlayer2->get_position();
        m_view = glm::lookAt(eyePos, center, up);
    }
}

void GlApplication::continuousKey(float deltaTime)
@@ -135,11 +143,46 @@ void GlApplication::continuousKey(float deltaTime)

    if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
        m_eyePhi += deltaTime * pi;
    } else if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
    }
    if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
        m_eyePhi -= deltaTime * pi;
    }
    
    computeView();
    float rads = glm::radians(20.0f*deltaTime);

    if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { // Spaceship 1 thrusters
        this->m_spaceshipPlayer1->thrust(deltaTime);
    }
    if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { // Spaceship 1 pitches down
        glm::vec3 xAxis = (float)sin(rads/2) * this->m_spaceshipPlayer1->get_x_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos( rads/2), xAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS
        || glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS) { // Spaceship 1 pitches up
        glm::vec3 xAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_x_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos(-rads/2), xAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS
        || glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) { // Spaceship 1 yaws left
        glm::vec3 zAxis = (float)sin( rads/2) * this->m_spaceshipPlayer1->get_z_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos( rads/2), zAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { // Spaceship 1 yaws right
        glm::vec3 zAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_z_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos(-rads/2), zAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) { // Spaceship 1 yaws left
        glm::vec3 yAxis = (float)sin( rads/2) * this->m_spaceshipPlayer1->get_y_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos( rads/2), yAxis));
    }
    if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) { // Spaceship 1 yaws right
        glm::vec3 yAxis = (float)sin(-rads/2) * this->m_spaceshipPlayer1->get_y_axis();
        this->m_spaceshipPlayer1->rotate(glm::quat(cos(-rads/2), yAxis));
    }

    if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
        this->quickLog();
    }
}

void GlApplication::resize(GLFWwindow * window, int framebufferWidth, int framebufferHeight)
@@ -159,7 +202,7 @@ void GlApplication::keyCallback(GLFWwindow * window, int key, int /*scancode*/,
    GlApplication & app = *static_cast<GlApplication *>(glfwGetWindowUserPointer(window));
    switch (key) {
        case 'R':
            app.computeView(true);
            app.computeView(0);
            break;
    }
}
+31 −10
Changes for src/model/Entity.cpp: 31 added lines, 10 removed lines.
Original line number Diff line number Diff line
#include "model/Entity.hpp"

#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/quaternion.hpp"


Entity::Entity() 
        : m_position(glm::vec3(0)),
          m_rotation(glm::vec3(0)),
          m_scale(1)
          m_rotation(glm::quat(0, 1, 0, 0)),
          m_scale(1), m_localX(1, 0, 0),
          m_localY(0, 1, 0), m_localZ(0, 0, 1)
{

}
@@ -18,9 +23,20 @@ void Entity::move(glm::vec3 translation) {
    this->computeModelMatrix();
}

void Entity::rotate(glm::vec3 rotation) {
    this->m_rotation = this->m_rotation + rotation;
void Entity::rotate(glm::quat rotation) {
    this->m_localX = normalize(rotation * this->m_localX);
    this->m_localY = normalize(rotation * this->m_localY);
    this->m_localZ = normalize(rotation * this->m_localZ);

    this->m_rotation = rotation * this->m_rotation; //*this->m_rotation;
    float l = sqrt(this->m_rotation.w * this->m_rotation.w +
                   this->m_rotation.x * this->m_rotation.x +
                   this->m_rotation.y * this->m_rotation.y +
                   this->m_rotation.z * this->m_rotation.z);
    
    /*std::cout << "SPACESHIP : " << l <<
    " / w=" << this->m_rotation.w << " / x=" << this->m_rotation.x <<
    " / y=" << this->m_rotation.y << " / z=" << this->m_rotation.z << std::endl;*/
    this->computeModelMatrix();
}

@@ -31,14 +47,19 @@ void Entity::setScale(float scale) {
}

void Entity::computeModelMatrix() {
    this->m_modelMatrix = glm::mat4(1);
    this->m_modelMatrix = glm::translate(this->m_modelMatrix, this->m_position);
    this->m_modelMatrix = glm::rotate(this->m_modelMatrix, this->m_rotation[0], {1, 0, 0});
    this->m_modelMatrix = glm::rotate(this->m_modelMatrix, this->m_rotation[1], {0, 1, 0});
    this->m_modelMatrix = glm::rotate(this->m_modelMatrix, this->m_rotation[2], {0, 0, 1});
    this->m_modelMatrix = glm::scale(this->m_modelMatrix, glm::vec3(this->m_scale));
    glm::mat4 translateMatrix = glm::translate(glm::mat4(1), this->m_position);
    glm::mat4 scaleMatrix     = glm::scale(glm::mat4(1), glm::vec3(this->m_scale));
    glm::mat4 rotationMatrix  = glm::mat4_cast(this->m_rotation);

    this->m_modelMatrix = translateMatrix * scaleMatrix * rotationMatrix;
}

glm::mat4 Entity::getModelMatrix() const {
    return this->m_modelMatrix;
}

glm::vec3 Entity::get_x_axis() const { return this->m_localX; }
glm::vec3 Entity::get_y_axis() const { return this->m_localY; }
glm::vec3 Entity::get_z_axis() const { return this->m_localZ; }

glm::vec3 Entity::get_position() const { return this->m_position; }
Loading