shaderss
This commit is contained in:
parent
3bfe34eee0
commit
5a17615729
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -9,7 +9,9 @@
|
|||||||
"texture.h": "c",
|
"texture.h": "c",
|
||||||
"libft.h": "c",
|
"libft.h": "c",
|
||||||
"render.h": "c",
|
"render.h": "c",
|
||||||
"math.h": "c"
|
"math.h": "c",
|
||||||
|
"mlx42_int.h": "c",
|
||||||
|
"glad.h": "c"
|
||||||
},
|
},
|
||||||
"cmake.ignoreCMakeListsMissing": true
|
"cmake.ignoreCMakeListsMissing": true
|
||||||
}
|
}
|
||||||
18
Makefile
18
Makefile
@ -1,12 +1,12 @@
|
|||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
# #
|
# #
|
||||||
# :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: whaffman <whaffman@student.codam.nl> +#+ #
|
# By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ #
|
||||||
# +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
|
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
|
||||||
# Updated: 2025/05/20 21:27:02 by whaffman ######## odam.nl #
|
# Updated: 2025/05/22 16:48:29 by qmennen ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -24,13 +24,15 @@ LIBFT_INC_PATH = $(LIBFT_PATH)/inc
|
|||||||
LIBFT = $(LIBFT_PATH)/libft.a
|
LIBFT = $(LIBFT_PATH)/libft.a
|
||||||
|
|
||||||
MLX42_PATH = $(LIB_PATH)/MLX42
|
MLX42_PATH = $(LIB_PATH)/MLX42
|
||||||
|
MLX42_TOP_INCLUDE = $(MLX42_PATH)/include
|
||||||
MLX42_INC_PATH = $(MLX42_PATH)/include/MLX42
|
MLX42_INC_PATH = $(MLX42_PATH)/include/MLX42
|
||||||
|
GLAD_INC_PATH = $(MLX42_PATH)/include/glad
|
||||||
MLX42 = $(MLX42_PATH)/build/libmlx42.a
|
MLX42 = $(MLX42_PATH)/build/libmlx42.a
|
||||||
|
|
||||||
CC = cc
|
CC = cc
|
||||||
RM = rm -rf
|
RM = rm -rf
|
||||||
|
|
||||||
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -I./$(MLX42_INC_PATH)
|
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -I./$(MLX42_INC_PATH) -I./$(GLAD_INC_PATH) -I./$(MLX42_TOP_INCLUDE)
|
||||||
|
|
||||||
UNAME_S := $(shell uname -s)
|
UNAME_S := $(shell uname -s)
|
||||||
ifeq ($(UNAME_S),Linux)
|
ifeq ($(UNAME_S),Linux)
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.4 MiB After Width: | Height: | Size: 761 KiB |
Binary file not shown.
67
assets/shaders/frag.glsl
Normal file
67
assets/shaders/frag.glsl
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#version 330 core
|
||||||
|
|
||||||
|
in vec2 TexCoord;
|
||||||
|
flat in int TexIndex;
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform sampler2D Texture0;
|
||||||
|
uniform sampler2D Texture1;
|
||||||
|
uniform sampler2D Texture2;
|
||||||
|
uniform sampler2D Texture3;
|
||||||
|
|
||||||
|
uniform float u_time;
|
||||||
|
uniform vec2 u_resolution;
|
||||||
|
|
||||||
|
float rand(vec2 co) {
|
||||||
|
return fract(sin(dot(co.xy, vec2(12.9898,78.233))) * 43758.5453);
|
||||||
|
}
|
||||||
|
|
||||||
|
float hash(vec2 p) {
|
||||||
|
p = fract(p * vec2(123.34, 456.21));
|
||||||
|
p += dot(p, p + 45.32);
|
||||||
|
return fract(p.x * p.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 texColor;
|
||||||
|
switch (TexIndex) {
|
||||||
|
case 0: texColor = texture(Texture0, TexCoord); break;
|
||||||
|
case 1: texColor = texture(Texture1, TexCoord); break;
|
||||||
|
case 2: texColor = texture(Texture2, TexCoord); break;
|
||||||
|
case 3: texColor = texture(Texture3, TexCoord); break;
|
||||||
|
default: texColor = vec4(1.0, 0.0, 0.0, 1.0); break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TexIndex == 1) {
|
||||||
|
vec2 uv = gl_FragCoord.xy / u_resolution.xy;
|
||||||
|
uv.x *= u_resolution.x / u_resolution.y;
|
||||||
|
float blockSize = 8.0;
|
||||||
|
|
||||||
|
vec2 blockUV = floor(gl_FragCoord.xy / blockSize);
|
||||||
|
float noise = hash(blockUV + vec2(u_time * 3.0, u_time * 7.0));
|
||||||
|
float noise2 = hash(blockUV + vec2(u_time * 13.0, u_time * 3.0));
|
||||||
|
float noise3 = hash(blockUV + vec2(u_time * 17.0, u_time * 13.0));
|
||||||
|
|
||||||
|
// Horizontal bands
|
||||||
|
noise *= 0.9 + 0.1 * sin(uv.y * 400.0);
|
||||||
|
noise2 *= 0.9 + 0.1 * sin(uv.y * 400.0);
|
||||||
|
noise3 *= 0.9 + 0.1 * sin(uv.y * 400.0);
|
||||||
|
|
||||||
|
float offset = 0.01;
|
||||||
|
float r = texture(Texture1, TexCoord + vec2(offset, 0.0)).r;
|
||||||
|
float g = texture(Texture1, TexCoord).g;
|
||||||
|
float b = texture(Texture1, TexCoord - vec2(offset, 0.0)).b;
|
||||||
|
vec3 color = vec3(r, g, b);
|
||||||
|
|
||||||
|
float strength = 0.4;
|
||||||
|
vec3 noisyColor = mix(color.rgb, vec3(noise, noise2, noise3), strength);
|
||||||
|
// float flicker = 0.75 + sin(u_time * 6.0 + sin(u_time * 3)) * 0.25;
|
||||||
|
float flicker = 0.75 + 0.25 * sin(u_time + sin(u_time * 7.0));
|
||||||
|
|
||||||
|
FragColor = vec4(noisyColor, texColor.a * flicker);
|
||||||
|
} else {
|
||||||
|
FragColor = texColor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
16
assets/shaders/vert.glsl
Normal file
16
assets/shaders/vert.glsl
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout(location = 0) in vec3 aPos;
|
||||||
|
layout(location = 1) in vec2 aTexCoord;
|
||||||
|
layout(location = 2) in int aTexIndex;
|
||||||
|
|
||||||
|
out vec2 TexCoord;
|
||||||
|
flat out int TexIndex;
|
||||||
|
uniform mat4 ProjMatrix;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
// Add view bobbing
|
||||||
|
gl_Position = ProjMatrix * vec4(aPos, 1.0);
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
TexIndex = aTexIndex;
|
||||||
|
}
|
||||||
@ -6,11 +6,13 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/05/22 14:03:26 by qmennen ### ########.fr */
|
/* Updated: 2025/05/22 17:48:48 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "cub3d.h"
|
#include "cub3d.h"
|
||||||
|
#include "glad.h"
|
||||||
|
#include "MLX42_Int.h"
|
||||||
|
|
||||||
int game_create(t_game **game)
|
int game_create(t_game **game)
|
||||||
{
|
{
|
||||||
@ -39,13 +41,17 @@ void free_game(t_game **game)
|
|||||||
void game_loop(void *param)
|
void game_loop(void *param)
|
||||||
{
|
{
|
||||||
t_game *game;
|
t_game *game;
|
||||||
|
mlx_ctx_t *ctx;
|
||||||
double delta_time;
|
double delta_time;
|
||||||
static int fps = 0;
|
static int fps = 0;
|
||||||
|
|
||||||
game = (t_game *)param;
|
game = (t_game *)param;
|
||||||
|
ctx = (mlx_ctx_t *)game->screen->mlx->context;
|
||||||
game->framecount++;
|
game->framecount++;
|
||||||
delta_time = game->screen->mlx->delta_time;
|
delta_time = game->screen->mlx->delta_time;
|
||||||
fps += (int)(1.f / delta_time);
|
fps += (int)(1.f / delta_time);
|
||||||
|
glUniform1f(glGetUniformLocation(ctx->shaderprogram, "u_time"), glfwGetTime());
|
||||||
|
glUniform2f(glGetUniformLocation(ctx->shaderprogram, "u_resolution"), game->screen->width, game->screen->height);
|
||||||
if (game->framecount % 20 == 0)
|
if (game->framecount % 20 == 0)
|
||||||
{
|
{
|
||||||
game->fps = (int)(fps / 20);
|
game->fps = (int)(fps / 20);
|
||||||
|
|||||||
@ -6,11 +6,12 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
|
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/05/22 14:06:24 by qmennen ### ########.fr */
|
/* Updated: 2025/05/22 17:29:23 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "cub3d.h"
|
#include "cub3d.h"
|
||||||
|
#include "glad.h"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -20,13 +21,13 @@ int main(int argc, char **argv)
|
|||||||
game = NULL;
|
game = NULL;
|
||||||
if (!valid_arguments(argc, argv))
|
if (!valid_arguments(argc, argv))
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
|
shader_init(&game);
|
||||||
if (!initialize_cub3d(&game, argv[1]))
|
if (!initialize_cub3d(&game, argv[1]))
|
||||||
{
|
{
|
||||||
perror("initialize_cub3d");
|
perror("initialize_cub3d");
|
||||||
game_free(game);
|
game_free(game);
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
perror("after init");
|
|
||||||
mlx_loop(game->screen->mlx);
|
mlx_loop(game->screen->mlx);
|
||||||
game_terminate(game);
|
game_terminate(game);
|
||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
|
|||||||
14
src/screen.c
14
src/screen.c
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* ::: :::::::: */
|
||||||
/* screen.c :+: :+: */
|
/* screen.c :+: :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: whaffman <whaffman@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
|
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/05/14 20:19:21 by whaffman ######## odam.nl */
|
/* Updated: 2025/05/22 16:56:31 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
56
src/shader/shaders.c
Normal file
56
src/shader/shaders.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* shaders.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/05/08 18:27:59 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/05/22 16:32:21 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
# include "cub3d.h"
|
||||||
|
# include "glad.h"
|
||||||
|
|
||||||
|
extern char *vert_shader;
|
||||||
|
extern char *frag_shader;
|
||||||
|
|
||||||
|
char *read_shader(int type)
|
||||||
|
{
|
||||||
|
char *shader;
|
||||||
|
char *target;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (type == 1)
|
||||||
|
target = "./assets/shaders/vert.glsl";
|
||||||
|
else
|
||||||
|
target = "./assets/shaders/frag.glsl";
|
||||||
|
fd = open(target, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
perror("Error opening shader file");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
shader = malloc(4096);
|
||||||
|
if (!shader)
|
||||||
|
{
|
||||||
|
perror("Error allocating memory for shader");
|
||||||
|
close(fd);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
read(fd, shader, 4096);
|
||||||
|
close(fd);
|
||||||
|
return (shader);
|
||||||
|
}
|
||||||
|
|
||||||
|
int shader_init(t_game **game)
|
||||||
|
{
|
||||||
|
vert_shader = read_shader(1);
|
||||||
|
if (!vert_shader)
|
||||||
|
return (FAILURE);
|
||||||
|
frag_shader = read_shader(2);
|
||||||
|
if (!frag_shader)
|
||||||
|
return (FAILURE);
|
||||||
|
return (SUCCESS);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user