From 0bd05282cf5bbdf42976ebe1f79fe54b103f233e Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Sun, 13 Oct 2024 18:06:57 +0200 Subject: [PATCH] + | Vector classes --- .vscode/settings.json | 4 +- Makefile | 1 + includes/{RT.h => RT.hpp} | 7 ++- includes/Vector/Vec2.hpp | 38 +++++++++++++ includes/Vector/Vec3.hpp | 49 ++++++++++++++++ includes/Vector/Vec4.hpp | 40 +++++++++++++ includes/Vector/Vector.hpp | 111 +++++++++++++++++++++++++++++++++++++ includes/Window.hpp | 45 +++++++++++++++ srcs/RT.cpp | 38 +++---------- srcs/Window.cpp | 107 +++++++++++++++++++++++++++++++++++ 10 files changed, 407 insertions(+), 33 deletions(-) rename includes/{RT.h => RT.hpp} (92%) create mode 100644 includes/Vector/Vec2.hpp create mode 100644 includes/Vector/Vec3.hpp create mode 100644 includes/Vector/Vec4.hpp create mode 100644 includes/Vector/Vector.hpp create mode 100644 includes/Window.hpp create mode 100644 srcs/Window.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index a4c2a5b..c6585fd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -42,6 +42,8 @@ "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", - "xutility": "cpp" + "xutility": "cpp", + "array": "cpp", + "vector": "cpp" } } \ No newline at end of file diff --git a/Makefile b/Makefile index dd9a8e6..0717308 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ SRCS_DIR := srcs OBJS_DIR := .objs ALL_SRCS := RT.cpp gl.cpp \ + Window.cpp SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%) diff --git a/includes/RT.h b/includes/RT.hpp similarity index 92% rename from includes/RT.h rename to includes/RT.hpp index 4c3eb69..21c471b 100644 --- a/includes/RT.h +++ b/includes/RT.hpp @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef RT_H -# define RT_H +#ifndef RT__HPP +# define RT__HPP # define WIDTH 1920 # define HEIGHT 1080 @@ -21,4 +21,7 @@ # include +# include "Vector/Vector.hpp" +# include "Window.hpp" + #endif \ No newline at end of file diff --git a/includes/Vector/Vec2.hpp b/includes/Vector/Vec2.hpp new file mode 100644 index 0000000..059a123 --- /dev/null +++ b/includes/Vector/Vec2.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Vec2.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: TheRed +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/13 17:16:11 by TheRed #+# #+# */ +/* Updated: 2024/10/13 17:16:11 by TheRed ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_VEC2__HPP +#define RT_VEC2__HPP + +# include "Vector/Vector.hpp" + +namespace RT +{ + template + class Vec2 : public Vector, T, 2> + { + public: + using Vector, T, 2>::Vector; + + constexpr Vec2(T x, T y) + { + this->data[0] = x; + this->data[1] = y; + } + }; + + using Vec2f = Vec2; + using Vec2d = Vec2; + using Vec2i = Vec2; +} + +#endif \ No newline at end of file diff --git a/includes/Vector/Vec3.hpp b/includes/Vector/Vec3.hpp new file mode 100644 index 0000000..c9898a3 --- /dev/null +++ b/includes/Vector/Vec3.hpp @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Vec3.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: TheRed +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/13 17:17:04 by TheRed #+# #+# */ +/* Updated: 2024/10/13 17:17:04 by TheRed ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_VEC3__HPP +# define RT_VEC3__HPP + +# include "Vector/Vector.hpp" +# include + +namespace RT +{ + template + class Vec3 : public Vector, T, 3> + { + public: + using Vector, T, 3>::Vector; + + constexpr Vec3(T x, T y, T z) + { + this->data[0] = x; + this->data[1] = y; + this->data[2] = z; + } + + constexpr T Distance(const Vec3& other) const + { + T dx = this->data[0] - other.data[0]; + T dy = this->data[1] - other.data[1]; + T dz = this->data[2] - other.data[2]; + + return std::sqrt(dx * dx + dy * dy + dz * dz); + } + }; + + using Vec3f = Vec3; + using Vec3d = Vec3; + using Vec3i = Vec3; +} + +#endif \ No newline at end of file diff --git a/includes/Vector/Vec4.hpp b/includes/Vector/Vec4.hpp new file mode 100644 index 0000000..f5761b0 --- /dev/null +++ b/includes/Vector/Vec4.hpp @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Vec4.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: TheRed +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/13 17:18:35 by TheRed #+# #+# */ +/* Updated: 2024/10/13 17:18:35 by TheRed ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_VEC4__HPP +# define RT_VEC4__HPP + +# include "Vector/Vector.hpp" + +namespace RT +{ + template + class Vec4 : public Vector, T, 4> + { + public: + using Vector, T, 4>::Vector; + + constexpr Vec4(T r, T g, T b, T a) + { + this->data[0] = r; + this->data[1] = g; + this->data[2] = b; + this->data[3] = a; + } + }; + + using Vec4f = Vec4; + using Vec4d = Vec4; + using Vec4i = Vec4; +} + +#endif \ No newline at end of file diff --git a/includes/Vector/Vector.hpp b/includes/Vector/Vector.hpp new file mode 100644 index 0000000..b2c3d5e --- /dev/null +++ b/includes/Vector/Vector.hpp @@ -0,0 +1,111 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Vector.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: TheRed +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/13 17:13:03 by TheRed #+# #+# */ +/* Updated: 2024/10/13 17:13:03 by TheRed ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_VECTOR__HPP +#define RT_VECTOR__HPP + +#include +#include + +namespace RT +{ + template + class Vector + { + public: + std::array data; + + constexpr Vector() { data.fill(0); } + + constexpr Vector(std::initializer_list values) { std::copy(values.begin(), values.end(), data.begin()); } + + constexpr T& operator[](std::size_t i) { return data[i]; } + constexpr const T& operator[](std::size_t i) const { return data[i]; } + + constexpr Derived operator+(const Vector& vec) const + { + Derived result; + for (std::size_t i = 0; i < N; ++i) + result[i] = data[i] + vec[i]; + return result; + } + constexpr Derived operator-(const Vector& vec) const + { + Derived result; + for (std::size_t i = 0; i < N; ++i) + result[i] = data[i] - vec[i]; + return result; + } + constexpr Derived operator*(T scalar) const + { + Derived result; + for (std::size_t i = 0; i < N; ++i) + result[i] = data[i] * scalar; + return result; + } + constexpr Derived operator/(T scalar) const + { + Derived result; + for (std::size_t i = 0; i < N; ++i) + result[i] = data[i] / scalar; + return result; + } + + constexpr Derived& operator+=(const Vector& vec) + { + *this = *this + vec; + return static_cast(*this); + } + constexpr Derived& operator-=(const Vector& vec) + { + *this = *this - vec; + return static_cast(*this); + } + constexpr Derived& operator*=(const Vector& vec) + { + *this = *this * vec; + return static_cast(*this); + } + constexpr Derived& operator/=(const Vector& vec) + { + *this = *this / vec; + return static_cast(*this); + } + + constexpr Derived& operator*=(T scalar) + { + *this = *this * scalar; + return static_cast(*this); + } + constexpr Derived& operator/=(T scalar) + { + *this = *this / scalar; + return static_cast(*this); + } + + void print() const + { + std::cout << "Vec" << N << "("; + for (std::size_t i = 0; i < N; ++i) { + std::cout << data[i]; + if (i < N - 1) std::cout << ", "; + } + std::cout << ")" << std::endl; + } + }; +} + +#include "Vector/Vec2.hpp" +#include "Vector/Vec3.hpp" +#include "Vector/Vec4.hpp" + +#endif \ No newline at end of file diff --git a/includes/Window.hpp b/includes/Window.hpp new file mode 100644 index 0000000..4f12b9f --- /dev/null +++ b/includes/Window.hpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Window.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: TheRed +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/13 16:15:41 by TheRed #+# #+# */ +/* Updated: 2024/10/13 16:15:41 by TheRed ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_WINDOW__HPP +# define RT_WINDOW__HPP + +# include "RT.hpp" + +class Window +{ + public: + Window(void); + Window(Window const &src); + ~Window(void); + + Window &operator=(Window const &rhs); + + GLFWwindow *getWindow(void) const; + RT::Vec2i getMousePos(void) const; + + + void display(); + void pollEvents(); + bool shouldClose(); + + static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); + static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos); + static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods); + + private: + GLFWwindow *_window; + RT::Vec2i _mousePos; + +}; + +#endif \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index adfe533..8abaf61 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -10,42 +10,20 @@ /* */ /* ************************************************************************** */ -#include "RT.h" - - +#include "RT.hpp" int main(void) { - if (!glfwInit()) - { - fprintf( stderr, "Failed to initialize GLFW\n" ); - return (-1); - } + Window window; + GLFWwindow *win = window.getWindow(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - GLFWwindow* window; - window = glfwCreateWindow(WIDTH, HEIGHT, "RT_GPU", NULL, NULL); - if (!window ) - { - fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); - glfwTerminate(); - return -1; - } - - glfwMakeContextCurrent(window); - gladLoadGL(glfwGetProcAddress); - glfwSwapInterval(1); - - while (!glfwWindowShouldClose(window)) + while (!window.shouldClose()) { glClear(GL_COLOR_BUFFER_BIT); - - glfwSwapBuffers(window); - glfwPollEvents(); - } + window.display(); + window.pollEvents(); + } + return (0); } \ No newline at end of file diff --git a/srcs/Window.cpp b/srcs/Window.cpp new file mode 100644 index 0000000..3f69b02 --- /dev/null +++ b/srcs/Window.cpp @@ -0,0 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* window.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: TheRed +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */ +/* Updated: 2024/10/13 16:16:24 by TheRed ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "Window.hpp" + +Window::Window(void) +{ + if (!glfwInit()) + { + fprintf( stderr, "Failed to initialize GLFW\n" ); + exit(-1); + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + _window = glfwCreateWindow(WIDTH, HEIGHT, "RT_GPU", NULL, NULL); + if (!_window ) + { + fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); + glfwTerminate(); + exit(-1); + } + + glfwMakeContextCurrent(_window); + glfwSetWindowUserPointer(_window, this); + + glfwSetKeyCallback(_window, keyCallback); + glfwSetCursorPosCallback(_window, mouseMoveCallback); + glfwSetMouseButtonCallback(_window, mouseButtonCallback); + + gladLoadGL(glfwGetProcAddress); + glfwSwapInterval(1); +} +Window::Window(Window const &src) +{ + *this = src; +} +Window &Window::operator=(Window const &rhs) +{ + if (this != &rhs) + _window = rhs._window; + return (*this); +} +Window::~Window(void) +{ + glfwTerminate(); +} + + +void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) +{ + Window* win = static_cast(glfwGetWindowUserPointer(window)); + + if (action == GLFW_PRESS) + { + + } +} +void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) +{ + Window* win = static_cast(glfwGetWindowUserPointer(window)); + + win->_mousePos = RT::Vec2i(xpos, ypos); +} +void Window::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) +{ + Window* win = static_cast(glfwGetWindowUserPointer(window)); + + if (action == GLFW_PRESS) + { + + } +} + + +void Window::display() +{ + glfwSwapBuffers(_window); +} +void Window::pollEvents() +{ + glfwPollEvents(); +} +bool Window::shouldClose() +{ + return glfwWindowShouldClose(_window); +} + +GLFWwindow *Window::getWindow(void) const +{ + return (_window); +} +RT::Vec2i Window::getMousePos(void) const +{ + return (_mousePos); +}