Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(w3ddevice): darken water by what the mirror covers, not by its colour

Olcay Seygana49673e7a6ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 057d2848f..4b26fda7f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -701,11 +701,13 @@ found and fixed — EA's own, not port damage.**
on; the water you actually play beside gets it now. The reflection only ever darkens: open water
keeps the exact colour the map painted it, and a black shroud stays black. The water shows only
what the land does: an enemy hidden under the fog of war, or a stealthed unit, used to give itself
- away in the reflection. It is not free, since the world is drawn a second time for the mirror, but
- the mirror only draws the ground it can see, only the part of the screen the water covers, and
- only every other frame while the camera holds still. On one 1280x720 view of Golden Oasis, where
- the river fills the screen, a frame takes 6.9ms against 5.8ms with no reflection; before that
- work it took 8.3ms. Over the small lakes on Fortress Avalanche the difference is 7.1ms against 6.6ms.
+ away in the reflection. A palm shows the same in sunlight as under the fog: whatever stands over
+ the water darkens it by one amount, where a sunlit palm used to be nearly as bright as open water
+ and vanished the moment the fog lifted off it. It is not free, since the world is drawn a second
+ time for the mirror, but the mirror leaves the ground out, draws only the part of the screen the
+ water covers, and only every other frame while the camera holds still. On one 1280x720 view of
+ Golden Oasis, where the river fills the screen, a frame takes 6.7ms against 5.8ms with no
+ reflection; before that work it took 8.3ms.
- Bright things can bleed light into the air. Off by default, because the artwork was painted in 2003 for a screen that had no glow at all.
- Two dropdowns on the display page, not two percentages. Glow is off, subtle, normal or strong; what glows is only the brightest, bright things, or most of the picture. The numbers underneath were a strength you had to find by experiment and a brightness that ran backwards, where turning it down put more of the screen in the haze, and nothing on screen told you that.
- Scorch marks follow the texture quality setting like everything else. The terrain and the trees
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/FlatHeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/FlatHeightMap.cpp
index 096f85cd6..210be7dc2 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/FlatHeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/FlatHeightMap.cpp
@@ -529,6 +529,11 @@ void FlatHeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
if (m_disableTextures)
devicePasses=1; //force to 1 lighting-only pass
+ // A water mirror leaves the ground out: the reflection darkens water wherever anything stands in
+ // the mirror, and the banks would lay a dark band along every shore.
+ if (ShaderClass::Is_Backface_Culling_Inverted())
+ devicePasses=0;
+
//Specify all textures that this shader may need.
W3DShaderManager::setTexture(0,m_stageZeroTexture);
if (m_shroud && rinfo.Additional_Pass_Count() && !m_disableTextures)
@@ -589,7 +594,8 @@ void FlatHeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
#if 1
//Draw feathered shorelines
- renderShoreLines(&rinfo.Camera);
+ if (!ShaderClass::Is_Backface_Culling_Inverted())
+ renderShoreLines(&rinfo.Camera);
#ifdef DO_ROADS
DX8Wrapper::Set_Texture(0,NULL);
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
index e2f37f464..322864229 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
@@ -2022,7 +2022,7 @@ void HeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
}
Int pass;
- for (pass=0; pass<devicePasses; pass++) {
+ for (pass=0; pass<devicePasses && !ShaderClass::Is_Backface_Culling_Inverted(); pass++) { //a water mirror leaves the ground out, see the shorelines below
#ifdef TIMING_TESTS
#endif
if (!doMultiPassWireFrame) //multi-pass wireframe doesn't use regular shaders.
@@ -2040,24 +2040,6 @@ void HeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
{
static int count = 0;
count++;
- if (ShaderClass::Is_Backface_Culling_Inverted())
- { // The draw window reaches well past what the mirror sees, and a mirror frame has
- // nothing else to spend on a block of ground that could never show in the water.
- // A block split across the window's wrap seam is not worth the arithmetic and is drawn.
- const Int cellXMin = getXWithOrigin(i*VERTEX_BUFFER_TILE_LENGTH)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
- const Int cellXMax = getXWithOrigin((i+1)*VERTEX_BUFFER_TILE_LENGTH-1)+m_map->getDrawOrgX()+1-m_map->getBorderSizeInline();
- const Int cellYMin = getYWithOrigin(j*VERTEX_BUFFER_TILE_LENGTH)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
- const Int cellYMax = getYWithOrigin((j+1)*VERTEX_BUFFER_TILE_LENGTH-1)+m_map->getDrawOrgY()+1-m_map->getBorderSizeInline();
- if (cellXMax > cellXMin && cellYMax > cellYMin)
- {
- AABoxClass block;
- block.Init_Min_Max(
- Vector3((cellXMin-1)*MAP_XY_FACTOR, (cellYMin-1)*MAP_XY_FACTOR, m_minHeight),
- Vector3((cellXMax+1)*MAP_XY_FACTOR, (cellYMax+1)*MAP_XY_FACTOR, m_maxHeight));
- if (rinfo.Camera.Cull_Box(block))
- continue;
- }
- }
Int numPolys = VERTEX_BUFFER_TILE_LENGTH*VERTEX_BUFFER_TILE_LENGTH*2;
Int numVertex = (VERTEX_BUFFER_TILE_LENGTH*2)*(VERTEX_BUFFER_TILE_LENGTH*2);
if (HALF_RES_MESH) {
@@ -2095,12 +2077,18 @@ void HeightMapRenderObjClass::Render(RenderInfoClass & rinfo)
if (pass) //shader was applied at least once?
W3DShaderManager::resetShader(st);
- //Draw feathered shorelines
- renderShoreLines(&rinfo.Camera);
+ // A water mirror leaves the ground out, and everything drawn onto it. The reflection darkens
+ // water wherever anything stands in the mirror, and the banks would lay a dark band along
+ // every shore; the ground was also most of what the mirror drew.
+ if (!ShaderClass::Is_Backface_Culling_Inverted())
+ {
+ //Draw feathered shorelines
+ renderShoreLines(&rinfo.Camera);
- //Do additional pass over any tiles that have 3 textures blended together.
- if (TheGlobalData->m_use3WayTerrainBlends)
- renderExtraBlendTiles();
+ //Do additional pass over any tiles that have 3 textures blended together.
+ if (TheGlobalData->m_use3WayTerrainBlends)
+ renderExtraBlendTiles();
+ }
Int yCoordMin = m_map->getDrawOrgY();
Int yCoordMax = m_y+m_map->getDrawOrgY()-1;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
index 19549b2c3..13f671a80 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
@@ -100,7 +100,7 @@
#define SEA_REFLECTION_SIZE 256 //dimensions of reflection texture
#define WATER_REFLECTION_SIZE 512 //dimensions of the reflection texture laid over map water
#define WATER_REFLECTION_AREA_MARGIN 0.05f //slack around the water's screen area, so filtering at its edge reads inside it
-#define WATER_REFLECTION_STRENGTH 0.8f //how far a reflected object darkens the water under it
+#define WATER_REFLECTION_STRENGTH 0.55f //how far a reflected object darkens the water under it
#define WATER_REFLECTION_SKY_COLOR Vector3(1.0f,1.0f,1.0f) //white, so where nothing stands the water keeps its own colour
#define SEA_BUMP_SCALE (0.06f) //scales the du/dv offsets stored in bump map (~ amount to perturb)
@@ -1060,9 +1060,15 @@ void WaterRenderObjClass::ReAcquireResources(void)
}
shader =
"ps.1.1\n \
+ def c1, 0.333333, 0.333333, 0.333333, 0\n\
tex t0 ; the mirrored scene, looked up through the projected stage, white where nothing stands\n\
- mul r0, 1-t0, c0 ; how much of the reflection gets through\n\
- mov r0, 1-r0 ; multiplied into the water, so white leaves it as it was\n";
+ dp3 r1.rgb, t0, c1 ; how bright the mirror is here\n\
+ mov r1.rgb, 1-r1 ; how far that is from the white it was cleared to\n\
+ add r1.rgb, r1, r1\n\
+ add r1.rgb, r1, r1 ; four times and clamped, so anything standing there counts in full whatever its colour\n\
+ mul r0.rgb, r1, c0 ; darkened by the strength\n\
+ mov r0.rgb, 1-r0 ; multiplied into the water, so white leaves it as it was\n\
+ +mov r0.a, c0\n";
hr = D3DXAssembleShader( shader, (UINT)strlen(shader), NULL, NULL, 0, &compiledShader, NULL);
if (hr==0) {
hr = DX8Wrapper::_Get_D3D_Device()->CreatePixelShader((DWORD*)compiledShader->GetBufferPointer(), &m_reflectionPixelShader);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
index 3bc2df876..7bb159ea5 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
@@ -283,20 +283,30 @@ static void write_river_water(std::string & hlsl)
/*
** The reflection W3DWater lays over map water once the water itself is drawn:
**
+** def c1, 0.333333, 0.333333, 0.333333, 0
** tex t0 ; the mirrored scene, looked up through a projected stage
-** mul r0, 1-t0, c0
-** mov r0, 1-r0
+** dp3 r1.rgb, t0, c1
+** mov r1.rgb, 1-r1
+** add r1.rgb, r1, r1
+** add r1.rgb, r1, r1
+** mul r0.rgb, r1, c0
+** mov r0.rgb, 1-r0
+** +mov r0.a, c0
**
** The result multiplies the water already drawn, and the mirror was cleared to white, so open sky
-** leaves the water its own colour and a reflected object only darkens it. D3D9 reads the strength
-** out of c0. A transcribed program shares the combiners' constant buffer and not the engine's
-** register bank, so here it is the texture factor's alpha, which W3DWater sets to the same value.
+** leaves the water its own colour. Whatever stands in the mirror darkens it by the same amount
+** whatever its own colour: a sunlit palm is nearly as bright as the white around it and used to
+** vanish from the water the moment the fog lifted off it. The two doublings clamp at one each, which
+** is the saturate. D3D9 reads the strength out of c0. A transcribed program shares the combiners'
+** constant buffer and not the engine's register bank, so here it is the texture factor's alpha,
+** which W3DWater sets to the same value.
*/
static void write_water_reflection(std::string & hlsl)
{
write_pixel_preamble(hlsl);
hlsl +=
- " float4 current = 1.0 - saturate((1.0 - texel0) * TextureFactor.a);\n";
+ " float coverage = saturate((1.0 - dot(texel0.rgb, float3(0.333333, 0.333333, 0.333333))) * 4.0);\n"
+ " float4 current = float4((1.0 - TextureFactor.a * coverage).xxx, TextureFactor.a);\n";
}
// Every ps_1_1 instruction clamps its result to zero and one, so each step saturates and not only
diff --git a/GeneralsMD/Code/Tests/test_engineshader.cpp b/GeneralsMD/Code/Tests/test_engineshader.cpp
index 2872a98bb..a458c3c50 100644
--- a/GeneralsMD/Code/Tests/test_engineshader.cpp
+++ b/GeneralsMD/Code/Tests/test_engineshader.cpp
@@ -145,7 +145,8 @@ TEST(engineshader_fades_the_water_reflection_towards_white_by_the_texture_factor
{
std::string hlsl;
CHECK(EngineShader_Pixel_Program(ENGINE_SHADER_WATER_REFLECTION, plain_pipeline(), hlsl));
- CHECK(contains(hlsl, "float4 current = 1.0 - saturate((1.0 - texel0) * TextureFactor.a);"));
+ CHECK(contains(hlsl, "float coverage = saturate((1.0 - dot(texel0.rgb, float3(0.333333, 0.333333, 0.333333))) * 4.0);"));
+ CHECK(contains(hlsl, "float4 current = float4((1.0 - TextureFactor.a * coverage).xxx, TextureFactor.a);"));
CHECK(!contains(hlsl, "* texel3"));
}