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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(ww3d2): keep normal mapping under point lights
Olcay Seygan2be66f718070
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 94e11bd30..a3917187a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -257,6 +257,7 @@ found and fixed — EA's own, not port damage.**- The ground is drawn at twice the detail. Every map square used to get a 64-pixel patch of its ground texture, squeezed into sixteen-bit colour; it gets 128 pixels now in full colour, so sand, gravel and tracks stop turning to blocks and bands when you zoom in, and a ground texture that ships at twice the size is shown at that size.- The ground has relief. Every grain of sand, pebble and crack catches the map's own sun on one side and falls into shade on the other, worked out from the ground's own picture when the map loads, and it follows the sun on a dusk map or a night one. The light the ground already had from its hills is kept; the relief goes on top. It is part of the new picture, so a machine on the old renderer keeps the flat ground.- A damaged building looks damaged, not carved out of marble. The damaged and burning versions of a building's skin are separate pictures a quarter the size of the healthy one, and the surface detail made from them spread one blotch of soot into a hill across a whole wall panel, so a command centre under fire turned black and white. They draw flat now, and the panels, rivets and deck stripes show through the soot again.+- A tank keeps its surface detail when it fires. The flash of a gun, an explosion or a fire lights whatever stands next to it, and that light used to switch the whole model back to the old flat lighting for as long as it lasted, so every shot and every blast nearby wiped the relief off a vehicle for a moment. The flash is added on top now and the sun keeps drawing the detail.- A long thin texture loads at the size it was drawn at. Anything wider than eight to one used to be stretched onto a bigger, blurrier one, because that was the limit of a 2002 graphics card; the card is asked now, and modern ones have no such limit.- Anything standing still keeps its shadow in the fog. A town you have already walked through usedto sit on flat ground the moment you looked away, buildings and derricks and walls with nothingdiff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cppindex 66e1c39fe..2f4d0b2ee 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx11backend.cpp@@ -677,22 +677,16 @@ void DX11BackendClass::Set_Normal_Map(ID3D11ShaderResourceView * normal_map)NormalMap = normal_map;}-// Directional lights only, because the pixel half sums nothing else; a draw under a point light is-// lit per vertex the way it always was. A transcribed program is its own lighting.+// The pixel half bumps the directional lights; a point or spot light is summed per vertex into the+// base it adds to (ffvertex.cpp). It used to turn the whole draw back to per-vertex lighting, so a+// tank lost its relief every time its own gun flashed, and anything next to an explosion or a fire+// went flat with it. A transcribed program is its own lighting.bool DX11BackendClass::Normal_Mapped() const{- if (NormalMap == NULL || Textures[0] == NULL- || VertexProgram != ENGINE_SHADER_NONE || PixelProgram != ENGINE_SHADER_NONE- || RenderStates.Get_Render_State(D3DRS_LIGHTING) == FALSE- || (VertexFormat & D3DFVF_NORMAL) == 0) {- return false;- }- for (unsigned index = 0; index < MAXIMUM_VERTEX_LIGHTS; ++index) {- if (Lights[index].Enabled && Lights[index].Type != D3DLIGHT_DIRECTIONAL) {- return false;- }- }- return true;+ return NormalMap != NULL && Textures[0] != NULL+ && VertexProgram == ENGINE_SHADER_NONE && PixelProgram == ENGINE_SHADER_NONE+ && RenderStates.Get_Render_State(D3DRS_LIGHTING) != FALSE+ && (VertexFormat & D3DFVF_NORMAL) != 0;}bool DX11BackendClass::Terrain_Bumped() const@@ -1568,13 +1562,14 @@ void DX11BackendClass::Upload_Constants()pixel_block.AlphaReference[0] =static_cast<float>(RenderStates.Get_Render_State(D3DRS_ALPHAREF) & 0xff);- // The normal mapped program's lights, the enabled ones first and in camera space like the- // vertex block's. A slot with no light gets a direction anyway: the highlight normalises the- // half vector, and a zero direction there is a NaN that no zero colour cancels.+ // The normal mapped program's lights, the enabled directional ones first and in camera space+ // like the vertex block's; a point or spot light is already in the vertex colour it adds to. A+ // slot with no light gets a direction anyway: the highlight normalises the half vector, and a+ // zero direction there is a NaN that no zero colour cancels.unsigned normal_slot = 0;for (unsigned index = 0; index < MAXIMUM_VERTEX_LIGHTS && normal_slot < NORMAL_MAPPED_LIGHTS;++index) {- if (!Lights[index].Enabled) {+ if (!Lights[index].Enabled || Lights[index].Type != D3DLIGHT_DIRECTIONAL) {continue;}float * direction = pixel_block.NormalLightDirection[normal_slot];diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffvertex.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffvertex.cppindex 2cd33e5a7..01ce07838 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffvertex.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/ffvertex.cpp@@ -102,7 +102,7 @@ static bool material_source_expression(DWORD source, const char * material_const}}-static void append_light(std::string & body, unsigned index, DWORD type)+static void append_light(std::string & body, unsigned index, DWORD type, const char * accumulator){char line[1024];@@ -147,8 +147,8 @@ static void append_light(std::string & body, unsigned index, DWORD type)snprintf(line, sizeof(line)," float lambert = max(dot(view_normal, to_light), 0.0);\n"- " diffuse_light += Light%uDiffuse.rgb * lambert * attenuation;\n",- index);+ " %s += Light%uDiffuse.rgb * lambert * attenuation;\n",+ accumulator, index);body += line;// The specular term is Blinn's half vector, which is what D3D9's fixed-function pipeline uses@@ -504,11 +504,17 @@ bool VertexShader_Generate(const VertexPipelineDescription & description,//// A light's own ambient is not in that second sum because DX11BackendClass::Set_Light does// not carry one; every light W3D creates leaves it black.+ // A normal mapped draw lights its directional lights per pixel. A point or spot light - the+ // flash of a gun, the glow of a fire - stays per vertex and is summed on its own, so it can+ // be handed to the pixel half as part of the base it adds the bumped light to.body += " float3 diffuse_light = float3(0.0, 0.0, 0.0);\n";+ body += " float3 local_light = float3(0.0, 0.0, 0.0);\n";body += " float3 specular_light = float3(0.0, 0.0, 0.0);\n";for (unsigned index = 0; index < description.LightCount; ++index) {- append_light(body, index, description.Lights[index].Type);+ const DWORD type = description.Lights[index].Type;+ append_light(body, index, type, type == D3DLIGHT_DIRECTIONAL ? "diffuse_light" : "local_light");}+ body += " diffuse_light += local_light;\n";std::string diffuse;std::string ambient;@@ -529,8 +535,8 @@ bool VertexShader_Generate(const VertexPipelineDescription & description,if (description.NormalMapped) {body += " output.ViewPosition = view_position.xyz;\n";body += " output.ViewNormal = view_normal;\n";- body += " output.LitBase = " + ambient + ".rgb * GlobalAmbient.rgb + " + emissive- + ".rgb;\n";+ body += " output.LitBase = " + diffuse + ".rgb * local_light + " + ambient+ + ".rgb * GlobalAmbient.rgb + " + emissive + ".rgb;\n";body += " output.LitMaterial = " + diffuse + ".rgb;\n";}diff --git a/GeneralsMD/Code/Tests/test_ffvertex.cpp b/GeneralsMD/Code/Tests/test_ffvertex.cppindex 64fcaabad..bddfd8112 100644--- a/GeneralsMD/Code/Tests/test_ffvertex.cpp+++ b/GeneralsMD/Code/Tests/test_ffvertex.cpp@@ -119,6 +119,25 @@ TEST(ffvertex_a_point_light_attenuates_and_stops_at_its_range)CHECK(!contains(hlsl, "spot_cosine"));}+// A normal mapped draw bumps its directional lights per pixel. A gun flash or a fire is a point+// light, and it used to send the whole draw back to vertex lighting; now it rides in the base.+TEST(ffvertex_a_normal_mapped_draw_carries_its_point_lights_in_the_base)+{+ VertexPipelineDescription description = plain_description();+ description.LightingEnabled = true;+ description.NormalMapped = true;+ description.LightCount = 2;+ description.Lights[0].Type = D3DLIGHT_DIRECTIONAL;+ description.Lights[1].Type = D3DLIGHT_POINT;++ std::string hlsl;+ CHECK(VertexShader_Generate(description, VERTEX_SHADER_TARGET_D3D11, hlsl));+ CHECK(contains(hlsl, "diffuse_light += Light0Diffuse.rgb"));+ CHECK(contains(hlsl, "local_light += Light1Diffuse.rgb"));+ CHECK(contains(hlsl, "diffuse_light += local_light;"));+ CHECK(contains(hlsl, ".rgb * local_light + "));+}+TEST(ffvertex_a_spot_light_adds_the_cone_to_the_point_light_terms){VertexPipelineDescription description = plain_description();