diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d7ada2..4a18925 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ add_subdirectory(WoW/Source/DB) add_subdirectory(WoW/Source/Game) add_subdirectory(WoW/Source/Glue) add_subdirectory(WoW/Source/Magic) +add_subdirectory(WoW/Source/LiquidSystem) add_subdirectory(WoW/Source/Minigame) add_subdirectory(WoW/Source/Net) add_subdirectory(WoW/Source/Object) diff --git a/Engine/Source/Base/CDataAllocator.h b/Engine/Source/Base/CDataAllocator.h index bb2b34e..fb5cb96 100644 --- a/Engine/Source/Base/CDataAllocator.h +++ b/Engine/Source/Base/CDataAllocator.h @@ -2,6 +2,9 @@ #define ENGINE_SOURCE_BASE_CDATAALLOCATOR_H #include +#include +#include +#include "Engine/Source/Storm/H/storm.h" class CDataAllocator { public: @@ -24,6 +27,17 @@ class CDataAllocator { void Clear(const char* fileName, int32_t lineNumber, int32_t reportLeaks); void* GetData(int32_t zero, const char* fileName, int32_t lineNumber); void PutData(void* data, const char* fileName, int32_t lineNumber); + + template + T* New(int32_t zero); }; +// VERIFY: 6cf1bf27 manual +// OFFSET: [0x007D4920] +template +T* CDataAllocator::New(int32_t zero) { + void* data = this->GetData(zero, typeid(T).raw_name(), SLOG_OBJECT); + return data ? new (data) T : nullptr; +} + #endif diff --git a/Engine/Source/DayNight/DayNight.h b/Engine/Source/DayNight/DayNight.h index 38fb1c3..eed8517 100644 --- a/Engine/Source/DayNight/DayNight.h +++ b/Engine/Source/DayNight/DayNight.h @@ -7,6 +7,9 @@ #include "Engine/Source/Tempest/CImVector.h" #include "Engine/Source/Storm/H/stpl.h" +class LightFloatBandRec; +class LightParamsRec; + class C3Vector; class CImVector; class CM2Model; @@ -66,6 +69,9 @@ class DNLightBands { /* 0090 */ float m_skyboxWeight2; /* 0094 */ uint32_t m_cloudTypeId; /* 0098 */ float m_cloudWeight; + + DNLightBands(); + void Copy(const DNLightBands* src); }; struct DNOverrideSky { @@ -153,6 +159,9 @@ struct DNPlanet { /* 0014 */ float m_scale; /* 0018 */ float m_baseScale; /* 001C */ float m_period; + + void Initialize(const char* fileName); + void Render(); }; } @@ -177,6 +186,10 @@ struct DNSky { /* 0028 */ uint16_t m_nVerts; /* 002A */ uint16_t m_nIndices; /* 002C */ float m_sphRadius; + + void Render(); + void GenSphere(float sphRadius); + void SetColors(); }; } @@ -190,6 +203,32 @@ struct DNStars { }; } +namespace DayNight { + float InterpFloatBand(const LightFloatBandRec* rec, int32_t now, int32_t bandIndex); + float CalcIndividualColorFromDBRec(int32_t bandTime, const LightParamsRec* params, int32_t bandIndex); + void LoadLightingBands(int32_t bandTime, DNLightBands* bands, LightParamsRec* params); + float CalcFogRate(float fogStart, float fogEnd); + void CalcColors(int32_t bandTime, DNLightBands* bands, LightParamsRec* params); + void BlendColors(DNLightBands* dst, const DNLightBands* src, float t); + LightParamsRec* GetLightParams(const LightRec* light, int32_t slot); + void SetLightColors(); + DNOverrideSky* GetOverrideSky(const char* path, uint32_t flags); + void LoadMap(int32_t zoneID); + void LightLoad(TSGrowableArray* lightArray, uint32_t a2); + void ColorLerpBytes(CImVector* out, const CImVector* a, const CImVector* b, float t); + void BlendOutputs(DNLightBands* dst, const DNLightBands* src, float t); + void BlendAreaLightColors(DNLightBands* dst, const LightRec* light, int32_t clearSlot, int32_t stormSlot); + void BlendAreaLight(DNLightBands* dst, float maxFade, const LightRec* light, int32_t clearSlot, int32_t stormSlot); + void BlendZoneLight(DNLightBands* dst, float maxFade, const LightRec* light, float distPct, int32_t clearSlot, int32_t stormSlot); + void DoAreaLights(DNLightBands* light, int32_t underwater); + void UpdateFog(); + void SetDirection(); + void UpdateLighting(); + void Update(int32_t reset, const C3Vector* cameraPos); + void SetPlanets(); + CImVector DarkenColor(CImVector colour, float scale); +} + struct SMOFog { /* 0000 */ uint32_t m_flags; /* 0004 */ C3Vector m_position; @@ -203,4 +242,43 @@ struct SMOFog { /* 002C */ CImVector m_uwFogColor; }; +namespace DayNight { + + class LightQE { + public: + float m_key; // +0x00 + LightRec** m_light; // +0x04 + + static int32_t s_localSort; + + static bool HasHigherPriority(const LightQE& a, const LightQE& b); + }; + +} + +namespace DayNight { + + class LightQueue { + public: + void* m_vtable; + void* m_alloc; + uint32_t m_allocBytes; + LightQE* m_data; + uint32_t m_dataBytes; + uint32_t m_chunk; + uint32_t m_capacity; + uint32_t m_count; + + LightQueue(uint32_t initial, uint32_t chunk); + ~LightQueue(); + + bool Resize(uint32_t bytes, bool noClear); + void Insert(float key, LightRec** light); + bool Erase(uint32_t index, uint32_t count); + void Pop(LightQE* out); + }; + +} + + #endif diff --git a/Engine/Source/Gx/CGxDevice/CGxDevice.cpp b/Engine/Source/Gx/CGxDevice/CGxDevice.cpp index 641827e..f9f0053 100644 --- a/Engine/Source/Gx/CGxDevice/CGxDevice.cpp +++ b/Engine/Source/Gx/CGxDevice/CGxDevice.cpp @@ -1918,9 +1918,9 @@ void CGxDevice::RsSet(EGxRenderState which, int32_t value) { CGxAppRenderState& state = this->m_appRenderStates[which]; - if (state.m_value.m_data.i[0] != value) { + if (state.m_value.m_data.p != reinterpret_cast(static_cast(value))) { this->IRsDirty(which); - state.m_value.m_data.i[0] = value; + state.m_value.m_data.p = reinterpret_cast(static_cast(value)); } } diff --git a/Engine/Source/Gx/Gx.cpp b/Engine/Source/Gx/Gx.cpp index 68fd1bc..6f5f251 100644 --- a/Engine/Source/Gx/Gx.cpp +++ b/Engine/Source/Gx/Gx.cpp @@ -826,9 +826,9 @@ void GxRsSet(EGxRenderState which, int32_t value) { CGxAppRenderState& state = g_theGxDevicePtr->m_appRenderStates[which]; - if (state.m_value.m_data.i[0] != value) { + if (state.m_value.m_data.p != reinterpret_cast(static_cast(value))) { g_theGxDevicePtr->IRsDirty(which); - state.m_value.m_data.i[0] = value; + state.m_value.m_data.p = reinterpret_cast(static_cast(value)); } } diff --git a/Engine/Source/Model2/M2Light.h b/Engine/Source/Model2/M2Light.h index 238a2d6..6325c25 100644 --- a/Engine/Source/Model2/M2Light.h +++ b/Engine/Source/Model2/M2Light.h @@ -67,6 +67,7 @@ class CM2Lighting { CM2Lighting(); CM2Lighting(const CAaSphere& sphere); + CM2Lighting(const C3Vector& position); void Reset(); void AddAmbient(const C3Vector& color); void AddDiffuse(const C3Vector& color, const C3Vector& dir); diff --git a/Engine/Source/Model2/M2Model.cpp b/Engine/Source/Model2/M2Model.cpp index 03c0b76..c65ec53 100644 --- a/Engine/Source/Model2/M2Model.cpp +++ b/Engine/Source/Model2/M2Model.cpp @@ -7863,3 +7863,30 @@ CM2Lighting::CM2Lighting(const CAaSphere& sphere) { this->m_liquidPlane.d = 0.0f; this->Initialize(nullptr, sphere); } + +// OFFSET: 0x007B0F10 +CM2Lighting::CM2Lighting(const C3Vector& position) { + this->m_sphere4.c.x = 0.0f; + this->m_sphere4.r = 0.0f; + this->m_sphere4.c.y = 0.0f; + this->m_sphere4.c.z = 0.0f; + this->m_vector18 = C3Vector(0.0f, 0.0f, 0.0f); + this->m_vector24 = C3Vector(0.0f, 0.0f, 0.0f); + this->m_vector30 = C3Vector(0.0f, 0.0f, 0.0f); + this->m_vector3C = C3Vector(0.0f, 0.0f, 0.0f); + this->m_vector48 = C3Vector(0.0f, 0.0f, 0.0f); + this->m_sunAmbient = C3Vector(0.0f, 0.0f, 0.0f); + this->m_sunDiffuse = C3Vector(0.0f, 0.0f, 0.0f); + this->m_sunSpecular = C3Vector(0.0f, 0.0f, 0.0f); + this->m_sunDir = C3Vector(0.0f, 0.0f, 0.0f); + this->m_fogColor = C3Vector(0.0f, 0.0f, 0.0f); + this->m_liquidPlane.n.x = 0.0f; + this->m_liquidPlane.n.y = 0.0f; + this->m_liquidPlane.n.z = 1.0f; + this->m_liquidPlane.d = 0.0f; + + CAaSphere sphere; + sphere.c = position; + sphere.r = 0.0f; + this->Initialize(nullptr, sphere); +} diff --git a/Engine/Source/Storm/H/stpl.h b/Engine/Source/Storm/H/stpl.h index c4d7d0f..a58f126 100644 --- a/Engine/Source/Storm/H/stpl.h +++ b/Engine/Source/Storm/H/stpl.h @@ -137,6 +137,23 @@ class TSGrowableArray : public TSFixedArray { void SetCount(uint32_t count); }; +template +class TSCGrowableArray { + public: + /* 0000 */ union { + T m_inline[N]; + TSGrowableArray m_dynamic; + }; + uint32_t m_count; + + TSCGrowableArray(); + ~TSCGrowableArray(); + T& operator[](uint32_t index); + void BecomeDynamic(); + T* New(const T& source); + void SetCount(uint32_t count); +}; + template class TSHashObject { public: @@ -1529,4 +1546,50 @@ void TSBitField::Load(const void* data, uint32_t bytes, int32_t bits) { memcpy(this->m_bits.m_data, data, bytes); } +// VERIFY: 47bab0b9 auto +// OFFSET: [0x007917B0] +template +T& TSCGrowableArray::operator[](uint32_t index) { + if (this->m_count == 0xFFFFFFFF) { + return this->m_dynamic.m_data[index]; + } + + return this->m_inline[index]; +} + +// VERIFY: 2e6b9329 manual +// OFFSET: [0x007B3B70] +template +void TSCGrowableArray::BecomeDynamic() { + uint32_t count = this->m_count; + + if (static_cast(count) > static_cast(N)) { + count = N; + } + + T saved[N]; + + if (count) { + memcpy(saved, this->m_inline, sizeof(T) * count); + } + + new (&this->m_dynamic) TSGrowableArray(); + this->m_dynamic.Add(count, saved); + this->m_dynamic.Add(this->m_count - count, &this->m_inline[count]); + this->m_count = 0xFFFFFFFF; +} + +// INLINED +template +void TSCGrowableArray::SetCount(uint32_t count) { + if (this->m_count == 0xFFFFFFFF) { + this->m_dynamic.SetCount(count); + } else if (count <= N) { + this->m_count = count; + } else { + this->BecomeDynamic(); + this->m_dynamic.SetCount(count); + } +} + #endif diff --git a/Engine/Source/Tempest/C2iVector.cpp b/Engine/Source/Tempest/C2iVector.cpp index f5e6ff7..0c1acfb 100644 --- a/Engine/Source/Tempest/C2iVector.cpp +++ b/Engine/Source/Tempest/C2iVector.cpp @@ -6,3 +6,9 @@ uint32_t C2iVector::MajorAxis() const { return abs(this->x) <= abs(this->y); } + +// VERIFY: f2fd55ef auto +// OFFSET: 0x006839F0 +bool operator<(const C2iVector& l, const C2iVector& r) { + return l.x < r.x && l.y < r.y; +} diff --git a/Engine/Source/Tempest/C2iVector.h b/Engine/Source/Tempest/C2iVector.h index bbb7bcd..3b73730 100644 --- a/Engine/Source/Tempest/C2iVector.h +++ b/Engine/Source/Tempest/C2iVector.h @@ -12,4 +12,6 @@ class C2iVector { uint32_t MajorAxis() const; }; +bool operator<(const C2iVector& l, const C2iVector& r); + #endif diff --git a/Tools/WowClient/CMakeLists.txt b/Tools/WowClient/CMakeLists.txt index 2fbf2d6..62c798e 100644 --- a/Tools/WowClient/CMakeLists.txt +++ b/Tools/WowClient/CMakeLists.txt @@ -74,6 +74,7 @@ target_link_libraries(WowClient UIUtil Ui WorldClient + LiquidSystem WowServices WowSvcs Triage diff --git a/WoW/Source/CMakeLists.txt b/WoW/Source/CMakeLists.txt index 5088af9..31babdf 100644 --- a/WoW/Source/CMakeLists.txt +++ b/WoW/Source/CMakeLists.txt @@ -61,6 +61,7 @@ target_link_libraries(Wow DB Game Glue + LiquidSystem Magic Minigame Net diff --git a/WoW/Source/DB/DBClient/Lights.cpp b/WoW/Source/DB/DBClient/Lights.cpp index c7884d9..3301b48 100644 --- a/WoW/Source/DB/DBClient/Lights.cpp +++ b/WoW/Source/DB/DBClient/Lights.cpp @@ -117,3 +117,35 @@ CImVector DayNight::LoadLightIntBand(int32_t time, const LightParamsRec* params, g_lightIntBandDB.GetRecord(band + 18 * params->m_id - 17, &rec); return InterpolateIntBand(rec, time); } + +// VERIFY: 95c2a7e6 auto +// OFFSET: 0x007EAEF0 +float DayNight::InterpFloatBand(const LightFloatBandRec* rec, int32_t now, int32_t bandIndex) { + if (!rec->m_num) { + return 0.0f; + } + + float t; + int32_t lo; + int32_t hi; + GetBandTimeRange(rec->m_time, rec->m_num, now, lo, t, hi); + + if (bandIndex) { + return (rec->m_data[hi] - rec->m_data[lo]) * t + rec->m_data[lo]; + } + + float loValue = rec->m_data[lo] * 0.027777778f; + return loValue + (0.027777778f * rec->m_data[hi] - loValue) * t; +} + +// VERIFY: fb110f5d manual +// OFFSET: 0x007ED2D0 +void DayNight::ColorLerpBytes(CImVector* out, const CImVector* a, const CImVector* b, float t) { + float blue = b->b < a->b ? a->b - (a->b - b->b) * t : a->b + (b->b - a->b) * t; + float green = b->g < a->g ? a->g - (a->g - b->g) * t : a->g + (b->g - a->g) * t; + float red = b->r < a->r ? a->r - (a->r - b->r) * t : a->r + (b->r - a->r) * t; + out->b = CMath::fint_(blue - 0.5f); + out->g = CMath::fint_(green - 0.5f); + out->r = CMath::fint_(red - 0.5f); + out->a = 0xFF; +} diff --git a/WoW/Source/LiquidSystem/CMakeLists.txt b/WoW/Source/LiquidSystem/CMakeLists.txt new file mode 100644 index 0000000..99de393 --- /dev/null +++ b/WoW/Source/LiquidSystem/CMakeLists.txt @@ -0,0 +1,10 @@ +file(GLOB_RECURSE PRIVATE_SOURCES CONFIGURE_DEPENDS "*.cpp") + +add_library(LiquidSystem STATIC + ${PRIVATE_SOURCES} +) + +target_include_directories(LiquidSystem + PUBLIC + ${CMAKE_SOURCE_DIR} +) diff --git a/WoW/Source/LiquidSystem/Liquid.cpp b/WoW/Source/LiquidSystem/Liquid.cpp new file mode 100644 index 0000000..0a92ada --- /dev/null +++ b/WoW/Source/LiquidSystem/Liquid.cpp @@ -0,0 +1,129 @@ +#include "Liquid.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00D43B34 +CDataAllocator Liquid::s_unk_D43B34(0x6C, 0x20); + +// STATIC: 0x00B23F64 +float Liquid::s_unk_B23F64 = 1.0f; + +// STATIC: 0x00B23F68 +int32_t Liquid::s_unk_B23F68 = 1; + +// STATIC: 0x00D439E8 +IDatabase* Liquid::s_liquidMaterialDB; + +// STATIC: 0x00D439EC +IDatabase* Liquid::s_liquidTypeDB; + +// STATIC: 0x00D439F0 +char Liquid::s_unk_D439F0[256]; + +// STATIC: 0x00D43AF0 +int32_t Liquid::s_unk_D43AF0; + +// STATIC: 0x00D43AF4 +int32_t Liquid::s_unk_D43AF4; + +// STATIC: 0x00D43AF8 +int32_t Liquid::s_unk_D43AF8; + +// STATIC: 0x00D43AFC +int32_t Liquid::s_unk_D43AFC; + +// VERIFY: fb13c55c auto +// OFFSET: 0x008A1770 +int32_t Liquid::Initialize(int32_t a1, float a2, int32_t a3, const char* a4, IDatabase* liquidTypeDB, IDatabase* liquidMaterialDB) { + SStrCopy(Liquid::s_unk_D439F0, a4, sizeof(Liquid::s_unk_D439F0)); + Liquid::s_unk_B23F64 = a2; + Liquid::s_unk_D43AF8++; + Liquid::s_unk_D43AFC++; + Liquid::s_unk_D43AF4 = a1; + Liquid::s_unk_D43AF0 = a3; + Liquid::s_unk_B23F68 = 1; + Liquid::s_liquidTypeDB = liquidTypeDB; + Liquid::s_liquidMaterialDB = liquidMaterialDB; + return 1; +} + +// VERIFY: c3031eda manual +// OFFSET: 0x008A2400 +void Liquid::Shutdown() { + int32_t count = Liquid::s_unk_D43AFC--; + Liquid::s_liquidTypeDB = nullptr; + Liquid::s_liquidMaterialDB = nullptr; + + if (count == 1) { + Liquid::CMaterialBank::Destroy(); + Liquid::s_unk_D43AF0 = 0; + } + + Liquid::CSettingsBank::Destroy(); + Liquid::ClearMaterials(); + Liquid::s_unk_D43B34.Clear(".?AVCInstance@Liquid@@", SLOG_OBJECT, 1); +} + +// VERIFY: 58e8bcb0 auto +// OFFSET: 0x008A1700 +void Liquid::CInstance::AddRef() { + this->m_refCount++; +} + +// VERIFY: 01aeb3b0 manual +// OFFSET: 0x008A1710 +void Liquid::CInstance::Purge() { + this->m_geomFactory->Purge(); +} + +// VERIFY: e803d76a auto +// OFFSET: 0x008A1740 +int32_t Liquid::UseCameraRelativeCoords() { + return Liquid::s_unk_D43AF4; +} + +// VERIFY: d02f3d04 auto +// OFFSET: 0x008A1750 +float Liquid::WorldCoordScale() { + return Liquid::s_unk_B23F64; +} + +// VERIFY: ecbae4e7 auto +// OFFSET: 0x008A1760 +const char* Liquid::GetShaderSuffix() { + return Liquid::s_unk_D439F0; +} + +// VERIFY: 6413183d manual +// OFFSET: 0x008A1A40 +Liquid::CInstance::CInstance() { + this->m_material = nullptr; + this->m_settings = nullptr; + this->m_geomFactory = nullptr; + this->m_waveManager = nullptr; + this->m_environment = nullptr; + this->m_bounds.r = 0.0f; + this->m_queued = 0; + this->m_refCount = 1; +} + +// VERIFY: d1684687 manual +// OFFSET: 0x008A1AC0 +void Liquid::CInstance::Release() { + if (this->m_refCount-- == 1) { + this->m_geomFactory->Release(); + this->m_environment->Release(); + + if (this->m_waveManager) { + this->m_waveManager->Release(); + } + + Liquid::s_unk_D43B34.PutData(this, nullptr, 0); + } +} + +// VERIFY: 7f5df1db auto +// OFFSET: 0x008A1B00 +Liquid::CInstance* Liquid::CInstance::Create() { + void* mem = Liquid::s_unk_D43B34.GetData(0, ".?AVCInstance@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CInstance() : nullptr; +} diff --git a/WoW/Source/LiquidSystem/Liquid.h b/WoW/Source/LiquidSystem/Liquid.h new file mode 100644 index 0000000..cce0100 --- /dev/null +++ b/WoW/Source/LiquidSystem/Liquid.h @@ -0,0 +1,375 @@ +#ifndef WOW_SOURCE_LIQUIDSYSTEM_LIQUID_H +#define WOW_SOURCE_LIQUIDSYSTEM_LIQUID_H + +#include +#include +#include "Engine/Source/Base/CDataAllocator.h" +#include "Engine/Source/Gx/Types.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C2Vector.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/C4Vector.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "WoW/Source/LiquidSystem/Types.h" + +template +class IDatabase; +class CGxShader; +class CGxTex; +class CM2Lighting; +class CTexture; +class LiquidMaterialRec; +class LiquidTypeRec; + +namespace Liquid { + class CInstance; + class CMaterialSettings; + class IWaveManager; + class IEnvironment; + class IGeomFactory; + + struct VSLight { + /* 0000 */ C4Vector m_position; + /* 0010 */ C4Vector m_color; + /* 0020 */ C4Vector m_attenuation; + }; + + struct VSConstants { + /* 0000 */ C44Matrix m_projection; + /* 0040 */ C4Vector m_fog; + /* 0050 */ C44Matrix m_modelView; + /* 0090 */ C44Matrix m_texTransforms[6]; + /* 0210 */ C4Vector m_sunDir; + /* 0220 */ C4Vector m_sunAmbient; + /* 0230 */ C4Vector m_sunDiffuse; + /* 0240 */ C4Vector m_sunSpecular; + /* 0250 */ VSLight m_lights[3]; + }; + + struct VSWaveConstants { + /* 0000 */ float m_directionalPhase[4]; + /* 0010 */ float m_circularPhase[4]; + /* 0020 */ C4Vector m_directionalWaves[3]; + /* 0050 */ float unk_0050[4]; + /* 0060 */ float unk_0060[4]; + /* 0070 */ C4Vector m_circularWaves[3]; + /* 00A0 */ float unk_00A0[4]; + /* 00B0 */ float unk_00B0[4]; + }; + + struct PSConstants { + /* 0000 */ C44Matrix m_modelViewProj; + /* 0040 */ C4Vector m_fogColor; + /* 0050 */ C4Vector m_cameraPos; + }; + + struct PSProcWaterConstants { + /* 0000 */ C4Vector m_sunDir; + /* 0010 */ C4Vector m_sunAmbient; + /* 0020 */ C4Vector m_sunDiffuse; + /* 0030 */ C4Vector m_sunSpecular; + /* 0040 */ float m_params[8]; + }; + + class IEnvironment { + public: + virtual ~IEnvironment(); + virtual void Release() = 0; + virtual void SetInteriorFog(int32_t interiorFog) = 0; + virtual void SelectLights(CM2Lighting* lighting) = 0; + }; + + class IWaveManager { + public: + virtual ~IWaveManager(); + virtual void AddRef() = 0; + virtual void Release() = 0; + virtual void* GetData() = 0; + virtual int32_t GetCount() = 0; + }; + + class IGeomFactory { + public: + virtual ~IGeomFactory(); + virtual void Release() = 0; + virtual void GetBuffers(EGxVertexBufferFormat format, CGxBuf** vertexBuf, CGxBuf** indexBuf, CGxBatch* batch) = 0; + virtual void Purge() = 0; + virtual bool SetTransform(C44Matrix* transform) = 0; + }; + + class IMaterial { + public: + virtual ~IMaterial(); + virtual void Release() = 0; + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings) = 0; + }; + + class CMaterialProcWater : public IMaterial { + public: + static int32_t s_refCount; + static CGxShader* s_vertexShaders[4]; + static CGxShader* s_pixelShader; + + static CMaterialProcWater* Create(); + + virtual ~CMaterialProcWater(); + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + + CMaterialProcWater(); + }; + + class CMaterialProcWaterFFP : public IMaterial { + public: + static CMaterialProcWaterFFP* Create(); + + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + }; + + class CMaterialWater : public IMaterial { + public: + static int32_t s_refCount; + static CGxShader* s_vertexShaders[4]; + static CGxShader* s_pixelShader; + + static CMaterialWater* Create(); + + virtual ~CMaterialWater(); + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + + CMaterialWater(); + }; + + class CMaterialWaterNoSpec : public IMaterial { + public: + static int32_t s_refCount; + static CGxShader* s_vertexShaders[4]; + static CGxShader* s_pixelShader; + + static CMaterialWaterNoSpec* Create(); + + virtual ~CMaterialWaterNoSpec(); + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + + CMaterialWaterNoSpec(); + }; + + class CMaterialWaterFFP : public IMaterial { + public: + static CMaterialWaterFFP* Create(); + + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + }; + + class CMaterialMagma : public IMaterial { + public: + static int32_t s_refCount; + static CGxShader* s_vertexShader; + static CGxShader* s_pixelShader; + + static CMaterialMagma* Create(); + + virtual ~CMaterialMagma(); + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + + CMaterialMagma(); + }; + + class CMaterialMagmaFFP : public IMaterial { + public: + static CMaterialMagmaFFP* Create(); + + virtual void Release(); + virtual void Render(IEnvironment* environment, IGeomFactory* geomFactory, IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, CMaterialSettings* settings); + }; + + class IVertexData { + public: + /* 0004 */ uint32_t unk_0004; + + virtual ~IVertexData(); + virtual float GetHeight(uint32_t index) = 0; + virtual uint8_t GetDepth(uint32_t index) = 0; + virtual const uint16_t* GetTexCoord(uint32_t index) = 0; + }; + + class CChunkVertexData : public IVertexData { + public: + /* 0008 */ const uint8_t* m_data; + /* 000C */ uint32_t m_format; + + virtual float GetHeight(uint32_t index); + virtual uint8_t GetDepth(uint32_t index); + virtual const uint16_t* GetTexCoord(uint32_t index); + + CChunkVertexData(const uint8_t* data, uint32_t format); + }; + + class CAreaData; + + class CInstanceVertexData : public IVertexData { + public: + /* 0008 */ const uint8_t* m_data; + /* 000C */ const SMLiquidInstance* m_instance; + /* 0010 */ uint32_t m_vertexCount; + + virtual float GetHeight(uint32_t index); + virtual uint8_t GetDepth(uint32_t index); + virtual const uint16_t* GetTexCoord(uint32_t index); + + CInstanceVertexData(const CAreaData* areaData, const SMLiquidInstance* instance); + }; + + class CAreaData { + public: + static uint8_t s_emptyTable[8]; + static uint8_t s_fullTable[8]; + static uint16_t s_defaultTexCoord[2]; + + /* 0000 */ uint32_t unk_0000; + /* 0004 */ const uint8_t* m_data; + + CAreaData(const void* data); + void SetData(const void* data); + bool HasData() const; + const SMLiquidChunkDesc* GetChunkDesc(uint32_t x, uint32_t y) const; + const SMLiquidInstance* GetInstanceDesc(const SMLiquidChunkDesc* chunk, uint32_t index) const; + const uint8_t* GetFishableTable(const SMLiquidChunkDesc* chunk) const; + const uint8_t* GetDeepTable(const SMLiquidChunkDesc* chunk) const; + const uint8_t* GetExistsTable(const SMLiquidInstance* instance) const; + }; + + class CMaterialSettings { + public: + /* 0000 */ char m_textureNames[6][128]; + /* 0300 */ CImVector m_colors[2]; + /* 0308 */ int32_t m_ints[4]; + /* 0318 */ float m_floats[18]; + /* 0360 */ int32_t m_transparent; + /* 0364 */ int32_t m_texturesReady[6]; + /* 037C */ TSGrowableArray m_textures[6]; + /* 03DC */ TSGrowableArray m_pendingTextures[6]; + + CMaterialSettings(); + ~CMaterialSettings(); + void Destroy(); + float GetFloat(uint32_t index) const; + int32_t GetInt(uint32_t index) const; + CGxTex* GetTexture(uint32_t index, uint32_t period); + void InitTexture(); + }; + + class CInstance { + public: + static CInstance* Create(); + + /* 0000 */ IMaterial* m_material; + /* 0004 */ CMaterialSettings* m_settings; + /* 0008 */ IGeomFactory* m_geomFactory; + /* 000C */ IWaveManager* m_waveManager; + /* 0010 */ IEnvironment* m_environment; + /* 0014 */ C44Matrix m_transform; + /* 0054 */ CAaSphere m_bounds; + /* 0064 */ int32_t m_queued; + /* 0068 */ int32_t m_refCount; + + CInstance(); + void AddRef(); + void Purge(); + void Release(); + }; + + class CRenderer { + public: + typedef int32_t (*DrawListSorter)(const void* a, const void* b); + + static DrawListSorter s_drawListSorter[2]; + + static int32_t CompareOpaque(const void* a, const void* b); + static int32_t CompareTransparent(const void* a, const void* b); + + /* 0000 */ TSGrowableArray m_instances[2]; + + ~CRenderer(); + void Draw(CInstance* instance); + void Flush(const C3Vector& cameraPos, uint32_t pass); + }; + + class CSettingsBank { + public: + static TSGrowableArray s_settings; + + static void Destroy(); + static CMaterialSettings* GetSettings(uint32_t liquidType); + static void ResetTextures(); + static bool UpdateRecord(CMaterialSettings* settings, uint32_t liquidType); + }; + + class CMaterialBank { + public: + static TSGrowableArray s_unk_D43B24; + + static void Destroy(); + static IMaterial* GetMaterial(uint32_t liquidType); + }; + + extern CDataAllocator s_unk_D43B00; + extern CDataAllocator s_unk_D43B34; + extern CDataAllocator s_unk_D45048; + extern CDataAllocator s_unk_D4505C; + extern CDataAllocator s_unk_D45070; + extern CDataAllocator s_unk_D45084; + extern CDataAllocator s_unk_D45098; + extern CDataAllocator s_unk_D450AC; + extern CDataAllocator s_unk_D450C0; + extern float s_unk_B23F64; + extern int32_t s_unk_B23F68; + extern IDatabase* s_liquidMaterialDB; + extern IDatabase* s_liquidTypeDB; + extern char s_unk_D439F0[256]; + extern int32_t s_unk_D43AF0; + extern int32_t s_unk_D43AF4; + extern int32_t s_unk_D43AF8; + extern int32_t s_unk_D43AFC; + extern uint8_t s_unk_D43B48[4]; + extern CTexture* s_unk_D43B4C; + extern CTexture* s_unk_D43B50[32]; + extern CTexture* s_wmoWaterTex; + extern CTexture* s_oceanDepthTex; + extern CTexture* s_riverDepthTex; + extern VSConstants s_vsConstants; + extern VSWaveConstants s_vsWaveConstants; + extern PSConstants s_psConstants; + extern PSProcWaterConstants s_psProcWaterConstants; + + void CacheProceduralTextures(); + void ClearMaterials(); + void CreateProceduralTextures(); + void DestroyProceduralTextures(); + void ExtWaterDepthTexCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); + const char* GetShaderSuffix(); + C44Matrix* GetTexScrollMtx(const C2Vector& speed); + int32_t Initialize(int32_t a1, float a2, int32_t a3, const char* a4, IDatabase* liquidTypeDB, IDatabase* liquidMaterialDB); + void InvalidateProceduralTextures(); + void SetCircularWave(uint32_t index, const C2Vector& a2, const C2Vector& a3, float a4, float a5, float a6, float speed); + void SetDirectionalWave(uint32_t index, const C2Vector& direction, float a3, float a4, float a5, float speed); + void SetProcWaterLighting(const CM2Lighting& lighting); + void SetProcWaterParams(float a1, float a2, float a3, float a4, float a5, float a6, float a7); + void SetShaderConstants(); + void SetShaderLighting(const CM2Lighting& lighting); + void SetShaderTransforms(const C3Vector& cameraPos, const C44Matrix& transform); + void Shutdown(); + void UpdateProceduralTextures(); + int32_t UseCameraRelativeCoords(); + void WmoWaterTexCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); + float WorldCoordScale(); +} + +#endif diff --git a/WoW/Source/LiquidSystem/LiquidData.cpp b/WoW/Source/LiquidSystem/LiquidData.cpp new file mode 100644 index 0000000..3334dfb --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidData.cpp @@ -0,0 +1,165 @@ +#include "Liquid.h" + +// STATIC: 0x00A59484 +uint8_t Liquid::CAreaData::s_emptyTable[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +// STATIC: 0x00A5948C +uint8_t Liquid::CAreaData::s_fullTable[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + +// STATIC: 0x00A59494 +uint16_t Liquid::CAreaData::s_defaultTexCoord[2] = { 0, 0 }; + +// VERIFY: b3a3de07 auto +// OFFSET: 0x007D4F10 +void Liquid::CAreaData::SetData(const void* data) { + this->m_data = static_cast(data); +} + +// VERIFY: b03625ac auto +// OFFSET: 0x008A3050 +Liquid::CAreaData::CAreaData(const void* data) { + this->m_data = static_cast(data); +} + +// VERIFY: 71cd1ec5 manual +// OFFSET: 0x008A3060 +bool Liquid::CAreaData::HasData() const { + return this->m_data != nullptr; +} + +// VERIFY: 6ee106da auto +// OFFSET: 0x008A3070 +const SMLiquidChunkDesc* Liquid::CAreaData::GetChunkDesc(uint32_t x, uint32_t y) const { + return reinterpret_cast(this->m_data) + (x + 16 * y); +} + +// VERIFY: f4b46567 auto +// OFFSET: 0x008A3090 +const SMLiquidInstance* Liquid::CAreaData::GetInstanceDesc(const SMLiquidChunkDesc* chunk, uint32_t index) const { + return reinterpret_cast(this->m_data + chunk->m_offsetInstances) + index; +} + +// VERIFY: 6e52450a auto +// OFFSET: 0x008A30B0 +const uint8_t* Liquid::CAreaData::GetFishableTable(const SMLiquidChunkDesc* chunk) const { + if (!chunk->m_layerCount) { + return Liquid::CAreaData::s_emptyTable; + } + + if (chunk->m_offsetAttributes) { + return this->m_data + chunk->m_offsetAttributes; + } + + return Liquid::CAreaData::s_fullTable; +} + +// VERIFY: 3dff8387 auto +// OFFSET: 0x008A30E0 +const uint8_t* Liquid::CAreaData::GetDeepTable(const SMLiquidChunkDesc* chunk) const { + if (!chunk->m_layerCount) { + return Liquid::CAreaData::s_emptyTable; + } + + if (chunk->m_offsetAttributes) { + return this->m_data + chunk->m_offsetAttributes + 8; + } + + return Liquid::CAreaData::s_fullTable; +} + +// VERIFY: 6b0d6dd8 manual +// OFFSET: 0x008A3110 +const uint8_t* Liquid::CAreaData::GetExistsTable(const SMLiquidInstance* instance) const { + if (instance->m_offsetExistsBitmap) { + return this->m_data + instance->m_offsetExistsBitmap; + } + + return Liquid::CAreaData::s_fullTable; +} + +// VERIFY: 0ecbc03f manual +// OFFSET: 0x008A3130 +float Liquid::CInstanceVertexData::GetHeight(uint32_t index) { + if (this->m_instance->m_vertexFormat > 1) { + return 0.0f; + } + + return reinterpret_cast(this->m_data + this->m_instance->m_offsetVertexData)[index]; +} + +// VERIFY: 43850342 manual +// OFFSET: 0x008A3160 +uint8_t Liquid::CInstanceVertexData::GetDepth(uint32_t index) { + const SMLiquidInstance* instance = this->m_instance; + + if (instance->m_vertexFormat == 0) { + return (this->m_data + instance->m_offsetVertexData + 4 * this->m_vertexCount)[index]; + } + + if (instance->m_vertexFormat != 2) { + return 0; + } + + if (instance->m_offsetVertexData) { + return (this->m_data + instance->m_offsetVertexData)[index]; + } + + return 0xFF; +} + +// VERIFY: 5fa5f4f8 manual +// OFFSET: 0x008A31B0 +const uint16_t* Liquid::CInstanceVertexData::GetTexCoord(uint32_t index) { + if (this->m_instance->m_vertexFormat == 1) { + return reinterpret_cast(this->m_data + this->m_instance->m_offsetVertexData + 4 * (index + this->m_vertexCount)); + } + + return Liquid::CAreaData::s_defaultTexCoord; +} + +// VERIFY: 7b8fdcd4 auto +// OFFSET: 0x008A31E0 +Liquid::CChunkVertexData::CChunkVertexData(const uint8_t* data, uint32_t format) { + this->m_data = data; + this->m_format = format; +} + +// VERIFY: 9dcee70a manual +// OFFSET: 0x008A3200 +float Liquid::CChunkVertexData::GetHeight(uint32_t index) { + if (this->m_format > 1) { + return 0.0f; + } + + return *reinterpret_cast(this->m_data + 8 * index + 4); +} + +// VERIFY: 0121d633 manual +// OFFSET: 0x008A3220 +uint8_t Liquid::CChunkVertexData::GetDepth(uint32_t index) { + uint32_t format = this->m_format; + + if (format == 0 || format == 2) { + return this->m_data[8 * index]; + } + + return 0; +} + +// VERIFY: 681ed984 manual +// OFFSET: 0x008A3250 +const uint16_t* Liquid::CChunkVertexData::GetTexCoord(uint32_t index) { + if (this->m_format == 1) { + return reinterpret_cast(this->m_data + 8 * index); + } + + return Liquid::CAreaData::s_defaultTexCoord; +} + +// VERIFY: 3d77147d auto +// OFFSET: 0x008A3280 +Liquid::CInstanceVertexData::CInstanceVertexData(const Liquid::CAreaData* areaData, const SMLiquidInstance* instance) { + this->m_data = areaData->m_data; + this->m_instance = instance; + this->m_vertexCount = (instance->m_width + 1) * (instance->m_height + 1); +} diff --git a/WoW/Source/LiquidSystem/LiquidMaterial.cpp b/WoW/Source/LiquidSystem/LiquidMaterial.cpp new file mode 100644 index 0000000..93e0779 --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidMaterial.cpp @@ -0,0 +1,154 @@ +#include "Liquid.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Os/TimeManager.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/Tempest/C33Matrix.h" + +// STATIC: 0x00D44CA8 +Liquid::VSConstants Liquid::s_vsConstants; + +// STATIC: 0x00B24120 +Liquid::PSConstants Liquid::s_psConstants; + +// VERIFY: ea58aa9f manual +// OFFSET: 0x008A32F0 +void Liquid::SetShaderTransforms(const C3Vector& cameraPos, const C44Matrix& transform) { + C44Matrix view; + GxXformView(view); + + C44Matrix world = transform; + + if (Liquid::UseCameraRelativeCoords()) { + world.d0 -= cameraPos.x; + world.d1 -= cameraPos.y; + world.d2 -= cameraPos.z; + } + + C44Matrix modelView = world * view; + Liquid::s_vsConstants.m_modelView = modelView; + + C44Matrix projection; + projection = g_theGxDevicePtr->m_projNative; + + if (g_theGxDevicePtr->DeviceApi() == GxApi_OpenGl) { + projection.c0 *= -1.0f; + projection.c1 *= -1.0f; + projection.c2 *= -1.0f; + projection.c3 *= -1.0f; + } + + Liquid::s_psConstants.m_modelViewProj = modelView * projection; + Liquid::s_psConstants.m_cameraPos.x = cameraPos.x; + Liquid::s_psConstants.m_cameraPos.y = cameraPos.y; + Liquid::s_psConstants.m_cameraPos.z = cameraPos.z; + Liquid::s_psConstants.m_cameraPos.w = 1.0f; +} + +// VERIFY: ab5711b4 manual +// OFFSET: 0x008A34B0 +C44Matrix* Liquid::GetTexScrollMtx(const C2Vector& speed) { + float u = 0.0f; + float v = 0.0f; + uint32_t time = OsGetAsyncTimeMs(); + + if (speed.x != 0.0f) { + uint32_t period = static_cast(1000.0f / speed.x); + u = static_cast(time % period) / static_cast(period); + } + + if (speed.y != 0.0f) { + uint32_t period = static_cast(1000.0f / speed.y); + v = static_cast(time % period) / static_cast(period); + } + + // STATIC: 0x00D450D8 + static C44Matrix s_matrix; + + s_matrix.d0 = u; + s_matrix.d1 = v; + s_matrix.d2 = 0.0f; + + return &s_matrix; +} + +// VERIFY: 0ef940ce manual +// OFFSET: 0x008A38B0 +void Liquid::SetShaderLighting(const CM2Lighting& lighting) { + C44Matrix view; + GxXformView(view); + + C3Vector sunDir = lighting.m_sunDir * C33Matrix(view); + Liquid::s_vsConstants.m_sunDir.x = sunDir.x; + Liquid::s_vsConstants.m_sunDir.y = sunDir.y; + Liquid::s_vsConstants.m_sunDir.z = sunDir.z; + Liquid::s_vsConstants.m_sunDir.w = 1.0f; + + Liquid::s_vsConstants.m_sunAmbient.x = lighting.m_sunAmbient.x; + Liquid::s_vsConstants.m_sunAmbient.y = lighting.m_sunAmbient.y; + Liquid::s_vsConstants.m_sunAmbient.z = lighting.m_sunAmbient.z; + Liquid::s_vsConstants.m_sunAmbient.w = 1.0f; + + Liquid::s_vsConstants.m_sunDiffuse.x = lighting.m_sunDiffuse.x; + Liquid::s_vsConstants.m_sunDiffuse.y = lighting.m_sunDiffuse.y; + Liquid::s_vsConstants.m_sunDiffuse.z = lighting.m_sunDiffuse.z; + Liquid::s_vsConstants.m_sunDiffuse.w = 1.0f; + + Liquid::s_vsConstants.m_sunSpecular.x = lighting.m_sunSpecular.x; + Liquid::s_vsConstants.m_sunSpecular.y = lighting.m_sunSpecular.y; + Liquid::s_vsConstants.m_sunSpecular.z = lighting.m_sunSpecular.z; + Liquid::s_vsConstants.m_sunSpecular.w = 6.0f; + + uint32_t lightCount = lighting.m_lightCount; + + for (uint32_t i = 0; i < lightCount; i++) { + if (i >= sizeof(Liquid::s_vsConstants.m_lights) / sizeof(Liquid::s_vsConstants.m_lights[0])) { + break; + } + + const CM2Light* light = lighting.m_lights[i]; + C3Vector position = light->m_pos * C33Matrix(view); + Liquid::VSLight& constants = Liquid::s_vsConstants.m_lights[i]; + + constants.m_position.x = position.x; + constants.m_position.y = position.y; + constants.m_position.z = position.z; + constants.m_position.w = 1.0f; + + constants.m_color.x = light->m_dirColor.x * (1.0f / 255.0f); + constants.m_color.y = light->m_dirColor.y * (1.0f / 255.0f); + constants.m_color.z = light->m_dirColor.z * (1.0f / 255.0f); + constants.m_color.w = 1.0f; + + constants.m_attenuation.x = light->m_constantAttenuation; + constants.m_attenuation.y = light->m_linearAttenuation; + constants.m_attenuation.z = light->m_quadraticAttenuation; + } + + if (g_theGxDevicePtr->MasterEnable(GxMasterEnable_Fog)) { + float fogEnd = lighting.m_fogEnd; + float fogRange = 1.0f / (fogEnd - lighting.m_fogStart); + Liquid::s_vsConstants.m_fog.x = -(fogRange * CShaderEffect::s_fogMul); + Liquid::s_vsConstants.m_fog.y = fogEnd * fogRange; + Liquid::s_vsConstants.m_fog.z = lighting.m_fogDensity; + Liquid::s_vsConstants.m_fog.w = 0.0f; + } else { + Liquid::s_vsConstants.m_fog.x = 0.0f; + Liquid::s_vsConstants.m_fog.y = 1.0f; + Liquid::s_vsConstants.m_fog.z = 1.0f; + Liquid::s_vsConstants.m_fog.w = 0.0f; + } + + Liquid::s_psConstants.m_fogColor.x = lighting.m_fogColor.x; + Liquid::s_psConstants.m_fogColor.y = lighting.m_fogColor.y; + Liquid::s_psConstants.m_fogColor.z = lighting.m_fogColor.z; + Liquid::s_psConstants.m_fogColor.w = 1.0f; +} + +// VERIFY: e12a4641 auto +// OFFSET: 0x008A3DA0 +void Liquid::SetShaderConstants() { + g_theGxDevicePtr->ShaderConstantsSet(GxSh_Vertex, 0, reinterpret_cast(&Liquid::s_vsConstants), 46); + g_theGxDevicePtr->ShaderConstantsSet(GxSh_Pixel, 0, reinterpret_cast(&Liquid::s_psConstants), 6); +} diff --git a/WoW/Source/LiquidSystem/LiquidMaterialBank.cpp b/WoW/Source/LiquidSystem/LiquidMaterialBank.cpp new file mode 100644 index 0000000..3000592 --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidMaterialBank.cpp @@ -0,0 +1,99 @@ +#include "Liquid.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Services/SysMessage.h" +#include "WoW/Source/DB/WowClientDB.h" +#include "WoW/Source/DB/Rec/LiquidTypeRec.h" + +// STATIC: 0x00D43B24 +TSGrowableArray Liquid::CMaterialBank::s_unk_D43B24; + +// VERIFY: 44d8beb7 manual +// OFFSET: 0x008A1F50 +void Liquid::CMaterialBank::Destroy() { + for (uint32_t i = 0; i < Liquid::CMaterialBank::s_unk_D43B24.Count(); i++) { + if (Liquid::CMaterialBank::s_unk_D43B24[i]) { + Liquid::CMaterialBank::s_unk_D43B24[i]->Release(); + } + } + + Liquid::CMaterialBank::s_unk_D43B24.SetCount(0); +} + +// VERIFY: 502f2f3f auto +// OFFSET: 0x008A4280 +void Liquid::ClearMaterials() { + Liquid::s_unk_D45048.Clear(".?AVCMaterialProcWater@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D4505C.Clear(".?AVCMaterialProcWaterFFP@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D45070.Clear(".?AVCMaterialWater@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D45084.Clear(".?AVCMaterialWaterNoSpec@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D45098.Clear(".?AVCMaterialWaterFFP@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D450AC.Clear(".?AVCMaterialMagma@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D450C0.Clear(".?AVCMaterialMagmaFFP@Liquid@@", SLOG_OBJECT, 1); +} + +// VERIFY: e742ff22 auto +// OFFSET: 0x008A1FA0 +Liquid::IMaterial* Liquid::CMaterialBank::GetMaterial(uint32_t liquidType) { + LiquidTypeRec* typeRec; + + while (true) { + if (!Liquid::s_unk_D43AFC) { + return nullptr; + } + + typeRec = Liquid::s_liquidTypeDB->GetRecord(liquidType); + + if (typeRec) { + break; + } + + SysMsgPrintf(STATUS_ERROR, 2, "Material Bank: Liquid type [%d] not found, defaulting to water!", liquidType); + liquidType = 1; + } + + uint32_t materialId = typeRec->m_materialID; + + if (materialId < Liquid::CMaterialBank::s_unk_D43B24.Count() && Liquid::CMaterialBank::s_unk_D43B24.m_data[materialId]) { + return Liquid::CMaterialBank::s_unk_D43B24.m_data[materialId]; + } + + bool vertexShaders = g_theGxDevicePtr->Caps().m_shaderTargets[GxSh_Vertex] >= 1; + bool shaders = Liquid::s_unk_D43AF0 && vertexShaders && g_theGxDevicePtr->Caps().m_shaderTargets[GxSh_Pixel] >= 3; + + Liquid::IMaterial* material = nullptr; + + switch (materialId) { + case 1: + if (shaders) { + if (Liquid::s_unk_B23F68) { + material = Liquid::CMaterialWater::Create(); + } else { + material = Liquid::CMaterialWaterNoSpec::Create(); + } + } else { + material = Liquid::CMaterialWaterFFP::Create(); + } + break; + + case 2: + if (shaders) { + material = Liquid::CMaterialMagma::Create(); + } else { + material = Liquid::CMaterialMagmaFFP::Create(); + } + break; + + case 3: + if (shaders) { + material = Liquid::CMaterialProcWater::Create(); + } else { + material = Liquid::CMaterialProcWaterFFP::Create(); + } + break; + } + + Liquid::CMaterialBank::s_unk_D43B24.SetCount(materialId + 1); + Liquid::CMaterialBank::s_unk_D43B24.m_data[materialId] = material; + + return material; +} diff --git a/WoW/Source/LiquidSystem/LiquidMaterialSettings.cpp b/WoW/Source/LiquidSystem/LiquidMaterialSettings.cpp new file mode 100644 index 0000000..9ba065c --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidMaterialSettings.cpp @@ -0,0 +1,188 @@ +#include "Liquid.h" +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Base/Status.h" +#include "Engine/Source/Gx/CGxTexFlags.h" +#include "Engine/Source/Os/TimeManager.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/CMath.h" + +// VERIFY: c119ee84 auto +// OFFSET: 0x008A16C0 +int32_t Liquid::CMaterialSettings::GetInt(uint32_t index) const { + return this->m_ints[index]; +} + +// VERIFY: 7e415766 auto +// OFFSET: 0x008A16E0 +float Liquid::CMaterialSettings::GetFloat(uint32_t index) const { + return this->m_floats[index]; +} + +// VERIFY: 64d572f1 manual +// OFFSET: 0x008A1C90 +Liquid::CMaterialSettings::~CMaterialSettings() { +} + +// VERIFY: 0f1ff559 manual +// OFFSET: 0x008A1D00 +void Liquid::CMaterialSettings::Destroy() { + for (uint32_t i = 0; i < 6; i++) { + for (uint32_t j = 0; j < this->m_textures[i].Count(); j++) { + HandleClose(reinterpret_cast(this->m_textures[i].m_data[j])); + } + + this->m_textures[i].m_count = 0; + + for (uint32_t j = 0; j < this->m_pendingTextures[i].Count(); j++) { + HandleClose(reinterpret_cast(this->m_pendingTextures[i].m_data[j])); + } + + this->m_pendingTextures[i].m_count = 0; + } +} + +// VERIFY: d9c082b2 manual +// OFFSET: 0x008A1D60 +CGxTex* Liquid::CMaterialSettings::GetTexture(uint32_t index, uint32_t period) { + TSGrowableArray* textures = &this->m_textures[index]; + uint32_t count = textures->Count(); + + if (!count) { + return nullptr; + } + + if (count == 1) { + return TextureGetGxTex(textures->m_data[0], 0, nullptr); + } + + if (!this->m_texturesReady[index]) { + TSGrowableArray* pending = &this->m_pendingTextures[index]; + + if (pending->Count()) { + bool ready = true; + + for (uint32_t i = 0; i < count && ready; i++) { + CTexture* texture = textures->m_data[i]; + + if (TextureIsBlob(texture)) { + TextureIncreasePriority(texture); + ready = false; + } + } + + if (ready) { + this->m_texturesReady[index] = 1; + + for (uint32_t i = 0; i < pending->Count(); i++) { + HandleClose(reinterpret_cast(pending->m_data[i])); + } + + pending->m_count = 0; + } else { + textures = pending; + } + } else { + bool loaded = true; + + for (uint32_t i = 0; i < count && loaded; i++) { + if (!TextureGetGxTex(textures->m_data[i], 0, nullptr)) { + loaded = false; + } + } + + if (!loaded) { + return nullptr; + } + + this->m_texturesReady[index] = 1; + } + } + + if (!period) { + period = 1; + } + + float frame = static_cast(OsGetAsyncTimeMs() % period) / static_cast(period) * static_cast(count); + + return TextureGetGxTex(textures->m_data[CMath::fint_(frame - 0.5f)], 1, nullptr); +} + +// VERIFY: 7226420f manual +// OFFSET: 0x008A2100 +Liquid::CMaterialSettings::CMaterialSettings() { +} + +// VERIFY: d19ea806 manual +// OFFSET: 0x008A2450 +void Liquid::CMaterialSettings::InitTexture() { + for (uint32_t i = 0; i < 6; i++) { + this->m_texturesReady[i] = 0; + + const char* name = this->m_textureNames[i]; + + if (!name[0]) { + continue; + } + + int32_t procedural = SStrStrI(name, "procedural") != nullptr; + CGxTexFlags flags(procedural ? GxTex_Linear : GxTex_LinearMipLinear, !procedural, !procedural, 0, 0, 0, 1, 0, 0, 0); + bool animated = SStrStrI(name, "%d") != nullptr; + CStatus status; + + if (animated) { + char fileName[128]; + int32_t hasBlob = 0; + + for (uint32_t frame = 1; frame <= 30; frame++) { + SStrPrintf(fileName, sizeof(fileName), name, frame); + CTexture* texture = TextureCreate(fileName, flags, &status, 0); + this->m_textures[i].New(texture); + + if (TextureIsBlob(texture)) { + hasBlob = 1; + } + } + + if (hasBlob) { + for (uint32_t frame = 1; frame <= 30; frame++) { + SStrPrintf(fileName, sizeof(fileName), name, frame); + CTexture* texture = TextureCreateBlob(fileName, flags, &status, 1); + + if (texture) { + this->m_pendingTextures[i].New(texture); + } + } + + if (this->m_pendingTextures[i].Count() != this->m_textures[i].Count()) { + for (uint32_t j = 0; j < this->m_pendingTextures[i].Count(); j++) { + HandleClose(reinterpret_cast(this->m_pendingTextures[i].m_data[j])); + } + + this->m_pendingTextures[i].m_count = 0; + } + } + } else { + CTexture* texture; + + if (procedural) { + texture = TextureCacheGetTexture(name); + + if (!texture) { + CImVector color; + color.b = 0x00; + color.g = 0xFF; + color.r = 0x00; + color.a = 0xFF; + texture = TextureCreateSolid(color); + } + } else { + texture = TextureCreate(name, flags, &status, 0); + } + + if (texture) { + this->m_textures[i].New(texture); + } + } + } +} diff --git a/WoW/Source/LiquidSystem/LiquidProcTextures.cpp b/WoW/Source/LiquidSystem/LiquidProcTextures.cpp new file mode 100644 index 0000000..201c283 --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidProcTextures.cpp @@ -0,0 +1,284 @@ +#include "Liquid.h" +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Gx/CGxTexFlags.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/Tempest/CiRect.h" +#include "Engine/Source/Tempest/CMath.h" +#include "Engine/Source/Tempest/NTempest.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" + +// STATIC: 0x00D43B48 +uint8_t Liquid::s_unk_D43B48[4]; + +// STATIC: 0x00D43B4C +CTexture* Liquid::s_unk_D43B4C; + +// STATIC: 0x00D43B50 +CTexture* Liquid::s_unk_D43B50[32]; + +// STATIC: 0x00D43BD0 +CTexture* Liquid::s_wmoWaterTex; + +// STATIC: 0x00D43BD4 +CTexture* Liquid::s_oceanDepthTex; + +// STATIC: 0x00D43BD8 +CTexture* Liquid::s_riverDepthTex; + +// VERIFY: 901024cc manual +// OFFSET: 0x008A2980 +void Liquid::DestroyProceduralTextures() { + if (Liquid::s_unk_D43B4C) { + HandleClose(reinterpret_cast(Liquid::s_unk_D43B4C)); + Liquid::s_unk_D43B4C = nullptr; + } + + if (Liquid::s_riverDepthTex) { + HandleClose(reinterpret_cast(Liquid::s_riverDepthTex)); + Liquid::s_riverDepthTex = nullptr; + } + + if (Liquid::s_oceanDepthTex) { + HandleClose(reinterpret_cast(Liquid::s_oceanDepthTex)); + Liquid::s_oceanDepthTex = nullptr; + } + + if (Liquid::s_wmoWaterTex) { + HandleClose(reinterpret_cast(Liquid::s_wmoWaterTex)); + Liquid::s_wmoWaterTex = nullptr; + } + + for (uint32_t i = 0; i < sizeof(Liquid::s_unk_D43B50) / sizeof(Liquid::s_unk_D43B50[0]); i++) { + if (Liquid::s_unk_D43B50[i]) { + HandleClose(reinterpret_cast(Liquid::s_unk_D43B50[i])); + Liquid::s_unk_D43B50[i] = nullptr; + } + } +} + +// VERIFY: bbba7190 auto +// OFFSET: 0x008A2A10 +void Liquid::CacheProceduralTextures() { + CGxTexFlags flags; + + TextureCacheNewTexture(Liquid::s_riverDepthTex); + TextureCacheNewTexture(Liquid::s_oceanDepthTex); + TextureCacheNewTexture(Liquid::s_wmoWaterTex); + + for (uint32_t i = 0; i < sizeof(Liquid::s_unk_D43B50) / sizeof(Liquid::s_unk_D43B50[0]); i++) { + if (Liquid::s_unk_D43B50[i]) { + TextureCacheNewTexture(Liquid::s_unk_D43B50[i]); + } + } + + if (Liquid::s_unk_D43B4C) { + TextureCacheNewTexture(Liquid::s_unk_D43B4C); + } + + Liquid::CSettingsBank::ResetTextures(); +} + +// VERIFY: 5d576297 auto +// OFFSET: 0x008A2E20 +void Liquid::CreateProceduralTextures() { + Liquid::s_unk_D43B48[3] = 0; + Liquid::s_unk_D43B48[2] = 0; + Liquid::s_unk_D43B48[1] = 0; + Liquid::s_unk_D43B48[0] = 0; + + CGxTexFlags flags; + Liquid::s_riverDepthTex = TextureCreate(8, 64, GxTex_Argb8888, GxTex_Argb8888, flags, reinterpret_cast(1), Liquid::ExtWaterDepthTexCallback, "proceduralRiverDepthTex", 0); + + flags = CGxTexFlags(); + Liquid::s_oceanDepthTex = TextureCreate(8, 64, GxTex_Argb8888, GxTex_Argb8888, flags, nullptr, Liquid::ExtWaterDepthTexCallback, "proceduralOceanDepthTex", 0); + + flags = CGxTexFlags(); + Liquid::s_wmoWaterTex = TextureCreate(8, 64, GxTex_Argb8888, GxTex_Argb8888, flags, nullptr, Liquid::WmoWaterTexCallback, "proceduralWmoWaterTex", 0); + + Liquid::CacheProceduralTextures(); +} + +// VERIFY: 7e3d28c3 manual +// OFFSET: 0x008A2F00 +void Liquid::UpdateProceduralTextures() { + if (!Liquid::s_unk_D43B48[3] && Liquid::s_oceanDepthTex) { + CGxTex* gxTex = TextureGetGxTex(Liquid::s_oceanDepthTex, 0, nullptr); + + if (gxTex) { + CiRect rect; + rect.minY = 0; + rect.minX = 0; + rect.maxY = 64; + rect.maxX = 8; + GxTexUpdate(gxTex, rect, 0); + Liquid::s_unk_D43B48[3] = 1; + } + } + + if (!Liquid::s_unk_D43B48[2] && Liquid::s_riverDepthTex) { + CGxTex* gxTex = TextureGetGxTex(Liquid::s_riverDepthTex, 0, nullptr); + + if (gxTex) { + CiRect rect; + rect.minY = 0; + rect.minX = 0; + rect.maxY = 64; + rect.maxX = 8; + GxTexUpdate(gxTex, rect, 0); + Liquid::s_unk_D43B48[2] = 1; + } + } + + if (!Liquid::s_unk_D43B48[1] && Liquid::s_wmoWaterTex) { + CGxTex* gxTex = TextureGetGxTex(Liquid::s_wmoWaterTex, 0, nullptr); + + if (gxTex) { + CiRect rect; + rect.minY = 0; + rect.minX = 0; + rect.maxY = 64; + rect.maxX = 8; + GxTexUpdate(gxTex, rect, 0); + Liquid::s_unk_D43B48[1] = 1; + } + } + + if (!Liquid::s_unk_D43B48[0]) { + for (uint32_t i = 0; i < sizeof(Liquid::s_unk_D43B50) / sizeof(Liquid::s_unk_D43B50[0]); i++) { + if (Liquid::s_unk_D43B50[i]) { + GxTexUpdate(TextureGetGxTex(Liquid::s_unk_D43B50[i], 0, nullptr), 0, 0, 0, 0, 1); + } + } + + Liquid::s_unk_D43B48[0] = 1; + } + + if (Liquid::s_unk_D43B4C) { + CGxTex* gxTex = TextureGetGxTex(Liquid::s_unk_D43B4C, 1, nullptr); + + if (gxTex) { + GxTexStretch(gxTex, nullptr, nullptr, 0, 0); + } + } +} + +// VERIFY: ff857bf4 auto +// OFFSET: 0x008A2AA0 +void Liquid::InvalidateProceduralTextures() { + Liquid::s_unk_D43B48[3] = 0; + Liquid::s_unk_D43B48[2] = 0; + Liquid::s_unk_D43B48[1] = 0; +} + +// VERIFY: 33364e6a manual +// OFFSET: 0x008A2AC0 +void Liquid::WmoWaterTexCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + // STATIC: 0x00D43BE0 + static CImVector s_texels[64 * 8]; + + if (command != GxTex_Latch) { + return; + } + + DayNight::DNInfo* info = DayNight::GetInfo(); + CImVector band = info->m_bands.m_band17; + uint8_t shallowAlpha = CMath::fint_(info->m_bands.m_waterShallowAlpha * 255.0f); + uint8_t deepAlpha = CMath::fint_(info->m_bands.m_waterDeepAlpha * 255.0f); + int32_t alphaStep = ((deepAlpha - shallowAlpha) << 8) >> 6; + int32_t alpha = shallowAlpha << 8; + + CImVector color; + color.b = band.b; + color.g = band.g; + color.r = band.r; + + CImVector white; + white.b = 0xFF; + white.g = 0xFF; + white.r = 0xFF; + + for (uint32_t y = 0; y < 64; y++) { + for (uint32_t x = 0; x < 4; x++) { + color.a = alpha >> 8; + s_texels[y * 8 + x] = color; + } + + for (uint32_t x = 4; x < 8; x++) { + white.a = alpha >> 8; + s_texels[y * 8 + x] = white; + } + + alpha += alphaStep; + } + + texelStrideInBytes = 8 * sizeof(CImVector); + texels = s_texels; +} + +// VERIFY: e0acfc63 manual +// OFFSET: 0x008A2BF0 +void Liquid::ExtWaterDepthTexCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + // STATIC: 0x00D443E8 + static CImVector s_texels[64 * 8]; + + if (command != GxTex_Latch) { + return; + } + + DayNight::DNInfo* info = DayNight::GetInfo(); + + uint8_t shallowAlpha[2]; + shallowAlpha[0] = CMath::fint_(info->m_bands.m_oceanShallowAlpha * 255.0f); + shallowAlpha[1] = CMath::fint_(info->m_bands.m_waterShallowAlpha * 255.0f); + + uint8_t deepAlpha[2]; + deepAlpha[0] = CMath::fint_(info->m_bands.m_oceanDeepAlpha * 255.0f); + deepAlpha[1] = CMath::fint_(info->m_bands.m_waterDeepAlpha * 255.0f); + + uintptr_t type = reinterpret_cast(userArg); + CImVector shallowColor = (&info->m_bands.m_band14)[type * 2]; + CImVector deepColor = (&info->m_bands.m_band15)[type * 2]; + + int32_t alphaStep = ((deepAlpha[type] - shallowAlpha[type]) << 8) >> 6; + int32_t redStep = ((deepColor.r - shallowColor.r) << 8) >> 6; + int32_t blueStep = ((deepColor.b - shallowColor.b) << 8) >> 6; + int32_t greenStep = ((deepColor.g - shallowColor.g) << 8) >> 6; + + int32_t alpha = shallowAlpha[type] << 8; + int32_t red = shallowColor.r << 8; + int32_t green = shallowColor.g << 8; + int32_t blue = shallowColor.b << 8; + + CImVector* row = s_texels; + + for (uint32_t y = 0; y < height; y++) { + CImVector color; + color.b = blue >> 8; + color.g = green >> 8; + color.r = red >> 8; + color.a = alpha >> 8; + + if (y == height - 1 && type == 0) { + C3Vector rgb(color); + C3Vector hsv; + RGBtoHSV(rgb, hsv); + hsv.z *= 0.9f; + HSVtoRGB(hsv, rgb); + color = rgb; + } + + for (uint32_t x = 0; x < width; x++) { + row[x] = color; + } + + green += greenStep; + blue += blueStep; + alpha += alphaStep; + red += redStep; + row += width; + } + + texelStrideInBytes = width * sizeof(CImVector); + texels = s_texels; +} diff --git a/WoW/Source/LiquidSystem/LiquidRenderer.cpp b/WoW/Source/LiquidSystem/LiquidRenderer.cpp new file mode 100644 index 0000000..4ef5ed3 --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidRenderer.cpp @@ -0,0 +1,137 @@ +#include "Liquid.h" +#include +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/CGxLight.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00B23F6C +Liquid::CRenderer::DrawListSorter Liquid::CRenderer::s_drawListSorter[2] = { + Liquid::CRenderer::CompareOpaque, + Liquid::CRenderer::CompareTransparent +}; + +// VERIFY: 3d910c8b manual +// OFFSET: 0x00791910 +Liquid::CRenderer::~CRenderer() { +} + +// VERIFY: c8176254 manual +// OFFSET: 0x008A1980 +int32_t Liquid::CRenderer::CompareOpaque(const void* a, const void* b) { + const Liquid::CInstance* l = static_cast(a); + const Liquid::CInstance* r = static_cast(b); + + if (l->m_material < r->m_material) { + return -1; + } + + if (l->m_material > r->m_material) { + return 1; + } + + if (l->m_settings < r->m_settings) { + return -1; + } + + if (l->m_settings > r->m_settings) { + return 1; + } + + if (l->m_geomFactory < r->m_geomFactory) { + return -1; + } + + if (l->m_geomFactory > r->m_geomFactory) { + return 1; + } + + if (l->m_transform.d3 > r->m_transform.d3) { + return 1; + } + + if (l->m_transform.d3 < r->m_transform.d3) { + return -1; + } + + return 0; +} + +// VERIFY: abfe3e46 manual +// OFFSET: 0x008A19E0 +int32_t Liquid::CRenderer::CompareTransparent(const void* a, const void* b) { + const Liquid::CInstance* l = static_cast(a); + const Liquid::CInstance* r = static_cast(b); + + if (l->m_material < r->m_material) { + return -1; + } + + if (l->m_material > r->m_material) { + return 1; + } + + if (l->m_settings < r->m_settings) { + return -1; + } + + if (l->m_settings > r->m_settings) { + return 1; + } + + if (l->m_geomFactory < r->m_geomFactory) { + return -1; + } + + if (l->m_geomFactory > r->m_geomFactory) { + return 1; + } + + if (l->m_transform.d3 > r->m_transform.d3) { + return -1; + } + + if (l->m_transform.d3 < r->m_transform.d3) { + return 1; + } + + return 0; +} + +// VERIFY: f6fe74d2 manual +// OFFSET: 0x008A20C0 +void Liquid::CRenderer::Draw(Liquid::CInstance* instance) { + if (!instance->m_queued) { + this->m_instances[instance->m_settings->m_transparent != 0].New(instance); + instance->m_queued = 1; + } +} + +// VERIFY: d17a0633 manual +// OFFSET: 0x008A2240 +void Liquid::CRenderer::Flush(const C3Vector& cameraPos, uint32_t pass) { + CGxLight lights[4]; + + for (uint32_t i = 0; i < 4; i++) { + g_theGxDevicePtr->Light(i, lights[i]); + } + + TSGrowableArray& instances = this->m_instances[pass]; + qsort(instances.m_data, instances.Count(), sizeof(Liquid::CInstance*), Liquid::CRenderer::s_drawListSorter[pass]); + + uint32_t count = instances.Count(); + + for (uint32_t i = 0; i < count; i++) { + Liquid::CInstance* instance = instances.m_data[i]; + instance->m_material->Render(instance->m_environment, instance->m_geomFactory, instance->m_waveManager, cameraPos, &instance->m_transform, &instance->m_bounds, instance->m_settings); + instance->m_queued = 0; + } + + instances.Clear(); + + for (uint32_t i = 0; i < 4; i++) { + if (lights[i].m_flags & 0x1) { + g_theGxDevicePtr->LightSet(i, lights[i], C3Vector(0.0f, 0.0f, 0.0f)); + g_theGxDevicePtr->LightEnable(i, 1); + } + } +} diff --git a/WoW/Source/LiquidSystem/LiquidSettingsBank.cpp b/WoW/Source/LiquidSystem/LiquidSettingsBank.cpp new file mode 100644 index 0000000..c3d231a --- /dev/null +++ b/WoW/Source/LiquidSystem/LiquidSettingsBank.cpp @@ -0,0 +1,111 @@ +#include "Liquid.h" +#include "Engine/Source/Services/SysMessage.h" +#include "WoW/Source/DB/WowClientDB.h" +#include "WoW/Source/DB/Rec/LiquidTypeRec.h" +#include "WoW/Source/DB/Rec/LiquidMaterialRec.h" + +// STATIC: 0x00D43B00 +CDataAllocator Liquid::s_unk_D43B00(0x43C, 0x20); + +// STATIC: 0x00D43B14 +TSGrowableArray Liquid::CSettingsBank::s_settings; + +// VERIFY: f4d601a2 manual +// OFFSET: 0x008A2380 +void Liquid::CSettingsBank::Destroy() { + if (!--Liquid::s_unk_D43AF8) { + for (uint32_t i = 0; i < Liquid::CSettingsBank::s_settings.Count(); i++) { + if (Liquid::CSettingsBank::s_settings[i]) { + Liquid::CSettingsBank::s_settings[i]->Destroy(); + + Liquid::CMaterialSettings* settings = Liquid::CSettingsBank::s_settings[i]; + settings->~CMaterialSettings(); + Liquid::s_unk_D43B00.PutData(settings, nullptr, 0); + } + } + + Liquid::CSettingsBank::s_settings.SetCount(0); + } + + Liquid::s_unk_D43B00.Clear(".?AVCMaterialSettings@Liquid@@", SLOG_OBJECT, 1); +} + +// VERIFY: 2a7eaf1c manual +// OFFSET: 0x008A2780 +void Liquid::CSettingsBank::ResetTextures() { + for (uint32_t i = 0; i < Liquid::CSettingsBank::s_settings.Count(); i++) { + if (Liquid::CSettingsBank::s_settings[i]) { + Liquid::CSettingsBank::s_settings[i]->Destroy(); + Liquid::CSettingsBank::s_settings[i]->InitTexture(); + } + } +} + +// VERIFY: b281ff61 manual +// OFFSET: 0x008A27C0 +bool Liquid::CSettingsBank::UpdateRecord(Liquid::CMaterialSettings* settings, uint32_t liquidType) { + settings->Destroy(); + + LiquidTypeRec* typeRec = Liquid::s_liquidTypeDB->GetRecord(liquidType); + + if (!typeRec) { + return false; + } + + LiquidMaterialRec* materialRec = Liquid::s_liquidMaterialDB->GetRecord(typeRec->m_materialID); + + if (!materialRec) { + return false; + } + + for (uint32_t i = 0; i < 6; i++) { + const char* src = typeRec->m_texture[i]; + char* dst = settings->m_textureNames[i]; + + while ((*dst++ = *src++) != 0) { + } + } + + settings->m_colors[0] = *reinterpret_cast(&typeRec->m_color[0]); + settings->m_colors[1] = *reinterpret_cast(&typeRec->m_color[1]); + settings->m_ints[0] = typeRec->m_int[0]; + settings->m_ints[1] = typeRec->m_int[1]; + settings->m_ints[2] = typeRec->m_int[2]; + settings->m_ints[3] = typeRec->m_int[3]; + + for (uint32_t i = 0; i < 18; i++) { + settings->m_floats[i] = typeRec->m_float[i]; + } + + settings->m_transparent = materialRec->m_flags & 0x1; + settings->InitTexture(); + + return true; +} + +// VERIFY: ff01b3c6 auto +// OFFSET: 0x008A28F0 +Liquid::CMaterialSettings* Liquid::CSettingsBank::GetSettings(uint32_t liquidType) { + Liquid::CMaterialSettings* settings; + + while (true) { + if (liquidType < Liquid::CSettingsBank::s_settings.Count() && Liquid::CSettingsBank::s_settings.m_data[liquidType]) { + return Liquid::CSettingsBank::s_settings.m_data[liquidType]; + } + + void* mem = Liquid::s_unk_D43B00.GetData(0, ".?AVCMaterialSettings@Liquid@@", SLOG_OBJECT); + settings = mem ? new (mem) Liquid::CMaterialSettings() : nullptr; + + if (Liquid::CSettingsBank::UpdateRecord(settings, liquidType)) { + break; + } + + SysMsgPrintf(STATUS_ERROR, 2, "Settings Bank: Liquid type [%d] not found, defaulting to water!", liquidType); + liquidType = 1; + } + + Liquid::CSettingsBank::s_settings.SetCount(liquidType + 1); + Liquid::CSettingsBank::s_settings.m_data[liquidType] = settings; + + return settings; +} diff --git a/WoW/Source/LiquidSystem/MaterialMagma.cpp b/WoW/Source/LiquidSystem/MaterialMagma.cpp new file mode 100644 index 0000000..f770530 --- /dev/null +++ b/WoW/Source/LiquidSystem/MaterialMagma.cpp @@ -0,0 +1,183 @@ +#include "Liquid.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00D450C0 +CDataAllocator Liquid::s_unk_D450C0(4, 8); + +// STATIC: 0x00D450AC +CDataAllocator Liquid::s_unk_D450AC(4, 8); + +// STATIC: 0x00D44C40 +int32_t Liquid::CMaterialMagma::s_refCount; + +// STATIC: 0x00D44BF4 +CGxShader* Liquid::CMaterialMagma::s_vertexShader; + +// STATIC: 0x00D44BF0 +CGxShader* Liquid::CMaterialMagma::s_pixelShader; + +// VERIFY: 83f1c351 auto +// OFFSET: 0x008A4170 +void Liquid::CMaterialMagma::Release() { + this->~CMaterialMagma(); + Liquid::s_unk_D450AC.PutData(this, nullptr, 0); +} + +// VERIFY: a8322b3f auto +// OFFSET: 0x008A4190 +Liquid::CMaterialMagma::CMaterialMagma() { + if (Liquid::CMaterialMagma::s_refCount++ == 0) { + g_theGxDevicePtr->ShaderCreate(&Liquid::CMaterialMagma::s_vertexShader, GxSh_Vertex, "Shaders\\Vertex", "vsLiquidMagma", 1); + g_theGxDevicePtr->ShaderCreate(&Liquid::CMaterialMagma::s_pixelShader, GxSh_Pixel, "Shaders\\Pixel", "psLiquidMagma", 1); + } +} + +// VERIFY: 90d48f71 auto +// OFFSET: 0x008A4200 +Liquid::CMaterialMagma::~CMaterialMagma() { + if (Liquid::CMaterialMagma::s_refCount-- == 1) { + if (Liquid::CMaterialMagma::s_vertexShader) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialMagma::s_vertexShader); + } + + if (Liquid::CMaterialMagma::s_pixelShader) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialMagma::s_pixelShader); + } + } +} + +// VERIFY: 3c4b9f03 auto +// OFFSET: 0x008A4260 +void Liquid::CMaterialMagmaFFP::Release() { + this->~CMaterialMagmaFFP(); + Liquid::s_unk_D450C0.PutData(this, nullptr, 0); +} + +// VERIFY: 49300414 auto +// OFFSET: 0x008A4870 +Liquid::CMaterialMagma* Liquid::CMaterialMagma::Create() { + void* mem = Liquid::s_unk_D450AC.GetData(0, ".?AVCMaterialMagma@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialMagma() : nullptr; +} + +// VERIFY: 7d417cc9 auto +// OFFSET: 0x008A48D0 +Liquid::CMaterialMagmaFFP* Liquid::CMaterialMagmaFFP::Create() { + void* mem = Liquid::s_unk_D450C0.GetData(0, ".?AVCMaterialMagmaFFP@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialMagmaFFP() : nullptr; +} + +// VERIFY: b5c1ce43 manual +// OFFSET: 0x008A6090 +void Liquid::CMaterialMagma::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture0 = settings->GetTexture(0, 1250); + + if (!texture0) { + return; + } + + g_theGxDevicePtr->RsPush(); + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + g_theGxDevicePtr->RsSet(GxRs_Fog, 1); + + Liquid::s_vsConstants.m_projection = g_theGxDevicePtr->m_projNative; + + if (g_theGxDevicePtr->DeviceApi() == GxApi_OpenGl) { + Liquid::s_vsConstants.m_projection.c0 *= -1.0f; + Liquid::s_vsConstants.m_projection.c1 *= -1.0f; + Liquid::s_vsConstants.m_projection.c2 *= -1.0f; + Liquid::s_vsConstants.m_projection.c3 *= -1.0f; + } + + C44Matrix matrix; + Liquid::SetShaderTransforms(cameraPos, geomFactory->SetTransform(const_cast(transform)) ? matrix : *transform); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + Liquid::SetShaderLighting(lighting); + + C2Vector speed; + speed.x = settings->GetFloat(0); + speed.y = settings->GetFloat(1); + C44Matrix texMatrix = *Liquid::GetTexScrollMtx(speed); + texMatrix.Scale(Liquid::WorldCoordScale()); + Liquid::s_vsConstants.m_texTransforms[0] = texMatrix; + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + Liquid::SetShaderConstants(); + g_theGxDevicePtr->RsSet(GxRs_VertexShader, Liquid::CMaterialMagma::s_vertexShader); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, Liquid::CMaterialMagma::s_pixelShader); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + geomFactory->GetBuffers(GxVBF_PCT, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PCT); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + g_theGxDevicePtr->Draw(&batch, 1); + g_theGxDevicePtr->RsPop(); +} + +// VERIFY: 35b378c5 manual +// OFFSET: 0x008A6350 +void Liquid::CMaterialMagmaFFP::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture0 = settings->GetTexture(0, 1250); + + if (!texture0) { + return; + } + + g_theGxDevicePtr->RsPush(); + g_theGxDevicePtr->XformPush(GxXform_World); + g_theGxDevicePtr->XformPush(GxXform_Tex0); + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + + C44Matrix world = *transform; + + if (Liquid::UseCameraRelativeCoords()) { + world.d0 -= cameraPos.x; + world.d1 -= cameraPos.y; + world.d2 -= cameraPos.z; + } + + GxXformSet(GxXform_World, world); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + lighting.SetupGxLights(&cameraPos); + g_theGxDevicePtr->RsSet(GxRs_Lighting, 0); + lighting.SetupGxFog(); + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + + C2Vector speed; + speed.x = settings->GetFloat(0); + speed.y = settings->GetFloat(1); + GxXformSet(GxXform_Tex0, *Liquid::GetTexScrollMtx(speed)); + g_theGxDevicePtr->RsSet(GxRs_TextureShader0, 1); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + geomFactory->GetBuffers(GxVBF_PCT, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PCT); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + g_theGxDevicePtr->Draw(&batch, 1); + + GxXformPop(GxXform_Tex0); + GxXformPop(GxXform_World); + g_theGxDevicePtr->RsPop(); +} diff --git a/WoW/Source/LiquidSystem/MaterialProcWater.cpp b/WoW/Source/LiquidSystem/MaterialProcWater.cpp new file mode 100644 index 0000000..e3c4c7a --- /dev/null +++ b/WoW/Source/LiquidSystem/MaterialProcWater.cpp @@ -0,0 +1,403 @@ +#include "Liquid.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Os/TimeManager.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00D4505C +CDataAllocator Liquid::s_unk_D4505C(4, 8); + +// STATIC: 0x00D45048 +CDataAllocator Liquid::s_unk_D45048(4, 8); + +// STATIC: 0x00D44C34 +int32_t Liquid::CMaterialProcWater::s_refCount; + +// STATIC: 0x00D44C24 +CGxShader* Liquid::CMaterialProcWater::s_vertexShaders[4]; + +// STATIC: 0x00D44C20 +CGxShader* Liquid::CMaterialProcWater::s_pixelShader; + +// STATIC: 0x00D44C48 +Liquid::PSProcWaterConstants Liquid::s_psProcWaterConstants; + +// STATIC: 0x00D44F88 +Liquid::VSWaveConstants Liquid::s_vsWaveConstants; + +// VERIFY: 241af1fe manual +// OFFSET: 0x008A3620 +void Liquid::SetDirectionalWave(uint32_t index, const C2Vector& direction, float a3, float a4, float a5, float speed) { + Liquid::s_vsWaveConstants.m_directionalPhase[index] = static_cast(static_cast(OsGetAsyncTimeMs() * speed) & 0x1FFF) / 1024.0f * 6.2831855f; + + float clampedA3 = a3 < 0.001f ? 0.001f : a3; + C4Vector& wave = Liquid::s_vsWaveConstants.m_directionalWaves[index]; + wave.x = direction.x; + wave.y = direction.y; + wave.z = 1.0f / clampedA3; + wave.w = a5; + + Liquid::s_vsWaveConstants.unk_0050[index] = 1.0f / (a4 < 0.001f ? 0.001f : a4); +} + +// VERIFY: ffbfd269 manual +// OFFSET: 0x008A3710 +void Liquid::SetCircularWave(uint32_t index, const C2Vector& a2, const C2Vector& a3, float a4, float a5, float a6, float speed) { + Liquid::s_vsWaveConstants.m_circularPhase[index] = static_cast(static_cast(OsGetAsyncTimeMs() * speed) & 0x1FFF) / 1024.0f * 6.2831855f; + + float invA4 = 1.0f / (a4 < 0.001f ? 0.001f : a4); + Liquid::s_vsWaveConstants.unk_0060[index] = 1.0f / (a5 < 0.001f ? 0.001f : a5); + + C4Vector& wave = Liquid::s_vsWaveConstants.m_circularWaves[index]; + wave.x = a2.x; + wave.y = a2.y; + wave.z = a3.x; + wave.w = a3.y; + + Liquid::s_vsWaveConstants.unk_00A0[index] = invA4; + Liquid::s_vsWaveConstants.unk_00B0[index] = a6; +} + +// VERIFY: e47f29d4 manual +// OFFSET: 0x008A3810 +void Liquid::SetProcWaterParams(float a1, float a2, float a3, float a4, float a5, float a6, float a7) { + float* params = Liquid::s_psProcWaterConstants.m_params; + params[0] = 1.0f / (a1 < 0.001f ? 0.001f : a1); + params[1] = a2; + params[2] = a3; + params[3] = a4; + params[4] = a7; + params[5] = a5; + params[6] = a6; + params[7] = 0.0f; +} + +// VERIFY: e928fbf9 auto +// OFFSET: 0x008A3C90 +void Liquid::SetProcWaterLighting(const CM2Lighting& lighting) { + Liquid::s_psProcWaterConstants.m_sunDir.x = -lighting.m_sunDir.x; + Liquid::s_psProcWaterConstants.m_sunDir.y = -lighting.m_sunDir.y; + Liquid::s_psProcWaterConstants.m_sunDir.z = -lighting.m_sunDir.z; + Liquid::s_psProcWaterConstants.m_sunDir.w = 0.0f; + + Liquid::s_psProcWaterConstants.m_sunAmbient.x = lighting.m_sunAmbient.x; + Liquid::s_psProcWaterConstants.m_sunAmbient.y = lighting.m_sunAmbient.y; + Liquid::s_psProcWaterConstants.m_sunAmbient.z = lighting.m_sunAmbient.z; + Liquid::s_psProcWaterConstants.m_sunAmbient.w = 0.0f; + + Liquid::s_psProcWaterConstants.m_sunDiffuse.x = lighting.m_sunDiffuse.x; + Liquid::s_psProcWaterConstants.m_sunDiffuse.y = lighting.m_sunDiffuse.y; + Liquid::s_psProcWaterConstants.m_sunDiffuse.z = lighting.m_sunDiffuse.z; + Liquid::s_psProcWaterConstants.m_sunDiffuse.w = 0.0f; + + Liquid::s_psProcWaterConstants.m_sunSpecular.x = lighting.m_sunSpecular.x; + Liquid::s_psProcWaterConstants.m_sunSpecular.y = lighting.m_sunSpecular.y; + Liquid::s_psProcWaterConstants.m_sunSpecular.z = lighting.m_sunSpecular.z; + Liquid::s_psProcWaterConstants.m_sunSpecular.w = 50.0f; +} + +// VERIFY: 616ba53c auto +// OFFSET: 0x008A3DE0 +void Liquid::CMaterialProcWater::Release() { + this->~CMaterialProcWater(); + Liquid::s_unk_D45048.PutData(this, nullptr, 0); +} + +// VERIFY: 624c4ea8 auto +// OFFSET: 0x008A3E00 +Liquid::CMaterialProcWater::CMaterialProcWater() { + if (Liquid::CMaterialProcWater::s_refCount++ == 0) { + char name[256]; + SStrPrintf(name, sizeof(name), "vsLiquidProcWater%s", Liquid::GetShaderSuffix()); + g_theGxDevicePtr->ShaderCreate(Liquid::CMaterialProcWater::s_vertexShaders, GxSh_Vertex, "Shaders\\Vertex", name, 4); + SStrPrintf(name, sizeof(name), "psLiquidProcWater%s", Liquid::GetShaderSuffix()); + g_theGxDevicePtr->ShaderCreate(&Liquid::CMaterialProcWater::s_pixelShader, GxSh_Pixel, "Shaders\\Pixel", name, 1); + } +} + +// VERIFY: 0b2449de manual +// OFFSET: 0x008A3EC0 +Liquid::CMaterialProcWater::~CMaterialProcWater() { + if (Liquid::CMaterialProcWater::s_refCount-- == 1) { + for (uint32_t i = 0; i < 4; i++) { + if (Liquid::CMaterialProcWater::s_vertexShaders[i]) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialProcWater::s_vertexShaders[i]); + } + } + + if (Liquid::CMaterialProcWater::s_pixelShader) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialProcWater::s_pixelShader); + } + } +} + +// VERIFY: 5b0d20f4 auto +// OFFSET: 0x008A3F30 +void Liquid::CMaterialProcWaterFFP::Release() { + this->~CMaterialProcWaterFFP(); + Liquid::s_unk_D4505C.PutData(this, nullptr, 0); +} + +// VERIFY: 46263496 auto +// OFFSET: 0x008A4710 +Liquid::CMaterialProcWater* Liquid::CMaterialProcWater::Create() { + void* mem = Liquid::s_unk_D45048.GetData(0, ".?AVCMaterialProcWater@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialProcWater() : nullptr; +} + +// VERIFY: 3ec533ba auto +// OFFSET: 0x008A4770 +Liquid::CMaterialProcWaterFFP* Liquid::CMaterialProcWaterFFP::Create() { + void* mem = Liquid::s_unk_D4505C.GetData(0, ".?AVCMaterialProcWaterFFP@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialProcWaterFFP() : nullptr; +} + +// VERIFY: be2b8036 manual +// OFFSET: 0x008A48F0 +void Liquid::CMaterialProcWater::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture0 = settings->GetTexture(0, 1250); + + if (!texture0) { + return; + } + + CGxTex* texture1 = settings->GetTexture(1, 1250); + + if (!texture1) { + return; + } + + CGxTex* animTextures[2]; + + for (uint32_t i = 0; i < 2; i++) { + animTextures[i] = settings->GetTexture(i + 2, settings->GetInt(i + 1)); + + if (!animTextures[i]) { + return; + } + } + + CGxTex* texture4 = settings->GetTexture(4, 1250); + + if (!texture4) { + return; + } + + CGxTex* texture5 = settings->GetTexture(5, settings->GetInt(3)); + + if (!texture5) { + return; + } + + float scales[4]; + float angles[4]; + + for (uint32_t i = 0; i < 4; i++) { + scales[i] = settings->GetFloat(i) * Liquid::WorldCoordScale(); + angles[i] = settings->GetFloat(i + 4) * 57.29578f; + } + + float scale4 = settings->GetFloat(8) * Liquid::WorldCoordScale(); + float scale5 = settings->GetFloat(9) * Liquid::WorldCoordScale(); + float angle5 = settings->GetFloat(10) * 57.29578f; + + g_theGxDevicePtr->RsPush(); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + + bool identity = geomFactory->SetTransform(const_cast(transform)); + geomFactory->GetBuffers(GxVBF_PT2, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PT2); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + g_theGxDevicePtr->RsSet(GxRs_DepthWrite, 0); + g_theGxDevicePtr->RsSet(GxRs_BlendingMode, 2); + GxUpdateAlphaRef(GxRs_AlphaRef); + g_theGxDevicePtr->RsSet(GxRs_Fog, 1); + + Liquid::s_vsConstants.m_projection = g_theGxDevicePtr->m_projNative; + + if (g_theGxDevicePtr->DeviceApi() == GxApi_OpenGl) { + Liquid::s_vsConstants.m_projection.c0 *= -1.0f; + Liquid::s_vsConstants.m_projection.c1 *= -1.0f; + Liquid::s_vsConstants.m_projection.c2 *= -1.0f; + Liquid::s_vsConstants.m_projection.c3 *= -1.0f; + } + + C44Matrix matrix; + Liquid::SetShaderTransforms(cameraPos, identity ? matrix : *transform); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + Liquid::SetShaderLighting(lighting); + Liquid::SetProcWaterLighting(lighting); + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + g_theGxDevicePtr->RsSet(GxRs_Texture1, texture1); + g_theGxDevicePtr->RsSet(GxRs_Texture2, texture4); + g_theGxDevicePtr->RsSet(GxRs_Texture3, texture5); + + for (uint32_t i = 0; i < 2; i++) { + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture4 + i), animTextures[i]); + } + + for (uint32_t i = 0; i < 4; i++) { + matrix = C44Matrix::RotationAroundZ(angles[i]); + matrix.Scale(scales[i]); + Liquid::s_vsConstants.m_texTransforms[i] = matrix; + } + + C44Matrix scaleMatrix; + scaleMatrix.b1 = scale4; + Liquid::s_vsConstants.m_texTransforms[4] = scaleMatrix; + + C44Matrix rotationMatrix = C44Matrix::RotationAroundZ(angle5); + rotationMatrix.Scale(scale5); + Liquid::s_vsConstants.m_texTransforms[5] = rotationMatrix; + + const float* waves = nullptr; + const float* wavesEnd; + + if (waveManager) { + waves = static_cast(waveManager->GetData()); + wavesEnd = waves + waveManager->GetCount(); + } else { + wavesEnd = waves; + } + + for (uint32_t i = 0; i < 3; i++) { + if (waves + 6 > wavesEnd) { + C2Vector direction; + Liquid::SetDirectionalWave(i, direction, 0.0f, 0.0f, 0.0f, 0.0f); + } else { + C2Vector direction(waves[0], waves[1]); + Liquid::SetDirectionalWave(i, direction, waves[2], waves[3], waves[4], waves[5]); + } + + waves += 6; + } + + for (uint32_t i = 0; i < 3; i++) { + if (waves + 8 > wavesEnd) { + C2Vector a2; + C2Vector a3; + Liquid::SetCircularWave(i, a2, a3, 0.0f, 0.0f, 0.0f, 0.0f); + } else { + C2Vector a3(waves[2], waves[3]); + C2Vector a2(waves[0], waves[1]); + Liquid::SetCircularWave(i, a2, a3, waves[4], waves[5], waves[6], waves[7]); + } + + waves += 8; + } + + float param0 = settings->GetFloat(11) * Liquid::WorldCoordScale(); + float param1 = settings->GetFloat(12); + float param2 = settings->GetFloat(13); + float param3 = settings->GetFloat(14); + float param4 = settings->GetFloat(15); + float param5 = settings->GetFloat(16); + float param6 = settings->GetFloat(17); + Liquid::SetProcWaterParams(param0, param1, param2, param3, param4, param5, param6); + + Liquid::SetShaderConstants(); + g_theGxDevicePtr->ShaderConstantsSet(GxSh_Vertex, 46, reinterpret_cast(&Liquid::s_vsWaveConstants), 12); + g_theGxDevicePtr->ShaderConstantsSet(GxSh_Pixel, 6, reinterpret_cast(&Liquid::s_psProcWaterConstants), 6); + + uint32_t lightCount = lighting.m_lightCount; + + if (lightCount >= 3) { + lightCount = 3; + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, Liquid::CMaterialProcWater::s_vertexShaders[lightCount]); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, Liquid::CMaterialProcWater::s_pixelShader); + g_theGxDevicePtr->Draw(&batch, 1); + g_theGxDevicePtr->RsPop(); +} + +// VERIFY: 3685e63c manual +// OFFSET: 0x008A5170 +void Liquid::CMaterialProcWaterFFP::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture0 = settings->GetTexture(4, 1250); + + if (!texture0) { + return; + } + + CGxTex* texture1 = settings->GetTexture(5, settings->GetInt(2)); + + if (!texture1) { + return; + } + + float scale0 = settings->GetFloat(8) * Liquid::WorldCoordScale(); + float scale1 = settings->GetFloat(9) * Liquid::WorldCoordScale(); + float angle1 = settings->GetFloat(10) * 57.29578f; + + g_theGxDevicePtr->RsPush(); + g_theGxDevicePtr->XformPush(GxXform_World); + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + g_theGxDevicePtr->RsSet(GxRs_BlendingMode, 2); + GxUpdateAlphaRef(GxRs_AlphaRef); + GxRsSet(GxRs_DepthWrite, g_theGxDevicePtr->Caps().m_maxClipPlanes == 0); + + C44Matrix world = *transform; + + if (Liquid::UseCameraRelativeCoords()) { + world.d0 -= cameraPos.x; + world.d1 -= cameraPos.y; + world.d2 -= cameraPos.z; + } + + GxXformSet(GxXform_World, world); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + lighting.SetupGxLights(&cameraPos); + lighting.SetupGxFog(); + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + g_theGxDevicePtr->RsSet(GxRs_TextureCoord0, 1); + + C44Matrix texMatrix0; + texMatrix0.b1 = scale0; + g_theGxDevicePtr->XformPushAndSet(GxXform_Tex0, texMatrix0); + + g_theGxDevicePtr->RsSet(GxRs_Texture1, texture1); + g_theGxDevicePtr->RsSet(GxRs_TextureCoord1, 0); + g_theGxDevicePtr->RsSet(GxRs_TextureShader1, 1); + g_theGxDevicePtr->RsSet(GxRs_ColorOp1, 2); + g_theGxDevicePtr->RsSet(GxRs_AlphaOp1, 3); + + C44Matrix texMatrix1 = C44Matrix::RotationAroundZ(angle1); + texMatrix1.Scale(scale1); + g_theGxDevicePtr->XformPushAndSet(GxXform_Tex1, texMatrix1); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + geomFactory->GetBuffers(GxVBF_PNCT2, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PNCT2); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + g_theGxDevicePtr->Draw(&batch, 1); + + GxXformPop(GxXform_Tex1); + GxXformPop(GxXform_Tex0); + GxXformPop(GxXform_World); + g_theGxDevicePtr->RsPop(); +} diff --git a/WoW/Source/LiquidSystem/MaterialWater.cpp b/WoW/Source/LiquidSystem/MaterialWater.cpp new file mode 100644 index 0000000..c7e6bb1 --- /dev/null +++ b/WoW/Source/LiquidSystem/MaterialWater.cpp @@ -0,0 +1,359 @@ +#include "Liquid.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Storm/H/storm.h" + +// STATIC: 0x00D45098 +CDataAllocator Liquid::s_unk_D45098(4, 8); + +// STATIC: 0x00D45084 +CDataAllocator Liquid::s_unk_D45084(4, 8); + +// STATIC: 0x00D45070 +CDataAllocator Liquid::s_unk_D45070(4, 8); + +// STATIC: 0x00D44C38 +int32_t Liquid::CMaterialWater::s_refCount; + +// STATIC: 0x00D44C10 +CGxShader* Liquid::CMaterialWater::s_vertexShaders[4]; + +// STATIC: 0x00D44C0C +CGxShader* Liquid::CMaterialWater::s_pixelShader; + +// STATIC: 0x00D44C3C +int32_t Liquid::CMaterialWaterNoSpec::s_refCount; + +// STATIC: 0x00D44BFC +CGxShader* Liquid::CMaterialWaterNoSpec::s_vertexShaders[4]; + +// STATIC: 0x00D44BF8 +CGxShader* Liquid::CMaterialWaterNoSpec::s_pixelShader; + +// VERIFY: 6cccb7b7 auto +// OFFSET: 0x008A3F50 +void Liquid::CMaterialWater::Release() { + this->~CMaterialWater(); + Liquid::s_unk_D45070.PutData(this, nullptr, 0); +} + +// VERIFY: 05103905 auto +// OFFSET: 0x008A3F70 +Liquid::CMaterialWater::CMaterialWater() { + if (Liquid::CMaterialWater::s_refCount++ == 0) { + g_theGxDevicePtr->ShaderCreate(Liquid::CMaterialWater::s_vertexShaders, GxSh_Vertex, "Shaders\\Vertex", "vsLiquidWater", 4); + g_theGxDevicePtr->ShaderCreate(&Liquid::CMaterialWater::s_pixelShader, GxSh_Pixel, "Shaders\\Pixel", "psLiquidWater", 1); + } +} + +// VERIFY: 181045ab manual +// OFFSET: 0x008A3FE0 +Liquid::CMaterialWater::~CMaterialWater() { + if (Liquid::CMaterialWater::s_refCount-- == 1) { + for (uint32_t i = 0; i < 4; i++) { + if (Liquid::CMaterialWater::s_vertexShaders[i]) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialWater::s_vertexShaders[i]); + } + } + + if (Liquid::CMaterialWater::s_pixelShader) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialWater::s_pixelShader); + } + } +} + +// VERIFY: 62a636a6 auto +// OFFSET: 0x008A4050 +void Liquid::CMaterialWaterNoSpec::Release() { + this->~CMaterialWaterNoSpec(); + Liquid::s_unk_D45084.PutData(this, nullptr, 0); +} + +// VERIFY: 4a9ff4b4 auto +// OFFSET: 0x008A4070 +Liquid::CMaterialWaterNoSpec::CMaterialWaterNoSpec() { + if (Liquid::CMaterialWaterNoSpec::s_refCount++ == 0) { + g_theGxDevicePtr->ShaderCreate(Liquid::CMaterialWaterNoSpec::s_vertexShaders, GxSh_Vertex, "Shaders\\Vertex", "vsLiquidWaterNoSpec", 4); + g_theGxDevicePtr->ShaderCreate(&Liquid::CMaterialWaterNoSpec::s_pixelShader, GxSh_Pixel, "Shaders\\Pixel", "psLiquidWaterNoSpec", 1); + } +} + +// VERIFY: eafe186b manual +// OFFSET: 0x008A40E0 +Liquid::CMaterialWaterNoSpec::~CMaterialWaterNoSpec() { + if (Liquid::CMaterialWaterNoSpec::s_refCount-- == 1) { + for (uint32_t i = 0; i < 4; i++) { + if (Liquid::CMaterialWaterNoSpec::s_vertexShaders[i]) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialWaterNoSpec::s_vertexShaders[i]); + } + } + + if (Liquid::CMaterialWaterNoSpec::s_pixelShader) { + g_theGxDevicePtr->ShaderDestroy(Liquid::CMaterialWaterNoSpec::s_pixelShader); + } + } +} + +// VERIFY: 2a90d358 auto +// OFFSET: 0x008A4150 +void Liquid::CMaterialWaterFFP::Release() { + this->~CMaterialWaterFFP(); + Liquid::s_unk_D45098.PutData(this, nullptr, 0); +} + +// VERIFY: b9a59821 auto +// OFFSET: 0x008A4790 +Liquid::CMaterialWater* Liquid::CMaterialWater::Create() { + void* mem = Liquid::s_unk_D45070.GetData(0, ".?AVCMaterialWater@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialWater() : nullptr; +} + +// VERIFY: 942c0c77 auto +// OFFSET: 0x008A47F0 +Liquid::CMaterialWaterNoSpec* Liquid::CMaterialWaterNoSpec::Create() { + void* mem = Liquid::s_unk_D45084.GetData(0, ".?AVCMaterialWaterNoSpec@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialWaterNoSpec() : nullptr; +} + +// VERIFY: 193cac49 auto +// OFFSET: 0x008A4850 +Liquid::CMaterialWaterFFP* Liquid::CMaterialWaterFFP::Create() { + void* mem = Liquid::s_unk_D45098.GetData(0, ".?AVCMaterialWaterFFP@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CMaterialWaterFFP() : nullptr; +} + +// VERIFY: a882be97 manual +// OFFSET: 0x008A5590 +void Liquid::CMaterialWater::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture1 = settings->GetTexture(0, settings->GetInt(1)); + + if (!texture1) { + return; + } + + CGxTex* texture0 = settings->GetTexture(1, 1250); + + if (!texture0) { + return; + } + + float scale1 = settings->GetFloat(0); + float angle1 = settings->GetFloat(1) * 57.29578f; + float scale0 = settings->GetFloat(2); + + g_theGxDevicePtr->RsPush(); + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + g_theGxDevicePtr->RsSet(GxRs_BlendingMode, 2); + GxUpdateAlphaRef(GxRs_AlphaRef); + GxRsSet(GxRs_DepthWrite, g_theGxDevicePtr->Caps().m_maxClipPlanes == 0); + g_theGxDevicePtr->RsSet(GxRs_Fog, 1); + + Liquid::s_vsConstants.m_projection = g_theGxDevicePtr->m_projNative; + + if (g_theGxDevicePtr->DeviceApi() == GxApi_OpenGl) { + Liquid::s_vsConstants.m_projection.c0 *= -1.0f; + Liquid::s_vsConstants.m_projection.c1 *= -1.0f; + Liquid::s_vsConstants.m_projection.c2 *= -1.0f; + Liquid::s_vsConstants.m_projection.c3 *= -1.0f; + } + + C44Matrix matrix; + Liquid::SetShaderTransforms(cameraPos, geomFactory->SetTransform(const_cast(transform)) ? matrix : *transform); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + Liquid::SetShaderLighting(lighting); + + C44Matrix texMatrix1; + texMatrix1.b1 = scale0; + Liquid::s_vsConstants.m_texTransforms[1] = texMatrix1; + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + + C44Matrix texMatrix0 = C44Matrix::RotationAroundZ(angle1); + texMatrix0.Scale(scale1); + Liquid::s_vsConstants.m_texTransforms[0] = texMatrix0; + + g_theGxDevicePtr->RsSet(GxRs_Texture1, texture1); + Liquid::SetShaderConstants(); + + uint32_t lightCount = lighting.m_lightCount; + + if (lightCount >= 3) { + lightCount = 3; + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, Liquid::CMaterialWater::s_vertexShaders[lightCount]); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, Liquid::CMaterialWater::s_pixelShader); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + geomFactory->GetBuffers(GxVBF_PNCT2, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PNCT2); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + g_theGxDevicePtr->Draw(&batch, 1); + g_theGxDevicePtr->RsPop(); +} + +// VERIFY: b899ce66 manual +// OFFSET: 0x008A5900 +void Liquid::CMaterialWaterNoSpec::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture1 = settings->GetTexture(0, settings->GetInt(1)); + + if (!texture1) { + return; + } + + CGxTex* texture0 = settings->GetTexture(1, 1250); + + if (!texture0) { + return; + } + + float scale1 = settings->GetFloat(0); + float angle1 = settings->GetFloat(1) * 57.29578f; + float scale0 = settings->GetFloat(2); + + g_theGxDevicePtr->RsPush(); + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + g_theGxDevicePtr->RsSet(GxRs_BlendingMode, 2); + GxUpdateAlphaRef(GxRs_AlphaRef); + GxRsSet(GxRs_DepthWrite, g_theGxDevicePtr->Caps().m_maxClipPlanes == 0); + g_theGxDevicePtr->RsSet(GxRs_Fog, 1); + + Liquid::s_vsConstants.m_projection = g_theGxDevicePtr->m_projNative; + + if (g_theGxDevicePtr->DeviceApi() == GxApi_OpenGl) { + Liquid::s_vsConstants.m_projection.c0 *= -1.0f; + Liquid::s_vsConstants.m_projection.c1 *= -1.0f; + Liquid::s_vsConstants.m_projection.c2 *= -1.0f; + Liquid::s_vsConstants.m_projection.c3 *= -1.0f; + } + + C44Matrix matrix; + Liquid::SetShaderTransforms(cameraPos, geomFactory->SetTransform(const_cast(transform)) ? matrix : *transform); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + Liquid::SetShaderLighting(lighting); + + C44Matrix texMatrix1; + texMatrix1.b1 = scale0; + Liquid::s_vsConstants.m_texTransforms[1] = texMatrix1; + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + + C44Matrix texMatrix0 = C44Matrix::RotationAroundZ(angle1); + texMatrix0.Scale(scale1); + Liquid::s_vsConstants.m_texTransforms[0] = texMatrix0; + + g_theGxDevicePtr->RsSet(GxRs_Texture1, texture1); + Liquid::SetShaderConstants(); + + uint32_t lightCount = lighting.m_lightCount; + + if (lightCount >= 3) { + lightCount = 3; + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, Liquid::CMaterialWaterNoSpec::s_vertexShaders[lightCount]); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, Liquid::CMaterialWaterNoSpec::s_pixelShader); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + geomFactory->GetBuffers(GxVBF_PNCT2, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PNCT2); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + g_theGxDevicePtr->Draw(&batch, 1); + g_theGxDevicePtr->RsPop(); +} + +// VERIFY: 556f1caa manual +// OFFSET: 0x008A5C70 +void Liquid::CMaterialWaterFFP::Render(Liquid::IEnvironment* environment, Liquid::IGeomFactory* geomFactory, Liquid::IWaveManager* waveManager, const C3Vector& cameraPos, const C44Matrix* transform, const CAaSphere* bounds, Liquid::CMaterialSettings* settings) { + CGxTex* texture1 = settings->GetTexture(0, settings->GetInt(1)); + + if (!texture1) { + return; + } + + CGxTex* texture0 = settings->GetTexture(1, 1250); + + if (!texture0) { + return; + } + + float scale1 = settings->GetFloat(0) * Liquid::WorldCoordScale(); + float angle1 = settings->GetFloat(1) * 57.29578f; + float scale0 = settings->GetFloat(2) * Liquid::WorldCoordScale(); + + g_theGxDevicePtr->RsPush(); + g_theGxDevicePtr->XformPush(GxXform_World); + g_theGxDevicePtr->RsSet(GxRs_Culling, 0); + g_theGxDevicePtr->RsSet(GxRs_BlendingMode, 2); + GxUpdateAlphaRef(GxRs_AlphaRef); + GxRsSet(GxRs_DepthWrite, g_theGxDevicePtr->Caps().m_maxClipPlanes == 0); + + C44Matrix world = *transform; + + if (Liquid::UseCameraRelativeCoords()) { + world.d0 -= cameraPos.x; + world.d1 -= cameraPos.y; + world.d2 -= cameraPos.z; + } + + GxXformSet(GxXform_World, world); + + CM2Lighting lighting(*bounds); + environment->SelectLights(&lighting); + lighting.SetupGxLights(&cameraPos); + lighting.SetupGxFog(); + + g_theGxDevicePtr->RsSet(GxRs_Texture0, texture0); + g_theGxDevicePtr->RsSet(GxRs_TextureCoord0, 1); + + C44Matrix texMatrix0; + texMatrix0.b1 = scale0; + g_theGxDevicePtr->XformPushAndSet(GxXform_Tex0, texMatrix0); + + g_theGxDevicePtr->RsSet(GxRs_Texture1, texture1); + g_theGxDevicePtr->RsSet(GxRs_TextureCoord1, 0); + g_theGxDevicePtr->RsSet(GxRs_TextureShader1, 1); + g_theGxDevicePtr->RsSet(GxRs_ColorOp1, 2); + g_theGxDevicePtr->RsSet(GxRs_AlphaOp1, 3); + + C44Matrix texMatrix1 = C44Matrix::RotationAroundZ(angle1); + texMatrix1.Scale(scale1); + g_theGxDevicePtr->XformPushAndSet(GxXform_Tex1, texMatrix1); + + CGxBuf* vertexBuf = nullptr; + CGxBuf* indexBuf = nullptr; + CGxBatch batch; + batch.m_primType = GxPrims_Last; + batch.m_start = 0; + batch.m_count = 0; + batch.m_minIndex = 0; + batch.m_maxIndex = 0; + geomFactory->GetBuffers(GxVBF_PNCT2, &vertexBuf, &indexBuf, &batch); + GxPrimVertexPtr(vertexBuf, GxVBF_PNCT2); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + g_theGxDevicePtr->Draw(&batch, 1); + + GxXformPop(GxXform_Tex1); + GxXformPop(GxXform_Tex0); + GxXformPop(GxXform_World); + g_theGxDevicePtr->RsPop(); +} diff --git a/WoW/Source/LiquidSystem/Types.h b/WoW/Source/LiquidSystem/Types.h new file mode 100644 index 0000000..93f944b --- /dev/null +++ b/WoW/Source/LiquidSystem/Types.h @@ -0,0 +1,26 @@ +#ifndef WOW_SOURCE_LIQUIDSYSTEM_TYPES_H +#define WOW_SOURCE_LIQUIDSYSTEM_TYPES_H + +#include +#include + +struct SMLiquidChunkDesc { + /* 0000 */ uint32_t m_offsetInstances; + /* 0004 */ uint32_t m_layerCount; + /* 0008 */ uint32_t m_offsetAttributes; +}; + +struct SMLiquidInstance { + /* 0000 */ uint16_t m_liquidType; + /* 0002 */ uint16_t m_vertexFormat; + /* 0004 */ float m_minHeight; + /* 0008 */ float m_maxHeight; + /* 000C */ uint8_t m_xOffset; + /* 000D */ uint8_t m_yOffset; + /* 000E */ uint8_t m_width; + /* 000F */ uint8_t m_height; + /* 0010 */ uint32_t m_offsetExistsBitmap; + /* 0014 */ uint32_t m_offsetVertexData; +}; + +#endif diff --git a/WoW/Source/LoadingScreen.cpp b/WoW/Source/LoadingScreen.cpp index b3779a4..097898d 100644 --- a/WoW/Source/LoadingScreen.cpp +++ b/WoW/Source/LoadingScreen.cpp @@ -849,3 +849,32 @@ void LoadingScreenMiniUpdate(float progress) { LoadingScreenMiniEnable(); s_progress = progress; } + +// VERIFY: c6318812 manual +// OFFSET: 0x0040A160 +bool LoadDynamicBackground() { + CStatus status; + + if (!s_dynamicElementsTexture) { + CGxTexFlags flags; + s_dynamicElementsTexture = TextureCreate("Interface\\Glues\\LoadingScreens\\DynamicElements", flags, &status, 1); + + if (!s_dynamicElementsTexture) { + return false; + } + } + + for (uint32_t i = 0; i < 12; i++) { + char fileName[260]; + SStrPrintf(fileName, sizeof(fileName), "Interface\\WorldMap\\World\\World%d", i + 1); + + CGxTexFlags flags; + s_dynamicBackgroundTextures[i] = TextureCreate(fileName, flags, &status, 1); + + if (!s_dynamicBackgroundTextures[i]) { + return false; + } + } + + return true; +} diff --git a/WoW/Source/WorldClient/CChunkLiquid.cpp b/WoW/Source/WorldClient/CChunkLiquid.cpp deleted file mode 100644 index 6e2291d..0000000 --- a/WoW/Source/WorldClient/CChunkLiquid.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "CChunkLiquid.h" diff --git a/WoW/Source/WorldClient/CChunkLiquid.h b/WoW/Source/WorldClient/CChunkLiquid.h index 0ad30f8..ea5b32c 100644 --- a/WoW/Source/WorldClient/CChunkLiquid.h +++ b/WoW/Source/WorldClient/CChunkLiquid.h @@ -3,61 +3,62 @@ #include #include -#include "Engine/Source/Tempest/C2Vector.h" -#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Base/BitArray.h" +#include "Engine/Source/Gx/Types.h" #include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C2Vector.h" #include "Engine/Source/Tempest/C2iVector.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Tempest/CAaSphere.h" -class CChunkLiquid; class CMapChunk; -class CMapChunkLiquidBuf; -class CChunkGeomFactory { - public: - /* 0004 */ uint32_t unk_0004; - /* 0008 */ uint32_t unk_0008; - /* 000C */ uint32_t unk_000C; - /* 0010 */ uint32_t unk_0010; - /* 0014 */ CChunkLiquid* m_liquidChunk; - /* 0018 */ uint32_t unk_0018; - /* 001C */ CMapChunkLiquidBuf* m_mapChunkBuf; - /* 0020 */ uint32_t unk_0020; - /* 0024 */ uint32_t unk_0024; - /* 0028 */ uint32_t unk_0028; - /* 002C */ uint16_t m_unkFlags2c; - /* 002E */ uint16_t m_unkFlags2e; - /* 0030 */ uint32_t unk_0030; - /* 0034 */ C44Matrix m_matrix; - - virtual ~CChunkGeomFactory(); -}; +namespace Liquid { + class CInstance; + class CWaveManager; + class IVertexData; +} class CChunkLiquid { public: + static Liquid::CWaveManager* s_waveManager; + static int32_t s_waveManagerRefs; + /* 0000 */ int32_t m_objectIndex; - /* 0004 */ int32_t unk_0004; + /* 0004 */ int32_t m_type; /* 0008 */ int32_t unk_0008; /* 000C */ C3Vector m_topLeftCoords; - /* 0018 */ C3Vector m_center; - /* 0024 */ float m_radius; + /* 0018 */ CAaSphere m_sphere; /* 0028 */ C2Vector m_height; /* 0030 */ float m_resPurgeTimer; /* 0034 */ C2iVector m_tileBegin; /* 003C */ C2iVector m_tileEnd; - /* 0044 */ int32_t unk_0044; - /* 0048 */ int32_t unk_0048; - /* 004C */ int32_t unk_004C; - /* 0050 */ int32_t unk_0050; + /* 0044 */ Liquid::IVertexData* m_vertexData; + /* 0048 */ CBitArray m_tileBits; /* 0054 */ uint8_t* m_tiles; - /* 0058 */ uint8_t* m_liquidInstPtr; + /* 0058 */ Liquid::CInstance* m_instance; /* 005C */ CMapChunk* m_owner; - /* 0060 */ CChunkLiquid* m_prev; - /* 0064 */ CChunkLiquid* m_next; - /* 0068 */ int32_t unk_0068; - /* 006C */ int32_t unk_006C; - /* 0070 */ int32_t unk_0070; - /* 0074 */ int32_t unk_0074; - /* 0078 */ C3Vector m_verts [81]; + /* 0060 */ TSLink m_link; + /* 0068 */ TSLink m_updateLink; + /* 0070 */ TSLink m_chunkLink; + /* 0078 */ C3Vector m_verts[81]; + + CChunkLiquid(); + ~CChunkLiquid(); + void Batch(); + void BuildIndices(uint16_t* indices, uint16_t baseVertex, CGxBatch* batch) const; + void BuildVertices(const C44Matrix* transform, uint32_t stride, C3Vector** position, C3Vector** normal, uint32_t** color, C2Vector** texCoord1, C2Vector** texCoord0) const; + void CreateVertXY(); + void GetAaBox(CAaBox& box) const; + bool GetHeight(const C2Vector& frac, const C2iVector& tile, float& height) const; + CAaSphere* GetSphere(); + bool GetTileInfo(const C2iVector& tile, int32_t* type, int32_t* flag6, int32_t* flag7) const; + void Purge(); + void RenderPrep(); + bool TileExists(uint32_t x, uint32_t y) const; + void UpdateResourcePurging(); }; #endif diff --git a/WoW/Source/WorldClient/CWorldMath.cpp b/WoW/Source/WorldClient/CWorldMath.cpp index 6775ef8..9e5e4e8 100644 --- a/WoW/Source/WorldClient/CWorldMath.cpp +++ b/WoW/Source/WorldClient/CWorldMath.cpp @@ -1 +1,135 @@ #include "CWorldMath.h" +#include "Engine/Source/Tempest/C33Matrix.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include + +// VERIFY: a0e6ed09 manual +// OFFSET: 0x007A61D0 +int32_t CWorldMath::ComputeAaBoxOutcode(CAaBox* box, C3Vector* point) { + float d0 = point->x - box->b.x + 0.019444443f; + float d1 = point->y - box->b.y + 0.019444443f; + float d2 = point->z - box->b.z + 0.019444443f; + float d3 = box->t.x - point->x + 0.019444443f; + float d4 = box->t.y - point->y + 0.019444443f; + float d5 = box->t.z - point->z + 0.019444443f; + + uint32_t outcode = *reinterpret_cast(&d0) >> 31; + outcode |= (*reinterpret_cast(&d1) >> 30) & 0x02; + outcode |= (*reinterpret_cast(&d2) >> 29) & 0x04; + outcode |= (*reinterpret_cast(&d3) >> 28) & 0x08; + outcode |= (*reinterpret_cast(&d4) >> 27) & 0x10; + outcode |= (*reinterpret_cast(&d5) >> 26) & 0x20; + + return outcode; +} + +// VERIFY: fc48135f auto +// OFFSET: 0x007F9320 +void CWorldMath::TransformAABoxInner(const float* row1, const float* row0, CAaBox& dst, CAaBox& src, const float* row2) { + const float* rows[3] = { row0, row1, row2 }; + + for (int32_t axis = 0; axis < 3; axis++) { + for (int32_t component = 0; component < 3; component++) { + float e = rows[component][axis]; + + float a = e * (&src.b.x)[component]; + float b = e * (&src.t.x)[component]; + + (&dst.b.x)[axis] += a < b ? a : b; + (&dst.t.x)[axis] += a < b ? b : a; + } + } +} + +// VERIFY: 51bf69b0 manual +// OFFSET: 0x007F93D0 +void CWorldMath::TransformAABox(C33Matrix& m, CAaBox& src, CAaBox& dst) { + dst.b = { 0.0f, 0.0f, 0.0f }; + dst.t = { 0.0f, 0.0f, 0.0f }; + + CWorldMath::TransformAABoxInner(&m.b0, &m.a0, dst, src, &m.c0); +} + +// VERIFY: 07b42bca manual +// OFFSET: 0x007F9430 +void CWorldMath::TransformAABox(C44Matrix& m, CAaBox& src, CAaBox& dst) { + dst.b = C3Vector(m.d0, m.d1, m.d2); + dst.t = C3Vector(m.d0, m.d1, m.d2); + + CWorldMath::TransformAABoxInner(&m.b0, &m.a0, dst, src, &m.c0); +} + +// VERIFY: 011d2b0a manual +// OFFSET: 0x007F9480 +bool CWorldMath::VectorIntersectAABox2(CAaBox& box, C3Vector& start, C3Vector& end) { + const float* minB = &box.b.x; + const float* maxB = &box.t.x; + const float* origin = &start.x; + const float* target = &end.x; + + float hit[3]; + float dir[3]; + float maxT[3]; + + dir[0] = end.x - start.x; + dir[1] = end.y - start.y; + dir[2] = end.z - start.z; + maxT[0] = -1.0f; + maxT[1] = -1.0f; + maxT[2] = -1.0f; + + int32_t inside = 1; + + for (uint32_t i = 0; i < 3; i++) { + if (origin[i] < minB[i]) { + if (target[i] < minB[i]) { + return false; + } + + hit[i] = minB[i]; + inside = 0; + + if (dir[i] != 0.0f) { + maxT[i] = (minB[i] - origin[i]) / dir[i]; + } + } else if (origin[i] > maxB[i]) { + if (target[i] > maxB[i]) { + return false; + } + + hit[i] = maxB[i]; + inside = 0; + + if (dir[i] != 0.0f) { + maxT[i] = (maxB[i] - origin[i]) / dir[i]; + } + } + } + + if (inside) { + return true; + } + + uint32_t plane = maxT[0] < maxT[1]; + + if (maxT[plane] < maxT[2]) { + plane = 2; + } + + if (*reinterpret_cast(&maxT[plane]) & 0x80000000) { + return false; + } + + for (uint32_t i = 0; i < 3; i++) { + if (i != plane) { + hit[i] = origin[i] + maxT[plane] * dir[i]; + + if (minB[i] - 0.00001f > hit[i] || maxB[i] + 0.00001f < hit[i]) { + return false; + } + } + } + + return true; +} diff --git a/WoW/Source/WorldClient/CWorldMath.h b/WoW/Source/WorldClient/CWorldMath.h index 43bafb8..be8cdd7 100644 --- a/WoW/Source/WorldClient/CWorldMath.h +++ b/WoW/Source/WorldClient/CWorldMath.h @@ -1,4 +1,20 @@ #ifndef WOW_SOURCE_WORLDCLIENT_CWORLDMATH_H #define WOW_SOURCE_WORLDCLIENT_CWORLDMATH_H +#include + +class C33Matrix; +class C3Vector; +class C44Matrix; +class CAaBox; + +class CWorldMath { + public: + static void TransformAABox(C44Matrix& m, CAaBox& src, CAaBox& dst); + static void TransformAABoxInner(const float* row1, const float* row0, CAaBox& dst, CAaBox& src, const float* row2); + static void TransformAABox(C33Matrix& m, CAaBox& src, CAaBox& dst); + static bool VectorIntersectAABox2(CAaBox& box, C3Vector& start, C3Vector& end); + static int32_t ComputeAaBoxOutcode(CAaBox* box, C3Vector* point); +}; + #endif diff --git a/WoW/Source/WorldClient/CWorldOcclusion.h b/WoW/Source/WorldClient/CWorldOcclusion.h index 54a379d..4e3e55b 100644 --- a/WoW/Source/WorldClient/CWorldOcclusion.h +++ b/WoW/Source/WorldClient/CWorldOcclusion.h @@ -5,6 +5,10 @@ #include #include "Engine/Source/Storm/H/stpl.h" +class C3Vector; +class CAaBox; +class CAaSphere; + class CWorldOccluder { public: static void Initialize(); @@ -28,4 +32,14 @@ class CClipVolume { /* 0004 */ uint32_t m_planeCount; }; +class CWorldOcclusion { + public: + static void ClearVolumes(); + static uint32_t GetClipVolumeCount(); + static uint32_t QueryVolumes(CAaSphere* sphere); + static uint32_t QueryVolumes(C3Vector* sphere, uint32_t count); + static uint32_t QueryBuffer(CAaBox* box, uint8_t flags); + static uint32_t QueryBuffer(CAaSphere* box, uint8_t flags); +}; + #endif diff --git a/WoW/Source/WorldClient/CWorldSceneRender.cpp b/WoW/Source/WorldClient/CWorldSceneRender.cpp index b179ffb..f758d23 100644 --- a/WoW/Source/WorldClient/CWorldSceneRender.cpp +++ b/WoW/Source/WorldClient/CWorldSceneRender.cpp @@ -1 +1,42 @@ #include "CWorldSceneRender.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/World.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" + +// VERIFY: 78b69e1a manual +// OFFSET: 0x007D5610 +void CWorldScene::RenderCollisionDebug() { + if (!CMap::debugVertexArray.Count()) { + return; + } + + g_theGxDevicePtr->RsPush(); + GxRsSet(GxRs_PolygonOffset, 1.0f); + + C44Matrix matrix; + matrix.Translate(C3Vector(-CWorldScene::s_activeWorldView.x, -CWorldScene::s_activeWorldView.y, -CWorldScene::s_activeWorldView.z)); + g_theGxDevicePtr->XformPushAndSet(GxXform_World, matrix); + + GxRsSet(GxRs_BlendingMode, 2); + GxUpdateAlphaRef(GxRs_AlphaRef); + GxRsSet(GxRs_Lighting, 0); + GxRsSet(GxRs_DepthWrite, 0); + GxRsSet(GxRs_DepthTest, 0); + GxRsSet(GxRs_Culling, 0); + + CGxVertexPC* vertices = CMap::debugVertexArray.m_data; + GxPrimVertexPtr(CMap::debugVertexArray.Count(), &vertices->p, sizeof(CGxVertexPC), nullptr, 0, &vertices->c, sizeof(CGxVertexPC), nullptr, 0, nullptr, 0); + GxPrimIndexPtr(CMap::debugIndexArray.Count(), CMap::debugIndexArray.m_data); + + CGxBatch batch; + batch.m_count = CMap::debugIndexArray.Count(); + batch.m_maxIndex = CMap::debugVertexArray.Count() - 1; + batch.m_primType = GxPrim_Triangles; + batch.m_start = 0; + batch.m_minIndex = 0; + g_theGxDevicePtr->Draw(&batch, 1); + + GxXformPop(GxXform_World); + g_theGxDevicePtr->RsPop(); +} diff --git a/WoW/Source/WorldClient/ClipInfo.cpp b/WoW/Source/WorldClient/ClipInfo.cpp index 59e6a3b..5fa4acb 100644 --- a/WoW/Source/WorldClient/ClipInfo.cpp +++ b/WoW/Source/WorldClient/ClipInfo.cpp @@ -1 +1,17 @@ #include "ClipInfo.h" +#include "Engine/Source/Tempest/C3Vector.h" + +// VERIFY: caefdc42 auto +// OFFSET: 0x0078FFA0 +void World::CLIPINFO::Set(const C3Vector& ndc) { + this->d[0] = ndc.x; + this->d[1] = 1.0f - ndc.x; + this->d[2] = ndc.y; + this->d[3] = 1.0f - ndc.y; + this->d[4] = ndc.z; + this->d[5] = 1.0f - ndc.z; + + const uint32_t* bits = reinterpret_cast(this->d); + + this->outcode = (bits[0] & 0x80000000) + ((bits[1] >> 1) & 0x40000000) + ((bits[2] >> 2) & 0x20000000) + ((bits[3] >> 3) & 0x10000000) + ((bits[4] >> 4) & 0x08000000) + ((bits[5] >> 5) & 0x04000000); +} diff --git a/WoW/Source/WorldClient/ClipInfo.h b/WoW/Source/WorldClient/ClipInfo.h index 3c82788..0ff495f 100644 --- a/WoW/Source/WorldClient/ClipInfo.h +++ b/WoW/Source/WorldClient/ClipInfo.h @@ -4,6 +4,8 @@ #include #include +class C3Vector; + class ClipInfo { public: /* 0000 */ uint32_t unk_0000; @@ -15,4 +17,17 @@ class ClipInfo { /* 0018 */ uint32_t unk_0018; }; +namespace World { + + struct CLIPINFO { + float d[6]; + uint32_t outcode; + uint32_t pad; + + void Set(const C3Vector& ndc); + }; + +} + + #endif diff --git a/WoW/Source/WorldClient/FVBBList.cpp b/WoW/Source/WorldClient/FVBBList.cpp index f2f8931..2c6cfca 100644 --- a/WoW/Source/WorldClient/FVBBList.cpp +++ b/WoW/Source/WorldClient/FVBBList.cpp @@ -3,6 +3,19 @@ #include "Engine/Source/Gx/CGxBuf.h" #include "Engine/Source/Gx/CGxPool.h" +// VERIFY: 931e6f81 manual +// OFFSET: 0x007D55B0 +void FVBBList::Release(Block* block) { + if (!block) { + return; + } + + *block->m_owner = nullptr; + block->m_owner = nullptr; + block->m_link.Unlink(); + this->m_blocks.LinkToTail(block); +} + // VERIFY: 59fe6fba manual // OFFSET: 0x007D58B0 void FVBBList::Initialize(EGxPoolTarget target, EGxPoolUsage usage, uint32_t itemSize, uint32_t itemCount, uint32_t blockCount) { diff --git a/WoW/Source/WorldClient/FVBBList.h b/WoW/Source/WorldClient/FVBBList.h index 0580ece..9eededf 100644 --- a/WoW/Source/WorldClient/FVBBList.h +++ b/WoW/Source/WorldClient/FVBBList.h @@ -15,7 +15,7 @@ class FVBBList { /* 0000 */ TSLink m_link; /* 0008 */ uint32_t unk_0008; /* 000C */ CGxBuf* m_buf; - /* 0010 */ uint32_t unk_0010; + /* 0010 */ Block** m_owner; }; /* 0000 */ CGxPool* m_pool; @@ -24,6 +24,7 @@ class FVBBList { void Destroy(); void Initialize(EGxPoolTarget target, EGxPoolUsage usage, uint32_t itemSize, uint32_t itemCount, uint32_t blockCount); + void Release(Block* block); }; #endif diff --git a/WoW/Source/WorldClient/Liquid.cpp b/WoW/Source/WorldClient/Liquid.cpp deleted file mode 100644 index 4891b88..0000000 --- a/WoW/Source/WorldClient/Liquid.cpp +++ /dev/null @@ -1,274 +0,0 @@ -#include "Liquid.h" -#include "Engine/Source/Base/Handle.h" -#include "Engine/Source/Gx/CGxTexFlags.h" -#include "Engine/Source/Services/Texture.h" -#include "Engine/Source/Storm/H/storm.h" - -// STATIC: 0x00D31960 -CDataAllocator Liquid::s_unk_D31960(0x48, 4); - -// STATIC: 0x00D31974 -CDataAllocator Liquid::s_unk_D31974(0x74, 0x10); - -// STATIC: 0x00D31A00 -CDataAllocator Liquid::s_unk_D31A00(0xC, 0x10); - -// STATIC: 0x00D31A30 -CDataAllocator Liquid::s_unk_D31A30(0x128, 0x10); - -// STATIC: 0x00D43B00 -CDataAllocator Liquid::s_unk_D43B00(0x43C, 0x20); - -// STATIC: 0x00D43B24 -TSGrowableArray Liquid::CMaterialBank::s_unk_D43B24; - -// STATIC: 0x00D43B34 -CDataAllocator Liquid::s_unk_D43B34(0x6C, 0x20); - -// STATIC: 0x00D45048 -CDataAllocator Liquid::s_unk_D45048(4, 8); - -// STATIC: 0x00D4505C -CDataAllocator Liquid::s_unk_D4505C(4, 8); - -// STATIC: 0x00D45070 -CDataAllocator Liquid::s_unk_D45070(4, 8); - -// STATIC: 0x00D45084 -CDataAllocator Liquid::s_unk_D45084(4, 8); - -// STATIC: 0x00D45098 -CDataAllocator Liquid::s_unk_D45098(4, 8); - -// STATIC: 0x00D450AC -CDataAllocator Liquid::s_unk_D450AC(4, 8); - -// STATIC: 0x00D450C0 -CDataAllocator Liquid::s_unk_D450C0(4, 8); - -// STATIC: 0x00B23F64 -float Liquid::s_unk_B23F64 = 1.0f; - -// STATIC: 0x00B23F68 -int32_t Liquid::s_unk_B23F68 = 1; - -// STATIC: 0x00D439E8 -IDatabase* Liquid::s_liquidMaterialDB; - -// STATIC: 0x00D439EC -IDatabase* Liquid::s_liquidTypeDB; - -// STATIC: 0x00D439F0 -char Liquid::s_unk_D439F0[256]; - -// STATIC: 0x00D43AF0 -int32_t Liquid::s_unk_D43AF0; - -// STATIC: 0x00D43AF4 -int32_t Liquid::s_unk_D43AF4; - -// STATIC: 0x00D43AF8 -int32_t Liquid::s_unk_D43AF8; - -// STATIC: 0x00D43AFC -int32_t Liquid::s_unk_D43AFC; - -// STATIC: 0x00D43B14 -TSGrowableArray Liquid::CSettingsBank::s_settings; - -// STATIC: 0x00D43B48 -uint8_t Liquid::s_unk_D43B48[4]; - -// STATIC: 0x00D43B4C -CTexture* Liquid::s_unk_D43B4C; - -// STATIC: 0x00D43B50 -CTexture* Liquid::s_unk_D43B50[32]; - -// STATIC: 0x00D43BD0 -CTexture* Liquid::s_wmoWaterTex; - -// STATIC: 0x00D43BD4 -CTexture* Liquid::s_oceanDepthTex; - -// STATIC: 0x00D43BD8 -CTexture* Liquid::s_riverDepthTex; - -// VERIFY: fb13c55c auto -// OFFSET: 0x008A1770 -int32_t Liquid::Initialize(int32_t a1, float a2, int32_t a3, const char* a4, IDatabase* liquidTypeDB, IDatabase* liquidMaterialDB) { - SStrCopy(Liquid::s_unk_D439F0, a4, sizeof(Liquid::s_unk_D439F0)); - Liquid::s_unk_B23F64 = a2; - Liquid::s_unk_D43AF8++; - Liquid::s_unk_D43AFC++; - Liquid::s_unk_D43AF4 = a1; - Liquid::s_unk_D43AF0 = a3; - Liquid::s_unk_B23F68 = 1; - Liquid::s_liquidTypeDB = liquidTypeDB; - Liquid::s_liquidMaterialDB = liquidMaterialDB; - return 1; -} - -// VERIFY: 2a7eaf1c manual -// OFFSET: 0x008A2780 -void Liquid::CSettingsBank::ResetTextures() { - for (uint32_t i = 0; i < Liquid::CSettingsBank::s_settings.Count(); i++) { - if (Liquid::CSettingsBank::s_settings[i]) { - Liquid::CSettingsBank::s_settings[i]->Destroy(); - Liquid::CSettingsBank::s_settings[i]->InitTexture(); - } - } -} - -// VERIFY: bbba7190 auto -// OFFSET: 0x008A2A10 -void Liquid::CacheProceduralTextures() { - CGxTexFlags flags; - - TextureCacheNewTexture(Liquid::s_riverDepthTex); - TextureCacheNewTexture(Liquid::s_oceanDepthTex); - TextureCacheNewTexture(Liquid::s_wmoWaterTex); - - for (uint32_t i = 0; i < sizeof(Liquid::s_unk_D43B50) / sizeof(Liquid::s_unk_D43B50[0]); i++) { - if (Liquid::s_unk_D43B50[i]) { - TextureCacheNewTexture(Liquid::s_unk_D43B50[i]); - } - } - - if (Liquid::s_unk_D43B4C) { - TextureCacheNewTexture(Liquid::s_unk_D43B4C); - } - - Liquid::CSettingsBank::ResetTextures(); -} - -// VERIFY: 5d576297 auto -// OFFSET: 0x008A2E20 -void Liquid::CreateProceduralTextures() { - Liquid::s_unk_D43B48[3] = 0; - Liquid::s_unk_D43B48[2] = 0; - Liquid::s_unk_D43B48[1] = 0; - Liquid::s_unk_D43B48[0] = 0; - - CGxTexFlags flags; - Liquid::s_riverDepthTex = TextureCreate(8, 64, GxTex_Argb8888, GxTex_Argb8888, flags, reinterpret_cast(1), Liquid::ExtWaterDepthTexCallback, "proceduralRiverDepthTex", 0); - - flags = CGxTexFlags(); - Liquid::s_oceanDepthTex = TextureCreate(8, 64, GxTex_Argb8888, GxTex_Argb8888, flags, nullptr, Liquid::ExtWaterDepthTexCallback, "proceduralOceanDepthTex", 0); - - flags = CGxTexFlags(); - Liquid::s_wmoWaterTex = TextureCreate(8, 64, GxTex_Argb8888, GxTex_Argb8888, flags, nullptr, Liquid::WmoWaterTexCallback, "proceduralWmoWaterTex", 0); - - Liquid::CacheProceduralTextures(); -} - -// VERIFY: a171683e auto -// OFFSET: 0x007D6670 -void Liquid::ClearWaveManagers() { - Liquid::s_unk_D31A30.Clear(".?AVCWaveManager@Liquid@@", SLOG_OBJECT, 1); -} - -// VERIFY: 36347972 auto -// OFFSET: 0x007D47D0 -void Liquid::ClearGeomFactories() { - Liquid::s_unk_D31960.Clear(".?AVCMeshGeomFactory@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D31974.Clear(".?AVCChunkGeomFactory@Liquid@@", SLOG_OBJECT, 1); -} - -// VERIFY: e3c196d2 auto -// OFFSET: 0x007D5100 -void Liquid::ClearClientEnvironments() { - Liquid::s_unk_D31A00.Clear(".?AVCClientEnvironment@Liquid@@", SLOG_OBJECT, 1); -} - -// VERIFY: c3031eda manual -// OFFSET: 0x008A2400 -void Liquid::Shutdown() { - int32_t count = Liquid::s_unk_D43AFC--; - Liquid::s_liquidTypeDB = nullptr; - Liquid::s_liquidMaterialDB = nullptr; - - if (count == 1) { - Liquid::CMaterialBank::Destroy(); - Liquid::s_unk_D43AF0 = 0; - } - - Liquid::CSettingsBank::Destroy(); - Liquid::ClearMaterials(); - Liquid::s_unk_D43B34.Clear(".?AVCInstance@Liquid@@", SLOG_OBJECT, 1); -} - -// VERIFY: 901024cc manual -// OFFSET: 0x008A2980 -void Liquid::DestroyProceduralTextures() { - if (Liquid::s_unk_D43B4C) { - HandleClose(reinterpret_cast(Liquid::s_unk_D43B4C)); - Liquid::s_unk_D43B4C = nullptr; - } - - if (Liquid::s_riverDepthTex) { - HandleClose(reinterpret_cast(Liquid::s_riverDepthTex)); - Liquid::s_riverDepthTex = nullptr; - } - - if (Liquid::s_oceanDepthTex) { - HandleClose(reinterpret_cast(Liquid::s_oceanDepthTex)); - Liquid::s_oceanDepthTex = nullptr; - } - - if (Liquid::s_wmoWaterTex) { - HandleClose(reinterpret_cast(Liquid::s_wmoWaterTex)); - Liquid::s_wmoWaterTex = nullptr; - } - - for (uint32_t i = 0; i < sizeof(Liquid::s_unk_D43B50) / sizeof(Liquid::s_unk_D43B50[0]); i++) { - if (Liquid::s_unk_D43B50[i]) { - HandleClose(reinterpret_cast(Liquid::s_unk_D43B50[i])); - Liquid::s_unk_D43B50[i] = nullptr; - } - } -} - -// VERIFY: 44d8beb7 manual -// OFFSET: 0x008A1F50 -void Liquid::CMaterialBank::Destroy() { - for (uint32_t i = 0; i < Liquid::CMaterialBank::s_unk_D43B24.Count(); i++) { - if (Liquid::CMaterialBank::s_unk_D43B24[i]) { - Liquid::CMaterialBank::s_unk_D43B24[i]->Release(); - } - } - - Liquid::CMaterialBank::s_unk_D43B24.SetCount(0); -} - -// VERIFY: f4d601a2 manual -// OFFSET: 0x008A2380 -void Liquid::CSettingsBank::Destroy() { - if (!--Liquid::s_unk_D43AF8) { - for (uint32_t i = 0; i < Liquid::CSettingsBank::s_settings.Count(); i++) { - if (Liquid::CSettingsBank::s_settings[i]) { - Liquid::CSettingsBank::s_settings[i]->Destroy(); - - Liquid::CMaterialSettings* settings = Liquid::CSettingsBank::s_settings[i]; - settings->~CMaterialSettings(); - Liquid::s_unk_D43B00.PutData(settings, nullptr, 0); - } - } - - Liquid::CSettingsBank::s_settings.SetCount(0); - } - - Liquid::s_unk_D43B00.Clear(".?AVCMaterialSettings@Liquid@@", SLOG_OBJECT, 1); -} - -// VERIFY: 502f2f3f auto -// OFFSET: 0x008A4280 -void Liquid::ClearMaterials() { - Liquid::s_unk_D45048.Clear(".?AVCMaterialProcWater@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D4505C.Clear(".?AVCMaterialProcWaterFFP@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D45070.Clear(".?AVCMaterialWater@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D45084.Clear(".?AVCMaterialWaterNoSpec@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D45098.Clear(".?AVCMaterialWaterFFP@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D450AC.Clear(".?AVCMaterialMagma@Liquid@@", SLOG_OBJECT, 1); - Liquid::s_unk_D450C0.Clear(".?AVCMaterialMagmaFFP@Liquid@@", SLOG_OBJECT, 1); -} diff --git a/WoW/Source/WorldClient/Liquid.h b/WoW/Source/WorldClient/Liquid.h deleted file mode 100644 index 31b3c30..0000000 --- a/WoW/Source/WorldClient/Liquid.h +++ /dev/null @@ -1,96 +0,0 @@ -#ifndef WOW_SOURCE_WORLDCLIENT_LIQUID_H -#define WOW_SOURCE_WORLDCLIENT_LIQUID_H - -#include -#include -#include "Engine/Source/Gx/Types.h" -#include "Engine/Source/Base/CDataAllocator.h" -#include "Engine/Source/Storm/H/stpl.h" - -template -class IDatabase; -class CTexture; -class LiquidMaterialRec; -class LiquidTypeRec; - -namespace Liquid { - class CInstance; - - class CRenderer { - public: - /* 0000 */ TSGrowableArray unk_0000[2]; - - void Draw(CInstance* instance); - }; - - class CMaterialSettings { - public: - ~CMaterialSettings(); - void Destroy(); - void InitTexture(); - }; - - class CSettingsBank { - public: - static TSGrowableArray s_settings; - - static void Destroy(); - static void ResetTextures(); - }; - - class IMaterial { - public: - virtual ~IMaterial(); - virtual void Release(); - }; - - class CMaterialBank { - public: - static TSGrowableArray s_unk_D43B24; - - static void Destroy(); - }; - - extern CDataAllocator s_unk_D31960; - extern CDataAllocator s_unk_D31974; - extern CDataAllocator s_unk_D31A00; - extern CDataAllocator s_unk_D31A30; - extern CDataAllocator s_unk_D43B00; - extern CDataAllocator s_unk_D43B34; - extern CDataAllocator s_unk_D45048; - extern CDataAllocator s_unk_D4505C; - extern CDataAllocator s_unk_D45070; - extern CDataAllocator s_unk_D45084; - extern CDataAllocator s_unk_D45098; - extern CDataAllocator s_unk_D450AC; - extern CDataAllocator s_unk_D450C0; - extern float s_unk_B23F64; - extern int32_t s_unk_B23F68; - extern IDatabase* s_liquidMaterialDB; - extern IDatabase* s_liquidTypeDB; - extern char s_unk_D439F0[256]; - extern int32_t s_unk_D43AF0; - extern int32_t s_unk_D43AF4; - extern int32_t s_unk_D43AF8; - extern int32_t s_unk_D43AFC; - extern uint8_t s_unk_D43B48[4]; - extern CTexture* s_unk_D43B4C; - extern CTexture* s_unk_D43B50[32]; - extern CTexture* s_wmoWaterTex; - extern CTexture* s_oceanDepthTex; - extern CTexture* s_riverDepthTex; - - void CacheProceduralTextures(); - void ClearClientEnvironments(); - void ClearGeomFactories(); - void ClearMaterials(); - void ClearWaveManagers(); - void CreateProceduralTextures(); - void DestroyProceduralTextures(); - void ExtWaterDepthTexCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); - int32_t Initialize(int32_t a1, float a2, int32_t a3, const char* a4, IDatabase* liquidTypeDB, IDatabase* liquidMaterialDB); - void Shutdown(); - void WmoWaterTexCallback(EGxTexCommand command, uint32_t width, uint32_t height, uint32_t depth, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); -} - -#endif diff --git a/WoW/Source/WorldClient/LiquidEnvironment.cpp b/WoW/Source/WorldClient/LiquidEnvironment.cpp new file mode 100644 index 0000000..05da639 --- /dev/null +++ b/WoW/Source/WorldClient/LiquidEnvironment.cpp @@ -0,0 +1,89 @@ +#include "LiquidEnvironment.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Model2/M2Scene.h" +#include "Engine/Source/Storm/H/storm.h" +#include "WoW/Source/LiquidSystem/Liquid.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapLight.h" +#include "WoW/Source/WorldClient/World.h" + +// STATIC: 0x00D31A00 +CDataAllocator Liquid::s_unk_D31A00(0xC, 0x10); + +// VERIFY: e3c196d2 auto +// OFFSET: 0x007D5100 +void Liquid::ClearClientEnvironments() { + Liquid::s_unk_D31A00.Clear(".?AVCClientEnvironment@Liquid@@", SLOG_OBJECT, 1); +} + +// VERIFY: 5ea0339f auto +// OFFSET: 0x007D4F10 +void Liquid::CClientEnvironment::SetInteriorFog(int32_t interiorFog) { + this->m_interiorFog = interiorFog; +} + +// VERIFY: 442bf5b9 auto +// OFFSET: 0x007D4F20 +void Liquid::CClientEnvironment::Release() { + this->~CClientEnvironment(); + Liquid::s_unk_D31A00.PutData(this, nullptr, 0); +} + +// VERIFY: 2a7d5c2c manual +// OFFSET: 0x007D4F40 +void Liquid::CClientEnvironment::SelectLights(CM2Lighting* lighting) { + DayNight::DNInfo* dayNight = DayNight::GetInfo(); + + C3Vector fogColor; + float fogStart; + float fogEnd; + float fogDensity; + + if (this->m_interiorFog) { + fogColor = C3Vector(dayNight->m_fogInterior.m_color); + fogDensity = dayNight->m_fogInterior.m_density; + fogEnd = dayNight->m_fogInterior.m_end; + fogStart = dayNight->m_fogInterior.m_start; + } else { + fogColor = C3Vector(dayNight->m_fog.m_color); + fogDensity = dayNight->m_fog.m_density; + fogEnd = dayNight->m_fog.m_end; + fogStart = dayNight->m_fog.m_start; + } + + lighting->SetFog(fogColor, fogStart, fogEnd, fogDensity); + + if (this->m_customLight) { + static CM2Light s_light; + static int32_t s_lightInitialized; + + if (!s_lightInitialized) { + s_light.SetLightType(M2LIGHT_DIRECT); + s_light.m_constantAttenuation = 1.0f; + s_light.m_linearAttenuation = 0.0f; + s_light.m_quadraticAttenuation = 0.0f; + s_light.SetDirection(C3Vector(0.0f, 0.0f, -1.0f)); + s_light.m_dirColor = C3Vector(1.0f, 1.0f, 1.0f); + s_light.SetVisible(1); + s_lightInitialized = 1; + } + + lighting->AddLight(&s_light); + } else { + lighting->AddLight(&CMap::s_mapLight->m_light); + } + + World::s_m2Scene->SelectLights(lighting); +} + +// VERIFY: 83664886 auto +// OFFSET: 0x007D5120 +Liquid::CClientEnvironment* Liquid::CClientEnvironment::Create(int32_t customLight) { + void* mem = Liquid::s_unk_D31A00.GetData(0, ".?AVCClientEnvironment@Liquid@@", SLOG_OBJECT); + Liquid::CClientEnvironment* environment = mem ? new (mem) Liquid::CClientEnvironment() : nullptr; + environment->m_interiorFog = 0; + environment->m_customLight = customLight; + + return environment; +} diff --git a/WoW/Source/WorldClient/LiquidEnvironment.h b/WoW/Source/WorldClient/LiquidEnvironment.h new file mode 100644 index 0000000..5ef3830 --- /dev/null +++ b/WoW/Source/WorldClient/LiquidEnvironment.h @@ -0,0 +1,29 @@ +#ifndef WOW_SOURCE_WORLDCLIENT_LIQUIDENVIRONMENT_H +#define WOW_SOURCE_WORLDCLIENT_LIQUIDENVIRONMENT_H + +#include +#include +#include "Engine/Source/Base/CDataAllocator.h" +#include "WoW/Source/LiquidSystem/Liquid.h" + +class CM2Lighting; + +namespace Liquid { + class CClientEnvironment : public IEnvironment { + public: + static CClientEnvironment* Create(int32_t customLight); + + /* 0004 */ int32_t m_interiorFog; + /* 0008 */ int32_t m_customLight; + + virtual void Release(); + virtual void SetInteriorFog(int32_t interiorFog); + virtual void SelectLights(CM2Lighting* lighting); + }; + + extern CDataAllocator s_unk_D31A00; + + void ClearClientEnvironments(); +} + +#endif diff --git a/WoW/Source/WorldClient/LiquidGeomFactories.cpp b/WoW/Source/WorldClient/LiquidGeomFactories.cpp new file mode 100644 index 0000000..7e125d0 --- /dev/null +++ b/WoW/Source/WorldClient/LiquidGeomFactories.cpp @@ -0,0 +1,337 @@ +#include "LiquidGeomFactories.h" +#include "Engine/Source/Gx/CGxBuf.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Storm/H/storm.h" +#include "WoW/Source/WorldClient/CChunkLiquid.h" +#include "WoW/Source/LiquidSystem/Liquid.h" +#include "WoW/Source/WorldClient/MapChunkLiquid.h" +#include "WoW/Source/WorldClient/MapObj.h" +#include "WoW/Source/WorldClient/MapObjGroup.h" + +// STATIC: 0x00D31960 +CDataAllocator Liquid::s_unk_D31960(0x48, 4); + +// STATIC: 0x00D31974 +CDataAllocator Liquid::s_unk_D31974(0x74, 0x10); + +// VERIFY: 36347972 auto +// OFFSET: 0x007D47D0 +void Liquid::ClearGeomFactories() { + Liquid::s_unk_D31960.Clear(".?AVCMeshGeomFactory@Liquid@@", SLOG_OBJECT, 1); + Liquid::s_unk_D31974.Clear(".?AVCChunkGeomFactory@Liquid@@", SLOG_OBJECT, 1); +} + +// VERIFY: 32ee76a0 manual +// OFFSET: 0x007D4380 +void Liquid::CMeshGeomFactory::Purge() { + if (this->m_mapObjGroup) { + this->m_mapObjGroup->FreeLiquidVB(); + } +} + +// VERIFY: 2fdfe76c auto +// OFFSET: 0x007D4390 +void Liquid::CChunkGeomFactory::Purge() { + if (this->m_buf) { + CMapChunkLiquidBuf::Free(this->m_buf); + this->m_buf = nullptr; + } +} + +// VERIFY: a06cee34 manual +// OFFSET: 0x007D43B0 +void Liquid::CMeshGeomFactory::Release() { + if (this->m_refCount-- == 1) { + this->~CMeshGeomFactory(); + Liquid::s_unk_D31960.PutData(this, nullptr, 0); + } +} + +// VERIFY: 606bea00 manual +// OFFSET: 0x007D4760 +void Liquid::CChunkGeomFactory::Release() { + if (this->m_refCount-- == 1) { + this->~CChunkGeomFactory(); + Liquid::s_unk_D31974.PutData(this, nullptr, 0); + } +} + +// VERIFY: de869a00 manual +// OFFSET: 0x007D4790 +bool Liquid::CChunkGeomFactory::SetTransform(C44Matrix* transform) { + if (this->m_transform == *transform) { + return true; + } + + this->m_transform = *transform; + this->m_dirty = 1; + + return true; +} + +// VERIFY: 1be8aa58 manual +// OFFSET: 0x007D43F0 +void Liquid::CMeshGeomFactory::GetBuffers(EGxVertexBufferFormat format, CGxBuf** vertexBuf, CGxBuf** indexBuf, CGxBatch* batch) { + if (static_cast(this->m_mapObjGroup->m_luquidMaterialId) >= static_cast(this->m_mapObj->m_materialsCount)) { + SErrDisplayAppFatal("Water in chunk \"%s\" of object \"%s\" has no materialId.", this->m_mapObjGroup->m_groupName, this->m_mapObj->m_wmoName); + } + + uint32_t sharedCols = this->m_sharedVerts[0].m_count; + uint32_t sharedRows = this->m_sharedVerts[1].m_count; + uint32_t sharedTiles = this->m_mapObjGroup->GetNumSharedTiles(); + uint32_t vertexCount = (sharedRows + this->m_mapObjGroup->m_liquidVerts.y) * (sharedCols + this->m_mapObjGroup->m_liquidVerts.x) + 6 * sharedTiles; + this->m_mapObjGroup->AllocLiquidVB(format, vertexCount, 3 * vertexCount, &this->m_batch); + + *vertexBuf = this->m_mapObjGroup->m_liquidVertsBlock->m_buffer; + *indexBuf = this->m_mapObjGroup->m_liquidIndicesBlock->m_buffer; + + if (!(*vertexBuf)->unk_001C || !(*vertexBuf)->unk_001D || !(*indexBuf)->unk_001C || !(*indexBuf)->unk_001D) { + char* vertices = g_theGxDevicePtr->BufLock(*vertexBuf); + uint16_t* indices = reinterpret_cast(g_theGxDevicePtr->BufLock(*indexBuf)); + uint16_t* indexStart = indices; + + if (!vertices || !indices) { + if (vertices) { + CGxBuf* buf = *vertexBuf; + g_theGxDevicePtr->BufUnlock(buf, 0); + buf->unk_001C = 1; + } + + if (indices) { + CGxBuf* buf = *indexBuf; + g_theGxDevicePtr->BufUnlock(buf, 0); + buf->unk_001C = 1; + } + + return; + } + + uint32_t stride = GxVertexSize(format); + C3Vector* position = reinterpret_cast(vertices + GxVertexAttribOffset(format, GxVA_Position)); + C3Vector* normal = nullptr; + CImVector* color = nullptr; + C2Vector* texCoord0 = nullptr; + C2Vector* texCoord1 = nullptr; + + if (GxVertexHasAttrib(format, GxVA_Normal)) { + normal = reinterpret_cast(vertices + GxVertexAttribOffset(format, GxVA_Normal)); + } + + if (GxVertexHasAttrib(format, GxVA_Color0)) { + color = reinterpret_cast(vertices + GxVertexAttribOffset(format, GxVA_Color0)); + } + + if (GxVertexHasAttrib(format, GxVA_TexCoord0)) { + texCoord0 = reinterpret_cast(vertices + GxVertexAttribOffset(format, GxVA_TexCoord0)); + } + + if (GxVertexHasAttrib(format, GxVA_TexCoord1)) { + texCoord1 = reinterpret_cast(vertices + GxVertexAttribOffset(format, GxVA_TexCoord1)); + } + + const CImVector* liquidColor = reinterpret_cast(&this->unk_0014); + uint16_t count = this->m_mapObj->BuildLiquidVerts(this->m_mapObjGroup, C44Matrix(), liquidColor, this->unk_001C, this->unk_0020, this->m_sharedVerts, stride, &position, &normal, &color, &texCoord0, &texCoord1); + this->m_mapObj->BuildLiquidIndices(this->m_mapObjGroup, this->m_sharedVerts, &indices, 0); + count += this->m_mapObj->ClipGroupGeometry(this->m_mapObjGroup, C44Matrix(), liquidColor, this->unk_001C, this->unk_0020, stride, &position, &normal, &color, &texCoord0, &texCoord1, &indices, count); + + CGxBuf* buf = *vertexBuf; + g_theGxDevicePtr->BufUnlock(buf, 0); + buf->unk_001C = 1; + + if (indices) { + buf = *indexBuf; + g_theGxDevicePtr->BufUnlock(buf, 0); + buf->unk_001C = 1; + } + + CGxBatch* meshBatch = this->m_batch; + meshBatch->m_primType = GxPrim_TriangleStrip; + meshBatch->m_start = 0; + meshBatch->m_count = indices - indexStart; + meshBatch->m_minIndex = 0; + meshBatch->m_maxIndex = count - 1; + } + + *batch = *this->m_batch; +} + +// VERIFY: ac0e5a30 manual +// OFFSET: 0x007D4800 +Liquid::CMeshGeomFactory::~CMeshGeomFactory() { + if (this->m_mapObjGroup) { + this->m_mapObjGroup->FreeLiquidVB(); + } +} + +// VERIFY: 2528fd80 manual +// OFFSET: 0x007D4850 +Liquid::CChunkGeomFactory::CChunkGeomFactory() { + this->m_refCount = 1; + this->m_dirty = 1; + this->m_buf = nullptr; + this->m_batch.m_primType = GxPrims_Last; + this->m_batch.m_start = 0; + this->m_batch.m_count = 0; + this->m_batch.m_minIndex = 0; + this->m_batch.m_maxIndex = 0; +} + +// VERIFY: ae53ab8b manual +// OFFSET: 0x007D48C0 +Liquid::CChunkGeomFactory::~CChunkGeomFactory() { + if (this->m_buf) { + CMapChunkLiquidBuf::Free(this->m_buf); + this->m_buf = nullptr; + } +} + +// INLINED +Liquid::CMeshGeomFactory::CMeshGeomFactory() { + this->m_refCount = 1; + this->m_mapObj = nullptr; + this->m_mapObjGroup = nullptr; + this->m_batch = nullptr; + this->unk_0014 = -1; + this->unk_0020 = 0.0f; + this->unk_001C = 0; + this->unk_0024 = 0; +} + +// VERIFY: c7ce1eb1 auto +// OFFSET: 0x007D49B0 +Liquid::CMeshGeomFactory* Liquid::CMeshGeomFactory::Create(CMapObj* mapObj, CMapObjGroup* mapObjGroup) { + Liquid::CMeshGeomFactory* factory = Liquid::s_unk_D31960.New(0); + factory->m_mapObj = mapObj; + factory->m_mapObjGroup = mapObjGroup; + + return factory; +} + +// VERIFY: 1a3ea7df manual +// OFFSET: 0x007D49D0 +Liquid::CChunkGeomFactory* Liquid::CChunkGeomFactory::Create(const TSGrowableArray& chunks) { + void* mem = Liquid::s_unk_D31974.GetData(0, ".?AVCChunkGeomFactory@Liquid@@", SLOG_OBJECT); + Liquid::CChunkGeomFactory* factory = mem ? new (mem) Liquid::CChunkGeomFactory() : nullptr; + + for (uint32_t i = 0; i < chunks.m_count; i++) { + factory->m_chunks.New(chunks.m_data[i]); + } + + return factory; +} + +// VERIFY: bb721706 manual +// OFFSET: 0x007D4AB0 +void Liquid::CChunkGeomFactory::GetBuffers(EGxVertexBufferFormat format, CGxBuf** vertexBuf, CGxBuf** indexBuf, CGxBatch* batch) { + if (this->m_buf && this->m_buf->Valid() && !this->m_dirty) { + *vertexBuf = this->m_buf->m_vertexBuf; + *indexBuf = this->m_buf->m_indexBuf; + *batch = this->m_batch; + return; + } + + static TSGrowableArray s_vertexCounts; + + s_vertexCounts.Clear(); + + uint32_t vertexCount = 0; + uint32_t indexCount = 0; + + for (uint32_t i = 0; i < this->m_chunks.m_count; i++) { + CChunkLiquid* liquid = this->m_chunks.m_data[i]; + uint16_t cols = liquid->m_tileEnd.y - liquid->m_tileBegin.y; + uint16_t rows = liquid->m_tileEnd.x - liquid->m_tileBegin.x; + uint16_t count = (cols + 1) * (rows + 1); + s_vertexCounts.New(count); + vertexCount += count; + indexCount += 6 * cols * rows; + } + + if (!this->m_buf) { + this->m_buf = CMapChunkLiquidBuf::Alloc(vertexCount, indexCount, 0, format); + } + + *vertexBuf = this->m_buf->m_vertexBuf; + *indexBuf = this->m_buf->m_indexBuf; + + if (this->m_dirty) { + (*vertexBuf)->unk_001C = 0; + (*indexBuf)->unk_001C = 0; + } + + CGxBuf* vb = *vertexBuf; + + if (!vb->unk_001C || !vb->unk_001D) { + char* data = g_theGxDevicePtr->BufLock(vb); + + if (data) { + uint32_t stride = GxVertexSize(format); + C3Vector* position = reinterpret_cast(data + GxVertexAttribOffset(format, GxVA_Position)); + C3Vector* normal = nullptr; + uint32_t* color = nullptr; + C2Vector* texCoord0 = nullptr; + C2Vector* texCoord1 = nullptr; + + if (GxVertexHasAttrib(format, GxVA_Normal)) { + normal = reinterpret_cast(data + GxVertexAttribOffset(format, GxVA_Normal)); + } + + if (GxVertexHasAttrib(format, GxVA_Color0)) { + color = reinterpret_cast(data + GxVertexAttribOffset(format, GxVA_Color0)); + } + + if (GxVertexHasAttrib(format, GxVA_TexCoord0)) { + texCoord0 = reinterpret_cast(data + GxVertexAttribOffset(format, GxVA_TexCoord0)); + } + + if (GxVertexHasAttrib(format, GxVA_TexCoord1)) { + texCoord1 = reinterpret_cast(data + GxVertexAttribOffset(format, GxVA_TexCoord1)); + } + + CChunkLiquid* first = this->m_chunks.m_data[0]; + + for (uint32_t i = 0; i < this->m_chunks.m_count; i++) { + CChunkLiquid* liquid = this->m_chunks.m_data[i]; + C44Matrix transform = this->m_transform; + C3Vector offset(liquid->m_topLeftCoords.x - first->m_topLeftCoords.x, liquid->m_topLeftCoords.y - first->m_topLeftCoords.y, liquid->m_topLeftCoords.z - first->m_topLeftCoords.z); + transform.Translate(offset); + liquid->BuildVertices(&transform, stride, &position, &normal, &color, &texCoord1, &texCoord0); + } + + g_theGxDevicePtr->BufUnlock(vb, 0); + vb->unk_001C = 1; + } + } + + CGxBuf* ib = *indexBuf; + + if (!ib->unk_001C || !ib->unk_001D) { + batch->m_primType = GxPrim_TriangleStrip; + batch->m_start = 0; + batch->m_count = 0; + batch->m_minIndex = 0; + batch->m_maxIndex = vertexCount - 1; + + uint16_t* indices = reinterpret_cast(g_theGxDevicePtr->BufLock(ib)); + uint16_t baseVertex = 0; + + for (uint32_t i = 0; i < this->m_chunks.m_count; i++) { + this->m_chunks.m_data[i]->BuildIndices(indices + batch->m_count, baseVertex, batch); + baseVertex += s_vertexCounts.m_data[i]; + } + + g_theGxDevicePtr->BufUnlock(ib, 0); + ib->unk_001C = 1; + this->m_batch = *batch; + } + + this->m_format = format; + this->m_dirty = 0; +} + +// VERIFY: b01df36e auto +// OFFSET: 0x008813A0 +bool Liquid::CMeshGeomFactory::SetTransform(C44Matrix* transform) { + return false; +} diff --git a/WoW/Source/WorldClient/LiquidGeomFactories.h b/WoW/Source/WorldClient/LiquidGeomFactories.h new file mode 100644 index 0000000..cd70c13 --- /dev/null +++ b/WoW/Source/WorldClient/LiquidGeomFactories.h @@ -0,0 +1,69 @@ +#ifndef WOW_SOURCE_WORLDCLIENT_LIQUIDGEOMFACTORIES_H +#define WOW_SOURCE_WORLDCLIENT_LIQUIDGEOMFACTORIES_H + +#include +#include +#include "Engine/Source/Base/CDataAllocator.h" +#include "Engine/Source/Gx/Types.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "WoW/Source/LiquidSystem/Liquid.h" + +class CChunkLiquid; +class CMapChunkLiquidBuf; +class CMapObj; +class CMapObjGroup; + +namespace Liquid { + class CChunkGeomFactory : public IGeomFactory { + public: + static CChunkGeomFactory* Create(const TSGrowableArray& chunks); + + /* 0004 */ int32_t m_refCount; + /* 0008 */ int32_t m_dirty; + /* 000C */ TSGrowableArray m_chunks; + /* 001C */ CMapChunkLiquidBuf* m_buf; + /* 0020 */ CGxBatch m_batch; + /* 0030 */ EGxVertexBufferFormat m_format; + /* 0034 */ C44Matrix m_transform; + + virtual ~CChunkGeomFactory(); + virtual void Release(); + virtual void GetBuffers(EGxVertexBufferFormat format, CGxBuf** vertexBuf, CGxBuf** indexBuf, CGxBatch* batch); + virtual void Purge(); + virtual bool SetTransform(C44Matrix* transform); + + CChunkGeomFactory(); + }; + + class CMeshGeomFactory : public IGeomFactory { + public: + static CMeshGeomFactory* Create(CMapObj* mapObj, CMapObjGroup* mapObjGroup); + + /* 0004 */ int32_t m_refCount; + /* 0008 */ CMapObj* m_mapObj; + /* 000C */ CMapObjGroup* m_mapObjGroup; + /* 0010 */ CGxBatch* m_batch; + /* 0014 */ int32_t unk_0014; + /* 0018 */ int32_t unk_0018; + /* 001C */ int32_t unk_001C; + /* 0020 */ float unk_0020; + /* 0024 */ int32_t unk_0024; + /* 0028 */ TSGrowableArray m_sharedVerts[2]; + + virtual ~CMeshGeomFactory(); + virtual void Release(); + virtual void GetBuffers(EGxVertexBufferFormat format, CGxBuf** vertexBuf, CGxBuf** indexBuf, CGxBatch* batch); + virtual void Purge(); + virtual bool SetTransform(C44Matrix* transform); + + CMeshGeomFactory(); + }; + + extern CDataAllocator s_unk_D31960; + extern CDataAllocator s_unk_D31974; + + void ClearGeomFactories(); +} + +#endif diff --git a/WoW/Source/WorldClient/LiquidWaveManager.cpp b/WoW/Source/WorldClient/LiquidWaveManager.cpp new file mode 100644 index 0000000..67c5109 --- /dev/null +++ b/WoW/Source/WorldClient/LiquidWaveManager.cpp @@ -0,0 +1,169 @@ +#include "LiquidWaveManager.h" +#include +#include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/FrameTime.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include +#include +#include "Engine/Source/Storm/H/storm.h" +#include "WoW/Source/LiquidSystem/Liquid.h" + +// STATIC: 0x00D31A30 +CDataAllocator Liquid::s_unk_D31A30(0x128, 0x10); + +// STATIC: 0x00AF1690 +float Liquid::CWaveManager::s_fadeOutTime = 5000.0f; + +// STATIC: 0x00AF1694 +float Liquid::CWaveManager::s_fadeInTime = 5000.0f; + +// VERIFY: ba8e6430 manual +// OFFSET: 0x007D62A0 +void Liquid::CWaveManager::PrepareRenderTransform(const C3Vector* camPos) { + static int32_t s_lastFrame; + + if (s_lastFrame == CWorld::frameCnt) { + return; + } + + s_lastFrame = CWorld::frameCnt; + + static uint32_t s_lastTime = FrameTime::s_curTimeMs; + uint32_t elapsed = FrameTime::s_curTimeMs - s_lastTime; + s_lastTime = FrameTime::s_curTimeMs; + + for (uint32_t i = 0; i < 3; i++) { + float time = elapsed + this->m_time[i]; + this->m_time[i] = time; + + float fadeIn = time / Liquid::CWaveManager::s_fadeInTime; + + if (fadeIn > 1.0f) { + fadeIn = 1.0f; + } + + float fadeOut; + + if (time >= this->m_duration[i]) { + fadeOut = 0.0f; + } else { + fadeOut = (this->m_duration[i] - time) / Liquid::CWaveManager::s_fadeOutTime; + + if (fadeOut > 1.0f) { + fadeOut = 1.0f; + } + } + + float fade = fadeOut <= fadeIn ? fadeOut : fadeIn; + + if (fade < 0.0f) { + fade = 0.0f; + } + + this->m_data.m_waves[i].m_amplitude = fade * this->m_waves[i].m_amplitude; + + if (this->m_time[i] >= this->m_duration[i]) { + SWave& wave = this->m_waves[i]; + + this->m_time[i] = 0.0f; + this->m_duration[i] = static_cast(rand() % 20000 + 20000); + + C3Vector* target = CWorldScene::GetCamTarget(); + float toTargetX = target->x - camPos->x; + float toTargetY = target->y - camPos->y; + float toTargetZ = target->z - camPos->z; + float lengthSq = toTargetZ * toTargetZ + toTargetX * toTargetX + toTargetY * toTargetY; + + if (lengthSq > 0.00000023841858f) { + float inv = 1.0f / sqrtf(lengthSq); + toTargetX = toTargetX * inv; + toTargetY = inv * toTargetY; + } + + float distX = (rand() % 50 + 150.0f) * toTargetX; + float x = (rand() % 100 - 50) + camPos->x + distX; + float distY = (rand() % 50 + 150.0f) * toTargetY; + float y = (rand() % 100 - 50) + distY + camPos->y; + wave.m_x = x; + wave.m_y = y; + + float dirX = camPos->x - x; + float dirY = camPos->y - y; + float dirLengthSq = dirX * dirX + dirY * dirY; + + if (dirLengthSq > 0.00000023841858f) { + float inv = 1.0f / sqrtf(dirLengthSq); + dirX = dirX * inv; + dirY = dirY * inv; + } + + C44Matrix rotation; + rotation.RotateAroundZ((rand() % 2000 - 1000.0f) * 0.00078539818f); + C3Vector dir = C3Vector(dirX, dirY, 0.0f) * rotation; + wave.m_dirX = dir.x; + wave.m_dirY = dir.y; + + wave.m_speed = (rand() % 1000) * 0.039999999f + 10.0f; + wave.m_length = (rand() % 1000) * 0.2f + 200.0f; + wave.m_amplitude = (rand() % 1000) * 0.00050000002f + 0.5f; + wave.unk_001C = (rand() % 1000) * 0.000030000001f + 0.02f; + + this->m_data.m_waves[i] = wave; + this->m_data.m_waves[i].m_amplitude = 0.0f; + } + } +} + +// VERIFY: a171683e auto +// OFFSET: 0x007D6670 +void Liquid::ClearWaveManagers() { + Liquid::s_unk_D31A30.Clear(".?AVCWaveManager@Liquid@@", SLOG_OBJECT, 1); +} + +// VERIFY: 845a53cd auto +// OFFSET: 0x00530B70 +void Liquid::CWaveManager::AddRef() { + this->m_refCount++; +} + +// VERIFY: bd870ef6 auto +// OFFSET: 0x007D6200 +int32_t Liquid::CWaveManager::GetCount() { + return 42; +} + +// VERIFY: 0b2c6ff5 manual +// OFFSET: 0x007D6210 +void Liquid::CWaveManager::Release() { + if (this->m_refCount-- == 1) { + this->~CWaveManager(); + Liquid::s_unk_D31A30.PutData(this, nullptr, 0); + } +} + +// VERIFY: e6866435 manual +// OFFSET: 0x007D6240 +Liquid::CWaveManager::CWaveManager() { + this->m_refCount = 1; + memset(&this->m_data, 0, sizeof(this->m_data)); + + for (uint32_t i = 0; i < 3; i++) { + this->m_time[i] = 0.0f; + this->m_duration[i] = 0.0f; + } + + memset(this->m_waves, 0, sizeof(this->m_waves)); +} + +// VERIFY: 66640799 auto +// OFFSET: 0x007D6640 +Liquid::CWaveManager* Liquid::CWaveManager::Create() { + void* mem = Liquid::s_unk_D31A30.GetData(0, ".?AVCWaveManager@Liquid@@", SLOG_OBJECT); + return mem ? new (mem) Liquid::CWaveManager() : nullptr; +} + +// VERIFY: 79404128 auto +// OFFSET: 0x008C6C80 +void* Liquid::CWaveManager::GetData() { + return &this->m_data; +} diff --git a/WoW/Source/WorldClient/LiquidWaveManager.h b/WoW/Source/WorldClient/LiquidWaveManager.h new file mode 100644 index 0000000..c68c801 --- /dev/null +++ b/WoW/Source/WorldClient/LiquidWaveManager.h @@ -0,0 +1,54 @@ +#ifndef WOW_SOURCE_WORLDCLIENT_LIQUIDWAVEMANAGER_H +#define WOW_SOURCE_WORLDCLIENT_LIQUIDWAVEMANAGER_H + +#include +#include +#include "Engine/Source/Base/CDataAllocator.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "WoW/Source/LiquidSystem/Liquid.h" + +namespace Liquid { + struct SWave { + /* 0000 */ float m_x; + /* 0004 */ float m_y; + /* 0008 */ float m_dirX; + /* 000C */ float m_dirY; + /* 0010 */ float m_speed; + /* 0014 */ float m_length; + /* 0018 */ float m_amplitude; + /* 001C */ float unk_001C; + }; + + struct SWaveData { + /* 0000 */ uint8_t unk_0000[72]; + /* 0048 */ SWave m_waves[3]; + }; + + class CWaveManager : public IWaveManager { + public: + static float s_fadeOutTime; + static float s_fadeInTime; + + static CWaveManager* Create(); + + /* 0004 */ int32_t m_refCount; + /* 0008 */ SWaveData m_data; + /* 00B0 */ float m_time[3]; + /* 00BC */ float m_duration[3]; + /* 00C8 */ SWave m_waves[3]; + + virtual void AddRef(); + virtual void Release(); + virtual void* GetData(); + virtual int32_t GetCount(); + + CWaveManager(); + void PrepareRenderTransform(const C3Vector* camPos); + }; + + extern CDataAllocator s_unk_D31A30; + + void ClearWaveManagers(); +} + +#endif diff --git a/WoW/Source/WorldClient/Map.cpp b/WoW/Source/WorldClient/Map.cpp index ae114ad..1adf8f8 100644 --- a/WoW/Source/WorldClient/Map.cpp +++ b/WoW/Source/WorldClient/Map.cpp @@ -1,4 +1,8 @@ #include "Map.h" +#include "Engine/Source/Services/AsyncFile.h" +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "Engine/Source/DayNight/DayNight.h" +#include "WoW/Source/WorldClient/MapLowDetail.h" #include #include "Engine/Source/Base/Handle.h" #include "Engine/Source/Base/ObjectAlloc.h" @@ -8,15 +12,18 @@ #include "Engine/Source/Gx/CGxPool.h" #include "Engine/Source/Gx/CGxTexFlags.h" #include "Engine/Source/Os/Processor.h" +#include "Engine/Source/Services/SysMessage.h" #include "Engine/Source/Services/Texture.h" #include "Engine/Source/ShaderEffect/ShaderEffect.h" #include "Engine/Source/Storm/H/storm.h" #include "WoW/Source/DB/DBClient/DBClient.h" +#include "WoW/Source/DB/Rec/LiquidMaterialRec.h" +#include "WoW/Source/DB/Rec/LiquidTypeRec.h" #include "WoW/Source/WorldClient/CChunkLiquid.h" #include "WoW/Source/WorldClient/CMapBaseObjLink.h" #include "WoW/Source/WorldClient/DetailDoodad.h" #include "WoW/Source/WorldClient/FVBBList.h" -#include "WoW/Source/WorldClient/Liquid.h" +#include "WoW/Source/LiquidSystem/Liquid.h" #include "WoW/Source/WorldClient/MapArea.h" #include "WoW/Source/WorldClient/MapChunk.h" #include "WoW/Source/WorldClient/MapDoodadDef.h" @@ -26,6 +33,45 @@ #include "WoW/Source/WorldClient/MapObjDef.h" #include "WoW/Source/WorldClient/MapObjGroup.h" #include "WoW/Source/WorldClient/WaterRipple.h" +#include +#include "Engine/Source/Base/RCString.h" +#include "WoW/Source/WorldClient/World.h" +#include "Engine/Source/Model2/M2Scene.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Tempest/CMath.h" +#include "Engine/Source/Tempest/C2Vector.h" +#include "Engine/Source/Tempest/C33Matrix.h" +#include "Engine/Source/Tempest/C3Segment.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/C4Plane.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "Engine/Source/Tempest/CFacet.h" +#include "Engine/Source/Tempest/CFrustum.h" +#include "Engine/Source/Gx/CGxVertexPC.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "Engine/Source/Model2/M2Model.h" +#include "WoW/Source/WorldClient/Types.h" +#include "WoW/Source/WorldClient/MapBaseObj.h" +#include "WoW/Source/WorldClient/MapRenderChunk.h" +#include "WoW/Source/WorldClient/MapLoad.h" +#include "WoW/Source/WorldClient/CWorldMath.h" +#include "Engine/Source/Tempest/CiRect.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "WoW/Source/WorldClient/FrameTime.h" +#include "Engine/Source/Tempest/C34Matrix.h" +#include "Engine/Source/Model2/M2Light.h" +#include "WoW/Source/Object/WGUID.h" +#include "Engine/Source/Gxu/GxuLight.h" + +// STATIC: 0x00CF4938 +TSGrowableArray CMap::debugVertexArray; + +// STATIC: 0x00CF4948 +TSGrowableArray CMap::debugIndexArray; + +// STATIC: 0x00CE04A8 +CMapLight* CMap::s_mapLight; // STATIC: 0x00ADF800 float CMap::s_unk_ADF800 = 0.58333333f; @@ -42,6 +88,39 @@ TSExplicitList CM // STATIC: 0x00AEED80 TSExplicitList CMap::s_unk_AEED80; +// STATIC: 0x00AEED8C +TSList > CMap::s_unk_AEED8C = TSExplicitList(); + +// STATIC: 0x00AEEDA4 +TSList > CMap::s_mapChunkList = TSExplicitList(); + +// STATIC: 0x00AEEDBC +TSList > CMap::mapObjDefGroupList = TSExplicitList(); + +// STATIC: 0x00AEEDC8 +TSList > CMap::entityList = TSExplicitList(); + +// STATIC: 0x00AEEDD4 +TSList > CMap::lightList = TSExplicitList(); + +// STATIC: 0x00ADFBEC +TSList > CMap::s_unk_ADFBEC = TSExplicitList(); + +// STATIC: 0x00ADFC28 +TSExplicitList CMap::s_mapRenderChunkUpdateList; + +// STATIC: 0x00CE049D +bool CMap::enableSpecularTerrain; + +// STATIC: 0x00CE05D0 +char CMap::wdtFilename[256]; + +// STATIC: 0x00CE06D0 +char CMap::mapName[256]; + +// STATIC: 0x00CE07D0 +char CMap::mapPath[256]; + // STATIC: 0x00CDF7D0 float CMap::s_unk_CDF7D0[256]; @@ -85,10 +164,10 @@ int32_t CMap::oldSelectLightParm; uint32_t CMap::s_queryTag; // STATIC: 0x00CE04C8 -int32_t CMap::cCount; +uint32_t CMap::cCount; // STATIC: 0x00CE08D0 -uint32_t CMap::unk[4096]; +CMapAreaMed* CMap::unk[4096]; // STATIC: 0x00CE48D0 CMapArea* CMap::m_areaTable[4096]; @@ -168,6 +247,100 @@ uint32_t* CMap::mapObjDefHeap; // STATIC: 0x00D25430 uint32_t* CMap::chunkLiquidHeap; +// STATIC: 0x00ADFC10 +TSList > CMap::doodadDefList = TSExplicitList(); + +// STATIC: 0x00ADFC64 +C3Vector CMap::s_subchunkCoords[5] = { C3Vector(0.0f, 0.0f, 0.0f), C3Vector(0.0f, -4.1666665f, 0.0f), C3Vector(-4.1666665f, -4.1666665f, 0.0f), C3Vector(-4.1666665f, 0.0f, 0.0f), C3Vector(-2.0833333f, -2.0833333f, 0.0f) }; + +// STATIC: 0x00A3FAF0 +uint32_t CMap::s_holeMask[16] = { 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000 }; + +// STATIC: 0x00A3FB30 +int32_t CMap::s_fanIndices[8] = { 17, 0, 0, 1, 18, 17, 1, 18 }; + +// STATIC: 0x00A3FB50 +int32_t CMap::s_subchunkIndices[8] = { 3, 0, 0, 1, 2, 3, 1, 2 }; + +// STATIC: 0x00D25434 +TSHashTable CMap::mapObjDefHash; + +// STATIC: 0x00CE0498 +int32_t CMap::enableChunkBatching; + +// STATIC: 0x00ADFBB4 +DepthCoordTable* CMap::s_depthCoordTables[2] = { &CMap::s_unk_CDF7D0, &CMap::s_unk_CDFBD0 }; + +// STATIC: 0x00AEEDB0 +TSList > CMap::s_chunkLiquidList = TSExplicitList(); + +// STATIC: 0x00ADFC34 +TSList > CMap::s_mapChunkLiquidUpdateList = TSExplicitList(); + +// STATIC: 0x00CF0900 +CMapLowDetail CMap::s_lowDetail; + +// STATIC: 0x00CF08D0 +SMMapHeader CMap::header; + +// STATIC: 0x00CE04CC +uint32_t CMap::version; + +// STATIC: 0x00ADFBC8 +int32_t CMap::bPreload = 1; + +// STATIC: 0x00CE0494 +int32_t CMap::s_isStreamingMode; + +// STATIC: 0x00CE049C +bool CMap::s_unk_CE049C; + +// STATIC: 0x00CE04A0 +bool CMap::enableSpecular; + +// STATIC: 0x00CE04A1 +bool CMap::enablePixelShaders; + +// STATIC: 0x00CE04A2 +bool CMap::enableVertexShaders; + +// STATIC: 0x00CE049E +bool CMap::gTerrainPixelShadersValid; + +// STATIC: 0x00CE049F +bool CMap::enableTerrainShaderVertex; + +// STATIC: 0x00CE04A3 +static HASHKEY_NONE s_mapObjDefHashKey; + +// STATIC: 0x00ADFC04 +TSList > CMap::mapObjDefLinkList = TSExplicitList(); + +// STATIC: 0x00AF16CC +TSList > CMap::s_unk_AF16CC = TSExplicitList(); + +// VERIFY: ef54a4a5 auto +// OFFSET: 0x0079B870 +DepthCoordTable* CMap::GetDepthCoordTable(uint32_t liquidType) { + if (!liquidType) { + return nullptr; + } + + LiquidTypeRec* typeRec = g_liquidTypeDB.GetRecord(liquidType); + + if (!typeRec) { + return nullptr; + } + + LiquidMaterialRec* materialRec = g_liquidMaterialDB.GetRecord(typeRec->m_materialID); + + if (!materialRec || (materialRec->m_lvf != 0 && materialRec->m_lvf != 2)) { + return nullptr; + } + + return CMap::s_depthCoordTables[typeRec->m_int[0]]; +} + // VERIFY: 6c4a26d2 manual // OFFSET: 0x0079F320 void CMap::Destroy() { @@ -486,3 +659,1860 @@ void CMap::FootprintInitialize() { } } } + + +// VERIFY: 7ce5c762 manual +// OFFSET: 0x0079E4B0 +CGxShader* CMap::GetPixelShader(bool a1, bool a2, bool a3) { + if (!a1) + return CMap::pixelShader_Terrain0[0]; + if (!a2) + return CMap::pixelShader_Terrain0[2]; + if (a3) + return CMap::pixelShader_Terrain0_env; + return CMap::pixelShader_Terrain0[1]; +} + +// VERIFY: cdb37c44 manual +// OFFSET: 0x007A0820 +bool CMap::QueryLiquidTerrain(const C3Vector* pos, int32_t* liquidType, float* height, int32_t checkGround) { + float cellYf = -(pos->y - 17066.666f) * 0.24f; + float cellXf = 0.24f * -(pos->x - 17066.666f); + int32_t cellX = CMath::fint_(cellYf - 0.5f); + int32_t cellY = CMath::fint_(cellXf - 0.5f); + + CMapArea* area = CMap::m_areaTable[64 * ((cellY >> 7) & 0x3F) + ((cellX >> 7) & 0x3F)]; + + if (!area || area->m_asyncObject) { + return false; + } + + CMapChunk* chunk = area->m_mapChunks[((cellX >> 3) & 0xF) + 16 * ((cellY >> 3) & 0xF)]; + + if (!chunk) { + return false; + } + + C2iVector tile; + tile.x = cellX & 7; + tile.y = cellY & 7; + + C2Vector frac(cellYf - cellX, cellXf - cellY); + + for (CChunkLiquid* liquid = chunk->m_liquidChunkLinkList.Head(); liquid; liquid = chunk->m_liquidChunkLinkList.Next(liquid)) { + if (liquid->TileExists(tile.x, tile.y) && liquid->GetHeight(frac, tile, *height) && *height + 0.0099999998f > pos->z) { + if (!checkGround) { + *liquidType = liquid->m_type; + return true; + } + + float ground = -10000.0f; + CMap::GetHeightTerrain(chunk, *const_cast(pos), cellX, cellY, &ground); + + if (pos->z + 0.0099999998f > ground) { + *liquidType = liquid->m_type; + return true; + } + } + } + + return false; +} + +// OFFSET: 0x007A2070 +static int32_t CompareIntersectCandidates(const void* a, const void* b) { + const MapObjIntersectData* ca = static_cast(a); + const MapObjIntersectData* cb = static_cast(b); + + if (cb->t > ca->t) + return -1; + + if (cb->t < ca->t) + return 1; + + return 0; +} + +// VERIFY: 85e5829e manual +// OFFSET: 0x007A20E0 +void CMap::VectorIntersectSX(CiRect& rect) { + int32_t x = rect.minX; + + if (x <= rect.maxX) { + do { + CMap::scCollideList.m_data[CMap::cCount++] = x; + CMap::scCollideList.m_data[CMap::cCount++] = rect.minY; + x++; + } while (x <= rect.maxX); + } else { + do { + CMap::scCollideList.m_data[CMap::cCount++] = x; + CMap::scCollideList.m_data[CMap::cCount++] = rect.minY; + x--; + } while (x >= rect.maxX); + } +} + +// VERIFY: 8a929a73 manual +// OFFSET: 0x007A2180 +void CMap::VectorIntersectSY(CiRect& rect) { + int32_t y = rect.minY; + + if (y <= rect.maxY) { + do { + CMap::scCollideList.m_data[CMap::cCount++] = rect.minX; + CMap::scCollideList.m_data[CMap::cCount++] = y; + y++; + } while (y <= rect.maxY); + } else { + do { + CMap::scCollideList.m_data[CMap::cCount++] = rect.minX; + CMap::scCollideList.m_data[CMap::cCount++] = y; + y--; + } while (y >= rect.maxY); + } +} + +// VERIFY: 6d1fef26 manual +// OFFSET: 0x007A2230 +void CMap::VectorIntersectDX(C3Vector& start, C3Vector& end, CiRect& cells) { + int32_t y = cells.minY; + float slope = (end.y - start.y) / (end.x - start.x); + int32_t x = cells.minX; + float intercept = start.y - start.x * slope; + + int32_t next; + int32_t step; + + if (cells.maxX <= x) { + next = cells.minX; + step = -1; + } else { + next = x + 1; + step = 1; + } + + float edge = next * 4.1666665f; + + CMap::scCollideList.m_data[CMap::cCount++] = x; + CMap::scCollideList.m_data[CMap::cCount++] = y; + + while (x != cells.maxX + step) { + if (CMap::cCount >= 0x7FC) { + return; + } + + int32_t edgeY = CMath::fint_((slope * edge + intercept) * 0.24f - 0.5f); + + if (edgeY != y) { + CMap::scCollideList.m_data[CMap::cCount++] = x; + CMap::scCollideList.m_data[CMap::cCount++] = edgeY; + } + + x += step; + edge = step * 4.1666665f + edge; + + CMap::scCollideList.m_data[CMap::cCount++] = x; + CMap::scCollideList.m_data[CMap::cCount++] = edgeY; + + y = edgeY; + } + + if (y != cells.maxY) { + CMap::scCollideList.m_data[CMap::cCount++] = cells.maxX; + CMap::scCollideList.m_data[CMap::cCount++] = cells.maxY; + } +} + +// VERIFY: 6264b08b manual +// OFFSET: 0x007A23E0 +void CMap::VectorIntersectDY(C3Vector& start, C3Vector& end, CiRect& cells) { + float slope = (end.y - start.y) / (end.x - start.x); + int32_t x = cells.minX; + int32_t y = cells.minY; + float intercept = start.y - start.x * slope; + float invSlope = 1.0f / slope; + + int32_t next; + int32_t step; + + if (cells.maxY <= cells.minY) { + next = cells.minY; + step = -1; + } else { + next = y + 1; + step = 1; + } + + float edge = next * 4.1666665f; + + CMap::scCollideList.m_data[CMap::cCount++] = cells.minX; + CMap::scCollideList.m_data[CMap::cCount++] = cells.minY; + + while (y != cells.maxY + step) { + if (CMap::cCount >= 0x7FC) { + return; + } + + int32_t edgeX = CMath::fint_((edge - intercept) * invSlope * 0.24f - 0.5f); + + if (edgeX != x) { + CMap::scCollideList.m_data[CMap::cCount++] = edgeX; + CMap::scCollideList.m_data[CMap::cCount++] = y; + } + + CMap::scCollideList.m_data[CMap::cCount++] = edgeX; + edge = step * 4.1666665f + edge; + y += step; + CMap::scCollideList.m_data[CMap::cCount++] = y; + + x = edgeX; + } + + if (x != cells.maxX) { + CMap::scCollideList.m_data[CMap::cCount++] = cells.maxX; + CMap::scCollideList.m_data[CMap::cCount++] = cells.maxY; + } +} + + +// VERIFY: 2f69e7ee auto +// OFFSET: 0x007A39F0 +bool CMap::VectorIntersectTerrain(C3Vector* start, C3Vector* end, float* distance, uint32_t flags, CMapChunk** hitChunk) { + float startX = -(start->y - 17066.666f); + float startY = -(start->x - 17066.666f); + float endX = -(end->y - 17066.666f); + float endY = -(end->x - 17066.666f); + + float dX = endX - startX; + float dY = endY - startY; + + CiRect cells; + cells.maxY = CMath::fint_(endY * 0.24f - 0.5f); + cells.minX = CMath::fint_(startX * 0.24f - 0.5f); + cells.minY = CMath::fint_(startY * 0.24f - 0.5f); + cells.maxX = CMath::fint_(endX * 0.24f - 0.5f); + + CMap::cCount = 0; + + if (fabsf(dX) < 0.00000023841858f || cells.minX == cells.maxX) { + CMap::VectorIntersectSY(cells); + } else if (fabsf(dY) < 0.00000023841858f || cells.minY == cells.maxY) { + CMap::VectorIntersectSX(cells); + } else { + C3Vector e(endX, endY, 0.0f); + C3Vector s(startX, startY, 0.0f); + + if (fabsf(dY) >= fabsf(dX)) { + CMap::VectorIntersectDY(s, e, cells); + } else { + CMap::VectorIntersectDX(s, e, cells); + } + } + + return CMap::VectorIntersectSubChunkList(start, end, distance, flags, hitChunk); +} + + +// VERIFY: 35b33716 manual +// OFFSET: 0x007A4270 +CFacet* CMap::BuildImpassableFacets(World::FacetData* facets, C3Vector* up, C3Vector* edge, C3Vector* normal, C3Vector* origin) { + float d = -(normal->z * origin->z + normal->x * origin->x + normal->y * origin->y); + + CFacet* facet = facets->facets.New(); + facet->plane.n = *normal; + facet->plane.d = d; + facet->v[0] = *origin; + facet->v[1] = { origin->x + edge->x, origin->y + edge->y, origin->z + edge->z }; + facet->v[2] = { origin->x + edge->x + up->x, origin->y + edge->y + up->y, origin->z + edge->z + up->z }; + + facet = facets->facets.New(); + facet->plane.n = *normal; + facet->plane.d = d; + facet->v[0] = *origin; + facet->v[1] = { origin->x + edge->x + up->x, origin->y + edge->y + up->y, origin->z + edge->z + up->z }; + facet->v[2] = { origin->x + up->x, origin->y + up->y, origin->z + up->z }; + + return facet; +} + +// VERIFY: f65ff37c manual +// OFFSET: 0x007A43D0 +void CMap::CreateImpassableFacets(CMapChunk* chunk, CAaBox* box, World::FacetData* facets) { + C3Vector up = { 0.0f, 0.0f, 32000.0f }; + C3Vector origin = { 0.0f, 0.0f, 0.0f }; + C3Vector normal = { 0.0f, 0.0f, 0.0f }; + C3Vector edge = { 0.0f, 0.0f, 0.0f }; + + if (chunk->m_bbox.b.y > box->b.y) { + origin = { chunk->m_bbox.b.x, chunk->m_bbox.b.y, chunk->m_bbox.b.z }; + normal = { 0.0f, -1.0f, 0.0f }; + edge = { chunk->m_bbox.t.x - chunk->m_bbox.b.x, 0.0f, 0.0f }; + CMap::BuildImpassableFacets(facets, &up, &edge, &normal, &origin); + } + + if (chunk->m_bbox.t.y < box->t.y) { + origin = { chunk->m_bbox.t.x, chunk->m_bbox.t.y, chunk->m_bbox.b.z }; + normal = { 0.0f, 1.0f, 0.0f }; + edge = { chunk->m_bbox.b.x - chunk->m_bbox.t.x, 0.0f, 0.0f }; + CMap::BuildImpassableFacets(facets, &up, &edge, &normal, &origin); + } + + if (chunk->m_bbox.b.x > box->b.x) { + origin = { chunk->m_bbox.b.x, chunk->m_bbox.t.y, chunk->m_bbox.b.z }; + normal = { -1.0f, 0.0f, 0.0f }; + edge = { 0.0f, chunk->m_bbox.b.y - chunk->m_bbox.t.y, 0.0f }; + CMap::BuildImpassableFacets(facets, &up, &edge, &normal, &origin); + } + + if (chunk->m_bbox.t.x < box->t.x) { + origin = { chunk->m_bbox.t.x, chunk->m_bbox.b.y, chunk->m_bbox.b.z }; + normal = { 1.0f, 0.0f, 0.0f }; + edge = { 0.0f, chunk->m_bbox.t.y - chunk->m_bbox.b.y, 0.0f }; + CMap::BuildImpassableFacets(facets, &up, &edge, &normal, &origin); + } +} + + +// VERIFY: e45b1149 manual +// OFFSET: 0x007A4AF0 +void CMap::AppendMapObjFacets(CMapDoodadDef* def, CAaBox* box, World::FacetData* facets) { + uint32_t first = facets->facets.Count(); + + def->m_model->GetCollisionFacets(*box, def->m_mat, facets->facets); + + uint32_t count = facets->facets.Count(); + facets->facetIds.SetCount(count); + + for (uint32_t i = first; i < count; i++) { + facets->facetIds.m_data[i] = def->unk_00B8; + } +} + +// VERIFY: de8ab0e3 manual +// OFFSET: 0x007AD3B0 +bool CMap::GetHeightTerrain(CMapChunk* chunk, C3Vector& pos, int32_t cellX, int32_t cellY, float* outHeight) { + int32_t col = cellX & 7; + int32_t row = cellY & 7; + + if (chunk->m_header->m_holes & CMap::s_holeMask[4 * (row >> 1) + (col >> 1)]) { + return false; + } + + float originX = row * -4.1666665f; + float originY = -4.1666665f * col; + + float dx = pos.x - chunk->m_topLeftCoords.x; + float dy = pos.y - chunk->m_topLeftCoords.y; + + int32_t fan = 0; + + if (((originX + CMap::s_subchunkCoords[2].x) - (originX + CMap::s_subchunkCoords[0].x)) * ((originY + CMap::s_subchunkCoords[2].y) - dy) - ((originY + CMap::s_subchunkCoords[2].y) - (originY + CMap::s_subchunkCoords[0].y)) * ((originX + CMap::s_subchunkCoords[2].x) - dx) <= 0.0f) { + fan = 1; + } + + if (((originX + CMap::s_subchunkCoords[3].x) - (originX + CMap::s_subchunkCoords[1].x)) * ((originY + CMap::s_subchunkCoords[3].y) - dy) - ((originY + CMap::s_subchunkCoords[3].y) - (originY + CMap::s_subchunkCoords[1].y)) * ((originX + CMap::s_subchunkCoords[3].x) - dx) <= 0.0f) { + fan += 2; + } + + float* height = &chunk->m_height[row + col + 16 * row]; + + C3Vector a(originX + CMap::s_subchunkCoords[CMap::s_subchunkIndices[2 * fan]].x, originY + CMap::s_subchunkCoords[CMap::s_subchunkIndices[2 * fan]].y, height[CMap::s_fanIndices[2 * fan]]); + C3Vector b(originX + CMap::s_subchunkCoords[CMap::s_subchunkIndices[2 * fan + 1]].x, originY + CMap::s_subchunkCoords[CMap::s_subchunkIndices[2 * fan + 1]].y, height[CMap::s_fanIndices[2 * fan + 1]]); + C3Vector c(originX + CMap::s_subchunkCoords[4].x, originY + CMap::s_subchunkCoords[4].y, height[9]); + + if (CMap::s_useSSE) { + float ax = a.x - c.x; + float ay = a.y - c.y; + float az = a.z - c.z; + float bx = b.x - c.x; + float by = b.y - c.y; + float bz = b.z - c.z; + + float nx = bz * ay - az * by; + float ny = az * bx - bz * ax; + float nz = ax * by - ay * bx; + + float scale = 1.0f / sqrtf(ny * ny + nz * nz + 0.0f * 0.0f + nx * nx); + + *outHeight = chunk->m_topLeftCoords.z - (ny * scale * dy + nx * scale * dx - (c.z * (nz * scale) + c.y * (ny * scale) + c.x * (nx * scale))) / (nz * scale); + + return true; + } + + C4Plane plane; + plane.From3Pos(c, a, b); + + *outHeight = chunk->m_topLeftCoords.z - (plane.n.y * dy + plane.n.x * dx + plane.d) / plane.n.z; + + return true; +} + +// VERIFY: 468f4c9b manual +// OFFSET: 0x007B4090 +void CMapObjDefGroup::UpdateLights() { + for (CMapBaseObjLink* link = this->m_doodadDefLinkList.Head(); link; link = this->m_doodadDefLinkList.Next(link)) { + link->m_owner->m_flags = static_cast(link->m_owner->m_flags | 0x1); + } + + for (CMapBaseObjLink* link = this->m_entityLinkList.Head(); link; link = this->m_entityLinkList.Next(link)) { + link->m_owner->m_flags = static_cast(link->m_owner->m_flags | 0x1); + } +} + +// VERIFY: 9eee5402 auto +// OFFSET: 0x007B4DF0 +void CMap::UpdateArea(int32_t addToScene, CMapArea* area, CiRect* chunkRect, int32_t depth) { + if (chunkRect->minX > CWorld::gbChunkRect.maxX || chunkRect->minY > CWorld::gbChunkRect.maxY || chunkRect->maxX < CWorld::gbChunkRect.minX || chunkRect->maxY < CWorld::gbChunkRect.minY) { + area->PurgeChunks(chunkRect); + } else if (depth == 2) { + area->Update(addToScene, chunkRect); + } else { + CiRect rect; + rect.minY = chunkRect->minY; + rect.minX = chunkRect->minX; + rect.maxY = chunkRect->minY + ((chunkRect->maxY - chunkRect->minY) >> 1); + rect.maxX = chunkRect->minX + ((chunkRect->maxX - chunkRect->minX) >> 1); + CMap::UpdateArea(addToScene, area, &rect, depth + 1); + + rect.minX = ((chunkRect->maxX - chunkRect->minX) >> 1) + chunkRect->minX + 1; + rect.maxX = chunkRect->maxX; + CMap::UpdateArea(addToScene, area, &rect, depth + 1); + + rect.minY = ((chunkRect->maxY - chunkRect->minY) >> 1) + chunkRect->minY + 1; + rect.maxY = chunkRect->maxY; + CMap::UpdateArea(addToScene, area, &rect, depth + 1); + + rect.minX = chunkRect->minX; + rect.maxX = chunkRect->minX + ((chunkRect->maxX - chunkRect->minX) >> 1); + CMap::UpdateArea(addToScene, area, &rect, depth + 1); + } +} + +// VERIFY: fa65766b manual +// OFFSET: 0x007B5420 +void CMap::ProcessChunkLiquidUpdateList() { + CChunkLiquid* liquid = CMap::s_mapChunkLiquidUpdateList.Head(); + + while (liquid) { + CChunkLiquid* next = CMap::s_mapChunkLiquidUpdateList.Next(liquid); + liquid->UpdateResourcePurging(); + + if (liquid->m_resPurgeTimer < 0.0f) { + liquid->m_updateLink.Unlink(); + } + + liquid = next; + } +} + +// VERIFY: 21d0bf10 auto +// OFFSET: 0x007B5500 +void CMap::ProcessRenderChunkUpdateList() { + CMapRenderChunk* renderChunk = CMap::s_mapRenderChunkUpdateList.Head(); + + while (renderChunk) { + renderChunk->m_lastUpdateTime = FrameTime::s_tickTimeSec + renderChunk->m_lastUpdateTime; + CMapRenderChunk* next = CMap::s_mapRenderChunkUpdateList.Next(renderChunk); + + if (renderChunk->m_lastUpdateTime > 2.0f) { + renderChunk->FreeBuf(); + } + + if (!renderChunk->m_chunkBuf) { + renderChunk->m_renderChunkLink.Unlink(); + } + + renderChunk = next; + } +} + +// VERIFY: a5870e86 manual +// OFFSET: 0x007B5590 +void CMap::PrepareEntitys(int32_t addToScene) { + if (!addToScene) { + return; + } + + for (CMapEntity* entity = CMap::entityList.Head(); entity; entity = CMap::entityList.Next(entity)) { + if (!(entity->unk_007C & 0x4)) { + CWorldScene::AddEntityToSortTable(entity); + } + } +} + +// VERIFY: c3245130 manual +// OFFSET: 0x007B5630 +void CMap::PrepareMapDoodadDefs() { + int32_t streaming = SFile::IsStreamingMode(); + CMapDoodadDef* doodadDef = CMap::doodadDefList.Head(); + + while (doodadDef) { + CM2Model* model = doodadDef->m_model; + CMapDoodadDef* next = CMap::doodadDefList.Next(doodadDef); + + if (!model || model->IsLoaded(0, 0)) { + if (!(doodadDef->unk_007C & 0x10)) { + doodadDef->UpdateBounds(); + + if (!(doodadDef->m_flags & MAPOBJ_FLAG_INTERIOR) && CMap::QueryShadow(&doodadDef->m_position)) { + doodadDef->m_diffuseLightScale = 0.5f; + } + + doodadDef->ExtendChunkBounds(); + doodadDef->m_flags = static_cast(doodadDef->m_flags | MAPOBJ_FLAG_PREPARED | MAPOBJ_FLAG_UNPLACED); + } + + doodadDef->m_doodadDefLink.Unlink(); + } else if (streaming && (doodadDef->unk_007C & 0x10000)) { + doodadDef->m_model->LinkToAsyncHead(1); + doodadDef->unk_007C &= ~0x10000u; + } + + doodadDef = next; + } +} + +// VERIFY: 6cf7464a auto +// OFFSET: 0x007BD480 +int32_t CMap::SafeOpen(const char* fileName, SFileImpl** file) { + int32_t tries = 10; + + while (!SFileOpenFile(fileName, file)) { + DebugLog("CMap::SafeOpen() failed %s\n"); + + if (!--tries) { + SErrDisplayAppFatal("CMap::SafeOpen() failed %s", fileName); + } + } + + return 1; +} + +// VERIFY: 3ace6e8d manual +// OFFSET: 0x007BD4D0 +int32_t CMap::SafeRead(const char* fileName, SFileImpl* file, void* buffer, uint32_t size) { + int32_t tries = 10; + + while (!SFileReadFile(file, buffer, size, nullptr, nullptr, 0)) { + DebugLog("CMap::SafeRead() failed %s\n"); + + if (!--tries) { + DebugLog("CMap::SafeRead() failed %s"); + return 0; + } + } + + return 1; +} + + +// VERIFY: 69b638d4 auto +// OFFSET: 0x007BD540 +void CMap::LoadTex() { + char fileName[256]; + SStrCopy(fileName, CMap::wdtFilename, STORM_MAX_STR); + char* ext = strrchr(fileName, '.'); + SStrCopy(ext, ".tex", STORM_MAX_STR); + TextureLoadBlob(fileName); +} + +// VERIFY: 4c68f31b manual +// OFFSET: 0x007BD8A0 +void CMap::ValidateShaders() { + CMap::enableVertexShaders = CWorld::enables2 & Enable_VertexShader; + CMap::enablePixelShaders = (CWorld::enables & Enable_PixelShaders) != 0; + CMap::enableSpecular = (CWorld::enables & Enable_Specular) && (CWorld::enables & Enable_PixelShaders); + + bool valid = false; + bool flag = (CMap::header.m_flags >> 2) & 1; + CMap::gTerrainPixelShadersValid = false; + CMap::enableSpecularTerrain = false; + + if (CWorld::enables & Enable_PixelShaders) { + CGxShader* specularShader = CMap::GetPixelShader(flag, true, false); + CGxShader* shader = CMap::GetPixelShader(flag, false, false); + valid = specularShader && specularShader->Valid() && shader && shader->Valid(); + CMap::gTerrainPixelShadersValid = valid; + } + + if (CMap::enableSpecular && valid) { + CGxShader* specularShader = CMap::GetPixelShader(flag, true, false); + CGxShader* shader = CMap::GetPixelShader(flag, false, false); + CMap::enableSpecularTerrain = specularShader && specularShader->Valid() && shader && shader->Valid(); + } + + if (CMap::enableVertexShaders) { + CMap::enableTerrainShaderVertex = CMap::gTerrainPixelShadersValid && CMap::vertexShader_Terrain[0] && CMap::vertexShader_Terrain[0]->Valid(); + } + + CMap::enableChunkBatching = 0; + + if (CMap::gTerrainPixelShadersValid) { + CMap::enableChunkBatching = CMap::enableTerrainShaderVertex != 0; + } +} + +// VERIFY: 88a28231 auto +// OFFSET: 0x007BDD70 +void CMapObjDefGroup::MarkPrepared() { + this->m_flags = static_cast(this->m_flags | 0x10); +} + +// VERIFY: 5b940caf manual +// OFFSET: 0x007BDE50 +void CMap::CreateMapObjDefGroups(CMapObj* mapObj, CMapObjDef* mapObjDef) { + mapObjDef->m_defGroups.SetCount(mapObj->m_groupInfoCount); + + for (int32_t i = 0; i < mapObj->m_groupInfoCount; i++) { + CMapObjDefGroup* group = CMap::AllocMapObjDefGroup(); + CMapBaseObjLink* link = CMap::AllocBaseObjLink(group); + link->m_ref = mapObjDef; + mapObjDef->m_mapObjDefGroupLinkList.LinkToTail(link); + mapObjDef->m_defGroups[i] = group; + + CAaBox box; + mapObj->GetGroupBounds(&group->m_sphere, i); + group->m_sphere.c *= mapObjDef->m_mat; + mapObj->GetGroupBounds(&box, i); + CWorldMath::TransformAABox(mapObjDef->m_mat, box, group->m_bbox); + + group->m_groupNum = i; + group->m_ambientColor = mapObjDef->m_argbColor; + group->m_flags = static_cast(0); + group->m_flags = static_cast(group->m_flags | ((mapObj->GetGroupFlags(i) & 0x48) ? MAPOBJ_FLAG_EXTERIOR : MAPOBJ_FLAG_INTERIOR)); + } +} + +// VERIFY: 1fa4bc69 manual +// OFFSET: 0x007BF460 +CMapObjDef* CMap::CreateMapObjDef(char* fileName, SMMapObjDef* mapObjectDef, C3Vector* center, bool cached) { + CMapObjDef* mapObjDef; + + if (cached && (mapObjDef = CMap::mapObjDefHash.Ptr(mapObjectDef->m_uniqueId, s_mapObjDefHashKey)) != nullptr) { + return mapObjDef; + } + + mapObjDef = CMap::AllocMapObjDef(); + + if (cached) { + CMap::mapObjDefHash.Insert(mapObjDef, mapObjectDef->m_uniqueId, s_mapObjDefHashKey); + } + + mapObjDef->m_position.x = -mapObjectDef->m_position.z; + mapObjDef->m_position.y = -mapObjectDef->m_position.x; + mapObjDef->m_position.z = mapObjectDef->m_position.y; + mapObjDef->m_position.x = center->x + mapObjDef->m_position.x; + mapObjDef->m_position.y = mapObjDef->m_position.y + center->y; + mapObjDef->m_position.z = center->z + mapObjDef->m_position.z; + + float rotX = mapObjectDef->m_rotation.z * 0.017453292f; + float rotY = mapObjectDef->m_rotation.x * 0.017453292f; + float rotZ = 0.017453292f * mapObjectDef->m_rotation.y; + + mapObjDef->m_flags = static_cast(0); + mapObjDef->m_nameId = mapObjectDef->m_nameId; + mapObjDef->m_doodadSet = mapObjectDef->m_doodadSet; + mapObjDef->m_nameSet = mapObjectDef->m_nameSet; + mapObjDef->unk_0148 = 0; + mapObjDef->unk_014C = 0; + mapObjDef->m_doodadSetOverrides[0] = 0; + mapObjDef->m_doodadSetOverrides[1] = 0; + mapObjDef->m_doodadSetOverrides[2] = 0; + + mapObjDef->m_mat.d3 = 1.0f; + mapObjDef->m_mat.c2 = 1.0f; + mapObjDef->m_mat.b1 = 1.0f; + mapObjDef->m_mat.a0 = 1.0f; + mapObjDef->m_mat.d2 = 0.0f; + mapObjDef->m_mat.d1 = 0.0f; + mapObjDef->m_mat.d0 = 0.0f; + mapObjDef->m_mat.c3 = 0.0f; + mapObjDef->m_mat.c1 = 0.0f; + mapObjDef->m_mat.c0 = 0.0f; + mapObjDef->m_mat.b3 = 0.0f; + mapObjDef->m_mat.b2 = 0.0f; + mapObjDef->m_mat.b0 = 0.0f; + mapObjDef->m_mat.a3 = 0.0f; + mapObjDef->m_mat.a2 = 0.0f; + mapObjDef->m_mat.a1 = 0.0f; + mapObjDef->m_mat.d0 = mapObjDef->m_position.x; + mapObjDef->m_mat.d1 = mapObjDef->m_position.y; + mapObjDef->m_mat.d2 = mapObjDef->m_position.z; + mapObjDef->m_mat.RotateAroundZ(rotZ + 3.1415927f); + mapObjDef->m_mat.RotateAroundY(rotY); + mapObjDef->m_mat.RotateAroundX(rotX); + mapObjDef->m_invMat = mapObjDef->m_mat.AffineInverse(); + + mapObjDef->m_bbox.b.x = -mapObjectDef->m_extents.t.z; + mapObjDef->m_bbox.b.y = -mapObjectDef->m_extents.t.x; + mapObjDef->m_bbox.b.z = mapObjectDef->m_extents.b.y; + mapObjDef->m_bbox.b.x = center->x + mapObjDef->m_bbox.b.x; + mapObjDef->m_bbox.b.y = mapObjDef->m_bbox.b.y + center->y; + mapObjDef->m_bbox.b.z = center->z + mapObjDef->m_bbox.b.z; + mapObjDef->m_bbox.t.x = -mapObjectDef->m_extents.b.z; + mapObjDef->m_bbox.t.y = -mapObjectDef->m_extents.b.x; + mapObjDef->m_bbox.t.z = mapObjectDef->m_extents.t.y; + mapObjDef->m_bbox.t.x = center->x + mapObjDef->m_bbox.t.x; + mapObjDef->m_bbox.t.y = mapObjDef->m_bbox.t.y + center->y; + mapObjDef->m_bbox.t.z = mapObjDef->m_bbox.t.z + center->z; + + mapObjDef->m_sphere.c.x = (mapObjDef->m_bbox.b.x + mapObjDef->m_bbox.t.x) * 0.5f; + mapObjDef->m_sphere.c.y = (mapObjDef->m_bbox.t.y + mapObjDef->m_bbox.b.y) * 0.5f; + mapObjDef->m_sphere.c.z = 0.5f * (mapObjDef->m_bbox.t.z + mapObjDef->m_bbox.b.z); + float dz = mapObjDef->m_bbox.t.z - mapObjDef->m_sphere.c.z; + float dy = mapObjDef->m_bbox.t.y - mapObjDef->m_sphere.c.y; + float dx = mapObjDef->m_bbox.t.x - mapObjDef->m_sphere.c.x; + mapObjDef->m_sphere.r = sqrtf(dx * dx + dy * dy + dz * dz); + + mapObjDef->m_lightsArray.m_count = 0; + mapObjDef->m_owner = CMapObj::Create(fileName); + + return mapObjDef; +} + +// VERIFY: d875b6f9 manual +// OFFSET: 0x007BF8B0 +void CMap::LoadWdt() { + SFileImpl* file = nullptr; + SFileOpenFile(CMap::wdtFilename, &file); + + if (!file) { + SErrDisplayAppFatal("CMap::LoadWdt() failed %s\n", CMap::wdtFilename); + } + + SIffChunk iffChunk; + iffChunk.m_token = 0; + iffChunk.m_size = 0; + + SFileReadFile(file, &iffChunk, offsetof(SIffChunk, m_data), nullptr, nullptr, 0); + SFileReadFile(file, &CMap::version, sizeof(CMap::version), nullptr, nullptr, 0); + SFileReadFile(file, &iffChunk, offsetof(SIffChunk, m_data), nullptr, nullptr, 0); + SFileReadFile(file, &CMap::header, sizeof(CMap::header), nullptr, nullptr, 0); + SFileReadFile(file, &iffChunk, offsetof(SIffChunk, m_data), nullptr, nullptr, 0); + SFileReadFile(file, CMap::m_areaInfo, sizeof(CMap::m_areaInfo), nullptr, nullptr, 0); + + if (CMap::header.m_flags & 1) { + char fileName[256]; + SFileReadFile(file, &iffChunk, offsetof(SIffChunk, m_data), nullptr, nullptr, 0); + SFileReadFile(file, fileName, iffChunk.m_size, nullptr, nullptr, 0); + SFileReadFile(file, &iffChunk, offsetof(SIffChunk, m_data), nullptr, nullptr, 0); + + SMMapObjDef def; + SFileReadFile(file, &def, sizeof(def), nullptr, nullptr, 0); + def.m_uniqueId = CMap::uniqueId--; + + CMapObjDef* mapObjDef = CMap::mapObjDefHash.Ptr(def.m_uniqueId, s_mapObjDefHashKey); + + if (!mapObjDef) { + mapObjDef = CMap::AllocMapObjDef(); + CMap::mapObjDefHash.Insert(mapObjDef, def.m_uniqueId, s_mapObjDefHashKey); + + mapObjDef->m_position.x = -def.m_position.z; + mapObjDef->m_position.y = -def.m_position.x; + mapObjDef->m_position.z = def.m_position.y; + + float rotX = def.m_rotation.z * 0.017453292f; + float rotY = def.m_rotation.x * 0.017453292f; + float rotZ = 0.017453292f * def.m_rotation.y; + + mapObjDef->m_flags = static_cast(0); + mapObjDef->m_nameId = def.m_nameId; + mapObjDef->m_doodadSet = def.m_doodadSet; + mapObjDef->m_nameSet = def.m_nameSet; + mapObjDef->unk_0148 = 0; + mapObjDef->unk_014C = 0; + mapObjDef->m_doodadSetOverrides[0] = 0; + mapObjDef->m_doodadSetOverrides[1] = 0; + mapObjDef->m_doodadSetOverrides[2] = 0; + + mapObjDef->m_mat.d3 = 1.0f; + mapObjDef->m_mat.c2 = 1.0f; + mapObjDef->m_mat.b1 = 1.0f; + mapObjDef->m_mat.a0 = 1.0f; + mapObjDef->m_mat.d2 = 0.0f; + mapObjDef->m_mat.d1 = 0.0f; + mapObjDef->m_mat.d0 = 0.0f; + mapObjDef->m_mat.c3 = 0.0f; + mapObjDef->m_mat.c1 = 0.0f; + mapObjDef->m_mat.c0 = 0.0f; + mapObjDef->m_mat.b3 = 0.0f; + mapObjDef->m_mat.b2 = 0.0f; + mapObjDef->m_mat.b0 = 0.0f; + mapObjDef->m_mat.a3 = 0.0f; + mapObjDef->m_mat.a2 = 0.0f; + mapObjDef->m_mat.a1 = 0.0f; + mapObjDef->m_mat.d0 = mapObjDef->m_position.x; + mapObjDef->m_mat.d1 = mapObjDef->m_position.y; + mapObjDef->m_mat.d2 = mapObjDef->m_position.z; + mapObjDef->m_mat.RotateAroundZ(rotZ + 3.1415927f); + mapObjDef->m_mat.RotateAroundY(rotY); + mapObjDef->m_mat.RotateAroundX(rotX); + mapObjDef->m_invMat = mapObjDef->m_mat.AffineInverse(); + + mapObjDef->m_bbox.b.x = -def.m_extents.t.z; + mapObjDef->m_bbox.b.y = -def.m_extents.t.x; + mapObjDef->m_bbox.b.z = def.m_extents.b.y; + mapObjDef->m_bbox.t.x = -def.m_extents.b.z; + mapObjDef->m_bbox.t.y = -def.m_extents.b.x; + mapObjDef->m_bbox.t.z = def.m_extents.t.y; + + mapObjDef->m_sphere.c.x = (mapObjDef->m_bbox.b.x + mapObjDef->m_bbox.t.x) * 0.5f; + mapObjDef->m_sphere.c.y = (mapObjDef->m_bbox.t.y + mapObjDef->m_bbox.b.y) * 0.5f; + mapObjDef->m_sphere.c.z = 0.5f * (mapObjDef->m_bbox.t.z + mapObjDef->m_bbox.b.z); + float dz = mapObjDef->m_bbox.t.z - mapObjDef->m_sphere.c.z; + float dy = mapObjDef->m_bbox.t.y - mapObjDef->m_sphere.c.y; + float dx = mapObjDef->m_bbox.t.x - mapObjDef->m_sphere.c.x; + mapObjDef->m_sphere.r = sqrtf(dx * dx + dy * dy + dz * dz); + + mapObjDef->m_lightsArray.m_count = 0; + mapObjDef->m_owner = CMapObj::Create(fileName); + } + + CMapBaseObjLink* link = CMap::AllocBaseObjLink(mapObjDef); + link->m_ref = nullptr; + CMap::mapObjDefLinkList.LinkToTail(link); + CMap::bDungeon = 1; + } + + if (CMap::header.m_flags & 2) { + CMapRenderChunk::SetVertexFormat(GxVBF_PNC); + } else { + CMapRenderChunk::SetVertexFormat(GxVBF_PN); + } + + CMap::ValidateShaders(); + SFileCloseFile(file); +} + +// VERIFY: 2b31b0e9 manual +// OFFSET: 0x007BFCE0 +void CMap::Load(const char* mapName, int32_t zoneID) { + CMap::s_unk_CE049C = false; + + uint32_t length = SStrCopy(CMap::mapPath, "World\\Maps\\", STORM_MAX_STR); + SStrCopy(&CMap::mapPath[length], mapName, STORM_MAX_STR); + SStrCopy(CMap::mapName, mapName, STORM_MAX_STR); + SStrPrintf(CMap::wdtFilename, sizeof(CMap::wdtFilename), "%s\\%s.wdt", CMap::mapPath, CMap::mapName); + + CMap::s_mapLight = CMap::CreateLight(1, 0); + CMap::s_mapLight->m_light.SetLightType(M2LIGHT_DIRECT); + CMap::EnableLight(CMap::s_mapLight); + CMap::UpdateLight(CMap::s_mapLight); + CMap::PurgeMaps(); + CMapObj::ClearCache(1); + CMap::PurgeUnkCache(); + + s_mapId = zoneID; + CMap::bActive = 1; + CMap::bDungeon = 0; + CMap::bPreload = 1; + + int32_t streaming = SFile::IsStreamingMode(); + CMap::s_isStreamingMode = SFile::IsTrial() | streaming; + bool wait = CMap::s_isStreamingMode == 0; + + CMap::s_lowDetail.Load(CMap::mapPath, CMap::mapName); + CMap::LoadWdt(); + CMap::LoadTex(); + DayNight::LoadMap(zoneID); + CMap::PrepareUpdate(false); + + if (wait) { + AsyncFileReadWaitAll(); + } + + if (World::s_loadProgressCallback) { + World::s_loadProgressCallback(1.0f, World::s_loadProgressParam); + } + + CMap::bPreload = 0; + World::s_loadProgressCallback = nullptr; + CWorld::status.m_prepareAll = 1; +} + +// VERIFY: 512418e2 auto +// OFFSET: 0x007BFF20 +CMapObj* CMap::AllocMapObj() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::mapObjHeap, &memHandle, &object, false)) { + return nullptr; + } + + CMapObj* mapObj = new (object) CMapObj(); + mapObj->m_objectIndex = memHandle; + + return mapObj; +} + +// VERIFY: 3e380871 auto +// OFFSET: 0x007BFFE0 +CMapObjGroup* CMap::AllocMapObjGroup() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::mapObjGroupHeap, &memHandle, &object, false)) { + return nullptr; + } + + CMapObjGroup* mapObjGroup = new (object) CMapObjGroup(); + mapObjGroup->m_objectIndex = memHandle; + + return mapObjGroup; +} + +// VERIFY: 8cfa8101 manual +// OFFSET: 0x007C00A0 +void CMap::FreeArea(CMapArea* area) { + area->m_lameAssLink.Unlink(); + area->~CMapArea(); + ObjectFree(*CMap::areaHeap, area->m_objectIndex); +} + +// VERIFY: 53440967 manual +// OFFSET: 0x007C0110 +void CMap::FreeAreaMed(CMapAreaMed* areaMed) { + areaMed->m_lameAssLink.Unlink(); + areaMed->~CMapAreaMed(); + ObjectFree(*CMap::areaMedHeap, areaMed->m_objectIndex); +} + +// VERIFY: 27b1c9a8 manual +// OFFSET: 0x007C0180 +void CMap::FreeChunk(CMapChunk* chunk) { + chunk->m_lameAssLink.Unlink(); + chunk->~CMapChunk(); + ObjectFree(*CMap::chunkHeap, chunk->m_objectIndex); +} + +// VERIFY: d1b91d2a auto +// OFFSET: 0x007C01F0 +CMapDoodadDef* CMap::AllocDoodadDef() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::doodadDefHeap, &memHandle, &object, false)) { + return nullptr; + } + + CMapDoodadDef* doodadDef = new (object) CMapDoodadDef(); + doodadDef->m_objectIndex = memHandle; + + return doodadDef; +} + +// VERIFY: fe23a5b5 auto +// OFFSET: 0x007C03E0 +CMapObjDef* CMap::AllocMapObjDef() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::mapObjDefHeap, &memHandle, &object, false)) { + return nullptr; + } + + CMapObjDef* mapObjDef = new (object) CMapObjDef(); + mapObjDef->m_objectIndex = memHandle; + + return mapObjDef; +} + +// VERIFY: 37cad926 manual +// OFFSET: 0x007C04A0 +void CMap::FreeChunkLiquid(CChunkLiquid* liquid) { + liquid->m_link.Unlink(); + liquid->~CChunkLiquid(); + ObjectFree(*CMap::chunkLiquidHeap, liquid->m_objectIndex); +} + +// VERIFY: 03529b4c manual +// OFFSET: 0x007C0500 +CMapRenderChunk* CMap::AllocRenderChunk() { + CMapRenderChunk* renderChunk = CMap::s_mapRenderChunkFreeList.Head(); + + if (!renderChunk) { + renderChunk = CMap::s_mapRenderChunkFreeList.NewNode(2, 0, 0); + renderChunk->m_renderChunkLink.Unlink(); + return renderChunk; + } + + renderChunk->m_renderChunkLink.Unlink(); + new (renderChunk) CMapRenderChunk(); + + return renderChunk; +} + +// VERIFY: d11dd1c9 auto +// OFFSET: 0x007C0670 +CMapEntity* CMap::AllocEntity(int32_t linkToHead) { + uint32_t memHandle; + void* object = nullptr; + CMapEntity* entity; + + if (ObjectAlloc(*CMap::entityHeap, &memHandle, &object, false)) { + entity = new (object) CMapEntity(); + entity->m_objectIndex = memHandle; + } else { + entity = nullptr; + } + + if (linkToHead) { + CMap::entityList.LinkToHead(entity); + } else { + CMap::entityList.LinkToTail(entity); + } + + return entity; +} + +// VERIFY: 3adb704b manual +// OFFSET: 0x007C0750 +CMapBaseObjLink* CMap::AllocBaseObjLink(CMapBaseObj* baseObj) { + uint32_t memHandle; + void* object = nullptr; + CMapBaseObjLink* link; + + if (ObjectAlloc(*CMap::baseObjLinkHeap, &memHandle, &object, false)) { + link = new (object) CMapBaseObjLink; + link->m_objectIndex = memHandle; + } else { + link = nullptr; + } + + baseObj->m_refCount++; + link->m_owner = baseObj; + link->m_ref = nullptr; + baseObj->m_parentLinkList.LinkToTail(link); + + return link; +} + +// VERIFY: e4e64c5e auto +// OFFSET: 0x007C07C0 +CMapArea* CMap::AllocArea() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::areaHeap, &memHandle, &object, false)) { + CMap::s_unk_AEED8C.LinkToTail(nullptr); + return nullptr; + } + + CMapArea* area = new (object) CMapArea(); + area->m_objectIndex = memHandle; + CMap::s_unk_AEED8C.LinkToTail(area); + + return area; +} + +// VERIFY: 2acaa66b auto +// OFFSET: 0x007C0830 +CMapChunk* CMap::AllocMapChunk() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::chunkHeap, &memHandle, &object, false)) { + CMap::s_mapChunkList.LinkToTail(nullptr); + return nullptr; + } + + CMapChunk* chunk = new (object) CMapChunk(); + chunk->m_objectIndex = memHandle; + CMap::s_mapChunkList.LinkToTail(chunk); + + return chunk; +} + +// VERIFY: 430261e3 auto +// OFFSET: 0x007C08A0 +CMapLight* CMap::AllocLight() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::lightHeap, &memHandle, &object, false)) { + CMap::lightList.LinkToTail(nullptr); + return nullptr; + } + + CMapLight* light = new (object) CMapLight(); + light->m_objectIndex = memHandle; + CMap::lightList.LinkToTail(light); + + return light; +} + +// VERIFY: 7128fd21 auto +// OFFSET: 0x007C0910 +CMapObjDefGroup* CMap::AllocMapObjDefGroup() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::mapObjDefGroupHeap, &memHandle, &object, false)) { + CMap::mapObjDefGroupList.LinkToTail(nullptr); + return nullptr; + } + + CMapObjDefGroup* group = new (object) CMapObjDefGroup(); + group->m_objectIndex = memHandle; + CMap::mapObjDefGroupList.LinkToTail(group); + + return group; +} + +// VERIFY: ecff775f auto +// OFFSET: 0x007C0980 +CChunkLiquid* CMap::AllocChunkLiquid() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::chunkLiquidHeap, &memHandle, &object, false)) { + CMap::s_chunkLiquidList.LinkToTail(nullptr); + return nullptr; + } + + CChunkLiquid* liquid = new (object) CChunkLiquid(); + liquid->m_objectIndex = memHandle; + CMap::s_chunkLiquidList.LinkToTail(liquid); + + return liquid; +} + +// VERIFY: c9309208 manual +// OFFSET: 0x007C09F0 +void CMap::FreeBaseObjLink(CMapBaseObjLink* link) { + link->m_ownerLink.Unlink(); + link->m_refLink.Unlink(); + link->m_owner->m_refCount--; + link->m_ref = nullptr; + link->m_owner = nullptr; + link->~CMapBaseObjLink(); + ObjectFree(*CMap::baseObjLinkHeap, link->m_objectIndex); +} + +// VERIFY: c37a9deb auto +// OFFSET: 0x007C0A90 +CMapAreaLow* CMap::AllocAreaLow() { + uint32_t memHandle; + void* object = nullptr; + + if (!ObjectAlloc(*CMap::areaLowHeap, &memHandle, &object, false)) { + return nullptr; + } + + CMapAreaLow* areaLow = new (object) CMapAreaLow(); + areaLow->m_objectIndex = memHandle; + + return areaLow; +} + +// VERIFY: 391a570d auto +// OFFSET: 0x007C15F0 +void CMap::ClassifyStaticEntityLink(CMapStaticEntity* entity, CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup, uint32_t faceInfo, C3Vector* pos) { + CMapObjGroup* group = mapObjDef->m_owner->GetGroup(mapObjDefGroup->m_groupNum, false); + + if (!group) { + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_EXTERIOR); + } else if (group->m_flags & 0x8) { + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_EXTERIOR); + } else if (group->m_flags & 0x40) { + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_EXTERIOR); + } else { + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_INTERIOR); + } + + if (entity->m_flags & MAPOBJ_FLAG_INTERIOR) { + entity->QueryInteriorLighting(mapObjDef, mapObjDefGroup->m_groupNum, reinterpret_cast(&faceInfo), pos); + } +} + +// VERIFY: d84c44fe manual +// OFFSET: 0x007C1660 +bool CMap::LinkStaticEntityGetChunk(C3Vector& pos, float* outHeight, CMapChunk** outChunk) { + *outChunk = nullptr; + + int32_t cellX = CMath::fint_(-(pos.y - 17066.666f) * 0.24f - 0.5f); + int32_t cellY = CMath::fint_(0.24f * -(pos.x - 17066.666f) - 0.5f); + + CMapArea* area = CMap::m_areaTable[64 * ((cellY >> 7) & 0x3F) + ((cellX >> 7) & 0x3F)]; + + if (!area || area->m_asyncObject) { + return false; + } + + CMapChunk* chunk = area->m_mapChunks[((cellX >> 3) & 0xF) + 16 * ((cellY >> 3) & 0xF)]; + + if (!chunk) { + return false; + } + + *outChunk = chunk; + + return CMap::GetHeightTerrain(chunk, pos, cellX, cellY, outHeight); +} + +// VERIFY: 4e801f8f manual +// OFFSET: 0x007C1DC0 +void CMap::LinkIntersectMapObjDefGroup(CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup, C3Vector& start, C3Vector& end, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit) { + CMapObj* mapObj = mapObjDef->m_owner; + CMapObjGroup* group = mapObj->GetGroup(mapObjDefGroup->m_groupNum, false); + + if (!group) { + return; + } + + int32_t interiorFace = -1; + int32_t groundFace = -1; + + C3Segment seg; + seg.b = start; + seg.t = end; + + int16_t interior = !(group->m_flags & 0x8); + + if (group->GetFacesForLinking(seg, &interiorHit->t, &interiorFace, &groundHit->t, &groundFace)) { + if (interiorFace != -1) { + interiorHit->def = mapObjDef; + interiorHit->group = mapObjDefGroup; + interiorHit->faceIndex = interiorFace; + interiorHit->interior = interior; + } + + if (groundFace != -1) { + groundHit->faceIndex = groundFace; + groundHit->def = mapObjDef; + groundHit->group = mapObjDefGroup; + groundHit->interior = interior; + } + } + + if (!interior) { + return; + } + + float t = 1.05f; + int32_t groups[2]; + + if (mapObj->VectorIntersectPortals(mapObjDefGroup->m_groupNum, seg, &t, groups) && t - interiorHit->t < 0.0001f) { + interiorHit->def = mapObjDef; + CMapObjDefGroup* portalGroup = mapObjDef->m_defGroups[groups[0]]; + interiorHit->t = t; + interiorHit->group = portalGroup; + interiorHit->faceIndex = -1; + interiorHit->interior = !(mapObj->GetGroup(portalGroup->m_groupNum, false)->m_flags & 0x8); + } +} + +// VERIFY: 86bfa752 auto +// OFFSET: 0x007C1FF0 +void CMap::LinkObjectToMapObjDefGroup(CMapStaticEntity* object, CMapObjDefGroup* mapObjDefGroup) { + CMapBaseObjLink* link = CMap::AllocBaseObjLink(object); + link->m_ref = mapObjDefGroup; + + if (object->m_type & 0x20) { + if (object->unk_007C & 0x2) { + mapObjDefGroup->m_entityLinkList.LinkToHead(link); + return; + } + + mapObjDefGroup->m_entityLinkList.LinkToTail(link); + } else if (object->m_type & 0x40) { + mapObjDefGroup->m_doodadDefLinkList.LinkToTail(link); + } +} + +// VERIFY: d4db675a auto +// OFFSET: 0x007C2040 +bool CMap::LinkObjectToMapExterior(CMapStaticEntity* object) { + bool linked = false; + + if (CMap::bDungeon) { + return false; + } + + float maxX = -(object->m_bbox.t.x - 17066.666f); + float maxY = -(object->m_bbox.t.y - 17066.666f); + float minX = -(object->m_bbox.b.x - 17066.666f); + + int32_t lastCol = CMath::fint_(-(object->m_bbox.b.y - 17066.666f) * 0.03f - 0.5f); + int32_t lastRow = CMath::fint_(minX * 0.03f - 0.5f); + int32_t firstCol = CMath::fint_(maxY * 0.03f - 0.5f); + int32_t firstRow = CMath::fint_(maxX * 0.03f - 0.5f); + + for (int32_t row = firstRow; row <= lastRow; row++) { + for (int32_t col = firstCol; col <= lastCol; col++) { + CMapArea* area = CMap::m_areaTable[(((row >> 4) & 0x3F) << 6) + ((col >> 4) & 0x3F)]; + + if (!area || area->m_asyncObject) { + continue; + } + + CMapChunk* chunk = area->m_mapChunks[(col & 0xF) + 16 * (row & 0xF)]; + + if (!chunk || chunk->m_bbox.b.z > object->m_bbox.t.z) { + continue; + } + + CMapBaseObjLink* link = CMap::AllocBaseObjLink(object); + link->m_ref = chunk; + + if (object->m_type & 0x20) { + if (object->unk_007C & 0x2) { + chunk->m_entityLinkList.LinkToHead(link); + } else { + chunk->m_entityLinkList.LinkToTail(link); + } + } else if (object->m_type & 0x40) { + chunk->m_doodadDefLinkList.LinkToTail(link); + } + + object->m_flags = static_cast(object->m_flags | MAPOBJ_FLAG_EXTERIOR); + linked = true; + } + } + + return linked; +} + +// VERIFY: 4d233b7e manual +// OFFSET: 0x007C25D0 +bool CMap::LinkIntersectMapObjDef(CMapObjDef* mapObjDef, C3Vector& start, C3Vector& end, C3Vector& mid, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit) { + CMapObj* mapObj = mapObjDef->m_owner; + + if (!mapObj) { + return false; + } + + C3Vector localStart = start * mapObjDef->m_invMat; + C3Vector localEnd = end * mapObjDef->m_invMat; + C3Vector localMid = mid * mapObjDef->m_invMat; + + uint32_t noDepthSort = mapObjDef->m_flags & MAPOBJ_FLAG_NO_DEPTH_SORT; + + for (CMapBaseObjLink* link = mapObjDef->m_mapObjDefGroupLinkList.Head(); link; link = mapObjDef->m_mapObjDefGroupLinkList.Next(link)) { + CMapObjDefGroup* group = static_cast(link->m_owner); + uint32_t groupFlags = mapObj->GetGroupFlags(group->m_groupNum); + + if (!(groupFlags & 0x410080) && mapObj->TestGroupBounds(localStart, localEnd, group->m_groupNum) && ((!noDepthSort && (groupFlags & 0x8)) || mapObj->TestGroupBounds(localMid, group->m_groupNum))) { + CMap::LinkIntersectMapObjDefGroup(mapObjDef, group, localStart, localEnd, &interiorHit[noDepthSort == 0], &groundHit[noDepthSort == 0]); + } + } + + return true; +} + +// VERIFY: c04d5774 manual +// OFFSET: 0x007C2700 +bool CMap::LinkIntersectMapObjDefs(C3Vector& start, C3Vector& end, C3Vector& mid, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit, CMapChunk* chunk) { + groundHit[0].t = 1.05f; + groundHit[0].def = nullptr; + groundHit[1].t = 1.05f; + groundHit[1].def = nullptr; + interiorHit[0].t = 1.05f; + interiorHit[0].def = nullptr; + interiorHit[1].t = 1.05f; + interiorHit[1].def = nullptr; + + if (chunk) { + for (CMapBaseObjLink* link = chunk->m_mapObjDefLinkList.Head(); link; link = chunk->m_mapObjDefLinkList.Next(link)) { + CMapObjDef* mapObjDef = static_cast(link->m_owner); + + if (!(mapObjDef->m_flags & MAPOBJ_FLAG_DISABLED) && mapObjDef->TestAABox(start, end)) { + CMap::LinkIntersectMapObjDef(mapObjDef, start, end, mid, interiorHit, groundHit); + } + } + } else { + for (CMapObjDef* mapObjDef = CMap::mapObjDefHash.m_fulllist.Head(); mapObjDef; mapObjDef = CMap::mapObjDefHash.Next(mapObjDef)) { + if (!(mapObjDef->m_flags & MAPOBJ_FLAG_DISABLED) && mapObjDef->TestAABox(start, end)) { + CMap::LinkIntersectMapObjDef(mapObjDef, start, end, mid, interiorHit, groundHit); + } + } + } + + if (!interiorHit[0].def && groundHit[0].def) { + interiorHit[0] = groundHit[0]; + } + + if (!interiorHit[1].def && groundHit[1].def) { + interiorHit[1] = groundHit[1]; + } + + if (!interiorHit[0].def) { + if (!interiorHit[1].def) { + return false; + } + + interiorHit[0] = interiorHit[1]; + groundHit[0] = groundHit[1]; + interiorHit[1].def = nullptr; + groundHit[1].def = nullptr; + } + + if (!groundHit[0].def) { + groundHit[0] = interiorHit[0]; + groundHit[0].faceIndex = -1; + } + + if (!groundHit[1].def) { + groundHit[1] = interiorHit[1]; + groundHit[1].faceIndex = -1; + } + + return true; +} + +// VERIFY: fbe3b25a manual +// OFFSET: 0x007C28F0 +void CMap::LinkStaticEntity(CMapStaticEntity* entity, C3Vector& top, C3Vector& bottom, C3Vector& mid, int32_t& outInterior, int32_t& outHit, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit) { + float height = 100000.0f; + float terrainT = 2.0f; + + interiorHit[0].def = nullptr; + groundHit[0].def = nullptr; + interiorHit[1].def = nullptr; + groundHit[1].def = nullptr; + + CMapChunk* chunk = nullptr; + bool terrain = false; + + if (!CMap::bDungeon) { + terrain = CMap::LinkStaticEntityGetChunk(top, &height, &chunk); + terrainT = (top.z - height) * 0.001f; + + if (terrainT < 0.0f) { + terrain = false; + } + } + + bool hit; + + if (entity->m_flags & MAPOBJ_FLAG_NO_MAPOBJ_LINK) { + hit = false; + } else { + hit = CMap::LinkIntersectMapObjDefs(top, bottom, mid, interiorHit, groundHit, nullptr); + } + + if (!terrain && !hit) { + top = entity->m_vec2; + bottom = entity->m_vec2; + bottom.z = bottom.z + 1000.0f; + CMap::LinkIntersectMapObjDefs(top, bottom, mid, interiorHit, groundHit, nullptr); + } + + if (entity->unk_007C & 0x2000) { + interiorHit[1].def = nullptr; + groundHit[1].def = nullptr; + } + + if (terrain) { + if (terrainT < interiorHit[0].t) { + interiorHit[0].def = nullptr; + groundHit[0].def = nullptr; + } + + if (terrainT < interiorHit[1].t) { + interiorHit[1].def = nullptr; + groundHit[1].def = nullptr; + } + } + + outInterior = 0; + outHit = 0; + + if (interiorHit[0].def) { + outHit = 1; + } + + if (interiorHit[1].def) { + outHit = 1; + } + + if (interiorHit[0].def) { + outInterior = interiorHit[0].interior; + } else if (interiorHit[1].def) { + outInterior = interiorHit[1].interior; + } +} + +// VERIFY: c4018711 auto +// OFFSET: 0x007C2A70 +void CMap::LinkStaticEntitySingle2(CMapStaticEntity* entity) { + C3Vector top(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z + 0.1f); + C3Vector bottom(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z - 1000.0f); + + int32_t index = 0; + int32_t interior = 0; + int32_t hit = 0; + MapObjIntersectData interiorHit[2]; + MapObjIntersectData groundHit[2]; + + CMap::LinkStaticEntity(entity, top, bottom, top, interior, hit, interiorHit, groundHit); + + uint32_t isEntity = entity->m_type & 0x20; + uint32_t groundType = 0xFFFFFFFF; + + if (!hit) { + CMap::LinkObjectToMapExterior(entity); + + if (isEntity) { + static_cast(entity)->m_groundType = CMap::QueryGroundTypeTerrain(&entity->m_position, &groundType) ? groundType : -1; + } + + entity->m_flags = static_cast((entity->m_flags & ~MAPOBJ_FLAG_IN_MAPOBJ) | MAPOBJ_FLAG_EXTERIOR); + return; + } + + if (!groundHit[0].def) { + index = 1; + } + + if (interiorHit[0].def) { + CMap::LinkObjectToMapObjDefGroup(entity, interiorHit[0].group); + } + + if (interiorHit[1].def) { + CMap::LinkObjectToMapObjDefGroup(entity, interiorHit[1].group); + } + + if (isEntity) { + static_cast(entity)->m_groundType = groundHit[index].def->GetGroundType(groundHit[index].group->m_groupNum, groundHit[index].faceIndex); + } + + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_IN_MAPOBJ); + + if (!interior) { + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_EXTERIOR); + return; + } + + entity->unk_007C |= 0x1; + + C3Vector pos(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z - (bottom.z - top.z) * groundHit[index].t); + CMap::ClassifyStaticEntityLink(entity, groundHit[index].def, groundHit[index].group, groundHit[index].faceIndex, &pos); +} + +// VERIFY: 68d8dacb manual +// OFFSET: 0x007C2BF0 +void CMap::LinkStaticEntityMultiple2ToMapObjDefExterior(CMapStaticEntity* entity) { + for (CMapObjDef* mapObjDef = CMap::mapObjDefHash.m_fulllist.Head(); mapObjDef; mapObjDef = CMap::mapObjDefHash.Next(mapObjDef)) { + if (mapObjDef->m_flags & MAPOBJ_FLAG_DISABLED) { + continue; + } + + CMapObj* mapObj = mapObjDef->m_owner; + + if (!mapObj || !mapObj->m_isGroupLoaded) { + continue; + } + + CAaBox box; + CWorldMath::TransformAABox(mapObjDef->m_invMat, entity->m_bbox, box); + + if (!mapObj->TestBounds(box)) { + continue; + } + + for (CMapBaseObjLink* link = mapObjDef->m_mapObjDefGroupLinkList.Head(); link; link = mapObjDef->m_mapObjDefGroupLinkList.Next(link)) { + CMapObjDefGroup* group = static_cast(link->m_owner); + uint32_t groupFlags = mapObj->GetGroupFlags(group->m_groupNum); + + if (!(groupFlags & 0x410080) && (groupFlags & 0x8) && mapObj->TestGroupBounds(box, group->m_groupNum, 1)) { + CMap::LinkObjectToMapObjDefGroup(entity, group); + } + } + } +} + +// VERIFY: 3091ac75 manual +// OFFSET: 0x007C2D30 +void CMap::LinkStaticEntityMultiple2ToMapObjDefInterior(CMapStaticEntity* entity, CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup) { + CMapObj* mapObj = mapObjDef->m_owner; + + if (!mapObj || !mapObj->m_isGroupLoaded) { + return; + } + + CAaBox box; + CWorldMath::TransformAABox(mapObjDef->m_invMat, entity->m_bbox, box); + + if (!mapObj->TestBounds(box)) { + return; + } + + CMap::LinkObjectToMapObjDefGroup(entity, mapObjDefGroup); + + for (CMapBaseObjLink* link = mapObjDef->m_mapObjDefGroupLinkList.Head(); link; link = mapObjDef->m_mapObjDefGroupLinkList.Next(link)) { + CMapObjDefGroup* group = static_cast(link->m_owner); + + if (group != mapObjDefGroup && !(mapObj->GetGroupFlags(group->m_groupNum) & 0x410088) && mapObj->TestGroupBounds(box, group->m_groupNum, 1)) { + CMap::LinkObjectToMapObjDefGroup(entity, group); + } + } +} + +// VERIFY: e348a76f auto +// OFFSET: 0x007C2E70 +void CMap::LinkStaticEntityMultiple2(CMapStaticEntity* entity) { + C3Vector top(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z + 4.0f); + C3Vector bottom(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z - 1000.0f); + C3Vector mid(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z + 0.15000001f); + + float boxTop = entity->m_bbox.t.z + 0.1f; + + if (boxTop < top.z) { + top.z = boxTop; + } + + int32_t interior = 0; + int32_t hit; + MapObjIntersectData interiorHit[2]; + MapObjIntersectData groundHit[2]; + + CMap::LinkStaticEntity(entity, top, bottom, mid, interior, hit, interiorHit, groundHit); + + if (interior) { + CMap::LinkStaticEntityMultiple2ToMapObjDefInterior(entity, interiorHit[0].def, interiorHit[0].group); + entity->unk_007C |= 0x1; + + C3Vector pos(entity->m_vec2.x, entity->m_vec2.y, entity->m_vec2.z - (bottom.z - top.z) * groundHit[0].t); + CMap::ClassifyStaticEntityLink(entity, groundHit[0].def, groundHit[0].group, groundHit[0].faceIndex, &pos); + } else { + CMap::LinkStaticEntityMultiple2ToMapObjDefExterior(entity); + CMap::LinkObjectToMapExterior(entity); + entity->m_flags = static_cast(entity->m_flags | MAPOBJ_FLAG_EXTERIOR); + } +} + +// VERIFY: d35a4a66 manual +// OFFSET: 0x007C2F80 +void CMap::LinkStaticEntity(CMapStaticEntity* entity) { + CMapBaseObjLink* link = entity->m_parentLinkList.Head(); + + while (link) { + CMapBaseObjLink* next = entity->m_parentLinkList.Next(link); + CMap::FreeBaseObjLink(link); + link = next; + } + + if (entity->m_type & 0x20) { + static_cast(entity)->m_groundType = -1; + } + + if (entity->unk_007C & 0x2000) { + CMap::LinkStaticEntityMultiple2(entity); + } else { + CMap::LinkStaticEntitySingle2(entity); + } +} + +// VERIFY: e2dae8ec manual +// OFFSET: 0x007C3730 +void CMap::PurgeMaps() { + CMapBaseObjLink* link = CMap::s_unk_ADFBEC.Head(); + + while (link) { + CMapBaseObjLink* next = CMap::s_unk_ADFBEC.Next(link); + CMapArea* area = static_cast(link->m_owner); + CMap::FreeBaseObjLink(link); + CMap::m_areaTable[64 * area->m_index.y + area->m_index.x] = nullptr; + area->Purge(); + CMap::FreeArea(area); + link = next; + } + + for (uint32_t i = 0; i < 4096; i++) { + if (CMap::unk[i]) { + CMap::FreeAreaMed(CMap::unk[i]); + CMap::unk[i] = nullptr; + } + } + + CMap::s_lowDetail.Purge(); +} + +// VERIFY: 6db55b9e manual +// OFFSET: 0x007D9990 +CTexture* CMap::LoadTexture(const char* fileName) { + CStatus status; + CTexture* texture = TextureCreate(fileName, CGxTexFlags(GxTex_LinearMipLinear, 1, 1, 0, 0, 0, 1), &status, 0); + SysMsgAdd(&status); + + if (!texture) { + DebugLog("Failed to load texture: %s\n"); + } + + return texture; +} + +// VERIFY: fedf0bee auto +// OFFSET: 0x007D9A20 +void CMap::LoadArea(CMapArea* area) { + char fileName[256]; + SStrPrintf(fileName, sizeof(fileName), "%s\\%s_%d_%d.adt", CMap::mapPath, CMap::mapName, area->m_index.x, area->m_index.y); + area->Load(fileName); +} + +// VERIFY: a0b22da5 auto +// OFFSET: 0x007D9A70 +CMapArea* CMap::PrepareArea(int32_t areaIndexX, int32_t areaIndexY) { + CMapArea* area = CMap::AllocArea(); + CMapBaseObjLink* link = CMap::AllocBaseObjLink(area); + CMap::s_unk_ADFBEC.LinkToTail(link); + + area->m_tileChunkIndex.x = 16 * areaIndexX; + area->m_index.y = areaIndexY; + area->m_flags = static_cast(0); + area->m_index.x = areaIndexX; + area->m_tileChunkIndex.y = 16 * areaIndexY; + + area->m_topLeft2.x = 17066.666f - area->m_tileChunkIndex.y * 33.333332f; + area->m_bounds.t.x = area->m_topLeft2.x; + area->m_topLeft2.y = area->m_tileChunkIndex.x * -33.333332f + 17066.666f; + area->m_bounds.t.y = area->m_topLeft2.y; + area->m_bounds.b.x = area->m_topLeft2.x - 533.33331f; + area->m_bounds.b.y = area->m_topLeft2.y - 533.33331f; + + CMap::m_areaTable[64 * areaIndexY + areaIndexX] = area; + + return area; +} + +// OFFSET: 0x007D9B10 +CMapLight::CMapLight() { + this->m_sphere.r = 0.0f; + this->m_type |= 0x80; + this->unk_00CC = 0.0f; + this->m_radius = 0.0f; + this->unk_00D0 = 0; + this->unk_00C4 = 0.0f; +} + +// VERIFY: 0aba9189 manual +// OFFSET: 0x007D9BD0 +CMapLight* CMap::CreateLight(uint8_t a1, uint8_t a2) { + CMapLight* light = CMap::AllocLight(); + light->m_flags = static_cast(0); + light->m_position = C3Vector(0.0f, 0.0f, 0.0f); + light->m_sphere.r = 0.0f; + light->m_sphere.c = C3Vector(0.0f, 0.0f, 0.0f); + light->m_bbox.b = C3Vector(0.0f, 0.0f, 0.0f); + light->unk_00CC = 0.0f; + light->m_radius = 0.0f; + light->m_bbox.t = C3Vector(0.0f, 0.0f, 0.0f); + light->unk_00C4 = 0.0f; + light->unk_00D0 = a1; + light->unk_00D1 = a2; + + return light; +} + +// VERIFY: e4a742da manual +// OFFSET: 0x007D9C80 +void CMap::UpdateLightBounds(CMapLight* light) { + float radius = light->m_radius; + + if (light->unk_00CC == 0.0f) { + radius = 5.0f; + } + + light->m_radius = radius; + + C3Vector& pos = light->m_light.m_pos; + light->m_position = pos; + light->m_bbox.b = C3Vector(pos.x - radius, pos.y - radius, pos.z - radius); + light->m_bbox.t = C3Vector(pos.x + radius, pos.y + radius, pos.z + radius); + light->m_sphere.c = pos; + light->m_sphere.r = radius; +} + +// VERIFY: 66f3244f auto +// OFFSET: 0x007D9D50 +void CMap::EnableLight(CMapLight* light) { + light->m_flags = static_cast(light->m_flags & ~0x20); + light->m_light.SetVisible(1); +} + +// VERIFY: 8d2bb286 auto +// OFFSET: 0x007D9DE0 +void CMap::LinkLightToChunks(CMapLight* light) { + int32_t maxY = CMath::fint_(-(light->m_bbox.b.y - 17066.666f) * 0.03f - 0.5f); + int32_t maxX = CMath::fint_(-(light->m_bbox.b.x - 17066.666f) * 0.03f - 0.5f); + int32_t minY = CMath::fint_(-(light->m_bbox.t.y - 17066.666f) * 0.03f - 0.5f); + int32_t minX = CMath::fint_(-(light->m_bbox.t.x - 17066.666f) * 0.03f - 0.5f); + + for (int32_t x = minX; x <= maxX; x++) { + for (int32_t y = minY; y <= maxY; y++) { + CMapArea* area = CMap::m_areaTable[(((x >> 4) & 0x3F) << 6) + ((y >> 4) & 0x3F)]; + + if (!area || area->m_asyncObject) { + continue; + } + + CMapChunk* chunk = area->m_mapChunks[(y & 0xF) + 16 * (x & 0xF)]; + + if (!chunk) { + continue; + } + + CMapBaseObjLink* link = CMap::AllocBaseObjLink(light); + link->m_ref = chunk; + chunk->m_lightLinkList.LinkToTail(link); + chunk->UpdateLights(); + } + } +} + +// VERIFY: 75a5e857 manual +// OFFSET: 0x007D9F90 +void CMap::LinkLightToMapObjDefs(CMapLight* light) { + CAaBox box; + + for (CMapObjDef* mapObjDef = CMap::mapObjDefHash.m_fulllist.Head(); mapObjDef; mapObjDef = CMap::mapObjDefHash.Next(mapObjDef)) { + if (mapObjDef->m_flags & 0x20) { + continue; + } + + if (light->m_bbox.t.x >= mapObjDef->m_bbox.b.x && light->m_bbox.t.y >= mapObjDef->m_bbox.b.y && light->m_bbox.t.z >= mapObjDef->m_bbox.b.z + && light->m_bbox.b.x <= mapObjDef->m_bbox.t.x && light->m_bbox.b.y <= mapObjDef->m_bbox.t.y && light->m_bbox.b.z <= mapObjDef->m_bbox.t.z) { + CMapObj* mapObj = mapObjDef->m_owner; + + if (mapObj && mapObj->TestBounds(box)) { + for (CMapBaseObjLink* link = mapObjDef->m_mapObjDefGroupLinkList.Head(); link; link = mapObjDef->m_mapObjDefGroupLinkList.Next(link)) { + CMapObjDefGroup* group = static_cast(link->m_owner); + + if (!(group->m_flags & 0x2) && light->m_bbox.Intersects(group->m_bbox)) { + CMapBaseObjLink* lightLink = CMap::AllocBaseObjLink(light); + lightLink->m_ref = group; + group->m_lightLinkList.LinkToTail(lightLink); + group->UpdateLights(); + } + } + } + } + } +} + +// VERIFY: 7439bd46 manual +// OFFSET: 0x007DA100 +void CMap::UpdateLight(CMapLight* light) { + if (!light->m_parentLinkList.Head() && !light->m_light.m_type) { + CMapBaseObjLink* link = CMap::AllocBaseObjLink(light); + link->m_ref = nullptr; + CMap::s_unk_AF16CC.LinkToTail(link); + } + + if (CMap::bActive && light->m_light.m_type) { + CMapBaseObjLink* link = light->m_parentLinkList.Head(); + + while (link) { + CMapBaseObjLink* next = light->m_parentLinkList.Next(link); + CMap::FreeBaseObjLink(link); + link = next; + } + + CMap::UpdateLightBounds(light); + CMap::LinkLightToMapObjDefs(light); + CMap::LinkLightToChunks(light); + } +} diff --git a/WoW/Source/WorldClient/Map.h b/WoW/Source/WorldClient/Map.h index 2e38884..e1b30e4 100644 --- a/WoW/Source/WorldClient/Map.h +++ b/WoW/Source/WorldClient/Map.h @@ -7,22 +7,43 @@ #include "Engine/Source/Tempest/C4Quaternion.h" #include "Engine/Source/Tempest/CAaBox.h" #include "Engine/Source/Tempest/CImVector.h" +#include "Engine/Source/Gx/CGxVertexPC.h" #include "Engine/Source/Storm/H/stpl.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/MapLowDetail.h" #include "WoW/Source/WorldClient/MapRenderChunk.h" +#include "WoW/Source/WorldClient/Types.h" + +class C44Matrix; +class CAaSphere; +class CFacet; +class CFrustum; +class CM2Model; +class CMapAreaTexture; +class CMapBaseObj; +class CMapBaseObjLink; +class CMapChunk; +class CMapDoodadDef; +class CMapLight; +class CMapObjDef; +class CMapObjDefGroup; +class CMapObjGroup; +class CMapStaticEntity; +class CiRect; +class SFileImpl; +class SFile; +namespace World { class FacetData; } struct CGxBuf; struct CGxPool; class CGxShader; +class CChunkLiquid; class CMapArea; class FVBBList; +class CMapAreaMed; class CMapEntity; class CTexture; - -class CGxVertexPC { - public: - /* 0000 */ C3Vector m_p; - /* 000C */ CImVector m_c; -}; +struct SMMapHeader; class CMapAreaEntry { public: @@ -80,8 +101,34 @@ class CMapFootprintTexture { CMapFootprintTexture(); }; +typedef int32_t (*MAP_OBJECT_FUNC)(void*, uint32_t, uint64_t, uint32_t); + +struct MapObjIntersectData { + /* 0000 */ CMapObjDef* def; + /* 0004 */ CMapObjDefGroup* group; + /* 0008 */ float t; + /* 000C */ int16_t faceIndex; + /* 000E */ int16_t interior; +}; + class CMap { public: + static TSGrowableArray debugVertexArray; + static TSGrowableArray debugIndexArray; + static CMapLight* s_mapLight; + static CMapLowDetail s_lowDetail; + static SMMapHeader header; + static bool gTerrainPixelShadersValid; + static bool enableTerrainShaderVertex; + static uint32_t version; + static int32_t bPreload; + static int32_t s_isStreamingMode; + static bool s_unk_CE049C; + static bool enableSpecular; + static bool enablePixelShaders; + static bool enableVertexShaders; + static TSList > mapObjDefLinkList; + static TSList > s_unk_AF16CC; static CGxBuf* lowDetailIndexBuf; static CGxPool* lowDetailIndexPool; static CGxShader* pixelShader_TerrainSM; @@ -94,8 +141,8 @@ class CMap { static int32_t uniqueId; static int32_t oldSelectLightParm; static uint32_t s_queryTag; - static int32_t cCount; - static uint32_t unk[4096]; + static uint32_t cCount; + static CMapAreaMed* unk[4096]; static CMapArea* m_areaTable[4096]; static SMAreaInfo m_areaInfo[4096]; static TSGrowableArray scCollideList; @@ -124,7 +171,29 @@ class CMap { static FVBBList s_vbbList; static TSExplicitList s_mapRenderChunkFreeList; static TSExplicitList s_unk_AEED80; + static TSList > s_unk_AEED8C; + static TSList > s_mapChunkList; + static TSList > mapObjDefGroupList; + static TSList > entityList; + static TSList > lightList; + static TSList > s_unk_ADFBEC; + static TSExplicitList s_mapRenderChunkUpdateList; + static bool enableSpecularTerrain; + static char wdtFilename[256]; + static char mapName[256]; + static char mapPath[256]; + static C3Vector s_subchunkCoords[5]; + static uint32_t s_holeMask[16]; + static int32_t s_subchunkIndices[8]; + static int32_t s_fanIndices[8]; + static TSList > doodadDefList; + static TSHashTable mapObjDefHash; + static int32_t enableChunkBatching; + static TSList > s_chunkLiquidList; + static TSList > s_mapChunkLiquidUpdateList; + static DepthCoordTable* s_depthCoordTables[2]; + static void ClearAllRenderChunks(); static void Destroy(); static void FootprintDestroy(); static void FootprintInitialize(); @@ -134,6 +203,103 @@ class CMap { static void InitializePCFShaders(); static void QueryAreaId(CMapEntity* entity, uint32_t& areaId); static void Unload(); + static void ValidateShaders(); + static CGxShader* GetPixelShader(bool a1, bool a2, bool a3); + static CGxShader* GetTerrainPixelShader(int32_t bigAlpha, int32_t layers, int32_t shadowMode, int32_t a4, int32_t a5); + static void Load(const char* mapName, int32_t zoneID); + static void LoadTex(); + static void LoadWdt(); + static CTexture* LoadTexture(const char* fileName); + static int32_t SafeOpen(const char* fileName, SFileImpl** file); + static int32_t SafeRead(const char* fileName, SFileImpl* file, void* buffer, uint32_t size); + static CMapArea* AllocArea(); + static CMapAreaLow* AllocAreaLow(); + static void FreeArea(CMapArea* area); + static void FreeAreaMed(CMapAreaMed* areaMed); + static void FreeChunk(CMapChunk* chunk); + static CMapChunk* AllocMapChunk(); + static CMapRenderChunk* AllocRenderChunk(); + static CMapBaseObjLink* AllocBaseObjLink(CMapBaseObj* baseObj); + static CMapDoodadDef* AllocDoodadDef(); + static CMapObjDef* AllocMapObjDef(); + static CMapObj* AllocMapObj(); + static CMapEntity* AllocEntity(int32_t linkToHead); + static CMapObjGroup* AllocMapObjGroup(); + static CMapObjDefGroup* AllocMapObjDefGroup(); + static CMapLight* AllocLight(); + static CMapObjDef* CreateMapObjDef(char* fileName, SMMapObjDef* mapObjectDef, C3Vector* center, bool cached); + static CMapLight* CreateLight(uint8_t a1, uint8_t a2); + static CMapDoodadDef* CreateDoodadDef(uint32_t doodadRef, SMODoodadDef* doodadDef, char* name, uint32_t uniqueId, C44Matrix* mat, uint16_t doodadSet); + static void FreeBaseObjLink(CMapBaseObjLink* link); + static CMapArea* PrepareArea(int32_t areaIndexX, int32_t areaIndexY); + static void LoadArea(CMapArea* area); + static void PrepareUpdate(bool a1); + static void PurgeMaps(); + static void PurgeUnkCache(); + static void PreUpdateAreas(bool a1); + static void UpdateArea(int32_t addToScene, CMapArea* area, CiRect* chunkRect, int32_t depth); + static void PrepareMapObjDefs(bool a1); + static void PrepareMapObjDef(CMapObjDef* mapObjDef, CMapObj* mapObj); + static void CreateMapObjDefGroups(CMapObj* mapObj, CMapObjDef* mapObjDef); + static bool QueryShadow(C3Vector* pos); + static DepthCoordTable* GetDepthCoordTable(uint32_t liquidType); + static CChunkLiquid* AllocChunkLiquid(); + static void CreateWaterRipple(const C3Vector& pos, float a2, float a3, float a4, float a5, float a6, int32_t a7, int32_t a8); + static bool QueryLiquidSounds(C3Vector* pos, float radius, uint32_t* results); + static bool QueryLiquidStatus(C3Vector* pos, int32_t* liquidType, float* height, int32_t* a4, int32_t checkGround); + static bool QueryLiquidTerrain(const C3Vector* pos, int32_t* liquidType, float* height, int32_t checkGround); + static void RenderWaterRipples(); + static void FreeChunkLiquid(CChunkLiquid* liquid); + static void FreeAllChunkLiquids(); + static void ProcessChunkLiquidUpdateList(); + static void PrepareMapDoodadDefs(); + static void ProcessRenderChunkUpdateList(); + static CMapEntity* ObjectCreate(CM2Model* model, MAP_OBJECT_FUNC func, void* funcParam, uint64_t param64, uint32_t param32, uint32_t a7); + static void ObjectUpdate(CMapEntity* entity, C44Matrix& mat, CAaBox& box, CAaSphere& sphere, C3Vector& vec, bool a6, uint32_t a7); + static void PrepareEntitys(int32_t addToScene); + static void LinkStaticEntityMultiple2(CMapStaticEntity* entity); + static void LinkStaticEntity(CMapStaticEntity* entity, C3Vector& top, C3Vector& bottom, C3Vector& mid, int32_t& outInterior, int32_t& outHit, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit); + static void UpdateEntity(CMapEntity* entity); + static void LinkStaticEntityMultiple2ToMapObjDefInterior(CMapStaticEntity* entity, CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup); + static void LinkObjectToMapObjDefGroup(CMapStaticEntity* object, CMapObjDefGroup* mapObjDefGroup); + static void LinkStaticEntitySingle2(CMapStaticEntity* entity); + static void LinkStaticEntity(CMapStaticEntity* entity); + static void LinkIntersectMapObjDefGroup(CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup, C3Vector& start, C3Vector& end, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit); + static bool LinkIntersectMapObjDef(CMapObjDef* mapObjDef, C3Vector& start, C3Vector& end, C3Vector& mid, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit); + static bool LinkIntersectMapObjDefs(C3Vector& start, C3Vector& end, C3Vector& mid, MapObjIntersectData* interiorHit, MapObjIntersectData* groundHit, CMapChunk* chunk); + static bool GetHeightTerrain(CMapChunk* chunk, C3Vector& pos, int32_t cellX, int32_t cellY, float* outHeight); + static bool LinkStaticEntityGetChunk(C3Vector& pos, float* outHeight, CMapChunk** outChunk); + static void ClassifyStaticEntityLink(CMapStaticEntity* entity, CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup, uint32_t faceInfo, C3Vector* pos); + static bool LinkObjectToMapExterior(CMapStaticEntity* object); + static bool QueryGroundTypeTerrain(C3Vector* pos, uint32_t* groundType); + static void LinkStaticEntityMultiple2ToMapObjDefExterior(CMapStaticEntity* entity); + static void EnableLight(CMapLight* light); + static void UpdateLight(CMapLight* light); + static void UpdateLightBounds(CMapLight* light); + static void LinkLightToMapObjDefs(CMapLight* light); + static void LinkLightToChunks(CMapLight* light); + static bool Intersect(C3Vector* start, C3Vector* end, C3Vector* hitPoint, float* distance, uint32_t flags, void* hitInfo); + static bool VectorIntersect(C3Vector* start, C3Vector* end, uint32_t flags, uint32_t defIgnoreFlags, float* distance, uint16_t* hitIndex, CMapObj** outMapObj, CMapObjDef** outMapObjDef, CMapObjDefGroup** outMapObjDefGroup); + static bool VectorIntersectTerrain(C3Vector* start, C3Vector* end, float* distance, uint32_t flags, CMapChunk** hitChunk); + static bool VectorIntersectSubChunkList(C3Vector* start, C3Vector* end, float* distance, uint32_t flags, CMapChunk** hitChunk); + static void VectorIntersectDoodadDefs(TSExplicitList* list, uint32_t flags); + static void VectorIntersectSY(CiRect& rect); + static void VectorIntersectSX(CiRect& rect); + static void VectorIntersectDY(C3Vector& a1, C3Vector& a2, CiRect& rect); + static void VectorIntersectDX(C3Vector& a1, C3Vector& a2, CiRect& rect); + static void SetHitTestDebug(void* hitInfo, CMapBaseObj* hitObject); + static bool LocateViewerMapObjs(C3Vector& start, C3Vector& end, float dist, CMapObjDef** outDefs, uint32_t* outGroups); + static void TestQueryAdd(CFacet& facet, CImVector& color, C44Matrix* mat); + static bool GetFacets(CAaBox* a1, CAaBox* a2, World::FacetData* a3, uint32_t a4, uint32_t* a5); + static bool GetMapObjFacets(CFrustum* frustum, World::FacetData* facets, uint32_t flags, uint32_t* statusOut); + static bool QueryFacets(CFrustum* frustum, World::FacetData* facets, uint32_t flags, uint32_t* a4); + static bool GetChunkFacets(int32_t chunkX, int32_t chunkY, CiRect* subRect, CAaBox* a4, CAaBox* box, World::FacetData* facets, uint32_t flags); + static CFacet* BuildImpassableFacets(World::FacetData* facets, C3Vector* up, C3Vector* edge, C3Vector* normal, C3Vector* origin); + static void CreateImpassableFacets(CMapChunk* chunk, CAaBox* box, World::FacetData* facets); + static bool CreateFlightBoundsFacets(int32_t areaX, int32_t areaY, CAaBox* box, World::FacetData* facets); + static void AppendMapObjFacets(CMapDoodadDef* def, CAaBox* box, World::FacetData* facets); + static bool GetDoodadDefFacets(TSExplicitList* linkList, CAaBox* box, World::FacetData* facets, uint32_t flags); + static int32_t GetFlightBounds(const C3Vector& pos, float* height, int32_t which); /* 0000 */ uint8_t unk_0000[180]; /* 00B4 */ uint32_t unk_00B4; @@ -141,32 +307,4 @@ class CMap { extern int32_t s_mapId; -class CMapAreaLow { - public: - /* 0000 */ uint8_t unk_0000[1]; - /* 0001 */ uint8_t unk_0001; - /* 0002 */ uint8_t unk_0002; - /* 0003 */ uint8_t unk_0003; - /* 0004 */ uint8_t unk_0004; - /* 0005 */ uint8_t unk_0005; - /* 0006 */ uint8_t unk_0006; - /* 0007 */ uint8_t unk_0007; - /* 0008 */ uint8_t unk_0008; - /* 0009 */ uint8_t unk_0009; - /* 000A */ uint8_t unk_000A; - /* 000B */ uint8_t unk_000B; - /* 000C */ uint8_t unk_000C; - /* 000D */ uint8_t unk_000D; - /* 000E */ uint8_t unk_000E; - /* 000F */ uint8_t unk_000F; - /* 0010 */ uint8_t unk_0010; - /* 0011 */ uint8_t unk_0011; - /* 0012 */ uint8_t unk_0012; - /* 0013 */ uint8_t unk_0013; - /* 0014 */ uint8_t unk_0014; - /* 0015 */ uint8_t unk_0015; - /* 0016 */ uint8_t unk_0016[4]; - /* 001A */ uint8_t unk_001A[66]; -}; - #endif diff --git a/WoW/Source/WorldClient/MapArea.cpp b/WoW/Source/WorldClient/MapArea.cpp index 671a4b5..b00f052 100644 --- a/WoW/Source/WorldClient/MapArea.cpp +++ b/WoW/Source/WorldClient/MapArea.cpp @@ -1,4 +1,17 @@ #include "MapArea.h" +#include +#include "Engine/Source/Services/AsyncFile.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/CiRect.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapChunk.h" +#include "WoW/Source/WorldClient/MapRenderChunk.h" +#include "WoW/Source/WorldClient/Types.h" +#include "WoW/Source/WorldClient/World.h" void CMapArea::Destroy() { } @@ -7,3 +20,289 @@ void CMapArea::Destroy() { // OFFSET: 0x005EEB70 void CMapArea::Initialize() { } + +// VERIFY: 8d38ed01 manual +// OFFSET: 0x007D6810 +void CMapArea::BatchChunks(const C2iVector& pos) { + int32_t index = 16 * pos.y + pos.x; + + CMapChunk* chunks[4]; + chunks[0] = this->m_mapChunks[index]; + chunks[1] = this->m_mapChunks[index + 16]; + chunks[2] = this->m_mapChunks[16 * pos.y + 1 + pos.x]; + chunks[3] = this->m_mapChunks[16 * pos.y + 17 + pos.x]; + + for (int32_t i = 0; i < 4; i++) { + if (!chunks[i]) { + return; + } + } + + CMapChunk* topLeft = chunks[0]; + CMapChunk* bottomLeft = chunks[1]; + CMapChunk* topRight = chunks[2]; + CMapChunk* bottomRight = chunks[3]; + + int32_t mergeLeft = CMapChunk::CanMergeChunkLayers(bottomLeft, topLeft); + int32_t mergeRight = CMapChunk::CanMergeChunkLayers(bottomRight, topRight); + int32_t mergeTop = CMapChunk::CanMergeChunkLayers(topRight, topLeft); + int32_t mergeBottom = CMapChunk::CanMergeChunkLayers(bottomRight, bottomLeft); + + int32_t vertical = mergeRight + mergeLeft; + int32_t horizontal = mergeBottom + mergeTop; + + if (vertical || horizontal) { + int32_t firstPartner = 2; + int32_t secondStart = 1; + int32_t firstMerge = mergeTop; + int32_t secondMerge = mergeBottom; + uint8_t orientation = 2; + + if (vertical > horizontal) { + firstMerge = mergeLeft; + firstPartner = 1; + secondStart = 2; + secondMerge = mergeRight; + orientation = 1; + } + + if (firstMerge) { + CMapRenderChunk* renderChunk = CMap::AllocRenderChunk(); + CMapChunk* partner = chunks[firstPartner]; + topLeft->m_renderChunk = renderChunk; + renderChunk->AddBatch(topLeft, partner, &topLeft->m_topLeftCoords, orientation); + partner->m_renderChunk = topLeft->m_renderChunk; + } + + if (secondMerge) { + CMapRenderChunk* renderChunk = CMap::AllocRenderChunk(); + CMapChunk* first = chunks[secondStart]; + first->m_renderChunk = renderChunk; + renderChunk->AddBatch(first, bottomRight, &first->m_topLeftCoords, orientation); + bottomRight->m_renderChunk = first->m_renderChunk; + } + } + + for (int32_t i = 0; i < 4; i++) { + CMapChunk* chunk = chunks[i]; + + if (!chunk->m_renderChunk) { + chunk->AllocRenderChunkAndBatch(); + } + } +} + +// VERIFY: bb694f33 manual +// OFFSET: 0x007D6980 +void CMapArea::LoadTerrainTexture(CMapAreaTexture* areaTexture, int32_t textureId) { + bool shaders = g_theGxDevicePtr->Caps().m_texTarget[1] && g_theGxDevicePtr->Caps().m_shaderTargets[0] && g_theGxDevicePtr->Caps().m_shaderTargets[4]; + + uint8_t flags = 0; + + if (this->m_textureFlags) { + flags = this->m_textureFlags[textureId]; + } + + if ((flags & 0x1) || !CMap::enableSpecularTerrain) { + if (!(flags & 0x1) || shaders) { + areaTexture->m_texture = CMap::LoadTexture(areaTexture->m_textureName); + } else { + CImVector color; + color.b = 0x00; + color.g = 0x00; + color.r = 0x00; + color.a = 0xFF; + areaTexture->m_texture = TextureCreateSolid(color); + } + } else { + char fileName[256]; + SStrCopy(fileName, areaTexture->m_textureName, STORM_MAX_STR); + char* ext = SStrChrR(fileName, '.'); + SStrCopy(ext, "_s.blp", STORM_MAX_STR); + areaTexture->m_texture = CMap::LoadTexture(fileName); + } +} + +// VERIFY: 4696cf2f manual +// OFFSET: 0x007D6B30 +CMapChunk* CMapArea::PrepareChunk(int32_t chunkX, int32_t chunkY) { + int32_t index = chunkX + 16 * chunkY; + + bool firstUse = true; + + if (this->m_chunkInfo[index].m_flags & 0x1) { + firstUse = false; + } + + this->m_chunkInfo[index].m_flags |= 0x1; + + CMapChunk* chunk = CMap::AllocMapChunk(); + CMapBaseObjLink* link = CMap::AllocBaseObjLink(chunk); + link->m_ref = this; + this->m_chunkLinkList.LinkToTail(link); + + chunk->m_aIndex.x = chunkX; + chunk->m_flags = static_cast(0); + chunk->m_aIndex.y = chunkY; + chunk->m_cOffset.x = chunkX + this->m_tileChunkIndex.x; + chunk->m_cOffset.y = chunkY + this->m_tileChunkIndex.y; + chunk->m_sOffset.x = 8 * (chunkX + this->m_tileChunkIndex.x); + chunk->m_sOffset.y = 8 * (chunkY + this->m_tileChunkIndex.y); + chunk->Create(reinterpret_cast(static_cast(this->m_fileBuffer) + this->m_chunkInfo[index].m_offset), firstUse); + + return chunk; +} + +// VERIFY: 0ce80d37 manual +// OFFSET: 0x007D6BF0 +void CMapArea::Update(int32_t addToScene, CiRect* chunkRect) { + for (int32_t chunkY = chunkRect->minY; chunkY <= chunkRect->maxY; chunkY++) { + int32_t localY = chunkY & 0xF; + + for (int32_t chunkX = chunkRect->minX; chunkX <= chunkRect->maxX; chunkX++) { + int32_t localX = chunkX & 0xF; + + if (chunkX >= CWorld::gbChunkRect.minX && chunkX <= CWorld::gbChunkRect.maxX && chunkY >= CWorld::gbChunkRect.minY && chunkY <= CWorld::gbChunkRect.maxY) { + CMapChunk** slot = &this->m_mapChunks[localX + 16 * localY]; + CMapChunk* chunk = *slot; + + if (!chunk) { + this->PrepareChunk(localX, localY); + chunk = *slot; + } + + if (addToScene && CWorldScene::InsideFrustumRect(chunkRect)) { + CWorldScene::AddMapChunk(chunk); + chunk->CullAndBucketChunk(); + } + } else { + CMapChunk* chunk = this->m_mapChunks[16 * localY + localX]; + + if (chunk) { + this->m_mapChunks[16 * chunk->m_aIndex.y + chunk->m_aIndex.x] = nullptr; + CMap::FreeBaseObjLink(chunk->m_parentLinkList.Head()); + this->PurgeChunk(chunk); + } + } + } + } +} + +// VERIFY: 1e0fb652 manual +// OFFSET: 0x007D6D20 +void CMapArea::LoadTextures(char* fileNames, uint32_t fileNamesSize) { + uint32_t offset = 0; + int32_t streaming = SFile::IsStreamingMode(); + + this->m_textures.m_chunk = 16; + + if (!fileNamesSize) { + return; + } + + do { + CMapAreaTexture* areaTexture = this->m_textures.New(); + areaTexture->m_textureName = &fileNames[offset]; + areaTexture->m_texture = nullptr; + + if (!streaming) { + this->LoadTerrainTexture(&this->m_textures.m_data[this->m_textures.Count() - 1], this->m_textures.Count() - 1); + } + + while (fileNames[offset]) { + offset++; + } + + offset++; + } while (offset < fileNamesSize); +} + +// VERIFY: 2e3ea90a manual +// OFFSET: 0x007D6EF0 +void CMapArea::ProcessIffChunks() { + SIffChunk* version = static_cast(this->m_fileBuffer); + uint8_t* header = reinterpret_cast(&version->m_data) + version->m_size + offsetof(SIffChunk, m_data); + + this->m_header = reinterpret_cast(header); + this->m_chunkInfo = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mcin)->m_data); + char* textureNames = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mtex)->m_data); + this->m_doodadDef = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mddf)->m_data); + this->m_mapObjDef = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_modf)->m_data); + this->m_m2FileNames = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mmdx)->m_data); + this->m_wmoFileNames = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mwmo)->m_data); + this->m_modelFilenamesOffsets = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mmid)->m_data); + this->m_wmoFilenamesOffsets = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mwid)->m_data); + + if (this->m_header->m_flags & 0x1) { + this->m_flyingBbox = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mfbo)->m_data); + } + + if (this->m_header->m_mh2o && reinterpret_cast(header + this->m_header->m_mh2o)->m_size) { + void* mem = SMemAlloc(sizeof(Liquid::CAreaData), __FILE__, __LINE__, 0); + Liquid::CAreaData* liquid = mem ? new (mem) Liquid::CAreaData(nullptr) : nullptr; + this->m_liquid = liquid; + liquid->SetData(&reinterpret_cast(header + this->m_header->m_mh2o)->m_data); + } + + if (this->m_header->m_mtxf) { + this->m_textureFlags = reinterpret_cast(&reinterpret_cast(header + this->m_header->m_mtxf)->m_data); + } + + this->LoadTextures(textureNames, reinterpret_cast(header + this->m_header->m_mtex)->m_size); + + this->m_doodadDefCount = reinterpret_cast(header + this->m_header->m_mddf)->m_size / sizeof(SMDoodadDef); + this->m_mapObjDefCount = reinterpret_cast(header + this->m_header->m_modf)->m_size / sizeof(SMMapObjDef); +} + +// VERIFY: ff270d7a auto +// OFFSET: 0x007D7020 +void CMapArea::AsyncCallback(void* param) { + CMapArea* area = static_cast(param); + + area->ProcessIffChunks(); + AsyncFileReadDestroyObject(area->m_asyncObject); + area->m_asyncObject = nullptr; + area->m_file = nullptr; +} + +// OFFSET: 0x007D7050 +CMapArea::CMapArea() { + this->m_index.x = 0; + this->m_index.y = 0; + this->m_tileChunkIndex.x = 0; + this->m_tileChunkIndex.y = 0; + this->m_chunkLinkList.SetLinkOffset(offsetof(CMapBaseObjLink, m_refLink)); + this->m_file = nullptr; + this->m_asyncObject = nullptr; + memset(this->m_mapChunks, 0, sizeof(this->m_mapChunks)); + this->m_fileBuffer = nullptr; + this->m_fileSize = 0; + this->unk_008C = 0; + this->m_doodadDef = nullptr; + this->m_mapObjDef = nullptr; + this->m_chunkInfo = nullptr; + this->m_doodadDefCount = 0; + this->m_mapObjDefCount = 0; + this->m_m2FileNames = nullptr; + this->m_wmoFileNames = nullptr; + this->m_modelFilenamesOffsets = nullptr; + this->m_wmoFilenamesOffsets = nullptr; + this->m_flyingBbox = nullptr; + this->m_textureFlags = nullptr; + this->m_liquid = nullptr; +} + +// VERIFY: 09d8cf56 auto +// OFFSET: 0x007D7150 +void CMapArea::Load(const char* fileName) { + CMap::SafeOpen(fileName, &this->m_file); + this->m_fileSize = SFileGetFileSize(this->m_file, nullptr); + this->m_fileBuffer = CMapArea::AllocAsyncLoadBuffer(this->m_fileSize); + this->m_asyncObject = AsyncFileReadAllocObject(); + this->m_asyncObject->m_file = this->m_file; + this->m_asyncObject->m_buffer = this->m_fileBuffer; + this->m_asyncObject->m_size = this->m_fileSize; + this->m_asyncObject->m_userArg = this; + this->m_asyncObject->m_userPostloadCallback = reinterpret_cast(&CMapArea::AsyncCallback); + AsyncFileReadObject(this->m_asyncObject, 0); +} diff --git a/WoW/Source/WorldClient/MapArea.h b/WoW/Source/WorldClient/MapArea.h index e65f486..844a0c7 100644 --- a/WoW/Source/WorldClient/MapArea.h +++ b/WoW/Source/WorldClient/MapArea.h @@ -6,12 +6,17 @@ #include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/CAaBox.h" #include "Engine/Source/Storm/H/stpl.h" +#include "WoW/Source/LiquidSystem/Liquid.h" #include "WoW/Source/WorldClient/MapBaseObj.h" #include "Engine/Source/Tempest/C2iVector.h" +class CiRect; + class CAsyncObject; +class SFileImpl; class CMapArea; class CMapAreaChunkLink; +class CMapBaseObjLink; class CMapAreaTexture; class CMapChunk; struct SMAreaHeader; @@ -32,6 +37,8 @@ class CMapArea : public CMapBaseObj { public: static void Destroy(); static void Initialize(); + static void* AllocAsyncLoadBuffer(int32_t fileSize); + static void AsyncCallback(void* param); /* 0024 */ CAaBox m_bounds; /* 003C */ C3Vector m_topLeft2; @@ -39,9 +46,9 @@ class CMapArea : public CMapBaseObj { /* 0050 */ C2iVector m_tileChunkIndex; /* 0058 */ TSGrowableArray m_textures; /* 0068 */ SMAreaHeader* m_header; - /* 006C */ int32_t m_file; + /* 006C */ SFileImpl* m_file; /* 0070 */ CAsyncObject* m_asyncObject; - /* 0074 */ TSExplicitList m_chunkLinkList; + /* 0074 */ TSList > m_chunkLinkList; /* 0080 */ void* m_fileBuffer; /* 0084 */ int32_t m_fileSize; /* 0088 */ SMChunkInfo* m_chunkInfo; @@ -56,8 +63,20 @@ class CMapArea : public CMapBaseObj { /* 00AC */ uint32_t* m_wmoFilenamesOffsets; /* 00B0 */ int16_t* m_flyingBbox; /* 00B4 */ int32_t* m_textureFlags; - /* 00B8 */ uint8_t* unk_00B8; + /* 00B8 */ Liquid::CAreaData* m_liquid; /* 00BC */ CMapChunk* m_mapChunks [256]; + + CMapArea(); + void Load(const char* fileName); + void LoadTextures(char* fileNames, uint32_t fileNamesSize); + void LoadTerrainTexture(CMapAreaTexture* areaTexture, int32_t textureId); + void ProcessIffChunks(); + CMapChunk* PrepareChunk(int32_t chunkX, int32_t chunkY); + void PurgeChunks(CiRect* chunkRect); + void PurgeChunk(CMapChunk* chunk); + void Purge(); + void Update(int32_t addToScene, CiRect* chunkRect); + void BatchChunks(const C2iVector& pos); }; struct SMAreaHeader { @@ -85,9 +104,9 @@ struct SMChunkInfo { /* 000C */ uint8_t unk_000C[4]; }; -class CMapAreaMed { +class CMapAreaMed : public CMapBaseObj { public: - /* 0000 */ uint8_t unk_0000[33404]; + /* 0024 */ uint8_t unk_0024[33368]; }; #endif diff --git a/WoW/Source/WorldClient/MapBaseObj.cpp b/WoW/Source/WorldClient/MapBaseObj.cpp index 170741a..bcd35c3 100644 --- a/WoW/Source/WorldClient/MapBaseObj.cpp +++ b/WoW/Source/WorldClient/MapBaseObj.cpp @@ -1 +1,9 @@ #include "MapBaseObj.h" + +// VERIFY: ec60c430 manual +// OFFSET: 0x007D7260 +CMapBaseObj::CMapBaseObj() { + this->m_type = 1; + this->m_refCount = 0; + this->m_flags = static_cast(0); +} diff --git a/WoW/Source/WorldClient/MapBaseObj.h b/WoW/Source/WorldClient/MapBaseObj.h index 821c9ff..d052078 100644 --- a/WoW/Source/WorldClient/MapBaseObj.h +++ b/WoW/Source/WorldClient/MapBaseObj.h @@ -4,10 +4,11 @@ #include #include #include "Engine/Source/Storm/H/stpl.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" #include "WoW/Source/WorldClient/Types.h" +class CM2Lighting; class CMapBaseObj; -class CMapBaseObjLink; class CMapBaseObj { public: @@ -16,9 +17,13 @@ class CMapBaseObj { /* 000A */ uint16_t m_refCount; /* 000C */ MapObjFlag m_flags; /* 0010 */ TSLink m_lameAssLink; - /* 0018 */ TSExplicitList m_parentLinkList; + /* 0018 */ TSExplicitList m_parentLinkList; virtual ~CMapBaseObj(); + virtual void SelectLights(CM2Lighting* lighting); + virtual void SelectUnderwater(CM2Lighting* lighting); + + CMapBaseObj(); }; #endif diff --git a/WoW/Source/WorldClient/MapChunk.cpp b/WoW/Source/WorldClient/MapChunk.cpp index 91cc1f9..5467617 100644 --- a/WoW/Source/WorldClient/MapChunk.cpp +++ b/WoW/Source/WorldClient/MapChunk.cpp @@ -1,5 +1,36 @@ #include "MapChunk.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Tempest/CMath.h" +#include "WoW/Source/WorldClient/CChunkLiquid.h" +#include "WoW/Source/LiquidSystem/Liquid.h" +#include "WoW/Source/DB/Rec/LiquidTypeRec.h" +#include "WoW/Source/DB/Rec/LiquidMaterialRec.h" +#include "WoW/Source/DB/DBClient/DBClient.h" +#include "Engine/Source/Storm/H/storm.h" #include "WoW/Source/WorldClient/MapRenderChunk.h" +#include +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapObjGroup.h" +#include "WoW/Source/WorldClient/MapArea.h" +#include "Engine/Source/Tempest/C2iVector.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Tempest/CFacet.h" +#include "Engine/Source/Tempest/CFrustum.h" +#include "Engine/Source/Gx/Types.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/MapDoodadDef.h" +#include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/CWorldMath.h" +#include "Engine/Source/Tempest/CiRect.h" +#include "Engine/Source/Base/CDataAllocator.h" +#include "Engine/Source/Tempest/C4Plane.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Base/RCString.h" +#include "WoW/Source/WorldClient/Types.h" +#include "Engine/Source/Gxu/GxuLight.h" // STATIC: 0x00D25488 float CMapChunk::s_geoToTex; @@ -7,6 +38,84 @@ float CMapChunk::s_geoToTex; // STATIC: 0x00D25498 C3Vector CMapChunk::vertexList[145]; +// STATIC: 0x00AEEE3C +const int32_t CMapChunk::cornerVertexIndex[4] = { 0, 8, 136, 144 }; + +// STATIC: 0x00CD8F38 +int32_t CMapChunk::farCornerIndex; + +// OFFSET: 0x007C3A10 +void CMapChunk::ProcessIffChunks(bool a3) { + SIffChunk* headerChunk = reinterpret_cast(this->m_chunkInfoBeginPtr); + this->m_header = reinterpret_cast(&headerChunk->m_data); + + int32_t remaining = headerChunk->m_size - sizeof(SMChunk); + SIffChunk* chunk = reinterpret_cast(reinterpret_cast(this->m_header) + sizeof(SMChunk)); + + while (remaining > 0) { + uint8_t* data = reinterpret_cast(&chunk->m_data); + remaining -= offsetof(SIffChunk, m_data); + + switch (chunk->m_token) { + case 0x4D434E52: + this->m_normals = reinterpret_cast(data); + + if (a3) { + chunk->m_size = 448; + } + + break; + + case 0x4D435348: + this->m_shadowMap = data; + break; + + case 0x4D435654: + this->m_height = reinterpret_cast(data); + break; + + case 0x4D435246: + this->m_mcrfPtr = data; + break; + + case 0x4D435345: + if (this->m_header->m_nSndEmitters) { + this->m_soundEmitters = reinterpret_cast(data); + } + + break; + + case 0x4D434C59: + this->m_layers = reinterpret_cast(data); + break; + + case 0x4D434C51: + if (this->m_header->m_sizeLiquid > 8) { + this->m_liquid = reinterpret_cast(data); + chunk->m_size = this->m_header->m_sizeLiquid - 8; + } + + break; + + case 0x4D43414C: + this->m_additionalShadowmap = data; + + if (a3) { + chunk->m_size = this->m_header->m_sizeAlpha - 8; + } + + break; + + case 0x4D434356: + this->m_vertexShading = reinterpret_cast(data); + break; + } + + remaining -= chunk->m_size; + chunk = reinterpret_cast(data + chunk->m_size); + } +} + // VERIFY: 86d6cd8c manual // OFFSET: 0x007C3C60 void CMapChunk::InitializeVertexGrid() { @@ -44,3 +153,609 @@ void CMapChunk::Initialize() { void CMapChunk::Destroy() { CMapRenderChunk::Destroy(); } + + +// VERIFY: 5994fc04 auto +// OFFSET: 0x007C3B40 +void CMapChunk::AllocRenderChunkAndBatch() { + this->m_renderChunk = CMap::AllocRenderChunk(); + this->m_renderChunk->AddBatch(this, nullptr, &this->m_topLeftCoords, 0); +} + +// OFFSET: 0x007C3B60 +int16_t CMapChunk::CreateIndices(char* buf, int32_t baseVertex) { + uint16_t* indexDst = reinterpret_cast(buf); + int16_t totalIndices = 0; + + for (int row = 0; row < 8; ++row) { + for (int col = 0; col < 8; ++col) { + // Holes are stored as a 4×4 bitmask (one bit per 2×2 block of cells). + int holeMaskIndex = (row / 2) * 4 + (col / 2); + if (this->m_header->m_holes & CMap::s_holeMask[holeMaskIndex]) + continue; + + // The five vertices that define this cell. + // Each outer row has 9 verts, each inner row has 8 — so 17 per row-pair. + uint16_t TL = baseVertex + col; + uint16_t TR = baseVertex + col + 1; + uint16_t C = baseVertex + col + 9; // inner vertex, midpoint of cell + uint16_t BL = baseVertex + col + 17; // next outer row + uint16_t BR = baseVertex + col + 18; + + // Four triangles, all fanning from the center point (CCW winding). + indexDst[0] = C; + indexDst[1] = TL; + indexDst[2] = BL; // left + indexDst[3] = C; + indexDst[4] = TR; + indexDst[5] = TL; // top + indexDst[6] = C; + indexDst[7] = BR; + indexDst[8] = TR; // right + indexDst[9] = C; + indexDst[10] = BL; + indexDst[11] = BR; // bottom + indexDst += 12; + totalIndices += 12; + } + + // Advance past this row-pair (9 outer + 8 inner vertices). + baseVertex += 17; + } + + return totalIndices; +} + +// OFFSET: 0x007C4960 +void CMapChunk::CreateVerticesLocal(CGxVertexPN* v) { + // ~1/127: scales packed signed-byte normals into [-1, 1] float range + static const float NORMAL_SCALE = 0.0078740157f; + + // Both expressions simplify to: 33.333332f * 0.125f = ~4.1667 units/step + const float stepX = (17066.666f - (float)this->m_cOffset.x * 33.333332f - (17066.666f - (float)(this->m_cOffset.x + 1) * 33.333332f)) * -0.125f; + const float stepY = (17066.666f - (float)this->m_cOffset.y * 33.333332f - (17066.666f - (float)(this->m_cOffset.y + 1) * 33.333332f)) * -0.125f; + + const float halfStepX = stepX * 0.5f; + + float* height = this->m_height; + int8_t* normals = this->m_normals; + + for (int row = 0; row < 9; row++) { + // --- Outer grid row: 9 evenly-spaced vertices --- + const float outerX = (float)row * stepY; + + for (int col = 0; col < 9; col++) { + v->m_position.x = outerX; + v->m_position.y = (float)col * stepX; + v->m_position.z = height[col]; + v->m_normal.x = normals[col * 3 + 0] * NORMAL_SCALE; + v->m_normal.y = normals[col * 3 + 1] * NORMAL_SCALE; + v->m_normal.z = normals[col * 3 + 2] * NORMAL_SCALE; + v++; + } + + height += 9; + normals += 27; // 9 vertices * 3 components + + // --- Inner (center-point) grid row: 8 vertices, staggered by half a step --- + // The last outer row (row == 8) has no corresponding inner row + if (row < 8) { + const float innerX = (float)row * stepX + 0.5f * stepY; + + for (int col = 0; col < 8; col++) { + v->m_position.x = innerX; + v->m_position.y = (float)col * stepY + halfStepX; + v->m_position.z = height[col]; + v->m_normal.x = normals[col * 3 + 0] * NORMAL_SCALE; + v->m_normal.y = normals[col * 3 + 1] * NORMAL_SCALE; + v->m_normal.z = normals[col * 3 + 2] * NORMAL_SCALE; + v++; + } + + height += 8; + normals += 24; // 8 vertices * 3 components + } + } +} + + +// OFFSET: 0x007C4F10 +void CMapChunk::CreateVerticesLocal(CGxVertexPNC* v) { + float stepX = (17066.666f - this->m_cOffset.x * 33.333332f - (17066.666f - (this->m_cOffset.x + 1) * 33.333332f)) * -0.125f; + float stepY = -0.125f * (17066.666f - this->m_cOffset.y * 33.333332f - (17066.666f - 33.333332f * (this->m_cOffset.y + 1))); + float halfStepX = stepX * 0.5f; + float halfStepY = 0.5f * stepY; + + float* height = this->m_height; + int8_t* normals = this->m_normals; + uint32_t* shading = this->m_vertexShading; + + for (int32_t row = 0; row < 9; row++) { + for (int32_t col = 0; col < 9; col++) { + v->m_position.x = stepY * row; + v->m_position.y = col * stepX; + v->m_position.z = *height; + v->m_normal.x = normals[0] * 0.0078740157f; + v->m_normal.y = normals[1] * 0.0078740157f; + v->m_normal.z = normals[2] * 0.0078740157f; + + if (this->m_vertexShading) { + CImVector color = *reinterpret_cast(shading); + + if (g_theGxDevicePtr->Caps().m_colorFormat == GxCF_rgba) { + CImVector swapped; + swapped.b = color.r; + swapped.g = color.g; + swapped.r = color.b; + swapped.a = color.a; + v->m_color = swapped; + } else { + v->m_color = color; + } + } else { + *reinterpret_cast(&v->m_color) = 0xFF7F7F7F; + } + + height++; + v++; + normals += 3; + shading++; + } + + if (row < 8) { + float innerX = row * stepX + halfStepY; + + for (int32_t col = 0; col < 8; col++) { + v->m_position.x = innerX; + v->m_position.y = col * stepY + halfStepX; + v->m_position.z = *height; + v->m_normal.x = normals[0] * 0.0078740157f; + v->m_normal.y = normals[1] * 0.0078740157f; + v->m_normal.z = normals[2] * 0.0078740157f; + + if (this->m_vertexShading) { + CImVector color = *reinterpret_cast(shading); + + if (g_theGxDevicePtr->Caps().m_colorFormat == GxCF_rgba) { + CImVector swapped; + swapped.b = color.r; + swapped.g = color.g; + swapped.r = color.b; + swapped.a = color.a; + v->m_color = swapped; + } else { + v->m_color = color; + } + } else { + *reinterpret_cast(&v->m_color) = 0xFF7F7F7F; + } + + height++; + v++; + normals += 3; + shading++; + } + } + } +} + +// OFFSET: 0x007C51B0 +void CMapChunk::CreateIndices(uint16_t* buf, CGxBatch* batch) { + int32_t baseVertex = batch->m_maxIndex ? batch->m_maxIndex + 1 : 0; + int16_t count = this->CreateIndices(reinterpret_cast(buf), baseVertex); + + uint16_t minIndex = batch->m_minIndex; + + if (minIndex >= baseVertex) { + minIndex = baseVertex; + } + + batch->m_minIndex = minIndex; + + int32_t maxIndex = CMapRenderChunk::unk_AEEC60.m_maxIndex + baseVertex; + + if (batch->m_maxIndex > maxIndex) { + maxIndex = batch->m_maxIndex; + } + + batch->m_maxIndex = maxIndex; + batch->m_count += count; +} + +// OFFSET: 0x007C5220 +void CMapChunk::CreateBounds() { + int32_t tileX = this->m_cOffset.x; + int32_t tileY = this->m_cOffset.y; + + this->m_bbox.b.x = 17066.666f - 33.333332f * (tileY + 1); + this->m_bbox.t.x = 17066.666f - 33.333332f * tileY; + this->m_bbox.b.y = 17066.666f - 33.333332f * (tileX + 1); + this->m_bbox.t.y = 17066.666f - 33.333332f * tileX; + this->m_bbox.b.z = 3.4028235e38f; + this->m_bbox.t.z = -3.4028235e38f; + + float* h = this->m_height; + int count = 29; + do { + for (int i = 0; i < 5; i++) { + float height = h[i]; + if (this->m_bbox.b.z > height) + this->m_bbox.b.z = height; + if (this->m_bbox.t.z < height) + this->m_bbox.t.z = height; + } + h += 5; + } while (--count); + + this->m_bbox.b.z += this->m_topLeftCoords.z; + this->m_bbox.t.z += this->m_topLeftCoords.z; + + this->m_sphere.c.x = (this->m_bbox.b.x + this->m_bbox.t.x) * 0.5f; + this->m_sphere.c.y = (this->m_bbox.b.y + this->m_bbox.t.y) * 0.5f; + this->m_sphere.c.z = (this->m_bbox.b.z + this->m_bbox.t.z) * 0.5f; + + this->m_bbox2 = this->m_bbox; + + float ex = this->m_bbox.t.x - this->m_sphere.c.x; + float ey = this->m_bbox.t.y - this->m_sphere.c.y; + float ez = this->m_bbox.t.z - this->m_sphere.c.z; + this->m_sphere.r = sqrt(ex * ex + ey * ey + ez * ez); +} + +// VERIFY: baa581e9 auto +// OFFSET: 0x007C5440 +void CMapChunk::Batch() { + if (this->m_bLoaded) { + return; + } + + if (CMap::enableChunkBatching) { + CMapArea* area = static_cast(this->m_parentLinkList.Head()->m_ref); + + C2iVector pos; + pos.x = this->m_aIndex.x & ~1; + pos.y = this->m_aIndex.y & ~1; + area->BatchChunks(pos); + } else { + this->m_renderChunk = CMap::AllocRenderChunk(); + this->m_renderChunk->AddBatch(this, nullptr, &this->m_topLeftCoords, 0); + this->m_bLoaded = 1; + } +} + +// VERIFY: 2c8f1c5e auto +// OFFSET: 0x007C54C0 +void CMapChunk::CreateVertices(char* buf, int32_t vertexOffset, const C3Vector& offset) { + if (CMap::enableTerrainShaderVertex) { + if (CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PN) { + this->CreateVerticesWorld(reinterpret_cast(buf) + vertexOffset, offset); + } else { + this->CreateVerticesWorld(reinterpret_cast(buf) + vertexOffset, offset); + } + } else if (CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PN) { + this->CreateVerticesLocal(reinterpret_cast(buf)); + } else { + this->CreateVerticesLocal(reinterpret_cast(buf)); + } +} + +// VERIFY: 4f0133d9 auto +// OFFSET: 0x007C5530 +void CMapChunk::GetLiquidBounds(CAaBox& box) const { + box.t = this->m_bbox.t; + box.b = this->m_bbox.b; + + CChunkLiquid* liquid = const_cast(this)->m_liquidChunkLinkList.Head(); + + if (liquid) { + box.t.z = liquid->m_height.y; + box.b.z = liquid->m_height.x; + + do { + box.t.z = liquid->m_height.y >= box.t.z ? liquid->m_height.y : box.t.z; + box.b.z = liquid->m_height.x <= box.b.z ? liquid->m_height.x : box.b.z; + liquid = const_cast(this)->m_liquidChunkLinkList.Next(liquid); + } while (liquid); + } +} + +// VERIFY: 16a231ad manual +// OFFSET: 0x007C55D0 +bool CMapChunk::GetLiquidHeight(const C2Vector& pos, float* height) const { + int32_t x = CMath::fint_(pos.x - 0.5f); + int32_t y = CMath::fint_(pos.y - 0.5f); + + C2iVector tile; + tile.x = x & 7; + tile.y = y & 7; + + C2Vector frac(pos.x - x, pos.y - y); + + for (CChunkLiquid* liquid = const_cast(this)->m_liquidChunkLinkList.Head(); liquid; liquid = const_cast(this)->m_liquidChunkLinkList.Next(liquid)) { + if (liquid->TileExists(tile.x, tile.y) && liquid->GetHeight(frac, tile, *height)) { + return true; + } + } + + return false; +} + +// VERIFY: 981b86cf manual +// OFFSET: 0x007C5690 +void CMapChunk::CreateLiquids(bool a3) { + CChunkLiquid* liquid = this->m_liquidChunkLinkList.Head(); + + while (liquid) { + liquid->m_chunkLink.Unlink(); + liquid->Purge(); + CMap::FreeChunkLiquid(liquid); + liquid = this->m_liquidChunkLinkList.Next(liquid); + } + + SMLiquidChunk* mclq = this->m_liquid; + + if (mclq) { + uint32_t flag = 4; + + for (uint32_t i = 0; i < 4; i++) { + if (flag & this->m_header->m_flags) { + uint32_t type = i + 1; + + if (s_mapId == 530 && i == 2) { + type = 15; + } + + LiquidTypeRec* typeRec = g_liquidTypeDB.GetRecord(type); + LiquidMaterialRec* materialRec = g_liquidMaterialDB.GetRecord(typeRec->m_materialID); + + CChunkLiquid* chunkLiquid = CMap::AllocChunkLiquid(); + this->m_liquidChunkLinkList.LinkToTail(chunkLiquid); + chunkLiquid->m_type = type; + chunkLiquid->m_owner = this; + chunkLiquid->unk_0008 = materialRec->m_lvf; + chunkLiquid->m_topLeftCoords = this->m_topLeftCoords; + chunkLiquid->m_height = *reinterpret_cast(&mclq->m_minHeight); + + uint8_t* verts = reinterpret_cast(mclq->m_verts); + chunkLiquid->m_tileBegin.x = 0; + chunkLiquid->m_tileBegin.y = 0; + chunkLiquid->m_tileEnd.x = 8; + chunkLiquid->m_tileEnd.y = 8; + + void* mem = SMemAlloc(sizeof(Liquid::CChunkVertexData), __FILE__, __LINE__, 0); + Liquid::CChunkVertexData* vertexData = mem ? new (mem) Liquid::CChunkVertexData(verts, chunkLiquid->unk_0008) : nullptr; + chunkLiquid->m_tiles = reinterpret_cast(&mclq->m_tiles); + chunkLiquid->m_vertexData = vertexData; + mclq++; + + chunkLiquid->CreateVertXY(); + } + + flag *= 2; + } + } + + CMapArea* area = static_cast(this->m_parentLinkList.Head()->m_ref); + + if (area->m_liquid && area->m_liquid->HasData()) { + const SMLiquidChunkDesc* chunkDesc = area->m_liquid->GetChunkDesc(this->m_aIndex.x, this->m_aIndex.y); + this->m_deepBits.SetRawData(const_cast(reinterpret_cast(area->m_liquid->GetDeepTable(chunkDesc))), 64, false); + this->m_fishableBits.SetRawData(const_cast(reinterpret_cast(area->m_liquid->GetFishableTable(chunkDesc))), 64, false); + + for (uint32_t i = 0; i < chunkDesc->m_layerCount; i++) { + const SMLiquidInstance* instance = area->m_liquid->GetInstanceDesc(chunkDesc, i); + + CChunkLiquid* chunkLiquid = CMap::AllocChunkLiquid(); + this->m_liquidChunkLinkList.LinkToTail(chunkLiquid); + chunkLiquid->m_owner = this; + chunkLiquid->m_type = instance->m_liquidType; + chunkLiquid->unk_0008 = instance->m_vertexFormat; + chunkLiquid->m_topLeftCoords = this->m_topLeftCoords; + chunkLiquid->m_height = *reinterpret_cast(&instance->m_minHeight); + + uint8_t yOffset = instance->m_yOffset; + uint8_t xOffset = instance->m_xOffset; + chunkLiquid->m_tileBegin.x = yOffset; + chunkLiquid->m_tileBegin.y = xOffset; + chunkLiquid->m_tileEnd.x = yOffset + instance->m_height; + chunkLiquid->m_tileEnd.y = xOffset + instance->m_width; + + void* mem = SMemAlloc(sizeof(Liquid::CInstanceVertexData), __FILE__, __LINE__, 0); + chunkLiquid->m_vertexData = mem ? new (mem) Liquid::CInstanceVertexData(area->m_liquid, instance) : nullptr; + + uint32_t tileCount = instance->m_width * instance->m_height; + chunkLiquid->m_tileBits.SetRawData(const_cast(reinterpret_cast(area->m_liquid->GetExistsTable(instance))), tileCount, false); + chunkLiquid->CreateVertXY(); + } + } + + if (!this->m_liquidChunkLinkList.Head()) { + this->m_liquidBounds.b = C3Vector(3.4028235e38f, 3.4028235e38f, 3.4028235e38f); + this->m_liquidBounds.t = C3Vector(-3.4028235e38f, -3.4028235e38f, -3.4028235e38f); + } else { + this->GetLiquidBounds(this->m_liquidBounds); + } +} + +// VERIFY: 2c9fa8ff manual +// OFFSET: 0x007C5B20 +void CMapChunk::CullAndBucketChunk() { + if (this->m_liquidBounds.t.x < this->m_liquidBounds.b.x || CWorldScene::FrustumCull(&this->m_liquidBounds)) { + return; + } + + int32_t index = CMapChunk::cornerVertexIndex[CMapChunk::farCornerIndex]; + float camZ = CWorldScene::s_activeWorldView.z; + C3Vector corner(CMapChunk::vertexList[index].x + this->m_topLeftCoords.x, CMapChunk::vertexList[index].y + this->m_topLeftCoords.y, this->m_height[index] + this->m_topLeftCoords.z); + + for (CChunkLiquid* liquid = this->m_liquidChunkLinkList.Head(); liquid; liquid = this->m_liquidChunkLinkList.Next(liquid)) { + CAaBox box; + liquid->GetAaBox(box); + + if (!CWorldScene::FrustumCull(&box)) { + corner.z = (camZ <= liquid->m_height.y ? camZ : liquid->m_height.y) < liquid->m_height.x ? liquid->m_height.x : (camZ > liquid->m_height.y ? liquid->m_height.y : camZ); + CWorldScene::AddToDistanceBucket(liquid, corner); + } + } +} + +// OFFSET: 0x007C5C50 +CMapChunk::CMapChunk() { + this->m_aIndex.x = 0; + this->m_aIndex.y = 0; + this->m_sOffset.x = 0; + this->m_sOffset.y = 0; + this->m_cOffset.x = 0; + this->m_cOffset.y = 0; + this->m_sphere.r = 0.0f; + this->m_sphere.c = C3Vector(0.0f, 0.0f, 0.0f); + this->unk_00B4 = 0; + this->unk_00B8 = 0; + this->m_sortListLink.m_prevLink = nullptr; + this->m_sortListLink.m_next = nullptr; + this->m_doodadDefLinkList.SetLinkOffset(offsetof(CMapBaseObjLink, m_refLink)); + this->m_mapObjDefLinkList.SetLinkOffset(offsetof(CMapBaseObjLink, m_refLink)); + this->m_entityLinkList.SetLinkOffset(offsetof(CMapBaseObjLink, m_refLink)); + this->m_lightLinkList.SetLinkOffset(offsetof(CMapBaseObjLink, m_refLink)); + this->m_soundHandleList.SetLinkOffset(0); + this->m_type |= 4; + this->m_distToCamera = 0.0f; + this->m_flags = static_cast(this->m_flags | MAPOBJ_FLAG_UNPLACED); + this->m_detailDoodadInst = nullptr; + this->m_renderChunk = nullptr; + this->m_chunkInfoBeginPtr = nullptr; + this->m_header = nullptr; + this->m_lowQualityTexMap = nullptr; + this->m_predTexture = nullptr; + this->m_height = nullptr; + this->m_vertexShading = nullptr; + this->m_normals = nullptr; + this->m_shadowMap = nullptr; + this->m_layers = nullptr; + this->m_additionalShadowmap = nullptr; + this->m_mcrfPtr = nullptr; + this->m_liquid = nullptr; + this->m_soundEmitters = nullptr; + this->m_bLoaded = 0; +} + +// OFFSET: 0x007C64B0 +void CMapChunk::Create(SIffChunk* headerChunk, bool a3) { + this->m_chunkInfoBeginPtr = reinterpret_cast(headerChunk); + this->ProcessIffChunks(a3); + + this->m_areaId = this->m_header->m_areaid; + this->m_topLeftCoords.x = 17066.666f - this->m_cOffset.y * 33.333332f; + this->m_topLeftCoords.y = 17066.666f - this->m_cOffset.x * 33.333332f; + this->m_topLeftCoords.z = 0.0f; + this->m_topLeftCoords.z = this->m_header->m_position.z; + this->m_lowQualityTexMap = this->m_header->m_lowQualityTextureMap; + this->m_predTexture = reinterpret_cast(&this->m_header->m_predTex); + + this->CreateBounds(); + this->CreateLiquids(a3); + this->CreateSoundEmitters(a3); + + this->m_flags = static_cast(0); + + if (this->m_header->m_flags & 2) { + this->m_flags = MAPOBJ_FLAG_IMPASSABLE; + } + + CMapArea* area = static_cast(this->m_parentLinkList.Head()->m_ref); + this->CreateRefs(area, reinterpret_cast(this->m_mcrfPtr), this->m_header->m_nDoodadRefs, this->m_header->m_nMapObjRefs); + area->m_mapChunks[16 * this->m_aIndex.y + this->m_aIndex.x] = this; + this->m_flags = static_cast(this->m_flags | MAPOBJ_FLAG_PREPARED); +} + +// VERIFY: 1d9777de manual +// OFFSET: 0x007C65A0 +void CMapChunk::UpdateLights() { + for (CMapBaseObjLink* link = this->m_doodadDefLinkList.Head(); link; link = this->m_doodadDefLinkList.Next(link)) { + link->m_owner->m_flags = static_cast(link->m_owner->m_flags | 0x1); + } + + for (CMapBaseObjLink* link = this->m_entityLinkList.Head(); link; link = this->m_entityLinkList.Next(link)) { + link->m_owner->m_flags = static_cast(link->m_owner->m_flags | 0x1); + } +} + +// VERIFY: 967e03e0 auto +// OFFSET: 0x007D3FE0 +void CMapChunk::RenderPrep() { + if (this->m_renderChunk || (this->Batch(), this->m_renderChunk)) { + float dy = CWorldScene::s_activeWorldView.y - this->m_sphere.c.y; + float dz = CWorldScene::s_activeWorldView.z - this->m_sphere.c.z; + float dx = CWorldScene::s_activeWorldView.x - this->m_sphere.c.x; + + if (CWorld::shadowMipLevel || dx * dx + dz * dz + dy * dy > 603729.0f) { + this->m_renderChunk->unk_000A |= 0x10; + } + + this->m_renderChunk->RenderPrep(); + } + + if ((CWorld::enables & Enable_DetailDoodads) && CWorld::detailDoodadDist > this->m_distToCamera) { + if (!this->m_detailDoodadInst) { + this->CreateDetailDoodads(); + } + + if (this->m_detailDoodadInst) { + CWorldScene::AddChunkToSortList(static_cast(this->m_detailDoodadInst)); + } + } +} + +// OFFSET: 0x007D66D0 +bool CMapChunk::CanMergeChunkLayers(CMapChunk* chunkA, CMapChunk* chunkB) { + chunkB->m_bLoaded = 1; + chunkA->m_bLoaded = 1; + + if (CMap::header.m_flags & 4) { + uint32_t count = chunkB->m_header->m_nLayers; + SMLayer* layerA = chunkA->m_layers; + + for (uint32_t i = 0; i < chunkA->m_header->m_nLayers; i++, layerA++) { + if (layerA->m_flags & 0x4C0) { + return false; + } + + SMLayer* layerB = chunkB->m_layers; + bool found = false; + + for (uint32_t j = 0; j < chunkB->m_header->m_nLayers; j++, layerB++) { + if (layerB->m_flags & 0x4C0) { + return false; + } + + if (layerB->m_textureId == layerA->m_textureId) { + found = true; + break; + } + } + + if (!found) { + count++; + } + } + + return count <= 4; + } + + uint32_t layerCount = chunkA->m_header->m_nLayers; + + if (chunkB->m_header->m_nLayers != layerCount) { + return false; + } + + for (uint32_t i = 0; i < layerCount; i++) { + SMLayer& layerA = chunkA->m_layers[i]; + SMLayer& layerB = chunkB->m_layers[i]; + + if ((layerA.m_flags & 0xC0) || (layerB.m_flags & 0xC0) || layerB.m_textureId != layerA.m_textureId) { + return false; + } + } + + return true; +} diff --git a/WoW/Source/WorldClient/MapChunk.h b/WoW/Source/WorldClient/MapChunk.h index 6cd7b02..f038f04 100644 --- a/WoW/Source/WorldClient/MapChunk.h +++ b/WoW/Source/WorldClient/MapChunk.h @@ -3,6 +3,9 @@ #include #include +#include "Engine/Source/Tempest/C2Vector.h" +#include "WoW/Source/WorldClient/CChunkLiquid.h" +#include "Engine/Source/Base/BitArray.h" #include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/CAaBox.h" #include "Engine/Source/Tempest/CAaSphere.h" @@ -10,6 +13,13 @@ #include "Engine/Source/Storm/H/stpl.h" #include "WoW/Source/WorldClient/MapBaseObj.h" #include "Engine/Source/Tempest/C2iVector.h" +#include "WoW/Source/WorldClient/MapObjGroup.h" + +class CFrustum; +class CMapArea; +class CiRect; +namespace World { class FacetData; } +struct SIffChunk; class CChunkLiquid; class CMapChunk; @@ -18,6 +28,7 @@ class CMapChunkMapObjDefLink; class CMapObjDef; class CMapRenderChunk; class CWSoundEmitter; +class SI2SoundHandle; struct SMChunk; struct SMLayer; struct SMLiquidChunk; @@ -48,18 +59,20 @@ class CMapChunk : public CMapBaseObj { public: static float s_geoToTex; static C3Vector vertexList[145]; + static int32_t farCornerIndex; + static const int32_t cornerVertexIndex[4]; static void Destroy(); static void Initialize(); static void InitializeVertexGrid(); + static bool CanMergeChunkLayers(CMapChunk* chunkA, CMapChunk* chunkB); /* 0024 */ C2iVector m_aIndex; /* 002C */ C2iVector m_sOffset; /* 0034 */ C2iVector m_cOffset; /* 003C */ CAaSphere m_sphere; /* 004C */ CAaBox m_bbox; - /* 0064 */ C3Vector m_bottomRight; - /* 0070 */ C3Vector m_topLeft; + /* 0064 */ CAaBox m_liquidBounds; /* 007C */ C3Vector m_topLeftCoords; /* 0088 */ float m_distToCamera; /* 008C */ CAaBox m_bbox2; @@ -70,18 +83,12 @@ class CMapChunk : public CMapBaseObj { /* 00B4 */ int32_t unk_00B4; /* 00B8 */ int32_t unk_00B8; /* 00BC */ TSLink m_sortListLink; - /* 00C4 */ TSExplicitList m_doodadDefLinkList; - /* 00D0 */ TSExplicitList m_mapObjDefLinkList; - /* 00DC */ int32_t m_tSExplicitListMLinkoffsetDc; - /* 00E0 */ void* m_tSExplicitListPtrE0; - /* 00E4 */ void* m_tSExplicitListPtr2E4; - /* 00E8 */ int32_t m_tSExplicitListMLinkoffsetE8; - /* 00EC */ void* m_tSExplicitListPtrEc; - /* 00F0 */ void* m_tSExplicitListPtr2F0; - /* 00F4 */ int32_t m_tSExplicitListMLinkoffsetF4; - /* 00F8 */ void* m_tSExplicitListPtrF8; - /* 00FC */ void* m_tSExplicitListPtr2Fc; - /* 0100 */ TSExplicitList m_liquidChunkLinkList; + /* 00C4 */ TSList > m_doodadDefLinkList; + /* 00D0 */ TSList > m_mapObjDefLinkList; + /* 00DC */ TSList > m_entityLinkList; + /* 00E8 */ TSList > m_lightLinkList; + /* 00F4 */ TSList > m_soundHandleList; + /* 0100 */ TSExplicitList m_liquidChunkLinkList; /* 010C */ uint8_t* m_chunkInfoBeginPtr; /* 0110 */ SMChunk* m_header; /* 0114 */ uint8_t* m_lowQualityTexMap; @@ -95,12 +102,33 @@ class CMapChunk : public CMapBaseObj { /* 0134 */ uint8_t* m_mcrfPtr; /* 0138 */ SMLiquidChunk* m_liquid; /* 013C */ CWSoundEmitter* m_soundEmitters; - /* 0140 */ int32_t unk_0140; - /* 0144 */ int32_t unk_0144; - /* 0148 */ int32_t unk_0148; - /* 014C */ int32_t unk_014C; - /* 0150 */ int32_t unk_0150; - /* 0154 */ int32_t unk_0154; + /* 0140 */ CBitArray m_deepBits; + /* 014C */ CBitArray m_fishableBits; + + CMapChunk(); + void Create(SIffChunk* headerChunk, bool a3); + void CreateRefs(CMapArea* mapChunk, uint32_t* mcrfPtr, uint32_t doodadRefs, uint32_t mapObjRefs); + void ProcessIffChunks(bool a3); + void CreateBounds(); + void RenderPrep(); + void Batch(); + void AllocRenderChunkAndBatch(); + void CullAndBucketChunk(); + void Purge(); + void UpdateLights(); + void CreateLiquids(bool a3); + void CreateSoundEmitters(bool a3); + void CreateDetailDoodads(); + void GetLiquidBounds(CAaBox& box) const; + bool GetLiquidHeight(const C2Vector& pos, float* height) const; + int16_t CreateIndices(char* buf, int32_t offset); + void CreateIndices(uint16_t* buf, CGxBatch* batch); + void CreateVertices(char* buf, int32_t vertexOffset, const C3Vector& offset); + void CreateVerticesLocal(CGxVertexPN* buf); + void CreateVerticesLocal(CGxVertexPNC* buf); + void CreateVerticesWorld(CGxVertexPN* buf, const C3Vector& offset); + void CreateVerticesWorld(CGxVertexPNC* buf, const C3Vector& offset); + void Intersect(CiRect* rect, CFrustum* frustum, World::FacetData* facets); }; class CWSoundEmitter { diff --git a/WoW/Source/WorldClient/MapChunkLiquid.cpp b/WoW/Source/WorldClient/MapChunkLiquid.cpp index 8f614f8..c5edbdc 100644 --- a/WoW/Source/WorldClient/MapChunkLiquid.cpp +++ b/WoW/Source/WorldClient/MapChunkLiquid.cpp @@ -1 +1,597 @@ #include "MapChunkLiquid.h" +#include +#include "Engine/Source/Gx/CGxBuf.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/CGxPool.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Storm/H/storm.h" +#include "WoW/Source/LiquidSystem/Liquid.h" +#include "WoW/Source/WorldClient/CChunkLiquid.h" +#include "WoW/Source/WorldClient/FrameTime.h" +#include "WoW/Source/WorldClient/LiquidEnvironment.h" +#include "WoW/Source/WorldClient/LiquidGeomFactories.h" +#include "WoW/Source/WorldClient/LiquidWaveManager.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapArea.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/MapChunk.h" +#include "WoW/Source/WorldClient/World.h" + +// STATIC: 0x00AF0A0C +TSList > CMapChunkLiquidBuf::s_freeLists = TSExplicitList(); + +// STATIC: 0x00D2DD2C +Liquid::CWaveManager* CChunkLiquid::s_waveManager; + +// STATIC: 0x00D2DD30 +int32_t CChunkLiquid::s_waveManagerRefs; + +// VERIFY: da69d24e auto +// OFFSET: 0x007CDE10 +void CChunkLiquid::Purge() { + if (this->m_instance) { + this->m_instance->Release(); + this->m_instance = nullptr; + } +} + +// VERIFY: 0d06cb60 auto +// OFFSET: 0x007CDE30 +void CChunkLiquid::UpdateResourcePurging() { + if (this->m_resPurgeTimer >= 2.0f) { + if (this->m_instance) { + this->m_instance->Purge(); + } + + this->m_resPurgeTimer = -1.0f; + } + + if (this->m_resPurgeTimer >= 0.0f) { + this->m_resPurgeTimer = FrameTime::s_tickTimeSec + this->m_resPurgeTimer; + } +} + +// VERIFY: a8377593 manual +// OFFSET: 0x007CDE80 +void CChunkLiquid::GetAaBox(CAaBox& box) const { + box = this->m_owner->m_bbox; + box.b.z = this->m_height.x; + box.t.z = this->m_height.y; +} + +// VERIFY: ab9c5bab auto +// OFFSET: 0x007CDF50 +bool CMapChunkLiquidBuf::Valid() const { + return this->m_vertexBuf && this->m_vertexBuf->unk_001C && this->m_vertexBuf->unk_001D && this->m_indexBuf && this->m_indexBuf->unk_001C && this->m_indexBuf->unk_001D; +} + +// VERIFY: 8f8e7321 manual +// OFFSET: 0x007CDF80 +void CChunkLiquid::CreateVertXY() { + CMapChunk* owner = this->m_owner; + int32_t chunkX = owner->m_cOffset.x; + int32_t chunkY = owner->m_cOffset.y; + int32_t beginY = this->m_tileBegin.y; + int32_t rows = this->m_tileEnd.x - this->m_tileBegin.x + 1; + int32_t beginX = this->m_tileBegin.x; + int32_t cols = this->m_tileEnd.y - beginY + 1; + + C3Vector* vert = this->m_verts; + uint32_t index = 0; + + float stepY = -(17066.666f - chunkX * 33.333332f - (17066.666f - (chunkX + 1) * 33.333332f)) * 0.125f; + + for (int32_t row = 0; row < rows; row++) { + float stepX = 0.125f * -(17066.666f - chunkY * 33.333332f - (17066.666f - (chunkY + 1) * 33.333332f)); + float x = (row + beginX) * stepX; + + for (int32_t col = 0; col < cols; col++) { + vert->x = x; + vert->y = (col + beginY) * stepY; + vert->z = this->m_vertexData->GetHeight(index++) - this->m_owner->m_topLeftCoords.z; + vert++; + } + } +} + +// VERIFY: 6dc3ffdb manual +// OFFSET: 0x007CE0B0 +bool CChunkLiquid::GetHeight(const C2Vector& frac, const C2iVector& tile, float& height) const { + if (tile.x < this->m_tileBegin.y || tile.y < this->m_tileBegin.x || tile.x > this->m_tileEnd.y || tile.y > this->m_tileEnd.x) { + return false; + } + + int32_t cols = this->m_tileEnd.y - this->m_tileBegin.y + 1; + uint32_t index = (tile.x - this->m_tileBegin.y) + cols * (tile.y - this->m_tileBegin.x); + uint32_t next = index + cols; + + float h00 = this->m_vertexData->GetHeight(index); + float h01 = this->m_vertexData->GetHeight(index + 1); + float h10 = this->m_vertexData->GetHeight(next); + float h11 = this->m_vertexData->GetHeight(next + 1); + + float top = h00 + (h01 - h00) * frac.x; + height = top + (h10 + (h11 - h10) * frac.x - top) * frac.y; + + return true; +} + +// VERIFY: cf242b03 manual +// OFFSET: 0x007CE180 +bool CChunkLiquid::GetTileInfo(const C2iVector& tile, int32_t* type, int32_t* flag6, int32_t* flag7) const { + if (!this->m_tiles) { + return false; + } + + const uint8_t* entry = &this->m_tiles[tile.x + 8 * tile.y]; + *type = *entry & 0xF; + *flag6 = (*entry >> 6) & 1; + *flag7 = *entry >> 7; + + return *type != 15 && ((*type ^ (this->m_type - 1)) & 3) == 0; +} + +// VERIFY: b3cebac0 auto +// OFFSET: 0x007CE1F0 +bool CChunkLiquid::TileExists(uint32_t x, uint32_t y) const { + if (this->m_tiles) { + C2iVector tile; + tile.x = x; + tile.y = y; + + int32_t type; + int32_t flag6; + int32_t flag7; + + return this->GetTileInfo(tile, &type, &flag6, &flag7); + } + + if (x < static_cast(this->m_tileBegin.y)) { + return false; + } + + return y >= static_cast(this->m_tileBegin.x) && x < static_cast(this->m_tileEnd.y) && y < static_cast(this->m_tileEnd.x) && this->m_tileBits.Get(x + (this->m_tileEnd.y - this->m_tileBegin.y) * (y - this->m_tileBegin.x) - this->m_tileBegin.y); +} + +// VERIFY: 51ae5fe3 manual +// OFFSET: 0x007CE270 +void CChunkLiquid::BuildIndices(uint16_t* indices, uint16_t baseVertex, CGxBatch* batch) const { + uint16_t* out = indices; + uint16_t vertex = baseVertex; + uint16_t below = baseVertex + this->m_tileEnd.y - this->m_tileBegin.y + 1; + uint16_t last = 0; + int32_t inStrip = 0; + + for (int32_t row = this->m_tileBegin.x; row < this->m_tileEnd.x; row++) { + uint16_t belowNext = below + 1; + + for (int32_t col = this->m_tileBegin.y; col < this->m_tileEnd.y; col++) { + if (this->TileExists(col, row)) { + if (!inStrip) { + *out++ = vertex; + *out++ = vertex; + *out++ = below; + inStrip = 1; + } + + *out++ = vertex + 1; + last = belowNext; + *out++ = belowNext; + } else if (inStrip) { + inStrip = 0; + *out++ = last; + } + + below++; + belowNext++; + vertex++; + } + + if (inStrip) { + *out++ = last; + inStrip = 0; + } + + below++; + vertex++; + } + + batch->m_count += out - indices; +} + +// VERIFY: a801c800 manual +// OFFSET: 0x007CE390 +void CChunkLiquid::BuildVertices(const C44Matrix* transform, uint32_t stride, C3Vector** position, C3Vector** normal, uint32_t** color, C2Vector** texCoord1, C2Vector** texCoord0) const { + DepthCoordTable* depthCoords = CMap::GetDepthCoordTable(this->m_type); + bool customTexCoords = this->unk_0008 == 1; + uint32_t index = 0; + int32_t rows = this->m_tileEnd.x - this->m_tileBegin.x + 1; + const C3Vector* vert = this->m_verts; + int32_t cols = this->m_tileEnd.y - this->m_tileBegin.y + 1; + + for (int32_t row = 0; row < rows; row++) { + for (int32_t col = 0; col < cols; col++) { + C3Vector pos = *vert * *transform; + + if (*position) { + **position = pos; + *position = reinterpret_cast(reinterpret_cast(*position) + stride); + } + + if (*normal) { + (*normal)->x = 0.0f; + (*normal)->y = 0.0f; + (*normal)->z = 1.0f; + *normal = reinterpret_cast(reinterpret_cast(*normal) + stride); + } + + if (*color) { + **color = 0xFFFFFFFF; + *color = reinterpret_cast(reinterpret_cast(*color) + stride); + } + + if (*texCoord1) { + if (depthCoords) { + uint8_t depth = this->m_vertexData->GetDepth(index); + (*texCoord1)->x = 0.0f; + (*texCoord1)->y = (*depthCoords)[depth]; + } else { + (*texCoord1)->x = 0.0f; + (*texCoord1)->y = 0.0f; + } + + *texCoord1 = reinterpret_cast(reinterpret_cast(*texCoord1) + stride); + } + + if (*texCoord0) { + if (customTexCoords) { + const uint16_t* uv = this->m_vertexData->GetTexCoord(index); + (*texCoord0)->x = 0.01171875f * uv[0]; + (*texCoord0)->y = uv[1] * 0.01171875f; + } else { + (*texCoord0)->x = 0.060000002f * pos.x; + (*texCoord0)->y = pos.y * 0.060000002f; + } + + *texCoord0 = reinterpret_cast(reinterpret_cast(*texCoord0) + stride); + } + + vert++; + index++; + } + } +} + +// VERIFY: ef77a0e3 manual +// OFFSET: 0x007CE520 +CAaSphere* CChunkLiquid::GetSphere() { + if (this->m_sphere.r < 0.0f) { + CAaBox& bbox = this->m_owner->m_bbox; + float top = this->m_height.y; + + this->m_sphere.c = C3Vector((bbox.b.x + bbox.t.x) * 0.5f, (bbox.b.y + bbox.t.y) * 0.5f, 0.5f * (this->m_height.x + top)); + + float dy = bbox.t.y - this->m_sphere.c.y; + float dx = bbox.t.x - this->m_sphere.c.x; + this->m_sphere.r = sqrtf(dx * dx + dy * dy + (top - this->m_sphere.c.z) * (top - this->m_sphere.c.z)); + } + + return &this->m_sphere; +} + +// INLINED +CMapChunkLiquidBuf::CMapChunkLiquidBuf() { + this->unk_0000 = 0; + this->m_flushTimer = 0.0f; + this->m_vertexDataSize = 0; + this->m_indexDataSize = 0; + this->m_vertexBuf = nullptr; + this->m_indexBuf = nullptr; + this->m_vertexPool = nullptr; + this->m_indexPool = nullptr; +} + +// VERIFY: 1c787e83 manual +// OFFSET: 0x007CED30 +CMapChunkLiquidBuf::~CMapChunkLiquidBuf() { + if (this->m_vertexBuf) { + CGxBuf* buf = this->m_vertexBuf; + + if (buf != g_theGxDevicePtr->BufStream(buf->m_pool->m_target, 0, 0)) { + g_theGxDevicePtr->BufDestroy(buf); + } + } + + if (this->m_indexBuf) { + CGxBuf* buf = this->m_indexBuf; + + if (buf != g_theGxDevicePtr->BufStream(buf->m_pool->m_target, 0, 0)) { + g_theGxDevicePtr->BufDestroy(buf); + } + } + + if (this->m_vertexPool) { + g_theGxDevicePtr->PoolDestroy(this->m_vertexPool); + } + + if (this->m_indexPool) { + g_theGxDevicePtr->PoolDestroy(this->m_indexPool); + } +} + +// VERIFY: 61df3203 manual +// OFFSET: 0x007CEE10 +CChunkLiquid::CChunkLiquid() { + this->m_tileBegin.x = 0; + this->m_tileBegin.y = 0; + this->m_tileEnd.x = 0; + this->m_tileEnd.y = 0; + this->m_resPurgeTimer = 0.0f; + this->m_type = 0; + this->m_sphere.r = -1.0f; + this->m_vertexData = nullptr; + this->m_tiles = nullptr; + this->m_instance = nullptr; + this->m_owner = nullptr; + + if (CChunkLiquid::s_waveManagerRefs == 0) { + Liquid::CWaveManager* waveManager = Liquid::CWaveManager::Create(); + CChunkLiquid::s_waveManagerRefs++; + CChunkLiquid::s_waveManager = waveManager; + } else { + CChunkLiquid::s_waveManager->AddRef(); + CChunkLiquid::s_waveManagerRefs++; + } +} + +// VERIFY: 10ec5edc manual +// OFFSET: 0x007CEEF0 +CChunkLiquid::~CChunkLiquid() { + if (CChunkLiquid::s_waveManagerRefs) { + CChunkLiquid::s_waveManager->Release(); + CChunkLiquid::s_waveManagerRefs--; + } + + if (this->m_vertexData) { + delete this->m_vertexData; + this->m_vertexData = nullptr; + } +} + +// VERIFY: 86f2eb61 manual +// OFFSET: 0x007CEFD0 +CMapChunkLiquidBuf* CMapChunkLiquidBuf::CreateBuf(uint32_t vertexCount, uint32_t indexCount, int32_t a3, uint32_t vertexSize) { + void* mem = SMemAlloc(sizeof(CMapChunkLiquidBuf), __FILE__, __LINE__, 0); + + if (!mem) { + return nullptr; + } + + CMapChunkLiquidBuf* buf = new (mem) CMapChunkLiquidBuf(); + + CGxPool* vertexPool = g_theGxDevicePtr->PoolCreate(GxPoolTarget_Vertex, GxPoolUsage_Dynamic, vertexSize * vertexCount, GxPoolHintBit_Unk3, "CChunkBuf_Vertex"); + CGxPool* indexPool = g_theGxDevicePtr->PoolCreate(GxPoolTarget_Index, GxPoolUsage_Dynamic, 2 * indexCount, GxPoolHintBit_Unk3, "CChunkBuf_Index"); + CGxBuf* vertexBuf = g_theGxDevicePtr->BufCreate(vertexPool, vertexSize, vertexCount, 0); + CGxBuf* indexBuf = g_theGxDevicePtr->BufCreate(indexPool, 2, indexCount, 0); + + buf->m_flushTimer = 0.0f; + buf->m_indexDataSize = 2 * indexCount; + buf->unk_0000 = a3; + buf->m_vertexPool = vertexPool; + buf->m_vertexDataSize = vertexSize * vertexCount; + buf->m_indexBuf = indexBuf; + buf->m_indexPool = indexPool; + buf->m_vertexBuf = vertexBuf; + + return buf; +} + +// INLINED +CMapChunkLiquid::CMapChunkLiquid() { +} + +// VERIFY: 3a225d8f manual +// OFFSET: 0x007CF0C0 +CMapChunkLiquid::~CMapChunkLiquid() { +} + +// VERIFY: ac9225a1 manual +// OFFSET: 0x007CF140 +CMapChunkLiquidBuf* CMapChunkLiquidBuf::Alloc(uint32_t vertexCount, uint32_t indexCount, int32_t a3, EGxVertexBufferFormat format) { + uint32_t vertexSize = GxVertexSize(format); + + for (CMapChunkLiquid* list = CMapChunkLiquidBuf::s_freeLists.Head(); list; list = CMapChunkLiquidBuf::s_freeLists.Next(list)) { + if (list->m_vertexDataSize == vertexCount * vertexSize && list->m_indexDataSize == 2 * indexCount && list->m_liquids.Head()) { + CMapChunkLiquidBuf* buf = list->m_liquids.Head(); + buf->m_link.Unlink(); + buf->m_vertexBuf->unk_001C = 0; + buf->m_indexBuf->unk_001C = 0; + + return buf; + } + } + + return CMapChunkLiquidBuf::CreateBuf(vertexCount, indexCount, a3, vertexSize); +} + +// VERIFY: e344dd13 manual +// OFFSET: 0x007CF200 +void CChunkLiquid::Batch() { + static TSGrowableArray s_batch; + + s_batch.Clear(); + s_batch.ReallocData(32); + + if (CMap::enableChunkBatching) { + CMapArea* area = static_cast(this->m_owner->m_parentLinkList.Head()->m_ref); + CMapChunk** row = &area->m_mapChunks[16 * (this->m_owner->m_aIndex.y & ~1) + (this->m_owner->m_aIndex.x & ~1)]; + + for (int32_t y = 0; y < 2; y++) { + CMapChunk** slot = row; + + for (int32_t x = 0; x < 2; x++) { + CMapChunk* chunk = *slot; + + for (CChunkLiquid* liquid = chunk->m_liquidChunkLinkList.Head(); liquid; liquid = chunk->m_liquidChunkLinkList.Next(liquid)) { + if (liquid->m_type == this->m_type) { + s_batch.New(liquid); + } + } + + slot++; + } + + row += 16; + } + } else { + CChunkLiquid* self = this; + s_batch.New(self); + } + + CAaBox bounds; + Liquid::CChunkGeomFactory* factory = Liquid::CChunkGeomFactory::Create(s_batch); + + for (uint32_t i = 0; i < s_batch.m_count; i++) { + CChunkLiquid* liquid = s_batch.m_data[i]; + + C2Vector minXY = C2Vector(liquid->m_topLeftCoords); + minXY.x = minXY.x + liquid->m_tileBegin.y * 4.1666665f; + minXY.y = minXY.y + 4.1666665f * liquid->m_tileBegin.x; + C2Vector maxXY = C2Vector(liquid->m_topLeftCoords); + maxXY.x = maxXY.x + liquid->m_tileEnd.y * 4.1666665f; + maxXY.y = maxXY.y + 4.1666665f * liquid->m_tileEnd.x; + + CAaBox box(C3Vector(minXY.x, minXY.y, liquid->m_height.x), C3Vector(maxXY.x, maxXY.y, liquid->m_height.y)); + + if (i) { + bounds.t.z = bounds.t.z > box.t.z ? bounds.t.z : box.t.z; + bounds.t.y = bounds.t.y > box.t.y ? bounds.t.y : box.t.y; + bounds.t.x = bounds.t.x > box.t.x ? bounds.t.x : box.t.x; + bounds.b.z = bounds.b.z < box.b.z ? bounds.b.z : box.b.z; + bounds.b.y = bounds.b.y < box.b.y ? bounds.b.y : box.b.y; + bounds.b.x = bounds.b.x < box.b.x ? bounds.b.x : box.b.x; + } else { + bounds = box; + } + } + + C44Matrix transform; + transform.Translate(s_batch.m_data[0]->m_topLeftCoords); + + Liquid::CInstance* instance = Liquid::CInstance::Create(); + instance->m_material = Liquid::CMaterialBank::GetMaterial(this->m_type); + instance->m_settings = Liquid::CSettingsBank::GetSettings(this->m_type); + Liquid::CClientEnvironment* environment = Liquid::CClientEnvironment::Create(0); + instance->m_geomFactory = factory; + instance->m_environment = environment; + instance->m_waveManager = CChunkLiquid::s_waveManager; + CChunkLiquid::s_waveManager->AddRef(); + instance->m_transform = transform; + + float dx = bounds.t.x - bounds.b.x; + float dy = bounds.t.y - bounds.b.y; + float dz = bounds.t.z - bounds.b.z; + instance->m_bounds.c.x = (bounds.b.x + bounds.t.x) * 0.5f; + instance->m_bounds.c.y = (bounds.t.y + bounds.b.y) * 0.5f; + instance->m_bounds.c.z = (bounds.b.z + bounds.t.z) * 0.5f; + instance->m_bounds.r = 0.5f * sqrtf(dy * dy + dx * dx + dz * dz); + + for (uint32_t i = 0; i < s_batch.m_count; i++) { + s_batch.m_data[i]->m_instance = instance; + instance->AddRef(); + } + + instance->Release(); +} + +// VERIFY: 0f8f770e manual +// OFFSET: 0x007CF790 +void CMapChunkLiquidBuf::Free(CMapChunkLiquidBuf* buf) { + CMapChunkLiquid* list; + + for (list = CMapChunkLiquidBuf::s_freeLists.Head(); list; list = CMapChunkLiquidBuf::s_freeLists.Next(list)) { + if (list->m_vertexDataSize == buf->m_vertexDataSize && list->m_indexDataSize == buf->m_indexDataSize) { + break; + } + } + + if (!list) { + void* mem = SMemAlloc(sizeof(CMapChunkLiquid), __FILE__, __LINE__, 0); + list = mem ? new (mem) CMapChunkLiquid() : nullptr; + list->m_vertexDataSize = buf->m_vertexDataSize; + list->m_indexDataSize = buf->m_indexDataSize; + CMapChunkLiquidBuf::s_freeLists.LinkToTail(list); + } + + list->m_liquids.LinkToTail(buf); +} + +// VERIFY: 9b76b6fd manual +// OFFSET: 0x007CF840 +void CMapChunkLiquidBuf::Update(float elapsed) { + CMapChunkLiquid* list = CMapChunkLiquidBuf::s_freeLists.Head(); + + while (list) { + CMapChunkLiquid* nextList = CMapChunkLiquidBuf::s_freeLists.Next(list); + + CMapChunkLiquidBuf* buf = list->m_liquids.Head(); + + while (buf) { + CMapChunkLiquidBuf* next = list->m_liquids.Next(buf); + + if (!(buf->unk_0000 & 0x1)) { + buf->m_flushTimer += elapsed; + + if (buf->m_flushTimer > 5.0f) { + buf->m_link.Unlink(); + delete buf; + } + } + + buf = next; + } + + if (!list->m_liquids.Head()) { + list->m_link.Unlink(); + delete list; + } + + list = nextList; + } +} + +// VERIFY: 3c1420a2 auto +// OFFSET: 0x007CF9A0 +void CChunkLiquid::RenderPrep() { + if (!this->m_instance) { + this->Batch(); + } + + if (this->m_instance && this->m_instance->m_waveManager) { + static_cast(this->m_instance->m_waveManager)->PrepareRenderTransform(World::GetCamPos()); + } + + this->m_resPurgeTimer = 0.0f; +} + +// VERIFY: 87f4391a manual +// OFFSET: 0x007CF9E0 +void CMap::FreeAllChunkLiquids() { + CMapChunkLiquid* list = CMapChunkLiquidBuf::s_freeLists.Head(); + + while (list) { + CMapChunkLiquid* nextList = CMapChunkLiquidBuf::s_freeLists.Next(list); + CMapChunkLiquidBuf* buf = list->m_liquids.Head(); + + while (buf) { + CMapChunkLiquidBuf* next = list->m_liquids.Next(buf); + buf->m_link.Unlink(); + delete buf; + buf = next; + } + + list->m_link.Unlink(); + delete list; + list = nextList; + } +} diff --git a/WoW/Source/WorldClient/MapChunkLiquid.h b/WoW/Source/WorldClient/MapChunkLiquid.h index fd788b1..fa64d8d 100644 --- a/WoW/Source/WorldClient/MapChunkLiquid.h +++ b/WoW/Source/WorldClient/MapChunkLiquid.h @@ -3,7 +3,7 @@ #include #include -#include +#include "Engine/Source/Gx/Types.h" #include "Engine/Source/Storm/H/stpl.h" struct CGxBuf; @@ -13,6 +13,13 @@ class CMapChunkLiquid; class CMapChunkLiquidBuf { public: + static TSList > s_freeLists; + + static CMapChunkLiquidBuf* Alloc(uint32_t vertexCount, uint32_t indexCount, int32_t a3, EGxVertexBufferFormat format); + static CMapChunkLiquidBuf* CreateBuf(uint32_t vertexCount, uint32_t indexCount, int32_t a3, uint32_t vertexSize); + static void Free(CMapChunkLiquidBuf* buf); + static void Update(float elapsed); + /* 0000 */ uint32_t unk_0000; /* 0004 */ float m_flushTimer; /* 0008 */ uint32_t m_vertexDataSize; @@ -21,16 +28,22 @@ class CMapChunkLiquidBuf { /* 0014 */ CGxBuf* m_indexBuf; /* 0018 */ CGxPool* m_vertexPool; /* 001C */ CGxPool* m_indexPool; - /* 0020 */ uint32_t unk_0020; - /* 0024 */ uint32_t unk_0024; + /* 0020 */ TSLink m_link; + + CMapChunkLiquidBuf(); + ~CMapChunkLiquidBuf(); + bool Valid() const; }; class CMapChunkLiquid { public: /* 0000 */ uint32_t m_vertexDataSize; /* 0004 */ uint32_t m_indexDataSize; - /* 0008 */ TSExplicitList m_liquids; + /* 0008 */ TSExplicitList m_liquids; /* 0014 */ TSLink m_link; + + CMapChunkLiquid(); + ~CMapChunkLiquid(); }; #endif diff --git a/WoW/Source/WorldClient/MapDoodadDef.cpp b/WoW/Source/WorldClient/MapDoodadDef.cpp index 7c0b670..224036e 100644 --- a/WoW/Source/WorldClient/MapDoodadDef.cpp +++ b/WoW/Source/WorldClient/MapDoodadDef.cpp @@ -1 +1,133 @@ #include "MapDoodadDef.h" +#include "Engine/Source/Model2/M2Data.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Model2/M2Model.h" +#include "Engine/Source/Model2/M2Shared.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/CWorldMath.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapChunk.h" +#include "WoW/Source/WorldClient/MapLight.h" +#include "WoW/Source/WorldClient/MapObjDef.h" +#include "WoW/Source/WorldClient/Types.h" +#include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/WorldView.h" + +// OFFSET: 0x007B4FA0 +void CMapDoodadDef::ExtendBounds(CMapBaseObj* parent) { + if (parent->m_type & 0x4) { + CMapChunk* chunk = static_cast(parent); + chunk->m_bbox.t.z = this->m_bbox.t.z >= chunk->m_bbox.t.z ? this->m_bbox.t.z : chunk->m_bbox.t.z; + } else if (parent->m_type & 0x10) { + CMapObjDefGroup* group = static_cast(parent); + group->m_bbox |= this->m_bbox; + + CMapBaseObjLink* link = group->m_parentLinkList.Head(); + static_cast(link->m_ref)->m_bbox |= this->m_bbox; + } +} + +// VERIFY: 3ef424d0 manual +// OFFSET: 0x007B55E0 +void CMapDoodadDef::ExtendChunkBounds() { + for (CMapBaseObjLink* link = this->m_parentLinkList.Head(); link; link = this->m_parentLinkList.Next(link)) { + this->ExtendBounds(link->m_ref); + } +} + +// VERIFY: 2d4db79f manual +// OFFSET: 0x007BDB10 +void CMapDoodadDef::UpdateBounds() { + CAaBox box; + CAaSphere sphere; + sphere.r = 0.0f; + CAaBox collisionBox; + + if (this->m_model) { + box = this->m_model->GetBoundingBox(); + sphere = this->m_model->GetBoundingSphere(); + + CM2Model* model = this->m_model; + + if (!(model->m_shared->m_flags & 0x1)) { + model->WaitForLoad(nullptr); + } + + collisionBox = model->m_shared->m_data->m_collisionBounds.m_extent; + } + + this->m_sphere.c = sphere.c * this->m_mat; + this->m_sphere.r = this->m_scale * sphere.r; + + if (box.b.x <= box.t.x || box.b.y <= box.t.y || box.b.z <= box.t.z) { + CWorldMath::TransformAABox(this->m_mat, box, this->m_bbox); + } else { + this->m_bbox.b = this->m_position; + this->m_bbox.t = this->m_position; + } + + CWorldMath::TransformAABox(this->m_mat, collisionBox, this->m_bboxDoodadDef); + + C3Vector center; + center.x = (collisionBox.t.x + collisionBox.b.x) * 0.5f; + center.y = (collisionBox.t.y + collisionBox.b.y) * 0.5f; + center.z = 0.5f * (collisionBox.t.z + collisionBox.b.z); + this->m_vec2 = center * this->m_mat; + + float size = this->m_bbox.t.x - this->m_bbox.b.x; + + if (this->m_bbox.t.y - this->m_bbox.b.y >= size) { + size = this->m_bbox.t.y - this->m_bbox.b.y; + } + + if (this->m_bbox.t.z - this->m_bbox.b.z >= size) { + size = this->m_bbox.t.z - this->m_bbox.b.z; + } + + uint8_t fadeLevel; + + for (fadeLevel = 0; fadeLevel < 4; fadeLevel++) { + if (size <= CWorldView::s_fadeSize[fadeLevel]) { + break; + } + } + + this->m_fadeLevel = fadeLevel; + + if (!(CWorld::enables & Enable_NoAnimation) && fadeLevel < 3) { + this->m_model->m_fFlags &= ~0x40u; + } +} + +// VERIFY: 6f062999 manual +// OFFSET: 0x007C1150 +void CMapDoodadDef::SelectLights(CM2Lighting* lighting) { + if (this->m_flags & 0x2) { + lighting->AddAmbient(C3Vector(this->m_m2AmbietColor)); + lighting->AddDiffuse(C3Vector(this->m_m2DiffuseColor), CMapStaticEntity::s_interiorSunDir); + } else { + CM2Light* light = &CMap::s_mapLight->m_light; + lighting->AddAmbient(light->m_ambColor); + + float scale = this->m_diffuseLightScale; + C3Vector dir = light->m_dir; + lighting->AddDiffuse(C3Vector(light->m_dirColor.x * scale, light->m_dirColor.y * scale, scale * light->m_dirColor.z), dir); + } + + DayNight::DNInfo* dayNight = DayNight::GetInfo(); + + if (this->m_flags & 0x8000) { + lighting->SetFog(C3Vector(dayNight->m_fogInterior.m_color), dayNight->m_fogInterior.m_start, dayNight->m_fogInterior.m_end, dayNight->m_fogInterior.m_density); + } else { + lighting->SetFog(C3Vector(dayNight->m_fog.m_color), dayNight->m_fog.m_start, dayNight->m_fog.m_end, dayNight->m_fog.m_density); + } + + if (this->m_flags & 0x2) { + lighting->m_flags |= 0x8; + } else { + lighting->m_flags &= ~0x8; + } +} diff --git a/WoW/Source/WorldClient/MapDoodadDef.h b/WoW/Source/WorldClient/MapDoodadDef.h index b0064f7..01a0f26 100644 --- a/WoW/Source/WorldClient/MapDoodadDef.h +++ b/WoW/Source/WorldClient/MapDoodadDef.h @@ -3,47 +3,27 @@ #include #include -#include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/C44Matrix.h" #include "Engine/Source/Tempest/CAaBox.h" -#include "Engine/Source/Tempest/CAaSphere.h" -#include "Engine/Source/Tempest/CImVector.h" #include "Engine/Source/Storm/H/stpl.h" -#include "WoW/Source/WorldClient/MapBaseObj.h" +#include "WoW/Source/WorldClient/MapLoad.h" -class CM2Model; +class CM2Lighting; +class CMapBaseObj; +class CMapObjDef; -class CMapDoodadDef : public CMapBaseObj { +class CMapDoodadDef : public CMapStaticEntity { public: - /* 0024 */ uint8_t m_fadeLevel; - /* 0025 */ uint8_t m_culled; - /* 0026 */ uint8_t unk_0026; - /* 0027 */ uint8_t unk_0027; - /* 0028 */ uint32_t m_unkFlags28; - /* 002C */ int32_t m_unkCounter; - /* 0030 */ float m_distToCameraPlane; - /* 0034 */ CM2Model* m_model; - /* 0038 */ CAaSphere m_sphere; - /* 0048 */ CAaBox m_bboxStaticEntity; - /* 0060 */ C3Vector m_vec2; - /* 006C */ C3Vector m_position; - /* 0078 */ float m_scale; - /* 007C */ int32_t unk_007C; - /* 0080 */ int32_t unk_0080; - /* 0084 */ CImVector m_m2AmbietColor; - /* 0088 */ CImVector m_m2DiffuseColor; - /* 008C */ float m_diffuseLightScale; /* 0090 */ uint32_t m_uniqueId; /* 0094 */ void* unk_0094; /* 0098 */ void* unk_0098; /* 009C */ void* m_prevMapDoodadDef09c; /* 00A0 */ void* m_nextMapDoodadDef0a0; /* 00A4 */ int32_t unk_00A4; - /* 00A8 */ TSLink m_doodadDefLink; + /* 00A8 */ TSLink m_doodadDefLink; /* 00B0 */ int32_t m_cullPassStamp; /* 00B4 */ int32_t unk_00B4; - /* 00B8 */ int32_t unk_00B8; - /* 00BC */ int32_t unk_00BC; + /* 00B8 */ uint64_t unk_00B8; /* 00C0 */ CAaBox m_bboxDoodadDef; /* 00D8 */ C44Matrix m_mat; /* 0118 */ C44Matrix m_identity; @@ -53,6 +33,15 @@ class CMapDoodadDef : public CMapBaseObj { /* 0164 */ int32_t unk_0164; /* 0168 */ int32_t unk_0168; /* 016C */ int32_t unk_016C; + + virtual void SelectLights(CM2Lighting* lighting); + virtual void SelectUnderwater(CM2Lighting* lighting); + virtual bool QueryInteriorLighting(CMapObjDef* mapObjDef, int32_t groupNum, uint16_t* faceIndex, C3Vector* pos); + + CMapDoodadDef(); + void UpdateBounds(); + void ExtendBounds(CMapBaseObj* parent); + void ExtendChunkBounds(); }; #endif diff --git a/WoW/Source/WorldClient/MapEntity.cpp b/WoW/Source/WorldClient/MapEntity.cpp index 4b59a88..0a5a287 100644 --- a/WoW/Source/WorldClient/MapEntity.cpp +++ b/WoW/Source/WorldClient/MapEntity.cpp @@ -1 +1,128 @@ #include "MapEntity.h" +#include "WoW/Source/WorldClient/MapLight.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" + +// STATIC: 0x00ADEEBC +float CMapStaticEntity::s_characterAmbient = 1.0f; + +// STATIC: 0x00AEEDF0 +C3Vector CMapStaticEntity::s_interiorSunDir = C3Vector(-0.30822f, -0.30822f, -0.9f); + +// VERIFY: b7c5af51 manual +// OFFSET: 0x007A0B50 +CMapStaticEntity::CMapStaticEntity() { + this->m_sphere.c = C3Vector(0.0f, 0.0f, 0.0f); + this->m_sphere.r = 0.0f; + this->m_scale = 0.0f; + this->unk_007C &= 0xFFFE4000; + this->m_distanceToCamera = 0.0f; + this->unk_0080 = 0.0f; + this->m_model = nullptr; + this->m_diffuseLightScale = 0.0f; + this->m_unkCounter = 0; + this->unk_0028 = 0; + this->unk_0029 = 0; + this->unk_002A = 0; + this->unk_002B = 0; + this->m_fadeLevel = 0; + this->m_vec2 = C3Vector(0.0f, 0.0f, 0.0f); + this->unk_0025 = 2; +} + +// VERIFY: eeb1ad46 manual +// OFFSET: 0x007A0FE0 +CMapEntity::CMapEntity() { + this->unk_00A8 = C3Vector(0.0f, 0.0f, 0.0f); + this->m_func = nullptr; + this->m_funcParam = nullptr; + this->m_funcParam64 = 0; + this->m_funcParam32 = 0; + this->unk_00B4 = 0; + this->m_groundType = 0; + this->unk_00BC = 0; + this->unk_00BE = 0; + this->unk_00A4 = 0xFFFFFFFF; + this->unk_00C0.b = 0xFF; + this->unk_00C0.g = 0x00; + this->unk_00C0.r = 0x00; + this->unk_00C0.a = 0x00; + this->unk_00C4 = 0.0f; + this->m_type |= 0x20; +} + +// VERIFY: d83b5d3e manual +// OFFSET: 0x007C1730 +void CMapStaticEntity::SelectLights(CM2Lighting* lighting) { + if (CWorld::farFog > this->m_distanceToCamera) { + if (this->m_flags & MAPOBJ_FLAG_UNPLACED) { + this->m_flags = static_cast(this->m_flags & ~MAPOBJ_FLAG_UNPLACED); + } + + C3Vector diffuse = { 0.0f, 0.0f, 0.0f }; + C3Vector dir = { 0.0f, 0.0f, 0.0f }; + C3Vector ambient = { 0.0f, 0.0f, 0.0f }; + + if ((this->m_flags & 4) != 0) { + CM2Light* light = &CMap::s_mapLight->m_light; + + diffuse.x = light->m_dirColor.x * this->m_diffuseLightScale; + diffuse.y = light->m_dirColor.y * this->m_diffuseLightScale; + diffuse.z = light->m_dirColor.z * this->m_diffuseLightScale; + + dir = light->m_dir; + + ambient = C3Vector(this->m_m2AmbietColor); + } else { + diffuse = C3Vector(this->m_m2DiffuseColor); + diffuse.x *= this->m_diffuseLightScale; + diffuse.y *= this->m_diffuseLightScale; + diffuse.z *= this->m_diffuseLightScale; + + ambient = C3Vector(this->m_m2AmbietColor); + + dir = CMapStaticEntity::s_interiorSunDir; + + if ((this->unk_007C & 0x1000) != 0) { + DayNight::DNInfo* dayNight = DayNight::GetInfo(); + float blend = this->m_m2DiffuseColor.a / 255.0f; + + dir.x += (dayNight->m_light1.m_dir.x - dir.x) * blend; + dir.y += (dayNight->m_light1.m_dir.y - dir.y) * blend; + dir.z += (dayNight->m_light1.m_dir.z - dir.z) * blend; + + dir.Normalize(); + } + } + + if ((this->unk_007C & 0x8000) != 0) { + ambient.x *= CMapStaticEntity::s_characterAmbient; + ambient.y *= CMapStaticEntity::s_characterAmbient; + ambient.z *= CMapStaticEntity::s_characterAmbient; + + if (ambient.x >= 1.0f) { + ambient.x = 1.0f; + } + + if (ambient.y >= 1.0f) { + ambient.y = 1.0f; + } + + if (ambient.z >= 1.0f) { + ambient.z = 1.0f; + } + } + + lighting->AddAmbient(ambient); + lighting->AddDiffuse(diffuse, dir); + } + + DayNight::DNInfo* dayNight = DayNight::GetInfo(); + + if ((this->m_flags & 0x8000) != 0) { + lighting->SetFog(C3Vector(dayNight->m_fogInterior.m_color), dayNight->m_fogInterior.m_start, dayNight->m_fogInterior.m_end, dayNight->m_fogInterior.m_density); + } else { + lighting->SetFog(C3Vector(dayNight->m_fog.m_color), dayNight->m_fog.m_start, dayNight->m_fog.m_end, dayNight->m_fog.m_density); + } +} diff --git a/WoW/Source/WorldClient/MapEntity.h b/WoW/Source/WorldClient/MapEntity.h index 59a4cc2..373193e 100644 --- a/WoW/Source/WorldClient/MapEntity.h +++ b/WoW/Source/WorldClient/MapEntity.h @@ -3,9 +3,13 @@ #include #include +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CImVector.h" #include "Engine/Source/Storm/H/stpl.h" #include "WoW/Source/WorldClient/MapLoad.h" +class CMapObjDef; + class CMapEntity : public CMapStaticEntity { public: /* 0090 */ uint32_t (__cdecl* m_func )(uint32_t, uint32_t, uint64_t, uint32_t); @@ -13,15 +17,20 @@ class CMapEntity : public CMapStaticEntity { /* 0098 */ uint64_t m_funcParam64; /* 00A0 */ uint32_t m_funcParam32; /* 00A4 */ uint32_t unk_00A4; - /* 00A8 */ uint32_t unk_00A8; - /* 00AC */ uint32_t unk_00AC; - /* 00B0 */ uint32_t unk_00B0; + /* 00A8 */ C3Vector unk_00A8; /* 00B4 */ uint32_t unk_00B4; - /* 00B8 */ uint32_t unk_00B8; - /* 00BC */ uint32_t unk_00BC; - /* 00C0 */ uint32_t unk_00C0; - /* 00C4 */ uint32_t unk_00C4; + /* 00B8 */ int32_t m_groundType; + /* 00BC */ uint16_t unk_00BC; + /* 00BE */ uint16_t unk_00BE; + /* 00C0 */ CImVector unk_00C0; + /* 00C4 */ float unk_00C4; /* 00C8 */ TSLink m_sortEntryLink; + + virtual bool QueryInteriorLighting(CMapObjDef* mapObjDef, int32_t groupNum, uint16_t* faceIndex, C3Vector* pos); + + CMapEntity(); + void QueryLiquidSounds(uint32_t* results, uint32_t* distance); + bool UpdateMapObjLiquid(); }; #endif diff --git a/WoW/Source/WorldClient/MapLight.h b/WoW/Source/WorldClient/MapLight.h index 93024e6..14ba29a 100644 --- a/WoW/Source/WorldClient/MapLight.h +++ b/WoW/Source/WorldClient/MapLight.h @@ -3,32 +3,27 @@ #include #include +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Tempest/CAaSphere.h" #include "Engine/Source/Model2/M2Light.h" #include "WoW/Source/WorldClient/MapBaseObj.h" class CMapLight : public CMapBaseObj { public: - /* 0024 */ float unk_0024; - /* 0028 */ float unk_0028; - /* 002C */ float unk_002C; - /* 0030 */ float unk_0030; - /* 0034 */ float unk_0034; - /* 0038 */ float unk_0038; - /* 003C */ float unk_003C; - /* 0040 */ float unk_0040; - /* 0044 */ float unk_0044; - /* 0048 */ float unk_0048; - /* 004C */ float unk_004C; - /* 0050 */ float unk_0050; - /* 0054 */ float unk_0054; + /* 0024 */ C3Vector m_position; + /* 0030 */ CAaBox m_bbox; + /* 0048 */ CAaSphere m_sphere; /* 0058 */ CM2Light m_light; /* 00C4 */ float unk_00C4; - /* 00C8 */ float unk_00C8; + /* 00C8 */ float m_radius; /* 00CC */ float unk_00CC; /* 00D0 */ uint8_t unk_00D0; /* 00D1 */ uint8_t unk_00D1; /* 00D2 */ uint8_t unk_00D2; /* 00D3 */ uint8_t unk_00D3; + + CMapLight(); }; class CMapCacheLight { diff --git a/WoW/Source/WorldClient/MapLoad.cpp b/WoW/Source/WorldClient/MapLoad.cpp index 6430e58..c33110d 100644 --- a/WoW/Source/WorldClient/MapLoad.cpp +++ b/WoW/Source/WorldClient/MapLoad.cpp @@ -1 +1,25 @@ #include "MapLoad.h" +#include "Engine/Source/Model2/M2Model.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" + +// VERIFY: 1ce54c55 manual +// OFFSET: 0x00780CD0 +void CMapStaticEntity::ModelLightingCallback(CM2Model* model, CM2Lighting* lighting, void* userArg) { + CMapStaticEntity* entity = reinterpret_cast(userArg); + + lighting->m_flags |= 0x10; + auto dayNight = DayNight::GetInfo(); + if (entity) { + lighting->SetFog(C3Vector(dayNight->m_fog.m_color), dayNight->m_fog.m_start, dayNight->m_fog.m_end, dayNight->m_fog.m_density); + entity->SelectLights(lighting); + entity->SelectUnderwater(lighting); + if ((entity->m_flags & MAPOBJ_FLAG_INTERIOR) != 0) { + lighting->m_flags |= 8; + return; + } + } else { + lighting->AddAmbient(C3Vector(dayNight->m_light1.m_ambient)); + lighting->AddDiffuse(C3Vector(dayNight->m_light1.m_diffuse), dayNight->m_light1.m_dir); + } + lighting->m_flags &= ~8u; +} diff --git a/WoW/Source/WorldClient/MapLoad.h b/WoW/Source/WorldClient/MapLoad.h index a2909a6..92b41bc 100644 --- a/WoW/Source/WorldClient/MapLoad.h +++ b/WoW/Source/WorldClient/MapLoad.h @@ -9,15 +9,26 @@ #include "Engine/Source/Tempest/CImVector.h" #include "WoW/Source/WorldClient/MapBaseObj.h" +class CM2Lighting; +class CMapObjDef; + class CM2Model; class CMapStaticEntity : public CMapBaseObj { public: - /* 0024 */ uint8_t unk_0024; + static float s_characterAmbient; + static C3Vector s_interiorSunDir; + + static void ModelLightingCallback(CM2Model* model, CM2Lighting* lighting, void* userArg); + + /* 0024 */ uint8_t m_fadeLevel; /* 0025 */ uint8_t unk_0025; /* 0026 */ uint8_t unk_0026; /* 0027 */ uint8_t unk_0027; - /* 0028 */ uint32_t m_unkFlags28; + /* 0028 */ uint8_t unk_0028; + /* 0029 */ uint8_t unk_0029; + /* 002A */ uint8_t unk_002A; + /* 002B */ uint8_t unk_002B; /* 002C */ int32_t m_unkCounter; /* 0030 */ float m_distanceToCamera; /* 0034 */ CM2Model* m_model; @@ -26,11 +37,17 @@ class CMapStaticEntity : public CMapBaseObj { /* 0060 */ C3Vector m_vec2; /* 006C */ C3Vector m_position; /* 0078 */ float m_scale; - /* 007C */ int32_t unk_007C; - /* 0080 */ int32_t unk_0080; + /* 007C */ uint32_t unk_007C; + /* 0080 */ float unk_0080; /* 0084 */ CImVector m_m2AmbietColor; /* 0088 */ CImVector m_m2DiffuseColor; - /* 008C */ float unk_008C; + /* 008C */ float m_diffuseLightScale; + + virtual void SelectLights(CM2Lighting* lighting); + virtual void SelectUnderwater(CM2Lighting* lighting); + virtual bool QueryInteriorLighting(CMapObjDef* mapObjDef, int32_t groupNum, uint16_t* faceIndex, C3Vector* pos) = 0; + + CMapStaticEntity(); }; #endif diff --git a/WoW/Source/WorldClient/MapLowDetail.cpp b/WoW/Source/WorldClient/MapLowDetail.cpp index 3d29776..7aac47a 100644 --- a/WoW/Source/WorldClient/MapLowDetail.cpp +++ b/WoW/Source/WorldClient/MapLowDetail.cpp @@ -1 +1,173 @@ #include "MapLowDetail.h" +#include +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "Engine/Source/Storm/H/storm.h" +#include "WoW/Source/WorldClient/FVBBList.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapArea.h" +#include "WoW/Source/WorldClient/MapObj.h" +#include "WoW/Source/WorldClient/MapObjDef.h" + +// VERIFY: pending +// VERIFY: a9a4e286 manual +// OFFSET: 0x007C06E0 +CMapAreaLow::CMapAreaLow() { + this->m_index.x = 0; + this->m_index.y = 0; + this->unk_004C = 0; + this->unk_0050 = 0; + this->m_indexCount = 0; + this->m_block = nullptr; + this->unk_0058 = 0; + this->m_heights = nullptr; + this->m_holes = nullptr; +} + +// OFFSET: 0x007CBFE0 +void CMapLowDetail::Purge() { + for (uint32_t i = 0; i < 4096; i++) { + CMapAreaLow* areaLow = this->m_areas[i]; + + if (areaLow) { + if (areaLow->m_block) { + CMap::s_vbbList.Release(areaLow->m_block); + } + + this->m_areas[i]->m_block = nullptr; + } + } +} + +// VERIFY: pending +// VERIFY: ba76c774 manual +// OFFSET: 0x007CC310 +void CMapLowDetail::Load(const char* mapPath, const char* mapName) { + char fileName[256]; + SFileImpl* file; + + SStrPrintf(fileName, sizeof(fileName), "%s\\%s.wdl", mapPath, mapName); + SFileOpenFile(fileName, &file); + + if (!file) { + return; + } + + uint32_t size = SFileGetFileSize(file, nullptr); + this->m_data = static_cast(SMemAlloc(size, __FILE__, __LINE__, 0)); + SFileReadFile(file, this->m_data, size, nullptr, nullptr, 0); + SFileCloseFile(file); + + uint32_t* chunk = reinterpret_cast(this->m_data + reinterpret_cast(this->m_data)[1] + 8); + char* data = this->m_data + reinterpret_cast(this->m_data)[1] + 16; + + if (chunk[0] == 0x4D574D4F) { + this->m_mapObjNames = data; + uint32_t* mwid = reinterpret_cast(data + chunk[1]); + this->m_mapObjNameOffsets = mwid + 2; + uint32_t* modf = reinterpret_cast(reinterpret_cast(mwid) + mwid[1] + 8); + this->m_mapObjDefs = reinterpret_cast(modf + 2); + this->m_mapObjDefCount = modf[1] >> 6; + data = reinterpret_cast(modf + 2) + modf[1] + 8; + } + + this->m_areaOffsets = reinterpret_cast(data); + + uint32_t index = 0; + + for (uint32_t y = 0; y < 1024; y += 16) { + for (uint32_t x = 0; x < 1024; x += 16) { + if (this->m_areaOffsets[index]) { + char* mare = this->m_data + this->m_areaOffsets[index]; + CMapAreaLow* area = CMap::AllocAreaLow(); + this->m_areas[index] = area; + area->m_heights = reinterpret_cast(mare + 8); + uint32_t* next = reinterpret_cast(mare + reinterpret_cast(mare)[1] + 8); + + int32_t minHeight = 100000; + int32_t maxHeight = -100000; + + for (uint32_t i = 0; i < 545; i++) { + int32_t height = area->m_heights[i]; + + if (height > maxHeight) { + maxHeight = height; + } + + if (height < minHeight) { + minHeight = height; + } + } + + area->m_index.x = x >> 4; + area->m_index.y = y >> 4; + + float top = 17066.666f - static_cast(y) * 33.333332f; + area->m_topLeft.x = top; + area->m_bbox.t.x = top; + + float left = static_cast(x) * -33.333332f + 17066.666f; + area->m_topLeft.y = left; + area->m_bbox.t.y = left; + area->m_bbox.t.z = static_cast(maxHeight); + area->m_bbox.b.x = top - 533.33331f; + area->m_bbox.b.y = left - 533.33331f; + area->m_bbox.b.z = static_cast(minHeight); + + area->m_sphere.c.x = (area->m_bbox.t.x + area->m_bbox.b.x) * 0.5f; + area->m_sphere.c.y = (area->m_bbox.b.y + area->m_bbox.t.y) * 0.5f; + area->m_sphere.c.z = 0.5f * (area->m_bbox.b.z + area->m_bbox.t.z); + float dz = area->m_bbox.t.z - area->m_sphere.c.z; + float dy = area->m_bbox.t.y - area->m_sphere.c.y; + float dx = area->m_bbox.t.x - area->m_sphere.c.x; + area->m_sphere.r = sqrtf(dx * dx + dy * dy + dz * dz); + + if (next[0] == 0x4D41484F) { + uint16_t* holes = reinterpret_cast(next + 2); + area->m_holes = holes; + area->m_indexCount = 0; + + for (uint32_t row = 0; row < 16; row++) { + for (uint32_t bit = 0; bit < 16; bit++) { + if (!((1 << bit) & holes[row])) { + area->m_indexCount += 12; + } + } + } + } else { + area->m_holes = nullptr; + area->m_indexCount = 3072; + } + } + + index++; + } + } + + C3Vector center(17066.666f, 17066.666f, 0.0f); + this->m_mapObjDefList.SetCount(this->m_mapObjDefCount); + + for (uint32_t i = 0; i < this->m_mapObjDefCount; i++) { + SMMapObjDef* def = &this->m_mapObjDefs[i]; + CMapObjDef* mapObjDef = CMap::CreateMapObjDef(this->m_mapObjNames + this->m_mapObjNameOffsets[def->m_nameId], def, ¢er, false); + + if (!CMap::s_isStreamingMode) { + CMapObj* mapObj = mapObjDef->m_owner; + + if (!mapObj->m_isGroupLoaded) { + mapObj->WaitLoad(); + } + + uint32_t groupCount = mapObj->m_groupInfoCount; + + for (uint32_t group = 0; group < groupCount; group++) { + if (!mapObj->IsGroupLoaded(group)) { + mapObj->ReadGroup(group); + mapObj->WaitLoadGroup(group); + } + } + } + + mapObjDef->m_refCount++; + this->m_mapObjDefList.m_data[i] = mapObjDef; + } +} diff --git a/WoW/Source/WorldClient/MapLowDetail.h b/WoW/Source/WorldClient/MapLowDetail.h index 61e69b5..aeb18c1 100644 --- a/WoW/Source/WorldClient/MapLowDetail.h +++ b/WoW/Source/WorldClient/MapLowDetail.h @@ -1,4 +1,49 @@ #ifndef WOW_SOURCE_WORLDCLIENT_MAPLOWDETAIL_H #define WOW_SOURCE_WORLDCLIENT_MAPLOWDETAIL_H +#include +#include +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C2iVector.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "WoW/Source/WorldClient/FVBBList.h" + +class CMapObjDef; +struct SMMapObjDef; + +class CMapAreaLow { + public: + /* 0000 */ uint32_t m_objectIndex; + /* 0004 */ CAaBox m_bbox; + /* 001C */ CAaSphere m_sphere; + /* 002C */ C3Vector m_topLeft; + /* 0038 */ C2iVector m_index; + /* 0040 */ uint32_t m_indexCount; + /* 0044 */ int16_t* m_heights; + /* 0048 */ uint16_t* m_holes; + /* 004C */ uint32_t unk_004C; + /* 0050 */ uint32_t unk_0050; + /* 0054 */ FVBBList::Block* m_block; + /* 0058 */ uint32_t unk_0058; + + CMapAreaLow(); +}; + +class CMapLowDetail { + public: + /* 0000 */ char* m_data; + /* 0004 */ char* m_mapObjNames; + /* 0008 */ uint32_t* m_mapObjNameOffsets; + /* 000C */ SMMapObjDef* m_mapObjDefs; + /* 0010 */ uint32_t m_mapObjDefCount; + /* 0014 */ uint32_t* m_areaOffsets; + /* 0018 */ CMapAreaLow* m_areas [4096]; + /* 4018 */ TSGrowableArray m_mapObjDefList; + + void Load(const char* mapPath, const char* mapName); + void Purge(); +}; + #endif diff --git a/WoW/Source/WorldClient/MapMem.cpp b/WoW/Source/WorldClient/MapMem.cpp index b31c1ca..99e8da2 100644 --- a/WoW/Source/WorldClient/MapMem.cpp +++ b/WoW/Source/WorldClient/MapMem.cpp @@ -1 +1,8 @@ #include "MapMem.h" +#include "WoW/Source/WorldClient/MapArea.h" + +// VERIFY: bffb685d manual +// OFFSET: 0x007BFE40 +void* CMapArea::AllocAsyncLoadBuffer(int32_t fileSize) { + return SMemAlloc(fileSize, __FILE__, __LINE__, 0); +} diff --git a/WoW/Source/WorldClient/MapObj.cpp b/WoW/Source/WorldClient/MapObj.cpp index ba8fcbd..c53e651 100644 --- a/WoW/Source/WorldClient/MapObj.cpp +++ b/WoW/Source/WorldClient/MapObj.cpp @@ -1,9 +1,46 @@ #include "MapObj.h" +#include "Engine/Source/Tempest/C2Vector.h" #include "WoW/Source/WorldClient/Map.h" #include "Engine/Source/Base/ObjectAlloc.h" #include "Engine/Source/ShaderEffect/ShaderEffectManager.h" #include "Engine/Source/Storm/H/storm.h" #include "WoW/Source/WorldClient/MapObjGroup.h" +#include +#include "Engine/Source/Services/AsyncFile.h" +#include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Tempest/C3Segment.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/C4Plane.h" +#include "Engine/Source/Tempest/C4Vector.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "Engine/Source/Tempest/CFrustum.h" +#include "Engine/Source/Gx/Types.h" +#include "Engine/Source/Gx/CGxBuf.h" +#include "Engine/Source/Gx/CGxLight.h" +#include "Engine/Source/Gx/CGxTex.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/MapDoodadDef.h" +#include "WoW/Source/WorldClient/MapObjDef.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/CWorldMath.h" +#include "WoW/Source/WorldClient/MapChunk.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" +#include "Engine/Source/Base/CDataAllocator.h" +#include "WoW/Source/WorldClient/Types.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/BlizzardCore/Source/Packages/Mopaq/SFile.h" +#include "Engine/Source/DayNight/DayNight.h" +#include "WoW/Source/WorldClient/MapRenderChunk.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/Gxu/GxuLight.h" + +// STATIC: 0x00AEE008 +uint32_t CMapObj::s_unk_AEE008 = 2; // STATIC: 0x00ADFF50 static int32_t unk_ADFF50 = 2048; @@ -29,17 +66,122 @@ int32_t CMapObj::bPoolsDirty; // STATIC: 0x00D1C41C CMapObjDef* s_stampedMapObjDef; -// STATIC: 0x00D1C420 -CMapObjDef* s_curMapObjDef; - // STATIC: 0x00D1C424 uint32_t s_portalStamp; -// VERIFY: 2a9f3c91 manual +// STATIC: 0x00D1C428 +static HASHKEY_NONE s_mapObjHashKey; + +// VERIFY: bd88db75 auto +// OFFSET: 0x007A7B00 +static void __stdcall WriteLiquidVertex(CMapObjGroup* group, const C44Matrix& matrix, const C3Vector& vertex, const SMOLiquidVert* liquidVert, const CImVector* color, int32_t magma, float texCoord1U, int32_t* unused, uint32_t stride, C3Vector** position, C3Vector** normal, CImVector** vertexColor, C2Vector** texCoord0, C2Vector** texCoord1) { + DepthCoordTable* depthTable = CMap::GetDepthCoordTable(group->m_liquidType); + + if (*position) { + **position = vertex * matrix; + *position = reinterpret_cast(reinterpret_cast(*position) + stride); + } + + if (*normal) { + (*normal)->x = 0.0f; + (*normal)->y = 0.0f; + (*normal)->z = 1.0f; + *normal = reinterpret_cast(reinterpret_cast(*normal) + stride); + } + + if (*vertexColor) { + CImVector value; + + if (g_theGxDevicePtr->Caps().m_colorFormat == GxCF_rgba) { + value.b = color->r; + value.g = color->g; + value.r = color->b; + value.a = color->a; + } else { + value = *color; + } + + **vertexColor = value; + *vertexColor = reinterpret_cast(reinterpret_cast(*vertexColor) + stride); + } + + if (*texCoord0) { + if (magma) { + (*texCoord0)->x = liquidVert->m_magmaVert.m_s * 0.00390625f; + (*texCoord0)->y = liquidVert->m_magmaVert.m_t * 0.00390625f; + } else { + C44Matrix rotation = matrix; + rotation.d0 = 0.0f; + rotation.d1 = 0.0f; + rotation.d2 = 0.0f; + rotation.d3 = 1.0f; + + C3Vector local; + local.x = vertex.x - group->m_liquidCorner.x; + local.y = vertex.y - group->m_liquidCorner.y; + local.z = vertex.z - group->m_liquidCorner.z; + + C2Vector uv(local * rotation); + (*texCoord0)->x = uv.x * 0.24f; + (*texCoord0)->y = 0.24f * uv.y; + } + + *texCoord0 = reinterpret_cast(reinterpret_cast(*texCoord0) + stride); + } + + if (*texCoord1) { + float depth = depthTable ? (*depthTable)[liquidVert->m_waterVert.m_flow1] : 0.0f; + (*texCoord1)->x = texCoord1U; + (*texCoord1)->y = depth; + *texCoord1 = reinterpret_cast(reinterpret_cast(*texCoord1) + stride); + } +} + +// VERIFY: f7f5970e auto +// OFFSET: 0x007AE1A0 +void CMapObj::ReadGroup(uint32_t index) { + this->ReadGroup(index, false); +} + +// VERIFY: d92cbc0c auto +// OFFSET: 0x007AE1C0 +void CMapObj::WaitLoad() { + while (this->m_asyncObject) { + AsyncFileReadWait(this->m_asyncObject); + } +} + +// VERIFY: c6f5dc77 manual +// OFFSET: 0x007AE4C0 +int32_t CMapObj::IsGroupLoaded(int32_t index) { + if (this->m_isGroupLoaded) { + return this->m_mapObjGroupArray[index]->m_unkLoadedFlag & 1; + } + + return 0; +} + +// VERIFY: 8c25cdca manual +// OFFSET: 0x007AE7E0 +bool CMapObj::TestBounds(CAaBox& box) { + return this->m_isGroupLoaded && this->m_bbox.Intersects(box); +} + +// VERIFY: 0d13eb33 auto +// OFFSET: 0x007AEAB0 +void CMapObj::WaitLoadGroup(uint32_t index) { + CMapObjGroup* group = this->m_mapObjGroupArray[index]; + + while (group->m_asyncObjPtr) { + AsyncFileReadWait(group->m_asyncObjPtr); + } +} + +// VERIFY: baf76f3a manual // OFFSET: 0x007AFEE0 void CMapObj::Initialize() { s_portalStamp = 0; - s_curMapObjDef = nullptr; + CWorldScene::s_curMapObjDef = nullptr; s_stampedMapObjDef = nullptr; unk_ADFF50 = 2048; unk_D1BEE8.Reserve(1024, 0); @@ -103,3 +245,297 @@ void CMapObj::ClearCache(int32_t a1) { mapObj = next; } } + + + + + + + +// OFFSET: 0x007A8800 +void CMapObj::SetRenderModeLight() { + CGxLight light; + light.m_flags |= 0x1; + + float d = 1.0f / sqrtf(3.0f); + light.m_dir = { d, d, d }; + light.m_ambientColor = { 0.33f, 0.33f, 0.33f }; + light.m_dirColor = { 0.75f, 0.75f, 0.75f }; + light.m_specularColor = { 0.0f, 0.0f, 0.0f }; + light.m_constantAttenuation = 0.0f; + light.m_linearAttenuation = 0.0f; + light.m_quadraticAttenuation = 0.0f; + + C3Vector origin = { 0.0f, 0.0f, 0.0f }; + g_theGxDevicePtr->LightSet(0, light, origin); + g_theGxDevicePtr->LightEnable(0, 1); + + for (int i = 1; i < 4; i++) + g_theGxDevicePtr->LightEnable(i, 0); + + GxRsSet(GxRs_Lighting, 1); +} + + + + + + +// OFFSET: 0x007AE140 +uint32_t CMapObj::CreateWmoIgnoreFlags(uint32_t a1) { + uint32_t result = 0xEE; + if ((a1 & 0x80u) != 0) + return 0x2CA; + if ((a1 & 0x10) != 0) + result = 0xC6; + if ((a1 & 0x20) != 0) + result &= 0xDBu; + if ((a1 & 0x40) == 0) + result &= ~2u; + if ((a1 & 0x4000) == 0) + result &= ~0x40u; + if ((a1 & 0x1000000) != 0) { + if ((a1 & 0x40000000) != 0) + return 0xFFFFFCDB; + else + return result | 0x100; + } + return result; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + +// VERIFY: 81665f7f manual +// OFFSET: 0x0079BBF0 +void CMapObj::QueryLiquidSounds(uint32_t groupIndex, uint32_t fromGroup, uint32_t depth, uint32_t* minDepth, const C3Vector& pos, uint32_t* results) { + if (depth > CMapObj::s_unk_AEE008) { + return; + } + + if (!this->GetGroupInfo(groupIndex)->m_boundingBox.ContainsPoint(pos)) { + return; + } + + CMapObjGroup* group = this->GetGroup(groupIndex, false); + + if (!group) { + return; + } + + if ((group->m_flags & 0x8) && depth < *minDepth) { + *minDepth = depth; + } + + group->QueryLiquidSounds(pos, results); + + for (uint32_t i = 0; i < static_cast(group->m_portalCount); i++) { + uint32_t neighbor = this->m_portalRefList[group->m_portalStart + i].m_groupIndex; + + if (neighbor != 0xFFFF && neighbor != fromGroup) { + this->QueryLiquidSounds(neighbor, groupIndex, depth + 1, minDepth, pos, results); + } + } +} + +// VERIFY: 3402cfca auto +// OFFSET: 0x007A6D70 +SMOMaterial* CMapObj::GetLiquidMaterial(int32_t groupIndex) { + CMapObjGroup* group = this->GetGroup(groupIndex, false); + + if (!group) { + return nullptr; + } + + return &this->m_materialList[static_cast(group->m_luquidMaterialId)]; +} + +// VERIFY: 64db3118 auto +// OFFSET: 0x007AEB90 +bool CMapObj::QueryLiquidStatus(uint32_t ignoreFlags, const C3Vector& pos, int32_t* liquidType, float* level) { + for (uint32_t i = 0; i < static_cast(this->m_groupInfoCount); i++) { + if (!this->m_isGroupLoaded) { + continue; + } + + SMOGroupInfo* info = &this->m_groupInfo[i]; + + if (!info || (ignoreFlags & info->m_flags) || !info->m_boundingBox.ContainsPoint(pos)) { + continue; + } + + CMapObjGroup* group = this->m_mapObjGroupArray[i]; + + if ((group->m_unkLoadedFlag & 0x1) && group->QueryLiquid(pos, liquidType, level)) { + return true; + } + } + + return false; +} + +// VERIFY: bad0310c manual +// OFFSET: 0x007A7920 +void CMapObj::BuildLiquidIndices(CMapObjGroup* group, const TSGrowableArray* sharedVerts, uint16_t** indices, uint16_t baseVertex) { + int32_t rowStride = group->m_liquidVerts.x + sharedVerts[0].m_count; + uint16_t lastIndex = 0; + int32_t inStrip = 0; + uint32_t sharedRow = 0; + int32_t outRow = 0; + + for (int32_t y = 0; y < group->m_liquidTiles.y; y++) { + int32_t duplicate = 0; + + if (sharedRow < sharedVerts[1].m_count && sharedVerts[1].m_data[sharedRow] == y) { + duplicate = 1; + sharedRow++; + } + + int32_t nextRow = baseVertex + rowStride; + + for (int32_t r = duplicate + 1; r; r--) { + uint16_t top = outRow * rowStride + baseVertex; + uint16_t bottom = nextRow + top; + uint32_t sharedCol = 0; + + for (int32_t x = 0; x < group->m_liquidTiles.x; x++) { + uint8_t flags = group->m_liquidTileList[group->m_liquidTiles.x * y + x].m_flags; + int32_t visible = (flags & 0xF) != 0xF && (flags & 0x80) ? 1 : 0; + int32_t duplicateCol = 0; + + if (sharedCol < sharedVerts[0].m_count && sharedVerts[0].m_data[sharedCol] == x) { + duplicateCol = 1; + sharedCol++; + } + + uint16_t bottomNext = bottom + 1; + + for (int32_t c = duplicateCol + 1; c; c--) { + if (visible) { + if (!inStrip) { + inStrip = 1; + *(*indices)++ = top; + *(*indices)++ = top; + *(*indices)++ = bottom; + } + + *(*indices)++ = top + 1; + **indices = bottomNext; + lastIndex = bottomNext; + (*indices)++; + } else if (inStrip) { + **indices = lastIndex; + inStrip = 0; + (*indices)++; + } + + top++; + bottom++; + bottomNext++; + } + } + + if (inStrip) { + *(*indices)++ = lastIndex; + inStrip = 0; + } + + outRow++; + } + } +} + +// VERIFY: 5e3b42d7 manual +// OFFSET: 0x007A7CC0 +uint16_t CMapObj::BuildLiquidVerts(CMapObjGroup* group, const C44Matrix& matrix, const CImVector* color, int32_t magma, float texCoord1U, const TSGrowableArray* sharedVerts, uint32_t stride, C3Vector** position, C3Vector** normal, CImVector** vertexColor, C2Vector** texCoord0, C2Vector** texCoord1) { + float y = group->m_liquidCorner.y; + uint32_t sharedRow = 0; + + for (int32_t row = 0; row < group->m_liquidVerts.y; row++) { + int32_t duplicateRow = 0; + + if (sharedRow < sharedVerts[1].m_count && sharedVerts[1].m_data[sharedRow] == row) { + duplicateRow = 1; + sharedRow++; + } + + for (int32_t r = duplicateRow + 1; r; r--) { + float x = group->m_liquidCorner.x; + uint32_t sharedCol = 0; + + for (int32_t col = 0; col < group->m_liquidVerts.x; col++) { + const SMOLiquidVert* liquidVert = &group->m_liquidVertexList[group->m_liquidVerts.x * row + col]; + + C3Vector vertex; + vertex.x = x; + vertex.y = y; + vertex.z = liquidVert->m_waterVert.m_height; + + int32_t duplicateCol = 0; + + if (sharedCol < sharedVerts[0].m_count && sharedVerts[0].m_data[sharedCol] == col) { + duplicateCol = 1; + sharedCol++; + } + + int32_t unused; + + for (int32_t c = duplicateCol + 1; c; c--) { + WriteLiquidVertex(group, matrix, vertex, liquidVert, color, magma, texCoord1U, &unused, stride, position, normal, vertexColor, texCoord0, texCoord1); + } + + x += 4.1666665f; + } + } + + y += 4.1666665f; + } + + return (group->m_liquidVerts.x + sharedVerts[0].m_count) * (sharedVerts[1].m_count + group->m_liquidVerts.y); +} + +// VERIFY: 3da651a7 auto +// OFFSET: 0x007B0CC0 +CMapObj* CMapObj::Create(char* fileName) { + uint32_t hashval = SStrHash(fileName, 0, 0); + CMapObj* mapObj = CMapObj::mapObjHash.Ptr(hashval, s_mapObjHashKey); + + if (mapObj) { + mapObj->m_refCount++; + return mapObj; + } + + mapObj = CMap::AllocMapObj(); + + if (!mapObj->Read(fileName)) { + DebugLog("CMapObj::Create(): mapObj->Read(\"%s\") failed", fileName); + } + + CMapObj::mapObjHash.Insert(mapObj, hashval, s_mapObjHashKey); + mapObj->m_refCount = 1; + + return mapObj; +} diff --git a/WoW/Source/WorldClient/MapObj.h b/WoW/Source/WorldClient/MapObj.h index 98b9b50..fbd894b 100644 --- a/WoW/Source/WorldClient/MapObj.h +++ b/WoW/Source/WorldClient/MapObj.h @@ -9,6 +9,13 @@ #include "Engine/Source/Tempest/CAaBox.h" #include "Engine/Source/Tempest/CImVector.h" #include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/CWFrustum.h" + +class C2Vector; +class C44Matrix; +class CFrustum; +class CMapObjDefGroup; +struct C3Segment; class C3Vector; class C4Plane; @@ -21,6 +28,7 @@ struct SMODoodadDef; struct SMODoodadSet; struct SMOFog; struct SMOGroupInfo; +class CAaSphere; struct SMOHeader; struct SMOLight; struct SMOMaterial; @@ -39,17 +47,36 @@ class CMapObjIndex { /* 0000 */ int32_t m_objectIndex; }; +typedef void (*RENDER_CALLBACK)(uint32_t, void*); + class CMapObj : public CMapObjIndex, public TSHashObject { public: + static TSList > s_asyncLoadQueue; + static TSList > s_unk_ADFC58; static TSHashTable mapObjHash; static int32_t bPoolsDirty; static uint32_t* occluderHeap; static CShaderEffect* s_classicShaders[7]; static CShaderEffect* s_unifiedShaders[7]; + static uint32_t s_unk_AEE008; static void ClearCache(int32_t a1); static void Destroy(); static void Initialize(); + static uint32_t CreateWmoIgnoreFlags(uint32_t a1); + static void PrepareUpdate(); + static CMapObj* Create(char* fileName); + static void PostloadCallback(void* arg); + static void RenderGroupCollidable(CMapObj* mapObj, CMapObjGroup* mapObjGroup, uint32_t a3); + static void RenderGroupCollidableFaces(CMapObjGroup* mapObjGroup); + static void ExteriorRender(CMapObj* mapObj, CMapObjGroup* mapObjGroup, uint32_t a3); + static void InteriorRender(CMapObj* mapObj, CMapObjGroup* mapObjGroup, uint32_t a3); + static void UnifiedRender(CMapObj* mapObj, CMapObjGroup* mapObjGroup, uint32_t a3); + static void InvokeGroupRenderCallback(CMapObj* mapObj, uint32_t groupNum); + static void SetGroupRenderCallback(RENDER_CALLBACK callback, void* param); + static void SetRenderModeLight(); + static void SetEmissiveColor(CImVector color); + static void SelectWorldShaders(); /* 001C */ char m_wmoName [260]; /* 0120 */ SMOHeader* m_header; @@ -88,8 +115,7 @@ class CMapObj : public CMapObjIndex, public TSHashObject /* 01A4 */ int32_t unk_01A4; /* 01A8 */ CAaBox m_bbox; /* 01C0 */ float m_distToCamera; - /* 01C4 */ int32_t unk_01C4; - /* 01C8 */ int32_t unk_01C8; + /* 01C4 */ TSLink m_asyncLoadLink; /* 01CC */ void* m_pWmoData; /* 01D0 */ int32_t m_wmoFileSize; /* 01D4 */ int32_t m_refCount; @@ -100,6 +126,43 @@ class CMapObj : public CMapObjIndex, public TSHashObject /* 01E8 */ TSExplicitList m_mapObjGroupList; /* 01F4 */ int32_t m_mapObjGroupCount; /* 01F8 */ CMapObjGroup* m_mapObjGroupArray [512]; + + CMapObj(); + bool Read(char* fileName); + void ReadGroup(uint32_t index); + void ReadGroup(uint32_t index, bool preLoad); + void Load(); + void WaitLoad(); + void WaitLoadGroup(uint32_t index); + void GetBounds(CAaBox* box); + void GetGroupBounds(CAaSphere* sphere, int32_t index); + void GetGroupBounds(CAaBox* box, int32_t index); + int32_t GetDoodadSet(uint16_t index); + uint32_t GetGroupFlags(int32_t index); + SMOGroupInfo* GetGroupInfo(int32_t index); + int32_t IsGroupLoaded(int32_t index); + char* GetGroupName(int32_t index); + CMapObjGroup* GetGroup(int32_t index, bool a3); + void RenderGroup(int32_t groupIndex, C44Matrix& matrix, TSExplicitList* frustumList); + void CreateRefs(CMapObjGroup* mapObjGroup, CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup); + void CreateMaterial(uint8_t texture); + void CreateMaterials(); + bool TestBounds(CAaBox& box); + bool TestGroupBounds(C3Vector& start, C3Vector& end, uint32_t groupNum); + bool TestGroupBounds(C3Vector& point, uint32_t groupNum); + bool TestGroupBounds(CAaBox& box, uint32_t groupNum, int32_t a3); + bool GroupBoundingBoxIntersectsSphere(C3Vector& pos, uint32_t groupNum, float radius); + bool GetTris(CFrustum* frustum, uint32_t flags, uint32_t a4, CMapObjDef* mapObjDef); + bool VectorIntersectPortal(C3Segment& seg, float* t, int* outGroups, int useSphereTest); + bool VectorIntersectPortals(uint32_t groupNum, C3Segment& seg, float* t, int32_t* outGroups); + float CalcPortalFarthestDistance(SMOPortal* portal); + bool Intersect(C3Vector& start, C3Vector& end, float* distance, uint32_t flags, uint32_t ignoreFlags, uint32_t groupNum, int32_t* hitIndex); + SMOMaterial* GetLiquidMaterial(int32_t groupIndex); + bool QueryLiquidStatus(uint32_t ignoreFlags, const C3Vector& pos, int32_t* liquidType, float* level); + void QueryLiquidSounds(uint32_t groupIndex, uint32_t fromGroup, uint32_t depth, uint32_t* minDepth, const C3Vector& pos, uint32_t* results); + uint16_t BuildLiquidVerts(CMapObjGroup* group, const C44Matrix& matrix, const CImVector* color, int32_t magma, float texCoord1U, const TSGrowableArray* sharedVerts, uint32_t stride, C3Vector** position, C3Vector** normal, CImVector** vertexColor, C2Vector** texCoord0, C2Vector** texCoord1); + void BuildLiquidIndices(CMapObjGroup* group, const TSGrowableArray* sharedVerts, uint16_t** indices, uint16_t baseVertex); + uint16_t ClipGroupGeometry(CMapObjGroup* group, const C44Matrix& matrix, const CImVector* color, int32_t magma, float texCoord1U, uint32_t stride, C3Vector** position, C3Vector** normal, CImVector** vertexColor, C2Vector** texCoord0, C2Vector** texCoord1, uint16_t** indices, uint16_t baseVertex); }; struct SMOBatch { @@ -184,8 +247,8 @@ struct SMOVisibleBlock { /* 0002 */ uint16_t m_count; }; -extern CMapObjDef* s_curMapObjDef; extern uint32_t s_portalStamp; extern CMapObjDef* s_stampedMapObjDef; #endif + diff --git a/WoW/Source/WorldClient/MapObjDef.cpp b/WoW/Source/WorldClient/MapObjDef.cpp index b0b75d4..a2b22ff 100644 --- a/WoW/Source/WorldClient/MapObjDef.cpp +++ b/WoW/Source/WorldClient/MapObjDef.cpp @@ -1 +1,40 @@ #include "MapObjDef.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapLight.h" +#include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/CWorldMath.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" + +// VERIFY: 18be6c93 auto +// OFFSET: 0x007B3990 +bool CMapObjDef::TestAABox(C3Vector& start, C3Vector& end) { + return CWorldMath::VectorIntersectAABox2(this->m_bbox, start, end); +} + +// VERIFY: 7ed1c1b6 manual +// OFFSET: 0x007B3F30 +void CMapObjDefGroup::SelectLights(CM2Lighting* lighting) { + lighting->AddLight(&CMap::s_mapLight->m_light); + + if (CWorld::farFog > this->m_distanceToCamera) { + if (this->m_flags & MAPOBJ_FLAG_UNPLACED) { + this->m_flags = static_cast(this->m_flags & ~MAPOBJ_FLAG_UNPLACED); + } + + for (CMapBaseObjLink* link = this->m_lightLinkList.Head(); link; link = this->m_lightLinkList.Next(link)) { + CMapLight* light = static_cast(link->m_owner); + + if (light->m_light.m_visible && !light->unk_00D1) { + lighting->AddLight(&light->m_light); + } + } + } + + DayNight::DNInfo* dayNight = DayNight::GetInfo(); + + if (this->m_flags & 0x8000) { + lighting->SetFog(C3Vector(dayNight->m_fogInterior.m_color), dayNight->m_fogInterior.m_start, dayNight->m_fogInterior.m_end, dayNight->m_fogInterior.m_density); + } else { + lighting->SetFog(C3Vector(dayNight->m_fog.m_color), dayNight->m_fog.m_start, dayNight->m_fog.m_end, dayNight->m_fog.m_density); + } +} diff --git a/WoW/Source/WorldClient/MapObjDef.h b/WoW/Source/WorldClient/MapObjDef.h index 349c1c2..fc3a669 100644 --- a/WoW/Source/WorldClient/MapObjDef.h +++ b/WoW/Source/WorldClient/MapObjDef.h @@ -11,6 +11,8 @@ #include "Engine/Source/Storm/H/stpl.h" #include "WoW/Source/WorldClient/MapBaseObj.h" +class CM2Lighting; + class CMapObj; class CMapObjDefGroup; class CMapObjDefMapObjDefGroupLink; @@ -28,11 +30,9 @@ class CMapObjDefGroup : public CMapBaseObj { /* 0064 */ int32_t unk_0064; /* 0068 */ int32_t unk_0068; /* 006C */ TSList m_frustumList; - /* 0078 */ TSList m_doodadDefLinkList; - /* 0084 */ TSList m_entityLinkList; - /* 0090 */ uint32_t unk_0090; - /* 0094 */ uint32_t unk_0094; - /* 0098 */ uint32_t unk_0098; + /* 0078 */ TSList > m_doodadDefLinkList; + /* 0084 */ TSList > m_entityLinkList; + /* 0090 */ TSList > m_lightLinkList; /* 009C */ uint32_t unk_009C; /* 00A0 */ uint32_t unk_00A0; /* 00A4 */ uint32_t unk_00A4; @@ -40,16 +40,16 @@ class CMapObjDefGroup : public CMapBaseObj { /* 00B0 */ TSLink m_sortTableLink; /* 00B8 */ uint32_t unk_00B8; /* 00BC */ uint32_t unk_00BC; + + virtual void SelectLights(CM2Lighting* lighting); + + CMapObjDefGroup(); + void MarkPrepared(); + void UpdateLights(); }; -class CMapObjDef : public CMapBaseObj { +class CMapObjDef : public CMapBaseObj, public TSHashObject { public: - /* 0024 */ void* unk_0024; - /* 0028 */ void* unk_0028; - /* 002C */ void* unk_002C; - /* 0030 */ void* unk_0030; - /* 0034 */ void* unk_0034; - /* 0038 */ int32_t unk_0038; /* 003C */ C3Vector m_position; /* 0048 */ CAaBox m_bbox; /* 0060 */ CAaSphere m_sphere; @@ -64,15 +64,19 @@ class CMapObjDef : public CMapBaseObj { /* 0108 */ int32_t unk_0108; /* 010C */ int32_t unk_010C; /* 0110 */ int32_t unk_0110; - /* 0114 */ TSExplicitList m_mapObjDefGroupLinkList; - /* 0120 */ TSGrowableArray m_defGroups; - /* 0130 */ uint32_t m_groupCount; + /* 0114 */ TSList > m_mapObjDefGroupLinkList; + /* 0120 */ TSCGrowableArray m_defGroups; /* 0134 */ TSGrowableArray m_lightsArray; /* 0144 */ uint32_t m_argbColor; /* 0148 */ int32_t unk_0148; /* 014C */ int32_t unk_014C; /* 0150 */ int16_t m_doodadSetOverrides [3]; /* 0156 */ uint8_t unk_0156[2]; + + CMapObjDef(); + bool TestAABox(C3Vector& start, C3Vector& end); + int32_t GetGroundType(uint32_t groupNum, uint16_t faceIndex); + void RenderWithCallback(TSGrowableArray* groups); }; #endif diff --git a/WoW/Source/WorldClient/MapObjGroup.cpp b/WoW/Source/WorldClient/MapObjGroup.cpp index d0f3e74..c74626b 100644 --- a/WoW/Source/WorldClient/MapObjGroup.cpp +++ b/WoW/Source/WorldClient/MapObjGroup.cpp @@ -1,8 +1,18 @@ #include "MapObjGroup.h" -#include "Engine/Source/Gx/CGxBuf.h" +#include +#include +#include "Engine/Source/Tempest/C2iVector.h" +#include "Engine/Source/Tempest/C3Ray.h" +#include "Engine/Source/Tempest/CiRect.h" +#include "Engine/Source/Tempest/CMath.h" +#include "Engine/Source/Tempest/CWFrustum.h" +#include "Engine/Source/Tempest/NTempest.h" +#include "WoW/Source/DB/DBClient/DBClient.h" #include "Engine/Source/Gx/CGxDevice/CGxDevice.h" #include "Engine/Source/Gx/CGxPool.h" #include "WoW/Source/WorldClient/World.h" +#include "WoW/Source/WorldClient/MapObjDef.h" +#include "Engine/Source/Gx/Gx.h" // STATIC: 0x00AEEE58 VBBList CMapObjGroup::vertexVBList; @@ -13,6 +23,14 @@ VBBList CMapObjGroup::indexVBList; // STATIC: 0x00AEEEA8 TSExplicitList CMapObjGroup::s_unk_AEEEA8; +// VERIFY: 4f76965b auto +// OFFSET: 0x007C7790 +static uint8_t CullLiquidVertexZ(const CAaBox& box, const C3Vector& vertex) { + float below = vertex.z - box.b.z + 0.019444443f; + float above = box.t.z - vertex.z + 0.019444443f; + return ((*reinterpret_cast(&below) >> 29) & 0x4) | ((*reinterpret_cast(&above) >> 26) & 0x20); +} + // VERIFY: 03d37c83 auto // OFFSET: 0x007CB990 void CMapObjGroup::Initialize() { @@ -82,3 +100,806 @@ void CMapObjGroup::Destroy() { CMapObjGroup::indexVBList.Destroy(); CMapObjGroup::s_unk_AEEEA8.Clear(); } + + + + + + + + + + + + + + + + + + + + +// VERIFY: af527478 manual +// OFFSET: 0x007C8360 +bool CMapObjGroup::QueryLiquid(const C3Vector& pos, int32_t* liquidType, float* level) { + int32_t type = this->m_liquidType; + + if (!type) { + return false; + } + + if (!this->m_liquidTiles.x || !this->m_liquidTiles.y) { + *liquidType = type; + *level = 3.4028235e38f; + return true; + } + + float fx = (pos.x - this->m_liquidCorner.x) * 0.24f; + float fy = (pos.y - this->m_liquidCorner.y) * 0.24f; + + C2iVector tile; + tile.x = static_cast(floor(fx)); + tile.y = static_cast(floor(fy)); + + if (tile.x < 0 || tile.y < 0 || !(tile < this->m_liquidTiles)) { + return false; + } + + if ((this->m_liquidTileList[tile.y * this->m_liquidTiles.x + tile.x].m_flags & 0xF) == 0xF) { + return false; + } + + float dx = fx - tile.x; + float dy = fy - tile.y; + int32_t v0 = tile.y * this->m_liquidVerts.x + tile.x; + int32_t v1 = this->m_liquidVerts.x + v0; + float offset = (g_liquidTypeDB.GetRecord(type)->m_flags & 0x4) ? 0.0099999998f : 0.0f; + + const SMOLiquidVert* verts = this->m_liquidVertexList; + float h0 = verts[v0].m_waterVert.m_height; + float top = h0 + (verts[v0 + 1].m_waterVert.m_height - h0) * dx; + float h2 = verts[v1].m_waterVert.m_height; + float bottom = h2 + (verts[v1 + 1].m_waterVert.m_height - h2) * dx; + float height = top + (bottom - top) * dy; + + if (offset + height <= pos.z) { + return false; + } + + *level = height; + *liquidType = this->m_liquidType; + return true; +} + +// VERIFY: 0f477396 auto +// OFFSET: 0x007C8BF0 +uint32_t CMapObjGroup::GetNumSharedTiles() { + if (this->m_numSharedTiles > 0) { + return this->m_numSharedTiles; + } + + int32_t count = 0; + const SMOLTile* tile = this->m_liquidTileList; + + for (int32_t y = 0; y < this->m_liquidTiles.y; y++) { + for (int32_t x = 0; x < this->m_liquidTiles.x; x++) { + if ((tile->m_flags & 0xF) != 0xF && (tile->m_flags & 0x80)) { + count++; + } + + tile++; + } + } + + this->m_numSharedTiles = count; + return count; +} + +// VERIFY: e95ab74c manual +// OFFSET: 0x007C8C60 +void CMapObjGroup::GenLiquidVerts() { + float x = this->m_liquidCorner.x; + float y = this->m_liquidCorner.y; + C3Vector* out = this->m_liquidVertArray->m_verts.m_data; + const SMOLiquidVert* vert = this->m_liquidVertexList; + + this->m_liquidHeight = vert->m_waterVert.m_height; + this->m_liquidMaxHeight = vert->m_waterVert.m_height; + + for (int32_t row = 0; row < this->m_liquidVerts.y; row++) { + for (int32_t col = 0; col < this->m_liquidVerts.x; col++) { + float height = vert->m_waterVert.m_height; + out->x = x; + out->y = y; + out->z = height; + out++; + + this->m_liquidHeight = height <= this->m_liquidHeight ? height : this->m_liquidHeight; + this->m_liquidMaxHeight = height < this->m_liquidMaxHeight ? this->m_liquidMaxHeight : height; + + vert++; + x += 4.1666665f; + } + + x = this->m_liquidCorner.x; + y += 4.1666665f; + } +} + +// VERIFY: 5304b11e auto +// OFFSET: 0x007CBDC0 +void CMapObjGroup::AllocLiquidVB(EGxVertexBufferFormat format, uint32_t vertexCount, uint32_t indexCount, CGxBatch** batch) { + if (!this->m_liquidVertsBlock) { + CMapObjGroup::vertexVBList.AllocVBB(&this->m_liquidVertsBlock, GxVertexSize(format), vertexCount); + this->m_liquidVertsBlock->m_buffer->unk_001C = 0; + } + + if (!this->m_liquidIndicesBlock) { + CMapObjGroup::indexVBList.AllocVBB(&this->m_liquidIndicesBlock, 2, indexCount); + this->m_liquidIndicesBlock->m_buffer->unk_001C = 0; + } + + *batch = &this->m_liquidBatch; + this->m_liquidVBRefCount++; +} + +// VERIFY: cd2471f5 auto +// OFFSET: 0x007CBE30 +void CMapObjGroup::FreeLiquidVB() { + if (!this->m_liquidVBRefCount) { + return; + } + + if (--this->m_liquidVBRefCount) { + return; + } + + if (this->m_liquidVertsBlock) { + CMapObjGroup::vertexVBList.FreeVBB(this->m_liquidVertsBlock); + } + + if (this->m_liquidIndicesBlock) { + CMapObjGroup::indexVBList.FreeVBB(this->m_liquidIndicesBlock); + } + + this->m_liquidVertsBlock = nullptr; + this->m_liquidIndicesBlock = nullptr; +} + +// VERIFY: 0c68f770 auto +// OFFSET: 0x007D7310 +int32_t CMapObjGroup::GetLiquidType(uint32_t groupLiquid) { + if (groupLiquid < 21 && groupLiquid) { + switch ((groupLiquid - 1) & 3) { + case 0: + return (this->m_flags & 0x80000) ? 14 : 13; + + case 1: + return 14; + + case 2: + return 19; + + case 3: + return 20; + } + } + + return groupLiquid; +} + +// VERIFY: c9184804 manual +// OFFSET: 0x007C9110 +void CMapObjGroup::VectorIntersectLiquidSX(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles) { + int32_t x = CMath::fint_(start.x - 0.5f); + int32_t y = CMath::fint_(start.y - 0.5f); + int32_t endX = CMath::fint_(end.x - 0.5f); + int32_t step = endX >= x ? 1 : -1; + endX += step; + + while (x != endX) { + tiles[(*tileCount)++] = x; + tiles[(*tileCount)++] = y; + x += step; + } +} + +// VERIFY: 4f16d5fa manual +// OFFSET: 0x007C91A0 +void CMapObjGroup::VectorIntersectLiquidSY(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles) { + int32_t x = CMath::fint_(start.x - 0.5f); + int32_t y = CMath::fint_(start.y - 0.5f); + int32_t endY = CMath::fint_(end.y - 0.5f); + int32_t step = endY >= y ? 1 : -1; + endY += step; + + while (y != endY) { + tiles[(*tileCount)++] = x; + tiles[(*tileCount)++] = y; + y += step; + } +} + +// VERIFY: f3f15286 manual +// OFFSET: 0x007C9230 +void CMapObjGroup::VectorIntersectLiquidDX(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles) { + int32_t x = CMath::fint_(start.x - 0.5f); + int32_t y = CMath::fint_(start.y - 0.5f); + int32_t step; + float frac; + + if (start.x > end.x) { + step = -1; + frac = static_cast(x + 1) - start.x; + } else { + step = 1; + frac = start.x - static_cast(x); + } + + float slope = (end.y - start.y) / fabs(end.x - start.x); + float startY = start.y - frac * slope; + float offset = 0.0f; + int32_t endX = CMath::fint_(end.x - 0.5f) + step; + + if (x == endX) { + return; + } + + int32_t prevY = y; + int32_t curY = y; + + do { + if (curY != prevY) { + tiles[(*tileCount)++] = x; + tiles[(*tileCount)++] = prevY; + } + + offset += slope; + tiles[(*tileCount)++] = x; + tiles[(*tileCount)++] = curY; + x += step; + prevY = curY; + curY = CMath::fint_(offset + startY - 0.5f); + } while (x != endX); + + if (curY != prevY && curY >= 0 && curY < this->m_liquidTiles.y) { + tiles[(*tileCount)++] = x - step; + tiles[(*tileCount)++] = curY; + } +} + +// VERIFY: 3612548b manual +// OFFSET: 0x007C9370 +void CMapObjGroup::VectorIntersectLiquidDY(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles) { + int32_t x = CMath::fint_(start.x - 0.5f); + int32_t y = CMath::fint_(start.y - 0.5f); + int32_t step; + float frac; + + if (start.y > end.y) { + step = -1; + frac = static_cast(y + 1) - start.y; + } else { + step = 1; + frac = start.y - static_cast(y); + } + + float slope = (end.x - start.x) / fabs(end.y - start.y); + float startX = start.x - frac * slope; + float offset = 0.0f; + int32_t endY = CMath::fint_(end.y - 0.5f) + step; + + if (y == endY) { + return; + } + + int32_t prevX = x; + int32_t curX = x; + + do { + if (curX != prevX) { + tiles[(*tileCount)++] = prevX; + tiles[(*tileCount)++] = y; + } + + offset += slope; + tiles[(*tileCount)++] = curX; + tiles[(*tileCount)++] = y; + y += step; + prevX = curX; + curX = CMath::fint_(offset + startX - 0.5f); + } while (y != endY); + + if (curX != prevX && curX >= 0 && curX < this->m_liquidTiles.x) { + tiles[(*tileCount)++] = curX; + tiles[(*tileCount)++] = y - step; + } +} + +// VERIFY: 8f1675f7 manual +// OFFSET: 0x007C8DD0 +bool CMapObjGroup::VectorIntersectLiquidTiles(const C3Segment& seg, float* t, uint32_t flags, uint32_t* tileCount, uint8_t* tiles, World::TriData* triData, CMapObjDef* mapObjDef) { + int32_t type = this->m_liquidType; + + if (!type || !this->m_liquidTiles.x || !this->m_liquidTiles.y) { + return false; + } + + if ((flags & 0x10000) && !(flags & 0x20000) && !(g_liquidTypeDB.GetRecord(type)->m_flags & 0x4)) { + return false; + } + + float dx = seg.t.x - seg.b.x; + float dy = seg.t.y - seg.b.y; + float dz = seg.t.z - seg.b.z; + float invLength = 1.0f / sqrt(dx * dx + dz * dz + dy * dy); + + C3Ray ray; + ray.origin = seg.b; + ray.dir.x = dx * invLength; + ray.dir.y = dy * invLength; + ray.dir.z = invLength * dz; + + C3Vector* verts = this->m_liquidVertArray->m_verts.m_data; + uint32_t tris[2][3]; + tris[0][0] = 0; + tris[0][1] = this->m_liquidVerts.x + 1; + tris[0][2] = this->m_liquidVerts.x; + tris[1][0] = 0; + tris[1][1] = 1; + tris[1][2] = this->m_liquidVerts.x + 1; + + World::TriData::Batch* batch = nullptr; + uint16_t* indices = nullptr; + bool found = false; + + for (uint32_t i = 0; i < *tileCount; i += 2) { + uint32_t tileX = tiles[0]; + uint32_t tileY = tiles[1]; + + if ((this->m_liquidTileList[tileY * this->m_liquidTiles.x + tileX].m_flags & 0xF) != 0xF) { + uint32_t base = tileX + tileY * this->m_liquidVerts.x; + + for (uint32_t j = 0; j < 2; j++) { + uint32_t triIndices[3]; + triIndices[0] = base + tris[j][0]; + triIndices[1] = base + tris[j][1]; + triIndices[2] = base + tris[j][2]; + + float hitT = 1.0f; + + if (!NTempest::Intersect(ray, verts, triIndices, &hitT, nullptr, 0.0099999998f)) { + continue; + } + + hitT *= invLength; + + if (hitT >= *t || hitT < 0.0f) { + continue; + } + + *t = hitT; + + if (!triData) { + found = true; + continue; + } + + if (!found) { + batch = triData->AllocBatch(); + + if (batch) { + batch->m_def = mapObjDef; + batch->m_matrix = &mapObjDef->m_mat; + batch->m_vertexList = this->m_liquidVertArray->m_verts.m_data; + indices = triData->AllocIndices(3); + batch->m_indices = indices; + } + } + + found = true; + + if (!batch || !batch->m_indices) { + continue; + } + + batch->m_indexCount = 0; + + for (uint32_t k = 0; k < 3; k++) { + uint16_t index = base + tris[j][k]; + indices[batch->m_indexCount++] = index; + batch->m_minVertexIndex = batch->m_minVertexIndex < index ? batch->m_minVertexIndex : index; + batch->m_maxVertexIndex = batch->m_maxVertexIndex > index ? batch->m_maxVertexIndex : index; + } + + batch->m_faceCount++; + } + } + + tiles += 2; + } + + return found; +} + +// VERIFY: 4c425802 auto +// OFFSET: 0x007C9DD0 +bool CMapObjGroup::VectorIntersectLiquid(const C3Segment& seg, float* t, uint32_t flags, World::TriData* triData, CMapObjDef* mapObjDef) { + float sizeY = this->m_liquidTiles.y * 4.1666665f - 0.1f; + sizeY = sizeY > 0.0f ? sizeY : 0.0f; + float sizeX = 4.1666665f * this->m_liquidTiles.x - 0.1f; + sizeX = sizeX > 0.0f ? sizeX : 0.0f; + + CAaBox box; + box.b.x = this->m_liquidCorner.x; + box.b.y = this->m_liquidCorner.y; + box.b.z = this->m_liquidHeight; + box.t.x = sizeX + this->m_liquidCorner.x; + box.t.y = sizeY + this->m_liquidCorner.y; + box.t.z = this->m_liquidMaxHeight; + + C3Vector start = seg.b; + C3Vector end = seg.t; + C3Vector dir; + dir.x = end.x - start.x; + dir.y = end.y - start.y; + dir.z = end.z - start.z; + + float lengthSq = dir.z * dir.z + dir.y * dir.y + dir.x * dir.x; + + if (lengthSq > 2.3841858e-7f) { + float invLength = 1.0f / sqrt(lengthSq); + dir.x *= invLength; + dir.y *= invLength; + dir.z *= invLength; + } + + if (!box.ContainsPoint(start)) { + C3Ray ray; + ray.origin = seg.b; + ray.dir = dir; + float rayT = 1.0f; + + if (!NTempest::Intersect(ray, box, &rayT, &start)) { + return false; + } + } + + if (!box.ContainsPoint(end)) { + C3Ray ray; + ray.origin = end; + dir.x = -dir.x; + dir.y = -dir.y; + dir.z = -dir.z; + ray.dir = dir; + float rayT = 1.0f; + + if (!NTempest::Intersect(ray, box, &rayT, &end)) { + return false; + } + } + + uint32_t tileCount = 0; + uint8_t tiles[0x800]; + + start.x = (start.x - this->m_liquidCorner.x) * 0.24f; + start.y = (start.y - this->m_liquidCorner.y) * 0.24f; + start.z = start.z * 0.24f; + end.x = (end.x - this->m_liquidCorner.x) * 0.24f; + end.y = (end.y - this->m_liquidCorner.y) * 0.24f; + end.z = 0.24f * end.z; + + float deltaX = end.x - start.x; + float deltaY = fabs(end.y - start.y); + + if (deltaY < 2.3841858e-7f) { + this->VectorIntersectLiquidSX(start, end, &tileCount, tiles, sizeof(tiles)); + } else if (fabs(deltaX) < 2.3841858e-7f) { + this->VectorIntersectLiquidSY(start, end, &tileCount, tiles, sizeof(tiles)); + } else if (deltaY >= fabs(deltaX)) { + this->VectorIntersectLiquidDY(start, end, &tileCount, tiles, sizeof(tiles)); + } else { + this->VectorIntersectLiquidDX(start, end, &tileCount, tiles, sizeof(tiles)); + } + + return this->VectorIntersectLiquidTiles(seg, t, flags, &tileCount, tiles, triData, mapObjDef); +} + +// VERIFY: 2533e68d manual +// OFFSET: 0x007C94B0 +bool CMapObjGroup::GetLiquidTris(const CAaBox& box, uint32_t flags, World::TriData* triData, CMapObjDef* mapObjDef) { + int32_t type = this->m_liquidType; + + if (!type || !this->m_liquidTiles.x || !this->m_liquidTiles.y) { + return false; + } + + if ((flags & 0x10000) && !(flags & 0x20000) && !(g_liquidTypeDB.GetRecord(type)->m_flags & 0x4)) { + return false; + } + + C3Vector points[2]; + points[0].x = box.b.x - this->m_liquidCorner.x; + points[0].y = box.b.y - this->m_liquidCorner.y; + points[0].z = box.b.z; + points[1].x = box.t.x - this->m_liquidCorner.x; + points[1].y = box.t.y - this->m_liquidCorner.y; + points[1].z = box.t.z; + + CAaBox bounds = CAaBox::Bounding(points, 2); + + CiRect rect; + rect.maxX = static_cast(floor(bounds.t.x * 0.24f)); + rect.maxY = static_cast(floor(bounds.t.y * 0.24f)); + rect.minY = static_cast(floor(bounds.b.y * 0.24f)); + rect.minX = static_cast(floor(bounds.b.x * 0.24f)); + + if (rect.maxX < 0 || rect.maxY < 0 || rect.minX >= this->m_liquidTiles.x || rect.minY >= this->m_liquidTiles.y) { + return false; + } + + CiRect clip; + clip.minY = 0; + clip.minX = 0; + clip.maxY = this->m_liquidTiles.y - 1; + clip.maxX = this->m_liquidTiles.x - 1; + rect &= clip; + + int32_t vertSkip = this->m_liquidVerts.x - rect.maxX + rect.minX - 2; + int32_t height = rect.maxY - rect.minY; + int32_t width = rect.maxX - rect.minX; + uint32_t cullSize = (width + 2) * (height + 2); + int32_t first = rect.minX + rect.minY * this->m_liquidVerts.x; + + if (cullSize > 0x2000) { + return false; + } + + uint8_t* cull = static_cast(alloca(cullSize)); + const C3Vector* vert = this->m_liquidVertArray->m_verts.m_data + first; + uint8_t* cullOut = cull; + + for (int32_t y = rect.minY; y <= rect.maxY + 1; y++) { + for (int32_t x = rect.minX; x <= rect.maxX + 1; x++) { + *cullOut++ = CullLiquidVertexZ(box, *vert); + vert++; + } + + vert += vertSkip; + } + + const C3Vector* tileVert = this->m_liquidVertArray->m_verts.m_data + first; + const SMOLTile* tile = &this->m_liquidTileList[rect.minY * this->m_liquidTiles.x + rect.minX]; + int32_t tileSkip = this->m_liquidTiles.x - rect.maxX + rect.minX - 1; + + int32_t cullTris[2][3]; + cullTris[0][0] = 0; + cullTris[0][1] = width + 3; + cullTris[0][2] = width + 2; + cullTris[1][0] = 0; + cullTris[1][1] = 1; + cullTris[1][2] = width + 3; + + int32_t vertTris[2][3]; + vertTris[0][0] = 0; + vertTris[0][1] = this->m_liquidVerts.x + 1; + vertTris[0][2] = this->m_liquidVerts.x; + vertTris[1][0] = 0; + vertTris[1][1] = 1; + vertTris[1][2] = this->m_liquidVerts.x + 1; + + World::TriData::Batch* batch = nullptr; + uint16_t* indices = nullptr; + bool found = false; + const uint8_t* cullTile = cull; + + for (int32_t y = rect.minY; y <= rect.maxY; y++) { + for (int32_t x = rect.minX; x <= rect.maxX; x++) { + if ((tile->m_flags & 0xF) != 0xF) { + for (uint32_t j = 0; j < 2; j++) { + if (cullTile[cullTris[j][0]] & cullTile[cullTris[j][2]] & cullTile[cullTris[j][1]]) { + continue; + } + + if (!found) { + batch = triData->AllocBatch(); + + if (batch) { + batch->m_def = mapObjDef; + batch->m_matrix = &mapObjDef->m_mat; + batch->m_vertexList = this->m_liquidVertArray->m_verts.m_data; + indices = triData->AllocIndices(6 * (width + 1) * (height + 1)); + batch->m_indices = indices; + } + } + + found = true; + + if (!batch || !batch->m_indices) { + continue; + } + + for (uint32_t k = 0; k < 3; k++) { + uint16_t index = (tileVert - this->m_liquidVertArray->m_verts.m_data) + vertTris[j][k]; + indices[batch->m_indexCount++] = index; + batch->m_minVertexIndex = batch->m_minVertexIndex < index ? batch->m_minVertexIndex : index; + batch->m_maxVertexIndex = batch->m_maxVertexIndex > index ? batch->m_maxVertexIndex : index; + } + + batch->m_faceCount++; + } + } + + tileVert++; + tile++; + cullTile++; + } + + tileVert += tileSkip + 1; + tile += tileSkip; + cullTile++; + } + + return found; +} + +// VERIFY: 22aa2402 manual +// OFFSET: 0x007CAB70 +bool CMapObjGroup::GetLiquidTris(CFrustum& frustum, uint32_t flags, World::TriData* triData, CMapObjDef* mapObjDef) { + int32_t type = this->m_liquidType; + + if (!type || !this->m_liquidTiles.x || !this->m_liquidTiles.y) { + return false; + } + + if ((flags & 0x10000) && !(flags & 0x20000) && !(g_liquidTypeDB.GetRecord(type)->m_flags & 0x4)) { + return false; + } + + C3Vector offset; + offset.x = -this->m_liquidCorner.x; + offset.y = -this->m_liquidCorner.y; + offset.z = -this->m_liquidCorner.z; + + CWFrustum localFrustum; + static_cast(localFrustum) = frustum; + localFrustum.Translate(offset); + + CAaBox bounds = CAaBox::Bounding(localFrustum.corners, 8); + + CiRect rect; + rect.maxX = static_cast(floor(bounds.t.x * 0.24f)); + rect.maxY = static_cast(floor(bounds.t.y * 0.24f)); + rect.minY = static_cast(floor(bounds.b.y * 0.24f)); + rect.minX = static_cast(floor(bounds.b.x * 0.24f)); + + if (rect.maxX < 0 || rect.maxY < 0 || rect.minX >= this->m_liquidTiles.x || rect.minY >= this->m_liquidTiles.y) { + return false; + } + + CiRect clip; + clip.minY = 0; + clip.minX = 0; + clip.maxY = this->m_liquidTiles.y - 1; + clip.maxX = this->m_liquidTiles.x - 1; + rect &= clip; + + int32_t vertSkip = this->m_liquidVerts.x - rect.maxX + rect.minX - 2; + int32_t height = rect.maxY - rect.minY; + int32_t width = rect.maxX - rect.minX; + uint32_t cullSize = (width + 2) * (height + 2); + int32_t first = rect.minX + rect.minY * this->m_liquidVerts.x; + + if (cullSize > 0x2000) { + return false; + } + + uint8_t* cull = static_cast(alloca(cullSize)); + const C3Vector* vert = this->m_liquidVertArray->m_verts.m_data + first; + uint8_t* cullOut = cull; + + for (int32_t y = rect.minY; y <= rect.maxY + 1; y++) { + for (int32_t x = rect.minX; x <= rect.maxX + 1; x++) { + frustum.Cull(*vert, *cullOut); + vert++; + cullOut++; + } + + vert += vertSkip; + } + + const C3Vector* tileVert = this->m_liquidVertArray->m_verts.m_data + first; + const SMOLTile* tile = &this->m_liquidTileList[rect.minY * this->m_liquidTiles.x + rect.minX]; + int32_t tileSkip = this->m_liquidTiles.x - rect.maxX + rect.minX - 1; + + int32_t cullTris[2][3]; + cullTris[0][0] = 0; + cullTris[0][1] = width + 3; + cullTris[0][2] = width + 2; + cullTris[1][0] = 0; + cullTris[1][1] = 1; + cullTris[1][2] = width + 3; + + int32_t vertTris[2][3]; + vertTris[0][0] = 0; + vertTris[0][1] = this->m_liquidVerts.x + 1; + vertTris[0][2] = this->m_liquidVerts.x; + vertTris[1][0] = 0; + vertTris[1][1] = 1; + vertTris[1][2] = this->m_liquidVerts.x + 1; + + World::TriData::Batch* batch = nullptr; + uint16_t* indices = nullptr; + bool found = false; + const uint8_t* cullTile = cull; + + for (int32_t y = rect.minY; y <= rect.maxY; y++) { + for (int32_t x = rect.minX; x <= rect.maxX; x++) { + if ((tile->m_flags & 0xF) != 0xF) { + for (uint32_t j = 0; j < 2; j++) { + if (cullTile[cullTris[j][0]] & cullTile[cullTris[j][2]] & cullTile[cullTris[j][1]]) { + continue; + } + + if (!found) { + batch = triData->AllocBatch(); + + if (batch) { + batch->m_def = mapObjDef; + batch->m_matrix = &mapObjDef->m_mat; + batch->m_vertexList = this->m_liquidVertArray->m_verts.m_data; + indices = triData->AllocIndices(6 * (width + 1) * (height + 1)); + batch->m_indices = indices; + } + } + + found = true; + + if (!batch || !batch->m_indices) { + continue; + } + + for (uint32_t k = 0; k < 3; k++) { + uint16_t index = (tileVert - this->m_liquidVertArray->m_verts.m_data) + vertTris[j][k]; + indices[batch->m_indexCount++] = index; + batch->m_minVertexIndex = batch->m_minVertexIndex < index ? batch->m_minVertexIndex : index; + batch->m_maxVertexIndex = batch->m_maxVertexIndex > index ? batch->m_maxVertexIndex : index; + } + + batch->m_faceCount++; + } + } + + tileVert++; + tile++; + cullTile++; + } + + tileVert += tileSkip + 1; + tile += tileSkip; + cullTile++; + } + + return found; +} + +// VERIFY: 121a8eb8 manual +// OFFSET: 0x007CA110 +bool CMapObjGroup::GetLiquidFacets(const CAaBox& box, uint32_t flags, World::FacetData* facets, CMapObjDef* mapObjDef) { + World::TriData::statusFlags = 0; + World::TriData::nBatches = 0; + World::TriData::faceIndexCursor = 0; + World::TriData::indexCursor = 0; + World::TriData::unk_CB7538 = 0; + + World::TriData triData; + this->GetLiquidTris(box, flags, &triData, mapObjDef); + + WGUID guid; + guid.m_guidLow = mapObjDef->unk_0148; + guid.m_guidHigh = mapObjDef->unk_014C; + World::TriDataToFacetData(&triData, facets, guid); + + return World::TriData::nBatches != 0; +} diff --git a/WoW/Source/WorldClient/MapObjGroup.h b/WoW/Source/WorldClient/MapObjGroup.h index f23095e..ecd3069 100644 --- a/WoW/Source/WorldClient/MapObjGroup.h +++ b/WoW/Source/WorldClient/MapObjGroup.h @@ -14,6 +14,9 @@ #include "WoW/Common/AaBsp.h" #include "Engine/Source/Tempest/C2iVector.h" +class CFrustum; +struct SIffChunk; + struct BspQuery_Segment; class C2Vector; class C3Vector; @@ -28,12 +31,17 @@ class CMapObjDef; class CMapObjGroup; struct SMOBatch; struct SMOLTile; -struct SMOLiquidVert; +union SMOLiquidVert; struct SMOMaterial; struct SMOPoly; class VBBList; class VBBList_Block; +namespace World { + class FacetData; + class TriData; +} + struct BspQuery { /* 0000 */ int32_t* m_overflowFlags; /* 0004 */ SMOPoly* m_faces; @@ -89,6 +97,7 @@ class CMapObjGroup { static void Destroy(); static void Initialize(); + static void AsyncPostloadCallback(void* arg); /* 0000 */ int32_t m_objectIndex; /* 0004 */ VBBList_Block* m_vertsBlock; @@ -97,11 +106,8 @@ class CMapObjGroup { /* 0010 */ VBBList_Block* m_liquidVertsBlock; /* 0014 */ VBBList_Block* m_liquidIndicesBlock; /* 0018 */ float m_timer; - /* 001C */ void* unk_001C; - /* 0020 */ int32_t unk_0020; - /* 0024 */ int32_t unk_0024; - /* 0028 */ int32_t unk_0028; - /* 002C */ int16_t unk_002C [2]; + /* 001C */ CMapObjGroup::VertArray* m_liquidVertArray; + /* 0020 */ CGxBatch m_liquidBatch; /* 0030 */ int32_t m_flags; /* 0034 */ CAaBox m_bbox; /* 004C */ float m_distToCamera; @@ -130,7 +136,7 @@ class CMapObjGroup { /* 0104 */ uint16_t* m_doodadRefList; /* 0108 */ CImVector* m_colorVertexList; /* 010C */ CImVector* m_colorVertexListExtra; - /* 0110 */ int32_t unk_0110; + /* 0110 */ int32_t m_liquidVBRefCount; /* 0114 */ C2iVector m_liquidVerts; /* 011C */ C2iVector m_liquidTiles; /* 0124 */ C3Vector m_liquidCorner; @@ -138,9 +144,9 @@ class CMapObjGroup { /* 0134 */ SMOLiquidVert* m_liquidVertexList; /* 0138 */ SMOLTile* m_liquidTileList; /* 013C */ float m_liquidHeight; - /* 0140 */ int32_t unk_0140; + /* 0140 */ float m_liquidMaxHeight; /* 0144 */ int32_t m_liquidType; - /* 0148 */ int32_t m_unkFlag; + /* 0148 */ int32_t m_numSharedTiles; /* 014C */ int32_t unk_014C; /* 0150 */ int32_t m_polyListSize; /* 0154 */ int32_t m_indicesCount; @@ -160,7 +166,8 @@ class CMapObjGroup { /* 018C */ CMapObj* m_parent; /* 0190 */ int32_t unk_0190; /* 0194 */ CAsyncObject* m_asyncObjPtr; - /* 0198 */ int32_t m_unkLoadedFlag; + /* 0198 */ int8_t m_unkLoadedFlag; + /* 0199 */ uint8_t unk_0199[3]; /* 019C */ int32_t m_unkIndexMin1; /* 01A0 */ int32_t m_unkIndexMax1; /* 01A4 */ uint16_t m_unkIndexMin2; @@ -168,8 +175,39 @@ class CMapObjGroup { /* 01A8 */ int32_t m_tSExplicitListMLinkoffset; /* 01AC */ void* m_tSExplicitListPtr; /* 01B0 */ void* m_tSExplicitListPtr2; - /* 01B4 */ CMapObjGroup* m_perv; - /* 01B8 */ CMapObjGroup* m_next; + /* 01B4 */ TSLink m_asyncLoadLink; + + CMapObjGroup(); + void Create(); + void CreateDataPointers(SIffChunk* dataChunk); + void CreateOptionalDataPointers(SIffChunk* dataChunk); + void AllocVB(); + void AllocLiquidVB(EGxVertexBufferFormat format, uint32_t vertexCount, uint32_t indexCount, CGxBatch** batch); + void FreeLiquidVB(); + void SetIndexVB(); + void UploadIndexBuffer(CGxBuf* buf); + void SetVertexVB(); + void FillVertexVB(CGxBuf* buf, EGxVertexBufferFormat format); + void FixColorVertexAlpha(); + bool GetTris(CFrustum* frustum, uint32_t flags, uint16_t faceIgnoreFlags, uint32_t a5, CMapObjDef* mapObjDef); + void GetTrisFromQuery(uint32_t a2, BspQuery* a3, CMapObjDef* mapObjDef, uint32_t a5); + void SetLighting(uint32_t mode); + bool Intersect(C3Segment& seg, float* dist, uint32_t flags, uint16_t faceIgnoreFlags, int32_t* hitIndex); + bool GetFacesForLinking(C3Segment& seg, float* t0, int32_t* face0, float* t1, int32_t* face1); + bool QueryLiquid(const C3Vector& pos, int32_t* liquidType, float* level); + void QueryLiquidSounds(const C3Vector& pos, uint32_t* results); + void GenLiquidVerts(); + int32_t GetLiquidType(uint32_t groupLiquid); + uint32_t GetNumSharedTiles(); + bool GetLiquidTris(const CAaBox& box, uint32_t flags, World::TriData* triData, CMapObjDef* mapObjDef); + bool GetLiquidTris(CFrustum& frustum, uint32_t flags, World::TriData* triData, CMapObjDef* mapObjDef); + bool GetLiquidFacets(const CAaBox& box, uint32_t flags, World::FacetData* facets, CMapObjDef* mapObjDef); + bool VectorIntersectLiquid(const C3Segment& seg, float* t, uint32_t flags, World::TriData* triData, CMapObjDef* mapObjDef); + bool VectorIntersectLiquidTiles(const C3Segment& seg, float* t, uint32_t flags, uint32_t* tileCount, uint8_t* tiles, World::TriData* triData, CMapObjDef* mapObjDef); + void VectorIntersectLiquidSX(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles); + void VectorIntersectLiquidSY(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles); + void VectorIntersectLiquidDX(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles); + void VectorIntersectLiquidDY(const C3Vector& start, const C3Vector& end, uint32_t* tileCount, uint8_t* tiles, uint32_t maxTiles); }; struct SMOGroupHeader { @@ -193,10 +231,6 @@ struct SMOLTile { /* 0000 */ uint8_t m_flags; }; -struct SMOLiquidVert { - /* 0000 */ uint8_t unk_0000[8]; -}; - struct SMOMVert { /* 0000 */ int16_t m_s; /* 0002 */ int16_t m_t; @@ -211,6 +245,11 @@ struct SMOWVert { /* 0004 */ float m_height; }; +union SMOLiquidVert { + /* 0000 */ SMOWVert m_waterVert; + /* 0000 */ SMOMVert m_magmaVert; +}; + class VBBList_Block { public: /* 0000 */ TSLink m_link; @@ -220,6 +259,8 @@ class VBBList_Block { /* 0014 */ CGxPool* m_pool; /* 0018 */ CGxBuf* m_buffer; /* 001C */ VBBList_Block* m_listHead; + + void Set(uint32_t itemSize, uint32_t itemCount, uint32_t offset); }; class VBBList { @@ -232,24 +273,38 @@ class VBBList { /* 001C */ TSExplicitList unk_001C; void Destroy(); + VBBList_Block* AllocBlock(); + void AllocVBB(VBBList_Block** a1, uint32_t itemSize, uint32_t itemCount); + void FreeVBB(VBBList_Block* block); + void AssignBlock(VBBList_Block* a1, VBBList_Block** a2, uint32_t itemSize, uint32_t itemCount); }; namespace World { -namespace TriData { -struct Batch { - /* 0000 */ C44Matrix* m_matrix; - /* 0004 */ C3Vector* m_vertexList; - /* 0008 */ C3Vector* m_normalList; - /* 000C */ uint32_t unk_000C; - /* 0010 */ uint16_t* m_indices; - /* 0014 */ uint16_t* m_faceIndices; - /* 0018 */ uint16_t m_indexCount; - /* 001A */ uint16_t m_faceCount; - /* 001C */ uint16_t m_minVertexIndex; - /* 001E */ uint16_t m_maxVertexIndex; - /* 0020 */ CMapObjDef* m_def; -}; -} + class TriData { + public: + struct Batch { + /* 0000 */ C44Matrix* m_matrix; + /* 0004 */ C3Vector* m_vertexList; + /* 0008 */ C3Vector* m_normalList; + /* 000C */ uint32_t unk_000C; + /* 0010 */ uint16_t* m_indices; + /* 0014 */ uint16_t* m_faceIndices; + /* 0018 */ uint16_t m_indexCount; + /* 001A */ uint16_t m_faceCount; + /* 001C */ uint16_t m_minVertexIndex; + /* 001E */ uint16_t m_maxVertexIndex; + /* 0020 */ CMapObjDef* m_def; + }; + + static uint32_t statusFlags; + static uint32_t nBatches; + static uint32_t faceIndexCursor; + static uint32_t indexCursor; + static uint32_t unk_CB7538; + + Batch* AllocBatch(); + uint16_t* AllocIndices(uint32_t count); + }; } class CAaBsp_Query_Segment { diff --git a/WoW/Source/WorldClient/MapObjRead.cpp b/WoW/Source/WorldClient/MapObjRead.cpp index cfc628a..056495f 100644 --- a/WoW/Source/WorldClient/MapObjRead.cpp +++ b/WoW/Source/WorldClient/MapObjRead.cpp @@ -1 +1,79 @@ #include "MapObjRead.h" +#include "Engine/Source/Services/AsyncFile.h" +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapObj.h" +#include "WoW/Source/WorldClient/MapObjGroup.h" + +// STATIC: 0x00ADFC4C +TSList > CMapObj::s_asyncLoadQueue = TSExplicitList(); + +// STATIC: 0x00ADFC58 +TSList > CMapObj::s_unk_ADFC58 = TSExplicitList(); + +// VERIFY: 737e6a62 manual +// OFFSET: 0x007D80C0 +bool CMapObj::Read(char* fileName) { + SStrCopy(this->m_wmoName, fileName, STORM_MAX_STR); + + SFileImpl* file = nullptr; + + if (CMap::SafeOpen(fileName, &file)) { + SStrCopy(this->m_wmoName, fileName, STORM_MAX_STR); + } else { + CMap::SafeOpen("world\\wmo\\Dungeon\\test\\missingwmo.wmo", &file); + SStrCopy(this->m_wmoName, "world\\wmo\\Dungeon\\test\\missingwmo.wmo", STORM_MAX_STR); + } + + this->m_wmoFileSize = SFileGetFileSize(file, nullptr); + this->m_pWmoData = SMemAlloc(this->m_wmoFileSize, __FILE__, __LINE__, 0); + + this->m_asyncObject = AsyncFileReadAllocObject(); + this->m_asyncObject->m_file = file; + this->m_asyncObject->m_userArg = this; + this->m_asyncObject->m_buffer = this->m_pWmoData; + this->m_asyncObject->m_size = this->m_wmoFileSize; + this->m_asyncObject->m_userPostloadCallback = reinterpret_cast(CMapObj::PostloadCallback); + this->m_asyncObject->m_priority = 124; + AsyncFileReadObject(this->m_asyncObject, 0); + + CMapObj::s_asyncLoadQueue.LinkToTail(this); + + return true; +} + +// VERIFY: d48a58f4 manual +// OFFSET: 0x007D85E0 +void CMapObj::ReadGroup(uint32_t index, bool preLoad) { + CMapObjGroup* group = this->m_mapObjGroupArray[index]; + + char fileName[256]; + strcpy(fileName, this->m_wmoName); + SStrPrintf(strrchr(fileName, '.'), sizeof(fileName), "_%03d", index); + strcat(fileName, ".wmo"); + + SFileImpl* file = nullptr; + CMap::SafeOpen(fileName, &file); + + group->m_fileSize = SFileGetFileSize(file, nullptr); + group->m_filePtr = SMemAlloc(group->m_fileSize, __FILE__, __LINE__, 0); + group->m_parent = this; + + if (preLoad) { + CMap::SafeRead(fileName, file, group->m_filePtr, group->m_fileSize); + group->Create(); + SFileCloseFile(file); + return; + } + + CAsyncObject* object = AsyncFileReadAllocObject(); + object->m_file = file; + object->m_buffer = group->m_filePtr; + object->m_size = group->m_fileSize; + object->m_userArg = group; + object->m_userPostloadCallback = reinterpret_cast(CMapObjGroup::AsyncPostloadCallback); + group->m_asyncObjPtr = object; + AsyncFileReadObject(object, 0); + + CMapObj::s_unk_ADFC58.LinkToTail(group); +} diff --git a/WoW/Source/WorldClient/MapRenderChunk.cpp b/WoW/Source/WorldClient/MapRenderChunk.cpp index a5b7a0d..ffab13a 100644 --- a/WoW/Source/WorldClient/MapRenderChunk.cpp +++ b/WoW/Source/WorldClient/MapRenderChunk.cpp @@ -1,8 +1,28 @@ #include "MapRenderChunk.h" +#include #include #include "Engine/Source/Gx/CGxBuf.h" #include "Engine/Source/Gx/CGxDevice/CGxDevice.h" #include "Engine/Source/Gx/CGxPool.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "Engine/Source/Model2/M2Light.h" +#include "Engine/Source/Model2/M2Scene.h" +#include "Engine/Source/Gx/CGxDevice/CGxShader.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/ShaderEffect/ShadowCache.h" +#include "Engine/Source/Tempest/C33Matrix.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "WoW/Source/WorldClient/DayNight__DNInfo.h" +#include "WoW/Source/WorldClient/Map.h" +#include "WoW/Source/WorldClient/MapArea.h" +#include "WoW/Source/WorldClient/MapChunk.h" +#include "WoW/Source/WorldClient/MapLight.h" +#include "WoW/Source/WorldClient/Types.h" +#include "WoW/Source/WorldClient/World.h" // STATIC: 0x00AEEBF8 static uint32_t s_maxVertexCount = 145; @@ -10,15 +30,54 @@ static uint32_t s_maxVertexCount = 145; // STATIC: 0x00AEEC18 static uint16_t unk_AEEC18 = 768; +// STATIC: 0x00A3FFF4 +static const uint32_t s_nibbleMask[2] = { 0x0F, 0xF0 }; + +// STATIC: 0x00A3FFFC +static const uint32_t s_nibbleShift[2] = { 4, 0 }; + +// STATIC: 0x00A40004 +static const uint32_t s_nibbleUnshift[2] = { 0, 4 }; + +// STATIC: 0x00A4000C +static const uint32_t s_bitMask[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; + +// STATIC: 0x00A4002C +static const uint32_t s_bitShift[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; + +// STATIC: 0x00A4004C +static const uint8_t s_shadowAlpha[2] = { 0xFF, 0x00 }; + +// STATIC: 0x00AEE214 +static uint16_t s_shadowTexel[2] = { 0x0000, 0xFFFF }; + +// STATIC: 0x00AF14A0 +static uint16_t s_streamVertexCount = 145; + +// STATIC: 0x00AF14C0 +static uint16_t s_streamIndexCount = 768; + +// STATIC: 0x00AF14F8 +static float s_texAnimRate[8] = { 64.0f, 48.0f, 32.0f, 16.0f, 8.0f, 4.0f, 2.0f, 1.0f }; + +// STATIC: 0x00D1CED8 +static uint8_t s_alphaRowBuf[4][64]; + // STATIC: 0x00D1CFD8 static uint8_t s_defaultShadowRow[64]; // STATIC: 0x00D1D018 static uint8_t s_defaultAlphaRow[64]; +// STATIC: 0x00AEEC20 +static float s_layerChannel[4][4] = { { 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f, 0.0f } }; + // STATIC: 0x00AEEC70 TSExplicitList CMapRenderChunk::s_chunkBufBlockFreeList; +// STATIC: 0x00AEEC7C +TSExplicitList CMapRenderChunk::s_bufList; + // STATIC: 0x00AEEC88 TSExplicitList CMapRenderChunk::s_renderChunkBufFreeList; @@ -43,6 +102,27 @@ CGxPool* CMapRenderChunk::s_gxVertexPool; // STATIC: 0x00D1D06C EGxVertexBufferFormat CMapRenderChunk::s_gxBufVertexFormat; +// STATIC: 0x00D1D070 +CGxShader* CMapRenderChunk::s_currentShader[4]; + +// STATIC: 0x00D1D080 +CGxShader* CMapRenderChunk::s_currentShaderX[4]; + +// STATIC: 0x00D1D090 +CGxShader* CMapRenderChunk::s_pixelShader[2]; + +// STATIC: 0x00D1D098 +uint32_t CMapRenderChunk::s_defaultTex[8192]; + +// STATIC: 0x00D25098 +CMapRenderChunk::RenderLayersFunc CMapRenderChunk::s_renderLayersFunc; + +// STATIC: 0x00D2509C +CImVector CMapRenderChunk::s_fogColor; + +// STATIC: 0x00D250A0 +CMapRenderChunk::SVertexShaderConstants CMapRenderChunk::s_vertexShaderConsts; + // STATIC: 0x00D252F0 TSGrowableArray CMapRenderChunk::s_chunkBlockArray; @@ -152,3 +232,2484 @@ CMapChunkBuf::CMapChunkBuf() { CMapChunkBufBlock::CMapChunkBufBlock() { this->m_vertexCount = 0; } + +// VERIFY: 1baa8ad1 auto +// OFFSET: 0x007B7330 +void CMapRenderChunk::SetVertexFormat(EGxVertexBufferFormat format) { + CMapRenderChunk::s_gxBufVertexFormat = format; + CMapRenderChunk::s_bPoolsDirty = 1; +} + +// VERIFY: 75114f19 auto +// OFFSET: 0x007B7350 +void CMapRenderChunk::FreeLayers() { + for (uint32_t i = 0; i < this->m_layersCount; i++) { + CMapRenderChunkLayer& layer = this->m_layers[i]; + layer.m_texture = nullptr; + layer.m_textureId = -1; + layer.m_flags = 0; + + if (layer.m_layerTexture) { + HandleClose(reinterpret_cast(layer.m_layerTexture)); + layer.m_layerTexture = nullptr; + } + } + + this->m_layersCount = 0; + + if (this->m_shadowTexture) { + HandleClose(reinterpret_cast(this->m_shadowTexture)); + this->m_shadowTexture = nullptr; + } + + if (this->m_terrainBlendTexture) { + HandleClose(reinterpret_cast(this->m_terrainBlendTexture)); + this->m_terrainBlendTexture = nullptr; + } +} + +// VERIFY: 6909fb20 auto +// OFFSET: 0x007B73E0 +void CMapRenderChunk::UpdateLoaded() { + for (int32_t i = 0; i < this->m_layersCount; i++) { + if (!TextureGetGxTex(this->m_layers[i].m_texture, 0, nullptr)) { + return; + } + } + + this->m_unkFlags |= 0x8; +} + +// VERIFY: ef5458ef auto +// OFFSET: 0x007B7A70 +CTexture* CMapRenderChunk::AllocTexture(uint32_t width, uint32_t height, void* userArg, GxTexUserFunc userFunc, EGxTexFormat format, EGxTexFormat dataFormat) { + EGxTexFormat texFormat; + + if (CMap::header.m_flags & 0x4) { + texFormat = static_cast((CWorld::terrainAlphaBitDepth != 8) + 2); + } else { + texFormat = format; + } + + CGxTexFlags flags(GxTex_Linear, 0, 0, 0, 0, 0, 1); + flags.m_filter = GxTex_Linear; + + return TextureCreate(GxTex_2d, width, height, 0, texFormat, dataFormat, flags, userArg, userFunc, "TerrainBlend", 0); +} + +// VERIFY: 686076da auto +// OFFSET: 0x007B7AF0 +void CMapRenderChunk::AddBatch(CMapChunk* chunk1, CMapChunk* chunk2, C3Vector* position, uint8_t flags) { + this->m_mapChunks[0] = chunk1; + this->m_mapChunks[1] = chunk2; + this->m_vec1 = *position; + this->m_unkFlags = flags; + + CAaBox box = chunk1->m_bbox; + + if (chunk2) { + box |= chunk2->m_bbox; + } + + C3Vector diagonal = box.t - box.b; + this->m_sphere.r = sqrtf(diagonal.SquaredMag()) * 0.5f; + C3Vector center = box.t + box.b; + center *= 0.5f; + this->m_sphere.c = center; +} + +// VERIFY: c9761f91 auto +// OFFSET: 0x007B7BD0 +void CMapRenderChunk::SelectLights(CM2Lighting* lighting) { + lighting->AddLight(&CMap::s_mapLight->m_light); + + DayNight::DNInfo* info = DayNight::GetInfo(); + C3Vector fogColor; + fogColor.x = info->m_fog.m_color.r * 0.0039215689f; + fogColor.y = info->m_fog.m_color.g * 0.0039215689f; + fogColor.z = info->m_fog.m_color.b * 0.0039215689f; + + lighting->SetFog(fogColor, info->m_fog.m_start, info->m_fog.m_end, info->m_fog.m_density); +} + +// VERIFY: bc2de872 manual +// OFFSET: 0x007B9250 +void CMapRenderChunk::CreateLayer(CMapArea* area, SMLayer* layer, int32_t checkDuplicate) { + if (checkDuplicate) { + for (int32_t i = 0; i < this->m_layersCount; i++) { + if (this->m_layers[i].m_textureId == layer->m_textureId) { + return; + } + } + } + + CMapRenderChunkLayer* renderLayer = &this->m_layers[this->m_layersCount]; + renderLayer->m_flags = layer->m_flags; + renderLayer->m_layerIndex = this->m_layersCount; + + CMapAreaTexture* areaTexture = &area->m_textures[layer->m_textureId]; + + if (!areaTexture->m_texture) { + area->LoadTerrainTexture(areaTexture, layer->m_textureId); + } + + renderLayer->m_texture = areaTexture->m_texture; + renderLayer->m_textureId = layer->m_textureId; + renderLayer->m_layerTexture = nullptr; + renderLayer->m_owner = this; + + if (renderLayer->m_flags & 0x80) { + this->unk_000A |= 0x1; + } + + if (g_theGxDevicePtr->Caps().m_texTarget[1] && g_theGxDevicePtr->Caps().m_shaderTargets[0] && g_theGxDevicePtr->Caps().m_shaderTargets[4] && (renderLayer->m_flags & 0x400)) { + this->unk_000A |= 0x4; + } + + this->m_layersCount++; +} + +// VERIFY: 6c5dc3eb manual +// OFFSET: 0x007B9340 +CMapChunkBuf* CMapRenderChunk::AllocBuf(int32_t flags, CMapRenderChunk* renderChunk) { + if (flags & 0x3) { + CMapChunkBufBlock* block = CMapRenderChunk::s_chunkBufBlockFreeList.Head(); + + if (!block) { + return nullptr; + } + + block->m_blockLink.Unlink(); + + CMapChunkBuf* buf = &block->m_bufs[0]; + buf->m_renderChunk = renderChunk; + buf->m_unkFlags = flags; + + if (buf->m_vertexBuf) { + buf->m_vertexBuf->unk_001C = 0; + buf->m_indexBuf->unk_001C = 0; + return buf; + } + + uint32_t stride = CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PNC ? sizeof(CGxVertexPNC) : sizeof(CGxVertexPN); + buf->m_vertexBuf = g_theGxDevicePtr->BufCreate(CMapRenderChunk::s_gxVertexPool, stride, 290, 145 * stride * block->m_vertexCount); + buf->m_indexBuf = g_theGxDevicePtr->BufCreate(CMapRenderChunk::s_gxIndexPool, 2, 1536, 1536 * block->m_vertexCount); + return buf; + } + + CMapChunkBuf* freeBuf = CMapRenderChunk::s_renderChunkBufFreeList.Tail(); + + if (freeBuf) { + freeBuf->unk_0014.Unlink(); + freeBuf->m_renderChunk = renderChunk; + freeBuf->m_unkFlags = flags; + freeBuf->m_vertexBuf->unk_001C = 0; + freeBuf->m_indexBuf->unk_001C = 0; + return freeBuf; + } + + CMapChunkBufBlock* block = CMapRenderChunk::s_chunkBufBlockFreeList.Tail(); + + if (!block) { + return nullptr; + } + + block->m_blockLink.Unlink(); + + if (block->m_bufs[0].m_vertexBuf) { + GxBufDestroy(block->m_bufs[0].m_vertexBuf); + GxBufDestroy(block->m_bufs[0].m_indexBuf); + block->m_bufs[0].m_vertexBuf = nullptr; + block->m_bufs[0].m_indexBuf = nullptr; + } + + uint32_t stride = CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PNC ? sizeof(CGxVertexPNC) : sizeof(CGxVertexPN); + CMapChunkBuf* buf; + + for (int32_t i = 0; i < 2; i++) { + buf = &block->m_bufs[i]; + buf->m_vertexBuf = g_theGxDevicePtr->BufCreate(CMapRenderChunk::s_gxVertexPool, stride, 145, 145 * stride * (block->m_vertexCount + i)); + buf->m_indexBuf = g_theGxDevicePtr->BufCreate(CMapRenderChunk::s_gxIndexPool, 2, 768, 1536 * (block->m_vertexCount + i)); + buf->m_unkFlags = 0; + buf->m_renderChunk = nullptr; + + if (i == 0) { + CMapRenderChunk::s_renderChunkBufFreeList.LinkToTail(buf); + } + } + + buf->m_renderChunk = renderChunk; + buf->m_unkFlags = flags; + return buf; +} + +// VERIFY: e18a2023 manual +// OFFSET: 0x007B9560 +void CMapRenderChunk::PurgeChunkBufBlocks() { + for (uint32_t i = 0; i < CMapRenderChunk::s_chunkBlockArray.Count(); i++) { + CMapChunkBufBlock& block = CMapRenderChunk::s_chunkBlockArray[i]; + + if (block.m_blockLink.IsLinked() || !block.m_bufs[0].unk_0014.IsLinked() || !block.m_bufs[1].unk_0014.IsLinked()) { + continue; + } + + for (uint32_t j = 0; j < 2; j++) { + CMapChunkBuf& chunkBuf = block.m_bufs[j]; + CGxBuf* vertexBuf = chunkBuf.m_vertexBuf; + + if (vertexBuf && vertexBuf != g_theGxDevicePtr->BufStream(vertexBuf->m_pool->m_target, 0, 0)) { + g_theGxDevicePtr->BufDestroy(vertexBuf); + } + + CGxBuf* indexBuf = chunkBuf.m_indexBuf; + + if (indexBuf && indexBuf != g_theGxDevicePtr->BufStream(indexBuf->m_pool->m_target, 0, 0)) { + g_theGxDevicePtr->BufDestroy(indexBuf); + } + + chunkBuf.m_vertexBuf = nullptr; + chunkBuf.m_indexBuf = nullptr; + chunkBuf.m_unkFlags = 0; + chunkBuf.unk_0014.Unlink(); + } + + CMapRenderChunk::s_chunkBufBlockFreeList.LinkToHead(&block); + } +} + +// VERIFY: 92303a74 manual +// OFFSET: 0x007B9690 +CMapRenderChunk::CMapRenderChunk() { + this->m_sphere.c = C3Vector(); + this->m_sphere.r = 0.0f; + this->m_batch.m_primType = GxPrims_Last; + this->m_batch.m_start = 0; + this->m_batch.m_count = 0; + this->m_batch.m_minIndex = 0; + this->m_batch.m_maxIndex = 0; + this->m_lastUpdateTime = 0.0f; + this->m_unkFlags = 0; + this->m_mapChunks[0] = nullptr; + this->m_mapChunks[1] = nullptr; + this->m_layersCount = 0; + this->unk_000A = 0; + this->m_terrainBlendTexture = nullptr; + this->m_shadowTexture = nullptr; + this->m_chunkBuf = nullptr; +} + +// VERIFY: fad72c2f manual +// OFFSET: 0x007B9770 +void CMapRenderChunk::CreateLayers() { + int32_t chunkCount = 1; + + if (CMap::header.m_flags & 0x4) { + chunkCount = 2; + } + + for (int32_t i = 0; i < chunkCount; i++) { + CMapChunk* chunk = this->m_mapChunks[i]; + + if (!chunk) { + continue; + } + + CMapArea* area = static_cast(chunk->m_parentLinkList.Head()->m_ref); + SMChunk* header = chunk->m_header; + + if (!header->m_nLayers) { + continue; + } + + SMLayer* layer = chunk->m_layers; + + for (uint32_t j = 0; j < header->m_nLayers; j++) { + this->CreateLayer(area, layer, i != 0); + layer++; + } + } + + this->unk_000A |= 0x20; +} + +// VERIFY: eb3e1413 auto +// OFFSET: 0x007B9830 +void CMapRenderChunk::FreeBuf() { + CMapChunkBuf* chunkBuf = this->m_chunkBuf; + + if (!chunkBuf) { + return; + } + + if (chunkBuf->m_unkFlags & 0x3) { + CMapRenderChunk::s_chunkBufBlockFreeList.LinkToHead(chunkBuf->m_block); + } else { + CMapRenderChunk::s_renderChunkBufFreeList.LinkToHead(this->m_chunkBuf); + } + + chunkBuf->m_renderChunk = nullptr; + this->m_chunkBuf = nullptr; +} + +// VERIFY: c324fde5 manual +// OFFSET: 0x007B9CB0 +CMapChunkBufBlock::~CMapChunkBufBlock() { +} + +// VERIFY: a8d77235 manual +// OFFSET: 0x007B9D60 +CMapRenderChunk::~CMapRenderChunk() { + this->FreeBuf(); + this->FreeLayers(); + + if (this->m_mapChunks[0]) { + this->m_mapChunks[0]->m_renderChunk = nullptr; + this->m_mapChunks[0]->m_bLoaded = 0; + this->m_mapChunks[0] = nullptr; + } + + if (this->m_mapChunks[1]) { + this->m_mapChunks[1]->m_renderChunk = nullptr; + this->m_mapChunks[1]->m_bLoaded = 0; + this->m_mapChunks[1] = nullptr; + } +} + +// VERIFY: 2d552482 manual +// OFFSET: 0x007BA600 +void CMapRenderChunk::UpdatePools() { + if (!CMapRenderChunk::s_bPoolsDirty) { + return; + } + + CMap::ClearAllRenderChunks(); + CMapRenderChunk::Destroy(); + + int32_t radius = 1 - static_cast(World::s_farClip * -0.03f); + uint32_t count = (2 * radius * radius + 1) & ~1u; + + CMapRenderChunk::s_pnEstimateVertex = 145 * count; + CMapRenderChunk::s_pnEstimateIndex = 768 * count; + + uint32_t stride = CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PNC ? sizeof(CGxVertexPNC) : sizeof(CGxVertexPN); + CMapRenderChunk::s_gxVertexPool = g_theGxDevicePtr->PoolCreate(GxPoolTarget_Vertex, GxPoolUsage_Dynamic, CMapRenderChunk::s_pnEstimateVertex * stride, GxPoolHintBit_Unk3, "CMapRenderChunk_vtx"); + CMapRenderChunk::s_gxIndexPool = g_theGxDevicePtr->PoolCreate(GxPoolTarget_Index, GxPoolUsage_Dynamic, CMapRenderChunk::s_pnEstimateIndex * 2, GxPoolHintBit_Unk3, "CMapRenderChunk_idx"); + + CMapRenderChunk::s_chunkBlockArray.SetCount(count / 2); + + for (uint32_t i = 0; i < CMapRenderChunk::s_chunkBlockArray.Count(); i++) { + CMapChunkBufBlock* block = &CMapRenderChunk::s_chunkBlockArray[i]; + block->m_vertexCount = 2 * i; + block->m_bufs[0].m_block = block; + block->m_bufs[1].m_block = block; + CMapRenderChunk::s_chunkBufBlockFreeList.LinkToTail(block); + CMapRenderChunk::s_bufList.LinkToTail(block); + } + + CMapRenderChunk::s_bPoolsDirty = 0; +} + +// VERIFY: 8aa8ada7 manual +// OFFSET: 0x007D02C0 +void CMapRenderChunk::RenderPrepBufs(CGxBuf* vertexBuf, CGxBuf* indexBuf) { + int32_t vertexReady = vertexBuf->unk_001C && vertexBuf->unk_001D; + int32_t indexReady = indexBuf->unk_001C && indexBuf->unk_001D; + + if (!vertexReady) { + C3Vector offset; + char* vertices = g_theGxDevicePtr->BufLock(vertexBuf); + + if (this->m_mapChunks[0]) { + this->m_mapChunks[0]->CreateVertices(vertices, 0, offset); + } + + if (this->m_mapChunks[1]) { + offset.z = this->m_mapChunks[1]->m_topLeftCoords.z - this->m_mapChunks[0]->m_topLeftCoords.z; + + if (this->m_unkFlags & 0x2) { + offset.y = -33.333332f; + } else { + offset.x = -33.333332f; + } + + this->m_mapChunks[1]->CreateVertices(vertices, 145, offset); + } + + g_theGxDevicePtr->BufUnlock(vertexBuf, 0); + vertexBuf->unk_001C = 1; + } + + if (!indexReady) { + this->m_batch.m_primType = GxPrim_Triangles; + this->m_batch.m_start = 0; + this->m_batch.m_count = 0; + this->m_batch.m_minIndex = 0xFFFF; + this->m_batch.m_maxIndex = 0; + + uint16_t* indices = reinterpret_cast(g_theGxDevicePtr->BufLock(indexBuf)); + + if (this->m_mapChunks[0]) { + this->m_mapChunks[0]->CreateIndices(indices, &this->m_batch); + } + + if (this->m_mapChunks[1]) { + this->m_mapChunks[1]->CreateIndices(&indices[this->m_batch.m_count], &this->m_batch); + } + + g_theGxDevicePtr->BufUnlock(indexBuf, 0); + indexBuf->unk_001C = 1; + } +} + +// VERIFY: 28de08e7 manual +// OFFSET: 0x007D0420 +void CMapRenderChunk::UseStreamingBufs() { + uint32_t stride = CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PNC ? sizeof(CGxVertexPNC) : sizeof(CGxVertexPN); + uint32_t vertexCount = s_streamVertexCount; + uint32_t indexCount = s_streamIndexCount; + + if (this->m_unkFlags & 0x3) { + vertexCount *= 2; + indexCount *= 2; + } + + CGxBuf* vertexBuf = g_theGxDevicePtr->BufStream(GxPoolTarget_Vertex, stride, vertexCount); + CGxBuf* indexBuf = g_theGxDevicePtr->BufStream(GxPoolTarget_Index, 2, indexCount); + + this->RenderPrepBufs(vertexBuf, indexBuf); + + GxPrimVertexPtr(vertexBuf, CMapRenderChunk::s_gxBufVertexFormat); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); +} + +// VERIFY: fdc9dc29 manual +// OFFSET: 0x007D04A0 +void CMapRenderChunk::RenderSetup(int32_t a2) { + this->m_lastUpdateTime = 0.0f; + this->AllocLayerTextures(); + + if (!a2 || !CMap::enableTerrainShaderVertex) { + C44Matrix world; + C3Vector translation = this->m_vec1 - CWorldScene::s_activeWorldView; + world.d0 = translation.x; + world.d1 = translation.y; + world.d2 = translation.z; + GxXformSet(GxXform_World, world); + + CM2Lighting lighting(this->m_sphere); + World::s_m2Scene->SelectLights(&lighting); + this->SelectLights(&lighting); + lighting.SetupGxLights(&CWorldScene::s_activeWorldView); + lighting.SetupGxFog(); + } + + if (this->m_chunkBuf) { + GxPrimVertexPtr(this->m_chunkBuf->m_vertexBuf, CMapRenderChunk::s_gxBufVertexFormat); + g_theGxDevicePtr->PrimIndexPtr(this->m_chunkBuf->m_indexBuf); + } else { + this->UseStreamingBufs(); + } +} + +// VERIFY: 9923f02a manual +// OFFSET: 0x007D3010 +void CMapRenderChunk::RenderSolid() { + C44Matrix tex0; + GxXformSet(GxXform_Tex0, tex0); + C44Matrix tex1; + GxXformSet(GxXform_Tex1, tex1); + + GxRsSet(GxRs_BlendingMode, 0); + GxUpdateAlphaRef(GxRs_AlphaRef); + + CGxTex* gxTex = TextureGetGxTex(CWorldScene::s_defaultTexture, 1, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (CMap::gTerrainPixelShadersValid) { + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(CWorldScene::s_defaultBlendTexture, 1, nullptr)); + } else { + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(CWorldScene::s_defaultTexture, 1, nullptr)); + } + + g_theGxDevicePtr->Draw(&this->m_batch, 1); + + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// VERIFY: b2fa1daf auto +// OFFSET: 0x007D3230 +void CMapRenderChunk::RenderSolidPixelShader() { + this->RenderSolid(); +} + +// OFFSET: 0x007D3240 +void CMapRenderChunk::RenderSolidVertexPixelShader() { + GxRsSet(GxRs_BlendingMode, 0); + GxUpdateAlphaRef(GxRs_AlphaRef); + + CGxTex* gxTex = TextureGetGxTex(CWorldScene::s_defaultTexture, 1, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(CWorldScene::s_defaultBlendTexture, 1, nullptr)); + + CMapRenderChunk::SetVertexShader(this, 2, 0); + g_theGxDevicePtr->Draw(&this->m_batch, 1); + + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// VERIFY: 6e2aac38 auto +// OFFSET: 0x007D3F70 +void CMapRenderChunk::RenderPrep() { + if (!this->m_chunkBuf) { + this->m_chunkBuf = CMapRenderChunk::AllocBuf(this->m_unkFlags & 0x3, this); + } + + if (this->m_chunkBuf) { + CMapRenderChunk::s_bufList.LinkToHead(this->m_chunkBuf->m_block); + this->RenderPrepBufs(this->m_chunkBuf->m_vertexBuf, this->m_chunkBuf->m_indexBuf); + } + + if (!this->m_layersCount) { + this->CreateLayers(); + } + + if (!(this->m_unkFlags & 0x8)) { + this->UpdateLoaded(); + } +} + +// INLINED +CMapRenderChunkLayer::CMapRenderChunkLayer() { + this->m_flags = 0; + this->m_layerIndex = 0; + this->m_texture = nullptr; + this->m_textureId = -1; + this->m_layerTexture = nullptr; + this->m_owner = nullptr; +} + +// VERIFY: d15abdb3 auto +// OFFSET: 0x007B7420 +uint8_t* CMapRenderChunk::DecodeAlphaRLE(uint8_t* src, uint8_t* dst, int32_t count) { + int32_t srcIndex = 0; + int32_t dstIndex = 0; + + while (dstIndex < count) { + int8_t header = static_cast(src[srcIndex]); + srcIndex++; + + if (header < 0) { + uint32_t runLength = header & 0x7F; + uint8_t value = src[srcIndex]; + srcIndex++; + + if (runLength) { + memset(&dst[dstIndex], value, runLength); + dstIndex += runLength; + } + } else { + for (uint32_t i = static_cast(header); i; i--) { + dst[dstIndex] = src[srcIndex]; + srcIndex++; + dstIndex++; + } + } + } + + return &src[srcIndex]; +} + +// VERIFY: 425713a6 auto +// OFFSET: 0x007B74A0 +void CMapRenderChunk::SetupAlphaRows(uint8_t** shadowCursor, uint8_t** shadowRow, SLayerAlpha* layers, uint32_t size, uint8_t** alphaRows) { + if (*shadowCursor) { + *shadowRow = *shadowCursor; + *shadowCursor += size >> 3; + } else { + *shadowRow = s_defaultShadowRow; + } + + for (int32_t i = 0; i < 4; i++) { + if (!layers[i].m_alphaData) { + alphaRows[i] = s_defaultAlphaRow; + } else if (layers[i].m_flags & 0x200) { + alphaRows[i] = s_alphaRowBuf[i]; + layers[i].m_alphaData = CMapRenderChunk::DecodeAlphaRLE(layers[i].m_alphaData, s_alphaRowBuf[i], size); + } else { + alphaRows[i] = layers[i].m_alphaData; + layers[i].m_alphaData += size; + } + } +} + +// VERIFY: 900e0dee auto +// OFFSET: 0x007B7530 +void CMapRenderChunk::SetupAlphaRows4444(uint32_t size, uint8_t** shadowRow, uint8_t** alphaRows, SLayerAlpha* layers, uint8_t** shadowCursor) { + if (*shadowCursor) { + *shadowRow = *shadowCursor; + *shadowCursor += size >> 3; + } else { + *shadowRow = s_defaultShadowRow; + } + + for (int32_t i = 0; i < 4; i++) { + if (layers[i].m_alphaData) { + alphaRows[i] = layers[i].m_alphaData; + layers[i].m_alphaData += size >> 1; + } else { + alphaRows[i] = s_defaultAlphaRow; + } + } +} + +// VERIFY: 25ebba41 manual +// OFFSET: 0x007B75B0 +void __stdcall CMapRenderChunk::UnpackAlphaBitsFixed4444Mip0(uint16_t* dst, uint32_t size, SLayerAlpha* layer) { + uint8_t* alphaData = layer->m_alphaData; + uint32_t i = 0; + + for (uint32_t y = 0; y < size; y++) { + for (uint32_t x = 0; x < size; x++) { + *dst = static_cast((((alphaData[i >> 1] & s_nibbleMask[i & 1]) >> s_nibbleUnshift[i & 1]) << 12) | 0xFFF); + dst++; + i++; + } + } +} + +// VERIFY: ede6e802 manual +// OFFSET: 0x007B7620 +void __stdcall CMapRenderChunk::UnpackAlphaBitsFixed4444Mip1(uint16_t* dst, uint32_t size, SLayerAlpha* layer) { + uint8_t* alphaData = layer->m_alphaData; + uint32_t i = 0; + + for (uint32_t y = 0; y < size; y += 2) { + for (uint32_t x = 0; x < size - 2; x += 2) { + *dst = static_cast((((alphaData[i >> 1] & s_nibbleMask[i & 1]) >> s_nibbleUnshift[i & 1]) << 12) | 0xFFF); + dst++; + i += 2; + } + + i++; + *dst = static_cast((((alphaData[i >> 1] & s_nibbleMask[i & 1]) >> s_nibbleUnshift[i & 1]) << 12) | 0xFFF); + dst++; + i++; + + if (y == size - 4) { + i += 64; + } + + i += size; + } +} + +// VERIFY: 152a7dd5 manual +// OFFSET: 0x007B76F0 +void __stdcall CMapRenderChunk::UnpackAlphaBitsUnfixed4444Mip0(uint16_t* dst, uint32_t size, SLayerAlpha* layer) { + uint8_t* alphaData = layer->m_alphaData; + uint32_t i = 0; + uint16_t texel = 0xFFF; + + for (uint32_t y = 0; y < size - 1; y++) { + for (uint32_t x = 0; x < size - 1; x++) { + texel = static_cast((((alphaData[i >> 1] & s_nibbleMask[i & 1]) >> s_nibbleUnshift[i & 1]) << 12) | 0xFFF); + i++; + *dst = texel; + dst++; + } + + *dst = texel; + dst++; + i++; + } + + i -= size; + + for (uint32_t x = 0; x < size - 1; x++) { + texel = static_cast((((alphaData[i >> 1] & s_nibbleMask[i & 1]) >> s_nibbleUnshift[i & 1]) << 12) | 0xFFF); + i++; + *dst = texel; + dst++; + } + + *dst = texel; +} + +// VERIFY: 8998a684 manual +// OFFSET: 0x007B77D0 +void __stdcall CMapRenderChunk::UnpackAlphaBitsUnfixed4444Mip1(uint16_t* dst, uint32_t size, SLayerAlpha* layer) { + uint8_t* alphaData = layer->m_alphaData; + uint32_t i = 0; + + for (uint32_t y = 0; y < size - 1; y += 2) { + for (uint32_t x = 0; x < size - 1; x += 2) { + *dst = static_cast((((alphaData[i >> 1] & s_nibbleMask[i & 1]) >> s_nibbleUnshift[i & 1]) << 12) | 0xFFF); + dst++; + i += 2; + } + + i += size; + } +} + +// VERIFY: 3a09a0ac manual +// OFFSET: 0x007B7860 +void CMapRenderChunk::CollectLayerAlphaPtrs(CMapChunk* chunk, SLayerAlpha* layers) { + for (uint32_t i = 0; i < 4; i++) { + layers[i].m_alphaData = nullptr; + + if (this->m_layersCount > i) { + SMLayer* layer = &chunk->m_layers[i]; + + if (layer->m_flags & 0x100) { + layers[i].m_alphaData = &chunk->m_additionalShadowmap[layer->m_offsetInMCAL]; + } + + layers[i].m_flags = layer->m_flags; + } + } +} + +// VERIFY: c03f753e manual +// OFFSET: 0x007B7930 +void __stdcall CMapRenderChunk::ExpandShadowBitsToTexels(uint16_t* dst, uint32_t size, uint8_t* shadowMap) { + uint32_t i = 0; + + for (uint32_t y = 0; y < size; y++) { + for (uint32_t x = 0; x < size; x++) { + *dst = s_shadowTexel[(shadowMap[i >> 3] & s_bitMask[i & 7]) >> s_bitShift[i & 7]]; + dst++; + i++; + } + } +} + +// VERIFY: e6e2d775 manual +// OFFSET: 0x007B79A0 +void __stdcall CMapRenderChunk::ExpandShadowBitsToTexelsMip(uint16_t* dst, uint32_t size, uint8_t* shadowMap) { + uint32_t i = 0; + + for (uint32_t y = 0; y < size; y += 2) { + for (uint32_t x = 0; x < size - 2; x += 2) { + *dst = s_shadowTexel[(shadowMap[i >> 3] & s_bitMask[i & 7]) >> s_bitShift[i & 7]]; + dst++; + i += 2; + } + + i++; + *dst = s_shadowTexel[(shadowMap[i >> 3] & s_bitMask[i & 7]) >> s_bitShift[i & 7]]; + dst++; + i++; + + if (y == size - 4) { + i += size; + } + + i += size; + } +} + +// VERIFY: 3cd77f91 manual +// OFFSET: 0x007B7C60 +void __stdcall CMapRenderChunk::UnpackAlphaShadowBitsFixed8888Mip0(CImVector* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, int32_t alphaSlot, uint8_t* shadowMap) { + CImVector* dst = dstBase + dstOffset; + + for (uint32_t y = 0; y < size; y++) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, cursors, size, alphaRows); + + for (uint32_t x = 0; x < size; x++) { + CImVector color; + + if (alphaSlot) { + int32_t alpha[4]; + alpha[1] = alphaRows[1][x]; + alpha[2] = alphaRows[2][x]; + alpha[3] = alphaRows[3][x]; + alpha[alphaSlot] = 255 - alpha[3] - alpha[2] - alpha[1] - alphaRows[0][x]; + color.b = alpha[3]; + color.g = alpha[2]; + color.r = alpha[1]; + } else { + color.b = alphaRows[3][x]; + color.g = alphaRows[2][x]; + color.r = alphaRows[1][x]; + } + + color.a = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + dst[x] = color; + } + + dst += dstPitch; + } +} + +// VERIFY: 57dc0019 manual +// OFFSET: 0x007B7DC0 +void __stdcall CMapRenderChunk::UnpackAlphaShadowBitsFixed8888Mip1(CImVector* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, int32_t alphaSlot, uint8_t* shadowMap) { + CImVector* dst = dstBase + dstOffset; + + for (uint32_t y = 0; y < size; y += 2) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, cursors, size, alphaRows); + + uint32_t x = 0; + uint32_t n = 0; + + for (; x < size - 2; x += 2) { + CImVector color; + + if (alphaSlot) { + int32_t alpha[4]; + alpha[1] = alphaRows[1][x]; + alpha[2] = alphaRows[2][x]; + alpha[3] = alphaRows[3][x]; + alpha[alphaSlot] = 255 - alpha[3] - alpha[2] - alpha[1] - alphaRows[0][x]; + color.b = alpha[3]; + color.g = alpha[2]; + color.r = alpha[1]; + } else { + color.b = alphaRows[3][x]; + color.g = alphaRows[2][x]; + color.r = alphaRows[1][x]; + } + + color.a = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + dst[n] = color; + n++; + } + + x++; + + CImVector color; + + if (alphaSlot) { + int32_t alpha[4]; + alpha[1] = alphaRows[1][x]; + alpha[2] = alphaRows[2][x]; + alpha[3] = alphaRows[3][x]; + alpha[alphaSlot] = 255 - alpha[3] - alpha[2] - alpha[1] - alphaRows[0][x]; + color.b = alpha[3]; + color.g = alpha[2]; + color.r = alpha[1]; + } else { + color.b = alphaRows[3][x]; + color.g = alphaRows[2][x]; + color.r = alphaRows[1][x]; + } + + color.a = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + dst[n] = color; + + if (y < size - 2) { + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, cursors, size, alphaRows); + } + + if (y == size - 4) { + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, cursors, size, alphaRows); + } + + dst += dstPitch; + } +} + +// VERIFY: ddfa67aa manual +// OFFSET: 0x007B8070 +void __stdcall CMapRenderChunk::UnpackAlphaShadowBitsFixed4444Mip0(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap) { + uint16_t* dst = dstBase + dstOffset; + + for (uint32_t y = 0; y < size; y++) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + + for (uint32_t x = 0; x < size; x++) { + uint32_t mask = s_nibbleMask[x & 1]; + uint32_t shift = s_nibbleShift[x & 1]; + uint8_t alpha1 = static_cast((alphaRows[1][x >> 1] & mask) << shift); + uint8_t alpha2 = static_cast((alphaRows[2][x >> 1] & mask) << shift); + uint8_t alpha3 = static_cast((alphaRows[3][x >> 1] & mask) << shift); + uint8_t shadow = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + dst[x] = static_cast(((shadow & 0xF0) << 8) | ((alpha1 & 0xF0) << 4) | (alpha2 & 0xF0) | (alpha3 >> 4)); + } + + dst += dstPitch; + } +} + +// VERIFY: 17932350 manual +// OFFSET: 0x007B8190 +void __stdcall CMapRenderChunk::UnpackAlphaShadowBitsFixed4444Mip1(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap) { + uint16_t* dst = dstBase + dstOffset; + + for (uint32_t y = 0; y < size; y += 2) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + + uint32_t x = 0; + uint32_t n = 0; + + for (; x < size - 2; x += 2) { + uint32_t mask = s_nibbleMask[x & 1]; + uint32_t shift = s_nibbleShift[x & 1]; + uint8_t alpha1 = static_cast((alphaRows[1][x >> 1] & mask) << shift); + uint8_t alpha2 = static_cast((alphaRows[2][x >> 1] & mask) << shift); + uint8_t alpha3 = static_cast((alphaRows[3][x >> 1] & mask) << shift); + uint8_t shadow = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + dst[n] = static_cast(((shadow & 0xF0) << 8) | ((alpha1 & 0xF0) << 4) | (alpha2 & 0xF0) | (alpha3 >> 4)); + n++; + } + + x++; + + uint32_t mask = s_nibbleMask[x & 1]; + uint32_t shift = s_nibbleShift[x & 1]; + uint8_t alpha1 = static_cast((alphaRows[1][x >> 1] & mask) << shift); + uint8_t alpha2 = static_cast((alphaRows[2][x >> 1] & mask) << shift); + uint8_t alpha3 = static_cast((alphaRows[3][x >> 1] & mask) << shift); + uint8_t shadow = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + dst[n] = static_cast(((shadow & 0xF0) << 8) | ((alpha1 & 0xF0) << 4) | (alpha2 & 0xF0) | (alpha3 >> 4)); + + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + + if (y == size - 4) { + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + } + + dst += dstPitch; + } +} + +// VERIFY: 3b181805 manual +// OFFSET: 0x007B84A0 +void CMapRenderChunk::PackAlphaShadow4444(uint16_t* dst, uint8_t** alphaRows, uint8_t* shadowRow, uint32_t size) { + uint16_t texel = 0; + uint32_t x = 0; + + for (; x < size - 1; x++) { + uint32_t mask = s_nibbleMask[x & 1]; + uint32_t shift = s_nibbleShift[x & 1]; + uint8_t shadow = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + uint8_t alpha1 = static_cast((alphaRows[1][x >> 1] & mask) << shift); + uint8_t alpha2 = static_cast((alphaRows[2][x >> 1] & mask) << shift); + uint8_t alpha3 = static_cast((alphaRows[3][x >> 1] & mask) << shift); + texel = static_cast(((shadow & 0xF0) << 8) | ((alpha1 & 0xF0) << 4) | (alpha2 & 0xF0) | (alpha3 >> 4)); + dst[x] = texel; + } + + dst[x] = texel; +} + +// VERIFY: d74fae7b manual +// OFFSET: 0x007B85A0 +void __stdcall CMapRenderChunk::UnpackAlphaShadowBitsUnfixed4444Mip0(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap) { + uint16_t* dst = dstBase + dstOffset; + uint8_t* shadowRow = nullptr; + uint8_t* alphaRows[4]; + + for (uint32_t y = 0; y < size - 1; y++) { + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + CMapRenderChunk::PackAlphaShadow4444(dst, alphaRows, shadowRow, size); + dst += dstPitch; + } + + CMapRenderChunk::PackAlphaShadow4444(dst, alphaRows, shadowRow, size); +} + +// VERIFY: 81a839dc manual +// OFFSET: 0x007B8620 +void __stdcall CMapRenderChunk::UnpackAlphaShadowBitsUnfixed4444Mip1(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap) { + uint16_t* dst = dstBase + dstOffset; + + for (uint32_t y = 0; y < size - 1; y += 2) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + + uint16_t* texel = dst; + + for (uint32_t x = 0; x < size - 1; x += 2) { + uint32_t mask = s_nibbleMask[x & 1]; + uint32_t shift = s_nibbleShift[x & 1]; + uint8_t alpha1 = static_cast((alphaRows[1][x >> 1] & mask) << shift); + uint8_t alpha2 = static_cast((alphaRows[2][x >> 1] & mask) << shift); + uint8_t alpha3 = static_cast((alphaRows[3][x >> 1] & mask) << shift); + uint8_t shadow = s_shadowAlpha[(shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]]; + *texel = static_cast(((shadow & 0xF0) << 8) | ((alpha1 & 0xF0) << 4) | (alpha2 & 0xF0) | (alpha3 >> 4)); + texel++; + } + + CMapRenderChunk::SetupAlphaRows4444(size, &shadowRow, alphaRows, cursors, &shadowMap); + + dst += dstPitch; + } +} + +// VERIFY: 0b452c5b manual +// OFFSET: 0x007B87F0 +void CMapRenderChunk::UnpackAlphaShadowBits(void* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, int32_t alphaSlot, uint8_t* shadowMap, int32_t genFormat, int32_t doNotFixAlphaMap) { + if (doNotFixAlphaMap) { + if (genFormat == 3) { + if (this->unk_000A & 0x8) { + CMapRenderChunk::UnpackAlphaShadowBitsFixed4444Mip1(static_cast(dstBase), dstOffset, dstPitch, size, cursors, shadowMap); + } else { + CMapRenderChunk::UnpackAlphaShadowBitsFixed4444Mip0(static_cast(dstBase), dstOffset, dstPitch, size, cursors, shadowMap); + } + } else { + if (this->unk_000A & 0x8) { + CMapRenderChunk::UnpackAlphaShadowBitsFixed8888Mip1(static_cast(dstBase), dstOffset, dstPitch, size, cursors, alphaSlot, shadowMap); + } else { + CMapRenderChunk::UnpackAlphaShadowBitsFixed8888Mip0(static_cast(dstBase), dstOffset, dstPitch, size, cursors, alphaSlot, shadowMap); + } + } + + return; + } + + if (genFormat != 3) { + SErrDisplayAppFatal("CMapChunk::UnpackAlphaShadowBits(): Bad genformat."); + } + + if (this->unk_000A & 0x8) { + CMapRenderChunk::UnpackAlphaShadowBitsUnfixed4444Mip1(static_cast(dstBase), dstOffset, dstPitch, size, cursors, shadowMap); + } else { + CMapRenderChunk::UnpackAlphaShadowBitsUnfixed4444Mip0(static_cast(dstBase), dstOffset, dstPitch, size, cursors, shadowMap); + } +} + +// VERIFY: b610665e manual +// OFFSET: 0x007B88D0 +void __stdcall CMapRenderChunk::RecreateAlphaBitsFixed8888Mip0(CImVector* dst, uint32_t size, SLayerAlpha* layer, uint8_t* shadowMap) { + for (uint32_t y = 0; y < size; y++) { + uint8_t* shadowRow; + + if (shadowMap) { + shadowRow = shadowMap; + shadowMap += size >> 3; + } else { + shadowRow = s_defaultShadowRow; + } + + uint8_t* alphaRow; + + if (layer->m_flags & 0x200) { + alphaRow = s_alphaRowBuf[0]; + layer->m_alphaData = CMapRenderChunk::DecodeAlphaRLE(layer->m_alphaData, s_alphaRowBuf[0], size); + } else { + alphaRow = layer->m_alphaData; + layer->m_alphaData += size; + } + + for (uint32_t x = 0; x < size; x++) { + uint32_t alpha = alphaRow[x]; + + if ((shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]) { + alpha = (178 * alpha) >> 8; + } + + CImVector color; + color.b = alpha; + color.g = alpha; + color.r = alpha; + color.a = alpha; + *dst = color; + dst++; + } + } +} + +// VERIFY: 7b569ca8 manual +// OFFSET: 0x007B89C0 +void __stdcall CMapRenderChunk::RecreateAlphaBitsFixed8888Mip1(CImVector* dst, uint32_t size, SLayerAlpha* layer, uint8_t* shadowMap) { + for (uint32_t y = 0; y < size; y += 2) { + uint8_t* shadowRow; + + if (shadowMap) { + shadowRow = shadowMap; + shadowMap += size >> 3; + } else { + shadowRow = s_defaultShadowRow; + } + + uint8_t* alphaRow; + + if (layer->m_flags & 0x200) { + alphaRow = s_alphaRowBuf[0]; + layer->m_alphaData = CMapRenderChunk::DecodeAlphaRLE(layer->m_alphaData, s_alphaRowBuf[0], size); + } else { + alphaRow = layer->m_alphaData; + layer->m_alphaData += size; + } + + uint32_t x = 0; + + for (; x < size - 2; x += 2) { + uint32_t alpha = alphaRow[x]; + + if ((shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]) { + alpha = (178 * alpha) >> 8; + } + + CImVector color; + color.b = alpha; + color.g = alpha; + color.r = alpha; + color.a = alpha; + *dst = color; + dst++; + } + + x++; + + uint32_t alpha = alphaRow[x]; + + if ((shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]) { + alpha = (178 * alpha) >> 8; + } + + CImVector color; + color.b = alpha; + color.g = alpha; + color.r = alpha; + color.a = alpha; + *dst = color; + dst++; + + if (y < size - 2) { + if (shadowMap) { + shadowMap += size >> 3; + } + + if (layer->m_flags & 0x200) { + layer->m_alphaData = CMapRenderChunk::DecodeAlphaRLE(layer->m_alphaData, s_alphaRowBuf[0], size); + } else { + layer->m_alphaData += size; + } + } + + if (y == size - 4) { + if (shadowMap) { + shadowMap += size >> 3; + } + + if (layer->m_flags & 0x200) { + layer->m_alphaData = CMapRenderChunk::DecodeAlphaRLE(layer->m_alphaData, s_alphaRowBuf[0], size); + } else { + layer->m_alphaData += size; + } + } + } +} + +// VERIFY: 24f3ebfc manual +// OFFSET: 0x007B8B80 +void CMapRenderChunk::UnpackAlphaBitsFixed8888Mip0(CImVector* dst, uint32_t size, uint8_t* shadowMap) { + SLayerAlpha layers[4]; + this->CollectLayerAlphaPtrs(this->m_mapChunks[0], layers); + + for (uint32_t y = 0; y < size; y++) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, layers, size, alphaRows); + + for (uint32_t x = 0; x < size; x++) { + uint32_t alpha = 255 - alphaRows[1][x] - alphaRows[3][x] - alphaRows[2][x]; + + if ((shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]) { + alpha = (178 * alpha) >> 8; + } + + CImVector color; + color.b = alpha; + color.g = alpha; + color.r = alpha; + color.a = alpha; + *dst = color; + dst++; + } + } +} + +// VERIFY: d75a2a5a manual +// OFFSET: 0x007B8C70 +void CMapRenderChunk::UnpackAlphaBitsFixed8888Mip1(CImVector* dst, uint32_t size, uint8_t* shadowMap) { + SLayerAlpha layers[4]; + this->CollectLayerAlphaPtrs(this->m_mapChunks[0], layers); + + for (uint32_t y = 0; y < size; y += 2) { + uint8_t* shadowRow; + uint8_t* alphaRows[4]; + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, layers, size, alphaRows); + + uint32_t x = 0; + + for (; x < size - 2; x += 2) { + uint32_t alpha = 255 - alphaRows[1][x] - alphaRows[3][x] - alphaRows[2][x]; + + if ((shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]) { + alpha = (178 * alpha) >> 8; + } + + CImVector color; + color.b = alpha; + color.g = alpha; + color.r = alpha; + color.a = alpha; + *dst = color; + dst++; + } + + x++; + + uint32_t alpha = 255 - alphaRows[3][x] - alphaRows[2][x] - alphaRows[1][x]; + + if ((shadowRow[x >> 3] & s_bitMask[x & 7]) >> s_bitShift[x & 7]) { + alpha = (178 * alpha) >> 8; + } + + CImVector color; + color.b = alpha; + color.g = alpha; + color.r = alpha; + color.a = alpha; + *dst = color; + dst++; + + if (y < size - 2) { + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, layers, size, alphaRows); + } + + if (y == size - 4) { + CMapRenderChunk::SetupAlphaRows(&shadowMap, &shadowRow, layers, size, alphaRows); + } + } +} + +// VERIFY: 4cab62df manual +// OFFSET: 0x007B8E20 +void CMapRenderChunk::UnpackAlphaBits(void* dst, uint32_t size, SLayerAlpha* layerInfo, uint8_t* shadowMap, int32_t layerMode, int32_t bigAlpha) { + if (bigAlpha) { + if (layerMode == 3) { + if (this->unk_000A & 0x8) { + CMapRenderChunk::UnpackAlphaBitsFixed4444Mip1(static_cast(dst), size, layerInfo); + } else { + CMapRenderChunk::UnpackAlphaBitsFixed4444Mip0(static_cast(dst), size, layerInfo); + } + + return; + } + + if (layerMode == 2) { + if (layerInfo->m_alphaData) { + if (this->unk_000A & 0x8) { + CMapRenderChunk::RecreateAlphaBitsFixed8888Mip1(static_cast(dst), size, layerInfo, shadowMap); + } else { + CMapRenderChunk::RecreateAlphaBitsFixed8888Mip0(static_cast(dst), size, layerInfo, shadowMap); + } + } else { + if (this->unk_000A & 0x8) { + this->UnpackAlphaBitsFixed8888Mip1(static_cast(dst), size, shadowMap); + } else { + this->UnpackAlphaBitsFixed8888Mip0(static_cast(dst), size, shadowMap); + } + } + + return; + } + } else if (layerMode == 3) { + if (this->unk_000A & 0x8) { + CMapRenderChunk::UnpackAlphaBitsUnfixed4444Mip1(static_cast(dst), size, layerInfo); + } else { + CMapRenderChunk::UnpackAlphaBitsUnfixed4444Mip0(static_cast(dst), size, layerInfo); + } + + return; + } + + SErrDisplayAppFatal("CMapChunk::UnpackAlphaBits(): Bad genformat."); +} + +// VERIFY: 97b6c842 manual +// OFFSET: 0x007B9890 +void CMapRenderChunk::CreateChunkLayerTex(CMapRenderChunkLayer* layer) { + int32_t genFormat = 3; + + if (CMap::header.m_flags & 0x4) { + genFormat = 2; + } + + CMapChunk* chunk = this->m_mapChunks[0]; + SMLayer* chunkLayer = &chunk->m_layers[static_cast(layer->m_layerIndex)]; + uint32_t layerFlags = chunkLayer->m_flags; + uint8_t* alphaData = nullptr; + + if (layerFlags & 0x100) { + alphaData = &chunk->m_additionalShadowmap[chunkLayer->m_offsetInMCAL]; + } + + uint32_t chunkFlags = chunk->m_header->m_flags; + uint8_t* shadowMap = nullptr; + + if ((chunkFlags & 0x1) && (CWorld::enables & Enable_Shadow)) { + shadowMap = chunk->m_shadowMap; + } + + SLayerAlpha layerAlpha; + layerAlpha.m_flags = layerFlags; + layerAlpha.m_alphaData = alphaData; + + CMapArea* area = static_cast(chunk->m_parentLinkList.Head()->m_ref); + this->UnpackAlphaBits(CMapRenderChunk::s_defaultTex, 64 >> area->m_header->m_mampValue, &layerAlpha, shadowMap, genFormat, chunkFlags & 0x8000); +} + +// VERIFY: ffeb78ca manual +// OFFSET: 0x007B9950 +void CMapRenderChunk::CreateShadowTex() { + CMapChunk* chunk = this->m_mapChunks[0]; + uint8_t* shadowMap = nullptr; + + if (chunk->m_header->m_flags & 0x1) { + shadowMap = chunk->m_shadowMap; + } + + CMapArea* area = static_cast(chunk->m_parentLinkList.Head()->m_ref); + uint32_t size = 64 >> area->m_header->m_mampValue; + + if (this->unk_000A & 0x8) { + CMapRenderChunk::ExpandShadowBitsToTexelsMip(reinterpret_cast(CMapRenderChunk::s_defaultTex), size, shadowMap); + } else { + CMapRenderChunk::ExpandShadowBitsToTexels(reinterpret_cast(CMapRenderChunk::s_defaultTex), size, shadowMap); + } +} + +// OFFSET: 0x007B99B0 +void CMapRenderChunk::CreateShaderTexture() { + int32_t genFormat = 3; + + if (CMap::header.m_flags & 0x4) { + genFormat = 2; + } + + CMapChunk* chunk = this->m_mapChunks[0]; + uint32_t layerCount = chunk->m_header->m_nLayers; + uint8_t* shadowMap = nullptr; + SLayerAlpha layers[4]; + layers[0].m_alphaData = nullptr; + layers[1].m_alphaData = nullptr; + layers[2].m_alphaData = nullptr; + layers[3].m_alphaData = nullptr; + + for (uint32_t i = 0; i < layerCount; i++) { + uint32_t layerFlags = chunk->m_layers[i].m_flags; + + if (layerFlags & 0x100) { + layers[i].m_alphaData = &chunk->m_additionalShadowmap[chunk->m_layers[i].m_offsetInMCAL]; + } + + layers[i].m_flags = layerFlags; + } + + uint32_t chunkFlags = chunk->m_header->m_flags; + + if ((chunkFlags & 0x1) && (CWorld::enables & Enable_Shadow)) { + shadowMap = chunk->m_shadowMap; + } + + CMapArea* area = static_cast(chunk->m_parentLinkList.Head()->m_ref); + int32_t size = 64 >> area->m_header->m_mampValue; + int32_t pitch = size; + + if (this->m_unkFlags & 0x2) { + pitch = size * 2; + } + + if (this->unk_000A & 0x8) { + pitch >>= 1; + } + + this->UnpackAlphaShadowBits(CMapRenderChunk::s_defaultTex, 0, pitch, size, layers, 0, shadowMap, genFormat, chunkFlags & 0x8000); + + if (!this->m_mapChunks[1]) { + return; + } + + SMChunk* header = this->m_mapChunks[1]->m_header; + int32_t alphaSlot = 0; + layers[0].m_alphaData = nullptr; + layers[1].m_alphaData = nullptr; + layers[2].m_alphaData = nullptr; + layers[3].m_alphaData = nullptr; + uint32_t chunkLayerCount = header->m_nLayers; + + for (uint32_t j = 0; j < chunkLayerCount; j++) { + SMLayer* chunkLayer = &this->m_mapChunks[1]->m_layers[j]; + + for (uint32_t i = 0; i < this->m_layersCount; i++) { + if (this->m_layers[i].m_textureId != chunkLayer->m_textureId) { + continue; + } + + uint32_t layerFlags = chunkLayer->m_flags; + + if (layerFlags & 0x100) { + layers[i].m_alphaData = &this->m_mapChunks[1]->m_additionalShadowmap[chunkLayer->m_offsetInMCAL]; + } + + layers[i].m_flags = layerFlags; + + if (j == 0) { + alphaSlot = i; + } + + break; + } + } + + uint32_t chunk2Flags = header->m_flags; + uint8_t* shadowMap2 = nullptr; + + if ((chunk2Flags & 0x1) && (CWorld::enables & Enable_Shadow)) { + shadowMap2 = this->m_mapChunks[1]->m_shadowMap; + } + + int32_t offset = 0; + + if (this->m_unkFlags & 0x1) { + offset = size * size; + + if (this->unk_000A & 0x8) { + offset >>= 2; + } + } else if (this->m_unkFlags & 0x2) { + offset = size; + + if (this->unk_000A & 0x8) { + offset = size >> 1; + } + } + + this->UnpackAlphaShadowBits(CMapRenderChunk::s_defaultTex, offset, pitch, size, layers, alphaSlot, shadowMap2, genFormat, chunk2Flags & 0x8000); +} + +// VERIFY: ee6e5438 manual +// OFFSET: 0x007B9BC0 +void CMapRenderChunk::UpdateLayerGxTexture(EGxTexCommand cmd, uint32_t w, uint32_t h, uint32_t d, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + if (cmd != GxTex_Latch) { + return; + } + + CMapRenderChunkLayer* layer = static_cast(userArg); + layer->m_owner->CreateChunkLayerTex(layer); + + uint32_t stride = 4 * w; + + if (!(CMap::header.m_flags & 0x4)) { + stride = 2 * w; + } + + texelStrideInBytes = stride; + texels = CMapRenderChunk::s_defaultTex; +} + +// VERIFY: ea80bed7 manual +// OFFSET: 0x007B9C20 +void CMapRenderChunk::UpdateShadowGxTexture(EGxTexCommand cmd, uint32_t w, uint32_t h, uint32_t d, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + if (cmd != GxTex_Latch) { + return; + } + + static_cast(userArg)->CreateShadowTex(); + texelStrideInBytes = 2 * w; + texels = CMapRenderChunk::s_defaultTex; +} + +// VERIFY: 03ca5953 manual +// OFFSET: 0x007B9C60 +void CMapRenderChunk::UpdateShaderGxTexture(EGxTexCommand cmd, uint32_t w, uint32_t h, uint32_t d, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels) { + if (cmd != GxTex_Latch) { + return; + } + + static_cast(userArg)->CreateShaderTexture(); + texels = CMapRenderChunk::s_defaultTex; + + uint32_t stride = 4 * w; + + if (!(CMap::header.m_flags & 0x4)) { + stride = 2 * w; + } + + texelStrideInBytes = stride; +} + +// VERIFY: db4ab7ce manual +// OFFSET: 0x007B9DE0 +void CMapRenderChunk::AllocLayerTexture(CMapRenderChunkLayer* layer) { + if (layer->m_layerTexture) { + HandleClose(reinterpret_cast(layer->m_layerTexture)); + } + + layer->m_layerTexture = nullptr; + + uint32_t bigAlpha = (CMap::header.m_flags >> 2) & 1; + int32_t baseLayer = bigAlpha && layer->m_layerIndex == 0; + + if (!(layer->m_flags & 0x100) && !baseLayer) { + return; + } + + CMapChunk* chunk = this->m_mapChunks[0]; + int32_t width = 64 >> static_cast(chunk->m_parentLinkList.Head()->m_ref)->m_header->m_mampValue; + int32_t height = 64 >> static_cast(chunk->m_parentLinkList.Head()->m_ref)->m_header->m_mampValue; + + if (this->unk_000A & 0x8) { + width /= 2; + height /= 2; + } + + CTexture* texture; + + if (bigAlpha) { + texture = CMapRenderChunk::AllocTexture(width, height, layer, CMapRenderChunk::UpdateLayerGxTexture, GxTex_Argb8888, GxTex_Argb8888); + } else { + texture = CMapRenderChunk::AllocTexture(width, height, layer, CMapRenderChunk::UpdateLayerGxTexture, GxTex_Argb4444, GxTex_Argb4444); + } + + layer->m_layerTexture = texture; + GxTexUpdate(TextureGetGxTex(texture, 1, nullptr), 0, 0, width, height, 1); +} + +// VERIFY: 20e9adae auto +// OFFSET: 0x007B9EE0 +void CMapRenderChunk::AllocShadowTexture() { + if (this->m_shadowTexture) { + HandleClose(reinterpret_cast(this->m_shadowTexture)); + } + + this->m_shadowTexture = nullptr; + + if (!(CWorld::enables & Enable_Shadow)) { + return; + } + + CMapChunk* chunk = this->m_mapChunks[0]; + + if (!(chunk->m_header->m_flags & 0x1)) { + return; + } + + int32_t width = 64 >> static_cast(chunk->m_parentLinkList.Head()->m_ref)->m_header->m_mampValue; + int32_t height = width; + + if (this->unk_000A & 0x8) { + width /= 2; + height = width; + } + + CTexture* texture = CMapRenderChunk::AllocTexture(width, height, this, CMapRenderChunk::UpdateShadowGxTexture, GxTex_Argb4444, GxTex_Argb4444); + this->m_shadowTexture = texture; + GxTexUpdate(TextureGetGxTex(texture, 1, nullptr), 0, 0, width, height, 1); +} + +// VERIFY: fad6a25c manual +// OFFSET: 0x007B9F90 +void CMapRenderChunk::AllocShaderTexture() { + if (this->m_terrainBlendTexture) { + HandleClose(reinterpret_cast(this->m_terrainBlendTexture)); + } + + this->m_terrainBlendTexture = nullptr; + + int32_t width = 64 >> static_cast(this->m_mapChunks[0]->m_parentLinkList.Head()->m_ref)->m_header->m_mampValue; + int32_t height = width; + + if (this->unk_000A & 0x8) { + width /= 2; + height = width; + } + + if (this->m_unkFlags & 0x1) { + height *= 2; + } else if (this->m_unkFlags & 0x2) { + width *= 2; + } + + CTexture* texture; + + if (CMap::header.m_flags & 0x4) { + texture = CMapRenderChunk::AllocTexture(width, height, this, CMapRenderChunk::UpdateShaderGxTexture, GxTex_Argb8888, GxTex_Argb8888); + } else { + texture = CMapRenderChunk::AllocTexture(width, height, this, CMapRenderChunk::UpdateShaderGxTexture, GxTex_Argb4444, GxTex_Argb4444); + } + + this->m_terrainBlendTexture = texture; + GxTexUpdate(TextureGetGxTex(texture, 1, nullptr), 0, 0, width, height, 1); +} + +// VERIFY: 913e0d32 manual +// OFFSET: 0x007BA050 +void CMapRenderChunk::AllocLayerTextures() { + if (this->m_layersCount) { + int16_t flags = this->unk_000A; + int32_t wantMip = (flags & 0x10) != 0; + int32_t haveMip = (flags & 0x8) != 0; + + if ((flags & 0x20) || wantMip != haveMip) { + this->unk_000A = flags & ~0x8; + + if (this->unk_000A & 0x10) { + this->unk_000A |= 0x8; + } + + if (CMap::gTerrainPixelShadersValid || CMap::enableSpecularTerrain) { + this->AllocShaderTexture(); + } else { + for (uint32_t i = 0; i < this->m_layersCount; i++) { + this->AllocLayerTexture(&this->m_layers[i]); + } + + if (!(CMap::header.m_flags & 0x4)) { + this->AllocShadowTexture(); + } + } + } + } + + this->unk_000A &= ~0x30; +} + +// OFFSET: 0x0079E470 +CGxShader* CMapRenderChunk::GetVertexShader(int32_t pointLights, int32_t layers, int32_t specular, int32_t vertexColor, int32_t a5, int32_t shadow) { + return CMap::vertexShader_Terrain[64 * pointLights + 16 * (layers - 1) + 8 * specular + 4 * vertexColor + 2 * a5 + (shadow != 0)]; +} + +// VERIFY: a33ed04d auto +// OFFSET: 0x007CFBE0 +void CMapRenderChunk::InitializeVertexShaderConstants(const C44Matrix& world, const C44Matrix& view) { + memset(&CMapRenderChunk::s_vertexShaderConsts, 0, sizeof(CMapRenderChunk::s_vertexShaderConsts)); + + SVertexShaderConstants& consts = CMapRenderChunk::s_vertexShaderConsts; + consts.m_worldView = world * view; + consts.m_viewTranspose = view.Transpose(); + + CGxDevice* device = g_theGxDevicePtr; + consts.m_projection = device->m_projNative; + + if (device->DeviceApi() == GxApi_OpenGl) { + consts.m_projection.c0 *= -1.0f; + consts.m_projection.c1 *= -1.0f; + consts.m_projection.c2 *= -1.0f; + consts.m_projection.c3 *= -1.0f; + } + + consts.m_texOffset[0].x = 0.0f; + consts.m_texOffset[0].y = 0.0f; + consts.m_texOffset[0].z = 0.0f; + consts.m_texOffset[0].w = 0.0f; + consts.m_texOffset[1].x = 0.0f; + consts.m_texOffset[1].y = 0.0f; + consts.m_texOffset[1].z = 0.0f; + consts.m_texOffset[1].w = 0.0f; + consts.m_texOffset[2].x = 0.0f; + consts.m_texOffset[2].y = 0.0f; + consts.m_texOffset[2].z = 0.0f; + consts.m_texOffset[2].w = 0.0f; + consts.m_texOffset[3].x = 0.0f; + consts.m_texOffset[3].y = 0.0f; + consts.m_texOffset[3].z = 0.0f; + consts.m_texOffset[3].w = 0.0f; + consts.m_texScale[0].x = 0.0f; + consts.m_texScale[0].y = 0.0f; + consts.m_texScale[0].z = 0.0f; + consts.m_texScale[0].w = 0.0f; + + C3Vector sunDir(0.0f, 0.0f, 0.0f); + CM2Lighting lighting(sunDir); + lighting.AddLight(&CMap::s_mapLight->m_light); + + if (lighting.GetSunLight(sunDir, consts.m_sunAmbient, consts.m_sunDiffuse, consts.m_sunSpecular)) { + C33Matrix rotation(consts.m_worldView); + C3Vector dir = -sunDir * rotation; + consts.m_sunDir.x = dir.x; + consts.m_sunDir.y = dir.y; + consts.m_sunDir.z = dir.z; + consts.m_sunDir.w = 1.0f; + consts.m_sunSpecularW = 20.0f; + } else { + consts.m_sunAmbient = C3Vector(1.0f, 1.0f, 1.0f); + consts.m_sunAmbientW = 1.0f; + consts.m_sunSpecular = C3Vector(0.0f, 0.0f, 0.0f); + consts.m_sunSpecularW = 0.0f; + } + + for (int32_t i = 0; i < 3; i++) { + SVertexShaderConstants::SPointLight& light = consts.m_pointLights[i]; + light.m_position = C3Vector(0.0f, 0.0f, 0.0f); + light.m_positionW = 0.0f; + light.m_color = C3Vector(0.0f, 0.0f, 0.0f); + light.m_colorW = 0.0f; + light.m_attenuation = C3Vector(1.0f, 0.0f, 0.0f); + light.m_attenuationW = 0.0f; + } + + if (g_theGxDevicePtr->MasterEnable(GxMasterEnable_Fog)) { + DayNight::DNInfo* info = DayNight::GetInfo(); + float end = info->m_fog.m_end; + float range = 1.0f / (end - info->m_fog.m_start); + consts.m_fog.x = -(CShaderEffect::s_fogMul * range); + consts.m_fog.y = end * range; + consts.m_fog.z = info->m_fog.m_density; + consts.m_fog.w = 0.0f; + } else { + consts.m_fog.x = 0.0f; + consts.m_fog.y = 1.0f; + consts.m_fog.z = 1.0f; + consts.m_fog.w = 0.0f; + } +} + +// OFFSET: 0x007D0050 +void CMapRenderChunk::SetVertexShader(CMapRenderChunk* renderChunk, int32_t layerCount, int32_t a3) { + SVertexShaderConstants& consts = CMapRenderChunk::s_vertexShaderConsts; + int32_t specular = CMap::enableSpecularTerrain; + int32_t vertexColor = CMapRenderChunk::s_gxBufVertexFormat == GxVBF_PNC; + + CM2Lighting lighting(renderChunk->m_sphere); + World::s_m2Scene->SelectLights(&lighting); + renderChunk->SelectLights(&lighting); + + C3Vector position(0.0f, 0.0f, 0.0f); + int32_t pointLights = 0; + + for (uint32_t i = 0; i < 3; i++) { + SVertexShaderConstants::SPointLight& light = consts.m_pointLights[i]; + + if (lighting.GetPointLight(i, position, light.m_color, light.m_attenuation)) { + light.m_position = position - CWorldScene::s_activeWorldView; + light.m_positionW = 1.0f; + pointLights = 1; + } else { + light.m_position = C3Vector(0.0f, 0.0f, 0.0f); + light.m_positionW = 0.0f; + light.m_color = C3Vector(0.0f, 0.0f, 0.0f); + light.m_colorW = 0.0f; + light.m_attenuation = C3Vector(1.0f, 0.0f, 0.0f); + light.m_attenuationW = 0.0f; + } + } + + consts.m_position.x = renderChunk->m_vec1.x; + consts.m_position.y = renderChunk->m_vec1.y; + consts.m_position.z = renderChunk->m_vec1.z; + consts.m_position.w = 0.0f; + + C4Vector layerScale; + layerScale.x = -CMapChunk::s_geoToTex; + layerScale.y = -CMapChunk::s_geoToTex; + layerScale.z = 0.0f; + layerScale.w = 0.0f; + + for (int32_t i = 1; i < layerCount; i++) { + consts.m_texScale[i] = layerScale; + } + + C4Vector alphaScale; + alphaScale.x = -CMapChunk::s_geoToTex * 0.125f; + alphaScale.y = -CMapChunk::s_geoToTex * 0.125f; + alphaScale.z = 0.0f; + alphaScale.w = 0.0f; + + if (renderChunk->m_unkFlags & 0x1) { + alphaScale.x = CMapChunk::s_geoToTex * -0.0625f; + } else if (renderChunk->m_unkFlags & 0x2) { + alphaScale.y = CMapChunk::s_geoToTex * -0.0625f; + } + + consts.m_texScale[layerCount] = alphaScale; + + CGxShader* shader = CMapRenderChunk::GetVertexShader(pointLights, layerCount - 1, specular, vertexColor, a3, CShadowCache::GetShadowValue()); + g_theGxDevicePtr->RsSet(GxRs_VertexShader, shader); + g_theGxDevicePtr->ShaderConstantsSet(GxSh_Vertex, 0, reinterpret_cast(&consts), sizeof(SVertexShaderConstants) / sizeof(C4Vector)); +} + +// OFFSET: 0x007D06B0 +void CMapRenderChunk::ComputeTexMatrices(C44Matrix& layerMatrix, C44Matrix& alphaMatrix, const C3Vector& offset, float scale) { + layerMatrix.Scale(scale); + + float a0 = layerMatrix.a0; + float a1 = layerMatrix.a1; + float a2 = layerMatrix.a2; + float a3 = layerMatrix.a3; + layerMatrix.a0 = layerMatrix.b0; + layerMatrix.a1 = layerMatrix.b1; + layerMatrix.a2 = layerMatrix.b2; + layerMatrix.a3 = layerMatrix.b3; + layerMatrix.b0 = a0; + layerMatrix.b1 = a1; + layerMatrix.b2 = a2; + layerMatrix.b3 = a3; + layerMatrix.Translate(offset); + + alphaMatrix.Scale(scale * 0.125f); + + a0 = alphaMatrix.a0; + a1 = alphaMatrix.a1; + a2 = alphaMatrix.a2; + a3 = alphaMatrix.a3; + alphaMatrix.a0 = alphaMatrix.b0; + alphaMatrix.a1 = alphaMatrix.b1; + alphaMatrix.a2 = alphaMatrix.b2; + alphaMatrix.a3 = alphaMatrix.b3; + alphaMatrix.b0 = a0; + alphaMatrix.b1 = a1; + alphaMatrix.b2 = a2; + alphaMatrix.b3 = a3; + alphaMatrix.Translate(offset); +} + +// OFFSET: 0x007D0760 +void CMapRenderChunk::RenderMultiPassAdditive(CMapRenderChunk* renderChunk) { + C44Matrix layerMatrix; + C44Matrix alphaMatrix; + C3Vector offset = CWorldScene::s_activeWorldView - renderChunk->m_vec1; + CMapRenderChunk::ComputeTexMatrices(layerMatrix, alphaMatrix, offset, -CMapChunk::s_geoToTex); + GxXformSet(GxXform_Tex0, layerMatrix); + GxXformSet(GxXform_Tex1, alphaMatrix); + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + int32_t specular = (renderChunk->unk_000A & 0x4) != 0; + + for (int32_t i = 0; i < renderChunk->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &renderChunk->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_ReflectionMap); + GxRsSet(GxRs_TextureShader0, 0); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 0); + } + + if (layer->m_flags & 0x40) { + C3Vector move(World::texVect[layer->m_flags & 7]); + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + move.x *= moveScale; + move.y *= moveScale; + move.z *= moveScale; + GxXformTranslate(GxXform_Tex0, move); + } + + if (i == 1) { + GxRsSet(GxRs_BlendingMode, GxBlend_NoAlphaAdd); + GxUpdateAlphaRef(GxRs_AlphaRef); + + CImVector black; + black.b = 0; + black.g = 0; + black.r = 0; + black.a = 0; + GxRsSet(GxRs_FogColor, *reinterpret_cast(&black)); + } + + if (layer->m_layerTexture) { + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(layer->m_layerTexture, 1, nullptr)); + } else { + GxRsSet(GxRs_Texture1, 0); + } + + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_EyeLinear); + GxRsSet(GxRs_TextureShader0, 1); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 1); + } + + if (layer->m_flags & 0x40) { + GxXformSet(GxXform_Tex0, layerMatrix); + } + } + + GxRsSet(GxRs_FogColor, *reinterpret_cast(&CMapRenderChunk::s_fogColor)); + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// OFFSET: 0x007D0D70 +void CMapRenderChunk::RenderMultiPassAlpha(CMapRenderChunk* renderChunk) { + C44Matrix layerMatrix; + C44Matrix alphaMatrix; + C3Vector offset = CWorldScene::s_activeWorldView - renderChunk->m_vec1; + CMapRenderChunk::ComputeTexMatrices(layerMatrix, alphaMatrix, offset, -CMapChunk::s_geoToTex); + GxXformSet(GxXform_Tex0, layerMatrix); + GxXformSet(GxXform_Tex1, alphaMatrix); + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + int32_t specular = (renderChunk->unk_000A & 0x4) != 0; + + for (int32_t i = 0; i < renderChunk->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &renderChunk->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_ReflectionMap); + GxRsSet(GxRs_TextureShader0, 0); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 0); + } + + if (layer->m_flags & 0x40) { + C3Vector move(World::texVect[layer->m_flags & 7]); + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + move.x *= moveScale; + move.y *= moveScale; + move.z *= moveScale; + GxXformTranslate(GxXform_Tex0, move); + } + + if (i == 1) { + GxRsSet(GxRs_BlendingMode, GxBlend_Alpha); + GxUpdateAlphaRef(GxRs_AlphaRef); + } + + if (layer->m_layerTexture) { + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(layer->m_layerTexture, 1, nullptr)); + } else { + GxRsSet(GxRs_Texture1, 0); + } + + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_EyeLinear); + GxRsSet(GxRs_TextureShader0, 1); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 1); + } + + if (layer->m_flags & 0x40) { + GxXformSet(GxXform_Tex0, layerMatrix); + } + } + + if (renderChunk->m_shadowTexture && (CWorld::enables & Enable_Shadow)) { + GxRsSet(GxRs_BlendingMode, GxBlend_Alpha); + GxUpdateAlphaRef(GxRs_AlphaRef); + g_theGxDevicePtr->RsSet(GxRs_Texture0, TextureGetGxTex(CWorld::shadowModTexture, 1, nullptr)); + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(renderChunk->m_shadowTexture, 1, nullptr)); + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + } + + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// OFFSET: 0x007D13F0 +void CMapRenderChunk::RenderMultiPassPixelShaderBigAlpha(CMapRenderChunk* renderChunk) { + C44Matrix layerMatrix; + C44Matrix alphaMatrix; + C3Vector offset = CWorldScene::s_activeWorldView - renderChunk->m_vec1; + CMapRenderChunk::ComputeTexMatrices(layerMatrix, alphaMatrix, offset, -CMapChunk::s_geoToTex); + GxXformSet(GxXform_Tex0, layerMatrix); + GxXformSet(GxXform_Tex1, alphaMatrix); + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(renderChunk->m_terrainBlendTexture, 1, nullptr)); + + int32_t specular = (renderChunk->unk_000A & 0x4) != 0; + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CMapRenderChunk::s_pixelShader[1]); + + for (int32_t i = 0; i < renderChunk->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &renderChunk->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_ReflectionMap); + GxRsSet(GxRs_TextureShader0, 0); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 0); + } + + if (layer->m_flags & 0x40) { + C3Vector move(World::texVect[layer->m_flags & 7]); + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + move.x *= moveScale; + move.y *= moveScale; + move.z *= moveScale; + GxXformTranslate(GxXform_Tex0, move); + } + + if (i == 1) { + GxRsSet(GxRs_BlendingMode, GxBlend_NoAlphaAdd); + GxUpdateAlphaRef(GxRs_AlphaRef); + + if (g_theGxDevicePtr->Caps().unk_0134) { + CImVector black; + black.b = 0; + black.g = 0; + black.r = 0; + black.a = 0; + GxRsSet(GxRs_FogColor, *reinterpret_cast(&black)); + } else { + C4Vector black; + black.x = 0.0f; + black.y = 0.0f; + black.z = 0.0f; + black.w = 0.0f; + GxShaderConstantsSet(GxSh_Pixel, 2, &black.x, 1); + } + + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CMapRenderChunk::s_pixelShader[0]); + } + + GxShaderConstantsSet(GxSh_Pixel, 0, s_layerChannel[i], 1); + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_EyeLinear); + GxRsSet(GxRs_TextureShader0, 1); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 1); + } + + if (layer->m_flags & 0x40) { + GxXformSet(GxXform_Tex0, layerMatrix); + } + } + + if (g_theGxDevicePtr->Caps().unk_0134) { + GxRsSet(GxRs_FogColor, *reinterpret_cast(&CMapRenderChunk::s_fogColor)); + } else { + C4Vector fogColor(CMapRenderChunk::s_fogColor); + GxShaderConstantsSet(GxSh_Pixel, 2, &fogColor.x, 1); + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, static_cast(nullptr)); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, static_cast(nullptr)); + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// OFFSET: 0x007D1AD0 +void CMapRenderChunk::RenderMultiPassPixelShader(CMapRenderChunk* renderChunk) { + C44Matrix layerMatrix; + C44Matrix alphaMatrix; + C3Vector offset = CWorldScene::s_activeWorldView - renderChunk->m_vec1; + CMapRenderChunk::ComputeTexMatrices(layerMatrix, alphaMatrix, offset, -CMapChunk::s_geoToTex); + GxXformSet(GxXform_Tex0, layerMatrix); + GxXformSet(GxXform_Tex1, alphaMatrix); + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(renderChunk->m_terrainBlendTexture, 1, nullptr)); + + int32_t specular = (renderChunk->unk_000A & 0x4) != 0; + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CMapRenderChunk::s_pixelShader[1]); + + for (int32_t i = 0; i < renderChunk->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &renderChunk->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_ReflectionMap); + GxRsSet(GxRs_TextureShader0, 0); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 0); + } + + if (layer->m_flags & 0x40) { + C3Vector move(World::texVect[layer->m_flags & 7]); + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + move.x *= moveScale; + move.y *= moveScale; + move.z *= moveScale; + GxXformTranslate(GxXform_Tex0, move); + } + + if (i == 1) { + GxRsSet(GxRs_BlendingMode, GxBlend_Alpha); + GxUpdateAlphaRef(GxRs_AlphaRef); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CMapRenderChunk::s_pixelShader[0]); + } + + GxShaderConstantsSet(GxSh_Pixel, 0, s_layerChannel[i], 1); + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_EyeLinear); + GxRsSet(GxRs_TextureShader0, 1); + } + + if (layer->m_flags & 0x80) { + GxRsSet(GxRs_Lighting, 1); + } + + if (layer->m_flags & 0x40) { + GxXformSet(GxXform_Tex0, layerMatrix); + } + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, static_cast(nullptr)); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, static_cast(nullptr)); + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// OFFSET: 0x007D20A0 +void CMapRenderChunk::RenderMultiPassVertexPixelShaderBigAlpha(CMapRenderChunk* renderChunk) { + SVertexShaderConstants& consts = CMapRenderChunk::s_vertexShaderConsts; + C4Vector white; + white.x = 1.0f; + white.y = 1.0f; + white.z = 1.0f; + white.w = 1.0f; + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(renderChunk->m_terrainBlendTexture, 1, nullptr)); + + CGxShader* pixelShader = CMapRenderChunk::s_pixelShader[1]; + int32_t specular = (renderChunk->unk_000A & 0x4) != 0; + CMapRenderChunk::SetVertexShader(renderChunk, 2, specular); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, pixelShader); + + for (int32_t i = 0; i < renderChunk->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &renderChunk->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (layer->m_flags & 0x80) { + GxShaderConstantsSet(GxSh_Vertex, 25, &white.x, 1); + } + + if (layer->m_flags & 0x40) { + C4Vector dir = World::texVect[layer->m_flags & 7]; + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + consts.m_texOffset[0].x = -(dir.x * moveScale * 0.125f); + consts.m_texOffset[0].y = -(dir.y * moveScale * 0.125f); + GxShaderConstantsSet(GxSh_Vertex, 13, &consts.m_texOffset[0].x, 1); + } + + if (i == 1) { + GxRsSet(GxRs_BlendingMode, GxBlend_NoAlphaAdd); + GxUpdateAlphaRef(GxRs_AlphaRef); + + if (g_theGxDevicePtr->Caps().unk_0134) { + CImVector black; + black.b = 0; + black.g = 0; + black.r = 0; + black.a = 0; + GxRsSet(GxRs_FogColor, *reinterpret_cast(&black)); + } else { + C4Vector black; + black.x = 0.0f; + black.y = 0.0f; + black.z = 0.0f; + black.w = 0.0f; + GxShaderConstantsSet(GxSh_Pixel, 2, &black.x, 1); + } + + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CMapRenderChunk::s_pixelShader[0]); + + if (specular) { + CMapRenderChunk::SetVertexShader(renderChunk, 2, 0); + } + } + + GxShaderConstantsSet(GxSh_Pixel, 0, s_layerChannel[i], 1); + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + + if (layer->m_flags & 0x80) { + GxShaderConstantsSet(GxSh_Vertex, 25, &consts.m_sunAmbient.x, 1); + } + + if (layer->m_flags & 0x40) { + consts.m_texOffset[0].x = 0.0f; + consts.m_texOffset[0].y = 0.0f; + GxShaderConstantsSet(GxSh_Vertex, 13, &consts.m_texOffset[0].x, 1); + } + } + + if (g_theGxDevicePtr->Caps().unk_0134) { + GxRsSet(GxRs_FogColor, *reinterpret_cast(&CMapRenderChunk::s_fogColor)); + } else { + C4Vector fogColor(CMapRenderChunk::s_fogColor); + GxShaderConstantsSet(GxSh_Pixel, 2, &fogColor.x, 1); + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, static_cast(nullptr)); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, static_cast(nullptr)); + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// OFFSET: 0x007D2520 +void CMapRenderChunk::RenderMultiPassVertexPixelShader(CMapRenderChunk* renderChunk) { + SVertexShaderConstants& consts = CMapRenderChunk::s_vertexShaderConsts; + C4Vector white; + white.x = 1.0f; + white.y = 1.0f; + white.z = 1.0f; + white.w = 1.0f; + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + g_theGxDevicePtr->RsSet(GxRs_Texture1, TextureGetGxTex(renderChunk->m_terrainBlendTexture, 1, nullptr)); + + CGxShader* pixelShader = CMapRenderChunk::s_pixelShader[1]; + int32_t specular = (renderChunk->unk_000A & 0x4) != 0; + CMapRenderChunk::SetVertexShader(renderChunk, 2, specular); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, pixelShader); + + for (int32_t i = 0; i < renderChunk->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &renderChunk->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (layer->m_flags & 0x80) { + GxShaderConstantsSet(GxSh_Vertex, 25, &white.x, 1); + } + + if (layer->m_flags & 0x40) { + C4Vector dir = World::texVect[layer->m_flags & 7]; + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + consts.m_texOffset[0].x = -(dir.x * moveScale * 0.125f); + consts.m_texOffset[0].y = -(dir.y * moveScale * 0.125f); + GxShaderConstantsSet(GxSh_Vertex, 13, &consts.m_texOffset[0].x, 1); + } + + if (i == 1) { + GxRsSet(GxRs_BlendingMode, GxBlend_Alpha); + GxUpdateAlphaRef(GxRs_AlphaRef); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, CMapRenderChunk::s_pixelShader[0]); + + if (specular) { + CMapRenderChunk::SetVertexShader(renderChunk, 2, 0); + } + } + + GxShaderConstantsSet(GxSh_Pixel, 0, s_layerChannel[i], 1); + g_theGxDevicePtr->Draw(&renderChunk->m_batch, 1); + + if (layer->m_flags & 0x80) { + GxShaderConstantsSet(GxSh_Vertex, 25, &consts.m_sunAmbient.x, 1); + } + + if (layer->m_flags & 0x40) { + consts.m_texOffset[0].x = 0.0f; + consts.m_texOffset[0].y = 0.0f; + GxShaderConstantsSet(GxSh_Vertex, 13, &consts.m_texOffset[0].x, 1); + } + } + + g_theGxDevicePtr->RsSet(GxRs_VertexShader, static_cast(nullptr)); + g_theGxDevicePtr->RsSet(GxRs_PixelShader, static_cast(nullptr)); + GxRsSet(GxRs_Texture0, 0); + GxRsSet(GxRs_Texture1, 0); +} + +// OFFSET: 0x007D28B0 +void CMapRenderChunk::RenderSinglePassPixelShader() { + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + C44Matrix layerMatrix; + C44Matrix alphaMatrix; + C3Vector offset = CWorldScene::s_activeWorldView - this->m_vec1; + CMapRenderChunk::ComputeTexMatrices(layerMatrix, alphaMatrix, offset, -CMapChunk::s_geoToTex); + + if (this->unk_000A & 0x1) { + float lighting[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; + + for (uint32_t i = 0; i < this->m_layersCount; i++) { + if (this->m_layers[i].m_flags & 0x80) { + lighting[i] = 0.0f; + } + } + + GxShaderConstantsSet(GxSh_Pixel, 1, lighting, 1); + } + + int32_t specular = (this->unk_000A & 0x4) != 0; + int32_t i = 0; + + for (; i < this->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &this->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture0 + i), gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (i == 0 && specular) { + GxRsSet(GxRs_TexGen0, GxTexGen_ReflectionMap); + GxRsSet(GxRs_TextureShader0, 0); + } else { + GxXformSet(static_cast(GxXform_Tex0 + i), layerMatrix); + + if (layer->m_flags & 0x40) { + C4Vector move = World::texVect[layer->m_flags & 7]; + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + move.x *= moveScale; + move.y *= moveScale; + move.z *= moveScale; + move.w *= moveScale; + GxXformTranslate(static_cast(GxXform_Tex0 + i), C3Vector(move)); + } + } + } + + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture0 + i), TextureGetGxTex(this->m_terrainBlendTexture, 1, nullptr)); + GxXformSet(static_cast(GxXform_Tex0 + i), alphaMatrix); + g_theGxDevicePtr->Draw(&this->m_batch, 1); + + for (; i >= 0; i--) { + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture0 + i), static_cast(nullptr)); + } +} + +// OFFSET: 0x007D2D70 +void CMapRenderChunk::RenderSinglePassVertexPixelShader() { + SVertexShaderConstants& consts = CMapRenderChunk::s_vertexShaderConsts; + + GxRsSet(GxRs_BlendingMode, GxBlend_Opaque); + GxUpdateAlphaRef(GxRs_AlphaRef); + + if (this->unk_000A & 0x1) { + float lighting[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; + + for (uint32_t i = 0; i < this->m_layersCount; i++) { + if (this->m_layers[i].m_flags & 0x80) { + lighting[i] = 0.0f; + } + } + + GxShaderConstantsSet(GxSh_Pixel, 1, lighting, 1); + } + + int32_t specular = (this->unk_000A & 0x4) != 0; + int32_t i = 0; + + for (; i < this->m_layersCount; i++) { + CMapRenderChunkLayer* layer = &this->m_layers[i]; + CGxTex* gxTex = TextureGetGxTex(layer->m_texture, 0, nullptr); + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture0 + i), gxTex); + GxTexSetWrap(gxTex, GxTex_Wrap, GxTex_Wrap); + + if (layer->m_flags & 0x40) { + C4Vector dir = World::texVect[layer->m_flags & 7]; + float moveScale = 1.0f / CMapChunk::s_geoToTex / s_texAnimRate[(layer->m_flags >> 3) & 7]; + consts.m_texOffset[i].x = -(dir.x * moveScale * 0.125f); + consts.m_texOffset[i].y = -(dir.y * moveScale * 0.125f); + } + } + + CGxTex* blendTex = this->m_terrainBlendTexture ? TextureGetGxTex(this->m_terrainBlendTexture, 1, nullptr) : nullptr; + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture0 + i), blendTex); + + CMapRenderChunk::SetVertexShader(this, this->m_layersCount + 1, specular); + g_theGxDevicePtr->Draw(&this->m_batch, 1); + + for (uint32_t j = 0; j < this->m_layersCount; j++) { + if (this->m_layers[j].m_flags & 0x40) { + consts.m_texOffset[j].x = 0.0f; + consts.m_texOffset[j].y = 0.0f; + GxShaderConstantsSet(GxSh_Vertex, 13 + j, &consts.m_texOffset[j].x, 1); + } + } + + for (; i >= 0; i--) { + g_theGxDevicePtr->RsSet(static_cast(GxRs_Texture0 + i), static_cast(nullptr)); + } +} + +// OFFSET: 0x007D3E10 +void CMapRenderChunk::SetShaders(int32_t a1, int32_t a2) { + CMapRenderChunk::s_pixelShader[1] = nullptr; + CMapRenderChunk::s_pixelShader[0] = nullptr; + CMapRenderChunk::s_currentShaderX[0] = nullptr; + CMapRenderChunk::s_currentShaderX[1] = nullptr; + CMapRenderChunk::s_currentShaderX[2] = nullptr; + CMapRenderChunk::s_currentShaderX[3] = nullptr; + CMapRenderChunk::s_currentShader[0] = nullptr; + CMapRenderChunk::s_currentShader[1] = nullptr; + CMapRenderChunk::s_currentShader[2] = nullptr; + CMapRenderChunk::s_currentShader[3] = nullptr; + + if (!CMap::gTerrainPixelShadersValid) { + if (CMap::header.m_flags & 0x4) { + CMapRenderChunk::s_renderLayersFunc = CMapRenderChunk::RenderMultiPassAdditive; + } else { + CMapRenderChunk::s_renderLayersFunc = CMapRenderChunk::RenderMultiPassAlpha; + } + + return; + } + + int32_t bigAlpha = (CMap::header.m_flags >> 2) & 1; + int32_t shadowMode = CShadowCache::GetShadowValue(); + CMapRenderChunk::s_pixelShader[1] = CMap::GetPixelShader(bigAlpha, true, a1); + CMapRenderChunk::s_pixelShader[0] = CMap::GetPixelShader(bigAlpha, false, a1); + + for (int32_t layers = 1; layers <= 4; layers++) { + CGxShader* shader = CMap::GetTerrainPixelShader(bigAlpha, layers, shadowMode, a1, a2); + + if (layers <= g_theGxDevicePtr->Caps().m_numTmus && shader && shader->Valid()) { + CMapRenderChunk::s_currentShaderX[layers - 1] = shader; + } + } + + if (CMap::header.m_flags & 0x4) { + if (CMap::enableTerrainShaderVertex) { + CMapRenderChunk::s_renderLayersFunc = CMapRenderChunk::RenderMultiPassVertexPixelShaderBigAlpha; + } else { + CMapRenderChunk::s_renderLayersFunc = CMapRenderChunk::RenderMultiPassPixelShaderBigAlpha; + } + } else { + CMapRenderChunk::s_renderLayersFunc = CMapRenderChunk::RenderMultiPassVertexPixelShader; + + if (!CMap::enableTerrainShaderVertex) { + CMapRenderChunk::s_renderLayersFunc = CMapRenderChunk::RenderMultiPassPixelShader; + } + } +} diff --git a/WoW/Source/WorldClient/MapRenderChunk.h b/WoW/Source/WorldClient/MapRenderChunk.h index 73767a3..76de4f0 100644 --- a/WoW/Source/WorldClient/MapRenderChunk.h +++ b/WoW/Source/WorldClient/MapRenderChunk.h @@ -7,6 +7,15 @@ #include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/CAaSphere.h" #include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Gx/CGxTex.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/C4Vector.h" +#include "Engine/Source/Tempest/CImVector.h" + +class CMapArea; +class CM2Lighting; +class CGxShader; +struct SMLayer; struct CGxBuf; struct CGxPool; @@ -36,6 +45,7 @@ class CMapChunkBufBlock { /* 0044 */ TSLink m_bufLink; CMapChunkBufBlock(); + ~CMapChunkBufBlock(); }; class CMapRenderChunkLayer { @@ -44,12 +54,18 @@ class CMapRenderChunkLayer { /* 0002 */ int16_t m_layerIndex; /* 0004 */ CTexture* m_texture; /* 0008 */ int32_t m_textureId; - /* 000C */ int32_t m_layerTexture; + /* 000C */ CTexture* m_layerTexture; /* 0010 */ CMapRenderChunk* m_owner; + + CMapRenderChunkLayer(); }; class CMapRenderChunk { public: + struct SLayerAlpha; + struct SVertexShaderConstants; + typedef void (*RenderLayersFunc)(CMapRenderChunk* renderChunk); + static int32_t s_bPoolsDirty; static EGxVertexBufferFormat s_gxBufVertexFormat; static uint32_t s_pnEstimateVertex; @@ -59,22 +75,64 @@ class CMapRenderChunk { static TSGrowableArray s_chunkBlockArray; static TSExplicitList s_renderChunkBufFreeList; static TSExplicitList s_chunkBufBlockFreeList; + static TSExplicitList s_bufList; static CGxBatch unk_AEEC60; + static uint32_t s_defaultTex[8192]; + static CGxShader* s_currentShader[4]; + static CGxShader* s_currentShaderX[4]; + static CGxShader* s_pixelShader[2]; + static RenderLayersFunc s_renderLayersFunc; + static CImVector s_fogColor; + static SVertexShaderConstants s_vertexShaderConsts; static void Destroy(); static void DestroyBufs(); static void Initialize(); static void SetDirty(); - - ~CMapRenderChunk(); + static void UpdatePools(); + static void SetVertexFormat(EGxVertexBufferFormat format); + static void PurgeChunkBufBlocks(); + static CMapChunkBuf* AllocBuf(int32_t a1, CMapRenderChunk* renderChunk); + static CTexture* AllocTexture(uint32_t width, uint32_t height, void* userArg, GxTexUserFunc userFunc, EGxTexFormat format, EGxTexFormat dataFormat); + static void UpdateShaderGxTexture(EGxTexCommand cmd, uint32_t w, uint32_t h, uint32_t d, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); + static void UpdateLayerGxTexture(EGxTexCommand cmd, uint32_t w, uint32_t h, uint32_t d, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); + static void UpdateShadowGxTexture(EGxTexCommand cmd, uint32_t w, uint32_t h, uint32_t d, uint32_t mipLevel, void* userArg, uint32_t& texelStrideInBytes, const void*& texels); + static void RenderMultiPassAlpha(CMapRenderChunk* renderChunk); + static void RenderMultiPassAdditive(CMapRenderChunk* renderChunk); + static void SetShaders(int32_t a1, int32_t a2); + static void InitializeVertexShaderConstants(const C44Matrix& world, const C44Matrix& view); + static CGxShader* GetVertexShader(int32_t pointLights, int32_t layers, int32_t specular, int32_t vertexColor, int32_t a5, int32_t shadow); + static void ComputeTexMatrices(C44Matrix& layerMatrix, C44Matrix& alphaMatrix, const C3Vector& offset, float scale); + static void RenderMultiPassPixelShaderBigAlpha(CMapRenderChunk* renderChunk); + static void RenderMultiPassPixelShader(CMapRenderChunk* renderChunk); + static void RenderMultiPassVertexPixelShaderBigAlpha(CMapRenderChunk* renderChunk); + static void RenderMultiPassVertexPixelShader(CMapRenderChunk* renderChunk); + static void SetVertexShader(CMapRenderChunk* renderChunk, int32_t layerCount, int32_t a3); + static uint8_t* DecodeAlphaRLE(uint8_t* src, uint8_t* dst, int32_t count); + static void SetupAlphaRows(uint8_t** shadowCursor, uint8_t** shadowRow, SLayerAlpha* layers, uint32_t size, uint8_t** alphaRows); + static void SetupAlphaRows4444(uint32_t size, uint8_t** shadowRow, uint8_t** alphaRows, SLayerAlpha* layers, uint8_t** shadowCursor); + static void __stdcall UnpackAlphaBitsFixed4444Mip0(uint16_t* dst, uint32_t size, SLayerAlpha* layer); + static void __stdcall UnpackAlphaBitsFixed4444Mip1(uint16_t* dst, uint32_t size, SLayerAlpha* layer); + static void __stdcall UnpackAlphaBitsUnfixed4444Mip0(uint16_t* dst, uint32_t size, SLayerAlpha* layer); + static void __stdcall UnpackAlphaBitsUnfixed4444Mip1(uint16_t* dst, uint32_t size, SLayerAlpha* layer); + static void __stdcall ExpandShadowBitsToTexels(uint16_t* dst, uint32_t size, uint8_t* shadowMap); + static void __stdcall ExpandShadowBitsToTexelsMip(uint16_t* dst, uint32_t size, uint8_t* shadowMap); + static void __stdcall UnpackAlphaShadowBitsFixed8888Mip0(CImVector* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, int32_t alphaSlot, uint8_t* shadowMap); + static void __stdcall UnpackAlphaShadowBitsFixed8888Mip1(CImVector* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, int32_t alphaSlot, uint8_t* shadowMap); + static void __stdcall UnpackAlphaShadowBitsFixed4444Mip0(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap); + static void __stdcall UnpackAlphaShadowBitsFixed4444Mip1(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap); + static void PackAlphaShadow4444(uint16_t* dst, uint8_t** alphaRows, uint8_t* shadowRow, uint32_t size); + static void __stdcall UnpackAlphaShadowBitsUnfixed4444Mip0(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap); + static void __stdcall UnpackAlphaShadowBitsUnfixed4444Mip1(uint16_t* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, uint8_t* shadowMap); + static void __stdcall RecreateAlphaBitsFixed8888Mip0(CImVector* dst, uint32_t size, SLayerAlpha* layer, uint8_t* shadowMap); + static void __stdcall RecreateAlphaBitsFixed8888Mip1(CImVector* dst, uint32_t size, SLayerAlpha* layer, uint8_t* shadowMap); /* 0000 */ TSLink m_renderChunkLink; /* 0008 */ uint8_t m_unkFlags; /* 0009 */ uint8_t m_layersCount; /* 000A */ int16_t unk_000A; /* 000C */ float m_lastUpdateTime; - /* 0010 */ CMapChunk* m_mapChunkPtr1; - /* 0014 */ CMapChunk* m_mapChunkPtr2; + /* 0010 */ CMapChunk* m_mapChunks [2]; /* 0018 */ C3Vector m_vec1; /* 0024 */ CAaSphere m_sphere; /* 0034 */ CMapRenderChunkLayer m_layers [4]; @@ -82,6 +140,64 @@ class CMapRenderChunk { /* 0088 */ CTexture* m_shadowTexture; /* 008C */ CMapChunkBuf* m_chunkBuf; /* 0090 */ CGxBatch m_batch; + + ~CMapRenderChunk(); + CMapRenderChunk(); + void AddBatch(CMapChunk* chunk1, CMapChunk* chunk2, C3Vector* position, uint8_t flags); + void RenderPrep(); + void CreateLayers(); + void CreateLayer(CMapArea* area, SMLayer* layer, int32_t checkDuplicate); + void AllocLayerTextures(); + void AllocShaderTexture(); + void AllocLayerTexture(CMapRenderChunkLayer* layer); + void AllocShadowTexture(); + void UpdateLoaded(); + void UseStreamingBufs(); + void RenderPrepBufs(CGxBuf* vertexBuf, CGxBuf* indexBuf); + void CreateShaderTexture(); + void CreateChunkLayerTex(CMapRenderChunkLayer* layer); + void CreateShadowTex(); + void CollectLayerAlphaPtrs(CMapChunk* chunk, SLayerAlpha* layers); + void UnpackAlphaShadowBits(void* dstBase, int32_t dstOffset, int32_t dstPitch, uint32_t size, SLayerAlpha* cursors, int32_t alphaSlot, uint8_t* shadowMap, int32_t genFormat, int32_t doNotFixAlphaMap); + void UnpackAlphaBitsFixed8888Mip0(CImVector* dst, uint32_t size, uint8_t* shadowMap); + void UnpackAlphaBitsFixed8888Mip1(CImVector* dst, uint32_t size, uint8_t* shadowMap); + void UnpackAlphaBits(void* dst, uint32_t size, SLayerAlpha* layerInfo, uint8_t* shadowMap, int32_t layerMode, int32_t bigAlpha); + void RenderSetup(int32_t a2); + void RenderSolid(); + void RenderSolidVertexPixelShader(); + void FreeBuf(); + void FreeLayers(); + void SelectLights(CM2Lighting* lighting); + void RenderSolidPixelShader(); + void RenderSinglePassPixelShader(); + void RenderSinglePassVertexPixelShader(); +}; + +struct CMapRenderChunk::SVertexShaderConstants { + struct SPointLight { + /* 0000 */ C3Vector m_position; + /* 000C */ float m_positionW; + /* 0010 */ C3Vector m_color; + /* 001C */ float m_colorW; + /* 0020 */ C3Vector m_attenuation; + /* 002C */ float m_attenuationW; + }; + + /* 0000 */ C44Matrix m_worldView; + /* 0040 */ C44Matrix m_projection; + /* 0080 */ C44Matrix m_viewTranspose; + /* 00C0 */ C4Vector m_fog; + /* 00D0 */ C4Vector m_texOffset [4]; + /* 0110 */ C4Vector m_texScale [6]; + /* 0170 */ C4Vector m_position; + /* 0180 */ C4Vector m_sunDir; + /* 0190 */ C3Vector m_sunAmbient; + /* 019C */ float m_sunAmbientW; + /* 01A0 */ C3Vector m_sunDiffuse; + /* 01AC */ float m_sunDiffuseW; + /* 01B0 */ C3Vector m_sunSpecular; + /* 01BC */ float m_sunSpecularW; + /* 01C0 */ SPointLight m_pointLights [3]; }; struct SMMapHeader { @@ -90,7 +206,7 @@ struct SMMapHeader { /* 0008 */ uint32_t m_unused [6]; }; -struct TextureLayerInfo { +struct CMapRenderChunk::SLayerAlpha { /* 0000 */ uint32_t m_flags; /* 0004 */ uint8_t* m_alphaData; }; diff --git a/WoW/Source/WorldClient/Types.h b/WoW/Source/WorldClient/Types.h index 9b97533..4e5cd22 100644 --- a/WoW/Source/WorldClient/Types.h +++ b/WoW/Source/WorldClient/Types.h @@ -123,4 +123,6 @@ struct SMOPortalRef { /* 0006 */ uint16_t m_filler; }; +typedef float DepthCoordTable[256]; + #endif diff --git a/WoW/Source/WorldClient/WaterRipple.cpp b/WoW/Source/WorldClient/WaterRipple.cpp index 54320ca..5a919e2 100644 --- a/WoW/Source/WorldClient/WaterRipple.cpp +++ b/WoW/Source/WorldClient/WaterRipple.cpp @@ -1,7 +1,7 @@ #include "WaterRipple.h" #include "Engine/Source/Base/Handle.h" #include "Engine/Source/Base/Status.h" -#include "WoW/Source/WorldClient/Liquid.h" +#include "WoW/Source/LiquidSystem/Liquid.h" #include "Engine/Source/Gx/CGxDevice/CGxDevice.h" #include "Engine/Source/Gx/CGxDevice/CGxShader.h" #include "Engine/Source/Gx/CGxTexFlags.h" diff --git a/WoW/Source/WorldClient/World.cpp b/WoW/Source/WorldClient/World.cpp index 3c65d3a..3e8db11 100644 --- a/WoW/Source/WorldClient/World.cpp +++ b/WoW/Source/WorldClient/World.cpp @@ -1,4 +1,6 @@ #include "World.h" +#include "WoW/Source/WorldClient/SortTable.h" +#include "WoW/Source/WorldClient/CChunkLiquid.h" #include #include #include "Engine/Source/ConsoleBase/ConsoleCommand.h" @@ -27,6 +29,39 @@ #include "WoW/Source/WorldClient/Zones.h" #include "WoW/Source/WorldClient/Particulate.h" #include "WoW/Common/AaBsp.h" +#include +#include +#include "Engine/Source/Base/RCString.h" +#include "WoW/Source/WorldClient/MapObjGroup.h" +#include "Engine/Source/Tempest/C4Plane.h" +#include "Engine/Source/Tempest/C4Vector.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "Engine/Source/Tempest/CFacet.h" +#include "Engine/Source/Tempest/CFrustum.h" +#include "WoW/Source/WorldClient/ClipInfo.h" +#include "Engine/Source/Model2/M2Light.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" +#include "WoW/Source/WorldClient/MapChunk.h" +#include "WoW/Source/WorldClient/MapDoodadDef.h" +#include "WoW/Source/WorldClient/MapEntity.h" +#include "WoW/Source/WorldClient/MapObjDef.h" +#include "WoW/Source/WorldClient/MapLoad.h" +#include "Engine/Source/Tempest/CMath.h" +#include "WoW/Source/WorldClient/CPortalView.h" +#include "Engine/Source/Tempest/CRect.h" +#include "WoW/Source/WorldClient/CWorldMath.h" +#include "WoW/Source/WorldClient/CWorldOcclusion.h" +#include "WoW/Source/WorldClient/WorldView.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Gx/Types.h" +#include "WoW/Source/WorldClient/Types.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Gxu/Gxu.h" +#include "Engine/Source/Gx/CGxMatrixStack.h" +#include "WoW/Source/LiquidSystem/Liquid.h" +#include "WoW/Source/WorldClient/FrameTime.h" +#include "WoW/Source/Object/WGUID.h" +#include "Engine/Source/Gxu/GxuLight.h" // STATIC: 0x00ADEE48 static const char* s_zonePaths[11] = { @@ -202,6 +237,31 @@ uint32_t World::s_texCacheOverride; // STATIC: 0x00CDFFD0 int32_t World::s_waterLOD; +// STATIC: 0x00CD8790 +float s_viewerLiquidDepth; + + +// STATIC: 0x00CD8798 +int32_t CWorldScene::frustumIndex; + +// STATIC: 0x00CDB108 +C3Vector CWorldScene::s_frustumCorners[8]; + +// STATIC: 0x00CDB168 +CWFrustum CWorldScene::frustumStack[32]; + +// STATIC: 0x00CD8F90 +C4Plane CWorldScene::camPlaneXY; + +// STATIC: 0x00CD9048 +CSortTable CWorldScene::sortTable; + +// STATIC: 0x00CDFFF4 +CALLBACK_FUNC World::s_loadProgressCallback; + +// STATIC: 0x00CDFFF0 +void* World::s_loadProgressParam; + // VERIFY: 8564474b manual // OFFSET: 0x005EEB70 void World::SetTexLodBias(float bias) { @@ -284,6 +344,65 @@ static void ZonesInitialize() { } } +// VERIFY: 6886f483 auto +// OFFSET: 0x0077F1E0 +int32_t World::QueryObjectLiquid(CMapEntity* entity, int32_t* liquidType, float* height, int32_t* a4) { + if (!entity || !(entity->unk_007C & 0x20)) { + return 0; + } + + *height = entity->unk_0080; + *liquidType = entity->unk_00BC; + *a4 = 0; + + return 1; +} + +// VERIFY: 52533b77 auto +// OFFSET: 0x0077F220 +int32_t World::QueryObjectLiquidFlags(CMapEntity* entity, int32_t* flag8, int32_t* flag9) { + if (!entity || !(entity->unk_007C & 0x20)) { + return 0; + } + + *flag8 = (entity->unk_007C >> 8) & 1; + *flag9 = (entity->unk_007C >> 9) & 1; + + return 1; +} + +// VERIFY: 305f137e auto +// OFFSET: 0x0077F360 +int32_t World::QueryLiquidStatus(C3Vector* pos, int32_t* liquidType, float* height) { + int32_t a4 = 0; + return CMap::QueryLiquidStatus(pos, liquidType, height, &a4, 0); +} + +// VERIFY: d8d97f86 auto +// OFFSET: 0x0077F390 +int32_t World::QueryLiquidSounds(CMapEntity* entity, float radius, uint32_t* results) { + if (entity->unk_007C & 0x1) { + uint32_t distance = 9999; + entity->QueryLiquidSounds(results, &distance); + + if (distance < 2) { + CMap::QueryLiquidSounds(&entity->m_position, radius, results); + } + + return 1; + } + + CMap::QueryLiquidSounds(&entity->m_position, radius, results); + + return 1; +} + +// VERIFY: baf0a035 auto +// OFFSET: 0x0077F400 +void World::CreateWaterRipple(const C3Vector& pos, float a2, float a3, float a4, float a5, float a6, int32_t a7, int32_t a8) { + CMap::CreateWaterRipple(pos, a2, a3, a4, a5, a6, a7, a8); +} + // VERIFY: e146f1dc auto // OFFSET: 0x0077F490 void World::SetNearClip(float nearClip) { @@ -302,6 +421,28 @@ void World::SetHorizonNearClipScale(float scale) { World::s_horizonNearClipScale = scale; } +// VERIFY: 7db3332e auto +// OFFSET: 0x00780620 +int32_t World::SceneCamLiquidStatus(float* depth) { + if (depth) { + *depth = s_viewerLiquidDepth; + } + + return s_liquidTypeId; +} + +// VERIFY: fa04a252 auto +// OFFSET: 0x00780640 +C3Vector* World::GetCamPos() { + return &CWorldScene::s_activeWorldView; +} + +// VERIFY: 21d7eb14 auto +// OFFSET: 0x00780650 +C3Vector* CWorldScene::GetCamTarget() { + return &CWorldScene::camTarget; +} + // VERIFY: bf7ac74d auto // OFFSET: 0x00780710 void World::SetDetailDoodadDensity(int32_t density) { @@ -530,3 +671,362 @@ void CWorld::Initialize() { ZonesInitialize(); } + +// VERIFY: 0ee74c0d auto +// OFFSET: 0x00406DE0 +bool World::IsValidPosition(float x, float y, float z, float a4) { +if (_finite(x) && _finite(y) && _finite(z)) { + float v4 = -(y - 17066.666f); + float v5 = -(x - 17066.666f); + if (a4 > v4) + return 0; + if (34133.332f - a4 > v4 && a4 <= v5 && v5 < 34133.332f - a4) + return 1; +} +return 0; +} + + + + +// VERIFY: 91f1484d auto +// OFFSET: 0x0077F8D0 +int32_t World::GetFlightBoundsLower(const C3Vector& pos, float* height) { + return CMap::GetFlightBounds(pos, height, 1); +} + + + + +// OFFSET: 0x00783910 +bool World::GetFacets(CAaBox* a1, CAaBox* a2, FacetData* a3, uint32_t a4, uint32_t* a5) { + a3->facets.SetCount(0); + a3->facetIds.SetCount(0); + if (a5) + *a5 = 0; + if ((a4 & 0x4000) == 0) + return CMap::GetFacets(a1, a2, a3, a4, a5) != 0; + + static FacetData facetData = FacetData(); + + facetData.facets.SetCount(0); + facetData.facetIds.SetCount(0); + if (!CMap::GetFacets(a1, a2, &facetData, a4, a5)) + return 0; + + for (int32_t i = 0; i < facetData.facets.Count(); i++) { + if (facetData.facets[i].plane.n.z >= 0.64278764f) { + a3->facets.Add(1, &facetData.facets[i]); + a3->facetIds.Add(1, &facetData.facetIds[i]); + } + } + return 1; +} + + + + +// VERIFY: 9d92b389 auto +// OFFSET: 0x0078FB00 +void CWorldScene::FrustumXform(C44Matrix& mat) { + CWorldScene::frustumStack[CWorldScene::frustumIndex].Transform(mat); +} + +// VERIFY: ad5c1827 auto +// OFFSET: 0x0078FB20 +int32_t CWorldScene::FrustumCull(CAaBox* box) { + return CWorldScene::frustumStack[CWorldScene::frustumIndex].Cull(*box) == 0; +} + +// VERIFY: 7ebc59b0 auto +// OFFSET: 0x0078FB50 +void CWorldScene::FrustumPop() { + CWorldScene::frustumIndex--; +} + +// OFFSET: 0x00790650 +void CWorldScene::GetNearestCornerToCamera(CAaBox* box, C3Vector* outCorner) { + outCorner->x = (CWorldScene::camTarget.x >= CWorldScene::s_activeWorldView.x) + ? box->b.x + : box->t.x; + + outCorner->y = (CWorldScene::camTarget.y >= CWorldScene::s_activeWorldView.y) + ? box->b.y + : box->t.y; + + outCorner->z = (CWorldScene::camTarget.z >= CWorldScene::s_activeWorldView.z) + ? box->b.z + : box->t.z; +} + + + + + +// VERIFY: d30349f9 auto +// OFFSET: 0x00790A80 +void CWorldScene::RenderLiquidPass() { + CWorldScene::ApplyFogState(); + + if ((CWorld::enables & Enable_Water) && CWorldScene::s_unk_CD8610) { + CWorldScene::s_unk_CD8610->Flush(CWorldScene::s_activeWorldView, 1); + } + + CMap::RenderWaterRipples(); +} + +// VERIFY: d2772b54 manual +// OFFSET: 0x00790AF0 +void CWorldScene::FrustumSet(CRect* rect) { + C3Vector corners[8]; + + for (uint32_t i = 0; i < 8; i += 4) { + C3Vector* src = &CWorldScene::s_frustumCorners[i]; + + C3Vector edgeTop(src[2].x - src[1].x, src[2].y - src[1].y, src[2].z - src[1].z); + C3Vector topLeft(edgeTop.x * rect->minX + src[1].x, edgeTop.y * rect->minX + src[1].y, edgeTop.z * rect->minX + src[1].z); + C3Vector topRight(edgeTop.x * rect->maxX + src[1].x, edgeTop.y * rect->maxX + src[1].y, edgeTop.z * rect->maxX + src[1].z); + + C3Vector edgeBottom(src[3].x - src[0].x, src[3].y - src[0].y, src[3].z - src[0].z); + C3Vector bottomLeft(edgeBottom.x * rect->minX + src[0].x, edgeBottom.y * rect->minX + src[0].y, edgeBottom.z * rect->minX + src[0].z); + C3Vector bottomRight(edgeBottom.x * rect->maxX + src[0].x, edgeBottom.y * rect->maxX + src[0].y, edgeBottom.z * rect->maxX + src[0].z); + + C3Vector left(topLeft.x - bottomLeft.x, topLeft.y - bottomLeft.y, topLeft.z - bottomLeft.z); + corners[i + 0] = C3Vector(left.x * rect->minY + bottomLeft.x, left.y * rect->minY + bottomLeft.y, left.z * rect->minY + bottomLeft.z); + corners[i + 1] = C3Vector(left.x * rect->maxY + bottomLeft.x, left.y * rect->maxY + bottomLeft.y, left.z * rect->maxY + bottomLeft.z); + + C3Vector right(topRight.x - bottomRight.x, topRight.y - bottomRight.y, topRight.z - bottomRight.z); + corners[i + 3] = C3Vector(right.x * rect->minY + bottomRight.x, right.y * rect->minY + bottomRight.y, right.z * rect->minY + bottomRight.z); + corners[i + 2] = C3Vector(right.x * rect->maxY + bottomRight.x, right.y * rect->maxY + bottomRight.y, right.z * rect->maxY + bottomRight.z); + } + + CWorldScene::frustumStack[CWorldScene::frustumIndex].CalcPlanesFromCorners(corners); +} + +// VERIFY: d9802889 auto +// OFFSET: 0x00791100 +void CWorldScene::FrustumSet(CFrustum* frustum) { + static_cast(CWorldScene::frustumStack[CWorldScene::frustumIndex]) = *frustum; +} + +// VERIFY: 939d636d auto +// OFFSET: 0x00791120 +int32_t CWorldScene::FrustumCull(const C3Vector& center, float radius) { + CAaSphere sphere; + sphere.c = center; + sphere.r = radius; + + return CWorldScene::frustumStack[CWorldScene::frustumIndex].Cull(sphere) == 0; +} + +// OFFSET: 0x00791640 +bool World::NDCXform(CFrustum* frustum, C44Matrix* out, bool includeTranslation) { + C3Vector* corners = frustum->corners; + + float ax = corners[3].x - corners[0].x; + float ay = corners[3].y - corners[0].y; + float az = corners[3].z - corners[0].z; + + float bx = corners[1].x - corners[0].x; + float by = corners[1].y - corners[0].y; + float bz = corners[1].z - corners[0].z; + + float cx = corners[4].x - corners[0].x; + float cy = corners[4].y - corners[0].y; + float cz = corners[4].z - corners[0].z; + + *out = C44Matrix(); + + out->a0 = ax; + out->a1 = ay; + out->a2 = az; + out->b0 = bx; + out->b1 = by; + out->b2 = bz; + out->c0 = cx; + out->c1 = cy; + out->c2 = cz; + + if (includeTranslation) { + out->d0 = corners[0].x; + out->d1 = corners[0].y; + out->d2 = corners[0].z; + } + + float det = out->Determinant(); + + if (fabs(det) < 0.00000023841858f) { + *out = C44Matrix(); + return false; + } + + *out = out->Inverse(det); + + return true; +} + + + + + +// VERIFY: 4c456906 auto +// OFFSET: 0x00791950 +void CWorldScene::FrustumPush() { + CWorldScene::frustumIndex++; + static_cast(CWorldScene::frustumStack[CWorldScene::frustumIndex]) = CWorldScene::frustumStack[CWorldScene::frustumIndex - 1]; +} + +// VERIFY: b58d1116 manual +// OFFSET: 0x00792DF0 +void CWorldScene::AddToDistanceBucket(CChunkLiquid* liquid, const C3Vector& pos) { + int32_t bucket = 0; + float distance = pos.y * CWorldScene::camPlaneXY.n.y + pos.z * CWorldScene::camPlaneXY.n.z + pos.x * CWorldScene::camPlaneXY.n.x + CWorldScene::camPlaneXY.d; + + if (distance > 0.0f) { + float scaled = distance * 0.03f; + bucket = CMath::fint_(scaled - 0.5f); + + if (bucket >= 64) { + return; + } + } + + CWorldScene::sortTable.m_table[bucket].m_liquidList.LinkToTail(liquid); +} + +// OFFSET: 0x00792FC0 +void CWorldScene::AddViewerGroup(TSGrowableArray* group, uint16_t val) { + for (int32_t i = 0; i < group->Count(); i++) { + if (group->Ptr()[i] == val) { + return; + } + } + + group->Add(1, &val); +} + + + + + + + + + + + + + + + + + + +// VERIFY: 44ac3c58 auto +// OFFSET: 0x007935A0 +void CWorldScene::CullLiquid(CSortEntry* entry) { + if (!(CWorld::enables & Enable_Water)) { + CChunkLiquid* liquid = entry->m_liquidList.Head(); + + while (liquid) { + CChunkLiquid* next = entry->m_liquidList.Next(liquid); + CMap::s_mapChunkLiquidUpdateList.LinkToTail(liquid); + liquid = next; + } + } else { + CChunkLiquid* liquid = entry->m_liquidList.Head(); + + while (liquid) { + CChunkLiquid* next = entry->m_liquidList.Next(liquid); + CMap::s_mapChunkLiquidUpdateList.LinkToTail(liquid); + + CAaBox box; + liquid->GetAaBox(box); + + if (CWorldScene::frustumStack[CWorldScene::frustumIndex].Cull(box) && !CWorldOcclusion::QueryBuffer(&box, 0) && !CWorldOcclusion::QueryVolumes(liquid->GetSphere())) { + liquid->RenderPrep(); + + if (CWorldScene::s_unk_CD8610 && liquid->m_instance) { + CWorldScene::s_unk_CD8610->Draw(liquid->m_instance); + } + } + + liquid = next; + } + } +} + +// OFFSET: 0x007A3E40 +void World::AddAaBoxFacets(CAaBox* box, FacetData* facets) { + static const int32_t s_boxTriIndex[36] = { 0, 2, 3, 0, 1, 2, 1, 6, 2, 1, 5, 6, 4, 6, 5, 4, 7, 6, 0, 7, 4, 0, 3, 7, 0, 5, 1, 0, 4, 5, 2, 7, 3, 2, 6, 7 }; + + if (box->t.x <= box->b.x || box->t.y <= box->b.y || box->t.z <= box->b.z) + return; + + uint32_t firstFacet = facets->facets.Count(); + + C3Vector corner[8]; + corner[0] = { box->b.x, box->t.y, box->b.z }; + corner[1] = { box->t.x, box->t.y, box->b.z }; + corner[2] = { box->t.x, box->b.y, box->b.z }; + corner[3] = { box->b.x, box->b.y, box->b.z }; + corner[4] = { box->b.x, box->t.y, box->t.z }; + corner[5] = { box->t.x, box->t.y, box->t.z }; + corner[6] = { box->t.x, box->b.y, box->t.z }; + corner[7] = { box->b.x, box->b.y, box->t.z }; + + C4Plane plane[6]; + plane[0].From3Pos(corner[0], corner[2], corner[3]); + plane[1].From3Pos(corner[1], corner[6], corner[2]); + plane[2].From3Pos(corner[4], corner[6], corner[5]); + plane[3].From3Pos(corner[0], corner[7], corner[4]); + plane[4].From3Pos(corner[0], corner[5], corner[1]); + plane[5].From3Pos(corner[2], corner[7], corner[3]); + + CFacet facet(0.0f); + for (int32_t i = 0; i < 36; i += 3) { + facet.plane = plane[i / 6]; + facet.v[0] = corner[s_boxTriIndex[i]]; + facet.v[1] = corner[s_boxTriIndex[i + 1]]; + facet.v[2] = corner[s_boxTriIndex[i + 2]]; + facets->facets.Add(1, &facet); + } + + uint32_t count = facets->facets.Count(); + facets->facetIds.SetCount(count); + for (uint32_t i = firstFacet; i < count; i++) + facets->facetIds[i] = 0; +} + +// OFFSET: 0x007A6B90 +void CWorldScene::CalcScreenRectFromVerts(CRect& rect, C3Vector* clippedVerts, uint32_t clippedCount) { + rect.minX = 3.4028235e38; + rect.maxX = -3.4028235e38; + rect.maxY = -3.4028235e38; + rect.minY = 3.4028235e38; + + for (uint32_t i = 0; i < clippedCount; i++) { + if (rect.minX > clippedVerts[i].x) + rect.minX = clippedVerts[i].x; + if (rect.maxX < clippedVerts[i].x) + rect.maxX = clippedVerts[i].x; + if (rect.minY > clippedVerts[i].y) + rect.minY = clippedVerts[i].y; + if (rect.maxY < clippedVerts[i].y) + rect.maxY = clippedVerts[i].y; + } +} + + + + + + + + + + + + + + diff --git a/WoW/Source/WorldClient/World.h b/WoW/Source/WorldClient/World.h index 7fd2128..f6ce769 100644 --- a/WoW/Source/WorldClient/World.h +++ b/WoW/Source/WorldClient/World.h @@ -7,9 +7,25 @@ #include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Event/Types.h" #include "Engine/Source/Storm/H/stpl.h" +#include "WoW/Source/WorldClient/CMapBaseObjLink.h" #include "Engine/Source/Tempest/CRect.h" +#include "Engine/Source/Tempest/C4Plane.h" #include "Engine/Source/Tempest/C4Vector.h" #include "Engine/Source/Tempest/CWFrustum.h" +#include "WoW/Source/Object/WGUID.h" + +class CAaSphere; +class CFacet; +class CFrustum; +class CM2Lighting; +class CM2Model; +class CMapBaseObjLink; +class CMapObj; +class CMapObjDef; +class CMapObjDefGroup; +class CPortalView; +struct SMOPortal; +struct SMOPortalRef; class CAaBox; class C44Matrix; @@ -26,6 +42,10 @@ class CTexture; class Particulate; class Weather; +typedef void (*CALLBACK_FUNC)(float, void*); + +class CChunkLiquid; +class CDetailDoodadInst; class CMapBaseObj; class CMapChunk; class CMapDoodadDef; @@ -40,13 +60,15 @@ class CMapDoodadDefMapChunkLink { /* 0014 */ TSLink m_ownerLink; }; +class CSortTable; + class CSortEntry { public: /* 0000 */ TSExplicitList m_mapChunkList; /* 000C */ TSList m_exteriorGroupList; /* 0018 */ TSList m_entityList; /* 0024 */ TSList m_doodadDefList; - /* 0030 */ TSList m_liquidList; + /* 0030 */ TSList > m_liquidList; /* 003C */ TSList m_occluderList; /* 0048 */ TSList m_unkList7; /* 0054 */ TSList m_holedChunkList; @@ -91,11 +113,19 @@ class CWorldScene { public: static Liquid::CRenderer* s_unk_CD8610; static TSExplicitList frustumFreeList; + static int32_t frustumIndex; + static C3Vector s_frustumCorners[8]; + static CWFrustum frustumStack[32]; static GxuScene s_unk_B2EB68; static CTexture* s_defaultBlendTexture; static CTexture* s_defaultTexture; static float s_unk_CD877C; static CMapEntity* camTargetEntity; + static C3Vector s_activeWorldView; + static C3Vector camTarget; + static C4Plane camPlaneXY; + static CSortTable sortTable; + static CMapObjDef* s_curMapObjDef; static float s_fadeDist[5]; static float s_fadeDistDefault[5]; static float s_fadeDistMin[5]; @@ -108,6 +138,64 @@ class CWorldScene { static void Destroy(); static void Initialize(); static void ScaleFadeDist(float scale); + static void Update(C3Vector* camPos, C3Vector* camTarget); + static void GetNearestCornerToCamera(CAaBox* box, C3Vector* outCorner); + static void AddMapChunk(CMapChunk* mapChunk); + static void AddMapObjDefGroup(CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup); + static void AddMapChunkToRenderList(CMapChunk* mapChunk, C3Vector* pos); + static void AddMapObjDefGroupToSortTable(uint32_t groupNum, CMapObjDef* mapObjDef); + static void AddEntityToSortTable(CMapEntity* entity); + static CFrustum* AllocFrustum(); + static int32_t FrustumCull(CAaBox* box); + static int32_t FrustumCull(const C3Vector& center, float radius); + static void FrustumSet(C3Vector* corners, CRect* rect); + static void FrustumSet(CRect* rect); + static void FrustumSet(CFrustum* frustum); + static void FrustumPush(); + static void FrustumPop(); + static void FrustumXform(C44Matrix& mat); + static bool InsideFrustumRect(CiRect* rect); + static C3Vector* GetCamTarget(); + static void ApplyFogState(); + static void RenderLiquidPass(); + static void CullSortTable(CRect* a1); + static void CullDoodads(CSortEntry* entry, uint8_t fadeLevel); + static void CullLiquid(CSortEntry* entry); + static void AddChunkToSortList(CDetailDoodadInst* inst); + static void AddToDistanceBucket(CChunkLiquid* liquid, const C3Vector& pos); + static void AddDoodadDefModelToModelScene(CMapDoodadDef* a1); + static void AddDoodadDefs(TSExplicitList* linkList, uint32_t a2); + static void CullDoodadsExterior(TSExplicitList* linkList, uint8_t fadeLevel); + static void CullMapObjDefGroup(); + static void CullMapObjDefGroups(CSortEntry* entry, CRect* a2, uint32_t a3); + static void CullMapObjDefGroupFromExterior(CMapObjDef* mapObjDef, CMapObjDefGroup* mapObjDefGroup, CRect* a3, uint32_t a4); + static void CullEntitys(CSortEntry* entry); + static void CullThroughPortal(CRect* rect); + static void CullDoodadsInterior(TSExplicitList* linkList, CFrustum* frustumList, uint8_t fadeLevel, int32_t interior); + static void CullEntitysInterior(TSExplicitList* linkList, CFrustum* frustumList, int32_t force, int32_t interior); + static void CullChunks(CSortEntry* entry, int32_t index); + static void Render(const C3Vector& cameraPos, float time); + static void RenderChunks(); + static void RenderMapObjDefGroups(); + static void SetWorldProjection(C44Matrix& mat); + static void RenderChunksSolid(); + static void SetupMapObjDefCull(CMapObj* mapObj, C44Matrix& a2, C44Matrix& a3, C3Vector& a4, C3Vector& a5); + static void RenderThruPortalsExterior(CMapObj* mapObj, C44Matrix& mat, C44Matrix& invMat, C3Vector& worldPos, C3Vector& camTarget, CRect& a6, int32_t a7); + static void RenderInterior(CMapObj* mapObj, C44Matrix& mat, C44Matrix& invMat, C3Vector& worldPos, C3Vector& camTarget, TSGrowableArray* groups); + static void RenderThruPortals(CMapObj* mapObj, uint32_t groupNum, uint32_t fromGroup, CRect& ndcRect, uint32_t depth, int32_t interior); + static void PushPortalView(CPortalView* portalView); + static void TransformPortal(CMapObj* mapObj, SMOPortal* portal, SPortalExt* portalExt); + static void ClassifyPortalPlane(CMapObj* mapObj, SMOPortal* portal, SPortalExt* portalExt); + static uint32_t TransformAndClipVerts(CMapObj* mapObj, uint32_t a2, C3Vector* verts, uint32_t vertCount, C3Vector& a5, C3Vector*& clippedVerts, uint32_t& clippedCount); + static void ClipVerts(C3Vector* verts, uint32_t count, C3Vector** outVerts, uint32_t* outCount); + static void CalcScreenRectFromVerts(CRect& rect, C3Vector* clippedVerts, uint32_t clippedCount); + static void LocateViewer3(); + static void RenderCollisionDebug(); + static void AddViewerGroup(TSGrowableArray* group, uint16_t val); + static void AddExteriorPortalView(CMapObj* mapObj, SMOPortal* portal, SMOPortalRef* ref, SPortalExt* ext, uint32_t destIsExterior); + static void AddInteriorPortalView(CMapObj* mapObj, SMOPortal* portal, SMOPortalRef* ref, SPortalExt* ext, TSGrowableArray* a5); + static void SetupLighting(CM2Lighting* lighting, C3Vector* view); + static void SetCameraTarget(CMapEntity* entity, WGUID transportGuid); /* 0000 */ uint8_t unk_0000[16]; /* 0010 */ uint32_t unk_0010; @@ -140,7 +228,26 @@ class CWorldScene { }; namespace World { + extern CALLBACK_FUNC s_loadProgressCallback; + extern void* s_loadProgressParam; + + C3Vector* GetCamPos(); + void CreateWaterRipple(const C3Vector& pos, float a2, float a3, float a4, float a5, float a6, int32_t a7, int32_t a8); + int32_t QueryLiquidSounds(CMapEntity* entity, float radius, uint32_t* results); + int32_t QueryLiquidStatus(C3Vector* pos, int32_t* liquidType, float* height); + int32_t QueryObjectLiquid(CMapEntity* entity, int32_t* liquidType, float* height, int32_t* a4); + int32_t QueryObjectLiquidFlags(CMapEntity* entity, int32_t* flag8, int32_t* flag9); + int32_t SceneCamLiquidStatus(float* depth); + + class FacetData { + public: + TSGrowableArray facets; + TSGrowableArray facetIds; + }; + extern CM2Scene* s_m2Scene; +extern float s_viewerLiquidDepth; +extern uint32_t s_liquidTypeId; extern Weather* s_weather; extern float groundEffectDistValueSqr; extern C4Vector texVect[8]; @@ -170,6 +277,14 @@ void SetHorizonNearClipScale(float scale); void SetNearClip(float nearClip); void SetTexLodBias(float bias); float ValidateFarClip(float farClip, int32_t mapID); + bool NDCClip(C3Vector* verts, uint32_t count, C3Vector*** outVerts, uint32_t* outCount); + bool GetFacets(CAaBox* a1, CAaBox* a2, FacetData* a3, uint32_t a4, uint32_t* a5); + bool NDCXform(CFrustum* frustum, C44Matrix* out, bool includeTranslation); + bool IsValidPosition(float x, float y, float z, float a4); + void AddAaBoxFacets(CAaBox* box, FacetData* facets); + uint32_t TriDataToFacetData(void* unused, FacetData* facets, WGUID guid); + int32_t GetFlightBoundsLower(const C3Vector& pos, float* height); + void ObjectSetModel(CMapEntity* entity, CM2Model* model); } class CWorld { @@ -177,6 +292,7 @@ class CWorld { static int32_t bShowSimpleDoodads; static int32_t bLoadSimpleDoodads; static Particulate* particulate; + static int32_t s_waterRipples; static int32_t shadowMipLevel; static int32_t unk_CD7658; static uint32_t maxLights; @@ -215,6 +331,9 @@ class CWorld { static int32_t ConsoleCommand_WaterRipples(const char* command, const char* arguments); static void Destroy(); static void Initialize(); + static void LoadMap(const char* mapName, C3Vector* position, int32_t zoneID); + static void Update(C3Vector* camPos, C3Vector* camTarget, C3Vector* position); + static void SetLoadProgressCallback(CALLBACK_FUNC callback, void* param); }; #endif diff --git a/WoW/Source/WorldClient/WorldScene.cpp b/WoW/Source/WorldClient/WorldScene.cpp index 0b883d9..9421aa1 100644 --- a/WoW/Source/WorldClient/WorldScene.cpp +++ b/WoW/Source/WorldClient/WorldScene.cpp @@ -9,9 +9,14 @@ #include "WoW/Source/DB/DBClient/DBClient.h" #include "WoW/Source/WorldClient/Barrier.h" #include "WoW/Source/WorldClient/CWorldOcclusion.h" -#include "WoW/Source/WorldClient/Liquid.h" +#include "WoW/Source/LiquidSystem/Liquid.h" +#include "WoW/Source/WorldClient/LiquidWaveManager.h" +#include "WoW/Source/WorldClient/LiquidGeomFactories.h" +#include "WoW/Source/WorldClient/LiquidEnvironment.h" #include "WoW/Source/WorldClient/World.h" #include "WoW/Source/WorldClient/WorldView.h" +#include "WoW/Source/WorldClient/MapObj.h" +#include "WoW/Source/WorldClient/MapObjDef.h" // STATIC: 0x00ADF350 float CWorldScene::s_fadeSizeDefault[5] = { 1.0f, 4.0f, 15.0f, 100.0f, 100000.0f }; @@ -58,6 +63,15 @@ CMapEntity* CWorldScene::camTargetEntity; // STATIC: 0x00CD87B0 int32_t CWorldScene::s_unk_CD87B0; +// STATIC: 0x00CD8F5C +C3Vector CWorldScene::s_activeWorldView; + +// STATIC: 0x00CD8F68 +C3Vector CWorldScene::camTarget; + +// STATIC: 0x00D1C420 +CMapObjDef* CWorldScene::s_curMapObjDef; + // VERIFY: ce865553 manual // OFFSET: 0x00798310 void CWorldScene::Destroy() { @@ -152,3 +166,11 @@ void CWorldScene::ScaleFadeDist(float scale) { CWorldView::s_fadeDistSqr[4] = CWorldScene::s_fadeDist[4] * CWorldScene::s_fadeDist[4]; CWorldScene::s_fadeDistMinSqr[4] = CWorldScene::s_fadeDistMin[4] * CWorldScene::s_fadeDistMin[4]; } + +// VERIFY: 0f630b8d auto +// OFFSET: 0x007B3B20 +void CMapObjDef::RenderWithCallback(TSGrowableArray* groups) { + CMapObj::SetGroupRenderCallback(reinterpret_cast(CWorldScene::AddMapObjDefGroupToSortTable), this); + CWorldScene::s_curMapObjDef = this; + CWorldScene::RenderInterior(this->m_owner, this->m_mat, this->m_invMat, CWorldScene::s_activeWorldView, CWorldScene::camTarget, groups); +} diff --git a/WoW/Source/WorldClient/World_Console.cpp b/WoW/Source/WorldClient/World_Console.cpp new file mode 100644 index 0000000..380c3c8 --- /dev/null +++ b/WoW/Source/WorldClient/World_Console.cpp @@ -0,0 +1,28 @@ +#include "World.h" +#include +#include "Engine/Source/ConsoleBase/IConsole.h" +#include "WoW/Source/WorldClient/Types.h" + +// STATIC: 0x00ADF7F0 +int32_t CWorld::s_waterRipples = 1; + +// VERIFY: ae98adec auto +// OFFSET: 0x0077F690 +int32_t CWorld::ConsoleCommand_WaterRipples(const char* command, const char* arguments) { + sscanf(arguments, "%d", &CWorld::s_waterRipples); + return 1; +} + +// VERIFY: 05445309 auto +// OFFSET: 0x0077F6B0 +int32_t CWorld::ConsoleCommand_WaterParticulates(const char* command, const char* arguments) { + if (CWorld::enables & 0x2000000) { + ConsoleWrite("Particulates disabled", DEFAULT_COLOR); + CWorld::enables &= ~0x2000000; + } else { + ConsoleWrite("Particulates enabled", DEFAULT_COLOR); + CWorld::enables |= 0x2000000; + } + + return 1; +}