Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/sample-scenes/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/sample-scenes/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

248 changes: 248 additions & 0 deletions examples/sample-scenes/.idea/editor.xml

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions examples/sample-scenes/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions examples/sample-scenes/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/sample-scenes/.idea/sample-scenes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/sample-scenes/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions examples/sample-scenes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
cmake_minimum_required(VERSION 3.10)
project(WeirdSamples)

# Create folders if they don't exist
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/assets")
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/include")
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/src")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Option to switch between local and online version of WeirdEngine
option(USE_LOCAL_WEIRD_ENGINE "Use local version of WeirdEngine" ON)

# Set paths for the local WeirdEngine
set(WEIRD_ENGINE_LOCAL_PATH "../../")

if(USE_LOCAL_WEIRD_ENGINE)
# Use the local version of WeirdEngine
message(STATUS "Using local version of WeirdEngine")
add_subdirectory(${WEIRD_ENGINE_LOCAL_PATH} ${CMAKE_BINARY_DIR}/weird-engine)
else()
# Fetch WeirdEngine from GitHub
message(STATUS "Using online version of WeirdEngine")
include(FetchContent)
FetchContent_Declare(
WeirdEngine
GIT_REPOSITORY https://github.com/damacaa/weird-engine
GIT_TAG main
)
FetchContent_MakeAvailable(WeirdEngine)
endif()

# Glob source files and header files from the proper directories.
file(GLOB_RECURSE WEIRDGAME_SOURCES "${CMAKE_SOURCE_DIR}/src/*.cpp")
file(GLOB_RECURSE WEIRDGAME_HEADERS
"${CMAKE_SOURCE_DIR}/include/*.h"
"${CMAKE_SOURCE_DIR}/include/*.hpp"
)
file(GLOB_RECURSE WEIRDGAME_ASSETS
"${CMAKE_SOURCE_DIR}/assets/*.*"
)

# Mark headers as header-only so that Visual Studio treats them appropriately.
set_source_files_properties(${WEIRDGAME_HEADERS} PROPERTIES HEADER_FILE_ONLY TRUE)

# Add executable including both sources and headers.
add_executable(${PROJECT_NAME} ${WEIRDGAME_SOURCES} ${WEIRDGAME_HEADERS} ${WEIRDGAME_ASSETS})

# Set include directories.
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/include)

# Link to WeirdEngine.
target_link_libraries(${PROJECT_NAME} PRIVATE WeirdEngine)

# Set Asset Path Macro
# Define the assets path variable
set(ASSETS_PATH "${CMAKE_CURRENT_SOURCE_DIR}/assets/")
# set(ASSETS_PATH "./assets") # Set the asset path macro in release mode to a relative path that assumes the assets folder is in the same directory as the game executable

# Print the assets path for debugging or confirmation
message(STATUS "Assets path: ${ASSETS_PATH}")

# Use the variable in target_compile_definitions
target_compile_definitions(${PROJECT_NAME} PUBLIC ASSETS_PATH="${ASSETS_PATH}")

# Example for the future with more libraries
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
# Command for SDL3
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:SDL3::SDL3>
$<TARGET_FILE_DIR:${PROJECT_NAME}>

# Command for SDL3_image
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# $<TARGET_FILE:SDL3_image::SDL3_image> #<-- Note the target name might be different
# $<TARGET_FILE_DIR:${PROJECT_NAME}>

COMMENT "Copying runtime DLLs to executable directory"
)

# ----------------------------------------------------------
# Helper function to assign source groups based on folder structure.
function(assign_source_groups TARGET)
foreach(source_file IN LISTS ARGN)
# Get the full path to the file's directory.
get_filename_component(FILE_PATH "${source_file}" PATH)
# Compute the path relative to the project's root.
file(RELATIVE_PATH REL_PATH "${CMAKE_SOURCE_DIR}" "${FILE_PATH}")
# Replace forward slashes with backslashes for Visual Studio filter naming.
string(REPLACE "/" "\\" FILTER_PATH "${REL_PATH}")
if(FILTER_PATH STREQUAL "")
set(FILTER_PATH "Root")
endif()
# Assign the file to the computed filter.
source_group("${FILTER_PATH}" FILES "${source_file}")
endforeach()
endfunction()

