+ | Basic shader

This commit is contained in:
RedShip
2024-10-14 20:00:56 +02:00
parent eae1ae4790
commit 163828707a
6 changed files with 134 additions and 179 deletions

121
LMakefile
View File

@ -1,121 +0,0 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# 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

101
Makefile
View File

@ -5,71 +5,118 @@
# +:+ +:+ +:+ # # +:+ +:+ +:+ #
# By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ # # By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/09/27 14:52:58 by TheRed #+# #+# # # Created: 2024/10/13 19:39:57 by ycontre #+# #+# #
# Updated: 2024/10/13 20:04:05 by ycontre ### ########.fr # # Updated: 2024/10/14 19:31:54 by ycontre ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
BLACK =  BLACK = \033[30;49;3m
RED =  RED = \033[31;49;3m
GREEN =  GREEN = \033[32;49;3m
YELLOW =  YELLOW = \033[33;49;3m
BLUE =  BLUE = \033[34;49;3m
MAGENTA =  MAGENTA = \033[35;49;3m
CYAN =  CYAN = \033[36;49;3m
WHITE =  WHITE = \033[37;49;3m
RESET =  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 LINE_CLR = \33[2K\r
FILE = $(shell ls -lR srcs/ | grep -F .c | wc -l)
CMP = 1
NAME := RT NAME := RT
SRCS_DIR := srcs SRCS_DIR := srcs
OBJS_DIR := .objs OBJS_DIR := .objs
ASSETS_DIR := assets
SRC_ASSETS_DIR := assets_src
ALL_SRCS := RT.cpp gl.cpp \ ALL_SRCS := RT.cpp gl.cpp \
Window.cpp Shader.cpp \ Window.cpp Shader.cpp \
SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%) SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%)
OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o)) OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o))
CC := g++ -Wextra -Wall -Werror HEADERS := includes/RT.hpp
IFLAGS := -Ofast -I./includes -L./lib -lglfw3 -lopengl32 -lgdi32 -lcglm CC := clang -Wextra -Werror -Wall
RM := del /f /s /q CFLAGS := -Ofast
LDFLAGS := -lglfw -lstdc++
IFLAGS := -I ./includes
RM := rm -rf
MAKEFLAGS += --no-print-directory MAKEFLAGS += --no-print-directory
DIR_DUP = if not exist "$(@D)" mkdir "$(@D)" DIR_DUP = mkdir -p $(@D)
# RULES ********************************************************************** # # RULES ********************************************************************** #
all: $(NAME) all: $(NAME)
$(NAME): $(OBJS) $(HEADERS) bonus: all
@$(CC) -o $(NAME) $(OBJS) $(IFLAGS)
@echo $(WHITE) $(NAME): PROJECT COMPILED !$(RESET) & echo: $(NAME): $(OBJS) $(HEADERS) $(ASSETS)
@$(CC) $(LDFLAGS) $(CFLAGS) $(IFLAGS) $(OBJS) -o $(NAME)
@printf "$(LINE_CLR)$(BWHITE) $(NAME): PROJECT COMPILED !$(RESET)\n\n"
$(OBJS_DIR)/%.o: %.cpp $(OBJS_DIR)/%.o: %.cpp
@$(DIR_DUP) @$(DIR_DUP)
@echo $(WHITE) $(NAME): $(WHITE)$<$(RESET) $(GREEN)compiling...$(RESET) @if [ $(CMP) -eq '1' ]; then \
@$(CC) -c $^ $(IFLAGS) -o $@ 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 \
fclean: clean:
@echo $(WHITE)$(NAME):$(RED) cleaned.$(RESET) @$(RM) $(OBJS)
@del /f /s /q $(NAME).exe
@rmdir /S /Q "$(OBJS_DIR)" 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: re:
@$(MAKE) fclean @make fclean
@$(MAKE) all @make all
# **************************************************************************** # # **************************************************************************** #

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 18:10:10 by TheRed #+# #+# */ /* Created: 2024/10/13 18:10:10 by TheRed #+# #+# */
/* Updated: 2024/10/13 20:57:49 by ycontre ### ########.fr */ /* Updated: 2024/10/14 19:51:46 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,18 +26,25 @@ class Shader
// void compile(const char *vertexSource, const char *fragmentSource); // void compile(const char *vertexSource, const char *fragmentSource);
void attach(void); void attach(void);
void setupVertexBuffer(const RT::Vec2f* vertices, size_t size);
void drawTriangles(void);
// void setBool(const std::string &name, bool value) const; // void setBool(const std::string &name, bool value) const;
// void setInt(const std::string &name, int value) const; // void setInt(const std::string &name, int value) const;
// void setFloat(const std::string &name, float value) const; // void setFloat(const std::string &name, float value) const;
// void setVec2(const std::string &name, const RT::Vec2f &value) const; void setVec2f(const std::string &name, const RT::Vec2f &value) const;
// void setVec3(const std::string &name, const RT::Vec3f &value) const; // void setVec3(const std::string &name, const RT::Vec3f &value) const;
// void setVec4(const std::string &name, const RT::Vec4f &value) const; // void setVec4(const std::string &name, const RT::Vec4f &value) const;
// void setMat4(const std::string &name, const RT::Mat4f &value) const; // void setMat4(const std::string &name, const RT::Mat4f &value) const;
GLuint getProgram(void) const; GLuint getProgram(void) const;
private: private:
GLuint _screen_VAO, _screen_VBO;
GLuint _program; GLuint _program;
GLuint _vertex; GLuint _vertex;

View File

@ -1,7 +1,17 @@
#version 330 core #version 430 core
out vec4 FragColor; out vec4 FragColor;
uniform vec2 u_resolution;
void main() void main()
{ {
FragColor = vec4(1.0, 0., 0., 1.); vec2 uv;
vec4 color;
uv = gl_FragCoord.xy / u_resolution.xy;
uv.x *= u_resolution.x / u_resolution.y;
color = vec4(uv.x, uv.y, 0., 0.);
FragColor = color;
} }

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */ /* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
/* Updated: 2024/10/13 20:58:06 by ycontre ### ########.fr */ /* Updated: 2024/10/14 19:54:42 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,37 +20,20 @@ int main(void)
shader.attach(); shader.attach();
RT::Vec2f vertices[6] = { RT::Vec2f vertices[6] = {
{ -0.5f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f },
{ 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f } { 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f }
}; };
unsigned int VAO, VBO; shader.setupVertexBuffer(vertices, sizeof(vertices));
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// Bind the VAO
glBindVertexArray(VAO);
// Bind the VBO and upload the vertex data to it
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Set vertex attributes
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Unbind the VBO and VAO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!window.shouldClose()) while (!window.shouldClose())
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader.getProgram()); shader.setVec2f("u_resolution", RT::Vec2f(WIDTH, HEIGHT));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 6); glUseProgram(shader.getProgram());
shader.drawTriangles();
window.display(); window.display();
window.pollEvents(); window.pollEvents();

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */ /* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */ /* Created: 2024/10/13 20:21:13 by ycontre #+# #+# */
/* Updated: 2024/10/13 20:58:02 by ycontre ### ########.fr */ /* Updated: 2024/10/14 19:52:40 by ycontre ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -102,6 +102,35 @@ void Shader::checkCompileErrors(GLuint shader)
} }
} }
void Shader::setupVertexBuffer(const RT::Vec2f* 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, 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(void)
{
glBindVertexArray(_screen_VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
void Shader::setVec2f(const std::string &name, const RT::Vec2f &value) const
{
glUniform2f(glGetUniformLocation(_program, name.c_str()), value[0], value[1]);
}
GLuint Shader::getProgram(void) const GLuint Shader::getProgram(void) const
{ {
return (_program); return (_program);