Commit e9f7a61a authored by Benjamin Castel's avatar Benjamin Castel
Browse files

Commentted most of implementation files (lacking time to do more sadly)

parent 6070642f
Loading
Loading
Loading
Loading
+65 −0
Changes for src/GlApplication.cpp: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include "GlApplication.hpp"

/**
 * @brief Constructs a new GlApplication object
 * Constructor to a GlApplication object, given it's window dimensions
 * @param windowWidth The application's window's width
 * @param windowHeight The application's window's height
 */
GlApplication::GlApplication(int windowWidth, int windowHeight) 
        : Application(windowWidth, windowHeight), 
          drawBoundingBoxes(false),
@@ -30,6 +36,10 @@ GlApplication::GlApplication(int windowWidth, int windowHeight)
    this->m_guiElement.push_back(healthbar);
}

/**
 * @brief Method reseting the game's state
 * Method resetting the game's state, respawning entities and erasing the winner
 */
void GlApplication::resetGame() {
    this->winner = WIN_NOT_WIN_YET;

@@ -38,16 +48,28 @@ void GlApplication::resetGame() {
    this->createEntities();
}

/**
 * @brief Method printing a quick log
 * Method `cout`-ing a quick log
 */
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 << "]}" << std::endl;
}

/**
 * @brief Method toggling OBB
 * A method toggling OBB in-world displaying
 */
void GlApplication::toggleHitboxes() {
    this->drawBoundingBoxes = !this->drawBoundingBoxes;
}

/**
 * @brief Method loading every models
 * Method loading every 3D model into the application's buffer
 */
