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);
|
||||
}
|
||||
|
|
@ -13,31 +13,30 @@ uniform mat4 matNormal;
|
|||
|
||||
uniform float time;
|
||||
|
||||
uniform sampler2D perlinNoiseMap;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec3 fragPosition;
|
||||
out vec2 fragTexCoord;
|
||||
out vec4 fragColor;
|
||||
out vec3 fragNormal;
|
||||
|
||||
float perlinNoise(vec2 p);
|
||||
out float height;
|
||||
|
||||
void main()
|
||||
{
|
||||
float height = perlinNoise(vertexTexCoord + vec2(time, time) * 0.01);
|
||||
vec3 displacedPosition = vertexPosition + vec3(0.0, height*2.0, 0.0);
|
||||
vec2 animatedTexCoord = sin(vertexTexCoord + vec2(sin(time + vertexPosition.x * 0.1), cos(time + vertexPosition.z * 0.1)) * 0.3) * 0.5 + 0.5;
|
||||
|
||||
// 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
|
||||
fragPosition = vec3(matModel*vec4(displacedPosition, 1.0));
|
||||
fragTexCoord = vertexTexCoord;
|
||||
fragColor = vertexColor;
|
||||
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
|
||||
height = displacedPosition.y * 0.2;
|
||||
|
||||
// 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);
|
||||
gl_Position = mvp*vec4(displacedPosition , 1.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@
|
|||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "rlgl.h"
|
||||
|
||||
#include <printf.h>
|
||||
|
||||
#define RLIGHTS_IMPLEMENTATION
|
||||
#include "rlights.h"
|
||||
|
|
@ -38,25 +41,34 @@ int main(void)
|
|||
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - vertex displacement");
|
||||
|
||||
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.up = (Vector3) {0.0f, 1.0f, 0.0f};
|
||||
camera.fovy = 60.0f;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
Mesh planeMesh = GenMeshPlane(30, 30, 30, 30);
|
||||
Mesh planeMesh = GenMeshPlane(50, 50, 50, 50);
|
||||
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)
|
||||
TextFormat("resources/shaders/glsl%i/vertex_displacement.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);
|
||||
Image perlinNoiseImage = GenImagePerlinNoise(512, 512, 0, 0, 1.0f);
|
||||
Texture perlinNoiseMap = LoadTextureFromImage(perlinNoiseImage);
|
||||
UnloadImage(perlinNoiseImage);
|
||||
|
||||
int perlinNoiseMapLoc = GetShaderLocation(shader, "perlinNoiseMap");
|
||||
rlEnableShader(shader.id);
|
||||
rlActiveTextureSlot(1);
|
||||
rlEnableTexture(perlinNoiseMap.id);
|
||||
rlSetUniformSampler(perlinNoiseMapLoc, 1);
|
||||
|
||||
|
||||
planeModel.materials[0].shader = shader;
|
||||
|
||||
|
|
@ -84,9 +96,16 @@ int main(void)
|
|||
ClearBackground(RAYWHITE);
|
||||
|
||||
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();
|
||||
|
||||
DrawText("Displacement mapping", 10, 10, 20, DARKGRAY);
|
||||
DrawFPS(10, 40);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user