+ | First sphere

This commit is contained in:
TheRedShip
2024-10-14 23:16:28 +02:00
parent 163828707a
commit 9d7a30800e
10 changed files with 239 additions and 110 deletions

119
LMakefile Normal file
View File

@ -0,0 +1,119 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/13 19:39:57 by ycontre #+# #+# #
# Updated: 2024/10/14 19:31:54 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 -Wextra -Werror -Wall
CFLAGS := -Ofast
LDFLAGS := -lglfw -lstdc++
IFLAGS := -I ./includes
RM := rm -rf
MAKEFLAGS += --no-print-directory
DIR_DUP = mkdir -p $(@D)
# RULES ********************************************************************** #
all: $(NAME)
bonus: all
$(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
@$(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
@printf " $(BWHITE)$(NAME):$(BRED) cleaned.$(RESET)\n"
@$(RM) $(NAME)
mfclean: dclean
@$(RM) $(NAME)
mre:
@$(MAKE) mfclean
@$(MAKE) all
re:
@$(MAKE) fclean
@$(MAKE) all
# **************************************************************************** #
.PHONY: all clean fclean dclean re bonus

111
Makefile
View File

@ -1,123 +1,74 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# WMakefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ #
# By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/13 19:39:57 by ycontre #+# #+# #
# Updated: 2024/10/14 19:31:54 by ycontre ### ########.fr #
# Created: 2024/10/14 22:33:37 by TheRed #+# #+# #
# Updated: 2024/10/14 22:33:37 by TheRed ### ########.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
BLACK = 
RED = 
GREEN = 
YELLOW = 
BLUE = 
MAGENTA = 
CYAN = 
WHITE = 
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
RESET = 
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 \
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 := g++
CC := clang -Wextra -Werror -Wall
IFLAGS := -Ofast -I./includes -L./lib -lglfw3 -lopengl32 -lgdi32 -lcglm
CFLAGS := -Ofast
LDFLAGS := -lglfw -lstdc++
IFLAGS := -I ./includes
RM := rm -rf
RM := del /f /s /q
MAKEFLAGS += --no-print-directory
DIR_DUP = mkdir -p $(@D)
DIR_DUP = if not exist "$(@D)" mkdir "$(@D)"
# RULES ********************************************************************** #
all: $(NAME)
bonus: all
$(NAME): $(OBJS) $(HEADERS) $(ASSETS)
@$(CC) $(LDFLAGS) $(CFLAGS) $(IFLAGS) $(OBJS) -o $(NAME)
@printf "$(LINE_CLR)$(BWHITE) $(NAME): PROJECT COMPILED !$(RESET)\n\n"
$(NAME): $(OBJS) $(HEADERS)
@$(CC) -o $(NAME) $(OBJS) $(IFLAGS)
@echo $(WHITE) $(NAME): PROJECT COMPILED !$(RESET) & echo:
$(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 \
@echo $(WHITE) $(NAME): $(WHITE)$<$(RESET) $(GREEN)compiling...$(RESET)
@$(CC) -c $^ $(IFLAGS) -o $@
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
fclean:
@echo $(WHITE)$(NAME):$(RED) cleaned.$(RESET)
@del /f /s /q $(NAME).exe
@rmdir /S /Q "$(OBJS_DIR)"
re:
@make fclean
@make all
@$(MAKE) fclean
@$(MAKE) all
# **************************************************************************** #
.PHONY: all clean fclean dclean re bonus
.PHONY: all clean fclean dclean re bonus

View File

@ -27,7 +27,7 @@ class Shader
// void compile(const char *vertexSource, const char *fragmentSource);
void attach(void);
void setupVertexBuffer(const RT::Vec2f* vertices, size_t size);
void drawTriangles(void);
void drawTriangles(size_t size);

View File

@ -18,7 +18,7 @@
class Window
{
public:
Window(void);
Window(int width, int height, const char *title, int sleep);
Window(Window const &src);
~Window(void);

75
shaders/frag.frag Normal file
View File

@ -0,0 +1,75 @@
#version 430 core
out vec4 FragColor;
uniform vec2 u_resolution;
vec3 sphereCenter = vec3(0.0, 0.0, -5.0);
float sphereRadius = 1.0;
vec3 lightPos = vec3(5.0, 5.0, 5.0);
vec3 lightColor = vec3(1.0, 1.0, 1.0);
vec3 objectColor = vec3(0.4, 0.7, 0.9);
struct Ray {
vec3 origin;
vec3 direction;
};
bool intersectSphere(Ray ray, vec3 center, float radius, out float t) {
vec3 oc = ray.origin - center;
float a = dot(ray.direction, ray.direction);
float b = 2.0 * dot(oc, ray.direction);
float c = dot(oc, oc) - radius * radius;
float discriminant = b * b - 4.0 * a * c;
if (discriminant < 0.0) {
return false;
} else {
t = (-b - sqrt(discriminant)) / (2.0 * a);
return true;
}
}
vec3 computeLighting(vec3 point, vec3 normal, vec3 viewDir) {
vec3 lightDir = normalize(lightPos - point);
float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
return objectColor * diffuse;
}
void main()
{
vec2 uv;
vec4 color;
uv = gl_FragCoord.xy / u_resolution.xy * 2.0 - 1.0;
uv.x *= u_resolution.x / u_resolution.y;
vec3 rayOrigin = vec3(0.0, 0.0, 0.0);
vec3 rayDirection = normalize(vec3(uv, -1.0));
Ray ray = Ray(rayOrigin, rayDirection);
float t;
if (intersectSphere(ray, sphereCenter, sphereRadius, t))
{
vec3 hitPoint = ray.origin + t * ray.direction;
vec3 normal = normalize(hitPoint - sphereCenter);
vec3 viewDir = normalize(-ray.direction);
vec3 color = computeLighting(hitPoint, normal, viewDir);
FragColor = vec4(color, 1.0);
} else {
FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
}

View File

@ -1,17 +0,0 @@
#version 430 core
out vec4 FragColor;
uniform vec2 u_resolution;
void main()
{
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

@ -14,8 +14,8 @@
int main(void)
{
Window window;
Shader shader("shaders/vertex.glsl", "shaders/frag.glsl");
Window window(WIDTH, HEIGHT, "RT_GPU", 1);
Shader shader("shaders/vertex.vert", "shaders/frag.frag");
shader.attach();
@ -23,8 +23,9 @@ int main(void)
{ -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f },
{ 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f }
};
size_t size = sizeof(vertices) / sizeof(RT::Vec2f) / 3;
shader.setupVertexBuffer(vertices, sizeof(vertices));
shader.setupVertexBuffer(vertices, size);
while (!window.shouldClose())
{
@ -33,7 +34,7 @@ int main(void)
shader.setVec2f("u_resolution", RT::Vec2f(WIDTH, HEIGHT));
glUseProgram(shader.getProgram());
shader.drawTriangles();
shader.drawTriangles(size);
window.display();
window.pollEvents();

View File

@ -110,7 +110,7 @@ void Shader::setupVertexBuffer(const RT::Vec2f* vertices, size_t size)
glBindVertexArray(_screen_VAO);
glBindBuffer(GL_ARRAY_BUFFER, _screen_VBO);
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, size * 3 * sizeof(RT::Vec2f), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
@ -119,10 +119,10 @@ void Shader::setupVertexBuffer(const RT::Vec2f* vertices, size_t size)
glBindVertexArray(0);
}
void Shader::drawTriangles(void)
void Shader::drawTriangles(size_t size)
{
glBindVertexArray(_screen_VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawArrays(GL_TRIANGLES, 0, size * 3);
}

View File

@ -12,7 +12,7 @@
#include "Window.hpp"
Window::Window(void)
Window::Window(int width, int height, const char *title, int sleep)
{
if (!glfwInit())
{
@ -24,8 +24,8 @@ Window::Window(void)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
_window = glfwCreateWindow(WIDTH, HEIGHT, "RT_GPU", NULL, NULL);
if (!_window )
_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();
@ -40,7 +40,7 @@ Window::Window(void)
glfwSetMouseButtonCallback(_window, mouseButtonCallback);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
glfwSwapInterval(sleep);
}
Window::Window(Window const &src)
{