implement simulation of wave in ocean
This commit is contained in:
parent
ee043f5c3e
commit
97b154960e
|
|
@ -0,0 +1,17 @@
|
||||||
|
#version 330
|
||||||
|
|
||||||
|
// Input vertex attributes (from vertex shader)
|
||||||
|
in vec2 fragTexCoord;
|
||||||
|
in float height;
|
||||||
|
|
||||||
|
// Output fragment color
|
||||||
|
out vec4 finalColor;
|
||||||
|
|
||||||
|
// NOTE: Add here your custom variables
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 darkblue = vec4(0.0, 0.13, 0.18, 1.0);
|
||||||
|
vec4 lightblue = vec4(1.0, 1.0, 1.0, 1.0);
|
||||||
|
finalColor = mix(darkblue, lightblue, height);
|
||||||
|
}
|
||||||
|
|
@ -11,33 +11,32 @@ uniform mat4 mvp;
|
||||||
uniform mat4 matModel;
|
uniform mat4 matModel;
|
||||||
uniform mat4 matNormal;
|
uniform mat4 matNormal;
|
||||||
|
|
||||||
uniform float time;
|
uniform float time;
|
||||||
|
|
||||||
|
uniform sampler2D perlinNoiseMap;
|
||||||
|
|
||||||
// Output vertex attributes (to fragment shader)
|
// Output vertex attributes (to fragment shader)
|
||||||
out vec3 fragPosition;
|
out vec3 fragPosition;
|
||||||
out vec2 fragTexCoord;
|
out vec2 fragTexCoord;
|
||||||
out vec4 fragColor;
|
|
||||||
out vec3 fragNormal;
|
out vec3 fragNormal;
|
||||||
|
out float height;
|
||||||
float perlinNoise(vec2 p);
|
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
float height = perlinNoise(vertexTexCoord + vec2(time, time) * 0.01);
|
vec2 animatedTexCoord = sin(vertexTexCoord + vec2(sin(time + vertexPosition.x * 0.1), cos(time + vertexPosition.z * 0.1)) * 0.3) * 0.5 + 0.5;
|
||||||
vec3 displacedPosition = vertexPosition + vec3(0.0, height*2.0, 0.0);
|
|
||||||
|
// Fetch displacement from the displacement map and amplify it
|
||||||
|
float displacement = texture(perlinNoiseMap, animatedTexCoord).r * 7; // Amplified displacement
|
||||||
|
|
||||||
|
// Combine displacement and wave motion
|
||||||
|
vec3 displacedPosition = vertexPosition + vec3(0.0, displacement, 0.0);
|
||||||
|
|
||||||
// Send vertex attributes to fragment shader
|
// Send vertex attributes to fragment shader
|
||||||
fragPosition = vec3(matModel*vec4(displacedPosition, 1.0));
|
fragPosition = vec3(matModel*vec4(displacedPosition, 1.0));
|
||||||
fragTexCoord = vertexTexCoord;
|
fragTexCoord = vertexTexCoord;
|
||||||
fragColor = vertexColor;
|
|
||||||
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
|
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
|
||||||
|
height = displacedPosition.y * 0.2;
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvp*vec4(displacedPosition, 1.0);
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,9 @@
|
||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include "raymath.h"
|
#include "raymath.h"
|
||||||
|
#include "rlgl.h"
|
||||||
|
|
||||||
|
#include <printf.h>
|
||||||
|
|
||||||
#define RLIGHTS_IMPLEMENTATION
|
#define RLIGHTS_IMPLEMENTATION
|
||||||
#include "rlights.h"
|
#include "rlights.h"
|
||||||
|
|
@ -38,28 +41,37 @@ int main(void)
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - vertex displacement");
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - vertex displacement");
|
||||||
|
|
||||||
Camera camera = {0};
|
Camera camera = {0};
|
||||||
camera.position = (Vector3) {10.0f, 10.0f, 0.0f};
|
camera.position = (Vector3) {20.0f, 5.0f, -20.0f};
|
||||||
camera.target = (Vector3) {0.0f, 0.0f, 0.0f};
|
camera.target = (Vector3) {0.0f, 0.0f, 0.0f};
|
||||||
camera.up = (Vector3) {0.0f, 1.0f, 0.0f};
|
camera.up = (Vector3) {0.0f, 1.0f, 0.0f};
|
||||||
camera.fovy = 60.0f;
|
camera.fovy = 60.0f;
|
||||||
camera.projection = CAMERA_PERSPECTIVE;
|
camera.projection = CAMERA_PERSPECTIVE;
|
||||||
|
|
||||||
Mesh planeMesh = GenMeshPlane(30, 30, 30, 30);
|
Mesh planeMesh = GenMeshPlane(50, 50, 50, 50);
|
||||||
Model planeModel = LoadModelFromMesh(planeMesh);
|
Model planeModel = LoadModelFromMesh(planeMesh);
|
||||||
|
|
||||||
Shader shader = LoadShader(
|
Shader shader = LoadShader(
|
||||||
TextFormat("resources/shaders/glsl%i/vertex_displacement.vs", GLSL_VERSION),
|
TextFormat("resources/shaders/glsl%i/vertex_displacement.vs", GLSL_VERSION),
|
||||||
TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION)
|
TextFormat("resources/shaders/glsl%i/vertex_displacement.fs", GLSL_VERSION)
|
||||||
);
|
);
|
||||||
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
|
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
|
||||||
|
|
||||||
int ambientLoc = GetShaderLocation(shader, "ambient");
|
int ambientLoc = GetShaderLocation(shader, "ambient");
|
||||||
SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
|
SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
|
||||||
|
|
||||||
|
Image perlinNoiseImage = GenImagePerlinNoise(512, 512, 0, 0, 1.0f);
|
||||||
|
Texture perlinNoiseMap = LoadTextureFromImage(perlinNoiseImage);
|
||||||
|
UnloadImage(perlinNoiseImage);
|
||||||
|
|
||||||
Light light = CreateLight(LIGHT_DIRECTIONAL, (Vector3){-10.0f, 10.0f, 10.0f}, Vector3Zero(), WHITE, shader);
|
int perlinNoiseMapLoc = GetShaderLocation(shader, "perlinNoiseMap");
|
||||||
|
rlEnableShader(shader.id);
|
||||||
|
rlActiveTextureSlot(1);
|
||||||
|
rlEnableTexture(perlinNoiseMap.id);
|
||||||
|
rlSetUniformSampler(perlinNoiseMapLoc, 1);
|
||||||
|
|
||||||
|
|
||||||
planeModel.materials[0].shader = shader;
|
planeModel.materials[0].shader = shader;
|
||||||
|
|
||||||
float time = 0.0f;
|
float time = 0.0f;
|
||||||
|
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
|
|
@ -84,9 +96,16 @@ int main(void)
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
DrawModel(planeModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, (Color) {1, 34, 47, 255});
|
|
||||||
|
BeginShaderMode(shader);
|
||||||
|
DrawModel(planeModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, (Color) {255, 255, 255, 255});
|
||||||
|
EndShaderMode();
|
||||||
|
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
|
|
||||||
|
DrawText("Displacement mapping", 10, 10, 20, DARKGRAY);
|
||||||
|
DrawFPS(10, 40);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user