+ | Vector classes

This commit is contained in:
TheRedShip
2024-10-13 18:06:57 +02:00
parent 8784b5ce02
commit 0bd05282cf
10 changed files with 407 additions and 33 deletions

View File

@ -42,6 +42,8 @@
"xstddef": "cpp", "xstddef": "cpp",
"xstring": "cpp", "xstring": "cpp",
"xtr1common": "cpp", "xtr1common": "cpp",
"xutility": "cpp" "xutility": "cpp",
"array": "cpp",
"vector": "cpp"
} }
} }

View File

@ -30,6 +30,7 @@ SRCS_DIR := srcs
OBJS_DIR := .objs OBJS_DIR := .objs
ALL_SRCS := RT.cpp gl.cpp \ ALL_SRCS := RT.cpp gl.cpp \
Window.cpp
SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%) SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%)

View File

@ -10,8 +10,8 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef RT_H #ifndef RT__HPP
# define RT_H # define RT__HPP
# define WIDTH 1920 # define WIDTH 1920
# define HEIGHT 1080 # define HEIGHT 1080
@ -21,4 +21,7 @@
# include <iostream> # include <iostream>
# include "Vector/Vector.hpp"
# include "Window.hpp"
#endif #endif

38
includes/Vector/Vec2.hpp Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vec2.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<typename T>
class Vec2 : public Vector<Vec2<T>, T, 2>
{
public:
using Vector<Vec2<T>, T, 2>::Vector;
constexpr Vec2(T x, T y)
{
this->data[0] = x;
this->data[1] = y;
}
};
using Vec2f = Vec2<float>;
using Vec2d = Vec2<double>;
using Vec2i = Vec2<int>;
}
#endif

49
includes/Vector/Vec3.hpp Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vec3.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <cmath>
namespace RT
{
template<typename T>
class Vec3 : public Vector<Vec3<T>, T, 3>
{
public:
using Vector<Vec3<T>, 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<float>;
using Vec3d = Vec3<double>;
using Vec3i = Vec3<int>;
}
#endif

40
includes/Vector/Vec4.hpp Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vec4.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<typename T>
class Vec4 : public Vector<Vec4<T>, T, 4>
{
public:
using Vector<Vec4<T>, 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<float>;
using Vec4d = Vec4<double>;
using Vec4i = Vec4<int>;
}
#endif

111
includes/Vector/Vector.hpp Normal file
View File

@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <iostream>
#include <array>
namespace RT
{
template<typename Derived, typename T, std::size_t N>
class Vector
{
public:
std::array<T, N> data;
constexpr Vector() { data.fill(0); }
constexpr Vector(std::initializer_list<T> 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<Derived&>(*this);
}
constexpr Derived& operator-=(const Vector& vec)
{
*this = *this - vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator*=(const Vector& vec)
{
*this = *this * vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator/=(const Vector& vec)
{
*this = *this / vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator*=(T scalar)
{
*this = *this * scalar;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator/=(T scalar)
{
*this = *this / scalar;
return static_cast<Derived&>(*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

45
includes/Window.hpp Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Window.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -10,42 +10,20 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "RT.h" #include "RT.hpp"
int main(void) int main(void)
{ {
if (!glfwInit()) Window window;
{ GLFWwindow *win = window.getWindow();
fprintf( stderr, "Failed to initialize GLFW\n" );
return (-1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); while (!window.shouldClose())
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))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
window.display();
window.pollEvents();
}
return (0); return (0);
} }

107
srcs/Window.cpp Normal file
View File

@ -0,0 +1,107 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* window.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<Window*>(glfwGetWindowUserPointer(window));
if (action == GLFW_PRESS)
{
}
}
void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
{
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
win->_mousePos = RT::Vec2i(xpos, ypos);
}
void Window::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
Window* win = static_cast<Window*>(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);
}