# Assign source groups for all your files.
assign_source_groups(${PROJECT_NAME} ${WEIRDGAME_SOURCES} ${WEIRDGAME_HEADERS} ${WEIRDGAME_ASSETS})
Binary file added examples/sample-scenes/assets/fire/fire.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/sample-scenes/assets/fire/flame.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/sample-scenes/assets/fire/flame.xcf
Binary file not shown.
Binary file added examples/sample-scenes/assets/fire/flame2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions examples/sample-scenes/assets/fire/shaders/fireParticles.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#version 330 core

out vec4 FragColor;

// Inputs from vertex shader
in vec3 v_worldPos;
in vec3 v_normal;
in vec3 v_color;
in vec2 v_texCoord;

uniform float u_time;

void main()
{
FragColor = vec4(vec3(1.75), 1.0);
}
50 changes: 50 additions & 0 deletions examples/sample-scenes/assets/fire/shaders/fireParticles.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#version 330 core

// Vertex attributes
layout (location = 0) in vec3 in_position;
layout (location = 1) in vec3 in_normal;
layout (location = 2) in vec3 in_color;
layout (location = 3) in vec2 in_texCoord;

// Outputs to fragment shader
out vec3 v_worldPos;
out vec3 v_normal;
out vec3 v_color;
out vec2 v_texCoord;

// Uniforms
uniform mat4 u_model;
uniform mat4 u_camMatrix;
uniform mat3 u_normalMatrix;

uniform float u_time;

// ChatGPT: I want a function that, for a given x, gros linearly but then plateaus
float plateauFunction(float x, float maxValue, float growthRate)
{
return maxValue * (1.0 - exp(-growthRate * x));
}

void main()
{
// Animation delta
float delta = fract((10.0 * gl_InstanceID * 3.14) + (1.1 * u_time));

float maxHeight = 1.5 + (fract(gl_InstanceID * 123.45678) - 0.5); // Constant + random offset
float y = plateauFunction(2.0 * delta, maxHeight, 0.8);

float distanceToCenterXZ = 0.1 + (0.3 * smoothstep(0, 1, sqrt(1.5 * delta))); // TODO: find an alternative to sqrt

vec3 offset = vec3(
distanceToCenterXZ * sin(u_time + gl_InstanceID),
y,
distanceToCenterXZ * cos(u_time + gl_InstanceID)
);

v_worldPos = vec3(u_model * vec4(2.0 * (1.0 - delta * delta) * in_position, 1.0)) + offset;
v_normal = u_normalMatrix * in_normal;
v_color = in_color;
v_texCoord = in_texCoord;

gl_Position = u_camMatrix * vec4(v_worldPos, 1.0);
}
72 changes: 72 additions & 0 deletions examples/sample-scenes/assets/fire/shaders/flame.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#version 330 core

out vec4 FragColor;

// Inputs from vertex shader
in vec3 v_worldPos;
in vec3 v_normal;
in vec3 v_color;
in vec2 v_texCoord;


uniform float u_time;

uniform sampler2D t_noise;
uniform sampler2D t_flameShape;

const int NUM_STOPS = 4;
uniform vec3 colors[NUM_STOPS] = vec3[](
vec3(0.1, 0.1, 0.1), // Grey
vec3(0.8, 0.3, 0.2), // Red
vec3(1.1, 0.7, 0.2), // Orange
vec3(1.2, 1.2, 1.2) // White
);

uniform float stops[NUM_STOPS] = float[](0.0, 0.7, 0.9, 1.0);

vec3 getGradientColor(float t)
{
// This could be replace with a texture, but this approach makes it easier to iterate and choose colors
for (int i = 0; i < NUM_STOPS - 1; ++i) {
if (t >= stops[i] && t <= stops[i+1]) {
float localT = (t - stops[i]) / (stops[i+1] - stops[i]);
return mix(colors[i], colors[i+1], localT);
}
}

return colors[NUM_STOPS - 1]; // fallback
}


