From ee043f5c3e0afeaed74f963840dea4fbab3cb373 Mon Sep 17 00:00:00 2001 From: ZhuohaoHe Date: Wed, 24 Jul 2024 18:15:24 -0400 Subject: [PATCH] shaders-vertex_displacement init --- examples/Makefile | 3 +- examples/Makefile.Web | 3 +- .../shaders/glsl330/vertex_displacement.vs | 43 ++++++++ .../shaders/shaders_vertex_displacement.c | 104 ++++++++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 examples/shaders/resources/shaders/glsl330/vertex_displacement.vs create mode 100644 examples/shaders/shaders_vertex_displacement.c diff --git a/examples/Makefile b/examples/Makefile index be8d87204..24facdd53 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -608,7 +608,8 @@ SHADERS = \ shaders/shaders_texture_outline \ shaders/shaders_texture_tiling \ shaders/shaders_texture_waves \ - shaders/shaders_write_depth + shaders/shaders_write_depth \ + shaders/shaders_vertex_displacement AUDIO = \ audio/audio_mixed_processor \ diff --git a/examples/Makefile.Web b/examples/Makefile.Web index af3325aa5..99ee71fbf 100644 --- a/examples/Makefile.Web +++ b/examples/Makefile.Web @@ -476,7 +476,8 @@ SHADERS = \ shaders/shaders_texture_outline \ shaders/shaders_texture_tiling \ shaders/shaders_texture_waves \ - shaders/shaders_write_depth + shaders/shaders_write_depth \ + shaders/shaders_vertex_displacement \ AUDIO = \ audio/audio_mixed_processor \ diff --git a/examples/shaders/resources/shaders/glsl330/vertex_displacement.vs b/examples/shaders/resources/shaders/glsl330/vertex_displacement.vs new file mode 100644 index 000000000..3a816979e --- /dev/null +++ b/examples/shaders/resources/shaders/glsl330/vertex_displacement.vs @@ -0,0 +1,43 @@ +#version 330 + +// Input vertex attributes +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; +in vec4 vertexColor; + +// Input uniform values +uniform mat4 mvp; +uniform mat4 matModel; +uniform mat4 matNormal; + +uniform float time; + +// Output vertex attributes (to fragment shader) +out vec3 fragPosition; +out vec2 fragTexCoord; +out vec4 fragColor; +out vec3 fragNormal; + +float perlinNoise(vec2 p); + +void main() +{ + float height = perlinNoise(vertexTexCoord + vec2(time, time) * 0.01); + vec3 displacedPosition = vertexPosition + vec3(0.0, height*2.0, 0.0); + + // Send vertex attributes to fragment shader + fragPosition = vec3(matModel*vec4(displacedPosition, 1.0)); + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0))); + + // Calculate final vertex position + gl_Position = mvp*vec4(displacedPosition, 1.0); +} + +float perlinNoise(vec2 p) { + // Implement or call your Perlin noise function here + // This is a placeholder function + return fract(sin(dot(p ,vec2(12.9898,78.233))) * 43758.5453); +} diff --git a/examples/shaders/shaders_vertex_displacement.c b/examples/shaders/shaders_vertex_displacement.c new file mode 100644 index 000000000..98364ea3a --- /dev/null +++ b/examples/shaders/shaders_vertex_displacement.c @@ -0,0 +1,104 @@ +/******************************************************************************************* +* +* raylib [core] example - Basic window +* +* Example originally created with raylib 4.5, last time updated with raylib 4.5 +* +* Example contributed by (@) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2023 (@) +* +********************************************************************************************/ + +#include "raylib.h" +#include "raymath.h" + +#define RLIGHTS_IMPLEMENTATION +#include "rlights.h" + +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 +#else // PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 +#endif + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - vertex displacement"); + + Camera camera = {0}; + camera.position = (Vector3) {10.0f, 10.0f, 0.0f}; + camera.target = (Vector3) {0.0f, 0.0f, 0.0f}; + camera.up = (Vector3) {0.0f, 1.0f, 0.0f}; + camera.fovy = 60.0f; + camera.projection = CAMERA_PERSPECTIVE; + + Mesh planeMesh = GenMeshPlane(30, 30, 30, 30); + Model planeModel = LoadModelFromMesh(planeMesh); + + Shader shader = LoadShader( + TextFormat("resources/shaders/glsl%i/vertex_displacement.vs", GLSL_VERSION), + TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION) + ); + shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); + + int ambientLoc = GetShaderLocation(shader, "ambient"); + SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4); + + Light light = CreateLight(LIGHT_DIRECTIONAL, (Vector3){-10.0f, 10.0f, 10.0f}, Vector3Zero(), WHITE, shader); + + planeModel.materials[0].shader = shader; + + float time = 0.0f; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + UpdateCamera(&camera, CAMERA_FREE); + + float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; + SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3); + + time += GetFrameTime(); + SetShaderValue(shader, GetShaderLocation(shader, "time"), &time, SHADER_UNIFORM_FLOAT); + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode3D(camera); + DrawModel(planeModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, (Color) {1, 34, 47, 255}); + EndMode3D(); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + + UnloadShader(shader); + UnloadModel(planeModel); + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +}