Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cp/lavanode #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Binary file added bin/resources/realisticWater/data/waterbump.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions bin/resources/realisticWater/shaders/Water_ps.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2013, elvman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY elvman ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL elvman BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

const float LOG2 = 1.442695;

uniform vec3 CameraPosition; // Position of main position
uniform float WaveHeight;

uniform vec4 WaterColor;
uniform float ColorBlendFactor;

uniform sampler2D WaterBump; //coverage
uniform sampler2D RefractionMap; //coverage
uniform sampler2D ReflectionMap; //coverage

uniform bool FogEnabled;
uniform int FogMode;

varying vec2 bumpMapTexCoord;
varying vec3 refractionMapTexCoord;
varying vec3 reflectionMapTexCoord;
varying vec3 position3D;

void main()
{
//bump color
vec4 bumpColor = texture2D(WaterBump, bumpMapTexCoord);
vec2 perturbation = WaveHeight * (bumpColor.rg - 0.5);

//refraction
vec2 ProjectedRefractionTexCoords = clamp(refractionMapTexCoord.xy / refractionMapTexCoord.z + perturbation, 0.0, 1.0);
//calculate final refraction color
vec4 refractiveColor = texture2D(RefractionMap, ProjectedRefractionTexCoords );

//reflection
vec2 ProjectedReflectionTexCoords = clamp(reflectionMapTexCoord.xy / reflectionMapTexCoord.z + perturbation, 0.0, 0.1);
//calculate final reflection color
vec4 reflectiveColor = texture2D(ReflectionMap, ProjectedReflectionTexCoords );

//fresnel
vec3 eyeVector = normalize(CameraPosition - position3D);
vec3 upVector = vec3(0.0, 1.0, 0.0);

//fresnel can not be lower than 0
float fresnelTerm = max( dot(eyeVector, upVector), 0.0 );

float fogFactor = 1.0;

if (FogEnabled)
{
float z = gl_FragCoord.z / gl_FragCoord.w;

if (FogMode == 1) //exp
{
float fogFactor = exp2(-gl_Fog.density * z * LOG2);
fogFactor = clamp(fogFactor, 0.0, 1.0);
}
else if (FogMode == 0) //linear
{
fogFactor = (gl_Fog.end - z) / (gl_Fog.end - gl_Fog.start);
}
else if (FogMode == 2) //exp2
{
float fogFactor = exp2(-gl_Fog.density * gl_Fog.density * z * z * LOG2);
fogFactor = clamp(fogFactor, 0.0, 1.0);
}
}

vec4 combinedColor = refractiveColor * fresnelTerm + reflectiveColor * (1.0 - fresnelTerm);

vec4 finalColor = ColorBlendFactor * WaterColor + (1.0 - ColorBlendFactor) * combinedColor;

gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor );
}

83 changes: 83 additions & 0 deletions bin/resources/realisticWater/shaders/Water_ps.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2013, elvman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY elvman ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL elvman BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

float3 CameraPosition; // Position of main position
float WaveHeight;

float4 WaterColor;
float ColorBlendFactor;

sampler2D WaterBump; //coverage
sampler2D RefractionMap; //coverage
sampler2D ReflectionMap; //coverage

// Pixel shader output structure
struct PS_OUTPUT
{
float4 color : COLOR0; // Pixel color
};

struct PS_INPUT
{
float4 position : POSITION; // vertex position

float2 bumpMapTexCoord : TEXCOORD0;
float3 refractionMapTexCoord : TEXCOORD1;
float3 reflectionMapTexCoord : TEXCOORD2;

float3 position3D : TEXCOORD3;
};

PS_OUTPUT main( PS_INPUT input )
{
PS_OUTPUT output;

//bump color
float4 bumpColor = tex2D(WaterBump, input.bumpMapTexCoord);
float2 perturbation = WaveHeight * (bumpColor.rg - 0.5);

//refraction
float2 ProjectedRefractionTexCoords = saturate(input.refractionMapTexCoord.xy / input.refractionMapTexCoord.z + perturbation);
//calculate final refraction color
float4 refractiveColor = tex2D(RefractionMap, ProjectedRefractionTexCoords );

//reflection
float2 ProjectedReflectionTexCoords = saturate(input.reflectionMapTexCoord.xy / input.reflectionMapTexCoord.z + perturbation);
//calculate final reflection color
float4 reflectiveColor = tex2D(ReflectionMap, ProjectedReflectionTexCoords );

//fresnel
float3 eyeVector = normalize(CameraPosition - input.position3D);
float3 upVector = float3(0.0, 1.0, 0.0);

//fresnel can not be lower than 0
float fresnelTerm = max( dot(eyeVector, upVector), 0.0 );

float4 combinedColor = refractiveColor * fresnelTerm + reflectiveColor * (1.0 - fresnelTerm);

output.color = ColorBlendFactor * WaterColor + (1.0 - ColorBlendFactor) * combinedColor;

return output;
}