void main()
{
vec2 uv = v_texCoord;

// Noise0
float noise0 = texture(t_noise, fract((3.0f * uv) - vec2(0.0f, u_time))).x - 0.5f;

// Noise1
float noise1 = texture(t_noise, fract((2.0 * uv) - vec2(0.0f, 1.2f * u_time))).x - 0.5f;

// Combine all noise
float sumNoise = noise0 + noise1;

// Reduce noise at the bottom of the flame
float noiseMask = clamp(3.0f * (v_texCoord.y - 0.35f), 0, 1);
sumNoise *= noiseMask;

// Reduce overall noise strength
float noiseIntensity = 0.1f;
sumNoise *= noiseIntensity;

// Sample texture with noise applied to uv coordinates
float alpha = texture(t_flameShape, clamp(uv + sumNoise, 0, 1)).x;

alpha = smoothstep(.3, 1.0, alpha);
alpha = alpha * (alpha + 0.01f);
alpha = clamp(alpha, 0.0, 1.0);

FragColor = vec4(getGradientColor(alpha), alpha);
// FragColor = vec4(vec3(sumNoise / noiseIntensity), 1.0f); // Debug noise
// FragColor = vec4(vec3(alpha), 1.0f); // Debug alpha
}
53 changes: 53 additions & 0 deletions examples/sample-scenes/assets/fire/shaders/heatDistortion.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#version 330 core

out vec4 FragColor;

// Inputs from vertex shader
in vec3 v_worldPos;
in vec3 v_normal;
in vec3 v_color;
in vec2 v_texCoord;

uniform sampler2D t_texture;
uniform sampler2D t_noise;
uniform sampler2D t_flameShape;

uniform float u_time = 0.1f;
uniform vec2 u_resolution;

// Fog calculations used to modulate distortion with distance
float linearDepth(float z, float near, float far)
{
return (2.0 * near) / (far + near - z * (far - near));
}

void main()
{
// Mesh uv to sample mask texture
vec2 uv = v_texCoord;
// Screen uv to sample scene texture
vec2 screenUV = gl_FragCoord.xy / u_resolution;

// Use the flame shape to mask the noise
float bottomFade = texture(t_flameShape, vec2(uv.x, min(uv.y, 0.5))).r;
bottomFade = smoothstep(0.5, 1.0, bottomFade);

// Fade at the top
float topFade = 1.0 - uv.y;
bottomFade *= topFade;

// Blur is less strong when its farther from the camera, same math as fog
float depthMask = max(1.0 - (linearDepth(gl_FragCoord.z, 0.1, 100.0) * 1.5), 0.0);

// Reduce strength when the flame is looking up
float dotWorldY = 1.0f - abs(dot(normalize(v_normal), vec3(0, 1, 0)));

// Combine all masks
float mask = bottomFade * depthMask * dotWorldY;

// Calculate scene texture uv offset
vec2 offset = vec2(0, mask * 0.1f * ((texture(t_noise, (2.0f * uv) - (1.0f * u_time)).x) - 0.5));

FragColor = texture(t_texture, screenUV + offset);
// FragColor = vec4(vec3(mask), 1.0); // Debug mask
}
25 changes: 25 additions & 0 deletions examples/sample-scenes/assets/fire/shaders/smokeParticles.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#version 330 core

out vec4 FragColor;

// Inputs from vertex shader
in vec3 v_worldPos;
in vec3 v_normal;
in vec3 v_color;
in vec2 v_texCoord;
in float v_delta;

uniform float u_time;
uniform vec3 u_smokeColor = vec3(0.12);

void main()
{
// Distance to center generates a circular gradient
float d = 2.0 * length(v_texCoord - vec2(0.5));
float alpha = max(0.0, 1.0 - d);

// Animate alpha
alpha *= pow(1.0 - v_delta, 2.0);

FragColor = vec4(u_smokeColor * (1.0 - v_delta), 0.75 * alpha);
}
Loading
Loading