diff --git a/common/include/utils.hpp b/common/include/utils.hpp index 6a6cdc1..94104a4 100644 --- a/common/include/utils.hpp +++ b/common/include/utils.hpp @@ -37,7 +37,6 @@ typedef struct Point { x * other.y - y * other.x); } - } Point; struct Point2D { diff --git a/engine/include/Camera.hpp b/engine/include/Camera.hpp index be9d931..4c22bb3 100644 --- a/engine/include/Camera.hpp +++ b/engine/include/Camera.hpp @@ -1,13 +1,13 @@ #ifndef CAMERA_HPP #define CAMERA_HPP -#include #include #include +#include + #include "Camera.hpp" #include "utils.hpp" - class Camera { public: glm::vec3 position; @@ -21,8 +21,8 @@ class Camera { float far; Camera(); - Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, float near, - float far); + Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, + float near, float far); Camera(const Camera& other); diff --git a/engine/include/controller.hpp b/engine/include/controller.hpp index 49784ae..38fede4 100644 --- a/engine/include/controller.hpp +++ b/engine/include/controller.hpp @@ -1,35 +1,35 @@ #pragma once #include + #include "Camera.hpp" class CameraController { -public: - // Ctor - CameraController(Camera& camera) - : m_camera{camera} { - // This is temporary while the conversion from CameraData to Camera type - // is still not in place. - m_camera.forward = glm::normalize(m_camera.forward - m_camera.position); - } - // Copy - CameraController(const CameraController&) = delete; - CameraController& operator=(const CameraController&) = delete; - // Move - CameraController(CameraController&&) = delete; - CameraController& operator=(CameraController&&) = delete; - // Dtor - ~CameraController() = default; + public: + // Ctor + CameraController(Camera& camera) : m_camera{camera} { + // This is temporary while the conversion from CameraData to Camera type + // is still not in place. + m_camera.forward = glm::normalize(m_camera.forward - m_camera.position); + } + // Copy + CameraController(const CameraController&) = delete; + CameraController& operator=(const CameraController&) = delete; + // Move + CameraController(CameraController&&) = delete; + CameraController& operator=(CameraController&&) = delete; + // Dtor + ~CameraController() = default; - void update(float delta_time, bool& imgui); + void update(float delta_time, bool& imgui); -private: - static constexpr float normal_speed = 5.f; - static constexpr float sprint_speed = 10.f; - // FIXME: Change ref to something else - Camera& m_camera; - float m_speed = normal_speed; - float m_sensitivity = 1.f; + private: + static constexpr float normal_speed = 5.f; + static constexpr float sprint_speed = 10.f; + // FIXME: Change ref to something else + Camera& m_camera; + float m_speed = normal_speed; + float m_sensitivity = 1.f; }; glm::vec3 spherical2Cartesian(float theta, float phi, float r); diff --git a/engine/include/curves.hpp b/engine/include/curves.hpp index 72fdb6d..3529b3e 100644 --- a/engine/include/curves.hpp +++ b/engine/include/curves.hpp @@ -8,9 +8,7 @@ #include "model.hpp" #include "utils.hpp" - -enum Transformations {TIMEROTATION, TIMETRANSLATE, STATIC}; - +enum Transformations { TIMEROTATION, TIMETRANSLATE, STATIC }; class TimeRotations { public: @@ -44,10 +42,8 @@ class TimeTranslations { std::array rotationMatrix(Point x, Point y, Point z); }; - - glm::mat4 Scalematrix(float x, float y, float z); - glm::mat4 Rotationmatrix(float angle, float x, float y, float z); - glm::mat4 Translatematrix(float x, float y, float z); - +glm::mat4 Scalematrix(float x, float y, float z); +glm::mat4 Rotationmatrix(float angle, float x, float y, float z); +glm::mat4 Translatematrix(float x, float y, float z); #endif // CURVES_HPP \ No newline at end of file diff --git a/engine/include/frustsum.hpp b/engine/include/frustsum.hpp index 48575d9..96c988c 100644 --- a/engine/include/frustsum.hpp +++ b/engine/include/frustsum.hpp @@ -2,81 +2,84 @@ #define FRUSTSUM_CPP #include -#include + #include -#include "Window.hpp" -#include "Camera.hpp" +#include +#include "Camera.hpp" +#include "Window.hpp" struct Plane { - - glm::vec3 point = { 0.f, 0.f, 0.f }; - glm::vec3 normal = { 0.f, 1.f, 0.f }; - glm::vec4 abcd = { 0.f, 0.f, 0.f, 0.f }; - - Plane() = default; - Plane(const Plane& other) = default; - Plane(const glm::vec3& normal, glm::vec3 point); - - // rethink distance to point - float distanceToPoint(const glm::vec3& point) const { - return abcd.x * point.x + abcd.y * point.y + abcd.z * point.z + abcd.w; - } - - - void printPlane() { - std::cout << "Point: " << point.x << " " << point.y << " " << point.z << std::endl; - std::cout << "Normal: " << normal.x << " " << normal.y << " " << normal.z << std::endl; - } - - + glm::vec3 point = {0.f, 0.f, 0.f}; + glm::vec3 normal = {0.f, 1.f, 0.f}; + glm::vec4 abcd = {0.f, 0.f, 0.f, 0.f}; + + Plane() = default; + Plane(const Plane& other) = default; + Plane(const glm::vec3& normal, glm::vec3 point); + + // rethink distance to point + float distanceToPoint(const glm::vec3& point) const { + return abcd.x * point.x + abcd.y * point.y + abcd.z * point.z + abcd.w; + } + + void printPlane() { + std::cout << "Point: " << point.x << " " << point.y << " " << point.z + << std::endl; + std::cout << "Normal: " << normal.x << " " << normal.y << " " << normal.z + << std::endl; + } }; struct Frustsum { - Plane nearFace; - Plane farFace; - Plane rightFace; - Plane leftFace; - Plane topFace; - Plane bottomFace; - bool on = true; - - Frustsum() = default; - Frustsum(const Frustsum& other) = default; - Frustsum(const Plane& nearFace, const Plane& farFace, const Plane& rightFace, const Plane& leftFace, const Plane& topFace, const Plane& bottomFace) - : nearFace(nearFace), farFace(farFace), rightFace(rightFace), leftFace(leftFace), topFace(topFace), bottomFace(bottomFace) {} - Frustsum(const Camera& cam, float ratio, bool on); - - void printFrustsum() { - std::cout << "Near" << std::endl; - nearFace.printPlane(); - std::cout << "Far" << std::endl; - farFace.printPlane(); - std::cout << "right" << std::endl; - rightFace.printPlane(); - std::cout << "left" << std::endl; - leftFace.printPlane(); - std::cout << "top" << std::endl; - topFace.printPlane(); - std::cout << "bottom" << std::endl; - bottomFace.printPlane(); - } - + Plane nearFace; + Plane farFace; + Plane rightFace; + Plane leftFace; + Plane topFace; + Plane bottomFace; + bool on = true; + + Frustsum() = default; + Frustsum(const Frustsum& other) = default; + Frustsum(const Plane& nearFace, const Plane& farFace, const Plane& rightFace, + const Plane& leftFace, const Plane& topFace, const Plane& bottomFace) + : nearFace(nearFace), + farFace(farFace), + rightFace(rightFace), + leftFace(leftFace), + topFace(topFace), + bottomFace(bottomFace) {} + Frustsum(const Camera& cam, float ratio, bool on); + + void printFrustsum() { + std::cout << "Near" << std::endl; + nearFace.printPlane(); + std::cout << "Far" << std::endl; + farFace.printPlane(); + std::cout << "right" << std::endl; + rightFace.printPlane(); + std::cout << "left" << std::endl; + leftFace.printPlane(); + std::cout << "top" << std::endl; + topFace.printPlane(); + std::cout << "bottom" << std::endl; + bottomFace.printPlane(); + } }; - struct BoundingSphere { + glm::vec3 center; + float radius; - glm::vec3 center; - float radius; - - BoundingSphere() = default; - BoundingSphere(const BoundingSphere& other) = default; - BoundingSphere(const glm::vec3& center, float radius) : center(center), radius(radius) {} - BoundingSphere(std::vector points); - - bool isInsideFrustsum(const Frustsum& frustsum, glm::mat4 transformations) const; + BoundingSphere() = default; + BoundingSphere(const BoundingSphere& other) = default; + BoundingSphere(const glm::vec3& center, float radius) + : center(center), radius(radius) {} + BoundingSphere(std::vector points); + bool isInsideFrustsum(const Frustsum& frustsum, + glm::mat4 transformations) const; }; -#endif // FRUSTSUM_CPP \ No newline at end of file +#endif // FRUSTSUM_CPP \ No newline at end of file diff --git a/engine/include/group.hpp b/engine/include/group.hpp index 8450606..baaf458 100644 --- a/engine/include/group.hpp +++ b/engine/include/group.hpp @@ -17,14 +17,14 @@ class Group { std::vector subgroups; std::vector static_transformations; - std::vector rotations; std::vector translates; std::vector order; Group(); Group(std::vector models, std::vector subgroups, - std::vector static_transformations, std::vector rotations, + std::vector static_transformations, + std::vector rotations, std::vector translates, std::vector order); // Updated constructor @@ -34,8 +34,8 @@ class Group { void rotate(float angle, float x, float y, float z); - void drawGroup(bool lights, const Frustsum& frustsum, bool normals, float elapsed_time, int& nr_models); - + void drawGroup(bool lights, const Frustsum& frustsum, bool normals, + float elapsed_time, int& nr_models); }; #endif // GROUP_HPP diff --git a/engine/include/input.hpp b/engine/include/input.hpp index 2e4eeee..5451d29 100644 --- a/engine/include/input.hpp +++ b/engine/include/input.hpp @@ -1,13 +1,12 @@ #pragma once -#include - #include #include +#include struct Keyboard { - enum Key : uint16_t { - // clang-format off + enum Key : uint16_t { + // clang-format off A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, F1, F2, F3, F4, F5, F6, F7, @@ -17,68 +16,55 @@ struct Keyboard { Home, End, Insert, LShift, RShift, LCtrl, RCtrl, Alt, Mod, - // clang-format on - // Not all inputs are implemented - UnknownKey, - _Count, - }; + // clang-format on + // Not all inputs are implemented + UnknownKey, + _Count, + }; }; struct Mouse { - enum Button : uint8_t { - Left, - Middle, - Right, - // Not all inputs are implemented - UnknownButton, - _Count, - }; + enum Button : uint8_t { + Left, + Middle, + Right, + // Not all inputs are implemented + UnknownButton, + _Count, + }; }; class Input { -public: - // Keyboard event callbacks - static void on_key_down(unsigned char native_key, int, int); - static void on_key_up(unsigned char native_key, int, int); - static void on_special_key_down(int native_key, int, int); - static void on_special_key_up(int native_key, int, int); - // Mouse event callbacks - static void on_mouse_motion(int x, int y); - static void on_mouse_button(int button, int state, int, int); - // Updates input state (only mouse delta for now) - static void process_input(); + public: + // Keyboard event callbacks + static void on_key_down(unsigned char native_key, int, int); + static void on_key_up(unsigned char native_key, int, int); + static void on_special_key_down(int native_key, int, int); + static void on_special_key_up(int native_key, int, int); + // Mouse event callbacks + static void on_mouse_motion(int x, int y); + static void on_mouse_button(int button, int state, int, int); + // Updates input state (only mouse delta for now) + static void process_input(); - // Keyboard - [[nodiscard]] - static bool is_down(Keyboard::Key key); - [[nodiscard]] - static bool is_up(Keyboard::Key key); + // Keyboard + [[nodiscard]] static bool is_down(Keyboard::Key key); + [[nodiscard]] static bool is_up(Keyboard::Key key); - // Mouse - [[nodiscard]] - static bool is_down(Mouse::Button button); - [[nodiscard]] - static bool is_up(Mouse::Button button); - [[nodiscard]] - static glm::vec2 mouse_delta() { - return s_mouse_delta; - } - [[nodiscard]] - static glm::vec2 mouse_position() { - return s_mouse_position; - } + // Mouse + [[nodiscard]] static bool is_down(Mouse::Button button); + [[nodiscard]] static bool is_up(Mouse::Button button); + [[nodiscard]] static glm::vec2 mouse_delta() { return s_mouse_delta; } + [[nodiscard]] static glm::vec2 mouse_position() { return s_mouse_position; } -private: - inline static std::bitset s_keyboard_state; - inline static std::bitset s_mouse_state; - inline static glm::ivec2 s_mouse_position = glm::ivec2{}; - inline static glm::ivec2 s_mouse_delta = glm::ivec2{}; + private: + inline static std::bitset s_keyboard_state; + inline static std::bitset s_mouse_state; + inline static glm::ivec2 s_mouse_position = glm::ivec2{}; + inline static glm::ivec2 s_mouse_delta = glm::ivec2{}; -private: - [[nodiscard]] - static Keyboard::Key native_to_key(unsigned char); - [[nodiscard]] - static Keyboard::Key special_native_to_key(int); - [[nodiscard]] - static Mouse::Button native_to_button(int); + private: + [[nodiscard]] static Keyboard::Key native_to_key(unsigned char); + [[nodiscard]] static Keyboard::Key special_native_to_key(int); + [[nodiscard]] static Mouse::Button native_to_button(int); }; diff --git a/engine/include/light.hpp b/engine/include/light.hpp index 8990090..83e3b3b 100644 --- a/engine/include/light.hpp +++ b/engine/include/light.hpp @@ -31,7 +31,7 @@ bool setupLights(std::vector lights); void drawLights(std::vector lights); -struct Material{ +struct Material { glm::vec4 ambient; glm::vec4 diffuse; glm::vec4 specular; diff --git a/engine/include/menuimgui.hpp b/engine/include/menuimgui.hpp index 74e1eed..4345ab1 100644 --- a/engine/include/menuimgui.hpp +++ b/engine/include/menuimgui.hpp @@ -15,9 +15,9 @@ extern "C" { #include #include -#include "imgui.h" #include "backends/imgui_impl_glut.h" #include "backends/imgui_impl_opengl3.h" +#include "imgui.h" void setupMenu(); diff --git a/engine/include/model.hpp b/engine/include/model.hpp index 0dfee2f..0cb3cf6 100644 --- a/engine/include/model.hpp +++ b/engine/include/model.hpp @@ -17,10 +17,10 @@ extern "C" { #include #include +#include "frustsum.hpp" #include "light.hpp" #include "utils.hpp" #include "vertex.hpp" -#include "frustsum.hpp" std::vector generateVBO(const std::vector& points); diff --git a/engine/src/Camera.cpp b/engine/src/Camera.cpp index 03fd12f..09bfc17 100644 --- a/engine/src/Camera.cpp +++ b/engine/src/Camera.cpp @@ -10,11 +10,10 @@ Camera::Camera() { this->fov = 0; this->near = 0; this->far = 0; - } -Camera::Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, float near, - float far) { +Camera::Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, + float near, float far) { this->position = position; this->lookAt = lookAt; this->up = up; @@ -29,7 +28,6 @@ Camera::Camera(glm::vec3 position, glm::vec3 lookAt, glm::vec3 up, int fov, floa this->far = far; } - Camera::Camera(const Camera& other) { // Copy member variables from 'other' to the current object this->position = other.position; diff --git a/engine/src/controller.cpp b/engine/src/controller.cpp index d98124f..69bd33d 100644 --- a/engine/src/controller.cpp +++ b/engine/src/controller.cpp @@ -1,86 +1,83 @@ #include "controller.hpp" + #include "input.hpp" -glm::vec3 spherical_to_cartesian(glm::vec3 vec) { - const float p = vec.x; - const float theta = vec.y; - const float phi = vec.z; +glm::vec3 spherical_to_cartesian(glm::vec3 vec) { + const float p = vec.x; + const float theta = vec.y; + const float phi = vec.z; - return glm::vec3 { - std::sin(theta) * std::sin(phi) * p, - std::cos(phi) * p, - std::sin(phi) * p * std::cos(theta) - }; + return glm::vec3{std::sin(theta) * std::sin(phi) * p, std::cos(phi) * p, + std::sin(phi) * p * std::cos(theta)}; } -glm::vec3 cartesian_to_spherical(glm::vec3 vec) { - const float x = vec.x; - const float y = vec.y; - const float z = vec.z; +glm::vec3 cartesian_to_spherical(glm::vec3 vec) { + const float x = vec.x; + const float y = vec.y; + const float z = vec.z; - const float abs = std::sqrt(x * x + y * y + z * z); - return glm::vec3 {abs, std::atan2(x, z), std::acos(y / abs)}; + const float abs = std::sqrt(x * x + y * y + z * z); + return glm::vec3{abs, std::atan2(x, z), std::acos(y / abs)}; } void CameraController::update(float delta_time, bool& imgui) { - const glm::vec3 right = - glm::normalize(glm::cross(m_camera.forward, m_camera.up)); + const glm::vec3 right = + glm::normalize(glm::cross(m_camera.forward, m_camera.up)); - // Update speed - m_speed = Input::is_down(Keyboard::LShift) ? sprint_speed : normal_speed; + // Update speed + m_speed = Input::is_down(Keyboard::LShift) ? sprint_speed : normal_speed; - if (Input::is_down(Keyboard::F1)) { - imgui = true; - } - if(Input::is_down(Keyboard::F2)) { - imgui = false; - } - // Update camera position - if (Input::is_down(Keyboard::W)) { - m_camera.position += m_camera.forward * m_speed * delta_time; - } - if (Input::is_down(Keyboard::S)) { - m_camera.position -= m_camera.forward * m_speed * delta_time; - } - if (Input::is_down(Keyboard::D)) { - m_camera.position += right * m_speed * delta_time; - } - if (Input::is_down(Keyboard::A)) { - m_camera.position -= right * m_speed * delta_time; - } + if (Input::is_down(Keyboard::F1)) { + imgui = true; + } + if (Input::is_down(Keyboard::F2)) { + imgui = false; + } + // Update camera position + if (Input::is_down(Keyboard::W)) { + m_camera.position += m_camera.forward * m_speed * delta_time; + } + if (Input::is_down(Keyboard::S)) { + m_camera.position -= m_camera.forward * m_speed * delta_time; + } + if (Input::is_down(Keyboard::D)) { + m_camera.position += right * m_speed * delta_time; + } + if (Input::is_down(Keyboard::A)) { + m_camera.position -= right * m_speed * delta_time; + } - //Update camera direction with Keyboard - if (Input::is_down(Keyboard::Up)) { - glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); - spherical_forward.z += delta_time * m_sensitivity; - m_camera.forward = spherical_to_cartesian(spherical_forward); - } + // Update camera direction with Keyboard + if (Input::is_down(Keyboard::Up)) { + glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); + spherical_forward.z += delta_time * m_sensitivity; + m_camera.forward = spherical_to_cartesian(spherical_forward); + } - if (Input::is_down(Keyboard::Down)) { - glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); - spherical_forward.z -= delta_time * m_sensitivity; - m_camera.forward = spherical_to_cartesian(spherical_forward); - } + if (Input::is_down(Keyboard::Down)) { + glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); + spherical_forward.z -= delta_time * m_sensitivity; + m_camera.forward = spherical_to_cartesian(spherical_forward); + } - if (Input::is_down(Keyboard::Right)) { - glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); - spherical_forward.y += delta_time * m_sensitivity; - m_camera.forward = spherical_to_cartesian(spherical_forward); - } + if (Input::is_down(Keyboard::Right)) { + glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); + spherical_forward.y += delta_time * m_sensitivity; + m_camera.forward = spherical_to_cartesian(spherical_forward); + } - if (Input::is_down(Keyboard::Left)) { - glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); - spherical_forward.y -= delta_time * m_sensitivity; - m_camera.forward = spherical_to_cartesian(spherical_forward); - } + if (Input::is_down(Keyboard::Left)) { + glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); + spherical_forward.y -= delta_time * m_sensitivity; + m_camera.forward = spherical_to_cartesian(spherical_forward); + } - // Update camera direction - if (Input::is_down(Mouse::Right)) { - glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); - glm::vec2 mouse_delta = Input::mouse_delta() * delta_time * m_sensitivity; - spherical_forward.y -= mouse_delta.x; - spherical_forward.z += mouse_delta.y; - m_camera.forward = spherical_to_cartesian(spherical_forward); - } - + // Update camera direction + if (Input::is_down(Mouse::Right)) { + glm::vec3 spherical_forward = cartesian_to_spherical(m_camera.forward); + glm::vec2 mouse_delta = Input::mouse_delta() * delta_time * m_sensitivity; + spherical_forward.y -= mouse_delta.x; + spherical_forward.z += mouse_delta.y; + m_camera.forward = spherical_to_cartesian(spherical_forward); + } } \ No newline at end of file diff --git a/engine/src/curves.cpp b/engine/src/curves.cpp index 160e90c..e194c52 100644 --- a/engine/src/curves.cpp +++ b/engine/src/curves.cpp @@ -82,7 +82,8 @@ glm::mat4 TimeRotations::applyTimeRotation(float elapsed_time) { return glm::mat4(1.0f); } float angle = 360 * (elapsed_time / this->time); - return glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(this->x, this->y, this->z)); + return glm::rotate(glm::mat4(1.0f), glm::radians(angle), + glm::vec3(this->x, this->y, this->z)); } TimeTranslations::TimeTranslations() { @@ -91,7 +92,8 @@ TimeTranslations::TimeTranslations() { this->y_axis = Point(0, 1, 0); } -TimeTranslations::TimeTranslations(float time, bool align, std::vector curve) { +TimeTranslations::TimeTranslations(float time, bool align, + std::vector curve) { this->time = time; this->align = align; this->curvePoints = curve; @@ -107,7 +109,8 @@ std::pair TimeTranslations::getLocation(float elapsed_time) { return catmollRomPosition(this->curvePoints, t); } -std::array TimeTranslations::rotationMatrix(Point x, Point y, Point z) { +std::array TimeTranslations::rotationMatrix(Point x, Point y, + Point z) { return std::array{{ x.x, x.y, @@ -128,7 +131,6 @@ std::array TimeTranslations::rotationMatrix(Point x, Point y, Point z }}; } - glm::mat4 TimeTranslations::applyTimeTranslations(float elapsed_time) { if (this->time == 0) { return glm::mat4(1.0f); @@ -143,7 +145,6 @@ glm::mat4 TimeTranslations::applyTimeTranslations(float elapsed_time) { Point pos = position_dir.first; Point dir = position_dir.second; - glm::mat4 matrix = glm::mat4(1.0f); matrix = glm::translate(matrix, glm::vec3(pos.x, pos.y, pos.z)); @@ -175,25 +176,20 @@ void TimeTranslations::renderCatmullRomCurve() { glEnd(); } - -glm::mat4 Scalematrix(float x, float y, float z){ +glm::mat4 Scalematrix(float x, float y, float z) { glm::mat4 matrix = glm::mat4(1.0f); matrix = glm::scale(matrix, glm::vec3(x, y, z)); return matrix; } - -glm::mat4 Translatematrix(float x, float y, float z){ +glm::mat4 Translatematrix(float x, float y, float z) { glm::mat4 matrix = glm::mat4(1.0f); matrix = glm::translate(matrix, glm::vec3(x, y, z)); return matrix; } - -glm::mat4 Rotationmatrix(float angle, float x, float y, float z){ +glm::mat4 Rotationmatrix(float angle, float x, float y, float z) { glm::mat4 matrix = glm::mat4(1.0f); matrix = glm::rotate(matrix, glm::radians(angle), glm::vec3(x, y, z)); return matrix; } - - diff --git a/engine/src/frustsum.cpp b/engine/src/frustsum.cpp index 5093579..6ca8f5e 100644 --- a/engine/src/frustsum.cpp +++ b/engine/src/frustsum.cpp @@ -1,122 +1,125 @@ +#include "frustsum.hpp" + #include #include + #include "vertex.hpp" -#include "frustsum.hpp" Plane::Plane(const glm::vec3& normal, glm::vec3 point) { - this->normal = glm::normalize(normal); - this->point = point; - float d = -normal.x * point.x - normal.y * point.y - normal.z * point.z; + this->normal = glm::normalize(normal); + this->point = point; + float d = -normal.x * point.x - normal.y * point.y - normal.z * point.z; - this->abcd = glm::vec4(this->normal, d); + this->abcd = glm::vec4(this->normal, d); } - Frustsum::Frustsum(const Camera& cam, float ratio, bool on) { - this->on = on; - if (!on) { - return; - } - - float aspect = ratio; - glm::vec3 front = glm::normalize(cam.lookAt - cam.position); - float fovRadians = glm::radians(static_cast(cam.fov)); - - float halfHSide = cam.far * tanf(fovRadians * 0.5f); - float halfVSide = halfHSide * aspect; - - glm::vec3 frontMultFar = cam.far * front; - - // Near and far planes - glm::vec3 nearNormal = front; - glm::vec3 nearPoint = cam.position + cam.near * front; - - glm::vec3 farNormal = -front; - glm::vec3 farPoint = cam.position + frontMultFar; - - // Right and left planes - glm::vec3 rightNormal = glm::normalize(glm::cross(frontMultFar - cam.right * halfHSide, cam.up)); - glm::vec3 rightPoint = cam.position; - - glm::vec3 leftNormal = glm::normalize(glm::cross(cam.up, frontMultFar + cam.right * halfHSide)); - glm::vec3 leftPoint = cam.position; - - // Top and bottom planes - glm::vec3 topNormal = glm::normalize(glm::cross(cam.right, frontMultFar - cam.up * halfVSide)); - glm::vec3 topPoint = cam.position; - - glm::vec3 bottomNormal = glm::normalize(glm::cross(frontMultFar + cam.up * halfVSide, cam.right)); - glm::vec3 bottomPoint = cam.position; - - nearFace = { nearNormal, nearPoint }; - farFace = { farNormal, farPoint }; - rightFace = { rightNormal, rightPoint }; - leftFace = { leftNormal, leftPoint }; - topFace = { topNormal, topPoint }; - bottomFace = { bottomNormal, bottomPoint }; + this->on = on; + if (!on) { + return; + } + + float aspect = ratio; + glm::vec3 front = glm::normalize(cam.lookAt - cam.position); + float fovRadians = glm::radians(static_cast(cam.fov)); + + float halfHSide = cam.far * tanf(fovRadians * 0.5f); + float halfVSide = halfHSide * aspect; + + glm::vec3 frontMultFar = cam.far * front; + + // Near and far planes + glm::vec3 nearNormal = front; + glm::vec3 nearPoint = cam.position + cam.near * front; + + glm::vec3 farNormal = -front; + glm::vec3 farPoint = cam.position + frontMultFar; + + // Right and left planes + glm::vec3 rightNormal = + glm::normalize(glm::cross(frontMultFar - cam.right * halfHSide, cam.up)); + glm::vec3 rightPoint = cam.position; + + glm::vec3 leftNormal = + glm::normalize(glm::cross(cam.up, frontMultFar + cam.right * halfHSide)); + glm::vec3 leftPoint = cam.position; + + // Top and bottom planes + glm::vec3 topNormal = + glm::normalize(glm::cross(cam.right, frontMultFar - cam.up * halfVSide)); + glm::vec3 topPoint = cam.position; + + glm::vec3 bottomNormal = + glm::normalize(glm::cross(frontMultFar + cam.up * halfVSide, cam.right)); + glm::vec3 bottomPoint = cam.position; + + nearFace = {nearNormal, nearPoint}; + farFace = {farNormal, farPoint}; + rightFace = {rightNormal, rightPoint}; + leftFace = {leftNormal, leftPoint}; + topFace = {topNormal, topPoint}; + bottomFace = {bottomNormal, bottomPoint}; } - - BoundingSphere::BoundingSphere(std::vector points) { - // Initialize min and max points - glm::vec3 min = {INFINITY, INFINITY, INFINITY}; - glm::vec3 max = {-INFINITY, -INFINITY, -INFINITY}; - - // Sum to calculate the center - glm::vec3 sum = {0.0f, 0.0f, 0.0f}; - - for (const Vertex& point : points) { - // Update min and max - if (point.position.x < min.x) { - min.x = point.position.x; - } - if (point.position.y < min.y) { - min.y = point.position.y; - } - if (point.position.z < min.z) { - min.z = point.position.z; - } - - if (point.position.x > max.x) { - max.x = point.position.x; - } - if (point.position.y > max.y) { - max.y = point.position.y; - } - if (point.position.z > max.z) { - max.z = point.position.z; - } - - glm::vec3 pointPosition = {point.position.x, point.position.y, point.position.z}; - // Sum up the positions - sum += pointPosition; - } - - // Calculate the center as the average of all points - center = sum / static_cast(points.size()); - std::cout << "Center: " << center.x << ", " << center.y << ", " << center.z << std::endl; - - // Calculate the radius as the distance from the center to the farthest point (max) - radius = glm::distance(center, max); -} + // Initialize min and max points + glm::vec3 min = {INFINITY, INFINITY, INFINITY}; + glm::vec3 max = {-INFINITY, -INFINITY, -INFINITY}; + // Sum to calculate the center + glm::vec3 sum = {0.0f, 0.0f, 0.0f}; -bool BoundingSphere::isInsideFrustsum(const Frustsum& frustsum, glm::mat4 transformations) const { + for (const Vertex& point : points) { + // Update min and max + if (point.position.x < min.x) { + min.x = point.position.x; + } + if (point.position.y < min.y) { + min.y = point.position.y; + } + if (point.position.z < min.z) { + min.z = point.position.z; + } - if(!frustsum.on) { - return true; + if (point.position.x > max.x) { + max.x = point.position.x; + } + if (point.position.y > max.y) { + max.y = point.position.y; } + if (point.position.z > max.z) { + max.z = point.position.z; + } + + glm::vec3 pointPosition = {point.position.x, point.position.y, + point.position.z}; + // Sum up the positions + sum += pointPosition; + } - glm::vec3 center = glm::vec3(transformations * glm::vec4(this->center, 1.0f)); - float radius = this->radius * glm::length(glm::vec3(transformations[0])) / 2; + // Calculate the center as the average of all points + center = sum / static_cast(points.size()); + std::cout << "Center: " << center.x << ", " << center.y << ", " << center.z + << std::endl; - return frustsum.nearFace.distanceToPoint(center) > -radius && - frustsum.farFace.distanceToPoint(center) > -radius && - frustsum.rightFace.distanceToPoint(center) > -radius && - frustsum.leftFace.distanceToPoint(center) > -radius && - frustsum.topFace.distanceToPoint(center) > -radius && - frustsum.bottomFace.distanceToPoint(center) > -radius; + // Calculate the radius as the distance from the center to the farthest point + // (max) + radius = glm::distance(center, max); } - +bool BoundingSphere::isInsideFrustsum(const Frustsum& frustsum, + glm::mat4 transformations) const { + if (!frustsum.on) { + return true; + } + + glm::vec3 center = glm::vec3(transformations * glm::vec4(this->center, 1.0f)); + float radius = this->radius * glm::length(glm::vec3(transformations[0])) / 2; + + return frustsum.nearFace.distanceToPoint(center) > -radius && + frustsum.farFace.distanceToPoint(center) > -radius && + frustsum.rightFace.distanceToPoint(center) > -radius && + frustsum.leftFace.distanceToPoint(center) > -radius && + frustsum.topFace.distanceToPoint(center) > -radius && + frustsum.bottomFace.distanceToPoint(center) > -radius; +} diff --git a/engine/src/group.cpp b/engine/src/group.cpp index 5d40ad3..d1046da 100644 --- a/engine/src/group.cpp +++ b/engine/src/group.cpp @@ -24,12 +24,11 @@ Group::Group(std::vector models, std::vector subgroups, this->order = order; } - glm::mat4 applyTransformations(std::vector order, - std::vector static_transformations, - std::vector rotations, - std::vector translates, - float speed_factor) { + std::vector static_transformations, + std::vector rotations, + std::vector translates, + float speed_factor) { float elapsed_time = speed_factor * glutGet(GLUT_ELAPSED_TIME) / 1000.0f; int t = 0; int r = 0; @@ -58,20 +57,23 @@ glm::mat4 applyTransformations(std::vector order, return matrix; } -void Group::drawGroup(bool lights, const Frustsum& frustsum, bool normals, float speed_factor, int& nr_models) { +void Group::drawGroup(bool lights, const Frustsum& frustsum, bool normals, + float speed_factor, int& nr_models) { glPushMatrix(); - glm::mat4 matrix = applyTransformations(this->order, this->static_transformations, this->rotations, this->translates, speed_factor); + glm::mat4 matrix = + applyTransformations(this->order, this->static_transformations, + this->rotations, this->translates, speed_factor); for (Model& model : this->models) { if (lights) { setupMaterial(model.material); } - if(model.bounding_sphere.isInsideFrustsum(frustsum, matrix)) { + if (model.bounding_sphere.isInsideFrustsum(frustsum, matrix)) { nr_models++; model.drawModel(); - if(normals) model.drawNormals(); + if (normals) model.drawNormals(); } } diff --git a/engine/src/input.cpp b/engine/src/input.cpp index cabd2a0..e22dcaa 100644 --- a/engine/src/input.cpp +++ b/engine/src/input.cpp @@ -1,123 +1,121 @@ #include "input.hpp" -#include "fmt/core.h" -#include "menuimgui.hpp" + +#include #include #include #include -#include - +#include "fmt/core.h" +#include "menuimgui.hpp" void Input::on_key_down(unsigned char native_key, int x, int y) { - s_keyboard_state[native_to_key(native_key)] = true; - ImGui_ImplGLUT_KeyboardFunc(native_key, x, y); + s_keyboard_state[native_to_key(native_key)] = true; + ImGui_ImplGLUT_KeyboardFunc(native_key, x, y); } void Input::on_key_up(unsigned char native_key, int x, int y) { - s_keyboard_state[native_to_key(native_key)] = false; - ImGui_ImplGLUT_KeyboardUpFunc(native_key, x, y); + s_keyboard_state[native_to_key(native_key)] = false; + ImGui_ImplGLUT_KeyboardUpFunc(native_key, x, y); } void Input::on_special_key_down(int native_key, int x, int y) { - s_keyboard_state[special_native_to_key(native_key)] = true; - ImGui_ImplGLUT_SpecialFunc(native_key, x, y); + s_keyboard_state[special_native_to_key(native_key)] = true; + ImGui_ImplGLUT_SpecialFunc(native_key, x, y); } void Input::on_special_key_up(int native_key, int x, int y) { - s_keyboard_state[special_native_to_key(native_key)] = false; - ImGui_ImplGLUT_SpecialUpFunc(native_key, x, y); + s_keyboard_state[special_native_to_key(native_key)] = false; + ImGui_ImplGLUT_SpecialUpFunc(native_key, x, y); } void Input::on_mouse_motion(int x, int y) { - s_mouse_position = glm::ivec2{x, y}; - ImGui_ImplGLUT_MotionFunc(x, y); + s_mouse_position = glm::ivec2{x, y}; + ImGui_ImplGLUT_MotionFunc(x, y); } void Input::on_mouse_button(int native_button, int state, int x, int y) { - s_mouse_state[native_to_button(native_button)] = GLUT_DOWN == state; - ImGui_ImplGLUT_MouseFunc(native_button, state, x, y); + s_mouse_state[native_to_button(native_button)] = GLUT_DOWN == state; + ImGui_ImplGLUT_MouseFunc(native_button, state, x, y); } void Input::process_input() { - static glm::ivec2 prev_position = s_mouse_position; - s_mouse_delta = s_mouse_position - prev_position; - prev_position = s_mouse_position; + static glm::ivec2 prev_position = s_mouse_position; + s_mouse_delta = s_mouse_position - prev_position; + prev_position = s_mouse_position; } bool Input::is_down(Keyboard::Key key) { - assert(key < s_keyboard_state.size()); - return s_keyboard_state.test(key); + assert(key < s_keyboard_state.size()); + return s_keyboard_state.test(key); } bool Input::is_up(Keyboard::Key key) { - assert(key < s_keyboard_state.size()); - return !s_keyboard_state.test(key); + assert(key < s_keyboard_state.size()); + return !s_keyboard_state.test(key); } bool Input::is_down(Mouse::Button button) { - assert(button < s_mouse_state.size()); - return s_mouse_state.test(button); + assert(button < s_mouse_state.size()); + return s_mouse_state.test(button); } bool Input::is_up(Mouse::Button button) { - assert(button < s_mouse_state.size()); - return !s_mouse_state.test(button); + assert(button < s_mouse_state.size()); + return !s_mouse_state.test(button); } Keyboard::Key Input::native_to_key(unsigned char key_code) { - if (key_code >= 'A' && key_code <= 'Z') { - return Keyboard::Key(key_code - 'A'); - } - else if (key_code >= 'a' && key_code <= 'z') { - return Keyboard::Key(key_code - 'a'); - } - - return Keyboard::UnknownKey; + if (key_code >= 'A' && key_code <= 'Z') { + return Keyboard::Key(key_code - 'A'); + } else if (key_code >= 'a' && key_code <= 'z') { + return Keyboard::Key(key_code - 'a'); + } + + return Keyboard::UnknownKey; } Keyboard::Key Input::special_native_to_key(int key_code) { - static std::unordered_map special_key_mapping{ - {GLUT_KEY_F1, Keyboard::F1}, - {GLUT_KEY_F2, Keyboard::F2}, - {GLUT_KEY_F3, Keyboard::F3}, - {GLUT_KEY_F4, Keyboard::F4}, - {GLUT_KEY_F5, Keyboard::F5}, - {GLUT_KEY_F6, Keyboard::F6}, - {GLUT_KEY_F7, Keyboard::F7}, - {GLUT_KEY_F8, Keyboard::F8}, - {GLUT_KEY_F9, Keyboard::F9}, - {GLUT_KEY_F10, Keyboard::F10}, - {GLUT_KEY_F11, Keyboard::F11}, - {GLUT_KEY_F12, Keyboard::F12}, - {GLUT_KEY_LEFT, Keyboard::Left}, - {GLUT_KEY_UP, Keyboard::Up}, - {GLUT_KEY_RIGHT, Keyboard::Right}, - {GLUT_KEY_DOWN, Keyboard::Down}, - {GLUT_KEY_PAGE_UP, Keyboard::PageUp}, - {GLUT_KEY_PAGE_DOWN, Keyboard::PageDown}, - {GLUT_KEY_HOME, Keyboard::Home}, - {GLUT_KEY_END, Keyboard::End}, - {GLUT_KEY_INSERT, Keyboard::Insert}, - // GLUT undocumented special keys - {112, Keyboard::LShift}, - {113, Keyboard::RShift}, - {114, Keyboard::LCtrl}, - {115, Keyboard::RCtrl}, - {115, Keyboard::Alt}, - {118, Keyboard::Mod}, - }; - - const auto it = special_key_mapping.find(key_code); - return (it != special_key_mapping.end()) ? it->second - : Keyboard::UnknownKey; + static std::unordered_map special_key_mapping{ + {GLUT_KEY_F1, Keyboard::F1}, + {GLUT_KEY_F2, Keyboard::F2}, + {GLUT_KEY_F3, Keyboard::F3}, + {GLUT_KEY_F4, Keyboard::F4}, + {GLUT_KEY_F5, Keyboard::F5}, + {GLUT_KEY_F6, Keyboard::F6}, + {GLUT_KEY_F7, Keyboard::F7}, + {GLUT_KEY_F8, Keyboard::F8}, + {GLUT_KEY_F9, Keyboard::F9}, + {GLUT_KEY_F10, Keyboard::F10}, + {GLUT_KEY_F11, Keyboard::F11}, + {GLUT_KEY_F12, Keyboard::F12}, + {GLUT_KEY_LEFT, Keyboard::Left}, + {GLUT_KEY_UP, Keyboard::Up}, + {GLUT_KEY_RIGHT, Keyboard::Right}, + {GLUT_KEY_DOWN, Keyboard::Down}, + {GLUT_KEY_PAGE_UP, Keyboard::PageUp}, + {GLUT_KEY_PAGE_DOWN, Keyboard::PageDown}, + {GLUT_KEY_HOME, Keyboard::Home}, + {GLUT_KEY_END, Keyboard::End}, + {GLUT_KEY_INSERT, Keyboard::Insert}, + // GLUT undocumented special keys + {112, Keyboard::LShift}, + {113, Keyboard::RShift}, + {114, Keyboard::LCtrl}, + {115, Keyboard::RCtrl}, + {115, Keyboard::Alt}, + {118, Keyboard::Mod}, + }; + + const auto it = special_key_mapping.find(key_code); + return (it != special_key_mapping.end()) ? it->second : Keyboard::UnknownKey; } Mouse::Button Input::native_to_button(int button_code) { - // Shouldn't be possible to emmit another button_code value. - if (button_code >= 0 && button_code < 3) { - return Mouse::Button(button_code); - } + // Shouldn't be possible to emmit another button_code value. + if (button_code >= 0 && button_code < 3) { + return Mouse::Button(button_code); + } - return Mouse::UnknownButton; + return Mouse::UnknownButton; } diff --git a/engine/src/main.cpp b/engine/src/main.cpp index fecfae8..885489e 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -11,15 +11,16 @@ extern "C" { #define _USE_MATH_DEFINES #include + #include #include #include "Configuration.hpp" +#include "controller.hpp" #include "curves.hpp" +#include "input.hpp" #include "menuimgui.hpp" #include "parse.hpp" -#include "input.hpp" -#include "controller.hpp" std::string filename; bool simple = false; @@ -49,7 +50,6 @@ struct ModelInfo { std::unordered_map models_info; - void reshape(int w, int h) { float aspect_ratio = (float)w / (float)h; @@ -99,8 +99,8 @@ void setupConfig(char* arg) { if (filename.substr(filename.size() - 4) == ".xml") { c = parseConfig(filename); } else if (filename.substr(filename.size() - 3) == ".3d") { - c = parseConfig3D(filename); - } else if (filename.substr(filename.size() - 4) == ".obj") { + c = parseConfig3D(filename); + } else if (filename.substr(filename.size() - 4) == ".obj") { c = parseConfigObj(filename); } else { std::cout << "Invalid file format\n"; @@ -108,7 +108,6 @@ void setupConfig(char* arg) { } camera = c.camera; - } void setupModels(Group& group) { @@ -125,7 +124,7 @@ void setupModels(Group& group) { } void hotReload() { - setupConfig((char*) filename.c_str()); + setupConfig((char*)filename.c_str()); setupModels(c.group); lighting = setupLights(c.lights); } @@ -136,61 +135,65 @@ void resetCamera() { } void renderMenu() { - ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGLUT_NewFrame(); - ImGui::NewFrame(); - - ImGuiIO& io = ImGui::GetIO(); - { - ImGui::Begin("Infos", NULL, ImGuiWindowFlags_AlwaysAutoResize); - - ImGui::Text("FPS: %.1f (%.3f ms/frame)", io.Framerate, 1000.f / io.Framerate); - ImGui::Text("Camera Position: (%.3f, %.3f, %.3f)", camera.position.x, camera.position.y, camera.position.z); - ImGui::Text("Fov: %d Ratio: %.1f Near: %.3f Far: %.3f", c.camera.fov, static_cast(glutGet(GLUT_WINDOW_WIDTH) / glutGet(GLUT_WINDOW_HEIGHT)), c.camera.near, c.camera.far); - ImGui::Text("XML File: %s", filename.c_str()); - ImGui::Text("Models: %d (Total %d)", nr_models, total_models); - ImGui::Checkbox("Models Info", &models_menus); - ImGui::End(); - + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGLUT_NewFrame(); + ImGui::NewFrame(); + + ImGuiIO& io = ImGui::GetIO(); + { + ImGui::Begin("Infos", NULL, ImGuiWindowFlags_AlwaysAutoResize); + + ImGui::Text("FPS: %.1f (%.3f ms/frame)", io.Framerate, + 1000.f / io.Framerate); + ImGui::Text("Camera Position: (%.3f, %.3f, %.3f)", camera.position.x, + camera.position.y, camera.position.z); + ImGui::Text("Fov: %d Ratio: %.1f Near: %.3f Far: %.3f", c.camera.fov, + static_cast(glutGet(GLUT_WINDOW_WIDTH) / + glutGet(GLUT_WINDOW_HEIGHT)), + c.camera.near, c.camera.far); + ImGui::Text("XML File: %s", filename.c_str()); + ImGui::Text("Models: %d (Total %d)", nr_models, total_models); + ImGui::Checkbox("Models Info", &models_menus); + ImGui::End(); + } + { + ImGui::Begin("Options", NULL, ImGuiWindowFlags_AlwaysAutoResize); + + ImGui::Checkbox("Axis", &axis); + ImGui::SameLine(); + ImGui::Checkbox("Wireframe", &wireframe); + ImGui::SameLine(); + ImGui::Checkbox("Frustsum", &culling); + ImGui::Checkbox("Normals", &normals); + ImGui::SameLine(); + ImGui::Checkbox("Lighting", &lighting); + + ImGui::SliderFloat("Speed Factor", &speed_factor, 0.0f, 2.0f); + ImGui::Button("Reset", ImVec2(50, 20)); + if (ImGui::IsItemClicked()) { + resetCamera(); } - { - ImGui::Begin("Options", NULL, ImGuiWindowFlags_AlwaysAutoResize); - - ImGui::Checkbox("Axis", &axis); - ImGui::SameLine(); - ImGui::Checkbox("Wireframe", &wireframe); - ImGui::SameLine(); - ImGui::Checkbox("Frustsum", &culling); - ImGui::Checkbox("Normals", &normals); - ImGui::SameLine(); - ImGui::Checkbox("Lighting", &lighting); - - - ImGui::SliderFloat("Speed Factor", &speed_factor, 0.0f, 2.0f); - ImGui::Button("Reset", ImVec2(50, 20)); - if (ImGui::IsItemClicked()) { - resetCamera(); - } - ImGui::SameLine(); - ImGui::Button("Hot Reload", ImVec2(100, 20)); - if (ImGui::IsItemClicked()) { - hotReload(); - } - ImGui::End(); + ImGui::SameLine(); + ImGui::Button("Hot Reload", ImVec2(100, 20)); + if (ImGui::IsItemClicked()) { + hotReload(); } - if(models_menus){ - ImGui::Begin("Models", NULL, ImGuiWindowFlags_AlwaysAutoResize); - for (auto& model : models_info) { - ImGui::Text("Model: %s", model.first.c_str()); - ImGui::Text("Vertices: %d Triangles: %d", model.second.vertex_number, model.second.triangle_number); - //separator here - ImGui::Separator(); - } - ImGui::End(); + ImGui::End(); + } + if (models_menus) { + ImGui::Begin("Models", NULL, ImGuiWindowFlags_AlwaysAutoResize); + for (auto& model : models_info) { + ImGui::Text("Model: %s", model.first.c_str()); + ImGui::Text("Vertices: %d Triangles: %d", model.second.vertex_number, + model.second.triangle_number); + // separator here + ImGui::Separator(); } - ImGui::Render(); - glViewport(0, 0, (GLsizei) io.DisplaySize.x, (GLsizei) io.DisplaySize.y); - ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + ImGui::End(); + } + ImGui::Render(); + glViewport(0, 0, (GLsizei)io.DisplaySize.x, (GLsizei)io.DisplaySize.y); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } void fillMode() { @@ -202,13 +205,13 @@ void fillMode() { } inline float compute_delta_time() { - static auto begin = std::chrono::high_resolution_clock::now(); - const auto end = std::chrono::high_resolution_clock::now(); - const auto delta_millis = - std::chrono::duration_cast(end - begin) - .count(); - begin = end; - return delta_millis / 1000.f; + static auto begin = std::chrono::high_resolution_clock::now(); + const auto end = std::chrono::high_resolution_clock::now(); + const auto delta_millis = + std::chrono::duration_cast(end - begin) + .count(); + begin = end; + return delta_millis / 1000.f; } void update() { @@ -225,11 +228,12 @@ void renderScene(void) { // set camera glLoadIdentity(); glm::vec3 lookat = camera.position + camera.forward; - gluLookAt(camera.position.x, camera.position.y, camera.position.z, - lookat.x , lookat.y, lookat.z, - camera.up.x, camera.up.y, camera.up.z); + gluLookAt(camera.position.x, camera.position.y, camera.position.z, lookat.x, + lookat.y, lookat.z, camera.up.x, camera.up.y, camera.up.z); - Frustsum frustsum = Frustsum(camera, glutGet(GLUT_WINDOW_WIDTH) / glutGet(GLUT_WINDOW_HEIGHT), culling); + Frustsum frustsum = + Frustsum(camera, glutGet(GLUT_WINDOW_WIDTH) / glutGet(GLUT_WINDOW_HEIGHT), + culling); fillMode(); drawAxis(); @@ -242,14 +246,13 @@ void renderScene(void) { c.group.drawGroup(lighting, frustsum, normals, speed_factor, nr_models); // Start the Dear ImGui frame - if(imgui) { + if (imgui) { renderMenu(); } // End of frame glutSwapBuffers(); glutPostRedisplay(); - } void mode(int agrc, char** agrv) { @@ -258,10 +261,8 @@ void mode(int agrc, char** agrv) { simple = true; } } - } - int main(int argc, char** argv) { if (argc == 1) { std::cout << "Invalid Arguments\n"; @@ -309,6 +310,5 @@ int main(int argc, char** argv) { glutMainLoop(); shutDownMenu(); - return 1; } \ No newline at end of file diff --git a/engine/src/menuimgui.cpp b/engine/src/menuimgui.cpp index a09787d..c1e6ecf 100644 --- a/engine/src/menuimgui.cpp +++ b/engine/src/menuimgui.cpp @@ -1,27 +1,21 @@ #include "menuimgui.hpp" -void setupMenu() -{ - // Setup Dear ImGui context - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); +void setupMenu() { + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); - // Setup Dear ImGui style - ImGui::StyleColorsDark(); + // Setup Dear ImGui style + ImGui::StyleColorsDark(); - // Setup Platform/Renderer backends - ImGui_ImplGLUT_Init(); - ImGui_ImplOpenGL3_Init(); - ImGui_ImplGLUT_InstallFuncs(); + // Setup Platform/Renderer backends + ImGui_ImplGLUT_Init(); + ImGui_ImplOpenGL3_Init(); + ImGui_ImplGLUT_InstallFuncs(); } -void shutDownMenu() -{ - - ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGLUT_Shutdown(); - ImGui::DestroyContext(); +void shutDownMenu() { + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGLUT_Shutdown(); + ImGui::DestroyContext(); } - - - diff --git a/engine/src/model.cpp b/engine/src/model.cpp index 1db4adc..7017458 100644 --- a/engine/src/model.cpp +++ b/engine/src/model.cpp @@ -11,6 +11,7 @@ extern "C" { #define STB_IMAGE_IMPLEMENTATION #define _USE_MATH_DEFINES #include + #include #include "model.hpp" @@ -132,8 +133,9 @@ bool Model::loadTexture() { // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - // Para provar que a textura do teapot ser diferente se deve ao Mipmap troca se os comandos abaixo pelos comentados - // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + // Para provar que a textura do teapot ser diferente se deve ao Mipmap troca + // se os comandos abaixo pelos comentados glTexParameteri(GL_TEXTURE_2D, + // GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -204,18 +206,16 @@ void Model::drawModel() { } void Model::drawNormals() { - glDisable(GL_LIGHTING); glColor3f(1.0, 0.0, 0.0); for (const Vertex& point : this->vbo) { glBegin(GL_LINES); glVertex3f(point.position.x, point.position.y, point.position.z); - glVertex3f(point.position.x + point.normal.x, point.position.y + point.normal.y, + glVertex3f(point.position.x + point.normal.x, + point.position.y + point.normal.y, point.position.z + point.normal.z); glEnd(); } glEnable(GL_LIGHTING); - - } std::vector Model::getPoints() { return this->_points; } diff --git a/engine/src/parse.cpp b/engine/src/parse.cpp index 90a14bb..84ce682 100644 --- a/engine/src/parse.cpp +++ b/engine/src/parse.cpp @@ -66,19 +66,21 @@ Configuration parseConfig(std::string filename) { rapidxml::xml_node<>* camera = root->first_node("camera"); rapidxml::xml_node<>* position_n = camera->first_node("position"); - glm::vec3 position = glm::vec3(std::stof(position_n->first_attribute("x")->value()), - std::stof(position_n->first_attribute("y")->value()), - std::stof(position_n->first_attribute("z")->value())); + glm::vec3 position = + glm::vec3(std::stof(position_n->first_attribute("x")->value()), + std::stof(position_n->first_attribute("y")->value()), + std::stof(position_n->first_attribute("z")->value())); rapidxml::xml_node<>* lookAt_n = camera->first_node("lookAt"); - glm::vec3 lookAt = glm::vec3(std::stof(lookAt_n->first_attribute("x")->value()), - std::stof(lookAt_n->first_attribute("y")->value()), - std::stof(lookAt_n->first_attribute("z")->value())); + glm::vec3 lookAt = + glm::vec3(std::stof(lookAt_n->first_attribute("x")->value()), + std::stof(lookAt_n->first_attribute("y")->value()), + std::stof(lookAt_n->first_attribute("z")->value())); rapidxml::xml_node<>* up_n = camera->first_node("up"); glm::vec3 up = glm::vec3(std::stof(up_n->first_attribute("x")->value()), - std::stof(up_n->first_attribute("y")->value()), - std::stof(up_n->first_attribute("z")->value())); + std::stof(up_n->first_attribute("y")->value()), + std::stof(up_n->first_attribute("z")->value())); rapidxml::xml_node<>* projection = camera->first_node("projection"); int fov = std::stoi(projection->first_attribute("fov")->value()); @@ -178,7 +180,8 @@ void parseTransform(rapidxml::xml_node<>* transformNode, Group& group) { float z = std::stof(node->first_attribute("z")->value()); glm::mat4 matrix = Scalematrix(x, y, z); if (previous_static) { - group.static_transformations[last_static_index] = group.static_transformations[last_static_index] * matrix; + group.static_transformations[last_static_index] = + group.static_transformations[last_static_index] * matrix; } else { group.order.push_back(STATIC); group.static_transformations.push_back(matrix); @@ -192,7 +195,8 @@ void parseTransform(rapidxml::xml_node<>* transformNode, Group& group) { float x = std::stof(node->first_attribute("x")->value()); float y = std::stof(node->first_attribute("y")->value()); float z = std::stof(node->first_attribute("z")->value()); - TimeRotations r = TimeRotations(float(time), float(x), float(y), float(z)); + TimeRotations r = + TimeRotations(float(time), float(x), float(y), float(z)); group.rotations.push_back(r); group.order.push_back(TIMEROTATION); previous_static = false; @@ -204,7 +208,8 @@ void parseTransform(rapidxml::xml_node<>* transformNode, Group& group) { float z = std::stof(node->first_attribute("z")->value()); glm::mat4 matrix = Rotationmatrix(angle, x, y, z); if (previous_static) { - group.static_transformations[last_static_index] = group.static_transformations[last_static_index] * matrix; + group.static_transformations[last_static_index] = + group.static_transformations[last_static_index] * matrix; } else { group.order.push_back(STATIC); group.static_transformations.push_back(matrix); @@ -242,7 +247,8 @@ void parseTransform(rapidxml::xml_node<>* transformNode, Group& group) { float z = std::stof(node->first_attribute("z")->value()); glm::mat4 matrix = Translatematrix(x, y, z); if (previous_static) { - group.static_transformations[last_static_index] = group.static_transformations[last_static_index] * matrix; + group.static_transformations[last_static_index] = + group.static_transformations[last_static_index] * matrix; } else { group.order.push_back(STATIC); group.static_transformations.push_back(matrix); diff --git a/engine/src/read.cpp b/engine/src/read.cpp index dfa74f3..98ee823 100644 --- a/engine/src/read.cpp +++ b/engine/src/read.cpp @@ -65,8 +65,8 @@ Model readOBJfile(const char* filepath) { nx = normals[normal_index].x; ny = normals[normal_index].y; nz = normals[normal_index].z; - tx = 0; - ty = 1; + tx = textures[texture_index].x; + ty = textures[texture_index].y; if (texture_index == -1) { printf("Texture index is -1\n"); @@ -92,7 +92,6 @@ Model readOBJfile(const char* filepath) { // Assuming hash_models is a global or member variable hash_models[filepath] = model; - return model; } @@ -171,6 +170,5 @@ Model readFile(const char* filepath) { } else if (std::filesystem::path(filepath).extension() == ".obj") { return readOBJfile(filepath); } - return Model(); } diff --git a/generator/src/shapes/cone.cpp b/generator/src/shapes/cone.cpp index 654768d..a8726fd 100644 --- a/generator/src/shapes/cone.cpp +++ b/generator/src/shapes/cone.cpp @@ -20,7 +20,6 @@ coneTriangles(const float radius, const float height, const size_t slices, float rAux = (float)radius / stacks; // diferença do valor do raio entre stacks - // base for (float a = 0; a < (2 * M_PI); a += sliceAngle) { // divide a base pelo numero de slices @@ -109,18 +108,17 @@ coneTriangles(const float radius, const float height, const size_t slices, } count++; - if (i == stacks - 1) { // se for a ultima stack, calcula a normal para - // os pontos em comum com o topo - Point normalTop; - normalTop.x = (sin(a) + sin(a + sliceAngle)) / 2; - normalTop.y = 0; - normalTop.z = (cos(a) + cos(a + sliceAngle)) / 2; - normalTop = normalTop.normalize(); - sliceNormals[count - 2][0] = normalTop.x; - sliceNormals[count - 2][1] = normalTop.y; - sliceNormals[count - 2][2] = normalTop.z; - - } + if (i == stacks - 1) { // se for a ultima stack, calcula a normal para + // os pontos em comum com o topo + Point normalTop; + normalTop.x = (sin(a) + sin(a + sliceAngle)) / 2; + normalTop.y = 0; + normalTop.z = (cos(a) + cos(a + sliceAngle)) / 2; + normalTop = normalTop.normalize(); + sliceNormals[count - 2][0] = normalTop.x; + sliceNormals[count - 2][1] = normalTop.y; + sliceNormals[count - 2][2] = normalTop.z; + } float x4 = rBaixo * sin(a + sliceAngle); float y4 = yBaixo; @@ -154,19 +152,17 @@ coneTriangles(const float radius, const float height, const size_t slices, normal6.y = sliceNormals[count - 1][1]; normal6.z = sliceNormals[count - 1][2]; - if(i == stacks - 1){ - Point normalTop; - normalTop.x = (float)((sin(a) + sin(a + sliceAngle)) / 2.0); - normalTop.y = 0; - normalTop.z = (float)((cos(a) + cos(a + sliceAngle)) / 2.0); - normalTop = normalTop.normalize(); + if (i == stacks - 1) { + Point normalTop; + normalTop.x = (float)((sin(a) + sin(a + sliceAngle)) / 2.0); + normalTop.y = 0; + normalTop.z = (float)((cos(a) + cos(a + sliceAngle)) / 2.0); + normalTop = normalTop.normalize(); - normal6.x = normalTop.x; - normal6.y = normalTop.y; - normal6.z = normalTop.z; - } - - + normal6.x = normalTop.x; + normal6.y = normalTop.y; + normal6.z = normalTop.z; + } points.push_back(Point(x1, y1, z1)); points.push_back(Point(x2, y2, z2)); diff --git a/generator/src/shapes/cube.cpp b/generator/src/shapes/cube.cpp index cf32696..a2e8ed9 100644 --- a/generator/src/shapes/cube.cpp +++ b/generator/src/shapes/cube.cpp @@ -5,154 +5,190 @@ #include "save3d.hpp" #include "utils.hpp" -std::pair, std::vector>, std::vector> cubeAllPoints(float length, int divisions) { - float halfSize = length / 2.0f; - float step = length / divisions; - - std::vector points; - std::vector normals; - std::vector textures; - - for (int i = 0; i < divisions; ++i) { - for (int j = 0; j < divisions; ++j) { - float v1 = -halfSize + i * step; - float u1 = -halfSize + j * step; - float v2 = v1 + step; - float u2 = u1 + step; - - // Front Face - points.push_back(Point(v1, u1, halfSize)); - points.push_back(Point(v2, u1, halfSize)); - points.push_back(Point(v1, u2, halfSize)); - - points.push_back(Point(v1, u2, halfSize)); - points.push_back(Point(v2, u1, halfSize)); - points.push_back(Point(v2, u2, halfSize)); - - Point n1 = Point(0.0f, 0.0f, 1.0f); - for (int k = 0; k < 6; k++) { - normals.push_back(n1); - } - - textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); - - // Back Face - points.push_back(Point(v1, u1, -halfSize)); - points.push_back(Point(v1, u2, -halfSize)); - points.push_back(Point(v2, u1, -halfSize)); - - points.push_back(Point(v1, u2, -halfSize)); - points.push_back(Point(v2, u2, -halfSize)); - points.push_back(Point(v2, u1, -halfSize)); - - Point n2 = Point(0.0f, 0.0f, -1.0f); - for (int k = 0; k < 6; k++) { - normals.push_back(n2); - } - - textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - - // Left Face - points.push_back(Point(-halfSize, v1, u1)); - points.push_back(Point(-halfSize, v1, u2)); - points.push_back(Point(-halfSize, v2, u1)); - - points.push_back(Point(-halfSize, v1, u2)); - points.push_back(Point(-halfSize, v2, u2)); - points.push_back(Point(-halfSize, v2, u1)); - - Point n3 = Point(-1.0f, 0.0f, 0.0f); - for (int k = 0; k < 6; k++) { - normals.push_back(n3); - } - - textures.push_back(Point2D((i + 0.0f) / divisions, (j + 0.0f) / divisions)); - textures.push_back(Point2D((i + 1.0f) / divisions, (j + 0.0f) / divisions)); - textures.push_back(Point2D((i + 0.0f) / divisions, (j + 1.0f) / divisions)); - textures.push_back(Point2D((i + 1.0f) / divisions, (j + 0.0f) / divisions)); - textures.push_back(Point2D((i + 1.0f) / divisions, (j + 1.0f) / divisions)); - textures.push_back(Point2D((i + 0.0f) / divisions, (j + 1.0f) / divisions)); - - - // Right Face - points.push_back(Point(halfSize, v1, u1)); - points.push_back(Point(halfSize, v2, u1)); - points.push_back(Point(halfSize, v1, u2)); - - points.push_back(Point(halfSize, v1, u2)); - points.push_back(Point(halfSize, v2, u1)); - points.push_back(Point(halfSize, v2, u2)); - - Point n4 = Point(1.0f, 0.0f, 0.0f); - for (int k = 0; k < 6; k++) { - normals.push_back(n4); - } - - textures.push_back(Point2D((u1 + halfSize) / length, (v1 + halfSize) / length)); - textures.push_back(Point2D((u2 + halfSize) / length, (v1 + halfSize) / length)); - textures.push_back(Point2D((u1 + halfSize) / length, (v2 + halfSize) / length)); - textures.push_back(Point2D((u1 + halfSize) / length, (v2 + halfSize) / length)); - textures.push_back(Point2D((u2 + halfSize) / length, (v1 + halfSize) / length)); - textures.push_back(Point2D((u2 + halfSize) / length, (v2 + halfSize) / length)); - - // Top Face - points.push_back(Point(v1, halfSize, u1)); - points.push_back(Point(v1, halfSize, u2)); - points.push_back(Point(v2, halfSize, u1)); - - points.push_back(Point(v1, halfSize, u2)); - points.push_back(Point(v2, halfSize, u2)); - points.push_back(Point(v2, halfSize, u1)); - - Point n5 = Point(0.0f, 1.0f, 0.0f); - for (int k = 0; k < 6; k++) { - normals.push_back(n5); - } - - textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - - // Bottom Face - points.push_back(Point(v1, -halfSize, u1)); - points.push_back(Point(v2, -halfSize, u1)); - points.push_back(Point(v1, -halfSize, u2)); - - points.push_back(Point(v1, -halfSize, u2)); - points.push_back(Point(v2, -halfSize, u1)); - points.push_back(Point(v2, -halfSize, u2)); - - Point n6 = Point(0.0f, -1.0f, 0.0f); - for (int k = 0; k < 6; k++) { - normals.push_back(n6); - } - - textures.push_back(Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); - textures.push_back(Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); - } +std::pair, std::vector>, + std::vector> +cubeAllPoints(float length, int divisions) { + float halfSize = length / 2.0f; + float step = length / divisions; + + std::vector points; + std::vector normals; + std::vector textures; + + for (int i = 0; i < divisions; ++i) { + for (int j = 0; j < divisions; ++j) { + float v1 = -halfSize + i * step; + float u1 = -halfSize + j * step; + float v2 = v1 + step; + float u2 = u1 + step; + + // Front Face + points.push_back(Point(v1, u1, halfSize)); + points.push_back(Point(v2, u1, halfSize)); + points.push_back(Point(v1, u2, halfSize)); + + points.push_back(Point(v1, u2, halfSize)); + points.push_back(Point(v2, u1, halfSize)); + points.push_back(Point(v2, u2, halfSize)); + + Point n1 = Point(0.0f, 0.0f, 1.0f); + for (int k = 0; k < 6; k++) { + normals.push_back(n1); + } + + textures.push_back( + Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); + + // Back Face + points.push_back(Point(v1, u1, -halfSize)); + points.push_back(Point(v1, u2, -halfSize)); + points.push_back(Point(v2, u1, -halfSize)); + + points.push_back(Point(v1, u2, -halfSize)); + points.push_back(Point(v2, u2, -halfSize)); + points.push_back(Point(v2, u1, -halfSize)); + + Point n2 = Point(0.0f, 0.0f, -1.0f); + for (int k = 0; k < 6; k++) { + normals.push_back(n2); + } + + textures.push_back( + Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + + // Left Face + points.push_back(Point(-halfSize, v1, u1)); + points.push_back(Point(-halfSize, v1, u2)); + points.push_back(Point(-halfSize, v2, u1)); + + points.push_back(Point(-halfSize, v1, u2)); + points.push_back(Point(-halfSize, v2, u2)); + points.push_back(Point(-halfSize, v2, u1)); + + Point n3 = Point(-1.0f, 0.0f, 0.0f); + for (int k = 0; k < 6; k++) { + normals.push_back(n3); + } + + textures.push_back( + Point2D((i + 0.0f) / divisions, (j + 0.0f) / divisions)); + textures.push_back( + Point2D((i + 1.0f) / divisions, (j + 0.0f) / divisions)); + textures.push_back( + Point2D((i + 0.0f) / divisions, (j + 1.0f) / divisions)); + textures.push_back( + Point2D((i + 1.0f) / divisions, (j + 0.0f) / divisions)); + textures.push_back( + Point2D((i + 1.0f) / divisions, (j + 1.0f) / divisions)); + textures.push_back( + Point2D((i + 0.0f) / divisions, (j + 1.0f) / divisions)); + + // Right Face + points.push_back(Point(halfSize, v1, u1)); + points.push_back(Point(halfSize, v2, u1)); + points.push_back(Point(halfSize, v1, u2)); + + points.push_back(Point(halfSize, v1, u2)); + points.push_back(Point(halfSize, v2, u1)); + points.push_back(Point(halfSize, v2, u2)); + + Point n4 = Point(1.0f, 0.0f, 0.0f); + for (int k = 0; k < 6; k++) { + normals.push_back(n4); + } + + textures.push_back( + Point2D((u1 + halfSize) / length, (v1 + halfSize) / length)); + textures.push_back( + Point2D((u2 + halfSize) / length, (v1 + halfSize) / length)); + textures.push_back( + Point2D((u1 + halfSize) / length, (v2 + halfSize) / length)); + textures.push_back( + Point2D((u1 + halfSize) / length, (v2 + halfSize) / length)); + textures.push_back( + Point2D((u2 + halfSize) / length, (v1 + halfSize) / length)); + textures.push_back( + Point2D((u2 + halfSize) / length, (v2 + halfSize) / length)); + + // Top Face + points.push_back(Point(v1, halfSize, u1)); + points.push_back(Point(v1, halfSize, u2)); + points.push_back(Point(v2, halfSize, u1)); + + points.push_back(Point(v1, halfSize, u2)); + points.push_back(Point(v2, halfSize, u2)); + points.push_back(Point(v2, halfSize, u1)); + + Point n5 = Point(0.0f, 1.0f, 0.0f); + for (int k = 0; k < 6; k++) { + normals.push_back(n5); + } + + textures.push_back( + Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + + // Bottom Face + points.push_back(Point(v1, -halfSize, u1)); + points.push_back(Point(v2, -halfSize, u1)); + points.push_back(Point(v1, -halfSize, u2)); + + points.push_back(Point(v1, -halfSize, u2)); + points.push_back(Point(v2, -halfSize, u1)); + points.push_back(Point(v2, -halfSize, u2)); + + Point n6 = Point(0.0f, -1.0f, 0.0f); + for (int k = 0; k < 6; k++) { + normals.push_back(n6); + } + + textures.push_back( + Point2D((v1 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v1 + halfSize) / length, (u2 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u1 + halfSize) / length)); + textures.push_back( + Point2D((v2 + halfSize) / length, (u2 + halfSize) / length)); } + } - return std::pair(std::pair(points, normals), textures); + return std::pair(std::pair(points, normals), textures); } - bool generateCube(float length, int divisions, const char* filepath, bool advanced) { std::pair, std::vector>, diff --git a/generator/src/shapes/cylinder.cpp b/generator/src/shapes/cylinder.cpp index f1a4752..5312829 100644 --- a/generator/src/shapes/cylinder.cpp +++ b/generator/src/shapes/cylinder.cpp @@ -19,115 +19,116 @@ calculateCylinder(const float radius, const float height, const int slices) { Point n; Point2D t; + float delta = 2.0f * M_PI / slices; - float delta = 2.0f * M_PI / slices; - - for (int i = 0; i < slices; ++i) { - // topo - // ponto central - n = {0.0f, 1.0f, 0.0f}; - v = {0.0f, height / 2.0f, 0.0f}; - t = {0.4375f, 0.1875f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - v = {radius * sin(i * delta), height / 2.0f, radius * cos(i * delta)}; - t = {0.4375f + 0.1875 * sin(i * delta), 0.1875f + 0.1875 * cos(i * delta)}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - v = {radius * sin((i + 1) * delta), height / 2.0f, radius * cos((i + 1) * delta)}; - t = {0.4375f + 0.1875 * sin((i + 1) * delta), 0.1875f + 0.1875 * cos((i + 1) * delta)}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - - // corpo - - n = {sin(i + 1 * delta), 0.0f, cos(i + 1 * delta)}; - v = {radius * sin((i + 1) * delta), height / 2.0f, radius * cos((i + 1) * delta)}; - t = {(i + 1) / static_cast(slices), 1.0f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {sin(i * delta), 0.0f, cos(i * delta)}; - v = {radius * sin(i * delta), height / 2.0f, radius * cos(i * delta)}; - t = {i / static_cast(slices), 1.0f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {sin(i * delta), 0.0f, cos(i * delta)}; - v = {radius * sin(i * delta), -height / 2.0f, radius * cos(i * delta)}; - t = {i / static_cast(slices), 0.375f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {sin(i + 1 * delta), 0.0f, cos(i + 1 * delta)}; - v = {radius * sin((i + 1) * delta), -height / 2.0f, radius * cos((i + 1) * delta)}; - t = {(i + 1) / static_cast(slices), 0.375f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {sin(i + 1 * delta), 0.0f, cos(i + 1 * delta)}; - v = {radius * sin((i + 1) * delta), height / 2.0f, radius * cos((i + 1) * delta)}; - t = {(i + 1) / static_cast(slices), 1.0f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {sin(i * delta), 0.0f, cos(i * delta)}; - v = {radius * sin(i * delta), -height / 2.0f, radius * cos(i * delta)}; - t = {i / static_cast(slices), 0.375f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - - // base - - n = {0.0f, -1.0f, 0.0f}; - v = {0.0f, -height / 2.0f, 0.0f}; - t = {0.8125f, 0.1875f}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {0.0f, -1.0f, 0.0f}; - v = {radius * sin((i + 1) * delta), -height / 2.0f, radius * cos((i + 1) * delta)}; - t = {0.8125f + 0.1875 * sin((i + 1) * delta), 0.1875f + 0.1875 * cos((i + 1) * delta)}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - n = {0.0f, -1.0f, 0.0f}; - v = {radius * sin(i * delta), -height / 2.0f, radius * cos(i * delta)}; - t = {0.8125f + 0.1875 * sin(i * delta), 0.1875f + 0.1875 * cos(i * delta)}; - - points.push_back(v); - normals.push_back(n); - textures.push_back(t); - - - - } + for (int i = 0; i < slices; ++i) { + // topo + // ponto central + n = {0.0f, 1.0f, 0.0f}; + v = {0.0f, height / 2.0f, 0.0f}; + t = {0.4375f, 0.1875f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + v = {radius * sin(i * delta), height / 2.0f, radius * cos(i * delta)}; + t = {0.4375f + 0.1875 * sin(i * delta), 0.1875f + 0.1875 * cos(i * delta)}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + v = {radius * sin((i + 1) * delta), height / 2.0f, + radius * cos((i + 1) * delta)}; + t = {0.4375f + 0.1875 * sin((i + 1) * delta), + 0.1875f + 0.1875 * cos((i + 1) * delta)}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + // corpo + + n = {sin(i + 1 * delta), 0.0f, cos(i + 1 * delta)}; + v = {radius * sin((i + 1) * delta), height / 2.0f, + radius * cos((i + 1) * delta)}; + t = {(i + 1) / static_cast(slices), 1.0f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {sin(i * delta), 0.0f, cos(i * delta)}; + v = {radius * sin(i * delta), height / 2.0f, radius * cos(i * delta)}; + t = {i / static_cast(slices), 1.0f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {sin(i * delta), 0.0f, cos(i * delta)}; + v = {radius * sin(i * delta), -height / 2.0f, radius * cos(i * delta)}; + t = {i / static_cast(slices), 0.375f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {sin(i + 1 * delta), 0.0f, cos(i + 1 * delta)}; + v = {radius * sin((i + 1) * delta), -height / 2.0f, + radius * cos((i + 1) * delta)}; + t = {(i + 1) / static_cast(slices), 0.375f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {sin(i + 1 * delta), 0.0f, cos(i + 1 * delta)}; + v = {radius * sin((i + 1) * delta), height / 2.0f, + radius * cos((i + 1) * delta)}; + t = {(i + 1) / static_cast(slices), 1.0f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {sin(i * delta), 0.0f, cos(i * delta)}; + v = {radius * sin(i * delta), -height / 2.0f, radius * cos(i * delta)}; + t = {i / static_cast(slices), 0.375f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + // base + + n = {0.0f, -1.0f, 0.0f}; + v = {0.0f, -height / 2.0f, 0.0f}; + t = {0.8125f, 0.1875f}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {0.0f, -1.0f, 0.0f}; + v = {radius * sin((i + 1) * delta), -height / 2.0f, + radius * cos((i + 1) * delta)}; + t = {0.8125f + 0.1875 * sin((i + 1) * delta), + 0.1875f + 0.1875 * cos((i + 1) * delta)}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + + n = {0.0f, -1.0f, 0.0f}; + v = {radius * sin(i * delta), -height / 2.0f, radius * cos(i * delta)}; + t = {0.8125f + 0.1875 * sin(i * delta), 0.1875f + 0.1875 * cos(i * delta)}; + + points.push_back(v); + normals.push_back(n); + textures.push_back(t); + } return std::make_pair(std::make_pair(points, normals), textures); } diff --git a/generator/src/shapes/sphere.cpp b/generator/src/shapes/sphere.cpp index e51814b..5ee0ec3 100644 --- a/generator/src/shapes/sphere.cpp +++ b/generator/src/shapes/sphere.cpp @@ -62,22 +62,22 @@ sphereAllPoints(float radius, int slices, int stacks) { normals.push_back(p4.normalize()); // Texture coordinates - float u1 = phi1 / (2.0f * static_cast(M_PI)); - float u2 = phi2 / (2.0f * static_cast(M_PI)); - float v1 = theta1 / static_cast(M_PI); - float v2 = theta2 / static_cast(M_PI); - - Point2D t1(u1, v1); - Point2D t2(u2, v1); - Point2D t3(u1, v2); - Point2D t4(u2, v2); - - textures.push_back(t1); - textures.push_back(t4); - textures.push_back(t2); - textures.push_back(t1); - textures.push_back(t3); - textures.push_back(t4); + float u1 = phi1 / (2.0f * static_cast(M_PI)); + float u2 = phi2 / (2.0f * static_cast(M_PI)); + float v1 = theta1 / static_cast(M_PI); + float v2 = theta2 / static_cast(M_PI); + + Point2D t1(u1, v1); + Point2D t2(u2, v1); + Point2D t3(u1, v2); + Point2D t4(u2, v2); + + textures.push_back(t1); + textures.push_back(t4); + textures.push_back(t2); + textures.push_back(t1); + textures.push_back(t3); + textures.push_back(t4); } } diff --git a/scenes/advanced/default.xml b/scenes/advanced/default.xml index f1695e5..73f1281 100644 --- a/scenes/advanced/default.xml +++ b/scenes/advanced/default.xml @@ -11,10 +11,10 @@ - - - - - + + + + + \ No newline at end of file diff --git a/scenes/simple/race.xml b/scenes/simple/race.xml index ff5fdd4..78520d2 100644 --- a/scenes/simple/race.xml +++ b/scenes/simple/race.xml @@ -6,7 +6,9 @@ + + @@ -22,7 +24,9 @@ - + + + @@ -33,6 +37,13 @@ + + + + + + + diff --git a/textures/donut.png b/textures/donut.png new file mode 100644 index 0000000..6aab9ac Binary files /dev/null and b/textures/donut.png differ diff --git a/textures/ferrari.png b/textures/ferrari.png new file mode 100644 index 0000000..689494d Binary files /dev/null and b/textures/ferrari.png differ