65 changes: 65 additions & 0 deletions bin/resources/realisticWater/shaders/Water_vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2013, elvman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY elvman ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL elvman BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//uniform mat4 View;
uniform mat4 WorldViewProj; // World * View * Projection transformation
uniform mat4 WorldReflectionViewProj; // World * Reflection View * Projection transformation

uniform float WaveLength;

uniform float Time;
uniform float WindForce;
uniform vec2 WindDirection;

// Vertex shader output structure
varying vec2 bumpMapTexCoord;
varying vec3 refractionMapTexCoord;
varying vec3 reflectionMapTexCoord;
varying vec3 position3D;

void main()
{
//color = gl_Color;

// transform position to clip space
vec4 pos = WorldViewProj * gl_Vertex;
gl_Position = pos;

// calculate vawe coords
bumpMapTexCoord = gl_MultiTexCoord0.xy / WaveLength + Time * WindForce * WindDirection;

// refraction texcoords
refractionMapTexCoord.x = 0.5 * (pos.w + pos.x);
refractionMapTexCoord.y = 0.5 * (pos.w + pos.y);
refractionMapTexCoord.z = pos.w;

// reflection texcoords
pos = WorldReflectionViewProj * gl_Vertex;
reflectionMapTexCoord.x = 0.5 * (pos.w + pos.x);
reflectionMapTexCoord.y = 0.5 * (pos.w + pos.y);
reflectionMapTexCoord.z = pos.w;

// position of the vertex
position3D = gl_Vertex.xyz;
}
80 changes: 80 additions & 0 deletions bin/resources/realisticWater/shaders/Water_vs.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2013, elvman
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY elvman ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL elvman BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//float4x4 View;
float4x4 WorldViewProj; // World * View * Projection transformation
float4x4 WorldReflectionViewProj; // World * Reflection View * Projection transformation

float WaveLength;

float Time;
float WindForce;
float2 WindDirection;

// Vertex shader output structure
struct VS_OUTPUT
{
float4 position : POSITION; // vertex position

float2 bumpMapTexCoord : TEXCOORD0;
float3 refractionMapTexCoord : TEXCOORD1;
float3 reflectionMapTexCoord : TEXCOORD2;

float3 position3D : TEXCOORD3;
};

struct VS_INPUT
{
float4 position : POSITION;
float4 color : COLOR0;
float2 texCoord0 : TEXCOORD0;
};

VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT output;

// transform position to clip space
float4 pos = mul(input.position, WorldViewProj);
output.position = pos;

// calculate vawe coords
output.bumpMapTexCoord = input.texCoord0 / WaveLength + Time * WindForce * WindDirection;

// refraction texcoords
output.refractionMapTexCoord.x = 0.5 * (pos.w + pos.x);
output.refractionMapTexCoord.y = 0.5 * (pos.w - pos.y);
output.refractionMapTexCoord.z = pos.w;

// reflection texcoords
pos = mul(input.position, WorldReflectionViewProj);
output.reflectionMapTexCoord.x = 0.5 * (pos.w + pos.x);
output.reflectionMapTexCoord.y = 0.5 * (pos.w - pos.y);
output.reflectionMapTexCoord.z = pos.w;

// position of the vertex
output.position3D = input.position;

return output;
}
2 changes: 2 additions & 0 deletions src/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ add_executable(client
${CMAKE_CURRENT_LIST_DIR}/src/system/SystemUpdateKeyInput.cpp

${CMAKE_CURRENT_LIST_DIR}/include/client/misc/CBatchingMesh.hpp
${CMAKE_CURRENT_LIST_DIR}/include/client/misc/RealisticWater.hpp
${CMAKE_CURRENT_LIST_DIR}/include/client/misc/RGBtoHSV.hpp
${CMAKE_CURRENT_LIST_DIR}/include/client/misc/YAlignedBillboardSceneNode.hpp
${CMAKE_CURRENT_LIST_DIR}/src/misc/CBatchingMesh.cpp
${CMAKE_CURRENT_LIST_DIR}/src/misc/RealisticWater.cpp
${CMAKE_CURRENT_LIST_DIR}/src/misc/RGBtoHSV.cpp
${CMAKE_CURRENT_LIST_DIR}/src/misc/YAlignedBillboardSceneNode.cpp

Expand Down
Loading