CFluidUVMotion: Return std::array by value from CalculateFluidTextureOffset()

Same behavior, but allows for easier use of API and makes it harder to
misuse. It also makes it easier for analysis to determine out of bounds,
given we leverage std::array rather than a pointer that causes arrays to
decay and lose their size information.
This commit is contained in:
Lioncash 2020-04-12 09:20:22 -04:00
parent 3c8619ba44
commit 14f6dd2cd8
4 changed files with 26 additions and 19 deletions

View File

@ -128,11 +128,11 @@ CFluidPlaneShader::RenderSetupInfo CFluidPlaneCPU::RenderSetup(const CStateManag
const CScriptWater* water) {
CFluidPlaneShader::RenderSetupInfo out;
float uvT = mgr.GetFluidPlaneManager()->GetUVT();
bool hasBumpMap = HasBumpMap() && kEnableWaterBumpMaps;
const float uvT = mgr.GetFluidPlaneManager()->GetUVT();
const bool hasBumpMap = HasBumpMap() && kEnableWaterBumpMaps;
bool doubleLightmapBlend = false;
bool hasEnvMap = mgr.GetCameraManager()->GetFluidCounter() == 0 && HasEnvMap();
bool hasEnvBumpMap = HasEnvBumpMap();
const bool hasEnvMap = mgr.GetCameraManager()->GetFluidCounter() == 0 && HasEnvMap();
const bool hasEnvBumpMap = HasEnvBumpMap();
InitializeSineWave();
CGraphics::SetModelMatrix(xf);
@ -165,8 +165,7 @@ CFluidPlaneShader::RenderSetupInfo CFluidPlaneCPU::RenderSetup(const CStateManag
curTex++;
}
float fluidUVs[3][2];
x4c_uvMotion.CalculateFluidTextureOffset(uvT, fluidUVs);
const auto fluidUVs = x4c_uvMotion.CalculateFluidTextureOffset(uvT);
out.texMtxs[0][0][0] = x4c_uvMotion.GetFluidLayers()[1].GetUVScale();
out.texMtxs[0][1][1] = x4c_uvMotion.GetFluidLayers()[1].GetUVScale();

View File

@ -18,11 +18,10 @@ CFluidPlaneShader::RenderSetupInfo CFluidPlaneDoor::RenderSetup(const CStateMana
bool noNormals) {
CFluidPlaneShader::RenderSetupInfo out;
float uvT = mgr.GetFluidPlaneManager()->GetUVT();
const float uvT = mgr.GetFluidPlaneManager()->GetUVT();
CGraphics::SetModelMatrix(xf);
float fluidUVs[3][2];
x4c_uvMotion.CalculateFluidTextureOffset(uvT, fluidUVs);
const auto fluidUVs = x4c_uvMotion.CalculateFluidTextureOffset(uvT);
out.texMtxs[0][0][0] = x4c_uvMotion.GetFluidLayers()[1].GetUVScale();
out.texMtxs[0][1][1] = x4c_uvMotion.GetFluidLayers()[1].GetUVScale();

View File

@ -25,15 +25,16 @@ CFluidUVMotion::CFluidUVMotion(float timeToWrap, float orientation)
x0_fluidLayers[2].x8_orientation = 0.78539819f;
}
void CFluidUVMotion::CalculateFluidTextureOffset(float t, float offsets[3][2]) const {
float totalYOffset = t * x4c_ooTimeToWrap * std::cos(x50_orientation);
float totalXOffset = t * x4c_ooTimeToWrap * std::sin(x50_orientation);
CFluidUVMotion::FluidOffsets CFluidUVMotion::CalculateFluidTextureOffset(float t) const {
FluidOffsets offsets;
const float totalYOffset = t * x4c_ooTimeToWrap * std::cos(x50_orientation);
const float totalXOffset = t * x4c_ooTimeToWrap * std::sin(x50_orientation);
for (u32 i = 0; i < x0_fluidLayers.size(); ++i) {
for (size_t i = 0; i < x0_fluidLayers.size(); ++i) {
const SFluidLayerMotion& layer = x0_fluidLayers[i];
float speedT = t * layer.x4_ooTimeToWrap;
float cycleT = speedT - std::floor(speedT);
const float speedT = t * layer.x4_ooTimeToWrap;
const float cycleT = speedT - std::floor(speedT);
float localY;
float localX;
switch (layer.x0_motion) {
@ -42,7 +43,7 @@ void CFluidUVMotion::CalculateFluidTextureOffset(float t, float offsets[3][2]) c
localY = 0.f;
} break;
case EFluidUVMotion::Circular: {
float angle = (M_PIF * 2) * cycleT;
const float angle = (M_PIF * 2) * cycleT;
localY = layer.xc_magnitude * std::sin(angle);
localX = layer.xc_magnitude * std::cos(angle);
} break;
@ -55,11 +56,13 @@ void CFluidUVMotion::CalculateFluidTextureOffset(float t, float offsets[3][2]) c
break;
}
float x = localX * std::sin(layer.x8_orientation) + localY * std::cos(layer.x8_orientation) + totalXOffset;
float y = localY * std::sin(layer.x8_orientation) + localX * std::cos(layer.x8_orientation) + totalYOffset;
const float x = localX * std::sin(layer.x8_orientation) + localY * std::cos(layer.x8_orientation) + totalXOffset;
const float y = localY * std::sin(layer.x8_orientation) + localX * std::cos(layer.x8_orientation) + totalYOffset;
offsets[i][0] = x - std::floor(x);
offsets[i][1] = y - std::floor(y);
}
return offsets;
}
} // namespace urde

View File

@ -1,5 +1,7 @@
#pragma once
#include <array>
#include "Runtime/RetroTypes.hpp"
#include "Runtime/rstl.hpp"
@ -38,6 +40,8 @@ private:
float x50_orientation;
public:
using FluidOffsets = std::array<std::array<float, 2>, 3>;
CFluidUVMotion(float timeToWrap, float orientation, const SFluidLayerMotion& colorLayer,
const SFluidLayerMotion& pattern1Layer, const SFluidLayerMotion& pattern2Layer);
CFluidUVMotion(float timeToWrap, float orientation);
@ -45,6 +49,8 @@ public:
const rstl::reserved_vector<SFluidLayerMotion, 3>& GetFluidLayers() const { return x0_fluidLayers; }
float GetOrientation() const { return x50_orientation; }
float GetOOTimeToWrapTexPage() const { return x4c_ooTimeToWrap; }
void CalculateFluidTextureOffset(float, float[3][2]) const;
// In game binaries this uses an out pointer instead of return by value.
FluidOffsets CalculateFluidTextureOffset(float t) const;
};
} // namespace urde