Re: Where Code Meets Creative Content
Posted: Mon Sep 19, 2016 10:49 pm
Hey, nice demo! Runs fine even in WINE. Beautifiying code... add a little whitespace ;-)
Official forum of the Irrlicht Engine
https://irrlicht.sourceforge.io/forum/
Code: Select all
// Based on code by Elvman.
//uniform mat4 View;
uniform mat4 M05WorldViewProjection;
uniform mat4 M24WorldReflectionViewProj;
uniform float WaveLength;
uniform float Time;
uniform float WindForce;
uniform vec2 WindDirection;
// Vertex shader output structure
varying vec2 bumpMapTexCoord;
varying vec2 bumpMapTexCoord02;
varying vec3 refractionMapTexCoord;
varying vec3 reflectionMapTexCoord;
varying vec3 position3D;
// varying vec3 VertexGLPosition;
//----------------- --- -- -
void main()
{// color = gl_Color; // We may later want to see what we can do with this..
// Transform position to clip space..
vec4 pos = M05WorldViewProjection * gl_Vertex;
gl_Position = pos;
// VertexGLPosition = vec3(M01World * vec4(gl_Vertex.xyz,1)).xyz;
// Calculate Wave Coords..
bumpMapTexCoord = gl_MultiTexCoord0.xy / WaveLength + (Time * WindForce * WindDirection);
// Old code..
// WindDirection.x *= 1.333;
// WindDirection.y *= 0.777;
// New code..
vec2 SecondWindDir = WindDirection; // Avoid assigning a uniform any values..
SecondWindDir.x *= 1.333;
SecondWindDir.y *= 0.777;
// bumpMapTexCoord02 = gl_MultiTexCoord0.xy / WaveLength + (Time * WindForce * -WindDirection);
bumpMapTexCoord02 = gl_MultiTexCoord0.xy / WaveLength + (Time * WindForce * -SecondWindDir);
// 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 = M24WorldReflectionViewProj * 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;
}
// END..
You can try to add:Fragment shader failed to compile with the following errors:
ERROR: 0:164: error(#164) l-value required: assign "UserShaderMode" (can't modify a uniform)
ERROR: 0:178: error(#164) l-value required: assign "GeneralWaterColour" (can't modify a uniform)
ERROR: 0:218: error(#143) Undeclared identifier: lerp
ERROR: 0:218: error(#202) No matching overloaded function found: lerp
ERROR: error(#273) 4 compilation errors. No code generated
Code: Select all
#version 120
Code: Select all
// Allows me to see errors to help make it platform independent??
#version 120
uniform vec3 CameraPosition; // Camera Position..
uniform vec3 GeneralWaterColour;
uniform float TransOpagueLerp; // New thing, see the code for explanation..
uniform sampler2D WaterBump;
uniform sampler2D RefractionMap;
uniform sampler2D ReflectionMap;
uniform sampler2D DepthMap;
uniform int UserShaderMode; // Change this in here during development tests..
varying vec2 bumpMapTexCoord;
varying vec2 bumpMapTexCoord02;
varying vec3 refractionMapTexCoord;
varying vec3 reflectionMapTexCoord;
varying vec3 position3D;
//===========================================
void main()
{
vec3 upVector = vec3(0.0, 1.0, 0.0);
float fresnelTerm = dot(normalize(CameraPosition - position3D), upVector);
vec4 RGBTanNormals = texture2D(WaterBump, bumpMapTexCoord * 6.0);
vec4 RGBTanNormals02 = texture2D(WaterBump, bumpMapTexCoord02 * 10.0);
// Depend on the image format i.e. "TGA" vs "PNG" !!!
// Scaleing only AFTER the normals were extracted.. (or reflection ripples hover above or below the zero line which is bad)
vec2 Perturbence = (2.0 * (RGBTanNormals.rg - 0.5) * 0.1); // When TGA ??
Perturbence += (2.0 * (RGBTanNormals02.rg - 0.5) * 0.1);
// Perturbence /= 2.0;
// Perturbence.xy *= (fresnelTerm + 0.5); // MUST GO HLSL..
// vec2 Perturbence = 0.2 * (RGBTanNormals.rg - 0.25); // When some PNGs ??
vec2 GeneralCoordsTweak;
GeneralCoordsTweak.x = 0.000400; // Differs slightly from HLSL..
GeneralCoordsTweak.y = 0.001; // Sign differs between GLSL and HLSL..
vec2 DepthTexCoords = clamp(refractionMapTexCoord.xy / refractionMapTexCoord.z, 0.0, 1.0);
DepthTexCoords.xy += GeneralCoordsTweak.xy; // (more is to the left) Small tweak for horizontal allighnment (loss from xyz(rgb) in "some" normal maps)..
//DepthTexCoords.y += GeneralCoordsTweak.y; // Very usefull little vertical tweak used to fix some inherent vertical reflection problems..
vec4 PrimaryDepth = texture2D(DepthMap, DepthTexCoords); // Primary Depth used to get the initial depth..
// Perturbence.xy * 10.0;
Perturbence.xy *= PrimaryDepth.y; // Get perturbence to "gradually flatten out" as it "Visually" approaches "Geometric Water Edges"..
// Perturbence.xy *= clamp(refractionMapTexCoord.xy, 0.0, 1/0 -PrimaryDepth.y);
// float CamHeightSquared = CameraPosition.y * CameraPosition.y;
// This little trick ensures that the further the camera is from the
// water surface, the smaller the effect of the purturbance displacement
// on the reflections and refraction thus avoiding unnatural effects ar greater distances..
// This is because we are basically displaceing the UV Coords in Screen Space..
// There is probably a pragmatic formula for this that would be fed
// the camera height and FOV etc, but that still needs to be determined..
// Perturbence.xy *= 1.0 - (CameraPosition.y / 100.0);
float InverseHeight = 1.0 - (CameraPosition.y / 250.0);
float HeightCorrection = InverseHeight * InverseHeight;
Perturbence.xy *= HeightCorrection;
Perturbence.xy *= HeightCorrection;
// Perturbence.xy *= HeightCorrection;
// Perturbence.xy *= HeightCorrection;
// Perturbence.xy *= HeightCorrection;
// Perturbence.xy *= 1.0 - (CameraPosition.y / 100.0);
// Perturbence.xy *= 1.0 - (CameraPosition.y / 80.0);
// It's not a perfect world so we need a cutoff to avoid negative values..
if (CameraPosition.y > 250.0) {Perturbence.xy = vec2(0.0, 0.0);}
vec2 DepthUVsPerturbed = clamp(refractionMapTexCoord.xy / refractionMapTexCoord.z + Perturbence, 0.0, 1.0);
DepthUVsPerturbed.xy += GeneralCoordsTweak.xy; // (more is to the left) SMALL TWEAK FOR HORIZONTAL ALLIGHNMENT (LOSS FROM XYZ(RGB) in "SOME" NORMAL MAPS)..
// MINUS Pert versus PLUS Pert is your opinion..
// but + & - does emulate a shift between Reflct and Refract..
vec2 DepthUVsPerturbedINVERT = clamp(refractionMapTexCoord.xy / refractionMapTexCoord.z - Perturbence, 0.0, 1.0);
DepthUVsPerturbedINVERT.xy += GeneralCoordsTweak.xy; // (more is to the left) SMALL TWEAK FOR HORIZONTAL ALLIGHNMENT (LOSS FROM XYZ(RGB) in "SOME" NORMAL MAPS)..
//||||||||||||||||||||||||||||
vec4 SecDepthREFRACT = texture2D (DepthMap, DepthUVsPerturbed );
vec4 SecDepthINVERT = texture2D (DepthMap, DepthUVsPerturbedINVERT );
vec2 ProjectedRefractionTexCoords = clamp(refractionMapTexCoord.xy / refractionMapTexCoord.z + Perturbence, 0.0, 1.);
ProjectedRefractionTexCoords.xy += GeneralCoordsTweak.xy; // (more is to the left) SMALL TWEAK FOR HORIZONTAL ALLIGHNMENT (LOSS FROM XYZ(RGB) in "SOME" NORMAL MAPS)..
vec4 refractiveColor = texture2D(RefractionMap, ProjectedRefractionTexCoords);
//||||||||||||||||||||||||||||
// AGAIN.. MINUS Pert versus PLUS Pert is your opinion..
// but + & - does emulate a shift between Reflct and Refract..
vec2 ProjectedReflectionTexCoords = clamp(reflectionMapTexCoord.xy / reflectionMapTexCoord.z + Perturbence, 0.0, 1.0);
ProjectedReflectionTexCoords.xy += GeneralCoordsTweak.xy; // (more is to the left) SMALL TWEAK FOR HORIZONTAL ALLIGHNMENT (LOSS FROM XYZ(RGB) in "SOME" NORMAL MAPS)..
//||||||||||||||||||||||||||||
// vec4 reflectiveColor = texture2D(ReflectionMap, ProjectedReflectionTexCoords );
vec4 reflectiveColor = texture2D(ReflectionMap, ProjectedReflectionTexCoords);
float ONEMINUSfresnelTerm = 1.0 - fresnelTerm;
ONEMINUSfresnelTerm *= ONEMINUSfresnelTerm;
float ONEMINFresSOFT = 1.0 - fresnelTerm;
ONEMINFresSOFT *= ONEMINFresSOFT;
ONEMINFresSOFT *= SecDepthREFRACT.z;
float FresSoft = 1.0 - ONEMINFresSOFT;
float ONEMINFresSOFTINVERT = 1.0 - fresnelTerm;
ONEMINFresSOFTINVERT *= ONEMINFresSOFTINVERT;
ONEMINFresSOFTINVERT *= SecDepthINVERT.z;
float FresSOFTINVERT = 1.0 - fresnelTerm;
FresSOFTINVERT *= FresSOFTINVERT;
FresSOFTINVERT *= SecDepthINVERT.z;
// WORK AROUND? (Nadro)
vec3 NonUniformWaterColour = GeneralWaterColour;
NonUniformWaterColour *= SecDepthREFRACT.z; // SecDepthREFRACT.z is "Tight White Edge"..
vec3 ColouredWhiteEdge;// = GeneralWaterColour + (1.0 - SecDepthREFRACT.z);
ColouredWhiteEdge = NonUniformWaterColour + (1.0 - SecDepthREFRACT.z);
vec4 TintedRefract;
TintedRefract.xyz = refractiveColor.xyz;
TintedRefract.xyz *= (1.0 - SecDepthREFRACT.x); // PrimaryDepth.x is "Water Opacity"..
TintedRefract.xyz *= ColouredWhiteEdge.xyz;
// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
// if (UserShaderMode == 1) // DEFAULT
// {
// gl_FragColor.xyz = ( (TintedRefract.xyz * FresSoft) + (reflectiveColor.xyz * ONEMINFresSOFT ));
// Actually better as relfection and refraction phase should differ by approx 180°..
// vec3 ResultingRGB;
// gl_FragColor.xyz = ResultingRGB.xyz;
// ORIGINAL SAFE..
// XXXXXXXXXX gl_FragColor.xyz = ( (TintedRefract.xyz * FresSoft) + (reflectiveColor.xyz * ONEMINFresSOFT));
// gl_FragColor.xyz = ( (vec3(1.0, 0.0, 0.0) * FresSoft) + (reflectiveColor.xyz * ONEMINFresSOFT));
// /*
// float3 RawDiffuse = float3 (0.25, 0.0, 0.0);
vec3 RawDiffuse = GeneralWaterColour;
RawDiffuse.xyz *= (SecDepthREFRACT.z); // O.K.
RawDiffuse.xyz += (refractiveColor.xyz * (1 - SecDepthREFRACT.z)); // FINALLY
// */
vec4 GeneralTransparency = ( vec4(TintedRefract.xyz * FresSoft, 1.0) + (reflectiveColor * ONEMINFresSOFT) );
vec4 GeneralOpacity = ( vec4(RawDiffuse.xyz * FresSoft, 1.0) + (reflectiveColor * ONEMINFresSOFT) );
// gl_FragColor.xyz = lerp (GeneralTransparency.xyz, GeneralOpacity.xyz , TransOpagueLerp );
gl_FragColor.xyzw = GeneralTransparency; // NO LERP FOR OPACITY..
// GET THE HADRCODED VERSION OF THIS (i've got it somewhere, just have to find it)
// gl_FragColor.xyz = lerp( (TintedRefract.xyz * FresSoft), (reflectiveColor.xyz * ONEMINFresSOFT) ,ONEMINFresSOFTINVERT);
}
// END..
Code: Select all
vec4 InterpolateV4(vec4 ArgA, vec4 ArgB, float Slider)
{vec4 OutputVal = ArgB * Slider + ArgA * (1 - Slider);
return OutputVal;
}
// AND..
vec3 InterpolateV3( vec3 ArgA, vec3 ArgB, float Slider)
{vec3 OutputVal = ArgB * Slider + ArgA * (1 - Slider);
return OutputVal;
}
Code: Select all
// gl_FragColor.xyz = lerp (GeneralTransparency.xyz, GeneralOpacity.xyz , TransOpagueLerp );
// Relpaced with..
gl_FragColor.xyz = InterpolateV3(GeneralTransparency.xyz, GeneralOpacity.xyz, TransOpagueLerp);