diff --git a/LMakefile b/LMakefile new file mode 100644 index 0000000..d588785 --- /dev/null +++ b/LMakefile @@ -0,0 +1,121 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# LMakefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: ycontre +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# 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 diff --git a/Makefile b/Makefile index 0717308..aed801c 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: TheRed +#+ +:+ +#+ # +# By: ycontre +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 # # # # **************************************************************************** # diff --git a/includes/RT.hpp b/includes/RT.hpp index 21c471b..96ba047 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* RT.h :+: :+: :+: */ +/* RT.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 \ No newline at end of file diff --git a/includes/Shader.hpp b/includes/Shader.hpp index 50c089e..f3a0c6e 100644 --- a/includes/Shader.hpp +++ b/includes/Shader.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Shader.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); -}; \ No newline at end of file + private: + GLuint _program; + + GLuint _vertex; + GLuint _fragment; + + void checkCompileErrors(unsigned int shader); +}; + +#endif \ No newline at end of file diff --git a/includes/Vector/Vec3.hpp b/includes/Vector/Vec3.hpp index c9898a3..e3f4f69 100644 --- a/includes/Vector/Vec3.hpp +++ b/includes/Vector/Vec3.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Vec3.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/includes/Window.hpp b/includes/Window.hpp index 4f12b9f..7588218 100644 --- a/includes/Window.hpp +++ b/includes/Window.hpp @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* Window.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/shaders/frag.glsl b/shaders/frag.glsl new file mode 100644 index 0000000..002dd51 --- /dev/null +++ b/shaders/frag.glsl @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0, 0., 0., 0.); +} \ No newline at end of file diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl new file mode 100644 index 0000000..da5678f --- /dev/null +++ b/shaders/vertex.glsl @@ -0,0 +1,6 @@ +#version 330 core +layout(location = 0) in vec2 vPos; +void main() +{ + gl_Position = vec4(vPos, 0.0, 1.0); +}; \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 8abaf61..73f0d0e 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* RT.c :+: :+: :+: */ +/* RT.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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(); } diff --git a/srcs/Shader.cpp b/srcs/Shader.cpp new file mode 100644 index 0000000..fa995a2 --- /dev/null +++ b/srcs/Shader.cpp @@ -0,0 +1,108 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Shader.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ycontre +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} \ No newline at end of file diff --git a/srcs/Window.cpp b/srcs/Window.cpp index 3f69b02..ae98ca4 100644 --- a/srcs/Window.cpp +++ b/srcs/Window.cpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* window.cpp :+: :+: :+: */ +/* Window.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: TheRed +#+ +:+ +#+ */ +/* By: ycontre +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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(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(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(glfwGetWindowUserPointer(window)); + (void) win; (void) button; (void) mods; if (action == GLFW_PRESS) {