diff --git a/Engine/Source/Model2/CM2SceneRender.cpp b/Engine/Source/Model2/CM2SceneRender.cpp index 8ba3a27..f246083 100644 --- a/Engine/Source/Model2/CM2SceneRender.cpp +++ b/Engine/Source/Model2/CM2SceneRender.cpp @@ -706,7 +706,7 @@ void CM2SceneRender::DrawRibbon() { this->SetupMaterial(); this->SetupBillboardView(C3Vector(0.0f, 0.0f, 0.0f)); CShaderEffect::SetTexMtx_Identity(0); - this->m_curModel->m_ribbonEmitters[this->m_curElement->m_index]->Render(this->m_curModel->m_frameOfReference); + this->m_curModel->m_ribbonEmitters[this->m_curElement->m_index]->Render(static_cast(this->m_curModel->m_frameOfReference)); g_theGxDevicePtr->XformSetView(s_identity); CGxMatrixStack& world = g_theGxDevicePtr->m_xforms[GxXform_World]; @@ -824,7 +824,7 @@ uint32_t CM2SceneRender::DrawBatchedParticle(uint32_t index, M2Element* elements for (uint32_t i = 0; i < batched; i++) { emitter = elements[first[i]].m_emitter; - emitter->Render(this->m_curModel->m_frameOfReference, data, i == 0); + emitter->Render(static_cast(this->m_curModel->m_frameOfReference), data, i == 0); renderedVertices += emitter->m_renderedParticles * emitter->m_vertsPerParticle; data += vertexSize * emitter->m_renderedParticles * emitter->m_vertsPerParticle; renderedIndices += emitter->m_renderedParticles * emitter->m_indicesPerParticle; @@ -886,7 +886,7 @@ uint32_t CM2SceneRender::DrawParticle(uint32_t index, M2Element* elements, uint3 this->SetupLighting(); this->SetupMaterial(); this->SetupBillboardView(*reinterpret_cast(&this->m_scene->m_viewInv.d0)); - emitter->Render(this->m_curModel->m_frameOfReference, nullptr, 1); + emitter->Render(static_cast(this->m_curModel->m_frameOfReference), nullptr, 1); g_theGxDevicePtr->XformSetView(s_identity); CGxMatrixStack& world = g_theGxDevicePtr->m_xforms[GxXform_World]; diff --git a/Engine/Source/Services/CMakeLists.txt b/Engine/Source/Services/CMakeLists.txt index 83642a3..be82da1 100644 --- a/Engine/Source/Services/CMakeLists.txt +++ b/Engine/Source/Services/CMakeLists.txt @@ -8,3 +8,8 @@ target_include_directories(Services PUBLIC ${CMAKE_SOURCE_DIR} ) + +target_link_libraries(Services + PUBLIC + freetype +) diff --git a/Engine/Source/Services/Camera.cpp b/Engine/Source/Services/Camera.cpp new file mode 100644 index 0000000..28a6a5a --- /dev/null +++ b/Engine/Source/Services/Camera.cpp @@ -0,0 +1,445 @@ +#include +#include +#include +#include +#include "Engine/Source/Services/Camera.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Gxu/Gxu.h" +#include "Engine/Source/Services/CameraInt.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/C2Vector.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/CRect.h" + +// STATIC: 0x00B4A304 +static float s_inv2Pi = 1.0f / 6.2831855f; + +// INLINED +CBaseManaged::CBaseManaged() { + this->m_dataTypeId = 0; + this->m_flags = 0; + this->m_updateFcn = nullptr; + this->m_updateData = nullptr; + this->m_updatePriority = 0.0f; +} + +void CBaseManaged::Update(float time) { +} + +void CBaseManaged::Validate(float time) { +} + +// INLINED +template +TManaged::TManaged(const T& value) { + this->m_data = value; +} + +// INLINED +CAngle::CAngle(float angle) : TManaged(0.0f) { + this->Set_(angle); +} + +// VERIFY: 17bd52ca auto +// OFFSET: [0x004BEB90, 0x004BF580] +template +void TManaged::Set_(const T& value) { + if (value != this->m_data) { + this->m_flags |= 0x8; + this->m_data = value; + } +} + +// VERIFY: a5190ff5 manual +// OFFSET: 0x004BEC90 +void CAngle::Set_(const float& value) { + TManaged::Set_(this->ClampTo2Pi(value)); + this->m_cos = cos(this->m_data); + this->m_sin = sin(this->m_data); +} + +// VERIFY: 72a398ea auto +// OFFSET: 0x004BECF0 +void CCamera::SetupWorldProjection(const CRect& rect, uint32_t flags) { + C44Matrix projection; + GxuXformCreateProjection_SG(this->m_fov.m_data, (rect.maxX - rect.minX) / (rect.maxY - rect.minY), this->m_zNear.m_data, this->m_zFar.m_data, projection); + g_theGxDevicePtr->XformSetProjection(projection); + + C44Matrix view; + C3Vector eye(0.0f, 0.0f, 0.0f); + C3Vector target(this->m_target.m_data.x - this->m_position.m_data.x, this->m_target.m_data.y - this->m_position.m_data.y, this->m_target.m_data.z - this->m_position.m_data.z); + C3Vector up(this->m_rotation.m_sin * this->m_roll.m_sin, -(this->m_rotation.m_cos * this->m_roll.m_sin), this->m_roll.m_cos); + GxuXformCreateLookAtSgCompat(eye, target, up, view); + g_theGxDevicePtr->XformSetView(view); + + CShaderEffect::UpdateProjMatrix(); +} + +// VERIFY: 5a7761be auto +// OFFSET: 0x004BEE60 +void CameraSetupScreenProjection(const CRect& rect, const C2Vector& offset, float depth, int32_t forceOrtho) { + float centerX = (rect.minX + rect.maxX) * 0.5f; + float centerY = 0.5f * (rect.minY + rect.maxY); + float left = rect.minX - centerX; + float right = rect.maxX - centerX; + float bottom = rect.minY + -centerY; + float top = -centerY + rect.maxY; + + C44Matrix projection; + + if (forceOrtho || !g_theGxDevicePtr->StereoEnabled()) { + GxuXformCreateOrtho(left, right, bottom, top, -500.0f, 500.0f, projection); + projection.Scale(C3Vector(1.0f, 1.0f, -1.0f)); + } else { + GxuXformCreateOrthoDepth(left, right, bottom, top, -500.0f, 500.0f, projection); + } + + CGxCaps& caps = g_theGxDevicePtr->Caps(); + + if (!caps.m_pixelCenterOnEdge && caps.m_texelCenterOnEdge) { + CRect window = { 0.0f, 0.0f, 0.0f, 0.0f }; + g_theGxDevicePtr->CapsWindowSizeInScreenCoords(window); + projection.d0 = projection.d0 - (rect.maxX - rect.minX) / (window.maxX - window.minX) * 0.5f; + projection.d1 = 0.5f * ((rect.maxY - rect.minY) / (window.maxY - window.minY)) + projection.d1; + } + + GxXformSetProjection(projection); + + C44Matrix view; + view.Translate(C3Vector(offset.x - centerX, offset.y - centerY, 0.0f)); + GxXformSetView(view); +} + +// VERIFY: 523f309e auto +// OFFSET: 0x004BF0C0 +void CameraSetupWorldProjection(CCamera* camera, const CRect& rect, uint32_t flags) { + if (!camera) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + camera->SetupWorldProjection(rect, flags); +} + +// VERIFY: 6c58d581 manual +// OFFSET: 0x004BF0F0 +void CameraGetLineSegment(float x, float y, C3Vector* start, C3Vector* end) { + if (!start || !end || !(x >= 0.0f) || !(x <= 1.0f) || !(y >= 0.0f) || !(y <= 1.0f)) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + C3Vector corners[8]; + C44Matrix view; + C44Matrix projection; + CGxDevice* device = g_theGxDevicePtr; + view = device->m_xforms[GxXform_View].m_mtx[device->m_xforms[GxXform_View].m_level]; + projection = device->m_projection; + GxuXformCalcFrustumCorners(view, projection, corners); + + float nearLeftX = corners[0].x + (corners[1].x - corners[0].x) * y; + float nearLeftY = corners[0].y + (corners[1].y - corners[0].y) * y; + float nearLeftZ = corners[0].z + (corners[1].z - corners[0].z) * y; + float nearRightX = corners[3].x + (corners[2].x - corners[3].x) * y; + float nearRightY = corners[3].y + (corners[2].y - corners[3].y) * y; + float nearRightZ = corners[3].z + (corners[2].z - corners[3].z) * y; + + start->x = nearLeftX + (nearRightX - nearLeftX) * x; + start->y = nearLeftY + (nearRightY - nearLeftY) * x; + start->z = nearLeftZ + (nearRightZ - nearLeftZ) * x; + + float farLeftX = corners[4].x + (corners[5].x - corners[4].x) * y; + float farLeftY = corners[4].y + (corners[5].y - corners[4].y) * y; + float farLeftZ = corners[4].z + (corners[5].z - corners[4].z) * y; + float farRightX = corners[7].x + (corners[6].x - corners[7].x) * y; + float farRightY = corners[7].y + (corners[6].y - corners[7].y) * y; + float farRightZ = corners[7].z + (corners[6].z - corners[7].z) * y; + + end->x = farLeftX + (farRightX - farLeftX) * x; + end->y = farLeftY + (farRightY - farLeftY) * x; + end->z = farLeftZ + (farRightZ - farLeftZ) * x; +} + +// VERIFY: ad64dcf0 auto +// OFFSET: [0x004BF410, 0x004BF4B0] +template +void TManaged::Update(float time) { + if (this->m_updateFcn) { + T value = this->m_data; + this->m_updateFcn(time, this->m_updateData, &value); + this->Set_(value); + } + + this->m_flags &= ~0x4; +} + +// VERIFY: f9cfcece auto +// OFFSET: [0x004BF450, 0x004BF5D0] +template +void TManaged::Validate(float time) { + if (this->m_updateFcn) { + T value = this->m_data; + T original = this->m_data; + this->m_updateFcn(time, this->m_updateData, &value); + + if (original != value) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + } + + this->m_flags &= ~0x4; +} + +// VERIFY: a8b9e377 manual +// OFFSET: 0x004BF660 +CBaseManaged::~CBaseManaged() { +} + +// VERIFY: 11178118 manual +// OFFSET: [0x004BF6D0, 0x004BF740] +CDataMgr::~CDataMgr() { +} + +// INLINED +CDataMgr::CDataMgr(uint32_t count) { + this->m_managed.SetCount(count); +} + +// VERIFY: a7d7446b manual +// OFFSET: 0x004BF770 +CCamera::CCamera() : CDataMgr(9), m_position(C3Vector(100.0f, 0.0f, 0.0f)), m_target(C3Vector(0.0f, 0.0f, 0.0f)), m_distance(100.0f), m_zFar(5000.0f), m_zNear(8.0f), m_aoa(0.0f), m_fov(1.5707964f), m_roll(0.0f), m_rotation(0.0f) { + this->AddManaged(&this->m_position, 7, 0); + this->AddManaged(&this->m_target, 8, 0); + this->AddManaged(&this->m_distance, 1, 0); + this->AddManaged(&this->m_zFar, 2, 0); + this->AddManaged(&this->m_zNear, 3, 0); + this->AddManaged(&this->m_aoa, 0, 0); + this->AddManaged(&this->m_fov, 4, 0); + this->AddManaged(&this->m_roll, 5, 0); + this->AddManaged(&this->m_rotation, 6, 0); +} + +// VERIFY: d759f72d auto +// OFFSET: 0x004BFCA0 +CCamera* CameraCreate() { + void* mem = SMemAlloc(sizeof(CCamera), "HCAMERA", SLOG_OBJECT, 0); + CCamera* camera = mem ? new (mem) CCamera : nullptr; + + if (!camera) { + return nullptr; + } + + return reinterpret_cast(HandleCreate(camera, "HCAMERA")); +} + +// VERIFY: 2e746ce9 manual +// OFFSET: 0x004BFCE0 +CCamera* CameraDuplicate(CCamera* source) { + if (!source) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + void* mem = SMemAlloc(sizeof(CCamera), "HCAMERA", SLOG_OBJECT, 0); + CCamera* camera = mem ? new (mem) CCamera : nullptr; + + if (!camera) { + return nullptr; + } + + camera->m_position.m_updatePriority = 0.0f; + camera->m_position.m_updateFcn = nullptr; + camera->m_position.m_updateData = nullptr; + camera->m_position.Set_(source->m_position.m_data); + + camera->m_target.m_updatePriority = 0.0f; + camera->m_target.m_updateFcn = nullptr; + camera->m_target.m_updateData = nullptr; + camera->m_target.Set_(source->m_target.m_data); + + camera->m_distance.m_updatePriority = 0.0f; + camera->m_distance.m_updateFcn = nullptr; + camera->m_distance.m_updateData = nullptr; + camera->m_distance.Set_(source->m_distance.m_data); + + camera->m_zNear.m_updatePriority = 0.0f; + camera->m_zNear.m_updateFcn = nullptr; + camera->m_zNear.m_updateData = nullptr; + camera->m_zNear.Set_(source->m_zNear.m_data); + + camera->m_zFar.m_updatePriority = 0.0f; + camera->m_zFar.m_updateFcn = nullptr; + camera->m_zFar.m_updateData = nullptr; + camera->m_zFar.Set_(source->m_zFar.m_data); + + camera->m_aoa.m_updatePriority = 0.0f; + camera->m_aoa.m_updateFcn = nullptr; + camera->m_aoa.m_updateData = nullptr; + camera->m_aoa.Set_(source->m_aoa.m_data); + + camera->m_fov.m_updatePriority = 0.0f; + camera->m_fov.m_updateFcn = nullptr; + camera->m_fov.m_updateData = nullptr; + camera->m_fov.Set_(source->m_fov.m_data); + + camera->m_roll.m_updatePriority = 0.0f; + camera->m_roll.m_updateFcn = nullptr; + camera->m_roll.m_updateData = nullptr; + camera->m_roll.Set_(source->m_roll.m_data); + + camera->m_rotation.m_updatePriority = 0.0f; + camera->m_rotation.m_updateFcn = nullptr; + camera->m_rotation.m_updateData = nullptr; + camera->m_rotation.Set_(source->m_rotation.m_data); + + return reinterpret_cast(HandleCreate(camera, "HCAMERA")); +} + +// VERIFY: 1384d21a auto +// OFFSET: 0x004C1140 +float CAngle::ClampTo2Pi(float angle) { + float turns = static_cast(static_cast(angle * s_inv2Pi)) * 6.2831855f; + + if (angle < 0.0f) { + turns -= 6.2831855f; + } + + return angle - turns; +} + +// VERIFY: d430685f manual +// OFFSET: 0x004C1180 +static void GetManagedValue(CDataMgr* mgr, uint32_t index, float* value, uint8_t dataTypeId) { + if (!value || !mgr || index >= mgr->m_managed.Count() || !mgr->m_managed[index] || mgr->m_managed[index]->m_dataTypeId != dataTypeId) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + CBaseManaged* data = mgr->m_managed[index]; + + if (data->m_flags & 0x4) { + if (data->m_flags & 0x2) { + data->Validate(0.0f); + } else { + data->Update(0.0f); + } + } + + *value = static_cast*>(data)->m_data; +} + +// VERIFY: c7e90e1f auto +// OFFSET: 0x004C11F0 +float CameraGetFloat(CCamera* camera, uint32_t index) { + float value = 0.0f; + GetManagedValue(camera, index, &value, 6); + + return value; +} + +// VERIFY: ecbbb5cc manual +// OFFSET: 0x004C1220 +static void GetManagedValue(CDataMgr* mgr, uint32_t index, C3Vector* value, uint8_t dataTypeId) { + if (!value || !mgr || index >= mgr->m_managed.Count() || !mgr->m_managed[index] || mgr->m_managed[index]->m_dataTypeId != dataTypeId) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + CBaseManaged* data = mgr->m_managed[index]; + + if (data->m_flags & 0x4) { + if (data->m_flags & 0x2) { + data->Validate(0.0f); + } else { + data->Update(0.0f); + } + } + + *value = static_cast*>(data)->m_data; +} + +// VERIFY: 15f73a5e auto +// OFFSET: 0x004C1290 +void DataMgrGetCoord(CDataMgr* mgr, uint32_t index, C3Vector* value) { + GetManagedValue(mgr, index, value, 3); +} + +// VERIFY: 6e02d0ab manual +// OFFSET: 0x004C12B0 +void CameraSetCoord(CCamera* camera, uint32_t index, const C3Vector& value, uint32_t keepMask) { + C3Vector current(0.0f, 0.0f, 0.0f); + GetManagedValue(camera, index, ¤t, 3); + + C3Vector newValue((keepMask & 0x1) ? current.x : value.x, (keepMask & 0x2) ? current.y : value.y, (keepMask & 0x4) ? current.z : value.z); + + if (!camera || index >= camera->m_managed.Count() || !camera->m_managed[index] || camera->m_managed[index]->m_dataTypeId != 3 || (camera->m_managed[index]->m_flags & 0x2)) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + TManaged* data = static_cast*>(camera->m_managed[index]); + data->m_updateFcn = nullptr; + data->m_updateData = nullptr; + data->m_updatePriority = 0.0f; + data->Set_(newValue); +} + +// VERIFY: ef5a2ea9 manual +// OFFSET: 0x004C1360 +void CameraSetFloat(CCamera* camera, uint32_t index, float value) { + if (!camera || index >= camera->m_managed.Count() || !camera->m_managed[index] || camera->m_managed[index]->m_dataTypeId != 6 || (camera->m_managed[index]->m_flags & 0x2)) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + TManaged* data = static_cast*>(camera->m_managed[index]); + data->m_updateFcn = nullptr; + data->m_updateData = nullptr; + data->m_updatePriority = 0.0f; + data->Set_(value); +} + +// VERIFY: 8e9f0604 manual +// OFFSET: 0x004C13B0 +void CDataMgr::InsertByPriority(CBaseManaged* data) { + for (CBaseManaged* node = this->m_updateList.Head(); node; node = this->m_updateList.Next(node)) { + if (node->m_updatePriority <= data->m_updatePriority) { + this->m_updateList.LinkNode(data, 2, node); + return; + } + } + + this->m_updateList.LinkToTail(data); +} + +// VERIFY: e2e5042b auto +// OFFSET: 0x004C14B0 +void CDataMgr::AddManaged(TManaged* data, uint32_t index, uint8_t flags) { + data->m_flags = flags; + data->m_dataTypeId = 3; + this->m_managed[index] = data; + + if (flags & 0x1) { + this->InsertByPriority(data); + } +} + +// VERIFY: 0e9ed7c7 auto +// OFFSET: 0x004C14E0 +void CDataMgr::AddManaged(TManaged* data, uint32_t index, uint8_t flags) { + data->m_flags = flags; + data->m_dataTypeId = 6; + this->m_managed[index] = data; + + if (flags & 0x1) { + this->InsertByPriority(data); + } +} + +// VERIFY: e9e02e6c manual +// OFFSET: [0x004BFA50, 0x004BFC70] +CCamera::~CCamera() { +} diff --git a/Engine/Source/Services/Camera.h b/Engine/Source/Services/Camera.h index ae4cf24..dddb90f 100644 --- a/Engine/Source/Services/Camera.h +++ b/Engine/Source/Services/Camera.h @@ -4,11 +4,20 @@ #include #include +class C2Vector; class C3Vector; class CCamera; +class CDataMgr; +class CRect; CCamera* CameraCreate(); -void CameraSetCoord(CCamera* camera, uint32_t coord, const C3Vector& value); -void CameraSetFloat(CCamera* camera, uint32_t param, float value); +CCamera* CameraDuplicate(CCamera* camera); +void CameraGetLineSegment(float x, float y, C3Vector* start, C3Vector* end); +float CameraGetFloat(CCamera* camera, uint32_t index); +void CameraSetCoord(CCamera* camera, uint32_t index, const C3Vector& value, uint32_t keepMask = 0); +void CameraSetFloat(CCamera* camera, uint32_t index, float value); +void CameraSetupScreenProjection(const CRect& rect, const C2Vector& offset, float depth, int32_t forceOrtho); +void CameraSetupWorldProjection(CCamera* camera, const CRect& rect, uint32_t flags); +void DataMgrGetCoord(CDataMgr* mgr, uint32_t index, C3Vector* value); #endif diff --git a/Engine/Source/Services/CameraInt.h b/Engine/Source/Services/CameraInt.h new file mode 100644 index 0000000..f400494 --- /dev/null +++ b/Engine/Source/Services/CameraInt.h @@ -0,0 +1,77 @@ +#ifndef ENGINE_SOURCE_SERVICES_CAMERAINT_H +#define ENGINE_SOURCE_SERVICES_CAMERAINT_H + +#include +#include +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C3Vector.h" + +class CRect; + +class CBaseManaged { + public: + /* 0004 */ TSLink m_link; + /* 000C */ uint8_t m_dataTypeId; + /* 000D */ uint8_t m_flags; + /* 0010 */ void (*m_updateFcn)(float, void*, void*); + /* 0014 */ void* m_updateData; + /* 0018 */ float m_updatePriority; + + CBaseManaged(); + virtual ~CBaseManaged(); + virtual void Update(float time); + virtual void Validate(float time); +}; + +template +class TManaged : public CBaseManaged { + public: + /* 001C */ T m_data; + + TManaged(const T& value); + virtual void Update(float time); + virtual void Validate(float time); + virtual void Set_(const T& value); +}; + +class CAngle : public TManaged { + public: + /* 0020 */ float m_cos; + /* 0024 */ float m_sin; + + CAngle(float angle); + float ClampTo2Pi(float angle); + virtual void Set_(const float& value); +}; + +class CDataMgr : public CHandleObject { + public: + /* 0008 */ TSFixedArray m_managed; + /* 0014 */ TSExplicitList m_updateList; + + CDataMgr(uint32_t count); + virtual ~CDataMgr(); + void AddManaged(TManaged* data, uint32_t index, uint8_t flags); + void AddManaged(TManaged* data, uint32_t index, uint8_t flags); + void InsertByPriority(CBaseManaged* data); +}; + +class CCamera : public CDataMgr { + public: + /* 0020 */ TManaged m_position; + /* 0048 */ TManaged m_target; + /* 0070 */ TManaged m_distance; + /* 0090 */ TManaged m_zFar; + /* 00B0 */ TManaged m_zNear; + /* 00D0 */ CAngle m_aoa; + /* 00F8 */ CAngle m_fov; + /* 0120 */ CAngle m_roll; + /* 0148 */ CAngle m_rotation; + + CCamera(); + virtual ~CCamera(); + void SetupWorldProjection(const CRect& rect, uint32_t flags); +}; + +#endif diff --git a/Engine/Source/Services/GfxSingletonManager.cpp b/Engine/Source/Services/GfxSingletonManager.cpp index a7122ca..0943757 100644 --- a/Engine/Source/Services/GfxSingletonManager.cpp +++ b/Engine/Source/Services/GfxSingletonManager.cpp @@ -1 +1,146 @@ -#include "GfxSingletonManager.h" +#include +#include +#include +#include +#include "Engine/Source/Services/GfxSingletonManager.h" +#include "Engine/Source/Services/ParticleSystem.h" +#include "Engine/Source/Services/ParticleSystem2.h" +#include "Engine/Source/Services/RibbonEmitter.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/CRandom.h" + +// STATIC: 0x00B2D678 +static float s_scaler = 1.0f; + +// STATIC: 0x00DCE8CC +ParticleSystemManager* ParticleSystemManager::s_instance; + +// STATIC: 0x00DCE8D0 +RibbonManager* RibbonManager::s_instance; + +// VERIFY: 824e6114 auto +// OFFSET: 0x00980ED0 +float ParticleSystemManager::GetScaler() { + return s_scaler; +} + +// VERIFY: 5b9d99cc auto +// OFFSET: 0x00980F70 +void ParticleSystemManager::SetScaler(float scaler) { + if (scaler < 0.0f) { + s_scaler = 0.0f; + } else if (scaler < 1.0f) { + s_scaler = scaler; + } else { + s_scaler = 1.0f; + } +} + +// VERIFY: d3a1f51e manual +// OFFSET: 0x00981000 +void ParticleSystemManager::Flush() { + for (uint32_t i = this->m_emitters.Count(); i; i--) { + this->m_emitters[i - 1]->DecRef(); + } + + this->m_emitters.m_count = 0; + + for (uint32_t i = this->m_emitters2.Count(); i; i--) { + this->m_emitters2[i - 1]->DecRef(); + } + + this->m_emitters2.m_count = 0; + + for (uint32_t i = this->m_pendingEmitters.Count(); i; i--) { + this->m_pendingEmitters[i - 1]->DecRef(); + } + + this->m_pendingEmitters.m_count = 0; + + for (uint32_t i = this->m_pendingEmitters2.Count(); i; i--) { + this->m_pendingEmitters2[i - 1]->DecRef(); + } + + this->m_pendingEmitters2.m_count = 0; +} + +// VERIFY: 2e8d51c6 auto +// OFFSET: 0x00981090 +CParticleEmitter2* ParticleSystemManager::DuplicateEmitter(const CParticleEmitter2* emitter, int32_t copyParticles) { + if (!emitter) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + CParticleEmitter2* duplicate = const_cast(emitter)->Clone(copyParticles); + duplicate->AddRef(); + *this->m_emitters2.New() = duplicate; + + return duplicate; +} + +// VERIFY: 9ed5c945 auto +// OFFSET: 0x009810E0 +void RibbonManager::Flush() { + for (uint32_t i = this->m_emitters.Count(); i; i--) { + this->m_emitters[i - 1]->DecRef(); + } + + this->m_emitters.m_count = 0; + + for (uint32_t i = this->m_pendingEmitters.Count(); i; i--) { + this->m_pendingEmitters[i - 1]->DecRef(); + } + + this->m_pendingEmitters.m_count = 0; +} + +// VERIFY: 61e3c1a1 manual +// OFFSET: 0x00981130 +ParticleSystemManager* ParticleSystemManager::GetInstance() { + if (ParticleSystemManager::s_instance) { + return ParticleSystemManager::s_instance; + } + + void* mem = SMemAlloc(sizeof(ParticleSystemManager), __FILE__, __LINE__, 0); + ParticleSystemManager::s_instance = mem ? new (mem) ParticleSystemManager : nullptr; + + CRndSeed seed((rand() << 16) | static_cast(rand())); + + for (uint32_t i = 0; i < 128; i++) { + CParticleEmitter2::s_rndTable[i] = CRandom::ufloat_(seed); + } + + CParticleEmitter2::Init(); + + return ParticleSystemManager::s_instance; +} + +// VERIFY: fb3884dd manual +// OFFSET: 0x00981200 +ParticleSystemManager::~ParticleSystemManager() { + this->Flush(); +} + +// VERIFY: eda66b90 manual +// OFFSET: 0x00981270 +void ParticleSystemManager::Destroy() { + if (ParticleSystemManager::s_instance) { + CParticleEmitter2::Destroy(); + delete ParticleSystemManager::s_instance; + } + + ParticleSystemManager::s_instance = nullptr; +} + +// VERIFY: ef3840e9 manual +// OFFSET: 0x009812B0 +void RibbonManager::Destroy() { + delete RibbonManager::s_instance; + RibbonManager::s_instance = nullptr; +} + +// INLINED +RibbonManager::~RibbonManager() { + this->Flush(); +} diff --git a/Engine/Source/Services/GfxSingletonManager.h b/Engine/Source/Services/GfxSingletonManager.h index df4f582..4fb56a8 100644 --- a/Engine/Source/Services/GfxSingletonManager.h +++ b/Engine/Source/Services/GfxSingletonManager.h @@ -3,33 +3,45 @@ #include #include +#include "Engine/Source/Storm/H/stpl.h" class C3Vector; +class CParticleEmitter; +class CParticleEmitter2; +class CRibbonEmitter; class ParticleSystemManager { public: static int32_t (*sm_projectCallback)(const C3Vector&, float&, void*); static void* sm_projectParam; + static ParticleSystemManager* s_instance; static void Destroy(); + static ParticleSystemManager* GetInstance(); + static float GetScaler(); + static void SetScaler(float scaler); - /* 0000 */ uint8_t unk_0000[4]; - /* 0004 */ uint32_t unk_0004; - /* 0008 */ uint32_t unk_0008; - /* 000C */ uint8_t unk_000C[8]; - /* 0014 */ uint32_t unk_0014; - /* 0018 */ uint32_t unk_0018; - /* 001C */ uint8_t unk_001C[8]; - /* 0024 */ uint32_t unk_0024; - /* 0028 */ uint32_t unk_0028; - /* 002C */ uint8_t unk_002C[8]; - /* 0034 */ uint32_t unk_0034; - /* 0038 */ uint32_t unk_0038; + /* 0000 */ TSGrowableArray m_emitters; + /* 0010 */ TSGrowableArray m_emitters2; + /* 0020 */ TSGrowableArray m_pendingEmitters; + /* 0030 */ TSGrowableArray m_pendingEmitters2; + + ~ParticleSystemManager(); + CParticleEmitter2* DuplicateEmitter(const CParticleEmitter2* emitter, int32_t copyParticles); + void Flush(); }; class RibbonManager { public: + static RibbonManager* s_instance; + static void Destroy(); + + /* 0000 */ TSGrowableArray m_emitters; + /* 0010 */ TSGrowableArray m_pendingEmitters; + + ~RibbonManager(); + void Flush(); }; #endif diff --git a/Engine/Source/Services/Lightning.h b/Engine/Source/Services/Lightning.h index c18d4bc..815f843 100644 --- a/Engine/Source/Services/Lightning.h +++ b/Engine/Source/Services/Lightning.h @@ -3,55 +3,27 @@ #include #include +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C2Vector.h" +#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Tempest/CImVector.h" + +class CTexture; +struct LightningJoint; +struct SpellChainEffectsRec; class CLightning { public: - /* 0000 */ uint8_t unk_0000; - /* 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 */ uint16_t unk_000A; - /* 000C */ uint32_t unk_000C; - /* 0010 */ uint16_t unk_0010; - /* 0012 */ uint8_t unk_0012; - /* 0013 */ uint8_t unk_0013; - /* 0014 */ uint16_t unk_0014; - /* 0016 */ uint16_t unk_0016; - /* 0018 */ uint8_t unk_0018; - /* 0019 */ uint8_t unk_0019; - /* 001A */ uint8_t unk_001A; - /* 001B */ uint8_t unk_001B; - /* 001C */ uint32_t unk_001C; - /* 0020 */ uint32_t unk_0020; - /* 0024 */ uint8_t unk_0024; - /* 0025 */ uint8_t unk_0025; - /* 0026 */ uint8_t unk_0026; - /* 0027 */ uint8_t unk_0027; - /* 0028 */ uint8_t unk_0028; - /* 0029 */ uint8_t unk_0029; - /* 002A */ uint8_t unk_002A; - /* 002B */ uint8_t unk_002B; - /* 002C */ uint32_t unk_002C; - /* 0030 */ uint32_t unk_0030; - /* 0034 */ uint32_t unk_0034; - /* 0038 */ uint32_t unk_0038; - /* 003C */ uint32_t unk_003C; - /* 0040 */ uint32_t unk_0040; - /* 0044 */ uint32_t unk_0044; - /* 0048 */ uint32_t unk_0048; - /* 004C */ uint32_t unk_004C; - /* 0050 */ uint32_t unk_0050; - /* 0054 */ uint32_t unk_0054; - /* 0058 */ uint32_t unk_0058; - /* 005C */ uint32_t unk_005C; - /* 0060 */ uint32_t unk_0060; - /* 0064 */ uint32_t unk_0064; + /* 0000 */ C3Vector m_srcPos; + /* 000C */ C3Vector m_dstPos; + /* 0018 */ CImVector m_color; + /* 001C */ const SpellChainEffectsRec* m_chainEffects; + /* 0020 */ TSFixedArray m_joints; + /* 002C */ TSFixedArray m_points; + /* 0038 */ TSFixedArray m_positions; + /* 0044 */ TSFixedArray m_texCoords; + /* 0050 */ TSFixedArray m_colors; + /* 005C */ TSFixedArray m_indices; /* 0068 */ uint32_t unk_0068; /* 006C */ uint32_t unk_006C; /* 0070 */ uint32_t unk_0070; @@ -59,9 +31,10 @@ class CLightning { /* 0078 */ uint32_t unk_0078; /* 007C */ uint32_t unk_007C; /* 0080 */ uint32_t unk_0080; - /* 0084 */ uint32_t unk_0084; - /* 0088 */ uint8_t unk_0088[8]; - /* 0090 */ uint32_t unk_0090; + /* 0084 */ CTexture* m_texture; + /* 0088 */ void (*m_coordUpdate)(void*, uint32_t, C3Vector*, C3Vector*); + /* 008C */ void* m_coordUpdateParam; + /* 0090 */ uint32_t m_flags; /* 0094 */ uint32_t unk_0094; /* 0098 */ uint32_t unk_0098; /* 009C */ uint32_t unk_009C; diff --git a/Engine/Source/Services/ParticleSystem.cpp b/Engine/Source/Services/ParticleSystem.cpp new file mode 100644 index 0000000..a5e01d6 --- /dev/null +++ b/Engine/Source/Services/ParticleSystem.cpp @@ -0,0 +1,22 @@ +#include +#include +#include "Engine/Source/Services/ParticleSystem.h" +#include "Engine/Source/Storm/H/storm.h" + +// INLINED +CParticle::~CParticle() { + this->m_age = 0.0f; +} + +// VERIFY: 07404e3e manual +// OFFSET: 0x00982280 +CParticleEmitter::~CParticleEmitter() { +} + +// VERIFY: 520cd03a auto +// OFFSET: 0x009822F0 +void CParticleEmitter::DecRef() { + if (this->m_refCount-- == 1) { + delete this; + } +} diff --git a/Engine/Source/Services/ParticleSystem.h b/Engine/Source/Services/ParticleSystem.h new file mode 100644 index 0000000..ce1ccd5 --- /dev/null +++ b/Engine/Source/Services/ParticleSystem.h @@ -0,0 +1,28 @@ +#ifndef ENGINE_SOURCE_SERVICES_PARTICLESYSTEM_H +#define ENGINE_SOURCE_SERVICES_PARTICLESYSTEM_H + +#include +#include +#include "Engine/Source/Storm/H/stpl.h" + +class CParticle { + public: + /* 0000 */ float m_age; + /* 0004 */ uint8_t unk_0004[0x24]; + + ~CParticle(); +}; + +class CParticleEmitter { + public: + /* 0000 */ int32_t m_refCount; + /* 0004 */ uint8_t unk_0004[0x2C]; + /* 0030 */ TSGrowableArray m_particles; + /* 0040 */ TSGrowableArray m_liveList; + /* 0050 */ TSGrowableArray m_freeList; + + ~CParticleEmitter(); + void DecRef(); +}; + +#endif diff --git a/Engine/Source/Services/ParticleSystem2.cpp b/Engine/Source/Services/ParticleSystem2.cpp index fbf9788..b50fae4 100644 --- a/Engine/Source/Services/ParticleSystem2.cpp +++ b/Engine/Source/Services/ParticleSystem2.cpp @@ -1,8 +1,51 @@ +#include #include #include +#include +#include +#include +#include #include "Engine/Source/Services/ParticleSystem2.h" +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Gx/CGxBuf.h" +#include "Engine/Source/Gx/CGxPool.h" #include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Model2/M2Data.h" +#include "Engine/Source/Model2/M2Model.h" +#include "Engine/Source/Model2/M2Scene.h" +#include "Engine/Source/Model2/M2Shared.h" #include "Engine/Source/Services/GfxSingletonManager.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/C2Vector.h" +#include "Engine/Source/Tempest/C33Matrix.h" +#include "Engine/Source/Tempest/C4Vector.h" +#include "Engine/Source/Tempest/CAaSphere.h" +#include "Engine/Source/Tempest/CMath.h" +#include "Engine/Source/Tempest/CRange.h" + +// STATIC: 0x00B2D530 +static int32_t s_particleRenderEnable = 1; + +// STATIC: 0x00B2D534 +static float s_stepRound = 0.5f; + +// STATIC: 0x00B2D540 +static C3Vector s_particleNormal(0.0f, 0.0f, 1.0f); + +// STATIC: 0x00B2D550 +static C44Matrix s_particleXform(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); + +// STATIC: 0x00B2D590 +static C33Matrix s_billboardXform(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f); + +// STATIC: 0x00B2D5B4 +static const float s_cornerOffsets[4][2] = { { -1.0f, 1.0f }, { -1.0f, -1.0f }, { 1.0f, 1.0f }, { 1.0f, -1.0f } }; + +// STATIC: 0x00B2D5D4 +static const float s_cornerTexCoords[4][2] = { { 0.0f, 0.0f }, { 0.0f, 1.0f }, { 1.0f, 0.0f }, { 1.0f, 1.0f } }; // STATIC: 0x00DCE680 uint32_t CParticleEmitter2::s_initCount; @@ -13,15 +56,329 @@ CGxBuf* CParticleEmitter2::s_indexBuf; // STATIC: 0x00DCE688 CGxPool* CParticleEmitter2::s_indexPool; +// STATIC: 0x00DCE68C +float CParticleEmitter2::s_viewDist; + +// STATIC: 0x00DCE690 +float CParticleEmitter2::s_rndTable[128]; + // STATIC: 0x00DCE890 uint8_t CParticleEmitter2::s_noIndexBuffer; +static const uint32_t s_pqParams[2] = { 32, 32 }; + +// STATIC: 0x00DCE894 +CSortableParticleQueue CParticleEmitter2::s_pq(s_pqParams); + // STATIC: 0x00DCE8C4 void* ParticleSystemManager::sm_projectParam; // STATIC: 0x00DCE8C8 int32_t (*ParticleSystemManager::sm_projectCallback)(const C3Vector&, float&, void*); +// VERIFY: c5ae3283 manual +// OFFSET: 0x00979330 +static uint32_t PartTrackSearch(const M2Array& times, float t) { + uint32_t lo = 0; + uint32_t hi = times.m_count - 1; + + if (times.m_count == 1) { + return 0; + } + + while (1) { + uint32_t mid = (hi + lo) >> 1; + + if (times[mid].m_value * 0.000030518509f <= t) { + lo = mid + 1; + + if (mid + 1 >= times.m_count - 1 || times[mid + 1].m_value * 0.000030518509f > t) { + return mid; + } + } else { + hi = mid - 1; + } + + if (lo >= hi) { + return lo; + } + } +} + +// VERIFY: e650aa30 manual +// OFFSET: 0x009793B0 +static float PartTrackKeys(const M2Array& times, float t, uint32_t& key0, uint32_t& key1) { + if (times.m_count == 2) { + key0 = 0; + key1 = 1; + return t; + } + + if (times.m_count == 3) { + float mid = times[1].m_value * 0.000030518509f; + + if (t >= mid) { + key0 = 1; + key1 = 2; + return (t - mid) / (1.0f - mid); + } + + key0 = 0; + key1 = 1; + return t / mid; + } + + key0 = PartTrackSearch(times, t); + key1 = key0 + 1; + + return (t - times[key0].m_value * 0.000030518509f) / (0.000030518509f * (times[key1].m_value - times[key0].m_value)); +} + +// VERIFY: 91ba216a auto +// OFFSET: 0x00979480 +static C2Vector __stdcall PartTrackSample(const M2PartTrack& track, float t) { + C2Vector result; + + if (track.m_values.m_count == 1) { + result = track.m_values[0]; + return result; + } + + uint32_t key0; + uint32_t key1; + float blend = PartTrackKeys(track.m_times, t, key0, key1); + + result.x = (track.m_values[key1].x - track.m_values[key0].x) * blend + track.m_values[key0].x; + result.y = blend * (track.m_values[key1].y - track.m_values[key0].y) + track.m_values[key0].y; + + return result; +} + +// VERIFY: bae87e8d auto +// OFFSET: 0x009794F0 +static float __stdcall PartTrackSample(const M2PartTrack& track, float t) { + if (track.m_values.m_count == 1) { + return track.m_values[0].m_value * 0.000030518509f; + } + + uint32_t key0; + uint32_t key1; + float blend = PartTrackKeys(track.m_times, t, key0, key1); + + return blend * (0.000030518509f * track.m_values[key1].m_value - track.m_values[key0].m_value * 0.000030518509f) + track.m_values[key0].m_value * 0.000030518509f; +} + +// VERIFY: 3e432a29 auto +// OFFSET: 0x00979560 +static int32_t __stdcall PartTrackSample(const M2PartTrack& track, float t) { + if (track.m_values.m_count == 1) { + return track.m_values[0]; + } + + uint32_t key0; + uint32_t key1; + float blend = PartTrackKeys(track.m_times, t, key0, key1); + + return static_cast(CMath::fint_(blend * (track.m_values[key1] - track.m_values[key0]) + track.m_values[key0])); +} + +// VERIFY: 495193f9 manual +// OFFSET: 0x0097D7B0 +static uint32_t PopIndex(TSGrowableArray& array) { + uint32_t value = *array.Top(); + array.SetCount(array.Count() - 1); + + return value; +} + +// INLINED +static inline void AddVertex(CAaBox& bounds, CParticleEmitter2::ParticleVertex& vertex, const C3Vector& position, const CImVector& color, const C2Vector& texCoord) { + *reinterpret_cast(vertex.m_position) = position; + + if (bounds.b.x > position.x) { + bounds.b.x = position.x; + } + + if (bounds.b.y > position.y) { + bounds.b.y = position.y; + } + + if (position.z < bounds.b.z) { + bounds.b.z = position.z; + } + + if (bounds.t.x < position.x) { + bounds.t.x = position.x; + } + + if (position.y > bounds.t.y) { + bounds.t.y = position.y; + } + + if (position.z > bounds.t.z) { + bounds.t.z = position.z; + } + + *reinterpret_cast(vertex.m_normal) = s_particleNormal; + *reinterpret_cast(vertex.m_color) = color; + *reinterpret_cast(vertex.m_texCoord) = texCoord; + + vertex.m_position += vertex.m_positionStride; + vertex.m_normal += vertex.m_normalStride; + vertex.m_color += vertex.m_colorStride; + vertex.m_texCoord += vertex.m_texCoordStride; + vertex.m_count++; +} + +// VERIFY: c62573e0 auto +// OFFSET: 0x00978B30 +void CParticleEmitter2::SetModel(CM2Scene* scene, const char* name) { + if (scene && name && *name) { + this->m_scene = scene; + this->m_model = scene->CreateModel(name, 0); + + if (this->m_model) { + this->m_hasModel = 1; + } + } +} + +// VERIFY: 0a4f2f59 auto +// OFFSET: 0x00978BE0 +CAaBox& CParticleEmitter2::WorldBounds() { + return this->m_worldBounds; +} + +// VERIFY: 59fe6658 auto +// OFFSET: 0x00978BF0 +void CParticleEmitter2::SetMaterial(const CParticleMat& material, CTexture* texture) { + if (this->m_texture) { + HandleClose(reinterpret_cast(this->m_texture)); + } + + this->m_texture = reinterpret_cast(HandleDuplicate(reinterpret_cast(texture))); + this->m_materialBlend = material.m_blendMode; + this->m_materialFlags = material.m_flags; +} + +// VERIFY: 9ef515a2 auto +// OFFSET: 0x00978C40 +void CParticleEmitter2::SetTexture(CTexture* texture) { + if (this->m_texture) { + HandleClose(reinterpret_cast(this->m_texture)); + } + + this->m_texture = reinterpret_cast(HandleDuplicate(reinterpret_cast(texture))); +} + +// VERIFY: a4b2b0e5 manual +// OFFSET: 0x00978C70 +void CParticleEmitter2::SetTextureDimensions(uint32_t rows, uint32_t cols) { + if ((rows & (rows - 1)) || (cols & (cols - 1)) || !rows || !cols) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + this->m_textureRows = rows; + this->m_textureCols = cols; + this->m_textureCellShift = -1; + + for (uint32_t i = cols; i; i >>= 1) { + this->m_textureCellShift++; + } + + this->m_textureCellWidth = 1.0f / this->m_textureCols; + this->m_textureCellHeight = 1.0f / rows; +} + +// VERIFY: df2fab17 manual +// OFFSET: 0x00978D00 +void CParticleEmitter2::SetParticleStyle(int32_t head, int32_t tail, float tailLength, int32_t clampTail) { + if (head) { + this->m_flags |= 0x4; + } else { + this->m_flags &= ~0x4; + } + + if (tail) { + this->m_flags |= 0x8; + } else { + this->m_flags &= ~0x8; + } + + if (clampTail) { + this->m_flags |= 0x20000; + } else { + this->m_flags &= ~0x20000; + } + + this->m_vertsPerParticle = 0; + this->m_indicesPerParticle = 0; + this->m_tailLength = tailLength; + + if (this->m_flags & 0x4) { + this->m_vertsPerParticle = 4; + this->m_indicesPerParticle = 6; + } + + if (this->m_flags & 0x8) { + this->m_vertsPerParticle += 4; + this->m_indicesPerParticle += 6; + } +} + +// VERIFY: 9c2c4f85 auto +// OFFSET: 0x00978DA0 +void CParticleEmitter2::SetZsource(float zsource) { + this->m_zsource = zsource; + + if (fabs(zsource) < 0.001f) { + this->m_zsource = 0.0f; + } +} + +// VERIFY: bfe3df98 manual +// OFFSET: 0x00978DD0 +void CParticleEmitter2::SetFollowParams(float speed1, float scale1, float speed2, float scale2) { + float speedDelta = speed2 - speed1; + + if (fabs(speedDelta) < 0.00000023841858f) { + this->m_followSlope = 0.0f; + this->m_followIntercept = 0.0f; + return; + } + + float slope = (scale2 - scale1) / speedDelta; + this->m_followSlope = slope; + this->m_followIntercept = scale1 - speed1 * slope; +} + +// VERIFY: 0f5748cd auto +// OFFSET: 0x00978E30 +void CParticleEmitter2::SetChooseRandomTexture(int32_t choose) { + if (this->m_textureRows * this->m_textureCols > 1 && choose) { + this->m_flags |= 0x100000; + } else { + this->m_flags &= ~0x100000; + } +} + +// VERIFY: 46acd383 auto +// OFFSET: 0x00978E70 +CParticleEmitter2* CParticleEmitter2::AddRef() { + this->m_refCount++; + + return this; +} + +// VERIFY: 4e4248e4 manual +// OFFSET: 0x00978E80 +void CParticleEmitter2::DecRef() { + if (this->m_refCount-- == 1) { + delete this; + } +} + // VERIFY: c8cd89a9 manual // OFFSET: 0x00979170 void CParticleEmitter2::Init() { @@ -39,3 +396,2805 @@ void CParticleEmitter2::Init() { CParticleEmitter2::s_initCount++; } + +// VERIFY: 122129b6 auto +// OFFSET: 0x009791E0 +void CParticleEmitter2::Destroy() { + if (--CParticleEmitter2::s_initCount) { + return; + } + + if (CParticleEmitter2::s_indexBuf) { + CGxBuf* buf = CParticleEmitter2::s_indexBuf; + + if (buf != g_theGxDevicePtr->BufStream(CParticleEmitter2::s_indexBuf->m_pool->m_target, 0, 0)) { + g_theGxDevicePtr->BufDestroy(buf); + } + } + + if (CParticleEmitter2::s_indexPool) { + g_theGxDevicePtr->PoolDestroy(CParticleEmitter2::s_indexPool); + } +} + +// VERIFY: 822fde42 manual +// OFFSET: 0x009792D0 +float CParticleEmitter2::CalcVelocity() { + return (CRandom::sfloat_(this->m_random) * this->m_variation + 1.0f) * this->m_speed; +} + +// VERIFY: ec4fefb2 manual +// OFFSET: 0x009795D0 +CImVector CParticleEmitter2::InterpolateColorTrack(float t) { + const M2PartTrack* track = this->m_colorTrack; + CImVector color; + + if (track->m_values.m_count == 1) { + color.b = static_cast(CMath::fint_(track->m_values[0].z)); + color.g = static_cast(CMath::fint_(track->m_values[0].y)); + color.r = static_cast(CMath::fint_(track->m_values[0].x)); + color.a = 0xFF; + return color; + } + + uint32_t key0; + uint32_t key1; + float blend = PartTrackKeys(track->m_times, t, key0, key1); + + float r; + float g; + float b; + + if (this->m_flags & 0x10) { + r = (this->m_replacementColors[key1].x - this->m_replacementColors[key0].x) * blend + this->m_replacementColors[key0].x; + g = (this->m_replacementColors[key1].y - this->m_replacementColors[key0].y) * blend + this->m_replacementColors[key0].y; + b = blend * (this->m_replacementColors[key1].z - this->m_replacementColors[key0].z) + this->m_replacementColors[key0].z; + } else { + const C3Vector& value0 = track->m_values[key0]; + const C3Vector& value1 = track->m_values[key1]; + r = (value1.x - value0.x) * blend + value0.x; + g = (value1.y - value0.y) * blend + value0.y; + b = blend * (value1.z - value0.z) + value0.z; + } + + color.b = static_cast(CMath::fint_(b)); + color.a = 0xFF; + color.r = static_cast(CMath::fint_(r)); + color.g = static_cast(CMath::fint_(g)); + + return color; +} + +// VERIFY: a5a0291d auto +// OFFSET: 0x00979740 +void CParticleEmitter2::ProjectParticle(CParticle2& particle) { + float height; + + if (!ParticleSystemManager::sm_projectCallback(particle.m_position, height, ParticleSystemManager::sm_projectParam)) { + return; + } + + float life = particle.m_lifeSeed * this->m_lifeVariation * 0.000030518509f + this->m_life; + + if (life <= 0.001f) { + life = 0.001f; + } + + C2Vector scale = PartTrackSample(*this->m_scaleTrack, particle.m_age / life); + + if (scale.x <= scale.y) { + particle.m_position.z = scale.y + height; + } else { + particle.m_position.z = scale.x + height; + } +} + +// VERIFY: f7870b1d manual +// OFFSET: 0x00979870 +void CParticleEmitter2::CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) { + float age = CRandom::ufloat_(this->m_random) * time; + float lifeSeed = CRandom::sfloat_(this->m_random); + + if (lifeSeed < 1.0f) { + if (lifeSeed > -1.0f) { + particle.m_lifeSeed = static_cast(lifeSeed * 32767.0f + 0.5f); + } else { + particle.m_lifeSeed = -32767; + } + } else { + particle.m_lifeSeed = 0x7FFF; + } + + if (age < 0.0f) { + age = 0.0f; + } + + particle.m_age = age; + particle.m_randomSeed = static_cast(CRandom::uint32_(this->m_random)); + particle.m_position.x = 0.0f; + particle.m_position.y = 0.0f; + particle.m_position.z = 0.0f; + + if (!(this->m_flags & 0x200)) { + particle.m_position *= transform; + + if (this->m_flags & 0x40000) { + this->ProjectParticle(particle); + } + } + + float velocity = this->CalcVelocity(); + C3Vector direction = CRandom::C3Vector_(this->m_random); + + particle.m_velocity.x = direction.x * velocity + this->m_parentVelocity.x; + particle.m_velocity.y = direction.y * velocity + this->m_parentVelocity.y; + particle.m_velocity.z = velocity * direction.z + this->m_parentVelocity.z; + + if (0.0f != particle.m_velocity.x && fabs(particle.m_velocity.x) < 0.0000000099999999f) { + particle.m_velocity.x = 0.0f; + } + + if (0.0f != particle.m_velocity.y && fabs(particle.m_velocity.y) < 0.0000000099999999f) { + particle.m_velocity.y = 0.0f; + } + + if (0.0f != particle.m_velocity.z && fabs(particle.m_velocity.z) < 0.0000000099999999f) { + particle.m_velocity.z = 0.0f; + } +} + +// VERIFY: e014e81b manual +// OFFSET: 0x009799C0 +void CParticleEmitter2::CreateParticle(CParticle2_Model& particle, float time, const C44Matrix& transform) { + this->CreateParticle(static_cast(particle), time, transform); + + if (!(this->m_flags & 0x200)) { + C33Matrix rotation(transform); + C3Vector axis(rotation.a0, rotation.a1, rotation.a2); + float lengthSq = axis.y * axis.y + axis.z * axis.z + axis.x * axis.x; + + if (lengthSq > 0.00000023841858f) { + float scale = 1.0f / sqrt(lengthSq); + rotation.a0 = axis.x * scale; + rotation.a1 = axis.y * scale; + rotation.a2 = scale * axis.z; + } + + particle.m_orientation.FromRotationMatrix(rotation); + reinterpret_cast(particle.m_orientation).NormalizeSafe(); + } + + float tumbleZ = CRandom::ufloat_(this->m_random); + float tumbleY = CRandom::ufloat_(this->m_random); + + particle.m_angularVelocity.x = CRandom::ufloat_(this->m_random) * this->m_tumbleSpanX + this->m_tumbleBaseX; + particle.m_angularVelocity.y = (tumbleY + 1.0f) * this->m_tumbleSpanY; + particle.m_angularVelocity.z = (tumbleZ + 1.0f) * this->m_tumbleSpanZ; + + if (this->m_flags & 0x10000) { + float signZ = (CRandom::uint32_(this->m_random) & 1) ? 1.0f : -1.0f; + float signY = (CRandom::uint32_(this->m_random) & 1) ? 1.0f : -1.0f; + float signX = (CRandom::uint32_(this->m_random) & 1) ? 1.0f : -1.0f; + + particle.m_angularVelocity.x = signX * particle.m_angularVelocity.x; + particle.m_angularVelocity.y = particle.m_angularVelocity.y * signY; + particle.m_angularVelocity.z = particle.m_angularVelocity.z * signZ; + } + + if (!particle.m_model) { + particle.m_model = this->m_scene->CreateModel(this->m_model->m_shared->m_fileName, 0); + } +} + +// VERIFY: ab425a2a manual +// OFFSET: 0x00979BB0 +int32_t CParticleEmitter2::MoveParticle(CParticle2& particle, float deltaTime) { + if (this->m_windTime > particle.m_age) { + particle.m_velocity.x = this->m_windVectorX * deltaTime + particle.m_velocity.x; + particle.m_velocity.y = this->m_windVectorY * deltaTime + particle.m_velocity.y; + particle.m_velocity.z = deltaTime * this->m_windVectorZ + particle.m_velocity.z; + + if (0.0f != particle.m_velocity.x && fabs(particle.m_velocity.x) < 0.0000000099999999f) { + particle.m_velocity.x = 0.0f; + } + + if (0.0f != particle.m_velocity.y && fabs(particle.m_velocity.y) < 0.0000000099999999f) { + particle.m_velocity.y = 0.0f; + } + + if (0.0f != particle.m_velocity.z && fabs(particle.m_velocity.z) < 0.0000000099999999f) { + particle.m_velocity.z = 0.0f; + } + } + + if ((this->m_flags & 0x80000) && deltaTime + deltaTime < particle.m_age) { + particle.m_position.x = this->m_burstOffset.x + particle.m_position.x; + particle.m_position.y = this->m_burstOffset.y + particle.m_position.y; + particle.m_position.z = this->m_burstOffset.z + particle.m_position.z; + } + + float moveX = particle.m_velocity.x * deltaTime; + float moveY = particle.m_velocity.y * deltaTime; + float moveZ = particle.m_velocity.z * deltaTime; + + particle.m_position.x = moveX + particle.m_position.x; + particle.m_position.y = moveY + particle.m_position.y; + particle.m_position.z = moveZ + particle.m_position.z - this->m_gravity * deltaTime * deltaTime * 0.5f; + particle.m_velocity.z = particle.m_velocity.z - this->m_gravity * deltaTime; + + if (0.0f != this->m_drag) { + float drag = deltaTime * this->m_drag; + + if (drag > 1.0f) { + drag = 1.0f; + } + + particle.m_velocity.x = particle.m_velocity.x - drag * particle.m_velocity.x; + particle.m_velocity.y = particle.m_velocity.y - particle.m_velocity.y * drag; + particle.m_velocity.z = particle.m_velocity.z - drag * particle.m_velocity.z; + } + + if (0.0f != particle.m_velocity.x && fabs(particle.m_velocity.x) < 0.0000000099999999f) { + particle.m_velocity.x = 0.0f; + } + + if (0.0f != particle.m_velocity.y && fabs(particle.m_velocity.y) < 0.0000000099999999f) { + particle.m_velocity.y = 0.0f; + } + + if (0.0f != particle.m_velocity.z && fabs(particle.m_velocity.z) < 0.0000000099999999f) { + particle.m_velocity.z = 0.0f; + } + + if (!(this->m_flags & 0x1000)) { + return 1; + } + + float dot; + float x; + + if (this->m_flags & 0x200) { + dot = particle.m_position.z * moveZ + particle.m_position.y * moveY; + x = particle.m_position.x; + } else { + x = particle.m_position.x - this->m_xform.d0; + dot = (particle.m_position.z - this->m_xform.d2) * moveZ + (particle.m_position.y - this->m_xform.d1) * moveY; + } + + if (dot + x * moveX > 0.0f) { + return 0; + } + + return 1; +} + +// VERIFY: 6de406b9 manual +// OFFSET: 0x00979D60 +void CParticleEmitter2::InterpolateAllTracksSimple(const CParticle2& particle, CImVector* color, C2Vector* scale, int32_t* headCell, int32_t* tailCell) { + float life = 0.001f; + + if (this->m_life > 0.001f) { + life = this->m_life; + } + + float t = particle.m_age / life; + const SimpleKey* key = this->m_simpleKeys; + + if (t >= this->m_simpleMidTime) { + key++; + t = (t - this->m_simpleMidTime) / (1.0f - this->m_simpleMidTime); + } else { + t = t / this->m_simpleMidTime; + } + + color->a = static_cast(CMath::fint_((t * key->m_deltaA + key->m_color.a) * this->m_alphaScale)); + color->r = static_cast(CMath::fint_(key->m_deltaR * t + key->m_color.r)); + color->g = static_cast(CMath::fint_(key->m_deltaG * t + key->m_color.g)); + color->b = static_cast(CMath::fint_(key->m_deltaB * t + key->m_color.b)); + + float size = key->m_deltaScale * t + key->m_scale; + scale->x = size; + scale->y = size; + + *headCell = static_cast(CMath::fint_(t * key->m_deltaHeadCell + key->m_headCell)); + *tailCell = static_cast(CMath::fint_(key->m_deltaTailCell * t + key->m_tailCell)); +} + +// VERIFY: d35e6fe3 manual +// OFFSET: 0x00979E90 +void CParticleEmitter2::InterpolateAllTracks(const CParticle2& particle, CImVector* color, C2Vector* scale, int32_t* headCell, int32_t* tailCell) { + float life = particle.m_lifeSeed * this->m_lifeVariation * 0.000030518509f + this->m_life; + + if (life <= 0.001f) { + life = 0.001f; + } + + float t = particle.m_age / life; + CRndSeed seed(particle.m_randomSeed); + + *color = this->InterpolateColorTrack(t); + color->a = static_cast(CMath::fint_(PartTrackSample(*this->m_alphaTrack, t) * this->m_alphaScale * 255.0f)); + *scale = PartTrackSample(*this->m_scaleTrack, t); + + *headCell = 0; + *tailCell = 0; + + if (this->m_headCellTrack->m_times.m_count) { + *headCell = PartTrackSample(*this->m_headCellTrack, t); + } else if (this->m_flags & 0x100000) { + *headCell = CRandom::dice_(this->m_textureRows * this->m_textureCols, seed); + } + + if (this->m_tailCellTrack->m_times.m_count) { + *tailCell = PartTrackSample(*this->m_tailCellTrack, t); + } + + if (this->m_flags & 0x800000) { + float variationY = CRandom::sfloat_(seed); + float scaleX = CRandom::sfloat_(seed) * this->m_scaleVariationX + 1.0f; + float scaleY = this->m_scaleVariationY * variationY + 1.0f; + + if (scaleY <= 0.000099999997f) { + scaleY = 0.000099999997f; + } + + if (scaleX <= 0.000099999997f) { + scale->x = 0.000099999997f * scale->x; + } else { + scale->x = scaleX * scale->x; + } + + scale->y = scaleY * scale->y; + } else { + float scaleXY = CRandom::sfloat_(seed) * this->m_scaleVariationX + 1.0f; + + if (scaleXY < 0.000099999997f) { + scaleXY = 0.000099999997f; + } + + scale->x = scale->x * scaleXY; + scale->y = scaleXY * scale->y; + } +} + +// VERIFY: 23a48f63 manual +// OFFSET: 0x0097A130 +void CParticleEmitter2::RandomizeSpin(const CParticle2& particle, float* spin, float* spinRate) { + if (0.0f == this->m_initialSpinVariation && 0.0f == this->m_spinVariation) { + *spin = this->m_initialSpin; + *spinRate = this->m_spin; + return; + } + + CRndSeed seed(particle.m_randomSeed); + + if (0.0f == this->m_initialSpinVariation) { + *spin = this->m_initialSpin; + } else { + *spin = CRandom::sfloat_(seed) * this->m_initialSpinVariation + this->m_initialSpin; + } + + if (0.0f == this->m_spinVariation) { + *spinRate = this->m_spin; + } else { + *spinRate = CRandom::sfloat_(seed) * this->m_spinVariation + this->m_spin; + } +} + +// VERIFY: 21e8a355 manual +// OFFSET: 0x0097A260 +void __stdcall CParticleEmitter2::RenderIndices(CGxBuf* buf) { + uint16_t* indices = reinterpret_cast(g_theGxDevicePtr->BufLock(buf)); + uint16_t* end = indices + 0x1FFF8; + uint16_t vertex = 0; + + while (indices < end) { + indices[0] = vertex; + indices[1] = vertex + 1; + indices[2] = vertex + 2; + indices[3] = vertex + 3; + indices[4] = vertex + 2; + indices[5] = vertex + 1; + indices += 6; + vertex += 4; + } + + g_theGxDevicePtr->BufUnlock(buf, 0); + buf->unk_001C = 1; +} + +// VERIFY: 8573f305 manual +// OFFSET: 0x0097A2E0 +void CParticleEmitter2::FillOutParticleVertex(void* buffer, EGxVertexBufferFormat format, ParticleVertex* vertex) { + uint32_t stride = GxVertexSize(format); + + vertex->m_position = static_cast(buffer) + GxVertexAttribOffset(format, GxVA_Position); + vertex->m_positionStride = stride; + + if (this->m_materialFlags & 0x1) { + vertex->m_normalStride = stride; + vertex->m_normal = static_cast(buffer) + GxVertexAttribOffset(format, GxVA_Normal); + } else { + static C3Vector s_zeroNormal(0.0f, 0.0f, 0.0f); + vertex->m_normal = reinterpret_cast(&s_zeroNormal); + vertex->m_normalStride = 0; + } + + vertex->m_color = static_cast(buffer) + GxVertexAttribOffset(format, GxVA_Color0); + vertex->m_colorStride = stride; + vertex->m_texCoordStride = stride; + vertex->m_texCoord = static_cast(buffer) + GxVertexAttribOffset(format, GxVA_TexCoord0); +} + +// VERIFY: 5ce9bae8 manual +// OFFSET: 0x0097A390 +void CParticleEmitter2::RenderParticlesPrep(const C44Matrix* frameOfReference, const C44Matrix& view) { + C44Matrix translate; + translate.d0 = -this->m_viewPos.x; + translate.d1 = -this->m_viewPos.y; + translate.d2 = -this->m_viewPos.z; + + if (this->m_flags & 0x200) { + s_particleXform = this->m_xform * translate * view; + } else if (frameOfReference) { + s_particleXform = *frameOfReference * translate * view; + } else { + s_particleXform = translate * view; + } + + s_particleNormal.x = view.c0; + s_particleNormal.y = view.c1; + s_particleNormal.z = view.c2; + + if (!(this->m_flags & 0x4000)) { + return; + } + + s_billboardXform = C33Matrix(s_particleXform); + + if ((this->m_flags & 0x200) && fabs(this->m_xformScale) >= 0.00000023841858f) { + s_billboardXform.Scale(1.0f / this->m_xformScale); + } + + this->m_billboardAxis.x = s_billboardXform.c0; + this->m_billboardAxis.y = s_billboardXform.c1; + this->m_billboardAxis.z = s_billboardXform.c2; + + float lengthSq = this->m_billboardAxis.z * this->m_billboardAxis.z + this->m_billboardAxis.y * this->m_billboardAxis.y + this->m_billboardAxis.x * this->m_billboardAxis.x; + + if (lengthSq > 0.00000023841858f) { + float scale = 1.0f / sqrt(lengthSq); + this->m_billboardAxis.x = this->m_billboardAxis.x * scale; + this->m_billboardAxis.y = this->m_billboardAxis.y * scale; + this->m_billboardAxis.z = scale * this->m_billboardAxis.z; + } +} + +// VERIFY: d76443ea auto +// OFFSET: 0x0097A580 +void __stdcall CParticleEmitter2::RenderParticleVertices(CGxBuf* buf, EGxVertexBufferFormat format, uint32_t vertexCount, uint32_t indexCount) { + if (!s_particleRenderEnable) { + return; + } + + if (!CParticleEmitter2::s_indexBuf->unk_001C || !CParticleEmitter2::s_indexBuf->unk_001D) { + CParticleEmitter2::RenderIndices(CParticleEmitter2::s_indexBuf); + } + + C44Matrix identity; + g_theGxDevicePtr->XformSetView(identity); + + GxPrimVertexPtr(buf, format); + g_theGxDevicePtr->PrimIndexPtr(CParticleEmitter2::s_indexBuf); + + CShaderEffect::SetTexMtx_Identity(0); + CShaderEffect::SetDefaultShaders(0); + CShaderEffect::UpdateWorldViewMatrix(); + + CGxBatch batch; + batch.m_primType = GxPrim_Triangles; + batch.m_start = 0; + batch.m_count = indexCount; + batch.m_minIndex = 0; + batch.m_maxIndex = vertexCount - 1; + g_theGxDevicePtr->Draw(&batch, 1); +} + +// VERIFY: 57b13d1b manual +// OFFSET: 0x0097A670 +int32_t CParticleEmitter2::RenderParticle(CParticle2_Model& particle, const C44Matrix* frameOfReference) { + uint32_t twinkle = 0; + + if (this->m_twinkleOnOff < 1.0f || 0.0f != this->m_twinkleScaleSpan) { + twinkle = (static_cast(CMath::fint_(this->m_twinkleFPS * particle.m_age)) + (reinterpret_cast(&particle) >> 5)) & 0x7F; + } + + if (this->m_twinkleOnOff < 1.0f && this->m_twinkleOnOff < CParticleEmitter2::s_rndTable[twinkle]) { + CM2Model* model = particle.m_model; + + if (model->m_attachParent) { + model->m_fFlags &= ~0x80; + } else { + model->m_fFlags &= ~0x8; + } + + if (model->m_attachParent) { + model->m_fFlags &= ~0x20000; + } else { + model->m_fFlags &= ~0x10000; + } + + return 0; + } + + float life = particle.m_lifeSeed * this->m_lifeVariation * 0.000030518509f + this->m_life; + + if (life <= 0.001f) { + life = 0.001f; + } + + float t = particle.m_age / life; + CImVector color = this->InterpolateColorTrack(t); + float alpha = PartTrackSample(*this->m_alphaTrack, t) * this->m_alphaScale * 255.0f; + uint8_t alphaByte = static_cast(CMath::fint_(alpha)); + C2Vector scale = PartTrackSample(*this->m_scaleTrack, t); + + float twinkleScale = CParticleEmitter2::s_rndTable[twinkle] * this->m_twinkleScaleSpan + this->m_twinkleScaleBase; + scale.x = scale.x * twinkleScale; + scale.y = twinkleScale * scale.y; + + if (!(this->m_flags & 0x200) && (this->m_flags & 0x400)) { + scale.x = scale.x * this->m_xformScale; + scale.y = this->m_xformScale * scale.y; + } + + C44Matrix transform(particle.m_orientation); + transform.Scale(C3Vector(scale.x, scale.y, (scale.x + scale.y) * 0.5f)); + transform.d0 = particle.m_position.x; + transform.d1 = particle.m_position.y; + transform.d2 = particle.m_position.z; + + if (this->m_flags & 0x200) { + transform *= this->m_xform; + } else if (frameOfReference) { + transform *= *frameOfReference; + } + + CM2Model* model = particle.m_model; + model->m_fFlags |= 0x8000; + model->m_worldTransform = transform; + + model = particle.m_model; + model->unk_0180 = color.r * 0.0039215689f; + model->unk_0184 = color.g * 0.0039215689f; + model->unk_0188 = color.b * 0.0039215689f; + particle.m_model->unk_0178 = 0.0039215689f * alphaByte; + + model = particle.m_model; + + if (model->m_attachParent) { + model->m_fFlags |= 0x80; + } else { + model->m_fFlags |= 0x8; + } + + if (model->m_attachParent) { + model->m_fFlags |= 0x20000; + } else { + model->m_fFlags |= 0x10000; + } + + if (particle.m_model->IsLoaded(0, 0)) { + CAaSphere sphere = particle.m_model->GetBoundingSphere(); + sphere.c *= transform; + + float radius = scale.y; + + if (scale.x > scale.y) { + radius = scale.x; + } + + sphere.r = radius * sphere.r; + this->m_worldBounds |= sphere.Box(); + } + + return 1; +} + +// VERIFY: 3f71125d manual +// OFFSET: 0x0097A990 +void CParticleEmitter2::SetParticleColors(const CImVector& color0, const CImVector& color1, const CImVector& color2) { + this->m_replacementColors[0].x = color0.r; + this->m_replacementColors[0].y = color0.g; + this->m_replacementColors[0].z = color0.b; + this->m_replacementColors[1].x = color1.r; + this->m_replacementColors[1].y = color1.g; + this->m_replacementColors[1].z = color1.b; + this->m_replacementColors[2].x = color2.r; + this->m_replacementColors[2].y = color2.g; + this->m_replacementColors[2].z = color2.b; + + this->m_flags |= 0x10; + + if (!(this->m_flags & 0x1000000)) { + return; + } + + this->m_simpleKeys[0].m_color.r = color0.r; + this->m_simpleKeys[0].m_color.g = color0.g; + this->m_simpleKeys[0].m_color.b = color0.b; + this->m_simpleKeys[0].m_deltaR = color1.r - color0.r; + this->m_simpleKeys[0].m_deltaG = color1.g - color0.g; + this->m_simpleKeys[0].m_deltaB = color1.b - color0.b; + + this->m_simpleKeys[1].m_color.r = color1.r; + this->m_simpleKeys[1].m_color.b = color1.b; + this->m_simpleKeys[1].m_color.g = color1.g; + this->m_simpleKeys[1].m_deltaR = color2.r - color1.r; + this->m_simpleKeys[1].m_deltaG = color2.g - color1.g; + this->m_simpleKeys[1].m_deltaB = color2.b - color1.b; +} + +// VERIFY: cc45e19d manual +// OFFSET: 0x0097AB10 +void CParticleEmitter2::GetReplacementColors(CImVector& color0, CImVector& color1, CImVector& color2) { + color0.r = static_cast(CMath::fint_(this->m_replacementColors[0].x)); + color0.g = static_cast(CMath::fint_(this->m_replacementColors[0].y)); + color0.b = static_cast(CMath::fint_(this->m_replacementColors[0].z)); + color1.r = static_cast(CMath::fint_(this->m_replacementColors[1].x)); + color1.g = static_cast(CMath::fint_(this->m_replacementColors[1].y)); + color1.b = static_cast(CMath::fint_(this->m_replacementColors[1].z)); + color2.g = static_cast(CMath::fint_(this->m_replacementColors[2].y)); + color2.r = static_cast(CMath::fint_(this->m_replacementColors[2].x)); + color2.b = static_cast(CMath::fint_(this->m_replacementColors[2].z)); +} + +// VERIFY: fa4e24dc auto +// OFFSET: 0x0097AC00 +void CParticleEmitter2::SetTwinkleScale(const CRange& scale) { + this->m_twinkleScaleBase = scale.m_l; + this->m_twinkleScaleSpan = scale.m_h - scale.m_l; +} + +// VERIFY: 1fde211d auto +// OFFSET: 0x0097AC20 +void CParticleEmitter2::UpdateXform(const C44Matrix& transform, const C3Vector& viewPos, const C44Matrix* frameOfReference) { + this->m_viewPos = viewPos; + + if (!frameOfReference || (this->m_flags & 0x200)) { + this->m_xform = transform; + } else { + this->m_xform = transform * frameOfReference->AffineInverse(); + } + + this->m_xformScale = sqrt(transform.a2 * transform.a2 + transform.a1 * transform.a1 + transform.a0 * transform.a0); +} + +// VERIFY: 80901efd manual +// OFFSET: 0x0097ACB0 +void CParticleEmitter2::InternalUpdate(float deltaTime, int32_t child) { + float time = 0.0f; + + if (deltaTime < 0.0f || (time = deltaTime, deltaTime <= 0.1f)) { + this->m_burstOffset = this->m_followOffset; + this->StepUpdate(time, child); + return; + } + + float steps = floor(time * 10.0f); + float remainder = deltaTime - steps * 0.1f; + float maxSteps = floor(this->m_life * 10.0f); + + if (maxSteps < steps) { + steps = maxSteps; + } + + int32_t count = CMath::fint_(steps - s_stepRound); + float scale = 1.0f / (count + 1); + + this->m_burstOffset = C3Vector(this->m_followOffset.x * scale, this->m_followOffset.y * scale, scale * this->m_followOffset.z); + + for (; count; count--) { + this->StepUpdate(0.1f, child); + } + + this->StepUpdate(remainder, child); +} + +// VERIFY: a66e4b1d auto +// OFFSET: 0x0097AE00 +static void RecursiveEmitterModelLoaded(CM2Model* model, void* arg) { + CParticleEmitter2* emitter = static_cast(arg); + + if (!model || !emitter) { + return; + } + + if (!(model->m_fFlags & 1)) { + model->WaitForLoad(nullptr); + } + + uint32_t count = 4; + + if (model->m_shared->m_data->m_particles.m_count < 4) { + if (!(model->m_fFlags & 1)) { + model->WaitForLoad(nullptr); + } + + count = model->m_shared->m_data->m_particles.m_count; + } + + for (uint32_t i = 0; i < count; i++) { + if (!(model->m_fFlags & 1)) { + model->WaitForLoad(nullptr); + } + + CParticleEmitter2* child = model->m_particleEmitters[i]; + emitter->m_childEmitters[emitter->m_childEmitterCount++] = child; + child->m_flags |= 0x1; + } +} + +// VERIFY: 8fe41730 auto +// OFFSET: 0x0097AEB0 +void CParticleEmitter2::CreateChildEmittersFromModel(CM2Scene* scene, const char* name) { + if (!scene) { + return; + } + + this->m_childModel = scene->CreateModel(name, 0); + + if (this->m_childModel) { + this->m_childModel->SetLoadedCallback(RecursiveEmitterModelLoaded, this); + } +} + +// VERIFY: 763fa597 manual +// OFFSET: 0x0097AEF0 +void CParticleEmitter2::ChangeFrameOfReference(const C44Matrix* matrix) { + if (!(this->m_flags & 0x200)) { + uint32_t count = this->m_liveList.Count(); + + for (uint32_t i = 0; i < count; i++) { + uint32_t index = this->m_liveList[i]; + CParticle2* particle = this->m_hasModel ? static_cast(&this->m_particleModels[index]) : &this->m_particles[index]; + + particle->m_position *= *matrix; + + C3Vector velocity; + velocity.x = particle->m_velocity.y * matrix->b0 + matrix->c0 * particle->m_velocity.z + matrix->a0 * particle->m_velocity.x; + velocity.y = matrix->a1 * particle->m_velocity.x + matrix->c1 * particle->m_velocity.z + particle->m_velocity.y * matrix->b1; + velocity.z = matrix->c2 * particle->m_velocity.z + particle->m_velocity.y * matrix->b2 + particle->m_velocity.x * matrix->a2; + particle->m_velocity = velocity; + } + } + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + this->m_childEmitters[i]->ChangeFrameOfReference(matrix); + } +} + +// VERIFY: 7e076018 manual +// OFFSET: 0x0097AFF0 +void CParticleEmitter2::SetSimpleColorKeys() { + SimpleKey* keys = this->m_simpleKeys; + + if (this->m_flags & 0x10) { + keys[0].m_color.r = static_cast(CMath::fint_(this->m_replacementColors[0].x)); + keys[0].m_color.g = static_cast(CMath::fint_(this->m_replacementColors[0].y)); + keys[0].m_color.b = static_cast(CMath::fint_(this->m_replacementColors[0].z)); + keys = this->m_simpleKeys; + keys[1].m_color.r = static_cast(CMath::fint_(this->m_replacementColors[1].x)); + keys[1].m_color.b = static_cast(CMath::fint_(this->m_replacementColors[1].z)); + keys[1].m_color.g = static_cast(CMath::fint_(this->m_replacementColors[1].y)); + this->m_simpleKeys[0].m_deltaR = this->m_simpleKeys[1].m_color.r - this->m_simpleKeys[0].m_color.r; + this->m_simpleKeys[0].m_deltaG = this->m_simpleKeys[1].m_color.g - this->m_simpleKeys[0].m_color.g; + this->m_simpleKeys[0].m_deltaB = this->m_simpleKeys[1].m_color.b - this->m_simpleKeys[0].m_color.b; + this->m_simpleKeys[1].m_deltaR = static_cast(CMath::fint_(this->m_replacementColors[2].x)) - this->m_simpleKeys[1].m_color.r; + this->m_simpleKeys[1].m_deltaG = static_cast(CMath::fint_(this->m_replacementColors[2].y)) - this->m_simpleKeys[1].m_color.g; + this->m_simpleKeys[1].m_deltaB = static_cast(CMath::fint_(this->m_replacementColors[2].z)) - this->m_simpleKeys[1].m_color.b; + return; + } + + const M2PartTrack* track = this->m_colorTrack; + keys[0].m_color.r = static_cast(CMath::fint_(track->m_values[0].x)); + keys[0].m_color.g = static_cast(CMath::fint_(track->m_values[0].y)); + keys[0].m_color.b = static_cast(CMath::fint_(track->m_values[0].z)); + + switch (this->m_colorTrack->m_values.m_count) { + case 1: + this->m_simpleKeys[0].m_deltaR = 0; + this->m_simpleKeys[0].m_deltaG = 0; + this->m_simpleKeys[0].m_deltaB = 0; + break; + + case 2: + this->m_simpleKeys[0].m_deltaR = static_cast(CMath::fint_((this->m_colorTrack->m_values[1].x - this->m_colorTrack->m_values[0].x) * this->m_simpleMidTime)); + this->m_simpleKeys[0].m_deltaG = static_cast(CMath::fint_((this->m_colorTrack->m_values[1].y - this->m_colorTrack->m_values[0].y) * this->m_simpleMidTime)); + this->m_simpleKeys[0].m_deltaB = static_cast(CMath::fint_((this->m_colorTrack->m_values[1].z - this->m_colorTrack->m_values[0].z) * this->m_simpleMidTime)); + break; + + case 3: + this->m_simpleKeys[0].m_deltaR = static_cast(CMath::fint_(this->m_colorTrack->m_values[1].x - this->m_colorTrack->m_values[0].x)); + this->m_simpleKeys[0].m_deltaG = static_cast(CMath::fint_(this->m_colorTrack->m_values[1].y - this->m_colorTrack->m_values[0].y)); + this->m_simpleKeys[0].m_deltaB = static_cast(CMath::fint_(this->m_colorTrack->m_values[1].z - this->m_colorTrack->m_values[0].z)); + break; + } + + this->m_simpleKeys[1].m_color.r = this->m_simpleKeys[0].m_color.r + this->m_simpleKeys[0].m_deltaR; + this->m_simpleKeys[1].m_color.g = this->m_simpleKeys[0].m_color.g + this->m_simpleKeys[0].m_deltaG; + this->m_simpleKeys[1].m_color.b = this->m_simpleKeys[0].m_color.b + this->m_simpleKeys[0].m_deltaB; + + if (this->m_colorTrack->m_values.m_count == 1) { + this->m_simpleKeys[1].m_deltaR = 0; + this->m_simpleKeys[1].m_deltaG = 0; + this->m_simpleKeys[1].m_deltaB = 0; + } else if (this->m_colorTrack->m_values.m_count == 2) { + this->m_simpleKeys[1].m_deltaR = static_cast(CMath::fint_((this->m_colorTrack->m_values[1].x - this->m_colorTrack->m_values[0].x) * (1.0f - this->m_simpleMidTime))); + this->m_simpleKeys[1].m_deltaG = static_cast(CMath::fint_((this->m_colorTrack->m_values[1].y - this->m_colorTrack->m_values[0].y) * (1.0f - this->m_simpleMidTime))); + this->m_simpleKeys[1].m_deltaB = static_cast(CMath::fint_((this->m_colorTrack->m_values[1].z - this->m_colorTrack->m_values[0].z) * (1.0f - this->m_simpleMidTime))); + } else if (this->m_colorTrack->m_values.m_count == 3) { + this->m_simpleKeys[1].m_deltaR = static_cast(CMath::fint_(this->m_colorTrack->m_values[2].x - this->m_colorTrack->m_values[1].x)); + this->m_simpleKeys[1].m_deltaG = static_cast(CMath::fint_(this->m_colorTrack->m_values[2].y - this->m_colorTrack->m_values[1].y)); + this->m_simpleKeys[1].m_deltaB = static_cast(CMath::fint_(this->m_colorTrack->m_values[2].z - this->m_colorTrack->m_values[1].z)); + } +} + +// VERIFY: 6beda35b manual +// OFFSET: 0x0097B440 +void CParticleEmitter2::SetSimpleAlphaKeys() { + this->m_simpleKeys[0].m_color.a = static_cast(CMath::fint_(this->m_alphaTrack->m_values[0].m_value * 0.000030518509f * 255.0f)); + + const M2PartTrack* track = this->m_alphaTrack; + + switch (track->m_values.m_count) { + case 1: + this->m_simpleKeys[0].m_deltaA = 0; + break; + + case 2: + this->m_simpleKeys[0].m_deltaA = static_cast(CMath::fint_((track->m_values[1].m_value - track->m_values[0].m_value) * 0.000030518509f * this->m_simpleMidTime * 255.0f)); + break; + + case 3: + this->m_simpleKeys[0].m_deltaA = static_cast(CMath::fint_((track->m_values[1].m_value - track->m_values[0].m_value) * 0.000030518509f * 255.0f)); + break; + } + + this->m_simpleKeys[1].m_color.a = this->m_simpleKeys[0].m_color.a + this->m_simpleKeys[0].m_deltaA; + + if (this->m_alphaTrack->m_values.m_count == 1) { + this->m_simpleKeys[1].m_deltaA = 0; + } else if (this->m_alphaTrack->m_values.m_count == 2) { + int32_t delta = this->m_alphaTrack->m_values[1].m_value - this->m_alphaTrack->m_values[0].m_value; + this->m_simpleKeys[1].m_deltaA = static_cast(CMath::fint_((1.0f - this->m_simpleMidTime) * (delta * 0.000030518509f) * 255.0f)); + } else if (this->m_alphaTrack->m_values.m_count == 3) { + int32_t delta = this->m_alphaTrack->m_values[2].m_value - this->m_alphaTrack->m_values[1].m_value; + this->m_simpleKeys[1].m_deltaA = static_cast(CMath::fint_(delta * 0.000030518509f * 255.0f)); + } +} + +// VERIFY: 53e78e6f manual +// OFFSET: 0x0097B5F0 +void CParticleEmitter2::SetSimpleScaleKeys() { + this->m_simpleKeys[0].m_scale = this->m_scaleTrack->m_values[0].x; + + const M2PartTrack* track = this->m_scaleTrack; + float delta = 0.0f; + + switch (track->m_values.m_count) { + case 1: + this->m_simpleKeys[0].m_deltaScale = 0.0f; + break; + + case 2: + this->m_simpleKeys[0].m_deltaScale = (track->m_values[1].x - track->m_values[0].x) * this->m_simpleMidTime; + break; + + case 3: + this->m_simpleKeys[0].m_deltaScale = track->m_values[1].x - track->m_values[0].x; + break; + } + + this->m_simpleKeys[1].m_scale = this->m_simpleKeys[0].m_deltaScale + this->m_simpleKeys[0].m_scale; + + track = this->m_scaleTrack; + + if (track->m_values.m_count == 1) { + this->m_simpleKeys[1].m_deltaScale = delta; + } else if (track->m_values.m_count == 2) { + this->m_simpleKeys[1].m_deltaScale = (1.0f - this->m_simpleMidTime) * (track->m_values[1].x - track->m_values[0].x); + } else if (track->m_values.m_count == 3) { + delta = track->m_values[2].x - track->m_values[1].x; + this->m_simpleKeys[1].m_deltaScale = delta; + } +} + +// VERIFY: e0aa945f manual +// OFFSET: 0x0097B6C0 +void CParticleEmitter2::SetSimpleHeadCellKeys() { + const M2PartTrack* track = this->m_headCellTrack; + + if (!track->m_values.m_count) { + this->m_simpleKeys[0].m_headCell = 0; + this->m_simpleKeys[0].m_deltaHeadCell = 0; + this->m_simpleKeys[1].m_headCell = 0; + this->m_simpleKeys[1].m_deltaHeadCell = 0; + return; + } + + this->m_simpleKeys[0].m_headCell = track->m_values[0]; + + switch (this->m_headCellTrack->m_values.m_count) { + case 1: + this->m_simpleKeys[0].m_deltaHeadCell = 0; + break; + + case 2: + this->m_simpleKeys[0].m_deltaHeadCell = static_cast(CMath::fint_((this->m_headCellTrack->m_values[1] - this->m_headCellTrack->m_values[0]) * this->m_simpleMidTime)); + break; + + case 3: + case 4: + this->m_simpleKeys[0].m_deltaHeadCell = this->m_headCellTrack->m_values[1] - this->m_headCellTrack->m_values[0]; + break; + } + + this->m_simpleKeys[1].m_headCell = this->m_simpleKeys[0].m_headCell + this->m_simpleKeys[0].m_deltaHeadCell; + + switch (this->m_headCellTrack->m_values.m_count) { + case 1: + this->m_simpleKeys[1].m_deltaHeadCell = 0; + break; + + case 2: + this->m_simpleKeys[1].m_deltaHeadCell = static_cast(CMath::fint_((1.0f - this->m_simpleMidTime) * (this->m_headCellTrack->m_values[1] - this->m_headCellTrack->m_values[0]))); + break; + + case 3: + this->m_simpleKeys[1].m_deltaHeadCell = this->m_headCellTrack->m_values[2] - this->m_headCellTrack->m_values[1]; + break; + + case 4: + this->m_simpleKeys[1].m_deltaHeadCell = this->m_headCellTrack->m_values[3] - this->m_headCellTrack->m_values[2]; + break; + } +} + +// VERIFY: 73532e64 manual +// OFFSET: 0x0097B840 +void CParticleEmitter2::SetSimpleTailCellKeys() { + const M2PartTrack* track = this->m_tailCellTrack; + + if (!track->m_values.m_count) { + this->m_simpleKeys[0].m_tailCell = 0; + this->m_simpleKeys[0].m_deltaTailCell = 0; + this->m_simpleKeys[1].m_tailCell = 0; + this->m_simpleKeys[1].m_deltaTailCell = 0; + return; + } + + this->m_simpleKeys[0].m_tailCell = track->m_values[0]; + + switch (this->m_tailCellTrack->m_values.m_count) { + case 1: + this->m_simpleKeys[0].m_deltaTailCell = 0; + break; + + case 2: + this->m_simpleKeys[0].m_deltaTailCell = static_cast(CMath::fint_((this->m_tailCellTrack->m_values[1] - this->m_tailCellTrack->m_values[0]) * this->m_simpleMidTime)); + break; + + case 3: + case 4: + this->m_simpleKeys[0].m_deltaTailCell = this->m_tailCellTrack->m_values[1] - this->m_tailCellTrack->m_values[0]; + break; + } + + this->m_simpleKeys[1].m_tailCell = this->m_simpleKeys[0].m_tailCell + this->m_simpleKeys[0].m_deltaTailCell; + + switch (this->m_tailCellTrack->m_values.m_count) { + case 1: + this->m_simpleKeys[1].m_deltaTailCell = 0; + break; + + case 2: + this->m_simpleKeys[1].m_deltaTailCell = static_cast(CMath::fint_((1.0f - this->m_simpleMidTime) * (this->m_tailCellTrack->m_values[1] - this->m_tailCellTrack->m_values[0]))); + break; + + case 3: + this->m_simpleKeys[1].m_deltaTailCell = this->m_tailCellTrack->m_values[2] - this->m_tailCellTrack->m_values[1]; + break; + + case 4: + this->m_simpleKeys[1].m_deltaTailCell = this->m_tailCellTrack->m_values[3] - this->m_tailCellTrack->m_values[2]; + break; + } +} + +// VERIFY: c36b86ed auto +// OFFSET: 0x0097B9C0 +CParticleEmitter2::SimpleKey::SimpleKey() { + this->m_deltaR = 0; + this->m_deltaG = 0; + this->m_deltaB = 0; + this->m_deltaA = 0; +} + +// VERIFY: b35364c3 auto +// OFFSET: 0x0097B9E0 +int32_t CParticleEmitter2::HasLiveParticles() { + if (this->m_liveList.Count()) { + return 1; + } + + uint32_t count = this->m_childEmitterCount; + + for (uint32_t i = 0; i < count; i++) { + if (this->m_childEmitters[i]->HasLiveParticles()) { + return 1; + } + } + + return 0; +} + +// VERIFY: 0970c3c4 manual +// OFFSET: 0x0097BA30 +uint32_t CParticleEmitter2::GetNumParticleModels() { + uint32_t count = 0; + + if (this->m_hasModel == 1) { + count = this->m_liveList.Count(); + } + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + count += this->m_childEmitters[i]->GetNumParticleModels(); + } + + return count; +} + +// VERIFY: f0baee4c auto +// OFFSET: 0x0097BA70 +CM2Model* CParticleEmitter2::GetParticleModelInternal(uint32_t& index) { + if (this->m_hasModel == 1) { + uint32_t count = this->m_liveList.Count(); + + if (index < count) { + return this->m_particleModels[this->m_liveList[index]].m_model; + } + + index -= count; + } + + uint32_t childCount = this->m_childEmitterCount; + + for (uint32_t i = 0; i < childCount; i++) { + CM2Model* model = this->m_childEmitters[i]->GetParticleModelInternal(index); + + if (model) { + return model; + } + } + + return nullptr; +} + +// VERIFY: cc9ab909 auto +// OFFSET: 0x0097BAE0 +uint32_t CParticleEmitter2::GetNumBatches() { + uint32_t count = this->m_batchFlags & 0x1; + + if (count >= this->m_liveList.Count()) { + count = this->m_liveList.Count(); + } + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + count += this->m_childEmitters[i]->GetNumBatches(); + } + + return count; +} + +// VERIFY: e8406df5 auto +// OFFSET: 0x0097BB30 +uint32_t CParticleEmitter2::GetNumPrimitives() { + uint32_t count = this->m_liveList.Count() * this->m_indicesPerParticle / 3; + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + count += this->m_childEmitters[i]->GetNumPrimitives(); + } + + return count; +} + +// VERIFY: 6d758994 manual +// OFFSET: [0x0097BCC0, 0x0097D890] +CParticleEmitter2::~CParticleEmitter2() { + if (this->m_texture) { + HandleClose(reinterpret_cast(this->m_texture)); + this->m_texture = nullptr; + } + + if (this->m_model) { + this->m_model->Release(); + this->m_model = nullptr; + } + + if (this->m_childModel) { + this->m_childModel->Release(); + this->m_childModel = nullptr; + } + + delete[] this->m_simpleKeys; +} + +// VERIFY: 051fbb67 auto +// OFFSET: 0x0097BD80 +void CParticleEmitter2::SetEmissionRate(float rate) { + if (rate <= 0.0f) { + this->m_emissionRate = 0.0f; + } else { + this->m_emissionRate = rate; + } +} + +// VERIFY: 05fb6744 manual +// OFFSET: 0x0097BDB0 +int32_t CParticleEmitter2::MoveParticle(CParticle2_Model& particle, float deltaTime) { + float speed = sqrt(particle.m_angularVelocity.z * particle.m_angularVelocity.z + particle.m_angularVelocity.y * particle.m_angularVelocity.y + particle.m_angularVelocity.x * particle.m_angularVelocity.x); + + if (speed > 0.000099999997f) { + float halfAngle = speed * deltaTime * 0.5f; + float scale = sin(halfAngle) * (1.0f / speed); + + C4Quaternion rotation; + rotation.x = particle.m_angularVelocity.x * scale; + rotation.y = particle.m_angularVelocity.y * scale; + rotation.z = scale * particle.m_angularVelocity.z; + rotation.w = cos(halfAngle); + + const C4Quaternion& q = particle.m_orientation; + C4Quaternion result; + result.x = q.w * rotation.x + rotation.z * q.y + rotation.w * q.x - q.z * rotation.y; + result.y = rotation.w * q.y + q.w * rotation.y + q.z * rotation.x - rotation.z * q.x; + result.z = rotation.z * q.w + rotation.w * q.z + q.x * rotation.y - rotation.x * q.y; + result.w = rotation.w * q.w - q.x * rotation.x - q.y * rotation.y - rotation.z * q.z; + particle.m_orientation = result; + } + + return this->MoveParticle(static_cast(particle), deltaTime); +} + +// VERIFY: 47fcc4bb manual +// OFFSET: 0x0097BE80 +int32_t CParticleEmitter2::BuildVertex(const CParticle2& particle, ParticleVertex& vertex) { + uint32_t twinkle = 0; + + if (this->m_twinkleOnOff < 1.0f || 0.0f != this->m_twinkleScaleSpan) { + twinkle = (static_cast(CMath::fint_(this->m_twinkleFPS * particle.m_age)) + (reinterpret_cast(&particle) >> 5)) & 0x7F; + } + + if (this->m_twinkleOnOff < 1.0f && this->m_twinkleOnOff < CParticleEmitter2::s_rndTable[twinkle]) { + return 0; + } + + CImVector color; + C2Vector scale; + int32_t headCell; + int32_t tailCell; + + if (this->m_flags & 0x1000000) { + this->InterpolateAllTracksSimple(particle, &color, &scale, &headCell, &tailCell); + } else { + this->InterpolateAllTracks(particle, &color, &scale, &headCell, &tailCell); + } + + float spin; + float spinRate; + this->RandomizeSpin(particle, &spin, &spinRate); + + if (g_theGxDevicePtr->Caps().m_colorFormat == GxCF_rgba) { + uint8_t red = color.r; + color.r = color.b; + color.b = red; + } + + float twinkleScale = CParticleEmitter2::s_rndTable[twinkle] * this->m_twinkleScaleSpan + this->m_twinkleScaleBase; + scale.x = scale.x * twinkleScale; + scale.y = twinkleScale * scale.y; + + if (this->m_flags & 0x400) { + scale.x = scale.x * this->m_xformScale; + scale.y = this->m_xformScale * scale.y; + } + + C3Vector center = particle.m_position * s_particleXform; + uint32_t flags = this->m_flags; + + if (flags & 0x4) { + uint32_t column = headCell & (this->m_textureCols - 1); + float u = column * this->m_textureCellWidth; + int32_t row = headCell >> this->m_textureCellShift; + float v = row * this->m_textureCellHeight; + + if ((flags & 0x200000) && particle.m_velocity.z * particle.m_velocity.z + particle.m_velocity.y * particle.m_velocity.y + particle.m_velocity.x * particle.m_velocity.x > 0.00000023841858f) { + C3Vector reverse(-particle.m_velocity.x, -particle.m_velocity.y, -particle.m_velocity.z); + C3Vector direction = TransformDirection(reverse, s_particleXform); + C2Vector flat(direction); + + float flatSq = flat.y * flat.y + flat.x * flat.x; + float directionSq = direction.y * direction.y + direction.z * direction.z + direction.x * direction.x; + float inv = 0.0f; + + if (flatSq > 0.00000023841858f) { + inv = 1.0f / sqrt(flatSq); + } + + float axisX = flat.x * inv; + float axisY = flat.y * inv; + C2Vector size = scale; + + if (0.00000023841858f < inv) { + size.x = 1.0f / sqrt(directionSq) / inv * scale.x; + } + + for (uint32_t i = 0; i < 4; i++) { + float offsetX = s_cornerOffsets[i][0] * size.x; + float offsetY = s_cornerOffsets[i][1] * size.y; + C3Vector position(offsetY * -axisY + offsetX * axisX + center.x, offsetX * axisY + offsetY * axisX + center.y, center.z); + C2Vector texCoord; + texCoord.x = s_cornerTexCoords[i][0] * this->m_textureCellWidth + u; + texCoord.y = s_cornerTexCoords[i][1] * this->m_textureCellHeight + v; + AddVertex(this->m_worldBounds, vertex, position, color, texCoord); + } + } else if (0.0f == this->m_spin && 0.0f == this->m_spinVariation) { + if (flags & 0x4000) { + for (uint32_t i = 0; i < 4; i++) { + float offsetX = s_cornerOffsets[i][0] * scale.x; + float offsetY = s_cornerOffsets[i][1] * scale.y; + C3Vector position(s_billboardXform.b0 * offsetY + offsetX * s_billboardXform.a0 + center.x, offsetY * s_billboardXform.b1 + offsetX * s_billboardXform.a1 + center.y, offsetY * s_billboardXform.b2 + offsetX * s_billboardXform.a2 + center.z); + C2Vector texCoord; + texCoord.x = s_cornerTexCoords[i][0] * this->m_textureCellWidth + u; + texCoord.y = s_cornerTexCoords[i][1] * this->m_textureCellHeight + v; + AddVertex(this->m_worldBounds, vertex, position, color, texCoord); + } + } else { + for (uint32_t i = 0; i < 4; i++) { + C3Vector position(s_cornerOffsets[i][0] * scale.x + center.x, s_cornerOffsets[i][1] * scale.y + center.y, center.z); + C2Vector texCoord; + texCoord.x = s_cornerTexCoords[i][0] * this->m_textureCellWidth + u; + texCoord.y = s_cornerTexCoords[i][1] * this->m_textureCellHeight + v; + AddVertex(this->m_worldBounds, vertex, position, color, texCoord); + } + } + } else { + float angle = particle.m_age * spinRate + spin; + + if ((flags & 0x10000) && (reinterpret_cast(&particle) & 0x20)) { + angle = -angle; + } + + if (flags & 0x4000) { + for (uint32_t i = 0; i < 4; i++) { + C33Matrix rotation = C33Matrix::Rotation(angle, this->m_billboardAxis, true); + float offsetX = s_cornerOffsets[i][0] * scale.x; + float offsetY = s_cornerOffsets[i][1] * scale.y; + float billboardX = s_billboardXform.b0 * offsetY + offsetX * s_billboardXform.a0; + float billboardY = offsetY * s_billboardXform.b1 + offsetX * s_billboardXform.a1; + float billboardZ = offsetY * s_billboardXform.b2 + offsetX * s_billboardXform.a2; + C3Vector position(rotation.a2 * billboardZ + rotation.a1 * billboardY + billboardX * rotation.a0 + center.x, rotation.b2 * billboardZ + rotation.b1 * billboardY + rotation.b0 * billboardX + center.y, billboardX * rotation.c0 + billboardY * rotation.c1 + billboardZ * rotation.c2 + center.z); + C2Vector texCoord; + texCoord.x = s_cornerTexCoords[i][0] * this->m_textureCellWidth + u; + texCoord.y = s_cornerTexCoords[i][1] * this->m_textureCellHeight + v; + AddVertex(this->m_worldBounds, vertex, position, color, texCoord); + } + } else { + float sinAngle = sin(angle); + float cosAngle = cos(angle); + float cosX = cosAngle * scale.x; + float cosY = cosAngle * scale.y; + float sinX = scale.x * sinAngle; + float sinY = scale.y * sinAngle; + C2Vector texCoord; + + texCoord.x = u; + texCoord.y = v; + AddVertex(this->m_worldBounds, vertex, C3Vector(center.x - cosX - sinY, center.y - sinX + cosY, center.z), color, texCoord); + + texCoord.x = u; + texCoord.y = v + this->m_textureCellHeight; + AddVertex(this->m_worldBounds, vertex, C3Vector(center.x - cosX + sinY, center.y - sinX - cosY, center.z), color, texCoord); + + texCoord.x = this->m_textureCellWidth + u; + texCoord.y = v; + AddVertex(this->m_worldBounds, vertex, C3Vector(cosX + center.x - sinY, cosY + sinX + center.y, center.z), color, texCoord); + + texCoord.x = u + this->m_textureCellWidth; + texCoord.y = v + this->m_textureCellHeight; + AddVertex(this->m_worldBounds, vertex, C3Vector(cosX + sinY + center.x, sinX + center.y - cosY, center.z), color, texCoord); + } + } + } + + if (!(this->m_flags & 0x8)) { + return 1; + } + + uint32_t column = tailCell & (this->m_textureCols - 1); + float u = column * this->m_textureCellWidth; + int32_t row = tailCell >> this->m_textureCellShift; + float v = row * this->m_textureCellHeight; + + float tailLength = this->m_tailLength; + + if ((this->m_flags & 0x20000) && tailLength > particle.m_age) { + tailLength = particle.m_age; + } + + float reverseX = -particle.m_velocity.x; + float reverseY = -particle.m_velocity.y; + float reverseZ = -particle.m_velocity.z; + + C3Vector tail; + tail.x = (reverseY * s_particleXform.b0 + reverseZ * s_particleXform.c0 + reverseX * s_particleXform.a0) * tailLength; + tail.y = (reverseY * s_particleXform.b1 + reverseZ * s_particleXform.c1 + reverseX * s_particleXform.a1) * tailLength; + tail.z = tailLength * (reverseY * s_particleXform.b2 + reverseZ * s_particleXform.c2 + reverseX * s_particleXform.a2); + + C2Vector flat(tail); + float flatSq = flat.y * flat.y + flat.x * flat.x; + + if (flatSq < 0.00077160494f) { + for (uint32_t i = 0; i < 4; i++) { + C3Vector position(s_cornerOffsets[i][0] * scale.x + center.x, s_cornerOffsets[i][1] * scale.y + center.y, center.z); + C2Vector texCoord; + texCoord.x = s_cornerTexCoords[i][0] * this->m_textureCellWidth + u; + texCoord.y = s_cornerTexCoords[i][1] * this->m_textureCellHeight + v; + AddVertex(this->m_worldBounds, vertex, position, color, texCoord); + } + + return 1; + } + + float inv = 1.0f / sqrt(flatSq); + float perpX = flat.x * (scale.x * inv); + float perpY = flat.y * (inv * scale.y); + C3Vector end(tail.x + center.x, tail.y + center.y, tail.z + center.z); + C2Vector texCoord; + + texCoord.x = this->m_textureCellWidth * s_cornerTexCoords[0][0] + u; + texCoord.y = this->m_textureCellHeight * s_cornerTexCoords[0][1] + v; + AddVertex(this->m_worldBounds, vertex, C3Vector(center.x - perpY, center.y + perpX, center.z), color, texCoord); + + texCoord.x = this->m_textureCellWidth * s_cornerTexCoords[1][0] + u; + texCoord.y = this->m_textureCellHeight * s_cornerTexCoords[1][1] + v; + AddVertex(this->m_worldBounds, vertex, C3Vector(perpY + center.x, center.y - perpX, center.z), color, texCoord); + + texCoord.x = this->m_textureCellWidth * s_cornerTexCoords[2][0] + u; + texCoord.y = this->m_textureCellHeight * s_cornerTexCoords[2][1] + v; + AddVertex(this->m_worldBounds, vertex, C3Vector(end.x - perpY, perpX + end.y, end.z), color, texCoord); + + texCoord.x = u + s_cornerTexCoords[3][0] * this->m_textureCellWidth; + texCoord.y = v + this->m_textureCellHeight * s_cornerTexCoords[3][1]; + AddVertex(this->m_worldBounds, vertex, C3Vector(end.x + perpY, end.y - perpX, end.z), color, texCoord); + + return 1; +} + +// VERIFY: ae2ccc25 manual +// OFFSET: 0x0097D370 +void CParticleEmitter2::DetermineIfSimple() { + uint32_t colorKeys = this->m_colorTrack->m_times.m_count; + + if (colorKeys > 3 || !colorKeys) { + return; + } + + uint32_t alphaKeys = this->m_alphaTrack->m_times.m_count; + + if (alphaKeys > 3 || !alphaKeys) { + return; + } + + const M2PartTrack* scaleTrack = this->m_scaleTrack; + uint32_t scaleKeys = scaleTrack->m_times.m_count; + + if (scaleKeys > 3 || !scaleKeys) { + return; + } + + uint32_t headKeys = this->m_headCellTrack->m_times.m_count; + + if (headKeys > 4 || (headKeys == 4 && this->m_headCellTrack->m_times[1].m_value != this->m_headCellTrack->m_times[2].m_value)) { + return; + } + + uint32_t tailKeys = this->m_tailCellTrack->m_times.m_count; + + if (tailKeys > 4 || (tailKeys == 4 && this->m_tailCellTrack->m_times[1].m_value != this->m_tailCellTrack->m_times[2].m_value)) { + return; + } + + if (0.0f != this->m_lifeVariation || (!headKeys && (this->m_flags & 0x100000))) { + return; + } + + if (this->m_scaleVariationX != 0.0f || this->m_scaleVariationY != 0.0f) { + return; + } + + for (uint32_t i = 0; i < scaleTrack->m_values.m_count; i++) { + if (CMath::fnotequal(scaleTrack->m_values[i].x, scaleTrack->m_values[i].y)) { + return; + } + } + + int16_t midTime = 0; + + if (colorKeys == 3) { + midTime = this->m_colorTrack->m_times[1].m_value; + } else if (alphaKeys == 3) { + midTime = this->m_alphaTrack->m_times[1].m_value; + } else if (scaleKeys == 3) { + midTime = this->m_scaleTrack->m_times[1].m_value; + } else if (headKeys >= 3) { + midTime = this->m_headCellTrack->m_times[1].m_value; + } else if (tailKeys >= 3) { + midTime = this->m_tailCellTrack->m_times[1].m_value; + } + + if (alphaKeys == 3 && this->m_alphaTrack->m_times[1].m_value != midTime) { + return; + } + + if (scaleKeys == 3 && this->m_scaleTrack->m_times[1].m_value != midTime) { + return; + } + + if (headKeys >= 3 && this->m_headCellTrack->m_times[1].m_value != midTime) { + return; + } + + if (tailKeys >= 3 && this->m_tailCellTrack->m_times[1].m_value != midTime) { + return; + } + + this->m_simpleKeys = new (__FILE__, __LINE__) SimpleKey[2]; + + if (!this->m_simpleKeys) { + return; + } + + this->m_flags |= 0x1000000; + this->m_simpleMidTime = midTime * 0.000030518509f; + + this->SetSimpleColorKeys(); + this->SetSimpleAlphaKeys(); + this->SetSimpleScaleKeys(); + this->SetSimpleHeadCellKeys(); + this->SetSimpleTailCellKeys(); +} + +// VERIFY: 4fbdf517 manual +// OFFSET: 0x0097D730 +CSortableParticleQueue::CSortableParticleQueue(const uint32_t* params) : CMemBlock(sizeof(CSortableParticleRecord) * params[0], 0, typeid(CSortableParticleRecord).raw_name(), SLOG_OBJECT) { + uint32_t count = params[0]; + uint32_t chunk = params[1]; + + this->m_chunk = chunk; + this->m_capacity = count; + this->m_count = 0; + + if (count) { + this->m_count++; + return; + } + + if (!chunk) { + return; + } + + uint32_t grow = chunk; + + if (grow <= 1 - count) { + grow = 1 - count; + } + + if (this->Resize(sizeof(CSortableParticleRecord) * (grow + count), true)) { + this->m_capacity += grow; + this->m_count++; + } +} + +// VERIFY: 6d15b360 manual +// OFFSET: 0x0097D820 +void CParticleEmitter2::EmitParticle(float time, const C44Matrix& transform) { + uint32_t index = PopIndex(this->m_freeList); + this->m_liveList.New(index); + + if (this->m_hasModel) { + this->CreateParticle(this->m_particleModels[index], time, transform); + } else { + this->CreateParticle(this->m_particles[index], time, transform); + } +} + +// VERIFY: fb59bfc3 manual +// OFFSET: 0x0097D8C0 +void CParticleEmitter2::EmitNewParticles(float deltaTime, const C44Matrix& transform) { + float distanceScale = 1.0f; + + if (!(this->m_flags & 0x400000)) { + distanceScale = 1.0f - (CParticleEmitter2::s_viewDist - 50.0f) * 0.02f; + + if (distanceScale < 0.25f) { + distanceScale = 0.25f; + } else if (distanceScale >= 1.0f) { + distanceScale = 1.0f; + } + } + + float rate = CRandom::sfloat_(this->m_random) * this->m_emissionRateVariation + this->m_emissionRate; + rate = ParticleSystemManager::GetScaler() * rate * distanceScale; + + if ((this->m_flags & 0x40) && (this->m_flags & 0x2)) { + int32_t burst = static_cast(rate); + + while (this->m_freeList.Count() && burst--) { + this->EmitParticle(0.0f, transform); + } + + this->m_flags &= ~0x40; + } + + if ((this->m_flags & 0x3) != 0x3) { + return; + } + + uint32_t emitted = 0; + this->m_emitAccumulator = rate * deltaTime + this->m_emitAccumulator; + + if (this->m_flags & 0x2000) { + C44Matrix& xform = const_cast(transform); + C3Vector position(xform.d0, xform.d1, xform.d2); + C3Vector delta(position.x - this->m_parentPosition.x, position.y - this->m_parentPosition.y, position.z - this->m_parentPosition.z); + int32_t count = static_cast(this->m_emitAccumulator + 0.5f); + + if (this->m_freeList.Count()) { + while (count--) { + float t = CRandom::ufloat_(this->m_random); + xform.d0 = delta.x * t + this->m_parentPosition.x; + xform.d1 = delta.y * t + this->m_parentPosition.y; + xform.d2 = t * delta.z + this->m_parentPosition.z; + + this->EmitParticle(deltaTime, transform); + emitted++; + + if (!this->m_freeList.Count()) { + break; + } + } + } + + xform.d0 = position.x; + xform.d1 = position.y; + xform.d2 = position.z; + } else { + int32_t count = static_cast(this->m_emitAccumulator + 0.5f); + + if (this->m_freeList.Count()) { + while (count--) { + this->EmitParticle(deltaTime, transform); + emitted++; + + if (!this->m_freeList.Count()) { + break; + } + } + } + } + + this->m_emitAccumulator = this->m_emitAccumulator - emitted; +} + +// VERIFY: a5dfc49e manual +// OFFSET: 0x0097DB80 +int32_t CParticleEmitter2::UpdateLiveParticle(float deltaTime, CParticle2* particle, uint32_t index) { + C3Vector position = particle->m_position; + int32_t alive; + + if (this->m_hasModel) { + alive = this->MoveParticle(*static_cast(particle), deltaTime); + } else { + alive = this->MoveParticle(*particle, deltaTime); + } + + if (!alive) { + this->DestroyParticle(*particle); + this->m_freeList.New(this->m_liveList[index]); + this->m_liveList[index] = *this->m_liveList.Top(); + PopIndex(this->m_liveList); + return 0; + } + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + CParticleEmitter2* child = this->m_childEmitters[i]; + float d0 = this->m_xform.d0; + float d1 = this->m_xform.d1; + float d2 = this->m_xform.d2; + + this->m_xform.d0 = particle->m_position.x; + this->m_xform.d1 = particle->m_position.y; + this->m_xform.d2 = particle->m_position.z; + + if (child->m_flags & 0x800) { + child->m_parentVelocity = particle->m_velocity; + } + + if (child->m_flags & 0x2000) { + child->m_parentPosition = position; + } + + child->EmitNewParticles(deltaTime, this->m_xform); + + this->m_xform.d0 = d0; + this->m_xform.d1 = d1; + this->m_xform.d2 = d2; + } + + return 1; +} + +// VERIFY: e52cc262 manual +// OFFSET: 0x0097DD20 +void CParticleEmitter2::StepUpdate(float deltaTime, int32_t child) { + if (deltaTime <= 0.0f) { + return; + } + + if ((this->m_flags & 0x3) == 0x3 || ((this->m_flags & 0x40) && (this->m_flags & 0x2))) { + this->Sync(); + } + + if (!child) { + this->EmitNewParticles(deltaTime, this->m_xform); + } + + uint32_t i = 0; + + if (0.0f == this->m_lifeVariation) { + float life = 0.001f; + + if (this->m_life > 0.001f) { + life = this->m_life; + } + + while (i < this->m_liveList.Count()) { + uint32_t index = this->m_liveList[i]; + CParticle2* particle = this->m_hasModel ? static_cast(&this->m_particleModels[index]) : &this->m_particles[index]; + particle->m_age = particle->m_age + deltaTime; + + if (particle->m_age < life) { + if (this->UpdateLiveParticle(deltaTime, particle, i)) { + i++; + } + } else { + this->DestroyParticle(*particle); + this->m_freeList.New(this->m_liveList[i]); + this->m_liveList[i] = *this->m_liveList.Top(); + PopIndex(this->m_liveList); + } + } + } else { + while (i < this->m_liveList.Count()) { + float life = 0.001f; + uint32_t index = this->m_liveList[i]; + CParticle2* particle = this->m_hasModel ? static_cast(&this->m_particleModels[index]) : &this->m_particles[index]; + particle->m_age = particle->m_age + deltaTime; + + float particleLife = particle->m_lifeSeed * this->m_lifeVariation * 0.000030518509f + this->m_life; + + if (particleLife > 0.001f) { + life = particleLife; + } + + if (particle->m_age < life) { + if (this->UpdateLiveParticle(deltaTime, particle, i)) { + i++; + } + } else { + this->DestroyParticle(*particle); + this->m_freeList.New(this->m_liveList[i]); + this->m_liveList[i] = *this->m_liveList.Top(); + PopIndex(this->m_liveList); + } + } + } + + for (uint32_t j = 0; j < this->m_childEmitterCount; j++) { + this->m_childEmitters[j]->InternalUpdate(deltaTime, 1); + } +} + +// VERIFY: c7e311d7 manual +// OFFSET: 0x0097E080 +CSortableParticleRecord CSortableParticleQueue::Pop() { + CSortableParticleRecord* data = reinterpret_cast(this->m_data); + CSortableParticleRecord top = data[1]; + CSortableParticleRecord last = data[this->m_count - 1]; + + if (this->m_count) { + this->Erase(this->m_count - 1, 1); + } + + if (this->m_count < 2) { + return top; + } + + uint32_t limit = this->m_count - 1; + uint32_t half = limit >> 1; + uint32_t index = 1; + + if (half) { + while (1) { + uint32_t child = 2 * index; + data = reinterpret_cast(this->m_data); + + if (child < limit && data[child].m_depth <= data[child + 1].m_depth) { + child++; + } + + if (last.m_depth >= data[child].m_depth) { + break; + } + + data[index] = data[child]; + index = child; + + if (child > half) { + break; + } + } + } + + reinterpret_cast(this->m_data)[index] = last; + + return top; +} + +// VERIFY: b7a529c6 manual +// OFFSET: 0x0097E150 +CParticleEmitter2::CParticleEmitter2() : m_random(0) { + this->m_emitAccumulator = 0.0f; + this->m_refCount = 1; + this->m_textureCellShift = 0; + this->m_textureCellWidth = 1.0f; + this->m_textureCellHeight = 1.0f; + this->m_renderedParticles = 0; + this->m_emitterType = 0; + this->m_childEmitterCount = 4; + this->m_model = nullptr; + this->m_childModel = nullptr; + this->m_vertsPerParticle = 0; + this->m_indicesPerParticle = 0; + this->m_hasModel = 0; + this->m_emissionRate = 0.0f; + this->m_emissionRateVariation = 0.0f; + this->m_life = 0.0f; + this->m_lifeVariation = 0.0f; + this->m_tailLength = 1.0f; + this->m_speed = 0.0f; + this->m_gravity = 0.0f; + this->m_variation = 0.1f; + this->m_zsource = 0.0f; + this->m_initialSpin = 0.0f; + this->m_initialSpinVariation = 0.0f; + this->m_spin = 0.0f; + this->m_spinVariation = 0.0f; + this->m_materialBlend = 0; + this->m_materialFlags |= 0x7; + this->m_colorTrack = nullptr; + this->m_alphaTrack = nullptr; + this->m_scaleTrack = nullptr; + this->m_scaleVariationX = 0.0f; + this->m_scaleVariationY = 0.0f; + this->m_headCellTrack = nullptr; + this->m_tailCellTrack = nullptr; + this->m_simpleMidTime = 0.0f; + this->m_simpleKeys = nullptr; + this->m_alphaScale = 1.0f; + this->m_texture = nullptr; + this->unk_012C = 0; + this->m_twinkleFPS = 10.0f; + this->m_flags = 0x6; + this->m_textureRows = 1; + this->m_twinkleOnOff = 1.0f; + this->m_textureCols = 1; + this->m_twinkleScaleBase = 1.0f; + this->m_twinkleScaleSpan = 0.0f; + this->m_ivelScale = 1.0f; + this->m_tumbleBaseX = 0.0f; + this->m_tumbleSpanX = 0.0f; + this->m_tumbleBaseY = 0.0f; + this->m_tumbleSpanY = 0.0f; + this->m_tumbleBaseZ = 0.0f; + this->m_tumbleSpanZ = 0.0f; + this->m_drag = 0.0f; + this->m_windVectorX = 0.0f; + this->m_windVectorY = 0.0f; + this->m_windVectorZ = 0.0f; + this->m_windTime = 0.0f; + this->m_followIntercept = 0.0f; + this->m_followSlope = 0.0f; + this->m_ivelTimer = 0.0f; + this->m_worldBounds.b.x = 3.4028235e38f; + this->m_worldBounds.b.y = 3.4028235e38f; + this->m_worldBounds.b.z = 3.4028235e38f; + this->m_worldBounds.t.x = -3.4028235e38f; + this->m_worldBounds.t.y = -3.4028235e38f; + this->m_worldBounds.t.z = -3.4028235e38f; + this->unk_0230 = 0; + + this->m_random.SetSeed((rand() << 16) | static_cast(rand())); + this->m_childEmitterCount = 0; +} + +// VERIFY: 18fbfbdc manual +// OFFSET: 0x0097E3F0 +void CParticleEmitter2::SyncReserve(uint32_t count, uint32_t used, uint32_t spare) { + uint32_t target = count; + + if (count & (count - 1)) { + target = 2 * count - 1; + + for (uint32_t bits = target & (2 * count - 2); bits; bits &= bits - 1) { + target = bits; + } + } + + if (used + spare >= target) { + return; + } + + uint32_t shortfall = target - (used + spare); + + if (this->m_hasModel) { + if (shortfall + this->m_particleModels.Count() > this->m_particleModels.m_alloc) { + this->m_particleModels.ReallocData(shortfall + this->m_particleModels.Count()); + } + } else if (shortfall + this->m_particles.Count() > this->m_particles.m_alloc) { + this->m_particles.ReallocData(shortfall + this->m_particles.Count()); + } + + if (shortfall + this->m_liveList.Count() > this->m_liveList.m_alloc) { + this->m_liveList.ReallocData(shortfall + this->m_liveList.Count()); + } + + if (shortfall + this->m_freeList.Count() > this->m_freeList.m_alloc) { + this->m_freeList.ReallocData(shortfall + this->m_freeList.Count()); + } +} + +// VERIFY: f8eb894d manual +// OFFSET: 0x0097E480 +void CParticleEmitter2::SyncAllocation(uint32_t count) { + if (this->m_hasModel) { + uint32_t oldCount = this->m_particleModels.Count(); + + if (oldCount >= count) { + return; + } + + this->SyncReserve(count, oldCount, this->m_particleModels.m_alloc - this->m_particleModels.Count()); + this->m_particleModels.SetCount(count); + + if (count + this->m_liveList.Count() > this->m_liveList.m_alloc) { + this->m_liveList.ReallocData(count + this->m_liveList.Count()); + } + + if (count + this->m_freeList.Count() > this->m_freeList.m_alloc) { + this->m_freeList.ReallocData(count + this->m_freeList.Count()); + } + + for (uint32_t i = oldCount; i != count; i++) { + uint32_t index = i; + this->m_freeList.New(index); + } + } else { + uint32_t oldCount = this->m_particles.Count(); + + if (oldCount >= count) { + return; + } + + this->SyncReserve(count, oldCount, this->m_particles.m_alloc - this->m_particles.Count()); + this->m_particles.SetCount(count); + + if (count + this->m_liveList.Count() > this->m_liveList.m_alloc) { + this->m_liveList.ReallocData(count + this->m_liveList.Count()); + } + + if (count + this->m_freeList.Count() > this->m_freeList.m_alloc) { + this->m_freeList.ReallocData(count + this->m_freeList.Count()); + } + + for (uint32_t i = oldCount; i != count; i++) { + uint32_t index = i; + this->m_freeList.New(index); + } + } +} + +// VERIFY: e23b5cc2 manual +// OFFSET: 0x0097E580 +void CParticleEmitter2::BuildParticleVertices(ParticleVertex& vertex, uint32_t count, const C44Matrix& transform) { + uint32_t i = 0; + + if (this->m_flags & 0x20) { + for (; i < this->m_liveList.Count(); i++) { + uint32_t index = this->m_liveList[i]; + CParticle2* particle = this->m_hasModel ? static_cast(&this->m_particleModels[index]) : &this->m_particles[index]; + float depth = s_particleXform.c2 * particle->m_position.z + s_particleXform.b2 * particle->m_position.y + s_particleXform.a2 * particle->m_position.x + s_particleXform.d2; + CParticleEmitter2::s_pq.Push(depth, particle); + } + + for (uint32_t j = count; j; j--) { + CSortableParticleRecord record = CParticleEmitter2::s_pq.Pop(); + this->BuildVertex(*record.m_particle, vertex); + } + } else { + for (; i < count; i++) { + uint32_t index = this->m_liveList[i]; + CParticle2* particle = this->m_hasModel ? static_cast(&this->m_particleModels[index]) : &this->m_particles[index]; + this->BuildVertex(*particle, vertex); + } + } + + if (count) { + this->m_worldBounds = CAaBox::Transform(this->m_worldBounds, transform); + this->m_worldBounds.b.x = this->m_worldBounds.b.x + this->m_viewPos.x; + this->m_worldBounds.b.y = this->m_worldBounds.b.y + this->m_viewPos.y; + this->m_worldBounds.b.z = this->m_viewPos.z + this->m_worldBounds.b.z; + this->m_worldBounds.t.x = this->m_worldBounds.t.x + this->m_viewPos.x; + this->m_worldBounds.t.y = this->m_worldBounds.t.y + this->m_viewPos.y; + this->m_worldBounds.t.z = this->m_viewPos.z + this->m_worldBounds.t.z; + } + + this->m_renderedParticles = vertex.m_count / this->m_vertsPerParticle; +} + +// VERIFY: 156a4da3 manual +// OFFSET: 0x0097E730 +void CParticleEmitter2::RenderParticles(const C44Matrix* frameOfReference, void* buffer) { + CGxBuf* buf = nullptr; + + if (!s_particleRenderEnable) { + this->m_batchFlags &= ~0x1; + this->m_renderedParticles = 0; + return; + } + + C44Matrix view; + CGxDevice* device = g_theGxDevicePtr; + view = device->m_xforms[GxXform_View].m_mtx[device->m_xforms[GxXform_View].m_level]; + + this->RenderParticlesPrep(frameOfReference, view); + + if (!TextureGetGxTex(this->m_texture, 0, nullptr)) { + this->m_batchFlags &= ~0x1; + this->m_renderedParticles = 0; + return; + } + + uint32_t count = 0x4000 / this->m_vertsPerParticle; + + if (count >= this->m_liveList.Count()) { + count = this->m_liveList.Count(); + } + + EGxVertexBufferFormat format = (this->m_materialFlags & 0x1) ? GxVBF_PNCT : GxVBF_PCT; + + if (!buffer) { + buf = g_theGxDevicePtr->BufStream(GxPoolTarget_Vertex, GxVertexSize(format), count * this->m_vertsPerParticle); + buffer = g_theGxDevicePtr->BufLock(buf); + } + + ParticleVertex vertex; + vertex.m_count = 0; + this->FillOutParticleVertex(buffer, format, &vertex); + this->BuildParticleVertices(vertex, count, view.AffineInverse()); + + if (buf) { + g_theGxDevicePtr->BufUnlock(buf, 0); + buf->unk_001C = 1; + + if (this->m_renderedParticles) { + CParticleEmitter2::RenderParticleVertices(buf, format, vertex.m_count, this->m_renderedParticles * this->m_indicesPerParticle); + g_theGxDevicePtr->XformSetView(view); + } + } +} + +// VERIFY: 3d799495 manual +// OFFSET: 0x0097E8D0 +void CParticleEmitter2::PlaceParticleModels(const C44Matrix* frameOfReference) { + if (!this->m_model) { + return; + } + + uint32_t flags = this->m_flags; + + if (!(flags & 0x20)) { + for (uint32_t i = 0; i < this->m_liveList.Count(); i++) { + this->RenderParticle(this->m_particleModels[this->m_liveList[i]], frameOfReference); + } + + return; + } + + C44Matrix view; + CGxDevice* device = g_theGxDevicePtr; + view = device->m_xforms[GxXform_View].m_mtx[device->m_xforms[GxXform_View].m_level]; + + C44Matrix xform; + xform.a2 = 0.0f; + xform.b2 = 0.0f; + xform.c2 = 1.0f; + xform.d2 = 0.0f; + + if (flags & 0x200) { + xform = this->m_xform * view; + } else if (frameOfReference) { + xform = *frameOfReference * view; + } else { + xform = view; + } + + for (uint32_t i = 0; i < this->m_liveList.Count(); i++) { + CParticle2* particle = &this->m_particles[this->m_liveList[i]]; + float depth = particle->m_position.z * xform.c2 + particle->m_position.y * xform.b2 + particle->m_position.x * xform.a2 + xform.d2; + CParticleEmitter2::s_pq.Push(depth, particle); + } + + for (uint32_t i = this->m_liveList.Count(); i; i--) { + CSortableParticleRecord record = CParticleEmitter2::s_pq.Pop(); + this->RenderParticle(*static_cast(record.m_particle), frameOfReference); + } +} + +// VERIFY: f60f53b8 manual +// OFFSET: 0x0097EA60 +void CParticleEmitter2::Render(const C44Matrix* frameOfReference, void* buffer, uint32_t first) { + this->m_worldBounds.b.x = 3.4028235e38f; + this->m_worldBounds.b.y = 3.4028235e38f; + this->m_worldBounds.t.x = -3.4028235e38f; + this->m_worldBounds.b.z = 3.4028235e38f; + this->m_worldBounds.t.y = -3.4028235e38f; + this->m_worldBounds.t.z = -3.4028235e38f; + + if (!this->m_liveList.Count()) { + this->m_batchFlags &= ~0x1; + this->m_renderedParticles = 0; + return; + } + + this->m_batchFlags ^= (first ^ this->m_batchFlags) & 0x1; + + if (!this->m_hasModel) { + this->RenderParticles(frameOfReference, buffer); + } +} + +// VERIFY: 28969e0e manual +// OFFSET: 0x0097EB10 +void CParticleEmitter2::Update(float deltaTime, const C44Matrix& transform, const C3Vector& viewPos, const C44Matrix* frameOfReference) { + float dz = transform.d2 - viewPos.z; + float dy = transform.d1 - viewPos.y; + float dx = transform.d0 - viewPos.x; + float distanceSq = dx * dx + (dy * dy + dz * dz); + + CParticleEmitter2::s_viewDist = distanceSq <= 0.0f ? 0.0f : sqrt(distanceSq); + + this->m_parentPosition = C3Vector(this->m_xform.d0, this->m_xform.d1, this->m_xform.d2); + this->UpdateXform(transform, viewPos, frameOfReference); + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + this->m_childEmitters[i]->UpdateXform(transform, viewPos, frameOfReference); + } + + if (fabs(deltaTime) < 0.00000023841858f) { + this->m_flags |= 0x100; + return; + } + + float zero; + + if (this->m_flags & 0x80000) { + this->m_followOffset.x = this->m_xform.d0 - this->m_parentPosition.x; + this->m_followOffset.y = this->m_xform.d1 - this->m_parentPosition.y; + this->m_followOffset.z = this->m_xform.d2 - this->m_parentPosition.z; + + zero = 0.0f; + float lengthSq = this->m_followOffset.z * this->m_followOffset.z + this->m_followOffset.y * this->m_followOffset.y + this->m_followOffset.x * this->m_followOffset.x; + float speed = 0.0f; + + if (lengthSq > 0.0f) { + speed = sqrt(lengthSq) / deltaTime; + } + + float follow = speed * this->m_followSlope + this->m_followIntercept; + + if (follow < zero) { + follow = zero; + } else if (follow >= 1.0f) { + follow = 1.0f; + } + + this->m_followOffset.x = this->m_followOffset.x * follow; + this->m_followOffset.y = follow * this->m_followOffset.y; + this->m_followOffset.z = follow * this->m_followOffset.z; + } else { + zero = 0.0f; + } + + if (this->m_flags & 0x800) { + this->m_ivelTimer = this->m_ivelTimer + deltaTime; + + if (this->m_ivelTimer > 0.033333335f) { + float rate = this->m_ivelTimer * 29.999998f; + this->m_ivelTimer = zero; + + if (this->m_liveList.Count()) { + this->m_parentVelocity = C3Vector(this->m_xform.d0 - this->m_parentPosition.x, this->m_xform.d1 - this->m_parentPosition.y, this->m_xform.d2 - this->m_parentPosition.z); + + float scale = 1.0f / rate * this->m_ivelScale; + this->m_parentVelocity.x = this->m_parentVelocity.x * scale; + this->m_parentVelocity.y = scale * this->m_parentVelocity.y; + this->m_parentVelocity.z = scale * this->m_parentVelocity.z; + } else { + this->m_parentVelocity.x = zero; + this->m_parentVelocity.y = zero; + this->m_parentVelocity.z = zero; + } + } + } + + this->InternalUpdate(deltaTime, 0); + this->m_flags |= 0x80; + + if (this->m_hasModel == 1) { + this->PlaceParticleModels(frameOfReference); + } +} + +// VERIFY: 90fa6a95 manual +// OFFSET: 0x0097EDF0 +void CParticleEmitter2::Sync() { + float count = (this->m_lifeVariation + this->m_life) * (this->m_emissionRateVariation + this->m_emissionRate) * 1.15f; + this->SyncAllocation(static_cast(count)); + + for (uint32_t i = 0; i < this->m_childEmitterCount; i++) { + CParticleEmitter2* child = this->m_childEmitters[i]; + uint32_t childCount = static_cast(count * ((child->m_lifeVariation + child->m_life) * (child->m_emissionRateVariation + child->m_emissionRate) * 1.15f)); + + if (childCount > 0x1000) { + childCount = 0x1000; + } + + child->SyncAllocation(childCount); + } +} + +// VERIFY: 9cba3242 manual +// OFFSET: 0x0097EEC0 +CParticleEmitter2::CParticleEmitter2(const CParticleEmitter2& source, int32_t copyParticles) : m_random(0) { + this->m_emitAccumulator = 0.0f; + this->m_refCount = 1; + this->m_textureCellShift = source.m_textureCellShift; + this->m_textureCellWidth = source.m_textureCellWidth; + this->m_textureCellHeight = source.m_textureCellHeight; + this->m_priorityPlane = source.m_priorityPlane; + this->m_emitterType = source.m_emitterType; + this->m_childEmitterCount = 4; + this->m_vertsPerParticle = source.m_vertsPerParticle; + this->m_indicesPerParticle = source.m_indicesPerParticle; + memcpy(&this->m_hasModel, &source.m_hasModel, offsetof(CParticleEmitter2, m_textureRows) - offsetof(CParticleEmitter2, m_hasModel)); + this->m_textureRows = source.m_textureRows; + this->m_textureCols = source.m_textureCols; + this->unk_012C = source.unk_012C; + this->m_alphaScale = source.m_alphaScale; + this->m_flags = source.m_flags; + this->m_twinkleFPS = source.m_twinkleFPS; + this->m_twinkleOnOff = source.m_twinkleOnOff; + this->m_twinkleScaleBase = source.m_twinkleScaleBase; + this->m_twinkleScaleSpan = source.m_twinkleScaleSpan; + this->m_ivelScale = source.m_ivelScale; + this->m_tumbleBaseX = source.m_tumbleBaseX; + this->m_tumbleSpanX = source.m_tumbleSpanX; + this->m_tumbleBaseY = source.m_tumbleBaseY; + this->m_tumbleSpanY = source.m_tumbleSpanY; + this->m_tumbleBaseZ = source.m_tumbleBaseZ; + this->m_tumbleSpanZ = source.m_tumbleSpanZ; + this->m_drag = source.m_drag; + this->m_windVectorX = source.m_windVectorX; + this->m_windVectorY = source.m_windVectorY; + this->m_windVectorZ = source.m_windVectorZ; + this->m_windTime = source.m_windTime; + this->m_followIntercept = source.m_followIntercept; + this->m_followSlope = source.m_followSlope; + this->m_ivelTimer = source.m_ivelTimer; + this->m_worldBounds.b.x = 0.0f; + this->m_worldBounds.b.y = 0.0f; + this->m_worldBounds.b.z = 0.0f; + this->m_worldBounds.t.x = 0.0f; + this->m_worldBounds.t.y = 0.0f; + this->m_worldBounds.t.z = 0.0f; + this->unk_0230 = 0; + + this->m_random.SetSeed((rand() << 16) | static_cast(rand())); + this->m_childEmitterCount = source.m_childEmitterCount; + + for (uint32_t i = 0; i < source.m_childEmitterCount; i++) { + this->m_childEmitters[i] = ParticleSystemManager::GetInstance()->DuplicateEmitter(source.m_childEmitters[i], 0); + } + + this->m_model = source.m_model; + + if (this->m_model) { + this->m_model->m_refCount++; + } + + this->m_childModel = source.m_childModel; + + if (this->m_childModel) { + this->m_childModel->m_refCount++; + } + + this->m_texture = reinterpret_cast(HandleDuplicate(reinterpret_cast(source.m_texture))); + this->m_flags &= ~0x80; + + if (!copyParticles || (!source.m_freeList.m_count && !source.m_liveList.m_count)) { + return; + } + + this->Sync(); + this->m_freeList.m_count = 0; + this->m_liveList.m_count = 0; + + for (uint32_t i = 0; i < source.m_freeList.m_count; i++) { + *this->m_freeList.New() = source.m_freeList.m_data[i]; + } + + uint32_t liveCount = source.m_liveList.m_count; + + if (this->m_hasModel) { + for (uint32_t i = 0; i < liveCount; i++) { + uint32_t index = source.m_liveList.m_data[i]; + *this->m_liveList.New() = index; + + CParticle2_Model& particle = this->m_particleModels.m_data[index]; + const CParticle2_Model& sourceParticle = source.m_particleModels.m_data[index]; + memcpy(&particle, &sourceParticle, offsetof(CParticle2_Model, m_model)); + particle.m_model = sourceParticle.m_model; + + if (particle.m_model) { + particle.m_model->m_refCount++; + } + } + + return; + } + + for (uint32_t i = 0; i < liveCount; i++) { + uint32_t index = source.m_liveList.m_data[i]; + *this->m_liveList.New() = index; + memcpy(&this->m_particles.m_data[index], &source.m_particles.m_data[index], sizeof(CParticle2)); + } +} + +// VERIFY: 7bb8d416 auto +// OFFSET: 0x00632050 +void CParticleEmitter2::DestroyParticle(CParticle2& particle) { +} + +// VERIFY: acb8380a auto +// OFFSET: 0x009797F0 +CParticle2_Model::CParticle2_Model(const CParticle2_Model& source) { + this->m_age = source.m_age; + this->m_position = source.m_position; + this->m_velocity = source.m_velocity; + this->m_lifeSeed = source.m_lifeSeed; + this->m_randomSeed = source.m_randomSeed; + this->m_orientation = source.m_orientation; + this->m_angularVelocity = source.m_angularVelocity; + this->m_model = source.m_model; + + if (this->m_model) { + this->m_model->m_refCount++; + } +} + +// INLINED +CParticle2_Model::CParticle2_Model() { + this->m_model = nullptr; + this->m_orientation.x = 0.0f; + this->m_orientation.y = 0.0f; + this->m_orientation.z = 0.0f; + this->m_orientation.w = 1.0f; +} + +// INLINED +CParticle2_Model::~CParticle2_Model() { + if (this->m_model) { + this->m_model->Release(); + this->m_model = nullptr; + } +} + +// VERIFY: 378fed4b auto +// OFFSET: 0x007A0C70 +void CSortableParticleQueue::Erase(uint32_t index, uint32_t count) { + if (index >= this->m_count) { + return; + } + + if (index + count > this->m_count) { + count = this->m_count - index; + } + + uint32_t tail = this->m_count - index - count; + + if (tail) { + memmove(this->m_data + sizeof(CSortableParticleRecord) * index, this->m_data + sizeof(CSortableParticleRecord) * (index + count), sizeof(CSortableParticleRecord) * tail); + } + + this->m_count -= count; +} + +// VERIFY: 4f99cb2d manual +// OFFSET: 0x007A0F50 +void CSortableParticleQueue::Push(float depth, CParticle2* particle) { + if (this->m_count + 1 <= this->m_capacity) { + this->m_count++; + } else if (this->m_chunk) { + uint32_t grow = this->m_chunk; + + if (grow <= this->m_count - this->m_capacity + 1) { + grow = this->m_count - this->m_capacity + 1; + } + + if (this->Resize(sizeof(CSortableParticleRecord) * (grow + this->m_capacity), true)) { + this->m_capacity += grow; + this->m_count++; + } + } + + CSortableParticleRecord* data = reinterpret_cast(this->m_data); + uint32_t index = this->m_count - 1; + + if (this->m_count != 1 && index != 1) { + uint32_t parent; + + do { + data = reinterpret_cast(this->m_data); + parent = index >> 1; + + if (depth < data[parent].m_depth) { + break; + } + + data[index] = data[parent]; + index >>= 1; + } while (parent > 1); + } + + data = reinterpret_cast(this->m_data); + data[index].m_depth = depth; + data[index].m_particle = particle; +} + +// VERIFY: 15566d0e auto +// OFFSET: 0x00981310 +CPlaneParticleEmitter::CPlaneParticleEmitter() { + this->m_width = 0.0f; + this->m_height = 0.0f; + this->m_emitterType = 1; + this->m_latitude = 0.0f; + this->m_longitude = 0.0f; +} + +// VERIFY: 7803105c auto +// OFFSET: 0x00981350 +CPlaneParticleEmitter::CPlaneParticleEmitter(const CPlaneParticleEmitter& source, int32_t copyParticles) : CParticleEmitter2(source, copyParticles) { + this->m_width = source.m_width; + this->m_height = source.m_height; + this->m_latitude = source.m_latitude; + this->m_longitude = source.m_longitude; +} + +// VERIFY: f9a887d1 auto +// OFFSET: 0x009813B0 +void CPlaneParticleEmitter::SetWidth(float width) { + this->m_width = width; +} + +// VERIFY: 1eb2e6bb auto +// OFFSET: 0x009813C0 +void CPlaneParticleEmitter::SetHeight(float height) { + this->m_height = height; +} + +// VERIFY: bedfe79d auto +// OFFSET: 0x009813D0 +void CPlaneParticleEmitter::SetLatitude(float latitude) { + this->m_latitude = latitude; +} + +// VERIFY: 7690086b auto +// OFFSET: 0x009813E0 +void CPlaneParticleEmitter::SetLongitude(float longitude) { + this->m_longitude = longitude; +} + +// VERIFY: dfd2839f auto +// OFFSET: 0x009813F0 +CSphereParticleEmitter::CSphereParticleEmitter() { + this->m_minRadius = 0.0f; + this->m_maxRadius = 0.0f; + this->m_emitterType = 2; + this->m_latitude = 0.0f; + this->m_longitude = 0.0f; +} + +// VERIFY: cc657858 auto +// OFFSET: 0x00981430 +CSphereParticleEmitter::CSphereParticleEmitter(const CSphereParticleEmitter& source, int32_t copyParticles) : CParticleEmitter2(source, copyParticles) { + this->m_minRadius = source.m_minRadius; + this->m_maxRadius = source.m_maxRadius; + this->m_latitude = source.m_latitude; + this->m_longitude = source.m_longitude; + this->m_radiusSpan = source.m_radiusSpan; +} + +// VERIFY: a44879a5 auto +// OFFSET: 0x00981490 +void CSphereParticleEmitter::SetWidth(float width) { + this->m_minRadius = width; + this->m_radiusSpan = this->m_maxRadius - width; +} + +// VERIFY: 0f597e92 auto +// OFFSET: 0x009814B0 +void CSphereParticleEmitter::SetHeight(float height) { + this->m_maxRadius = height; + this->m_radiusSpan = height - this->m_minRadius; +} + +// VERIFY: be59e513 auto +// OFFSET: 0x009813E0 +void CSphereParticleEmitter::SetLatitude(float latitude) { + this->m_latitude = latitude; +} + +// VERIFY: 23c64fef auto +// OFFSET: 0x009814D0 +void CSphereParticleEmitter::SetLongitude(float longitude) { + this->m_longitude = longitude; +} + +// VERIFY: f388ef4b auto +// OFFSET: 0x009814E0 +void CSplineParticleEmitter::SetEmissionRate(float rate) { + this->m_ratePerUnit = rate; + this->m_emissionRate = rate * this->m_end; +} + +// VERIFY: 187f9f95 auto +// OFFSET: 0x00981500 +void CSplineParticleEmitter::SetSpline(const C3Vector* points, uint32_t count) { + this->m_spline.SetPoints(points, count); +} + +// VERIFY: e4738c7d manual +// OFFSET: 0x00981510 +CParticleEmitter2* CPlaneParticleEmitter::Clone(int32_t copyParticles) { + void* mem = SMemAlloc(sizeof(CPlaneParticleEmitter), __FILE__, __LINE__, 0); + + return mem ? new (mem) CPlaneParticleEmitter(*this, copyParticles) : nullptr; +} + +// VERIFY: 1e5e8bbd manual +// OFFSET: 0x00981550 +CParticleEmitter2* CSphereParticleEmitter::Clone(int32_t copyParticles) { + void* mem = SMemAlloc(sizeof(CSphereParticleEmitter), __FILE__, __LINE__, 0); + + return mem ? new (mem) CSphereParticleEmitter(*this, copyParticles) : nullptr; +} + +// VERIFY: 0c4d4b72 manual +// OFFSET: 0x00981590 +CPlaneParticleEmitter::~CPlaneParticleEmitter() { +} + +// VERIFY: 5b4809ff manual +// OFFSET: 0x009815C0 +void CPlaneParticleEmitter::CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) { + particle.m_age = CRandom::ufloat_(this->m_random) * time; + particle.m_randomSeed = static_cast(CRandom::uint32_(this->m_random)); + + float heightScale = CRandom::sfloat_(this->m_random); + float widthScale = CRandom::sfloat_(this->m_random); + + particle.m_position.x = widthScale * this->m_width * 0.5f; + particle.m_position.y = 0.5f * (this->m_height * heightScale); + particle.m_position.z = 0.0f; + + float speed = this->CalcVelocity(); + C3Vector velocity; + + if (0.0f == this->m_zsource) { + float latitude = CRandom::sfloat_(this->m_random) * this->m_latitude; + float longitude = CRandom::sfloat_(this->m_random) * this->m_longitude; + float cosLatitude = cos(latitude); + float sinLatitude = sin(latitude); + float cosLongitude = cos(longitude); + float sinLongitude = sin(longitude); + + velocity.x = cosLongitude * sinLatitude * speed; + velocity.y = sinLatitude * sinLongitude * speed; + velocity.z = speed * ((cosLongitude + 1.0f - cosLongitude) * cosLatitude); + } else { + C3Vector direction(particle.m_position.x, particle.m_position.y, particle.m_position.z - this->m_zsource); + float scale = speed / sqrt(direction.y * direction.y + direction.x * direction.x + direction.z * direction.z); + velocity.x = direction.x * scale; + velocity.y = direction.y * scale; + velocity.z = scale * direction.z; + } + + if (this->m_flags & 0x200) { + particle.m_velocity = velocity; + } else { + particle.m_velocity.x = transform.b0 * velocity.y + transform.c0 * velocity.z + transform.a0 * velocity.x; + particle.m_velocity.y = transform.b1 * velocity.y + transform.a1 * velocity.x + transform.c1 * velocity.z; + particle.m_velocity.z = velocity.z * transform.c2 + (velocity.y * transform.b2 + velocity.x * transform.a2); + particle.m_position *= transform; + + if (this->m_flags & 0x40000) { + this->ProjectParticle(particle); + } + } + + if (this->m_flags & 0x800) { + float scale = CRandom::sfloat_(this->m_random) * this->m_variation + 1.0f; + particle.m_velocity.x = this->m_parentVelocity.x * scale + particle.m_velocity.x; + particle.m_velocity.y = this->m_parentVelocity.y * scale + particle.m_velocity.y; + particle.m_velocity.z = scale * this->m_parentVelocity.z + particle.m_velocity.z; + } +} + +// VERIFY: 53c68299 manual +// OFFSET: 0x00981920 +CSphereParticleEmitter::~CSphereParticleEmitter() { +} + +// VERIFY: e86f6118 manual +// OFFSET: 0x00981950 +void CSphereParticleEmitter::CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) { + particle.m_age = CRandom::ufloat_(this->m_random) * time; + particle.m_randomSeed = static_cast(CRandom::uint32_(this->m_random)); + + float radius = CRandom::ufloat_(this->m_random) * this->m_radiusSpan + this->m_minRadius; + float latitude = CRandom::sfloat_(this->m_random) * this->m_latitude; + float longitude = CRandom::sfloat_(this->m_random) * this->m_longitude; + float cosLatitude = cos(latitude); + float sinLatitude = sin(latitude); + float cosLongitude = cos(longitude); + float sinLongitude = sin(longitude); + + C3Vector direction(cosLongitude * cosLatitude, cosLatitude * sinLongitude, sinLatitude); + + particle.m_position.x = direction.x * radius; + particle.m_position.y = direction.y * radius; + particle.m_position.z = radius * sinLatitude; + + C3Vector velocity; + + if (0.0f != this->m_zsource) { + direction = C3Vector(particle.m_position.x, particle.m_position.y, particle.m_position.z - this->m_zsource); + float lengthSq = direction.y * direction.y + direction.x * direction.x + direction.z * direction.z; + + if (lengthSq > 0.00000023841858f) { + float scale = 1.0f / sqrt(lengthSq); + direction.x = direction.x * scale; + direction.y = direction.y * scale; + direction.z = direction.z * scale; + } + + velocity = direction; + } else if (this->m_flags & 0x8000) { + velocity = C3Vector(0.0f, 0.0f, 1.0f); + } else { + velocity = direction; + } + + float speed = this->CalcVelocity(); + velocity.x = velocity.x * speed; + velocity.y = velocity.y * speed; + velocity.z = speed * velocity.z; + + if (this->m_flags & 0x200) { + particle.m_velocity = velocity; + } else { + particle.m_velocity.x = transform.c0 * velocity.z + transform.b0 * velocity.y + transform.a0 * velocity.x; + particle.m_velocity.y = transform.c1 * velocity.z + transform.b1 * velocity.y + transform.a1 * velocity.x; + particle.m_velocity.z = velocity.y * transform.b2 + velocity.z * transform.c2 + velocity.x * transform.a2; + particle.m_position *= transform; + + if (this->m_flags & 0x40000) { + this->ProjectParticle(particle); + } + } + + if (this->m_flags & 0x800) { + float scale = CRandom::sfloat_(this->m_random) * this->m_variation + 1.0f; + particle.m_velocity.x = this->m_parentVelocity.x * scale + particle.m_velocity.x; + particle.m_velocity.y = this->m_parentVelocity.y * scale + particle.m_velocity.y; + particle.m_velocity.z = scale * this->m_parentVelocity.z + particle.m_velocity.z; + } +} + +// VERIFY: ebe4b46d auto +// OFFSET: 0x00981C90 +void CSplineParticleEmitter::SetWidth(float width) { + if (width < 0.0f) { + this->m_start = 0.0f; + } else if (width < 1.0f) { + this->m_start = width; + } else { + this->m_start = 1.0f; + } +} + +// VERIFY: d4f39b10 manual +// OFFSET: 0x00981CD0 +void CSplineParticleEmitter::SetHeight(float height) { + float end = 0.0f; + + if (height >= 0.0f) { + end = height; + + if (height >= 1.0f) { + end = 1.0f; + } + } + + if (fabs(end - this->m_end) >= 0.00000023841858f) { + this->m_end = end; + this->m_emitAtEnd = 1; + this->m_emissionRate = end * this->m_ratePerUnit; + } +} + +// VERIFY: 4d7a6d2b auto +// OFFSET: 0x009813E0 +void CSplineParticleEmitter::SetLatitude(float latitude) { + this->m_latitude = latitude; +} + +// VERIFY: 027aa93b auto +// OFFSET: 0x009814D0 +void CSplineParticleEmitter::SetLongitude(float longitude) { + this->m_longitude = longitude; +} + +// VERIFY: 91250c53 manual +// OFFSET: 0x00981D40 +void CSplineParticleEmitter::CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) { + particle.m_age = CRandom::ufloat_(this->m_random) * time; + particle.m_randomSeed = static_cast(CRandom::uint32_(this->m_random)); + + float t; + + if (this->m_emitAtEnd) { + t = this->m_end; + this->m_emitAtEnd = 0; + } else { + t = (this->m_end - this->m_start) * CRandom::ufloat_(this->m_random) + this->m_start; + } + + this->m_spline.Pos(t, particle.m_position, C3Spline::EVAL_ARCLENGTH); + + C3Vector velocity(0.0f, 0.0f, this->CalcVelocity()); + + if (0.0f == this->m_zsource) { + if (0.0f != this->m_latitude) { + C3Vector up(0.0f, 0.0f, 1.0f); + C3Vector tangent(0.0f, 0.0f, 0.0f); + this->m_spline.Vel(t, tangent, C3Spline::EVAL_ARCLENGTH); + + float scale = 1.0f / sqrt(tangent.z * tangent.z + tangent.y * tangent.y + tangent.x * tangent.x); + tangent.x = tangent.x * scale; + tangent.y = tangent.y * scale; + tangent.z = scale * tangent.z; + + if (fabs(tangent.z + (tangent.x + tangent.y) * 0.0f) > 0.89999998f) { + up.z = 1.0f; + } + + C33Matrix rotation = C33Matrix::Rotation(CRandom::sfloat_(this->m_random) * this->m_latitude, tangent, true); + C3Vector direction = up * rotation; + + if (0.0f != this->m_longitude) { + C3Vector offset(direction.x * this->m_longitude, direction.y * this->m_longitude, this->m_longitude * direction.z); + float distance = CRandom::ufloat_(this->m_random); + particle.m_position.x = offset.x * distance + particle.m_position.x; + particle.m_position.y = offset.y * distance + particle.m_position.y; + particle.m_position.z = distance * offset.z + particle.m_position.z; + } + + velocity = C3Vector(direction.x * velocity.z, direction.y * velocity.z, velocity.z * direction.z); + } + } else { + C3Vector direction(particle.m_position.x, particle.m_position.y, particle.m_position.z - this->m_zsource); + float scale = velocity.z / sqrt(direction.y * direction.y + direction.x * direction.x + direction.z * direction.z); + velocity = C3Vector(direction.x * scale, direction.y * scale, scale * direction.z); + } + + if (this->m_flags & 0x200) { + particle.m_velocity = velocity; + } else { + particle.m_velocity.x = transform.a0 * velocity.x + transform.b0 * velocity.y + transform.c0 * velocity.z; + particle.m_velocity.y = transform.b1 * velocity.y + transform.a1 * velocity.x + transform.c1 * velocity.z; + particle.m_velocity.z = velocity.z * transform.c2 + (velocity.x * transform.a2 + velocity.y * transform.b2); + particle.m_position *= transform; + + if (this->m_flags & 0x40000) { + this->ProjectParticle(particle); + } + } + + if (this->m_flags & 0x800) { + float scale = CRandom::sfloat_(this->m_random) * this->m_variation + 1.0f; + particle.m_velocity.x = this->m_parentVelocity.x * scale + particle.m_velocity.x; + particle.m_velocity.y = this->m_parentVelocity.y * scale + particle.m_velocity.y; + particle.m_velocity.z = scale * this->m_parentVelocity.z + particle.m_velocity.z; + } +} + +// VERIFY: 48680bae manual +// OFFSET: 0x009820F0 +CSplineParticleEmitter::CSplineParticleEmitter() { + this->m_start = 0.0f; + this->m_end = 0.0f; + this->m_latitude = 0.0f; + this->m_emitterType = 3; + this->m_longitude = 0.0f; + this->m_emitAtEnd = 0; + this->m_ratePerUnit = 0.0f; +} + +// VERIFY: e9bf0ccf manual +// OFFSET: 0x00982150 +CSplineParticleEmitter::~CSplineParticleEmitter() { +} + +// VERIFY: fdc47bd6 manual +// OFFSET: 0x009821C0 +CSplineParticleEmitter::CSplineParticleEmitter(const CSplineParticleEmitter& source, int32_t copyParticles) : CParticleEmitter2(source, copyParticles) { + this->m_start = source.m_start; + this->m_end = source.m_end; + this->m_latitude = source.m_latitude; + this->m_longitude = source.m_longitude; + this->m_ratePerUnit = source.m_ratePerUnit; + this->m_emitAtEnd = source.m_emitAtEnd; + this->m_spline = source.m_spline; +} + +// VERIFY: cb65ff4c manual +// OFFSET: 0x00982240 +CParticleEmitter2* CSplineParticleEmitter::Clone(int32_t copyParticles) { + void* mem = SMemAlloc(sizeof(CSplineParticleEmitter), __FILE__, __LINE__, 0); + + return mem ? new (mem) CSplineParticleEmitter(*this, copyParticles) : nullptr; +} diff --git a/Engine/Source/Services/ParticleSystem2.h b/Engine/Source/Services/ParticleSystem2.h index ca857a2..d527ae4 100644 --- a/Engine/Source/Services/ParticleSystem2.h +++ b/Engine/Source/Services/ParticleSystem2.h @@ -4,25 +4,25 @@ #include #include #include "Engine/Source/Gx/Types.h" -#include "Engine/Source/Tempest/C3Vector.h" +#include "Engine/Source/Storm/H/stpl.h" #include "Engine/Source/Tempest/C3Spline_Bezier3.h" +#include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/C44Matrix.h" #include "Engine/Source/Tempest/C4Quaternion.h" #include "Engine/Source/Tempest/CAaBox.h" -#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/CImVector.h" +#include "Engine/Source/Tempest/CMemBlock.h" +#include "Engine/Source/Tempest/CRandom.h" -class CM2Model; -class CParticle2; -class CParticle2_Model; -class CParticleEmitter2; class C2Vector; -class CImVector; +class CM2Model; +class CM2Scene; +class CParticleEmitter2; +class CRange; +class CTexture; class fixed16; template class M2PartTrack; -class CM2Scene; -class CRange; -class CTexture; struct CGxBuf; struct CGxPool; @@ -35,12 +35,15 @@ class CParticle2 { /* 001E */ uint16_t m_randomSeed; }; -class CParticle2_Model { +class CParticle2_Model : public CParticle2 { public: - /* 0000 */ CParticle2 m_base; /* 0020 */ C4Quaternion m_orientation; /* 0030 */ C3Vector m_angularVelocity; /* 003C */ CM2Model* m_model; + + CParticle2_Model(); + CParticle2_Model(const CParticle2_Model& source); + ~CParticle2_Model(); }; class CParticleMat { @@ -49,36 +52,84 @@ class CParticleMat { /* 0004 */ uint32_t m_flags; }; +struct CSortableParticleRecord { + /* 0000 */ float m_depth; + /* 0004 */ CParticle2* m_particle; +}; + +class CSortableParticleQueue : public CMemBlock { + public: + /* 0014 */ uint32_t m_chunk; + /* 0018 */ uint32_t m_capacity; + /* 001C */ uint32_t m_count; + + CSortableParticleQueue(const uint32_t* params); + void Erase(uint32_t index, uint32_t count); + CSortableParticleRecord Pop(); + void Push(float depth, CParticle2* particle); +}; + class CParticleEmitter2 { public: + struct ParticleVertex { + /* 0000 */ uint8_t* m_position; + /* 0004 */ uint8_t* m_normal; + /* 0008 */ uint8_t* m_color; + /* 000C */ uint8_t* m_texCoord; + /* 0010 */ uint32_t m_positionStride; + /* 0014 */ uint32_t m_normalStride; + /* 0018 */ uint32_t m_colorStride; + /* 001C */ uint32_t m_texCoordStride; + /* 0020 */ uint32_t m_count; + }; + + struct SimpleKey { + /* 0000 */ CImVector m_color; + /* 0004 */ int32_t m_deltaR; + /* 0008 */ int32_t m_deltaG; + /* 000C */ int32_t m_deltaB; + /* 0010 */ int32_t m_deltaA; + /* 0014 */ float m_scale; + /* 0018 */ float m_deltaScale; + /* 001C */ int32_t m_headCell; + /* 0020 */ int32_t m_deltaHeadCell; + /* 0024 */ int32_t m_tailCell; + /* 0028 */ int32_t m_deltaTailCell; + + SimpleKey(); + }; + static CGxBuf* s_indexBuf; static CGxPool* s_indexPool; static uint32_t s_initCount; static uint8_t s_noIndexBuffer; + static CSortableParticleQueue s_pq; + static float s_rndTable[128]; + static float s_viewDist; static void Destroy(); static void Init(); - static void RenderParticleVertices(CGxBuf* buf, EGxVertexBufferFormat format, uint32_t vertexCount, uint32_t indexCount); + static void __stdcall RenderIndices(CGxBuf* buf); + static void __stdcall RenderParticleVertices(CGxBuf* buf, EGxVertexBufferFormat format, uint32_t vertexCount, uint32_t indexCount); /* 0004 */ int32_t m_refCount; - /* 0008 */ uint32_t m_emitAccumulator; + /* 0008 */ float m_emitAccumulator; /* 000C */ uint32_t m_textureCellShift; - /* 0010 */ uint32_t m_textureCellWidth; - /* 0014 */ uint32_t m_textureCellHeight; + /* 0010 */ float m_textureCellWidth; + /* 0014 */ float m_textureCellHeight; /* 0018 */ uint32_t m_priorityPlane; /* 001C */ uint32_t m_renderedParticles; /* 0020 */ int32_t m_emitterType; - /* 0024 */ uint32_t m_random; - /* 0028 */ uint32_t m_random2; + /* 0024 */ CRndSeed m_random; /* 002C */ TSGrowableArray m_particles; /* 003C */ TSGrowableArray m_particleModels; /* 004C */ TSGrowableArray m_liveList; /* 005C */ TSGrowableArray m_freeList; /* 006C */ uint32_t m_childEmitterCount; /* 0070 */ CParticleEmitter2* m_childEmitters [4]; - /* 0080 */ uint32_t m_model; - /* 0084 */ uint32_t m_scene; - /* 0088 */ uint32_t m_childModel; + /* 0080 */ CM2Model* m_model; + /* 0084 */ CM2Scene* m_scene; + /* 0088 */ CM2Model* m_childModel; /* 008C */ uint32_t m_vertsPerParticle; /* 0090 */ uint32_t m_indicesPerParticle; /* 0094 */ uint32_t unk_0094; @@ -94,8 +145,8 @@ class CParticleEmitter2 { /* 00BC */ float m_zsource; /* 00C0 */ float m_initialSpin; /* 00C4 */ float m_initialSpinVariation; - /* 00C8 */ float m_spinVariation; - /* 00CC */ float m_spin; + /* 00C8 */ float m_spin; + /* 00CC */ float m_spinVariation; /* 00D0 */ uint32_t m_materialBlend; /* 00D4 */ uint32_t m_materialFlags; /* 00D8 */ const M2PartTrack* m_colorTrack; @@ -105,19 +156,11 @@ class CParticleEmitter2 { /* 00E8 */ float m_scaleVariationY; /* 00EC */ const M2PartTrack* m_headCellTrack; /* 00F0 */ const M2PartTrack* m_tailCellTrack; - /* 00F4 */ uint32_t m_replacementColors; - /* 00F8 */ uint32_t unk_00F8; - /* 00FC */ uint32_t unk_00FC; - /* 0100 */ uint32_t unk_0100; - /* 0104 */ uint32_t unk_0104; - /* 0108 */ uint32_t unk_0108; - /* 010C */ uint32_t unk_010C; - /* 0110 */ uint32_t unk_0110; - /* 0114 */ uint32_t unk_0114; - /* 0118 */ uint32_t m_simpleMidTime; - /* 011C */ uint32_t m_simpleKeys; - /* 0120 */ uint32_t m_textureCols; - /* 0124 */ uint32_t m_textureRows; + /* 00F4 */ C3Vector m_replacementColors [3]; + /* 0118 */ float m_simpleMidTime; + /* 011C */ SimpleKey* m_simpleKeys; + /* 0120 */ uint32_t m_textureRows; + /* 0124 */ uint32_t m_textureCols; /* 0128 */ CTexture* m_texture; /* 012C */ uint32_t unk_012C; /* 0130 */ float m_alphaScale; @@ -142,29 +185,23 @@ class CParticleEmitter2 { /* 017C */ float m_followIntercept; /* 0180 */ float m_followSlope; /* 0184 */ C44Matrix m_xform; - /* 01C4 */ uint32_t m_cameraPos; - /* 01C8 */ uint32_t unk_01C8; - /* 01CC */ uint32_t unk_01CC; + /* 01C4 */ C3Vector m_viewPos; /* 01D0 */ C3Vector m_parentPosition; - /* 01DC */ uint32_t m_ivelTimer; + /* 01DC */ float m_ivelTimer; /* 01E0 */ C3Vector m_parentVelocity; - /* 01EC */ uint32_t m_xformScale; + /* 01EC */ float m_xformScale; /* 01F0 */ uint32_t unk_01F0; - /* 01F4 */ uint32_t m_followOffset; - /* 01F8 */ uint32_t unk_01F8; - /* 01FC */ uint32_t unk_01FC; + /* 01F4 */ C3Vector m_followOffset; /* 0200 */ C3Vector m_burstOffset; - /* 020C */ uint32_t m_billboardAxis; - /* 0210 */ uint32_t unk_0210; - /* 0214 */ uint32_t unk_0214; + /* 020C */ C3Vector m_billboardAxis; /* 0218 */ CAaBox m_worldBounds; /* 0230 */ uint32_t unk_0230; virtual void Sync(); virtual void CreateParticle(CParticle2& particle, float time, const C44Matrix& transform); virtual void CreateParticle(CParticle2_Model& particle, float time, const C44Matrix& transform); - virtual void Unk0C(int32_t a1); - virtual CParticleEmitter2* Clone(int32_t a1) = 0; + virtual void DestroyParticle(CParticle2& particle); + virtual CParticleEmitter2* Clone(int32_t copyParticles) = 0; virtual ~CParticleEmitter2(); virtual void SetWidth(float width) = 0; virtual void SetHeight(float height) = 0; @@ -172,28 +209,60 @@ class CParticleEmitter2 { virtual void SetLongitude(float longitude) = 0; virtual void SetEmissionRate(float rate); + CParticleEmitter2(); + CParticleEmitter2(const CParticleEmitter2& source, int32_t copyParticles); + CParticleEmitter2* AddRef(); + void BuildParticleVertices(ParticleVertex& vertex, uint32_t count, const C44Matrix& transform); + int32_t BuildVertex(const CParticle2& particle, ParticleVertex& vertex); + float CalcVelocity(); void ChangeFrameOfReference(const C44Matrix* matrix); void CreateChildEmittersFromModel(CM2Scene* scene, const char* name); + void DecRef(); void DetermineIfSimple(); + void EmitNewParticles(float deltaTime, const C44Matrix& transform); + void EmitParticle(float time, const C44Matrix& transform); + void FillOutParticleVertex(void* buffer, EGxVertexBufferFormat format, ParticleVertex* vertex); uint32_t GetNumBatches(); uint32_t GetNumParticleModels(); uint32_t GetNumPrimitives(); CM2Model* GetParticleModelInternal(uint32_t& index); void GetReplacementColors(CImVector& color0, CImVector& color1, CImVector& color2); int32_t HasLiveParticles(); + void InternalUpdate(float deltaTime, int32_t child); + void InterpolateAllTracks(const CParticle2& particle, CImVector* color, C2Vector* scale, int32_t* headCell, int32_t* tailCell); + void InterpolateAllTracksSimple(const CParticle2& particle, CImVector* color, C2Vector* scale, int32_t* headCell, int32_t* tailCell); + CImVector InterpolateColorTrack(float t); + int32_t MoveParticle(CParticle2& particle, float deltaTime); + int32_t MoveParticle(CParticle2_Model& particle, float deltaTime); + void PlaceParticleModels(const C44Matrix* frameOfReference); + void ProjectParticle(CParticle2& particle); + void RandomizeSpin(const CParticle2& particle, float* spin, float* spinRate); + void Render(const C44Matrix* frameOfReference, void* buffer, uint32_t first); + int32_t RenderParticle(CParticle2_Model& particle, const C44Matrix* frameOfReference); + void RenderParticles(const C44Matrix* frameOfReference, void* buffer); + void RenderParticlesPrep(const C44Matrix* frameOfReference, const C44Matrix& view); void SetChooseRandomTexture(int32_t choose); void SetFollowParams(float speed1, float scale1, float speed2, float scale2); void SetMaterial(const CParticleMat& material, CTexture* texture); void SetModel(CM2Scene* scene, const char* name); - void SetParticleStyle(int32_t a1, int32_t a2, float tailLength, int32_t a4); + void SetParticleColors(const CImVector& color0, const CImVector& color1, const CImVector& color2); + void SetParticleStyle(int32_t head, int32_t tail, float tailLength, int32_t clampTail); + void SetSimpleAlphaKeys(); + void SetSimpleColorKeys(); + void SetSimpleHeadCellKeys(); + void SetSimpleScaleKeys(); + void SetSimpleTailCellKeys(); + void SetTexture(CTexture* texture); void SetTextureDimensions(uint32_t rows, uint32_t cols); void SetTwinkleScale(const CRange& scale); void SetZsource(float zsource); + void StepUpdate(float deltaTime, int32_t child); + void SyncAllocation(uint32_t count); + void SyncReserve(uint32_t count, uint32_t used, uint32_t spare); void Update(float deltaTime, const C44Matrix& transform, const C3Vector& viewPos, const C44Matrix* frameOfReference); - const CAaBox& WorldBounds() const; - void SetParticleColors(const CImVector& color0, const CImVector& color1, const CImVector& color2); - void SetTexture(CTexture* texture); - void Render(void* frameOfReference, uint8_t* buffer, int32_t first); + int32_t UpdateLiveParticle(float deltaTime, CParticle2* particle, uint32_t index); + void UpdateXform(const C44Matrix& transform, const C3Vector& viewPos, const C44Matrix* frameOfReference); + CAaBox& WorldBounds(); }; class CPlaneParticleEmitter : public CParticleEmitter2 { @@ -204,7 +273,10 @@ class CPlaneParticleEmitter : public CParticleEmitter2 { /* 0240 */ float m_longitude; CPlaneParticleEmitter(); - CParticleEmitter2* Clone(int32_t a1) override; + CPlaneParticleEmitter(const CPlaneParticleEmitter& source, int32_t copyParticles); + void CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) override; + CParticleEmitter2* Clone(int32_t copyParticles) override; + ~CPlaneParticleEmitter() override; void SetWidth(float width) override; void SetHeight(float height) override; void SetLatitude(float latitude) override; @@ -216,11 +288,14 @@ class CSphereParticleEmitter : public CParticleEmitter2 { /* 0234 */ float m_minRadius; /* 0238 */ float m_maxRadius; /* 023C */ float m_radiusSpan; - /* 0240 */ float m_longitude; - /* 0244 */ float unk_0244; + /* 0240 */ float m_latitude; + /* 0244 */ float m_longitude; CSphereParticleEmitter(); - CParticleEmitter2* Clone(int32_t a1) override; + CSphereParticleEmitter(const CSphereParticleEmitter& source, int32_t copyParticles); + void CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) override; + CParticleEmitter2* Clone(int32_t copyParticles) override; + ~CSphereParticleEmitter() override; void SetWidth(float width) override; void SetHeight(float height) override; void SetLatitude(float latitude) override; @@ -230,15 +305,18 @@ class CSphereParticleEmitter : public CParticleEmitter2 { class CSplineParticleEmitter : public CParticleEmitter2 { public: /* 0234 */ float m_ratePerUnit; - /* 0238 */ float m_width; - /* 023C */ float m_splineLength; - /* 0240 */ float unk_0240; - /* 0244 */ float unk_0244; - /* 0248 */ float unk_0248; + /* 0238 */ float m_start; + /* 023C */ float m_end; + /* 0240 */ float m_latitude; + /* 0244 */ float m_longitude; + /* 0248 */ int32_t m_emitAtEnd; /* 024C */ C3Spline_Bezier3 m_spline; CSplineParticleEmitter(); - CParticleEmitter2* Clone(int32_t a1) override; + CSplineParticleEmitter(const CSplineParticleEmitter& source, int32_t copyParticles); + void CreateParticle(CParticle2& particle, float time, const C44Matrix& transform) override; + CParticleEmitter2* Clone(int32_t copyParticles) override; + ~CSplineParticleEmitter() override; void SetWidth(float width) override; void SetHeight(float height) override; void SetLatitude(float latitude) override; diff --git a/Engine/Source/Services/Profile.cpp b/Engine/Source/Services/Profile.cpp index 78d664d..9271a5a 100644 --- a/Engine/Source/Services/Profile.cpp +++ b/Engine/Source/Services/Profile.cpp @@ -1 +1,427 @@ -#include "Profile.h" +#include +#include +#include +#include +#include +#include "Engine/Source/Services/Profile.h" +#include "Engine/Source/BlizzardCore/SFile2-Core.h" +#include "Engine/Source/Storm/H/storm.h" + +// VERIFY: c051543c auto +// OFFSET: 0x004BBCB0 +static void StrTrimTrailingSpace(char* str) { + char* ptr = str + SStrLen(str) - 1; + + if (ptr >= str) { + while (_ismbcspace(*ptr)) { + ptr--; + } + + ptr[1] = '\0'; + } +} + +// VERIFY: 74491526 manual +// OFFSET: 0x004BBD00 +int32_t ProfileInternal::PrfStrToInt(const char* str) { + int32_t result = 0; + + if (*str != '\'') { + return SStrToInt(str); + } + + const char* ptr = str + 1; + + for (int32_t i = 4; i; i--) { + char c = *ptr; + + if (!c || c == '\'') { + break; + } + + result = c | (result << 8); + ptr++; + } + + return result; +} + +// VERIFY: 7f0bd168 auto +// OFFSET: 0x004BBD40 +void ProfileClose(void* profile) { + if (profile) { + delete static_cast(profile); + } +} + +// INLINED +char* ProfileInternal::STRINGBLOCK::Data() { + return reinterpret_cast(this + 1); +} + +// VERIFY: d2c1e4f1 manual +// OFFSET: 0x004BBEA0 +ProfileInternal::STRINGBLOCK* ProfileInternal::STRINGBLOCK::AllocBlock(uint32_t size) { + uint32_t bytes = size <= 4 ? 4 : size; + STRINGBLOCK* block = new (SMemAlloc(bytes + sizeof(STRINGBLOCK), __FILE__, __LINE__, 0)) STRINGBLOCK; + block->m_size = size; + block->m_refs = 0; + block->m_used = 0; + + return block; +} + +// VERIFY: 34843167 manual +// OFFSET: 0x004BC1C0 +char* ProfileInternal::STRINGBLOCK::AllocString(TSList >& list, const char* str, int32_t reuse) { + if (!str) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + if (reuse) { + for (STRINGBLOCK* block = list.Head(); block; block = list.Next(block)) { + if (str >= block->Data() && str < block->Data() + block->m_size) { + block->m_refs++; + return const_cast(str); + } + } + } + + uint32_t length = SStrLen(str) + 1; + STRINGBLOCK* block = list.Head(); + + if (!block || length > block->m_size - block->m_used) { + block = AllocBlock(length <= 0x1000 ? 0x1000 : length); + list.LinkToHead(block); + } + + char* dest = block->Data() + block->m_used; + memcpy(dest, str, length); + block->m_used += length; + block->m_refs++; + + return dest; +} + +// VERIFY: e84487f4 manual +// OFFSET: 0x004BC290 +void ProfileInternal::STRINGBLOCK::FreeString(TSList >& list, char* str) { + if (!str) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + STRINGBLOCK* found = nullptr; + + for (STRINGBLOCK* block = list.Head(); block; block = list.Next(block)) { + if (str >= block->Data() && str < block->Data() + block->m_size) { + found = block; + break; + } + } + + if (found->m_refs-- == 1) { + found->m_used = 0; + list.LinkToHead(found); + } +} + +// VERIFY: 2d27ed1d manual +// OFFSET: [0x004BCF60, 0x004BCFE0] +ProfileInternal::PROFILE::~PROFILE() { + this->m_sections.Clear(); + this->m_strings.Clear(); +} + +// VERIFY: e55d3f97 manual +// OFFSET: 0x004BD150 +void* ProfileCreate() { + return new (SMemAlloc(sizeof(ProfileInternal::PROFILE), __FILE__, __LINE__, 0)) ProfileInternal::PROFILE; +} + +// VERIFY: 4d0d24db manual +// OFFSET: 0x004BD320 +int32_t ProfileGetValue(void* profile, const char* section, const char* key, int32_t* value, uint32_t index) { + if (!section) { + DebugValidate("section"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + if (!key) { + DebugValidate("key"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + if (!value) { + DebugValidate("value"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + *value = 0; + + ProfileInternal::SECTION* sect = static_cast(profile)->m_sections.Ptr(section); + + if (!sect) { + return 0; + } + + ProfileInternal::KEYVALUE* keyValue = sect->m_keys.Ptr(key); + + if (!keyValue || index >= keyValue->m_values.Count()) { + return 0; + } + + char* str = keyValue->m_values[index]; + + if (!str) { + return 0; + } + + *value = ProfileInternal::PrfStrToInt(str); + + return 1; +} + +// VERIFY: 477a2fb3 manual +// OFFSET: 0x004BD3B0 +int32_t ProfileGetValue(void* profile, const char* section, const char* key, char* buffer, uint32_t bufferSize, uint32_t index) { + if (!section) { + DebugValidate("section"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + if (!key) { + DebugValidate("key"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + if (!buffer) { + DebugValidate("value"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + *buffer = '\0'; + + ProfileInternal::SECTION* sect = static_cast(profile)->m_sections.Ptr(section); + + if (!sect) { + return 0; + } + + ProfileInternal::KEYVALUE* keyValue = sect->m_keys.Ptr(key); + + if (!keyValue || index >= keyValue->m_values.Count()) { + return 0; + } + + char* str = keyValue->m_values[index]; + + if (!str) { + return 0; + } + + SStrCopy(buffer, str, bufferSize); + + return 1; +} + +// VERIFY: 9c09dbf0 manual +// OFFSET: 0x004BD440 +char* ProfileInternal::AddValue(PROFILE* profile, const char* section, const char* key, const char* value, int32_t replace, int32_t reuse) { + SECTION* sect = profile->m_sections.Ptr(section); + + if (!sect) { + char* name = STRINGBLOCK::AllocString(profile->m_strings, section, reuse); + sect = profile->m_sections.New(SStrHashHT(name), name, 0, 0); + } + + KEYVALUE* keyValue = sect->m_keys.Ptr(key); + + if (!keyValue) { + char* name = STRINGBLOCK::AllocString(profile->m_strings, key, reuse); + keyValue = sect->m_keys.New(SStrHashHT(name), name, 0, 0); + } + + if (replace) { + for (uint32_t i = keyValue->m_values.Count(); i; i--) { + STRINGBLOCK::FreeString(profile->m_strings, keyValue->m_values[i - 1]); + } + + keyValue->m_values.Clear(); + } + + char** slot = keyValue->m_values.New(); + *slot = STRINGBLOCK::AllocString(profile->m_strings, value, reuse); + + return *slot; +} + +// VERIFY: 6aeb4c03 manual +// OFFSET: 0x004BD580 +void ProfileInternal::TokenizeStringValues(PROFILE* profile, const char* section, const char* key, char* values) { + if (!section || !key || !values || !*values) { + return; + } + + char* ptr = values; + + do { + int32_t quoted = 0; + + if (*ptr == '"') { + quoted = 1; + ptr++; + } + + char* start = ptr; + + if (*ptr) { + while (true) { + if (quoted) { + if (*ptr == '"') { + *ptr++ = '\0'; + + if (*ptr == ',') { + ptr++; + } + + break; + } + } else if (*ptr == ',') { + *ptr++ = '\0'; + break; + } + + if (!*++ptr) { + break; + } + } + } + + AddValue(profile, section, key, start, 0, 1); + } while (*ptr); +} + +// VERIFY: f8f127cf manual +// OFFSET: 0x004BD600 +int32_t ProfileInternal::IReadBuffer(PROFILE* profile, const void* data, uint32_t size) { + STRINGBLOCK* block = STRINGBLOCK::AllocBlock(size + 1); + char* text = block->Data(); + memcpy(text, data, size); + text[size] = '\0'; + block->m_used = block->m_size; + profile->m_strings.LinkToTail(block); + + const char* section = nullptr; + const char* lastSection = nullptr; + char* key = nullptr; + char* value = nullptr; + int32_t state = 0; + + for (char* ptr = text; *ptr; ptr++) { + switch (state) { + case 0: + if (SStrChr("\r\n", *ptr)) { + break; + } + + if (!SStrCmp(ptr, "//", 2)) { + state = 1; + } else if (*ptr == '[') { + section = ptr + 1; + state = 2; + } else { + key = ptr; + state = 4; + } + + break; + + case 1: + case 3: + if (SStrChr("\r\n", *ptr)) { + state = 0; + } + + break; + + case 2: + if (SStrChr("\r\n", *ptr)) { + section = lastSection; + state = 0; + } else if (*ptr == ']') { + *ptr = '\0'; + lastSection = section; + state = 3; + } + + break; + + case 4: + if (SStrChr("\r\n", *ptr)) { + state = 0; + key = nullptr; + } else if (*ptr == '=') { + *ptr = '\0'; + value = ptr + 1; + state = 5; + } + + break; + + case 5: + if (SStrChr("\r\n", *ptr)) { + *ptr = '\0'; + TokenizeStringValues(profile, section, key, value); + state = 0; + } + + break; + } + } + + if (state == 5) { + TokenizeStringValues(profile, section, key, value); + } + + return 1; +} + +// VERIFY: 1b2c8e3e auto +// OFFSET: 0x004BD7C0 +int32_t ProfileInternal::IReadFile(PROFILE* profile, const char* path) { + char name[260]; + SStrCopy(name, path, sizeof(name)); + StrTrimTrailingSpace(name); + + uint32_t bytes = 0; + void* buffer = nullptr; + + if (!SFile::LoadFile(name, &buffer, &bytes, 1, nullptr)) { + return 0; + } + + int32_t result = IReadBuffer(profile, buffer, bytes); + SFile::Unload(buffer); + + return result; +} + +// VERIFY: 23301000 auto +// OFFSET: 0x004BD840 +int32_t ProfileReadFile(void* profile, const char* path) { + if (!path) { + DebugValidate("path"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + return ProfileInternal::IReadFile(static_cast(profile), path); +} diff --git a/Engine/Source/Services/Profile.h b/Engine/Source/Services/Profile.h index 673acbb..d765791 100644 --- a/Engine/Source/Services/Profile.h +++ b/Engine/Source/Services/Profile.h @@ -1,4 +1,50 @@ #ifndef ENGINE_SOURCE_SERVICES_PROFILE_H #define ENGINE_SOURCE_SERVICES_PROFILE_H +#include +#include +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Storm/H/stpl.h" + +namespace ProfileInternal { + struct STRINGBLOCK : public TSLinkedNode { + static STRINGBLOCK* AllocBlock(uint32_t size); + static char* AllocString(TSList >& list, const char* str, int32_t reuse); + static void FreeString(TSList >& list, char* str); + + /* 0008 */ uint32_t m_refs; + /* 000C */ uint32_t m_size; + /* 0010 */ uint32_t m_used; + + char* Data(); + }; + + struct KEYVALUE : public TSHashObject { + /* 0018 */ TSGrowableArray m_values; + }; + + struct SECTION : public TSHashObject { + /* 0018 */ TSHashTable m_keys; + }; + + struct PROFILE : public CHandleObject { + /* 0008 */ TSHashTable m_sections; + /* 0030 */ TSList > m_strings; + + virtual ~PROFILE(); + }; + + char* AddValue(PROFILE* profile, const char* section, const char* key, const char* value, int32_t replace, int32_t reuse); + int32_t IReadBuffer(PROFILE* profile, const void* data, uint32_t size); + int32_t IReadFile(PROFILE* profile, const char* path); + int32_t PrfStrToInt(const char* str); + void TokenizeStringValues(PROFILE* profile, const char* section, const char* key, char* values); +} + +void ProfileClose(void* profile); +void* ProfileCreate(); +int32_t ProfileGetValue(void* profile, const char* section, const char* key, int32_t* value, uint32_t index); +int32_t ProfileGetValue(void* profile, const char* section, const char* key, char* buffer, uint32_t bufferSize, uint32_t index); +int32_t ProfileReadFile(void* profile, const char* path); + #endif diff --git a/Engine/Source/Services/RibbonEmitter.cpp b/Engine/Source/Services/RibbonEmitter.cpp index b242c49..8c66211 100644 --- a/Engine/Source/Services/RibbonEmitter.cpp +++ b/Engine/Source/Services/RibbonEmitter.cpp @@ -1,4 +1,18 @@ -#include "RibbonEmitter.h" +#include +#include +#include +#include "Engine/Source/Services/RibbonEmitter.h" +#include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Gx/CGxDevice/CGxDevice.h" +#include "Engine/Source/Gx/Gx.h" +#include "Engine/Source/Services/Texture.h" +#include "Engine/Source/ShaderEffect/ShaderEffect.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/C44Matrix.h" +#include "Engine/Source/Tempest/C4Vector.h" + +// STATIC: 0x00B2D658 +static int32_t s_ribbonRenderEnable = 1; // INLINED CRibbonMat::CRibbonMat() { @@ -9,3 +23,519 @@ CRibbonMat::CRibbonMat() { this->m_cull = 1; this->m_blendMode = 0; } + +// VERIFY: 68bb48ac manual +// OFFSET: 0x0097F510 +void CRibbonEmitter::ConvertTexSlotToTexCoords() { + uint32_t cols = this->m_cols; + uint32_t row = this->m_texSlot / cols; + float u = (this->m_texSlot % cols) * this->m_texCellU + this->m_texCoords.minY; + this->m_texSlotCoords.minY = u; + float v = row * this->m_texCellV + this->m_texCoords.minX; + this->m_texSlotCoords.minX = v; + this->m_texSlotCoords.maxY = u + this->m_texCellU; + this->m_texSlotCoords.maxX = v + this->m_texCellV; +} + +// VERIFY: 507021d0 manual +// OFFSET: 0x0097F570 +void CRibbonEmitter::SetDataEnabled(int32_t enabled) { + this->m_flags ^= (this->m_flags ^ (4 * enabled)) & 0x4; + + if (!(this->m_flags & 0x4)) { + this->m_flags &= ~0x1; + } +} + +// VERIFY: 2a6d93b6 manual +// OFFSET: 0x0097F5B0 +void CRibbonEmitter::SetUserEnabled(uint32_t enabled) { + this->m_flags ^= (this->m_flags ^ (8 * enabled)) & 0x8; + + if (!(this->m_flags & 0x8)) { + this->m_flags &= ~0x1; + } +} + +// VERIFY: f8d931dc auto +// OFFSET: 0x0097F5F0 +void CRibbonEmitter::SetTexSlot(uint32_t slot) { + if (this->m_texSlot != slot) { + this->m_texSlot = slot; + this->ConvertTexSlotToTexCoords(); + } +} + +// VERIFY: a4ee293a auto +// OFFSET: 0x0097F610 +void CRibbonEmitter::SetAbove(float above) { + this->m_above = above; +} + +// VERIFY: 3fe02f2e auto +// OFFSET: 0x0097F620 +void CRibbonEmitter::SetBelow(float below) { + this->m_below = below; +} + +// VERIFY: ca13231a auto +// OFFSET: 0x0097F630 +void CRibbonEmitter::SetGravity(float gravity) { + this->m_gravity = gravity; +} + +// VERIFY: bfe848c9 auto +// OFFSET: 0x0097F640 +int32_t CRibbonEmitter::IsDead() { + return this->m_readPos == this->m_writePos; +} + +// VERIFY: 6515ce90 manual +// OFFSET: 0x0097F700 +void CRibbonEmitter::InitInterpDeltas() { + float dz = this->m_prevPos.z - this->m_currPos.z; + float dy = this->m_prevPos.y - this->m_currPos.y; + float dx = this->m_prevPos.x - this->m_currPos.x; + float distance = sqrt(dx * dx + (dy * dy + dz * dz)); + + this->m_startBelow = C3Vector(this->m_prevPos.x - this->m_prevUp.x * this->m_below, this->m_prevPos.y - this->m_prevUp.y * this->m_below, this->m_prevPos.z - this->m_below * this->m_prevUp.z); + this->m_endBelow = C3Vector(this->m_currPos.x - this->m_currUp.x * this->m_below, this->m_currPos.y - this->m_currUp.y * this->m_below, this->m_currPos.z - this->m_below * this->m_currUp.z); + this->m_startAbove = C3Vector(this->m_above * this->m_prevUp.x + this->m_prevPos.x, this->m_prevUp.y * this->m_above + this->m_prevPos.y, this->m_above * this->m_prevUp.z + this->m_prevPos.z); + this->m_endAbove = C3Vector(this->m_currUp.x * this->m_above + this->m_currPos.x, this->m_currUp.y * this->m_above + this->m_currPos.y, this->m_above * this->m_currUp.z + this->m_currPos.z); + this->m_startTangent = C3Vector(this->m_prevDir.x * distance, this->m_prevDir.y * distance, this->m_prevDir.z * distance); + this->m_endTangent = C3Vector(this->m_currDir.x * distance, this->m_currDir.y * distance, distance * this->m_currDir.z); +} + +// VERIFY: 946634b1 manual +// OFFSET: 0x0097F900 +void CRibbonEmitter::CloseTextureHandles() { + uint32_t count = this->m_textures.Count(); + + for (uint32_t i = 0; i < count; i++) { + if (this->m_textures[i]) { + HandleClose(reinterpret_cast(this->m_textures[i])); + } + } +} + +// VERIFY: 684a7387 manual +// OFFSET: 0x0097F940 +void CRibbonEmitter::SetPos(const C44Matrix& transform, const C3Vector& position, const C44Matrix* frameOfReference) { + if (!(this->m_flags & 0x4) || !(this->m_flags & 0x8)) { + return; + } + + C44Matrix matrix = transform; + matrix.d0 = position.x + matrix.d0; + matrix.d1 = position.y + matrix.d1; + matrix.d2 = position.z + matrix.d2; + + if (frameOfReference) { + matrix *= frameOfReference->AffineInverse(); + } + + this->m_localPos = position; + + if (this->m_flags & 0x1) { + this->m_prevPos = this->m_currPos; + this->m_prevDir = this->m_currDir; + this->m_prevUp = this->m_currUp; + } else { + this->m_prevDir = C3Vector(matrix.c0, matrix.c1, matrix.c2); + this->m_edgeFraction = 0.0f; + this->m_prevUp = C3Vector(matrix.b0, matrix.b1, matrix.b2); + this->m_prevPos = C3Vector(matrix.d0, matrix.d1, matrix.d2); + this->m_flags |= 0x1; + } + + this->m_currPos = C3Vector(matrix.d0, matrix.d1, matrix.d2); + this->m_currDir = C3Vector(matrix.c0, matrix.c1, matrix.c2); + this->m_currUp = C3Vector(matrix.b0, matrix.b1, matrix.b2); +} + +// VERIFY: 139ccf62 manual +// OFFSET: 0x0097FAD0 +int32_t CRibbonEmitter::ReplaceTexture(uint32_t type, CTexture* texture) { + int32_t replaced = 0; + + for (uint32_t i = 0; i < this->m_textureTypes.Count(); i++) { + if (this->m_textureTypes[i] == type) { + if (this->m_textures[i]) { + HandleClose(reinterpret_cast(this->m_textures[i])); + } + + replaced++; + this->m_textures[i] = reinterpret_cast(HandleDuplicate(reinterpret_cast(texture))); + } + } + + return replaced; +} + +// VERIFY: b358630b auto +// OFFSET: 0x0097FB60 +void CRibbonEmitter::SetColor(float r, float g, float b) { + this->m_color.Set(this->m_color.a * 255.0f, r, g, b); +} + +// VERIFY: f4dee58f manual +// OFFSET: 0x0097FBA0 +void CRibbonEmitter::SetAlpha(float alpha) { + this->m_color.a = static_cast(static_cast(alpha * 255.0f + 0.5f)); +} + +// VERIFY: a976a0e7 manual +// OFFSET: 0x0097FBE0 +void CRibbonEmitter::ChangeFrameOfReference(const C44Matrix* matrix) { + const C44Matrix& m = *matrix; + + this->m_prevPos *= m; + this->m_prevDir = C3Vector(this->m_prevDir.y * m.b0 + this->m_prevDir.z * m.c0 + this->m_prevDir.x * m.a0, m.c1 * this->m_prevDir.z + this->m_prevDir.x * m.a1 + m.b1 * this->m_prevDir.y, this->m_prevDir.y * m.b2 + m.a2 * this->m_prevDir.x + this->m_prevDir.z * m.c2); + this->m_prevUp = C3Vector(this->m_prevUp.y * m.b0 + this->m_prevUp.x * m.a0 + this->m_prevUp.z * m.c0, m.c1 * this->m_prevUp.z + m.b1 * this->m_prevUp.y + this->m_prevUp.x * m.a1, this->m_prevUp.x * m.a2 + this->m_prevUp.z * m.c2 + this->m_prevUp.y * m.b2); + + this->m_currPos *= m; + this->m_currDir = C3Vector(this->m_currDir.z * m.c0 + this->m_currDir.y * m.b0 + this->m_currDir.x * m.a0, this->m_currDir.x * m.a1 + m.c1 * this->m_currDir.z + m.b1 * this->m_currDir.y, this->m_currDir.z * m.c2 + this->m_currDir.y * m.b2 + this->m_currDir.x * m.a2); + this->m_currUp = C3Vector(this->m_currUp.x * m.a0 + this->m_currUp.y * m.b0 + this->m_currUp.z * m.c0, this->m_currUp.x * m.a1 + this->m_currUp.z * m.c1 + this->m_currUp.y * m.b1, this->m_currUp.z * m.c2 + this->m_currUp.y * m.b2 + m.a2 * this->m_currUp.x); + + for (uint32_t i = 0; i < this->m_vertices.Count(); i++) { + this->m_vertices.m_data[i].p *= m; + } +} + +// VERIFY: b79ec970 auto +// OFFSET: 0x0097FE40 +uint32_t CRibbonEmitter::GetNumBatches() { + return this->m_materials.Count(); +} + +// VERIFY: a63fdf92 manual +// OFFSET: 0x0097FE50 +uint32_t CRibbonEmitter::GetNumPrimitives() { + if (this->m_readPos >= this->m_writePos) { + return 2 * (this->m_writePos + this->m_edgeAges.Count() - this->m_readPos); + } + + return 2 * (this->m_writePos - this->m_readPos); +} + +// VERIFY: bcdbf864 manual +// OFFSET: 0x0097FEF0 +void CRibbonEmitter::InterpEdge(float age, float t, uint32_t advance) { + CGxVertexPCT* vertex = &this->m_vertices.m_data[2 * this->m_writePos]; + float s = 1.0f - t; + + vertex[0].p.x = (this->m_endBelow.x - s * this->m_endTangent.x) * t + (this->m_startTangent.x * t + this->m_startBelow.x) * s; + vertex[0].p.y = (this->m_endBelow.y - this->m_endTangent.y * s) * t + (this->m_startBelow.y + this->m_startTangent.y * t) * s; + vertex[0].p.z = (this->m_endBelow.z - s * this->m_endTangent.z) * t + (this->m_startBelow.z + this->m_startTangent.z * t) * s; + + vertex[1].p.x = (this->m_endAbove.x - this->m_endTangent.x * s) * t + (t * this->m_startTangent.x + this->m_startAbove.x) * s; + vertex[1].p.y = (this->m_endAbove.y - this->m_endTangent.y * s) * t + (this->m_startTangent.y * t + this->m_startAbove.y) * s; + vertex[1].p.z = s * (t * this->m_startTangent.z + this->m_startAbove.z) + (this->m_endAbove.z - s * this->m_endTangent.z) * t; + + this->m_edgeAges.m_data[this->m_writePos] = age; + this->m_writePos += advance; + + if (this->m_writePos >= this->m_edgeAges.Count()) { + this->m_writePos -= this->m_edgeAges.Count(); + } +} + +// VERIFY: b2cbdd07 manual +// OFFSET: 0x00980090 +void CRibbonEmitter::Update(float deltaTime, int32_t suppressNewEdges) { + float dt = deltaTime; + float start = 1.0f; + + if (!(this->m_flags & 0x10) && this->m_edgesPerSec > 0.0f) { + dt = 1.0f / this->m_edgesPerSec + 0.000099999997f; + } + + if (dt < 0.0f) { + dt = 0.0f; + } else if (this->m_edgeLifetime <= dt) { + dt = this->m_edgeLifetime; + } + + while (this->m_readPos != this->m_writePos) { + if (this->m_edgeAges.m_data[this->m_readPos] + dt <= this->m_edgeLifetime) { + break; + } + + if (++this->m_readPos >= this->m_edgeAges.Count()) { + this->m_readPos -= this->m_edgeAges.Count(); + } + } + + if (!suppressNewEdges && (this->m_flags & 0x4) && (this->m_flags & 0x8) && (this->m_flags & 0x1)) { + float target = dt * this->m_edgesPerSec + this->m_edgeFraction; + CImVector color = this->m_color; + + if (g_theGxDevicePtr->Caps().m_colorFormat == GxCF_rgba) { + color.b = this->m_color.r; + color.g = this->m_color.g; + color.r = this->m_color.b; + color.a = this->m_color.a; + } + + this->InitInterpDeltas(); + + if (target >= 1.0f) { + float scale = 1.0f / (target - this->m_edgeFraction); + int64_t steps = static_cast(floor(target - 1.0f)); + + for (int32_t count = static_cast(steps) + 1; count; count--) { + float t = (start - this->m_edgeFraction) * scale; + this->m_vertices.m_data[2 * this->m_writePos].c = color; + this->m_vertices.m_data[2 * this->m_writePos + 1].c = color; + this->InterpEdge(-(t * dt), t, 1); + start = start + 1.0f; + } + } + + this->m_edgeFraction = target - floor(target); + this->InterpEdge(0.0f, 1.0f, 0); + + CGxVertexPCT* vertex = &this->m_vertices.m_data[2 * this->m_writePos]; + vertex[0].tc.x = this->m_texSlotCoords.minY; + vertex[0].tc.y = this->m_texSlotCoords.minX; + vertex[1].tc.x = this->m_texSlotCoords.minY; + vertex[1].tc.y = this->m_texSlotCoords.maxX; + vertex[0].c = color; + vertex[1].c = color; + } + + this->m_worldBounds.b.x = 3.4028235e38f; + this->m_worldBounds.b.y = 3.4028235e38f; + this->m_worldBounds.b.z = 3.4028235e38f; + this->m_worldBounds.t.x = -3.4028235e38f; + this->m_worldBounds.t.y = -3.4028235e38f; + this->m_worldBounds.t.z = -3.4028235e38f; + + for (uint32_t i = this->m_readPos; i != this->m_writePos;) { + CGxVertexPCT* vertex = &this->m_vertices.m_data[2 * i]; + float drop = (this->m_edgeAges.m_data[i] * 2.0f + dt) * this->m_gravity * dt; + vertex[0].p.z = vertex[0].p.z + drop; + vertex[1].p.z = drop + vertex[1].p.z; + + for (uint32_t j = 0; j < 2; j++) { + const C3Vector& p = vertex[j].p; + + if (this->m_worldBounds.b.x > p.x) { + this->m_worldBounds.b.x = p.x; + } + + if (this->m_worldBounds.b.y > p.y) { + this->m_worldBounds.b.y = p.y; + } + + if (this->m_worldBounds.b.z > p.z) { + this->m_worldBounds.b.z = p.z; + } + + if (this->m_worldBounds.t.x < p.x) { + this->m_worldBounds.t.x = p.x; + } + + if (this->m_worldBounds.t.y < p.y) { + this->m_worldBounds.t.y = p.y; + } + + if (this->m_worldBounds.t.z < p.z) { + this->m_worldBounds.t.z = p.z; + } + } + + this->m_edgeAges.m_data[i] = dt + this->m_edgeAges.m_data[i]; + + float u = this->m_edgeAges.m_data[i] * this->m_texCellU * this->m_invEdgeLifetime + this->m_texSlotCoords.minY; + vertex[0].tc.x = u; + vertex[0].tc.y = this->m_texSlotCoords.minX; + vertex[1].tc.x = u; + vertex[1].tc.y = this->m_texSlotCoords.maxX; + + if (++i >= this->m_edgeAges.Count()) { + i -= this->m_edgeAges.Count(); + } + } + + this->m_flags = (this->m_flags & ~0x30) | 0x10; +} + +// VERIFY: 05a5de95 manual +// OFFSET: 0x00980510 +static void DuplicateTextures(const TSGrowableArray& source, TSGrowableArray& dest) { + uint32_t count = source.m_count; + dest.SetCount(count); + + for (uint32_t i = 0; i < count; i++) { + dest.m_data[i] = reinterpret_cast(HandleDuplicate(reinterpret_cast(source.m_data[i]))); + } +} + +// VERIFY: 6449e9b3 manual +// OFFSET: 0x00980590 +CRibbonEmitter::~CRibbonEmitter() { + this->CloseTextureHandles(); + this->m_flags &= ~0x2; +} + +// VERIFY: 459f4678 auto +// OFFSET: 0x00980630 +CRibbonEmitter::CRibbonEmitter() { + this->m_refCount = 1; + this->m_texSlotCoords.minX = 0.0f; + this->m_texSlotCoords.minY = 0.0f; + this->m_texSlotCoords.maxX = 0.0f; + this->m_texSlotCoords.maxY = 0.0f; + this->m_worldBounds.b.x = 3.4028235e38f; + this->m_worldBounds.b.y = 3.4028235e38f; + this->m_worldBounds.b.z = 3.4028235e38f; + this->m_worldBounds.t.x = -3.4028235e38f; + this->m_worldBounds.t.y = -3.4028235e38f; + this->m_worldBounds.t.z = -3.4028235e38f; + this->m_texCoords.minX = 0.0f; + this->m_texCoords.minY = 0.0f; + this->m_texCoords.maxX = 0.0f; + this->m_texCoords.maxY = 0.0f; + this->m_flags &= ~0x12; +} + +// VERIFY: 85ed808e manual +// OFFSET: 0x009808A0 +void CRibbonEmitter::Initialize(float edgesPerSecond, float edgeLifetime, const CImVector& color, const TSGrowableArray& textures, const TSGrowableArray& materials, const TSGrowableArray& textureTypes, const CAaRect& texCoords, uint32_t rows, uint32_t cols) { + float edgesPerSec = ceil(edgesPerSecond); + + if (edgeLifetime < 0.25f) { + edgeLifetime = 0.25f; + } + + uint32_t edgeCount = static_cast(ceil(edgesPerSec * edgeLifetime) + 2.0f); + this->m_edgeAges.SetCount(edgeCount); + + this->m_flags &= ~0x1; + this->m_edgeFraction = 0.0f; + this->m_readPos = 0; + this->m_writePos = 0; + + this->m_vertices.SetCount(2 * edgeCount); + this->m_indices.SetCount(4 * edgeCount); + + for (uint32_t i = 0; i < this->m_indices.Count(); i++) { + this->m_indices.m_data[i] = i % (2 * edgeCount); + } + + this->m_invEdgeLifetime = 1.0f / edgeLifetime; + this->m_texCellU = (texCoords.maxY - texCoords.minY) / cols; + this->m_texCellV = (texCoords.maxX - texCoords.minX) / rows; + this->m_invTexCellU = 1.0f / this->m_texCellU; + this->m_invTexCellV = 1.0f / this->m_texCellV; + this->m_edgesPerSec = edgesPerSec; + this->m_edgeLifetime = edgeLifetime; + this->m_color = color; + this->m_materials = materials; + DuplicateTextures(textures, this->m_textures); + this->m_textureTypes = textureTypes; + this->m_texCoords = texCoords; + this->m_rows = rows; + this->m_cols = cols; + this->m_texSlot = 0; + this->ConvertTexSlotToTexCoords(); + this->m_flags |= 0xE; + this->m_above = 10.0f; + this->m_below = 10.0f; + this->m_gravity = 0.0f; +} + +// VERIFY: 6008d884 auto +// OFFSET: 0x00980B50 +void CRibbonEmitter::DecRef() { + if (this->m_refCount-- == 1) { + delete this; + } +} + +// VERIFY: b28c0986 manual +// OFFSET: 0x00980B70 +int32_t CRibbonEmitter::Render(const C44Matrix* frameOfReference) { + if (!s_ribbonRenderEnable || this->m_readPos == this->m_writePos) { + return 0; + } + + C44Matrix world; + + if (frameOfReference) { + world = *frameOfReference; + } + + world.d0 = world.d0 - this->m_localPos.x; + world.d1 = world.d1 - this->m_localPos.y; + world.d2 = world.d2 - this->m_localPos.z; + g_theGxDevicePtr->XformPushAndSet(GxXform_World, world); + + CGxBuf* vertexBuf = g_theGxDevicePtr->BufStream(GxPoolTarget_Vertex, sizeof(CGxVertexPCT), this->m_vertices.Count()); + CGxBuf* indexBuf = g_theGxDevicePtr->BufStream(GxPoolTarget_Index, sizeof(uint16_t), this->m_indices.Count()); + GxBufData(vertexBuf, this->m_vertices.m_data, sizeof(CGxVertexPCT) * this->m_vertices.Count(), 0); + GxPrimVertexPtr(vertexBuf, GxVBF_PCT); + + uint32_t indexCount; + + if (this->m_readPos >= this->m_writePos) { + indexCount = 2 * (this->m_writePos + this->m_edgeAges.Count() - this->m_readPos) + 2; + } else { + indexCount = 2 * (this->m_writePos - this->m_readPos) + 2; + } + + GxBufData(indexBuf, &this->m_indices.m_data[2 * this->m_readPos], sizeof(uint16_t) * indexCount, 0); + g_theGxDevicePtr->PrimIndexPtr(indexBuf); + + CGxBatch batch; + batch.m_primType = GxPrim_TriangleStrip; + batch.m_start = 0; + batch.m_count = indexCount; + batch.m_minIndex = 0; + batch.m_maxIndex = this->m_vertices.Count() - 1; + + for (uint32_t i = 0; i < this->m_materials.Count(); i++) { + g_theGxDevicePtr->RsPush(); + + const CRibbonMat& material = this->m_materials.m_data[i]; + C4Vector emissive; + + if (material.m_lit) { + emissive.x = 0.0f; + emissive.y = 0.0f; + emissive.z = 0.0f; + emissive.w = 0.0f; + } else { + emissive.x = 1.0f; + emissive.y = 1.0f; + emissive.z = 1.0f; + emissive.w = 0.0f; + } + + CShaderEffect::SetEmissive(emissive); + CShaderEffect::SetFogEnabled(material.m_fogged); + CShaderEffect::SetLightingEnabled(material.m_lit); + g_theGxDevicePtr->RsSet(GxRs_DepthTest, static_cast(material.m_depthTest)); + g_theGxDevicePtr->RsSet(GxRs_DepthWrite, static_cast(material.m_depthWrite)); + g_theGxDevicePtr->RsSet(GxRs_Culling, static_cast(material.m_cull)); + g_theGxDevicePtr->RsSet(GxRs_BlendingMode, static_cast(material.m_blendMode)); + CShaderEffect::SetAlphaRefDefault(); + + CGxTex* gxTex = TextureGetGxTex(this->m_textures.m_data[i], 0, nullptr); + + if (gxTex) { + g_theGxDevicePtr->RsSet(GxRs_Texture0, gxTex); + GxTexSetWrap(gxTex, GxTex_Clamp, GxTex_Clamp); + CShaderEffect::SetDefaultShaders(0); + CShaderEffect::UpdateWorldViewMatrix(); + g_theGxDevicePtr->Draw(&batch, 1); + } + + g_theGxDevicePtr->RsPop(); + } + + GxXformPop(GxXform_World); + + return 1; +} diff --git a/Engine/Source/Services/RibbonEmitter.h b/Engine/Source/Services/RibbonEmitter.h index 5f3f2cb..6d5ff7a 100644 --- a/Engine/Source/Services/RibbonEmitter.h +++ b/Engine/Source/Services/RibbonEmitter.h @@ -3,14 +3,14 @@ #include #include +#include "Engine/Source/Gx/CGxVertexPCT.h" #include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C3Vector.h" #include "Engine/Source/Tempest/CAaBox.h" +#include "Engine/Source/Tempest/CAaRect.h" +#include "Engine/Source/Tempest/CImVector.h" -class C3Vector; -class CAaRect; -class CImVector; class C44Matrix; -class CRibbonEmitter; class CTexture; class CRibbonMat { @@ -27,25 +27,68 @@ class CRibbonMat { class CRibbonEmitter { public: - /* 0000 */ uint8_t unk_0000 [0xF4]; + /* 0000 */ int32_t m_refCount; + /* 0004 */ TSGrowableArray m_edgeAges; + /* 0014 */ uint32_t m_writePos; + /* 0018 */ uint32_t m_readPos; + /* 001C */ float m_edgeFraction; + /* 0020 */ C3Vector m_prevPos; + /* 002C */ C3Vector m_localPos; + /* 0038 */ TSGrowableArray m_vertices; + /* 0048 */ TSGrowableArray m_indices; + /* 0058 */ float m_invEdgeLifetime; + /* 005C */ float m_texCellU; + /* 0060 */ float m_texCellV; + /* 0064 */ float m_invTexCellU; + /* 0068 */ float m_invTexCellV; + /* 006C */ CAaRect m_texSlotCoords; + /* 007C */ C3Vector m_prevUp; + /* 0088 */ C3Vector m_currUp; + /* 0094 */ C3Vector m_prevDir; + /* 00A0 */ C3Vector m_currDir; + /* 00AC */ C3Vector m_startTangent; + /* 00B8 */ C3Vector m_endTangent; + /* 00C4 */ C3Vector m_startBelow; + /* 00D0 */ C3Vector m_endBelow; + /* 00DC */ C3Vector m_startAbove; + /* 00E8 */ C3Vector m_endAbove; /* 00F4 */ CAaBox m_worldBounds; - /* 010C */ uint8_t unk_010C [0x74]; + /* 010C */ float m_edgesPerSec; + /* 0110 */ float m_edgeLifetime; + /* 0114 */ TSGrowableArray m_materials; + /* 0124 */ TSGrowableArray m_textures; + /* 0134 */ TSGrowableArray m_textureTypes; + /* 0144 */ CImVector m_color; + /* 0148 */ CAaRect m_texCoords; + /* 0158 */ uint32_t m_rows; + /* 015C */ uint32_t m_cols; + /* 0160 */ uint32_t m_flags; + /* 0164 */ C3Vector m_currPos; + /* 0170 */ uint32_t m_texSlot; + /* 0174 */ float m_above; + /* 0178 */ float m_below; + /* 017C */ float m_gravity; CRibbonEmitter(); ~CRibbonEmitter(); - void Initialize(float edgesPerSecond, float edgeLifetime, const CImVector& color, const TSGrowableArray& textures, const TSGrowableArray& materials, const TSGrowableArray& textureTypes, const CAaRect& texCoords, uint32_t rows, uint32_t cols); - void SetGravity(float gravity); void ChangeFrameOfReference(const C44Matrix* matrix); + void CloseTextureHandles(); + void ConvertTexSlotToTexCoords(); + void DecRef(); uint32_t GetNumBatches(); uint32_t GetNumPrimitives(); + void InitInterpDeltas(); + void Initialize(float edgesPerSecond, float edgeLifetime, const CImVector& color, const TSGrowableArray& textures, const TSGrowableArray& materials, const TSGrowableArray& textureTypes, const CAaRect& texCoords, uint32_t rows, uint32_t cols); + void InterpEdge(float age, float t, uint32_t advance); int32_t IsDead(); - void Render(void* frameOfReference); - void ReplaceTexture(uint32_t type, CTexture* texture); + int32_t Render(const C44Matrix* frameOfReference); + int32_t ReplaceTexture(uint32_t type, CTexture* texture); void SetAbove(float above); void SetAlpha(float alpha); void SetBelow(float below); void SetColor(float r, float g, float b); void SetDataEnabled(int32_t enabled); + void SetGravity(float gravity); void SetPos(const C44Matrix& transform, const C3Vector& position, const C44Matrix* frameOfReference); void SetTexSlot(uint32_t slot); void SetUserEnabled(uint32_t enabled); diff --git a/Engine/Source/Services/TextBlock.cpp b/Engine/Source/Services/TextBlock.cpp index 1811338..1bae1a1 100644 --- a/Engine/Source/Services/TextBlock.cpp +++ b/Engine/Source/Services/TextBlock.cpp @@ -1 +1,380 @@ -#include "TextBlock.h" +#include +#include +#include +#include "Engine/Source/Services/TextBlock.h" +#include "Engine/Source/Base/Coordinate.h" +#include "Engine/Source/Gxu/GxuFont.h" +#include "Engine/Source/Storm/H/storm.h" +#include "Engine/Source/Tempest/C2Vector.h" +#include "Engine/Source/Tempest/CImVector.h" + +// STATIC: 0x00B4A2A8 +static TSHashTable s_fontHash; + +// INLINED +TEXTBLOCK::TEXTBLOCK() { + this->m_string = nullptr; +} + +// INLINED +FONTHASHOBJ::FONTHASHOBJ() { + this->m_font = nullptr; +} + +// VERIFY: 411a7a2f auto +// OFFSET: 0x004BD870 +static uint32_t ConvertStringFlags(uint32_t flags) { + uint32_t result = (flags & 0x100) != 0; + + if (flags & 0x200) { + result |= 0x4; + } + + if (flags & 0x400) { + result |= 0x8; + } + + if (flags & 0x800) { + result |= 0x10; + } + + if (flags & 0x40) { + result |= 0x2; + } + + if (flags & 0x1000) { + result |= 0x40; + } + + if (flags & 0x2000) { + result |= 0x100; + } + + if (flags & 0x4000) { + result |= 0x200; + } + + if (flags & 0x8000) { + result |= 0x400; + } + + if (flags & 0x80) { + result |= 0x800; + } + + if (flags & 0x10000) { + result |= 0x1000; + } + + if (flags & 0x20000) { + result |= 0x2000; + } + + return result; +} + +// VERIFY: 2b778814 auto +// OFFSET: 0x004BD900 +int32_t TextBlockSetGradient(HTEXTBLOCK string, int32_t start, int32_t length) { + return GxuFontStringSetGradient(reinterpret_cast(string)->m_string, start, length); +} + +// VERIFY: 77b154ef manual +// OFFSET: 0x004BD920 +TEXTBLOCK::~TEXTBLOCK() { + GxuFontDestroyString(this->m_string); +} + +// VERIFY: 1ca29484 auto +// OFFSET: 0x004BD960 +const char* TextBlockGetFontName(HTEXTFONT font) { + if (!font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + return GxuFontGetFontName(reinterpret_cast(font)->m_font); +} + +// VERIFY: 007ea268 auto +// OFFSET: 0x004BD990 +uint32_t TextBlockGetFontFlags(HTEXTFONT font) { + if (!font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + uint32_t fontFlags = GxuFontGetFontFlags(reinterpret_cast(font)->m_font); + uint32_t flags = (fontFlags & 0x1) != 0; + + if (fontFlags & 0x8) { + flags |= 0x4; + } + + if (fontFlags & 0x2) { + flags |= 0x2; + } + + return flags; +} + +// VERIFY: 252921ac auto +// OFFSET: 0x004BD9D0 +CGxFont* TextBlockGetFontPtr(HTEXTFONT font) { + if (!font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + return reinterpret_cast(font)->m_font; +} + +// VERIFY: 76dd741a auto +// OFFSET: 0x004BD9F0 +CGxString* TextBlockGetStringPtr(HTEXTBLOCK string) { + if (!string) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + return reinterpret_cast(string)->m_string; +} + +// VERIFY: bc267847 auto +// OFFSET: 0x004BDA10 +void TextBlockAddShadow(HTEXTBLOCK string, CImVector color, const C2Vector& offset) { + if (!string) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + C2Vector ndcOffset = offset; + DDCToNDC(offset.x, offset.y, &ndcOffset.x, &ndcOffset.y); + GxuFontAddShadow(reinterpret_cast(string)->m_string, color, ndcOffset); +} + +// VERIFY: 5d029466 auto +// OFFSET: 0x004BDA70 +void TextBlockRemoveShadow(HTEXTBLOCK string) { + if (!string) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + GxuFontRemoveShadow(reinterpret_cast(string)->m_string); +} + +// VERIFY: e87143c4 manual +// OFFSET: 0x004BDAA0 +HTEXTBLOCK TextBlockCreate(HTEXTFONT font, const char* text, const CImVector& color, const C3Vector& position, float fontHeight, float blockWidth, float blockHeight, uint32_t flags, float charSpacing, float lineSpacing, float scale) { + if (!font || !text) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + void* mem = SMemAlloc(sizeof(TEXTBLOCK), "HTEXTBLOCK", SLOG_OBJECT, 0); + TEXTBLOCK* textBlock = mem ? new (mem) TEXTBLOCK : nullptr; + + C3Vector ndcPosition(0.0f, 0.0f, position.z); + DDCToNDC(position.x, position.y, &ndcPosition.x, &ndcPosition.y); + + EGxFontHJusts horzJust = GxHJ_Center; + + if (flags & 0x4) { + horzJust = GxHJ_Right; + } else if (!(flags & 0x2) && (flags & 0x1)) { + horzJust = GxHJ_Left; + } + + EGxFontVJusts vertJust = GxVJ_Middle; + + if (flags & 0x8) { + vertJust = GxVJ_Top; + } else if (flags & 0x20) { + vertJust = GxVJ_Bottom; + } + + GxuFontCreateString(reinterpret_cast(font)->m_font, text, DDCToNDCHeight(fontHeight), ndcPosition, DDCToNDCWidth(blockWidth), DDCToNDCHeight(blockHeight), DDCToNDCHeight(lineSpacing), textBlock->m_string, vertJust, horzJust, ConvertStringFlags(flags), color, DDCToNDCWidth(charSpacing), scale); + + return reinterpret_cast(HandleCreate(textBlock, "HTEXTBLOCK")); +} + +// VERIFY: 3c68977b auto +// OFFSET: 0x004BDBF0 +void TextBlockSetStringPos(HTEXTBLOCK string, const C3Vector& position) { + if (!string) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + C3Vector ndcPosition(0.0f, 0.0f, position.z); + DDCToNDC(position.x, position.y, &ndcPosition.x, &ndcPosition.y); + GxuFontSetStringPosition(reinterpret_cast(string)->m_string, ndcPosition); +} + +// VERIFY: 7827f7e0 manual +// OFFSET: 0x004BDC50 +C3Vector TextBlockGetStringPos(HTEXTBLOCK string) { + C3Vector ndcPosition = GxuFontGetStringPosition(reinterpret_cast(string)->m_string); + C3Vector position(0.0f, 0.0f, ndcPosition.z); + NDCToDDC(ndcPosition.x, ndcPosition.y, &position.x, &position.y); + + return position; +} + +// VERIFY: 3a46df44 auto +// OFFSET: 0x004BDCA0 +void TextBlockUpdateColor(HTEXTBLOCK string, const CImVector& color) { + if (!string) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + GxuFontSetStringColor(reinterpret_cast(string)->m_string, color); +} + +// VERIFY: 77525dd6 auto +// OFFSET: 0x004BDCD0 +void TextBlockGetTextExtent(HTEXTFONT font, const char* text, uint32_t numBytes, float fontHeight, float* extent, float shadowWidth, float scale, float unused, uint32_t flags) { + if (!font || !text || !extent) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + *extent = 0.0f; + + CGxFont* face = reinterpret_cast(font)->m_font; + + if (!face) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return; + } + + GxuFontGetTextExtent(face, text, numBytes, DDCToNDCHeight(fontHeight), extent, DDCToNDCWidth(shadowWidth), scale, DDCToNDCWidth(unused), ConvertStringFlags(flags)); + NDCToDDC(*extent, 0.0f, extent, nullptr); +} + +// VERIFY: 664108fa manual +// OFFSET: 0x004BDD80 +float TextBlockGetWrappedTextHeight(HTEXTFONT font, const char* text, float fontHeight, float blockWidth, const C2Vector& shadowOffset, float scale, float spacing, uint32_t flags) { + if (!font || !text || !reinterpret_cast(font)->m_font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0.0f; + } + + C2Vector ndcShadow = shadowOffset; + DDCToNDC(ndcShadow.x, ndcShadow.y, &ndcShadow.x, &ndcShadow.y); + + float height = GxuFontGetWrappedTextHeight(reinterpret_cast(font)->m_font, text, DDCToNDCHeight(fontHeight), DDCToNDCWidth(blockWidth), ndcShadow, scale, DDCToNDCHeight(spacing), ConvertStringFlags(flags)); + + return NDCToDDCHeight(height); +} + +// VERIFY: a9437d87 manual +// OFFSET: 0x004BDE40 +uint32_t TextBlockGetMaxCharsWithinWidth(HTEXTFONT font, const char* text, float fontHeight, float maxWidth, uint32_t numBytes, float* extent, float shadowWidth, float scale, float unused, uint32_t flags) { + if (!font || !text || !reinterpret_cast(font)->m_font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + uint32_t numChars = GxuFontGetMaxCharsWithinWidth(reinterpret_cast(font)->m_font, text, DDCToNDCHeight(fontHeight), DDCToNDCWidth(maxWidth), numBytes, extent, DDCToNDCWidth(shadowWidth), scale, DDCToNDCWidth(unused), ConvertStringFlags(flags)); + NDCToDDC(*extent, 0.0f, extent, nullptr); + + return numChars; +} + +// VERIFY: ec57cb90 manual +// OFFSET: 0x004BDEF0 +uint32_t TextBlockGetMaxCharsWithinWidthAndHeight(HTEXTFONT font, const char* text, float fontHeight, float blockWidth, float blockHeight, uint32_t numBytes, float shadowWidth, float scale, float spacing, uint32_t flags) { + if (!font || !text || !reinterpret_cast(font)->m_font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + return GxuFontGetMaxCharsWithinWidthAndHeight(reinterpret_cast(font)->m_font, text, DDCToNDCHeight(fontHeight), DDCToNDCWidth(blockWidth), DDCToNDCHeight(blockHeight), numBytes, DDCToNDCWidth(shadowWidth), scale, DDCToNDCHeight(spacing), ConvertStringFlags(flags)); +} + +// VERIFY: 2166778f manual +// OFFSET: 0x004BDF90 +uint32_t TextBlockGetMaxCharsWithinWidthFromEnd(HTEXTFONT font, const char* text, float fontHeight, float maxWidth, uint32_t numBytes, float* extent, float shadowWidth, float scale, float unused, uint32_t flags) { + if (!font || !text || !reinterpret_cast(font)->m_font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + uint32_t numChars = GxuFontGetMaxCharsWithinWidthFromEnd(reinterpret_cast(font)->m_font, text, DDCToNDCHeight(fontHeight), DDCToNDCWidth(maxWidth), numBytes, extent, DDCToNDCWidth(shadowWidth), scale, DDCToNDCWidth(unused), ConvertStringFlags(flags)); + NDCToDDC(*extent, 0.0f, extent, nullptr); + + return numChars; +} + +// VERIFY: db0dc9c8 auto +// OFFSET: 0x004BE040 +uint32_t TextBlockWrapText(HTEXTFONT font, const char* text, float fontHeight, float blockWidth, uint32_t* lineStarts, uint32_t maxLines, float shadowWidth, float scale, float unused, uint32_t flags) { + if (!font || !text || !reinterpret_cast(font)->m_font) { + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return 0; + } + + return GxuFontWrapText(reinterpret_cast(font)->m_font, text, SStrLen(text), DDCToNDCHeight(fontHeight), DDCToNDCWidth(blockWidth), lineStarts, maxLines, DDCToNDCWidth(shadowWidth), scale, DDCToNDCWidth(unused), ConvertStringFlags(flags)); +} + +// VERIFY: 6a182661 manual +// OFFSET: 0x004BE500 +FONTHASHOBJ::~FONTHASHOBJ() { + if (this->m_font) { + GxuFontDestroyFont(this->m_font); + } +} + +// VERIFY: e00f1e9e manual +// OFFSET: 0x004BE9C0 +HTEXTFONT TextBlockGenerateFont(const char* fontName, uint32_t fontFlags, float fontHeight) { + if (!fontName) { + DebugValidate("fontName"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + if (!*fontName) { + DebugValidate("*fontName"); + SErrSetLastError(STORM_ERROR_INVALID_PARAMETER); + return nullptr; + } + + float height = DDCToNDCHeight(fontHeight); + + if (height >= 1.0f) { + height = 1.0f; + } + + char key[276]; + SStrPrintf(key, sizeof(key), "%s-%d-%f", fontName, fontFlags, height); + + FONTHASHOBJ* hashObj = s_fontHash.Ptr(key); + + if (hashObj) { + return reinterpret_cast(HandleCreate(hashObj, "HTEXTFONT")); + } + + hashObj = s_fontHash.New(SStrHashHT(key), key, 0, 0); + + uint32_t gxFlags = (fontFlags & 0x1) != 0; + + if (fontFlags & 0x4) { + gxFlags |= 0x8; + } + + if (fontFlags & 0x2) { + gxFlags |= 0x2; + } + + if (GxuFontCreateFont(fontName, height, hashObj->m_font, gxFlags)) { + return reinterpret_cast(HandleCreate(hashObj, "HTEXTFONT")); + } + + s_fontHash.Delete(hashObj); + + return nullptr; +} diff --git a/Engine/Source/Services/TextBlock.h b/Engine/Source/Services/TextBlock.h index d210e20..9dad8f1 100644 --- a/Engine/Source/Services/TextBlock.h +++ b/Engine/Source/Services/TextBlock.h @@ -4,12 +4,47 @@ #include #include #include "Engine/Source/Base/Handle.h" +#include "Engine/Source/Services/Types.h" +#include "Engine/Source/Storm/H/stpl.h" +#include "Engine/Source/Tempest/C3Vector.h" -class CGxString; +class C2Vector; +class CImVector; +struct CGxFont; +struct CGxString; class TEXTBLOCK : public CHandleObject { public: /* 0008 */ CGxString* m_string; + + TEXTBLOCK(); + virtual ~TEXTBLOCK(); }; +struct FONTHASHOBJ : public CHandleObject, public TSHashObject { + /* 0020 */ CGxFont* m_font; + + FONTHASHOBJ(); + virtual ~FONTHASHOBJ(); +}; + +void TextBlockAddShadow(HTEXTBLOCK string, CImVector color, const C2Vector& offset); +HTEXTBLOCK TextBlockCreate(HTEXTFONT font, const char* text, const CImVector& color, const C3Vector& position, float fontHeight, float blockWidth, float blockHeight, uint32_t flags, float charSpacing, float lineSpacing, float scale); +HTEXTFONT TextBlockGenerateFont(const char* fontName, uint32_t fontFlags, float fontHeight); +uint32_t TextBlockGetFontFlags(HTEXTFONT font); +const char* TextBlockGetFontName(HTEXTFONT font); +CGxFont* TextBlockGetFontPtr(HTEXTFONT font); +uint32_t TextBlockGetMaxCharsWithinWidth(HTEXTFONT font, const char* text, float fontHeight, float maxWidth, uint32_t numBytes, float* extent, float shadowWidth, float scale, float unused, uint32_t flags); +uint32_t TextBlockGetMaxCharsWithinWidthAndHeight(HTEXTFONT font, const char* text, float fontHeight, float blockWidth, float blockHeight, uint32_t numBytes, float shadowWidth, float scale, float spacing, uint32_t flags); +uint32_t TextBlockGetMaxCharsWithinWidthFromEnd(HTEXTFONT font, const char* text, float fontHeight, float maxWidth, uint32_t numBytes, float* extent, float shadowWidth, float scale, float unused, uint32_t flags); +C3Vector TextBlockGetStringPos(HTEXTBLOCK string); +CGxString* TextBlockGetStringPtr(HTEXTBLOCK string); +void TextBlockGetTextExtent(HTEXTFONT font, const char* text, uint32_t numBytes, float fontHeight, float* extent, float shadowWidth, float scale, float unused, uint32_t flags); +float TextBlockGetWrappedTextHeight(HTEXTFONT font, const char* text, float fontHeight, float blockWidth, const C2Vector& shadowOffset, float scale, float spacing, uint32_t flags); +void TextBlockRemoveShadow(HTEXTBLOCK string); +int32_t TextBlockSetGradient(HTEXTBLOCK string, int32_t start, int32_t length); +void TextBlockSetStringPos(HTEXTBLOCK string, const C3Vector& position); +void TextBlockUpdateColor(HTEXTBLOCK string, const CImVector& color); +uint32_t TextBlockWrapText(HTEXTFONT font, const char* text, float fontHeight, float blockWidth, uint32_t* lineStarts, uint32_t maxLines, float shadowWidth, float scale, float unused, uint32_t flags); + #endif diff --git a/Engine/Source/Services/Types.h b/Engine/Source/Services/Types.h index c688e85..2abd5d1 100644 --- a/Engine/Source/Services/Types.h +++ b/Engine/Source/Services/Types.h @@ -31,6 +31,18 @@ class CAsyncObject { int32_t IsPending(); }; +struct HTEXTBLOCK__ { + /* 0000 */ int32_t m_unused; +}; + +typedef HTEXTBLOCK__* HTEXTBLOCK; + +struct HTEXTFONT__ { + /* 0000 */ int32_t m_unused; +}; + +typedef HTEXTFONT__* HTEXTFONT; + enum PIXEL_FORMAT { PIXEL_DXT1 = 0, PIXEL_DXT3 = 1, diff --git a/_Triage/CAngle.cpp b/_Triage/CAngle.cpp deleted file mode 100644 index 6c22eaf..0000000 --- a/_Triage/CAngle.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "CAngle.h" diff --git a/_Triage/CAngle.h b/_Triage/CAngle.h deleted file mode 100644 index 0ecfd7a..0000000 --- a/_Triage/CAngle.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef _TRIAGE_CANGLE_H -#define _TRIAGE_CANGLE_H - -#include -#include -#include "Engine/Source/Storm/H/stpl.h" - -class CBaseManaged; - -class CBaseManaged { - public: - /* 0004 */ TSLink m_link; - /* 000C */ uint8_t m_dataTypeId; - /* 000D */ uint8_t m_flags; - /* 000E */ uint8_t unk_000E[2]; - /* 0010 */ void (__stdcall* m_updateFcn )(float, void*, void*); - /* 0014 */ void* m_updateData; - /* 0018 */ float m_updatePriority; - - virtual ~CBaseManaged(); -}; - -template -struct TManaged : public CBaseManaged { - /* 001C */ T m_data; -}; - -class CAngle : public TManaged { - public: - /* 0020 */ float m_cos; - /* 0024 */ float m_sin; -}; - -#endif diff --git a/_Triage/Camera.h b/_Triage/Camera.h index 10cbace..58bfbac 100644 --- a/_Triage/Camera.h +++ b/_Triage/Camera.h @@ -8,7 +8,7 @@ #include "Engine/Source/Storm/H/stpl.h" #include "WoW/Source/Object/WGUID.h" #include "WoW/Source/UIUtil/CSimpleCamera.h" -#include "_Triage/CAngle.h" +#include "Engine/Source/Services/CameraInt.h" class C3Vector; class CM2Model; @@ -30,19 +30,6 @@ class CAMERA_SMOOTH { /* 0014 */ float m_param4; }; -class CCamera { - public: - /* 0000 */ TManaged m_position; - /* 0028 */ TManaged m_target; - /* 0050 */ TManaged m_distance; - /* 0070 */ TManaged m_zFar; - /* 0090 */ TManaged m_zNear; - /* 00B0 */ CAngle m_aoa; - /* 00D8 */ CAngle m_fov; - /* 0100 */ CAngle m_roll; - /* 0128 */ CAngle m_rotation; -}; - class CGCamera : public CSimpleCamera { public: /* 0048 */ CM2Model* m_model;