void GlApplication::loadModels() {
    m_models[ModelKeys::SPACESHIP_1] = RenderObject::createWavefrontInstance("meshes/fighter/fighter_blue.obj");
    m_models[ModelKeys::SPACESHIP_2] = RenderObject::createWavefrontInstance("meshes/fighter/fighter_red.obj");
@@ -70,6 +92,10 @@ void GlApplication::loadModels() {
    m_models[ModelKeys::OBB]->setDisableLightning(true);
}

/**
 * @brief Method creating game's entities
 * Method spawning every entities, putting them into the application's buffer.
 */
void GlApplication::createEntities() {
    const float pi = glm::pi<float>();

@@ -116,6 +142,10 @@ void GlApplication::createEntities() {
    m_entities[ModelKeys::SKYBOX].push_back(skybox);
}

/**
 * @brief Method setting window callbacks
 * Method setting's every required GLFW callbacks
 */
void GlApplication::setCallbacks()
{
    GLFWwindow * window = glfwGetCurrentContext();
@@ -123,6 +153,9 @@ void GlApplication::setCallbacks()
    glfwSetKeyCallback(window, GlApplication::keyCallback);
}

/**
 * @brief Method printing the usage of the game [DEPRECATED]
 */
void GlApplication::usage(std::string & shortDescription, std::string & synopsis, std::string & description)
{
    shortDescription = "Application for programming assignment 4";
@@ -134,6 +167,10 @@ void GlApplication::usage(std::string & shortDescription, std::string & synopsis
        "     R                reset the view\n";
}

/**
 * @brief Method rendering correct frame
 * Metrhod choosing which kind of screen to render
 */
void GlApplication::renderFrame()
{
    if(winner == WIN_NOT_WIN_YET) {
@@ -143,6 +180,10 @@ void GlApplication::renderFrame()
    }    
}

/**
 * @brief Method rendering game frame
 * Method rendering the game scene as a split screen game
 */
void GlApplication::renderGameFrame() {
    glClearColor(1, 0, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -190,6 +231,10 @@ void GlApplication::renderGameFrame() {
    renderGUI();
}

/**
 * @brief Method rendering end-game screens
 * Method choosing which end-game screen to render then properly rendering it
 */
void GlApplication::renderGameEndedFrame() {
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -225,6 +270,10 @@ void GlApplication::renderGameEndedFrame() {
    guiProgram.unbind();
}

/**
 * @brief Method rendering a GUI
 * Method getting every element to render then rendering them
 */
void GlApplication::renderGUI() {
    glDisable(GL_DEPTH_TEST);

@@ -238,6 +287,10 @@ void GlApplication::renderGUI() {
    glEnable(GL_DEPTH_TEST);
}

/**
 * @brief Method updating the window
 * Method updating the window
 */
void GlApplication::update()
{
    if(winner == WIN_NOT_WIN_YET) {
@@ -247,6 +300,10 @@ void GlApplication::update()
    }
}

/**
 * @brief Method updating the game
 * Method updating the game by updating every entities and handling somne behaviours
 */
void GlApplication::updateGame() {
    float prevTime = m_currentTime;
    m_currentTime = glfwGetTime();
@@ -333,6 +390,9 @@ void GlApplication::updateGame() {
    }
}

/**
 * @brief Method updateing end-game screens
 */
void GlApplication::updateGameEnded() {
    GLFWwindow *window = glfwGetCurrentContext();

@@ -341,6 +401,11 @@ void GlApplication::updateGameEnded() {
    }
}

/**
 * @brief Method computing the view matrixes
 * Method implementing cameras and computing the screen's view matrices
 * @param player the player to compute the view matrix's
 */
void GlApplication::computeView(Player player)
{
    if (player == Player::one) {
+14 −0
Changes for src/model/Asteroid.cpp: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include "model/Asteroid.hpp"


/**
 * @brief Constructs a new Astreroid object
 * Asteroid default constructor, using glm::pi and assigning random rotation Euler angles
 */
Asteroid::Asteroid()
        : Entity() {
    const float pi = glm::pi<float>();
    this->rotationAngle = glm::vec3(glm::radians((float)(rand() % 180)) - pi, glm::radians((float)(rand() % 180)) - pi, glm::radians((float)(rand() % 180)) - pi);
}

/**
 * @brief Détruit l'objet Asteroid
 * 
 */
Asteroid::~Asteroid() {}

/**
 * @brief Method updating the asteroid
 * method overriding Entity::update rotating the asteroid given a delta-time value
 * @param deltaTime 
 */
void Asteroid::update(const float &deltaTime) {
    glm::vec3 xAxis = (float)sin(this->rotationAngle.x * deltaTime * ASTEROID_SPEED) * this->get_x_axis();
    rotate(glm::quat(cos(this->rotationAngle.x * deltaTime * ASTEROID_SPEED), xAxis));
+95 −39
Changes for src/model/Entity.cpp: 95 added lines, 39 removed lines.
Original line number Diff line number Diff line
@@ -8,54 +8,77 @@
#include "glm/gtx/quaternion.hpp"


/**
 * @brief Construct a new Entity object
 * Entity default constructor, placing the entity at the center of the 3D world, facing Y front, Unscaled, with world-aligned local axis 
 */
Entity::Entity() 
        : m_position(glm::vec3(0)),
          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)
{

}

void Entity::update(const float &time) {

}

          m_localY(0, 1, 0), m_localZ(0, 0, 1) {}

/**
 * @brief Update method supposed to be called each frame
 * Overridable virtual update method used to update any entity's state each frame
 * @param time time elapsed since last loop
 */
void Entity::update(const float &time) {}

/**
 * @brief Method used to move the entity
 * Method translating the entity's current position by adding a translation vector to it, then computing the model matrix
 * @param translation The translation vector
 */
void Entity::move(glm::vec3 translation) {
    this->m_position += translation;

    this->computeModelMatrix();
}

/**
 * @brief Method used to rotate the entity
 * Method rotating the entity's current position by using a quaternion, then computing the model matrix
 * @param rotation The rotation quaternion
 */
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);
    this->m_rotation = rotation * this->m_rotation;

    /*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();
}

/**
 * @brief Method used to scale the entity by a single value
 * method scaling the entity by creating a vector with all 3 components being equal to a single scale factor, then computing the model matrix
 * @param scale The single scale factor
 */
void Entity::setScale(float scale) {
    this->m_scale = glm::vec3(scale);

    this->computeModelMatrix();
}

/**
 * @brief Method used to scale the entity by a value on each axis
 * Method scaling the entity by a given value on each axis, then computing the model matrix
 * @param scales 
 */
void Entity::setScale(glm::vec3 scales) {
    this->m_scale = scales;

    this->computeModelMatrix();
}

/**
 * @brief Method computing the entity's OBB's model matrix
 * Method using the entity's corresponding 3D model's renderObject to compuute it's OBB's model matrix for in-world display
 * @param renderObject The entity's 3D model's RenderObject
 * @return glm::mat4 The entity's bounding box's model matrix
 */
glm::mat4 Entity::get_box_model_matrix(std::unique_ptr<RenderObject> &renderObject) const {
    glm::vec3 boxDims = renderObject->get_dimensions();

@@ -72,6 +95,10 @@ glm::mat4 Entity::get_box_model_matrix(std::unique_ptr<RenderObject> &renderObje
    return unitTranslateMatrix * unitRotationMatrix * unitScaleMatrix; 
}

/**
 * @brief Method computing the entity's model matrix for in-world display
 * Method using the entity's position, rotation and scale to compute it's model matrix for in-world display
 */
void Entity::computeModelMatrix() {
    glm::mat4 translateMatrix = glm::translate(glm::mat4(1), this->m_position);
    glm::mat4 scaleMatrix     = glm::scale(glm::mat4(1), this->m_scale);
@@ -80,6 +107,19 @@ void Entity::computeModelMatrix() {
    this->m_modelMatrix = translateMatrix * rotationMatrix * scaleMatrix;
}

/**
 * @brief Method returning wether the object is colliding with another one
 * Method returnning wether or not the object is colliding with another, given an other entity and both entities' corresponding renderObjects.
 * If `movingObjects` is set to true, the method also take in account the two objects' velocities
 * @param renderObjectThis The entity's corresponding renderObject
 * @param other The other entity
 * @param renderObjectOther The other entity's render object
 * @param movingObjects Wether or not the object should take velocities in account
 * @param velocityThis The entity's velocity
 * @param velocityOther The other entity's velocity
 * @return true if ther is a collision with the other object
 * @return false if ther is no collision with the other object
 */
bool Entity::check_box_intersection(const std::unique_ptr<RenderObject> &renderObjectThis,
                                    const Entity &other,
                                    const std::unique_ptr<RenderObject> &renderObjectOther,
@@ -114,27 +154,6 @@ bool Entity::check_box_intersection(const std::unique_ptr<RenderObject> &renderO
        boxDimsOther.z * other.m_scale.z
    };

    /*std::array<glm::vec3, 8> verticesThis {
        centerThis + extentsThis[0]*axisThis[0] + extentsThis[1]*axisThis[1] + extentsThis[2]*axisThis[2],
        centerThis - extentsThis[0]*axisThis[0] + extentsThis[1]*axisThis[1] + extentsThis[2]*axisThis[2],
        centerThis + extentsThis[0]*axisThis[0] - extentsThis[1]*axisThis[1] + extentsThis[2]*axisThis[2],
        centerThis - extentsThis[0]*axisThis[0] - extentsThis[1]*axisThis[1] + extentsThis[2]*axisThis[2],
        centerThis + extentsThis[0]*axisThis[0] + extentsThis[1]*axisThis[1] - extentsThis[2]*axisThis[2],
        centerThis - extentsThis[0]*axisThis[0] + extentsThis[1]*axisThis[1] - extentsThis[2]*axisThis[2],
        centerThis + extentsThis[0]*axisThis[0] - extentsThis[1]*axisThis[1] - extentsThis[2]*axisThis[2],
        centerThis - extentsThis[0]*axisThis[0] - extentsThis[1]*axisThis[1] - extentsThis[2]*axisThis[2]
    };
    std::array<glm::vec3, 8> verticesOther {
        centerOther + extentsOther[0]*axisOther[0] + extentsOther[1]*axisOther[1] + extentsOther[2]*axisOther[2],
        centerOther - extentsOther[0]*axisOther[0] + extentsOther[1]*axisOther[1] + extentsOther[2]*axisOther[2],
        centerOther + extentsOther[0]*axisOther[0] - extentsOther[1]*axisOther[1] + extentsOther[2]*axisOther[2],
        centerOther - extentsOther[0]*axisOther[0] - extentsOther[1]*axisOther[1] + extentsOther[2]*axisOther[2],
        centerOther + extentsOther[0]*axisOther[0] + extentsOther[1]*axisOther[1] - extentsOther[2]*axisOther[2],
        centerOther - extentsOther[0]*axisOther[0] + extentsOther[1]*axisOther[1] - extentsOther[2]*axisOther[2],
        centerOther + extentsOther[0]*axisOther[0] - extentsOther[1]*axisOther[1] - extentsOther[2]*axisOther[2],
        centerOther - extentsOther[0]*axisOther[0] - extentsOther[1]*axisOther[1] - extentsOther[2]*axisOther[2]
    };*/

    glm::mat3 cMatrix {
        {glm::dot(axisThis[0], axisOther[0]), glm::dot(axisThis[0], axisOther[1]), glm::dot(axisThis[0], axisOther[2])},
        {glm::dot(axisThis[1], axisOther[0]), glm::dot(axisThis[1], axisOther[1]), glm::dot(axisThis[1], axisOther[2])},
@@ -242,19 +261,56 @@ bool Entity::check_box_intersection(const std::unique_ptr<RenderObject> &renderO
        }
    }

    return true; // Everything failed
    return true;
}

/**
 * @brief Method returning the entity's model matrix
 * Method returning the entity's pre-computed model matrix
 * @return glm::mat4 The model matrix of the entity
 */
glm::mat4 Entity::getModelMatrix() const {
    return this->m_modelMatrix;
}

/**
 * @brief Method returning the entity's X axis
 * Method returning the entity's local X axis
 * @return glm::vec3 The in-world representation of the entity's local X axis
 */
glm::vec3 Entity::get_x_axis() const { return this->m_localX; }

/**
 * @brief Method returning the entity's Y axis
 * Method returning the entity's local Y axis
 * @return glm::vec3 The in-world representation of the entity's local Y axis
 */
glm::vec3 Entity::get_y_axis() const { return this->m_localY; }

/**
 * @brief Method returning the entity's Z axis
 * Method returning the entity's local Z axis
 * @return glm::vec3 The in-world representation of the entity's local Z axis
 */
glm::vec3 Entity::get_z_axis() const { return this->m_localZ; }

/**
 * @brief Method returning the entity's in-world position
 * Method returning the entity's in-world position
 * @return glm::vec3 The entity's in-world position
 */
glm::vec3 Entity::get_position() const { return this->m_position; }

/**
 * @brief Method returning the entity's in-world rotation
 * Method returning the entity's in-world rotation
 * @return glm::vec3 The entity's in-world rotation
 */
glm::quat Entity::get_rotation() const { return this->m_rotation; }

/**
 * @brief Method returning the entity's in-world velocity
 * Method returning the entity's in-world velocity
 * @return glm::vec3 The entity's in-world velocity
 */
glm::vec3 Entity::get_velocity(const float &deltaTime) const { return glm::vec3(0); }
+11 −0
Changes for src/model/LifeGui.cpp: 11 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include "model/LifeGui.hpp"


/**
 * @brief Constructs a new LifeGui object
 * LifeGui default constructor, taking a rendering program
 * @param program The program used to render the LifeGui GUI element
 */
LifeGui::LifeGui(Program &program) 
		: GuiElement(program),
		  hp_3_texture(new Texture(GL_TEXTURE_2D)),
@@ -25,6 +31,11 @@ LifeGui::LifeGui(Program &program)
	setTexture(this->hp_3_texture);
}

/**
 * @brief Method setting the current life amount
 * Method setting how much health points the GUI element should be representing
 * @param life 
 */
void LifeGui::setCurrentLife(const unsigned int &life) {
	if(life >= 3) {
		this->setTexture(this->hp_3_texture);
+16 −0
Changes for src/model/Missile.cpp: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include "model/Missile.hpp"


/**
 * @brief Constructs a new Missile object
 * Constructor to a Missile object taking the player's ship that shot it in account
 * @param shotBy The spaceship that shot the missile
 */
Missile::Missile(const Entity* const shotBy)
        : Entity() { 
    m_position = shotBy->get_position();
@@ -11,8 +17,18 @@ Missile::Missile(const Entity* const shotBy)
    computeModelMatrix();
}

/**
 * @brief Destroys the Missile object
 * Destroys the Missile object
 */
Missile::~Missile() {}

/**
 * @brief Method updating the missile
 * Method overriding Entity::update updating the missile's position. Currently, other behaviours are handled in the Application
 * class because of a lack of developpement time
 * @param deltaTime 
 */
void Missile::update(const float &deltaTime) {
    move(normalize(this->m_localZ) * 30.0f * deltaTime);
}
Loading