Skip to content

Instantly share code, notes, and snippets.

@QuadStorm
Created June 8, 2019 16:15
Show Gist options
  • Save QuadStorm/9cbedc3c7ff954c40362f09e95def2b5 to your computer and use it in GitHub Desktop.
Save QuadStorm/9cbedc3c7ff954c40362f09e95def2b5 to your computer and use it in GitHub Desktop.
Blockland Vertex Shader + Curved World, version 460
#version 460
// curved world feature/effect; https://forum.blockland.us/index.php?topic=77967.msg7787623#msg7787623
bool curveTheWorld = false;
bool curveDirectionDown = true;
// adjust the effect
// exponential curve power
float curvePower = 1.4f;
// 'fisheye factor', note that high values make lights higher than their real position
float curveScale = 0.02f;
// Varying.
out vec4 vPos;
out vec3 worldNormal;
out vec3 worldPos;
// 460
uniform mat4 gl_ProjectionMatrix;
in vec4 gl_Vertex;
in vec4 gl_Normal;
in vec4 gl_Color;
in vec4 gl_MultiTexCoord0;
out vec4 gl_FrontColor;
// Misc uniforms.
uniform vec3 camPos;
uniform mat4 obj2World;
uniform mat4 world2Cam;
// Surface calculations, including specular power.
out vec2 texCoord;
vec4 viewDelta;
// Fogging.
uniform vec4 fogBaseColor;
uniform vec4 fogConsts;
uniform sampler2D fogTex;
out vec2 fogCoords;
void main()
{
// Bricks always in world space.
worldPos = (obj2World * vec4(gl_Vertex.xyz, 1.0f)).xyz;
worldNormal = ((obj2World * vec4(gl_Normal.xyz, 0.0f)).xyz);
if(length(gl_Normal.xyz) < 1.1f)
worldNormal.xyz = normalize(worldNormal.xyz);
// Do some camera delta math.
viewDelta.xyz = worldPos - camPos;
viewDelta.w = length(viewDelta.xyz);
viewDelta.xyz = -normalize(viewDelta.xyz);
// Fog can be done per-vertex.
fogCoords.x = clamp((fogConsts.y - viewDelta.w) * fogConsts.x, 0.0f, 1.0f);
fogCoords.y = clamp((worldPos.z - fogConsts.z) * fogConsts.w, 0.0f, 1.0f);
// Project the brick pos and normal.
vPos = world2Cam * vec4(worldPos, 1.0f);
if (curveTheWorld)
{
if (curveDirectionDown)
{
worldPos.z -= pow(viewDelta.w * curveScale, curvePower);
}
else
{
worldPos.z += pow(viewDelta.w * curveScale, curvePower);
}
gl_Position = gl_ProjectionMatrix * world2Cam * vec4(worldPos, 1.0f);
}
else
{
gl_Position = gl_ProjectionMatrix * vPos;
}
// Pass color through.
gl_FrontColor = gl_Color;
// Pass texcoords through.
texCoord = gl_MultiTexCoord0.st;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment