~ | Shader class working

This commit is contained in:
RedShip
2024-10-13 20:59:24 +02:00
parent 793398f888
commit d8fd3d0535
11 changed files with 285 additions and 26 deletions

121
LMakefile Normal file
View File

@ -0,0 +1,121 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# LMakefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/13 19:39:57 by ycontre #+# #+# #
# Updated: 2024/10/13 20:54:10 by ycontre ### ########.fr #
# #
# **************************************************************************** #
BLACK = \033[30;49;3m
RED = \033[31;49;3m
GREEN = \033[32;49;3m
YELLOW = \033[33;49;3m
BLUE = \033[34;49;3m
MAGENTA = \033[35;49;3m
CYAN = \033[36;49;3m
WHITE = \033[37;49;3m
BBLACK = \033[30;49;3;1m
BRED = \033[31;49;3;1m
BGREEN = \033[32;49;3;1m
BYELLOW = \033[33;49;3;1m
BBLUE = \033[34;49;3;1m
BMAGENTA = \033[35;49;3;1m
BCYAN = \033[36;49;3;1m
BWHITE = \033[37;49;3;1m
RESET = \033[0m
LINE_CLR = \33[2K\r
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)
CMP = 1
NAME := RT
SRCS_DIR := srcs
OBJS_DIR := .objs
ASSETS_DIR := assets
SRC_ASSETS_DIR := assets_src
ALL_SRCS := RT.cpp gl.cpp \
Window.cpp Shader.cpp \
SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%)
OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o))
HEADERS := includes/RT.hpp
CC := clang
CFLAGS := -Ofast
IFLAGS := -I ./includes -L/usr/local/lib -lglfw -lstdc++ # -L./lib -lglfw3
RM := rm -rf
MAKEFLAGS += --no-print-directory
DIR_DUP = mkdir -p $(@D)
# RULES ********************************************************************** #
all: $(NAME)
bonus: all
$(NAME): $(OBJS) $(HEADERS) $(ASSETS)
@$(CC) $(CFLAGS) $(IFLAGS) $(OBJS) -o $(NAME)
@printf "$(LINE_CLR)$(BWHITE) $(NAME): PROJECT COMPILED !$(RESET)\n\n"
$(OBJS_DIR)/%.o: %.cpp
@$(DIR_DUP)
@if [ $(CMP) -eq '1' ]; then \
printf "\n"; \
fi;
@printf "$(LINE_CLR)$(WHITE) $(NAME): $(CMP)/$(FILE) $(BWHITE)$<$(RESET) $(GREEN)compiling...$(RESET)"
@$(CC) $(CFLAGS) $(IFLAGS) -o $@ -c $^
@$(eval CMP=$(shell echo $$(($(CMP)+1))))
@if [ $(CMP) -gt $(FILE) ]; then \
printf "$(LINE_CLR)$(WHITE) $(NAME): $$(($(CMP)-1))/$(FILE)\n$(LINE_CLR)$(BGREEN) Compilation done !$(RESET)\n"; \
fi \
clean:
@$(RM) $(OBJS)
dclean: clean
@$(RM) $(OBJS_DIR)
fclean: dclean
@make --quiet clean -C ${MINILIB_DIR}
@printf " $(BWHITE)$(NAME):$(BRED) cleaned.$(RESET)\n"
@$(RM) $(NAME)
@$(MAKE) -C $(LFT_DIR) fclean
@killall convert 2>/dev/null > /dev/null|| true && \
sleep 0.5 && rm -rf $(ASSETS_DIR)&
mfclean: dclean
@$(RM) $(NAME)
mre:
@$(MAKE) mfclean
@$(MAKE) all
re:
@$(MAKE) fclean
@$(MAKE) all
# **************************************************************************** #
.PHONY: all clean fclean dclean re bonus

View File

@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ #
# By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/09/27 14:52:58 by TheRed #+# #+# #
# Updated: 2024/09/27 14:52:58 by TheRed ### ########.fr #
# Updated: 2024/10/13 20:04:05 by ycontre ### ########.fr #
# #
# **************************************************************************** #

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RT.h :+: :+: :+: */
/* RT.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/27 14:52:10 by TheRed #+# #+# */
/* Updated: 2024/09/27 14:52:10 by TheRed ### ########.fr */
/* Updated: 2024/10/13 20:52:17 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,5 +23,6 @@
# include "Vector/Vector.hpp"
# include "Window.hpp"
# include "Shader.hpp"
#endif

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Shader.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 18:10:10 by TheRed #+# #+# */
/* Updated: 2024/10/13 18:10:10 by TheRed ### ########.fr */
/* Updated: 2024/10/13 20:57:49 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,14 +18,14 @@
class Shader
{
public:
Shader(void);
Shader(char *vertexPath, char *fragmentPath);
Shader(Shader const &src);
~Shader(void);
Shader &operator=(Shader const &rhs);
// void compile(const char *vertexSource, const char *fragmentSource);
// void use(void) const;
void attach(void);
// void setBool(const std::string &name, bool value) const;
// void setInt(const std::string &name, int value) const;
@ -35,8 +35,15 @@ class Shader
// void setVec4(const std::string &name, const RT::Vec4f &value) const;
// void setMat4(const std::string &name, const RT::Mat4f &value) const;
private:
unsigned int _id;
GLuint getProgram(void) const;
// void checkCompileErrors(unsigned int shader, std::string type);
private:
GLuint _program;
GLuint _vertex;
GLuint _fragment;
void checkCompileErrors(unsigned int shader);
};
#endif

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Vec3.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:17:04 by TheRed #+# #+# */
/* Updated: 2024/10/13 17:17:04 by TheRed ### ########.fr */
/* Updated: 2024/10/13 20:32:13 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* Window.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 16:15:41 by TheRed #+# #+# */
/* Updated: 2024/10/13 16:15:41 by TheRed ### ########.fr */
/* Updated: 2024/10/13 20:48:46 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */

7
shaders/frag.glsl Normal file
View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 0., 0., 0.);
}

6
shaders/vertex.glsl Normal file
View File

@ -0,0 +1,6 @@
#version 330 core
layout(location = 0) in vec2 vPos;
void main()
{
gl_Position = vec4(vPos, 0.0, 1.0);
};

View File

@ -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
View 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);
}

View File

@ -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,7 +61,7 @@ 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)
{
@ -70,12 +70,14 @@ void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action,
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)
{