mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | Going to start object systems
This commit is contained in:
74
srcs/class/Camera.cpp
Normal file
74
srcs/class/Camera.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Camera.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 14:00:38 by TheRed #+# #+# */
|
||||
/* Updated: 2024/12/23 17:42:20 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Camera.hpp"
|
||||
|
||||
Camera::Camera(glm::vec3 start_pos, glm::vec3 start_up, float start_yaw, float start_pitch)
|
||||
: _forward(glm::vec3(0.0f, 0.0f, -1.0f)), _position(start_pos), _up(start_up), _pitch(start_pitch), _yaw(start_yaw)
|
||||
{
|
||||
update_camera_vectors();
|
||||
}
|
||||
|
||||
Camera::~Camera(void)
|
||||
{
|
||||
}
|
||||
|
||||
void Camera::update_camera_vectors()
|
||||
{
|
||||
glm::vec3 frontTemp;
|
||||
|
||||
frontTemp.x = cos(glm::radians(_yaw)) * cos(glm::radians(_pitch));
|
||||
frontTemp.y = sin(glm::radians(_pitch));
|
||||
frontTemp.z = sin(glm::radians(_yaw)) * cos(glm::radians(_pitch));
|
||||
_forward = glm::normalize(frontTemp);
|
||||
|
||||
_right = glm::normalize(glm::cross(_forward, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||
_up = glm::normalize(glm::cross(_right, _forward));
|
||||
}
|
||||
|
||||
glm::mat4 Camera::get_view_matrix()
|
||||
{
|
||||
return (glm::lookAt(_position, _position + _forward, _up));
|
||||
}
|
||||
|
||||
glm::vec3 Camera::get_position()
|
||||
{
|
||||
return (_position);
|
||||
}
|
||||
|
||||
void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true)
|
||||
{
|
||||
_yaw += xoffset * 0.2f;
|
||||
_pitch += yoffset * 0.2f;
|
||||
|
||||
if (constraint_pitch)
|
||||
{
|
||||
if (_pitch > 89.0f) _pitch = 89.0f;
|
||||
if (_pitch < -89.0f) _pitch = -89.0f;
|
||||
}
|
||||
|
||||
update_camera_vectors();
|
||||
}
|
||||
|
||||
void Camera::process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down)
|
||||
{
|
||||
float speed;
|
||||
|
||||
speed = .1f;
|
||||
|
||||
if (forward) _position += _forward * speed;
|
||||
if (backward) _position -= _forward * speed;
|
||||
if (left) _position -= _right * speed;
|
||||
if (right) _position += _right * speed;
|
||||
if (up) _position += _up * speed;
|
||||
if (down) _position -= _up * speed;
|
||||
}
|
12
srcs/class/Object.cpp
Normal file
12
srcs/class/Object.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Object.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/23 18:44:00 by ycontre #+# #+# */
|
||||
/* Updated: 2024/12/23 18:44:01 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
28
srcs/class/Scene.cpp
Normal file
28
srcs/class/Scene.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Scene.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
|
||||
/* Updated: 2024/12/23 18:40:17 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Scene.hpp"
|
||||
|
||||
Scene::Scene()
|
||||
{
|
||||
_camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f);
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
{
|
||||
delete _camera;
|
||||
}
|
||||
|
||||
Camera *Scene::getCamera(void) const
|
||||
{
|
||||
return (_camera);
|
||||
}
|
147
srcs/class/Shader.cpp
Normal file
147
srcs/class/Shader.cpp
Normal file
@ -0,0 +1,147 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Shader.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
|
||||
/* Updated: 2024/10/14 19:52:40 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Shader.hpp"
|
||||
|
||||
char* load_file(char const* path)
|
||||
{
|
||||
char* buffer = 0;
|
||||
long length = 0;
|
||||
FILE * f = fopen (path, "rb");
|
||||
|
||||
if (f)
|
||||
{
|
||||
fseek (f, 0, SEEK_END);
|
||||
length = ftell (f);
|
||||
fseek (f, 0, SEEK_SET);
|
||||
buffer = (char*)malloc ((length+1)*sizeof(char));
|
||||
if (buffer)
|
||||
{
|
||||
fread (buffer, sizeof(char), length, f);
|
||||
}
|
||||
fclose (f);
|
||||
}
|
||||
else
|
||||
return (NULL);
|
||||
buffer[length] = '\0';
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
Shader::Shader(std::string vertexPath, std::string fragmentPath)
|
||||
{
|
||||
const char *vertexCode = load_file(vertexPath.c_str());
|
||||
const char *fragmentCode = load_file(fragmentPath.c_str());
|
||||
|
||||
_vertex = glCreateShader(GL_VERTEX_SHADER);
|
||||
|
||||
glShaderSource(_vertex, 1, &vertexCode, NULL);
|
||||
glCompileShader(_vertex);
|
||||
|
||||
checkCompileErrors(_vertex);
|
||||
|
||||
_fragment = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
|
||||
glShaderSource(_fragment, 1, &fragmentCode, NULL);
|
||||
glCompileShader(_fragment);
|
||||
|
||||
checkCompileErrors(_fragment);
|
||||
}
|
||||
|
||||
Shader::Shader(Shader const &src)
|
||||
{
|
||||
*this = src;
|
||||
}
|
||||
|
||||
Shader &Shader::operator=(Shader const &rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
{
|
||||
_program = rhs._program;
|
||||
_vertex = rhs._vertex;
|
||||
_fragment = rhs._fragment;
|
||||
}
|
||||
return (*this);
|
||||
}
|
||||
|
||||
Shader::~Shader(void)
|
||||
{
|
||||
glDeleteShader(_vertex);
|
||||
glDeleteShader(_fragment);
|
||||
glDeleteProgram(_program);
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
|
||||
glAttachShader(_program, _vertex);
|
||||
glAttachShader(_program, _fragment);
|
||||
glLinkProgram(_program);
|
||||
}
|
||||
|
||||
void Shader::checkCompileErrors(GLuint shader)
|
||||
{
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
|
||||
if (!success)
|
||||
{
|
||||
glGetShaderInfoLog(shader, 512, NULL, infoLog);
|
||||
std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::setupVertexBuffer(const glm::vec2* vertices, size_t size)
|
||||
{
|
||||
glGenVertexArrays(1, &_screen_VAO);
|
||||
glGenBuffers(1, &_screen_VBO);
|
||||
|
||||
glBindVertexArray(_screen_VAO);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _screen_VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, size * 3 * sizeof(glm::vec2), vertices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Shader::drawTriangles(size_t size)
|
||||
{
|
||||
glBindVertexArray(_screen_VAO);
|
||||
glDrawArrays(GL_TRIANGLES, 0, size * 3);
|
||||
}
|
||||
|
||||
|
||||
void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const
|
||||
{
|
||||
glUniform2fv(glGetUniformLocation(_program, name.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void Shader::set_vec3(const std::string &name, const glm::vec3 &value) const
|
||||
{
|
||||
glUniform3fv(glGetUniformLocation(_program, name.c_str()), 1, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
void Shader::set_mat4(const std::string &name, const glm::mat4 &value) const
|
||||
{
|
||||
glUniformMatrix4fv(glGetUniformLocation(_program, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));
|
||||
}
|
||||
|
||||
GLuint Shader::getProgram(void) const
|
||||
{
|
||||
return (_program);
|
||||
}
|
141
srcs/class/Window.cpp
Normal file
141
srcs/class/Window.cpp
Normal file
@ -0,0 +1,141 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Window.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */
|
||||
/* Updated: 2024/12/23 18:39:37 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Window.hpp"
|
||||
|
||||
Window::Window(int width, int height, const char *title, int sleep)
|
||||
{
|
||||
_scene = new Scene();
|
||||
|
||||
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, title, 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(sleep);
|
||||
}
|
||||
|
||||
Window::~Window(void)
|
||||
{
|
||||
delete _scene;
|
||||
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
|
||||
void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||
{
|
||||
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
|
||||
(void) win; (void) key; (void) scancode; (void) mods; (void) action;
|
||||
|
||||
bool forward = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS;
|
||||
bool backward = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS;
|
||||
bool left = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS;
|
||||
bool right = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS;
|
||||
bool up = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS;
|
||||
bool down = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS;
|
||||
|
||||
win->_scene->getCamera()->process_keyboard(forward, backward, left, right, up, down);
|
||||
}
|
||||
void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
|
||||
{
|
||||
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
|
||||
(void) win; (void) xpos; (void) ypos;
|
||||
|
||||
static double lastX = 0;
|
||||
static double lastY = 0;
|
||||
|
||||
if (lastX == 0 && lastY == 0)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
}
|
||||
|
||||
double xoffset = xpos - lastX;
|
||||
double yoffset = lastY - ypos;
|
||||
|
||||
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
|
||||
{
|
||||
win->_scene->getCamera()->process_mouse(xoffset, yoffset, true);
|
||||
|
||||
// scene.frameCount = 0;
|
||||
}
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
}
|
||||
void Window::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
|
||||
{
|
||||
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
|
||||
(void) win; (void) button; (void) mods;
|
||||
|
||||
if (action == GLFW_PRESS)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Window::display()
|
||||
{
|
||||
static double lastTime = glfwGetTime();
|
||||
double currentTime = glfwGetTime();
|
||||
double delta = currentTime - lastTime;
|
||||
|
||||
lastTime = currentTime;
|
||||
_fps = 1.0f / delta;
|
||||
|
||||
glfwSwapBuffers(_window);
|
||||
}
|
||||
void Window::pollEvents()
|
||||
{
|
||||
glfwPollEvents();
|
||||
}
|
||||
bool Window::shouldClose()
|
||||
{
|
||||
return glfwWindowShouldClose(_window);
|
||||
}
|
||||
|
||||
GLFWwindow *Window::getWindow(void) const
|
||||
{
|
||||
return (_window);
|
||||
}
|
||||
|
||||
Scene *Window::getScene(void) const
|
||||
{
|
||||
return (_scene);
|
||||
}
|
||||
|
||||
float Window::getFps(void) const
|
||||
{
|
||||
return (_fps);
|
||||
}
|
Reference in New Issue
Block a user