mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
~ | Shader class working
This commit is contained in:
15
srcs/RT.cpp
15
srcs/RT.cpp
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* RT.c :+: :+: :+: */
|
||||
/* RT.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
|
||||
/* Updated: 2024/09/27 14:51:49 by TheRed ### ########.fr */
|
||||
/* Updated: 2024/10/13 20:58:06 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,12 +15,19 @@
|
||||
int main(void)
|
||||
{
|
||||
Window window;
|
||||
GLFWwindow *win = window.getWindow();
|
||||
|
||||
Shader shader("shaders/vertex.glsl", "shaders/frag.glsl");
|
||||
|
||||
shader.attach();
|
||||
|
||||
|
||||
|
||||
while (!window.shouldClose())
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shader.getProgram());
|
||||
|
||||
window.display();
|
||||
window.pollEvents();
|
||||
}
|
||||
|
108
srcs/Shader.cpp
Normal file
108
srcs/Shader.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Shader.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
|
||||
/* Updated: 2024/10/13 20:58:02 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Shader::Shader(char *vertexPath, char *fragmentPath)
|
||||
{
|
||||
const char *vertexCode = load_file(vertexPath);
|
||||
const char *fragmentCode = load_file(fragmentPath);
|
||||
|
||||
_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);
|
||||
}
|
||||
|
||||
void Shader::attach(void)
|
||||
{
|
||||
_program = glCreateProgram();
|
||||
|
||||
glAttachShader(_program, _vertex);
|
||||
glAttachShader(_program, _fragment);
|
||||
glLinkProgram(_program);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
GLuint Shader::getProgram(void) const
|
||||
{
|
||||
return (_program);
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* window.cpp :+: :+: :+: */
|
||||
/* Window.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
|
||||
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/13 16:16:24 by TheRed #+# #+# */
|
||||
/* Updated: 2024/10/13 16:16:24 by TheRed ### ########.fr */
|
||||
/* Updated: 2024/10/13 20:52:00 by ycontre ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -61,21 +61,23 @@ Window::~Window(void)
|
||||
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;
|
||||
if (action == GLFW_PRESS)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
|
||||
{
|
||||
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
|
||||
(void) win; (void) xpos; (void) ypos;
|
||||
|
||||
win->_mousePos = RT::Vec2i(xpos, 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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user