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.

feat(w3ddevice): reflect the scene in map water

Olcay Seygan48f932ed1f97
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 563ba830f..d4cc221d0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -671,6 +671,16 @@ found and fixed — EA's own, not port damage.**
## Bright things can glow
+- Water reflects what stands over it. The pools on Golden Oasis hold their palms upside down and the
+ stone bridge's arches under the bridge, and the lakes on Fortress Avalanche carry the pines on
+ their banks. Helicopters hovering by the river show dark and upside down in the water under them;
+ they were left out at first, because the mirror skipped every unit the game had set aside that
+ frame for its behind-building markers. The game has had a mirror in it since 2003, wired to a test sea no shipped map turns
+ 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: the world is drawn a second time for the mirror, and on
+ one 1280x720 view of Golden Oasis a frame went from 4.9ms to 8.7ms.
- 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/Include/W3DDevice/GameClient/W3DWater.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h
index 7ffc6bb0e..c8db1b453 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DWater.h
@@ -174,6 +174,7 @@ protected:
Real m_bumpFrameAccum; ///<fractional carry, so the flipbook can run slower than one frame per tick
Real m_fBumpScale; ///<scales bump map uv perturbation
TextureClass * m_pReflectionTexture; ///<render target for reflection
+ Real m_reflectionLevel; ///<height of the water plane the reflection texture was rendered for
RenderObjClass *m_skyBox; ///<box around level
WaterTracksRenderSystem *m_waterTrackSystem; ///<object responsible for rendering water wakes
@@ -216,6 +217,7 @@ protected:
IDirect3DPixelShader9 * m_waterPixelShader; ///<D3D pixel shader.
IDirect3DPixelShader9 * m_riverWaterPixelShader; ///<D3D pixel shader.
IDirect3DPixelShader9 * m_trapezoidWaterPixelShader; ///<D3D pixel shader
+ IDirect3DPixelShader9 * m_reflectionPixelShader; ///<darkens water where the reflection texture holds something
TextureClass *m_waterSparklesTexture;
Real m_riverXOffset;
Real m_riverYOffset;
@@ -250,7 +252,8 @@ protected:
void renderSkyBody(Matrix3D *mat); ///<draw the sky body (sun, moon, etc.)
void renderWaterMesh(void); ///<draw the water surface mesh (deformed 3d mesh).
HRESULT initBumpMap(LPDIRECT3DTEXTURE9 *pTex, TextureClass *pBumpSource); ///<copies data into bump-map format.
- void renderMirror(CameraClass *cam); ///< Draw reflected scene into texture
+ void renderMirror(CameraClass *cam, Real level); ///< Draw reflected scene into texture
+ void drawReflection(Int triangleCount, Int vertexCount); ///< Lay the reflection over the water just drawn
void drawSea(RenderInfoClass & rinfo); ///< Draw the surface of the water
///bounding box of frustum clipped polygon plane
Bool getClippedWaterPlane(CameraClass *cam, AABoxClass *box);
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 68fcb07d4..df4212544 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -2322,7 +2322,7 @@ AGAIN:
TheParticleUpdateMS = w3dElapsedMS( tParticleStart, tParticleEnd );
#endif
- if (TheWaterRenderObj && TheGlobalData->m_waterType == 2)
+ if (TheWaterRenderObj)
TheWaterRenderObj->updateRenderTargetTextures(primaryW3DView->get3DCamera()); //do a render into each texture
//Can't render into textures while rendering to screen so these textures need to be updated
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
index c91baca6f..6cbab15fa 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
@@ -441,10 +441,17 @@ void RTS3DScene::Visibility_Check(CameraClass * camera)
if( draw )
{
+ // The flags are last frame's. Customized_Render leaves an occluder, and any drawable
+ // the behind-building markers delay, for the occlusion lists to draw, and this pass
+ // fills no lists, so a stale flag keeps the object out of the mirror altogether.
+ drawInfo->m_flags = DrawableInfo::ERF_IS_NORMAL;
if (robj->Is_Force_Visible()) {
robj->Set_Visible(true);
} else {
- robj->Set_Visible(draw->getDrawsInMirror() && !camera->Cull_Sphere(robj->Get_Bounding_Sphere()));
+ // The same test the main pass hides a drawable by, or a unit under the fog of war,
+ // or a building standing in for its own ghost, shows up in the water and nowhere else.
+ Bool hidden = robj->Is_Hidden() || draw->isDrawableEffectivelyHidden() || draw->getFullyObscuredByShroud();
+ robj->Set_Visible(!hidden && draw->getDrawsInMirror() && !camera->Cull_Sphere(robj->Get_Bounding_Sphere()));
}
}
else
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
index ba52fe2d0..b2c09be06 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Water/W3DWater.cpp
@@ -98,6 +98,9 @@
#define PATCH_UV_TILES 42 //number of times the bump map texture is tiled across patch (must be integer!).
#define PATCH_SCALE (4.0f * MAP_XY_FACTOR) //horizontal scale factor. Adjust this and size to get desired vertex density.
#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_STRENGTH 0.8f //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)
#define BUMP_SIZE (50.f)
@@ -392,6 +395,7 @@ WaterRenderObjClass::WaterRenderObjClass(void)
m_waterType = WATER_TYPE_0_TRANSLUCENT;
m_tod=TIME_OF_DAY_AFTERNOON;
m_pReflectionTexture=NULL;
+ m_reflectionLevel=0;
m_skyBox=NULL;
m_vertexBufferD3D=NULL;
m_indexBufferD3D=NULL;
@@ -424,6 +428,7 @@ WaterRenderObjClass::WaterRenderObjClass(void)
m_waterPixelShader=0; ///<D3D handle to pixel shader.
m_riverWaterPixelShader=0; ///<D3D handle to pixel shader.
m_trapezoidWaterPixelShader=0; ///<D3D handle to pixel shader.
+ m_reflectionPixelShader=0;
m_waterSparklesTexture=0;
m_riverXOffset=0;
m_riverYOffset=0;
@@ -907,6 +912,9 @@ void WaterRenderObjClass::ReleaseResources(void)
if (m_trapezoidWaterPixelShader)
m_trapezoidWaterPixelShader->Release();
+ if (m_reflectionPixelShader)
+ m_reflectionPixelShader->Release();
+
if (m_riverWaterPixelShader)
m_riverWaterPixelShader->Release();
@@ -914,6 +922,7 @@ void WaterRenderObjClass::ReleaseResources(void)
m_dwWaveVertexShader=0;
m_waterPixelShader = 0;
m_trapezoidWaterPixelShader=0;
+ m_reflectionPixelShader=0;
m_riverWaterPixelShader=0;
}
@@ -986,6 +995,9 @@ void WaterRenderObjClass::ReAcquireResources(void)
m_pReflectionTexture = DX8Wrapper::Create_Render_Target (SEA_REFLECTION_SIZE, SEA_REFLECTION_SIZE);
}
+ if (m_waterType == WATER_TYPE_0_TRANSLUCENT)
+ m_pReflectionTexture = DX8Wrapper::Create_Render_Target (WATER_REFLECTION_SIZE, WATER_REFLECTION_SIZE);
+
if (m_waterTrackSystem)
m_waterTrackSystem->ReAcquireResources();
@@ -1041,6 +1053,17 @@ void WaterRenderObjClass::ReAcquireResources(void)
compiledShader->Release();
Direct3D11_Register_Engine_Shader(m_trapezoidWaterPixelShader, "trapezoid water ps.1.1");
}
+ shader =
+ "ps.1.1\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";
+ 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);
+ compiledShader->Release();
+ Direct3D11_Register_Engine_Shader(m_reflectionPixelShader, "water reflection ps.1.1");
+ }
}
//W3D Invalidate textures after losing the device and since we peek at the textures directly, it won't
@@ -1564,15 +1587,37 @@ void WaterRenderObjClass::loadSetting( Setting *setting, TimeOfDay timeOfDay )
//-------------------------------------------------------------------------------------------------
void WaterRenderObjClass::updateRenderTargetTextures(CameraClass *cam)
{
- if (m_waterType == WATER_TYPE_2_PVSHADER && getClippedWaterPlane(cam, NULL) &&
- TheTerrainRenderObject && TheTerrainRenderObject->getMap())
- renderMirror(cam); //generate texture containing reflected scene
+ if (!TheTerrainRenderObject || !TheTerrainRenderObject->getMap())
+ return;
+
+ if (m_waterType == WATER_TYPE_2_PVSHADER && getClippedWaterPlane(cam, NULL))
+ renderMirror(cam, m_level); //generate texture containing reflected scene
+
+ if (m_waterType != WATER_TYPE_0_TRANSLUCENT || !m_pReflectionTexture)
+ return;
+
+ // One mirror a frame, so the first water area in view decides the plane. A map whose visible
+ // water sits at two different heights reflects the second one at the first one's level.
+ for (PolygonTrigger *pTrig=PolygonTrigger::getFirstPolygonTrigger(); pTrig; pTrig = pTrig->getNext())
+ {
+ if (!pTrig->isWaterArea() || pTrig->getNumPoints() < 3)
+ continue;
+
+ Coord3D center;
+ pTrig->getCenterPoint(&center);
+ if (cam->Cull_Sphere(SphereClass(Vector3(center.x, center.y, center.z), pTrig->getRadius())))
+ continue;
+
+ m_reflectionLevel = pTrig->getPoint(0)->z;
+ renderMirror(cam, m_reflectionLevel);
+ return;
+ }
}
//-------------------------------------------------------------------------------------------------
/** Renders the reflected scene into an offscreen texture. */
//-------------------------------------------------------------------------------------------------
-void WaterRenderObjClass::renderMirror(CameraClass *cam)
+void WaterRenderObjClass::renderMirror(CameraClass *cam, Real level)
{
#ifdef EXTENDED_STATS
if (DX8Wrapper::stats.m_disableWater) {
@@ -1582,7 +1627,7 @@ void WaterRenderObjClass::renderMirror(CameraClass *cam)
Matrix3D OldCameraMatrix=cam->Get_Transform();
Matrix4x4 FullMatrix4(cam->Get_Transform()); //copy 3x4 matrix into a 4x4
Vector3 WaterNormal(0,0,1); //normal of plane used for reflection
- Vector4 WaterPlane(WaterNormal.X,WaterNormal.Y,WaterNormal.Z,m_level);
+ Vector4 WaterPlane(WaterNormal.X,WaterNormal.Y,WaterNormal.Z,level);
Vector3 rRight,rUp,rN,rPos; //orientation and translation vectors of camera
Matrix4x4 FullMatrix(FullMatrix4.Transpose()); //swap rows/columns
@@ -1611,7 +1656,9 @@ void WaterRenderObjClass::renderMirror(CameraClass *cam)
DX8Wrapper::Set_Render_Target_With_Z((TextureClass*)m_pReflectionTexture);
// Clear the backbuffer
- WW3D::Begin_Render(false,true,Vector3(0.0f,0.0f,0.0f)); //clearing only z-buffer since background always filled with clouds
+ // The sea's cloud plane fills its background; map water has no such plane, so its target is
+ // cleared to white, which the overlay multiplies in as no change at all.
+ WW3D::Begin_Render(m_waterType == WATER_TYPE_0_TRANSLUCENT,true,WATER_REFLECTION_SKY_COLOR);
cam->Set_Transform( reflectedTransform );
@@ -1628,12 +1675,21 @@ void WaterRenderObjClass::renderMirror(CameraClass *cam)
ShaderClass::Invert_Backface_Culling(true);
// Render the scene
- renderSky();
- if (m_tod == TIME_OF_DAY_NIGHT)
- renderSkyBody(&reflectedTransform);
+ if (m_waterType == WATER_TYPE_2_PVSHADER)
+ {
+ renderSky();
+ if (m_tod == TIME_OF_DAY_NIGHT)
+ renderSkyBody(&reflectedTransform);
+ }
+
+ // The mirrored camera sits under the water, so everything under the water is in front of it.
+ PlaneClass waterSurface(WaterNormal, level);
+ cam->Set_Oblique_Near_Plane(&waterSurface);
+ cam->Apply();
WW3D::Render(m_parentScene,cam);
+ cam->Set_Oblique_Near_Plane(NULL);
cam->Set_Transform(OldCameraMatrix); //restore original non-reflected matrix
cam->Set_Viewport(vOldMin,vOldMax);
@@ -3094,6 +3150,8 @@ void WaterRenderObjClass::drawRiverWater(PolygonTrigger *pTrig)
if (TheWaterTransparency->m_additiveBlend)
DX8Wrapper::Set_DX8_Render_State(D3DRS_SRCBLEND, D3DBLEND_ONE );
+ drawReflection(rectangleCount*2, (rectangleCount+1)*2);
+
// the shroud is in the vertex colour now - see applyShroudToRiverDiffuse. The second pass that
// used to apply it here darkened the banks twice.
DX8Wrapper::Set_DX8_Render_State(D3DRS_CULLMODE, cull);
@@ -3101,6 +3159,59 @@ void WaterRenderObjClass::drawRiverWater(PolygonTrigger *pTrig)
}
+//-------------------------------------------------------------------------------------------------
+/** Lays the reflection over water geometry still bound from the draw before. The texture was
+ * rendered from a camera mirrored in the water plane, and a point on that plane lands on the
+ * same screen position in both views, so the ordinary camera's projection is the whole lookup. */
+//-------------------------------------------------------------------------------------------------
+void WaterRenderObjClass::drawReflection(Int triangleCount, Int vertexCount)
+{
+ if (!m_pReflectionTexture || !m_reflectionPixelShader)
+ return; //the card refused a render target or a pixel shader
+
+ // multiplied into the water, so it keeps the colour it was drawn with and the shroud already on it
+ DX8Wrapper::Set_Shader(ShaderClass::_PresetMultiplicativeShader);
+ DX8Wrapper::Set_Texture(0, NULL);
+ DX8Wrapper::Apply_Render_State_Changes();
+
+ // camera space -> clip space -> 0..1 texture space, divided by w in the projected stage
+ D3DXMATRIX projection;
+ DX8Wrapper::_Get_DX8_Transform(D3DTS_PROJECTION, *(Matrix4x4*)&projection);
+ D3DXMATRIX clipToTexture;
+ memset(&clipToTexture, 0, sizeof(D3DXMATRIX));
+ clipToTexture._11 = 0.5f;
+ clipToTexture._22 = -0.5f;
+ clipToTexture._41 = 0.5f;
+ clipToTexture._42 = 0.5f;
+ clipToTexture._43 = 1.0f;
+ D3DXMATRIX viewToTexture = projection * clipToTexture;
+ DX8Wrapper::_Set_DX8_Transform(D3DTS_TEXTURE0, *(Matrix4x4*)&viewToTexture);
+
+ DX8Wrapper::Set_DX8_Texture(0, m_pReflectionTexture->Peek_D3D_Texture());
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 | D3DTTFF_PROJECTED);
+
+ // D3D9 reads the strength from c0 and the D3D11 transcription from the texture factor's alpha
+ DX8Wrapper::_Get_D3D_Device()->SetPixelShaderConstantF(0, D3DXVECTOR4(WATER_REFLECTION_STRENGTH, WATER_REFLECTION_STRENGTH, WATER_REFLECTION_STRENGTH, WATER_REFLECTION_STRENGTH), 1);
+ DX8Wrapper::Set_DX8_Render_State(D3DRS_TEXTUREFACTOR, REAL_TO_INT(WATER_REFLECTION_STRENGTH * 255.0f) << 24);
+ DX8Wrapper::Set_Pixel_Shader(m_reflectionPixelShader);
+ DX8Wrapper::Set_DX8_Render_State(D3DRS_ZFUNC, D3DCMP_LESSEQUAL);
+ DX8Wrapper::Set_DX8_Render_State(D3DRS_CULLMODE, D3DCULL_NONE);
+
+ DX8Wrapper::Draw_Triangles(0, triangleCount, 0, vertexCount);
+
+ DX8Wrapper::Set_Pixel_Shader(NULL);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU|0);
+ DX8Wrapper::Set_DX8_Texture_Stage_State(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
+ DX8Wrapper::Set_DX8_Texture(0, NULL);
+ ShaderClass::Invalidate(); //everything above was set behind the shader's back
+}
+
void WaterRenderObjClass::setupFlatWaterShader(void)
{
@@ -3512,6 +3623,9 @@ void WaterRenderObjClass::drawTrapezoidWater(Vector3 points[4])
W3DShaderManager::resetShader(W3DShaderManager::ST_SHROUD_TEXTURE);
}
}
+
+ drawReflection(rectangleCount*2, (rectangleCount+1)*2);
+
DX8Wrapper::Set_DX8_Render_State(D3DRS_CULLMODE, cull);
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.cpp
index d07322555..6cb1a18d2 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.cpp
@@ -97,7 +97,8 @@ CameraClass::CameraClass(void) :
ZFar(1000.0f), // far clip plane distance
ZBufferMin(0.0f), // smallest value we'll write into the z-buffer
ZBufferMax(1.0f), // largest value we'll write into the z-buffer
- FrustumValid(false)
+ FrustumValid(false),
+ ObliqueNearPlaneEnabled(false)
{
Set_Transform(Matrix3D(1));
Set_View_Plane(DEG_TO_RADF(50.0f));
@@ -131,7 +132,9 @@ CameraClass::CameraClass(const CameraClass & src) :
CameraInvTransform(src.CameraInvTransform),
AspectRatio(src.AspectRatio),
ZBufferMin(src.ZBufferMin),
- ZBufferMax(src.ZBufferMax)
+ ZBufferMax(src.ZBufferMax),
+ ObliqueNearPlaneEnabled(src.ObliqueNearPlaneEnabled),
+ ObliqueNearPlane(src.ObliqueNearPlane)
{
// just being paraniod in case any parent class doesn't completely copy the entire state...
FrustumValid = false;
@@ -165,7 +168,9 @@ CameraClass & CameraClass::operator = (const CameraClass & that)
NearClipBBox = that.NearClipBBox;
ProjectionTransform = that.ProjectionTransform;
CameraInvTransform = that.CameraInvTransform;
-
+ ObliqueNearPlaneEnabled = that.ObliqueNearPlaneEnabled;
+ ObliqueNearPlane = that.ObliqueNearPlane;
+
// just being paraniod in case any parent class doesn't completely copy the entire state...
FrustumValid = false;
}
@@ -796,6 +801,33 @@ void CameraClass::Get_D3D_Projection_Matrix(Matrix4x4 * set_tm)
(*set_tm)[2][3] = -ZNear * oozdiff;
}
+ if (ObliqueNearPlaneEnabled && Projection == PERSPECTIVE) {
+ /*
+ ** Lengyel's oblique near plane for a 0<z<1 depth range: the depth row becomes the mirror
+ ** plane in view space, scaled so the far plane still passes through the far corner of
+ ** the frustum on the plane's kept side.
+ */
+ const Matrix3D & camera_to_world = Get_Transform();
+ Vector3 view_normal;
+ Matrix3D::Inverse_Rotate_Vector(camera_to_world, ObliqueNearPlane.N, &view_normal);
+ float view_distance = Vector3::Dot_Product(ObliqueNearPlane.N, camera_to_world.Get_Translation()) - ObliqueNearPlane.D;
+
+ Matrix4x4 & projection = *set_tm;
+ float corner_x = ((view_normal.X > 0.0f ? 1.0f : -1.0f) + projection[0][2]) / projection[0][0];
+ float corner_y = ((view_normal.Y > 0.0f ? 1.0f : -1.0f) + projection[1][2]) / projection[1][1];
+ float corner_z = -1.0f;
+ float corner_w = (1.0f + projection[2][2]) / projection[2][3];
+ float scale = 1.0f / (view_normal.X * corner_x + view_normal.Y * corner_y + view_normal.Z * corner_z + view_distance * corner_w);
+ projection[2] = Vector4(view_normal.X * scale, view_normal.Y * scale, view_normal.Z * scale, view_distance * scale);
+ }
+}
+
+void CameraClass::Set_Oblique_Near_Plane(const PlaneClass * world_plane)
+{
+ ObliqueNearPlaneEnabled = (world_plane != NULL);
+ if (world_plane != NULL) {
+ ObliqueNearPlane = *world_plane;
+ }
}
void CameraClass::Get_View_Matrix(Matrix3D * set_tm)
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.h
index d2177808f..445798b78 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/camera.h
@@ -182,6 +182,11 @@ public:
void Get_Projection_Matrix(Matrix4x4 * set_tm);
void Get_D3D_Projection_Matrix(Matrix4x4 * set_tm);
void Get_View_Matrix(Matrix3D * set_tm);
+
+ // A mirror pass renders from beneath the mirror, so the near plane is swapped for the mirror
+ // plane itself and whatever lies behind it is clipped by the projection. NULL restores the
+ // ordinary near plane. Frustum culling does not see this; only the projection does.
+ void Set_Oblique_Near_Plane(const PlaneClass * world_plane);
const Matrix4x4 & Get_Projection_Matrix(void);
const Matrix3D & Get_View_Matrix(void);
@@ -253,6 +258,8 @@ protected:
mutable OBBoxClass NearClipBBox; // obbox which bounds the near clip plane
mutable Matrix4x4 ProjectionTransform;
mutable Matrix3D CameraInvTransform;
+ bool ObliqueNearPlaneEnabled;
+ PlaneClass ObliqueNearPlane; // world space, kept side in front of the normal
};
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
index bb3769493..3bc2df876 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.cpp
@@ -66,6 +66,8 @@ static const EngineShaderEntry ENGINE_SHADERS[] = {
{ ENGINE_SHADER_WATER_TRAPEZOID, "trapezoid water ps.1.1", "engine:trapezoidwater",
NULL, NULL },
{ ENGINE_SHADER_WATER_RIVER, "river water ps.1.1", "engine:riverwater", NULL, NULL },
+ { ENGINE_SHADER_WATER_REFLECTION, "water reflection ps.1.1", "engine:waterreflection",
+ NULL, NULL },
{ ENGINE_SHADER_TERRAIN, "terrain.pso", "engine:terrain",
TERRAIN_OPENING, TERRAIN_CHAIN },
{ ENGINE_SHADER_TERRAIN_NOISE, "terrainnoise.pso", "engine:terrainnoise",
@@ -278,6 +280,25 @@ static void write_river_water(std::string & hlsl)
" current.rgb = saturate(current.rgb + sparkle);\n";
}
+/*
+** The reflection W3DWater lays over map water once the water itself is drawn:
+**
+** tex t0 ; the mirrored scene, looked up through a projected stage
+** mul r0, 1-t0, c0
+** mov r0, 1-r0
+**
+** 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.
+*/
+static void write_water_reflection(std::string & hlsl)
+{
+ write_pixel_preamble(hlsl);
+ hlsl +=
+ " float4 current = 1.0 - saturate((1.0 - texel0) * TextureFactor.a);\n";
+}
+
// Every ps_1_1 instruction clamps its result to zero and one, so each step saturates and not only
// the last: a chain that overflows in the middle and comes back down is a different colour with the
// clamps than without them.
@@ -357,6 +378,9 @@ bool EngineShader_Pixel_Program(EngineShaderProgram program,
else if (program == ENGINE_SHADER_WATER_RIVER) {
write_river_water(hlsl);
}
+ else if (program == ENGINE_SHADER_WATER_REFLECTION) {
+ write_water_reflection(hlsl);
+ }
else {
return false;
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.h
index b301576cc..6e73bc83b 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/engineshader.h
@@ -59,6 +59,7 @@ enum EngineShaderProgram
ENGINE_SHADER_TREES,
ENGINE_SHADER_WATER_TRAPEZOID,
ENGINE_SHADER_WATER_RIVER,
+ ENGINE_SHADER_WATER_REFLECTION,
ENGINE_SHADER_TERRAIN,
ENGINE_SHADER_TERRAIN_NOISE,
ENGINE_SHADER_TERRAIN_NOISE_2,
diff --git a/GeneralsMD/Code/Tests/test_engineshader.cpp b/GeneralsMD/Code/Tests/test_engineshader.cpp
index 9ac7cbb99..2872a98bb 100644
--- a/GeneralsMD/Code/Tests/test_engineshader.cpp
+++ b/GeneralsMD/Code/Tests/test_engineshader.cpp
@@ -64,6 +64,7 @@ TEST(engineshader_names_a_shader_by_its_file_whatever_case_the_path_is_in)
CHECK(EngineShader_From_File("Trees.vso") == ENGINE_SHADER_TREES);
CHECK(EngineShader_From_File("trapezoid water ps.1.1") == ENGINE_SHADER_WATER_TRAPEZOID);
CHECK(EngineShader_From_File("river water ps.1.1") == ENGINE_SHADER_WATER_RIVER);
+ CHECK(EngineShader_From_File("water reflection ps.1.1") == ENGINE_SHADER_WATER_REFLECTION);
CHECK(EngineShader_From_File("shaders\\terrainnoise2.pso") == ENGINE_SHADER_TERRAIN_NOISE_2);
CHECK(EngineShader_From_File("shaders\\fterrain0.pso") == ENGINE_SHADER_FLAT_TERRAIN_BASE);
CHECK(EngineShader_From_File("shaders\\roadnoise2.pso") == ENGINE_SHADER_ROAD_NOISE_2);
@@ -74,7 +75,7 @@ TEST(engineshader_names_a_shader_by_its_file_whatever_case_the_path_is_in)
TEST(engineshader_writes_every_program_it_names)
{
static const EngineShaderProgram PIXEL_PROGRAMS[] = {
- ENGINE_SHADER_WATER_TRAPEZOID, ENGINE_SHADER_WATER_RIVER,
+ ENGINE_SHADER_WATER_TRAPEZOID, ENGINE_SHADER_WATER_RIVER, ENGINE_SHADER_WATER_REFLECTION,
ENGINE_SHADER_TERRAIN, ENGINE_SHADER_TERRAIN_NOISE, ENGINE_SHADER_TERRAIN_NOISE_2,
ENGINE_SHADER_FLAT_TERRAIN, ENGINE_SHADER_FLAT_TERRAIN_BASE,
ENGINE_SHADER_FLAT_TERRAIN_NOISE, ENGINE_SHADER_FLAT_TERRAIN_NOISE_2,
@@ -136,6 +137,18 @@ TEST(engineshader_darkens_only_the_river_waters_sparkles_by_the_vertex_alpha)
CHECK(contains(hlsl, "current.rgb = saturate(current.rgb + sparkle);"));
}
+// mul r0.rgb, t0, t3 ; +mov r0.a, c0. D3D9 takes the opacity from c0, which a transcribed program
+// cannot see, so the alpha has to be the texture factor W3DWater sets to the same value. Reading
+// c0 here would come back zero and the reflection would not show at all.
+// The reflection multiplies the water, so white has to come out white or open sky tints every pool.
+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, "* texel3"));
+}
+
// D3D9 applies the alpha test and the fog around a bound pixel shader and D3D11 applies neither, so
// a transcription that leaves them out draws water through the fog at full strength.
TEST(engineshader_writes_the_alpha_test_and_the_fog_into_a_water_program)
@@ -346,4 +359,9 @@ TEST(engineshader_writes_programs_the_compiler_accepts)
std::string river;
CHECK(EngineShader_Pixel_Program(ENGINE_SHADER_WATER_RIVER, pipeline, river));
CHECK(compiles(compile, river, EngineShader_Name(ENGINE_SHADER_WATER_RIVER), PIXEL_PROFILE));
+
+ std::string reflection;
+ CHECK(EngineShader_Pixel_Program(ENGINE_SHADER_WATER_REFLECTION, pipeline, reflection));
+ CHECK(compiles(compile, reflection, EngineShader_Name(ENGINE_SHADER_WATER_REFLECTION),
+ PIXEL_PROFILE));
}