Skip to content

Commit

Permalink
Implemented #686 (#692)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhog authored Mar 19, 2023
1 parent d4cf730 commit 476f32b
Show file tree
Hide file tree
Showing 21 changed files with 799 additions and 832 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
uniform sampler2D diffuseTex;
uniform sampler2D detailsTex;

in vec3 vVertPos;
in vec4 vVertCol;
in vec2 vDiffuseUV;
in vec2 vDetailsUV;
Expand Down
36 changes: 27 additions & 9 deletions cont/base/springcontent/shaders/GLSL/SMFBorderVertProg.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,48 @@
in vec3 vertexPos;
in vec4 vertexCol;

uniform sampler2D heightMapTex;
uniform float borderMinHeight;
uniform ivec2 texSquare; //TODO convert to texture array
uniform vec4 mapSize; // mapSize, 1.0/mapSize

const float SMF_TEXSQR_SIZE = 1024.0;
const vec4 detailPlaneS = vec4(0.005, 0.000, 0.005, 0.5);
const vec4 detailPlaneT = vec4(0.000, 0.005, 0.000, 0.5);

out vec3 vVertPos;
out vec4 vVertCol;
out vec2 vDiffuseUV;
out vec2 vDetailsUV;

void main() {
vVertPos = vertexPos;
vVertCol = vertexCol;
float HeightAtWorldPos(vec2 wxz){
// Some texel magic to make the heightmap tex perfectly align:
const vec2 HM_TEXEL = vec2(8.0, 8.0);
wxz += -HM_TEXEL * (wxz * mapSize.zw) + 0.5 * HM_TEXEL;

vec2 uvhm = clamp(wxz, HM_TEXEL, mapSize.xy - HM_TEXEL);
uvhm *= mapSize.zw;

vec4 vertexPos4 = vec4(vertexPos, 1.0);
return textureLod(heightMapTex, uvhm, 0.0).x;
}

vDiffuseUV = (floor(vertexPos4.xz) * (1.0 / SMF_TEXSQR_SIZE)) - vec2(texSquare);
void main() {
vec4 vertexWorldPos = vec4(vertexPos, 1.0);
vertexWorldPos.xz += vec2(texSquare) * SMF_TEXSQR_SIZE;
vertexWorldPos.y = mix(borderMinHeight, HeightAtWorldPos(vertexWorldPos.xz), float(vertexWorldPos.y == 0.0));
/*
if (vertexWorldPos.y == 0.0)
vertexWorldPos.y = HeightAtWorldPos(vertexWorldPos.xz);
else
vertexWorldPos.y = borderMinHeight;
*/

vVertCol = vertexCol;
vDiffuseUV = (vertexWorldPos.xz * (1.0 / SMF_TEXSQR_SIZE)) - vec2(texSquare);
vDetailsUV = vec2(
dot(vertexPos4, detailPlaneS),
dot(vertexPos4, detailPlaneT)
dot(vertexWorldPos, detailPlaneS),
dot(vertexWorldPos, detailPlaneT)
);

gl_Position = gl_ModelViewProjectionMatrix * vertexPos4;
gl_Position = gl_ModelViewProjectionMatrix * vertexWorldPos;
}

25 changes: 21 additions & 4 deletions cont/base/springcontent/shaders/GLSL/SMFVertProg.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ in vec3 vertexPos;
uniform ivec2 texSquare;
uniform vec3 cameraPos;
uniform vec4 lightDir; // mapInfo->light.sunDir
uniform vec2 specularTexGen; // 1.0/mapSize
uniform sampler2D heightMapTex;

out vec3 halfDir;
out float fogFactor;
Expand All @@ -14,17 +16,32 @@ out vec2 diffuseTexCoords;
const float SMF_TEXSQR_SIZE = 1024.0;
const float SMF_DETAILTEX_RES = 0.02;

// specularTexGen is inverseMapSize
vec2 mapSize = vec2(1.0) / specularTexGen;
float HeightAtWorldPos(vec2 wxz){
// Some texel magic to make the heightmap tex perfectly align:
const vec2 HM_TEXEL = vec2(8.0, 8.0);
wxz += -HM_TEXEL * (wxz * specularTexGen) + 0.5 * HM_TEXEL;

vec2 uvhm = clamp(wxz, HM_TEXEL, mapSize.xy - HM_TEXEL);
uvhm *= specularTexGen;

return textureLod(heightMapTex, uvhm, 0.0).x;
}

void main() {
// calc some lighting variables
vec3 viewDir = vec3(gl_ModelViewMatrixInverse * vec4(0.0, 0.0, 0.0, 1.0));

viewDir = normalize(viewDir - vertexPos);
halfDir = normalize(lightDir.xyz + viewDir);

vertexWorldPos = vec4(vertexPos, 1.0);
vertexWorldPos.xz += vec2(texSquare) * SMF_TEXSQR_SIZE;
vertexWorldPos.y = HeightAtWorldPos(vertexWorldPos.xz);

viewDir = normalize(viewDir - vertexWorldPos.xyz);
halfDir = normalize(lightDir.xyz + viewDir);

// calc texcoords
diffuseTexCoords = (floor(vertexWorldPos.xz) / SMF_TEXSQR_SIZE) - vec2(texSquare);
diffuseTexCoords = (vertexWorldPos.xz / SMF_TEXSQR_SIZE) - vec2(texSquare);

// transform vertex pos
gl_Position = gl_ModelViewProjectionMatrix * vertexWorldPos;
Expand Down
47 changes: 47 additions & 0 deletions cont/base/springcontent/shaders/GLSL/ShadowGenVertMapProg.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#if (GL_FRAGMENT_PRECISION_HIGH == 1)
// ancient GL3 ATI drivers confuse GLSL for GLSL-ES and require this
precision highp float;
#else
precision mediump float;
#endif

in vec3 vertexPos;

uniform sampler2D heightMapTex;
uniform float borderMinHeight;
uniform ivec2 texSquare;
uniform vec4 mapSize; // mapSize, 1.0/mapSize

const float SMF_TEXSQR_SIZE = 1024.0;

float HeightAtWorldPos(vec2 wxz){
// Some texel magic to make the heightmap tex perfectly align:
const vec2 HM_TEXEL = vec2(8.0, 8.0);
wxz += -HM_TEXEL * (wxz * mapSize.zw) + 0.5 * HM_TEXEL;

vec2 uvhm = clamp(wxz, HM_TEXEL, mapSize.xy - HM_TEXEL);
uvhm *= mapSize.zw;

return textureLod(heightMapTex, uvhm, 0.0).x;
}

void main() {
vec4 vertexWorldPos = vec4(vertexPos, 1.0);
vertexWorldPos.xz += vec2(texSquare) * SMF_TEXSQR_SIZE;
vertexWorldPos.y = mix(borderMinHeight, HeightAtWorldPos(vertexWorldPos.xz), float(vertexWorldPos.y == 0.0));
/*
if (vertexWorldPos.y == 0.0)
vertexWorldPos.y = HeightAtWorldPos(vertexWorldPos.xz);
else
vertexWorldPos.y = borderMinHeight;
*/
vec4 lightVertexPos = gl_ModelViewMatrix * vertexWorldPos;

lightVertexPos.xy += vec2(0.5);
//lightVertexPos.z -= 2e-3; // glEnable(GL_POLYGON_OFFSET_FILL); is in use

gl_Position = gl_ProjectionMatrix * lightVertexPos;

gl_ClipVertex = vertexWorldPos;
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Loading

0 comments on commit 476f32b

Please sign in to comment.