metaforce/Runtime/Graphics/CModelBoo.cpp

1186 lines
42 KiB
C++
Raw Normal View History

2016-03-04 23:04:53 +00:00
#include "Graphics/CModel.hpp"
2016-03-30 20:44:19 +00:00
#include "Graphics/CTexture.hpp"
2016-03-30 19:16:01 +00:00
#include "Graphics/CGraphics.hpp"
2016-04-04 02:32:57 +00:00
#include "Graphics/CLight.hpp"
2016-03-30 19:16:01 +00:00
#include "hecl/HMDLMeta.hpp"
2016-03-31 02:44:43 +00:00
#include "hecl/Runtime.hpp"
2016-04-04 06:16:03 +00:00
#include "boo/graphicsdev/Metal.hpp"
2016-07-21 05:21:45 +00:00
#include "Shaders/CModelShaders.hpp"
2016-07-31 02:06:47 +00:00
#include "Graphics/CBooRenderer.hpp"
2016-08-21 20:39:18 +00:00
#include "Character/CSkinRules.hpp"
2016-07-31 02:06:47 +00:00
#include "GameGlobalObjects.hpp"
2016-12-23 06:41:39 +00:00
#include "CSimplePool.hpp"
2016-07-31 20:52:04 +00:00
#include <array>
2016-02-13 00:57:09 +00:00
2016-03-04 23:04:53 +00:00
namespace urde
2016-02-13 00:57:09 +00:00
{
2016-09-02 17:50:03 +00:00
static logvisor::Module Log("urde::CBooModel");
2016-03-30 19:16:01 +00:00
bool CBooModel::g_DrawingOccluders = false;
2016-03-29 23:14:14 +00:00
2016-09-09 04:19:19 +00:00
static CBooModel* g_FirstModel = nullptr;
void CBooModel::ClearModelUniformCounters()
{
for (CBooModel* model = g_FirstModel ; model ; model = model->m_next)
model->ClearUniformCounter();
}
zeus::CVector3f CBooModel::g_PlayerPosition = {};
float CBooModel::g_ModSeconds = 0.f;
float CBooModel::g_TransformedTime = 0.f;
float CBooModel::g_TransformedTime2 = 0.f;
void CBooModel::SetNewPlayerPositionAndTime(const zeus::CVector3f& pos)
{
g_PlayerPosition = pos;
KillCachedViewDepState();
u32 modMillis = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count() % u64(100000.f * 4.f * M_PIF / 3.f);
g_ModSeconds = modMillis / 1000.f;
g_TransformedTime = 1.f / -(0.05f * std::sin(g_ModSeconds * 1.5f) - 1.f);
g_TransformedTime2 = 1.f / -(0.015f * std::sin(g_ModSeconds * 1.5f + 1.f) - 1.f);
}
CBooModel* CBooModel::g_LastModelCached = nullptr;
void CBooModel::KillCachedViewDepState()
{
g_LastModelCached = nullptr;
}
2017-03-26 05:53:04 +00:00
zeus::CVector3f CBooModel::g_ReflectViewPos = {};
static const zeus::CMatrix4f ReflectBaseMtx =
{
0.f, 0.f, 0.f, 0.f,
0.f, 0.f, 0.f, 0.f,
0.f, 0.f, 0.f, 1.f,
0.f, 0.f, 0.f, 1.f
};
void CBooModel::EnsureViewDepStateCached(const CBooModel& model, const CBooSurface* surf,
2017-04-15 05:32:25 +00:00
zeus::CMatrix4f* mtxsOut, float& alphaOut)
2017-03-26 05:53:04 +00:00
{
zeus::CVector3f modelToPlayer = g_PlayerPosition - CGraphics::g_GXModelMatrix.origin;
zeus::CVector3f modelToPlayerLocal = CGraphics::g_GXModelMatrix.transposeRotate(modelToPlayer);
zeus::CVector3f surfPos;
float surfSize = 0.f;
if (surf)
{
zeus::CVector3f surfCenter(surf->m_data.centroid);
zeus::CVector3f surfNormal(surf->m_data.reflectionNormal);
float dotDelta = surfNormal.dot(modelToPlayerLocal) - surfCenter.dot(surfNormal);
surfPos = modelToPlayerLocal - surfNormal * dotDelta;
}
else
{
surfPos = model.x20_aabb.center();
surfSize = (model.x20_aabb.max.x - model.x20_aabb.min.x) + (model.x20_aabb.max.y - model.x20_aabb.min.y) * 0.5f;
}
if (g_Renderer->x318_24_refectionDirty)
{
zeus::CVector3f playerToPos = g_ReflectViewPos - g_PlayerPosition;
zeus::CVector3f vecToPos = surfPos - g_PlayerPosition;
if (playerToPos.dot(playerToPos) < vecToPos.dot(vecToPos))
g_ReflectViewPos = surfPos;
}
else
{
g_ReflectViewPos = surfPos;
g_Renderer->x318_24_refectionDirty = true;
}
zeus::CVector3f playerToSurf = surfPos - modelToPlayerLocal;
float distance = std::max(-(0.5f * surfSize - playerToSurf.magnitude()), FLT_EPSILON);
if (distance >= 5.f)
{
alphaOut = 0.f;
}
else
{
alphaOut = (5.f - distance) / 5.f;
/* Indirect map matrix */
mtxsOut[0] = (CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix).toMatrix4f();
/* Reflection map matrix */
zeus::CVector3f v1 = playerToSurf * (1.f / surfSize);
zeus::CVector3f v2 = v1.cross(zeus::CVector3f::skUp);
if (v2.canBeNormalized())
v2.normalize();
else
v2 = zeus::CVector3f::skRight;
float timeScale = 0.32258067f * (0.02f * distance + 1.f);
float f1 = timeScale * g_TransformedTime;
float f2 = timeScale * g_TransformedTime2;
mtxsOut[1] = ReflectBaseMtx;
mtxsOut[1][0][0] = f1 * v2.x;
mtxsOut[1][1][0] = f1 * v2.y;
mtxsOut[1][3][0] = -surfPos.dot(v2) * f1 + 0.5f;
mtxsOut[1][2][1] = f2;
mtxsOut[1][3][1] = -modelToPlayerLocal.z * f2;
}
}
boo::ITexture* CBooModel::g_shadowMap = nullptr;
zeus::CTransform CBooModel::g_shadowTexXf;
2016-09-09 04:19:19 +00:00
CBooModel::~CBooModel()
{
if (m_prev)
m_prev->m_next = m_next;
if (m_next)
m_next->m_prev = m_prev;
2016-09-09 04:23:36 +00:00
if (this == g_FirstModel)
g_FirstModel = m_next;
2016-09-09 04:19:19 +00:00
}
CBooModel::CBooModel(TToken<CModel>& token, std::vector<CBooSurface>* surfaces, SShader& shader,
2016-03-31 02:44:43 +00:00
boo::IVertexFormat* vtxFmt, boo::IGraphicsBufferS* vbo, boo::IGraphicsBufferS* ibo,
const zeus::CAABox& aabb, u8 renderMask,
int numInsts, boo::ITexture* txtrOverrides[8])
: m_model(token), x0_surfaces(surfaces), x4_matSet(&shader.m_matSet), m_matSetIdx(shader.m_matSetIdx),
m_pipelines(&shader.m_shaders), x1c_textures(shader.x0_textures), x20_aabb(aabb),
x40_24_texturesLoaded(false), x40_25_modelVisible(0), x41_mask(renderMask),
m_staticVtxFmt(vtxFmt), m_staticVbo(vbo), m_staticIbo(ibo)
2016-03-31 02:44:43 +00:00
{
if (txtrOverrides)
for (int i=0 ; i<8 ; ++i)
m_txtrOverrides[i] = txtrOverrides[i];
2016-09-09 04:19:19 +00:00
if (!g_FirstModel)
g_FirstModel = this;
else
{
g_FirstModel->m_prev = this;
m_next = g_FirstModel;
g_FirstModel = this;
}
2016-03-31 02:44:43 +00:00
for (CBooSurface& surf : *x0_surfaces)
2016-03-29 23:14:14 +00:00
surf.m_parent = this;
for (auto it=x0_surfaces->rbegin() ; it != x0_surfaces->rend() ; ++it)
{
2016-03-31 02:44:43 +00:00
u32 matId = it->m_data.matIdx;
2016-03-31 06:18:56 +00:00
const MaterialSet::Material& matData = GetMaterialByIndex(matId);
2016-03-31 02:44:43 +00:00
if (matData.flags.depthSorting())
2016-03-29 23:14:14 +00:00
{
it->m_next = x3c_firstSortedSurface;
x3c_firstSortedSurface = &*it;
}
else
{
it->m_next = x38_firstUnsortedSurface;
x38_firstUnsortedSurface = &*it;
}
}
2016-09-12 04:53:28 +00:00
m_instances.reserve(numInsts);
for (int i=0 ; i<numInsts ; ++i)
2016-09-12 04:53:28 +00:00
PushNewModelInstance();
2016-03-29 23:14:14 +00:00
}
boo::IGraphicsBuffer* CBooModel::ModelInstance::GetBooVBO(const CBooModel& model,
boo::IGraphicsDataFactory::Context& ctx)
{
if (model.m_staticVbo)
return model.m_staticVbo;
if (!m_dynamicVbo && model.m_model)
{
const CModel& parent = *model.m_model;
m_dynamicVbo = ctx.newDynamicBuffer(boo::BufferUse::Vertex,
parent.m_hmdlMeta.vertStride, parent.m_hmdlMeta.vertCount);
m_dynamicVbo->load(parent.m_dynamicVertexData.get(),
parent.m_hmdlMeta.vertStride * parent.m_hmdlMeta.vertCount);
}
return m_dynamicVbo;
}
boo::IVertexFormat* CBooModel::ModelInstance::GetBooVtxFmt(const CBooModel& model,
boo::IGraphicsDataFactory::Context& ctx)
{
if (model.m_staticVtxFmt)
return model.m_staticVtxFmt;
if (!m_dynamicVtxFmt && model.m_model)
{
const CModel& parent = *model.m_model;
m_dynamicVtxFmt = hecl::Runtime::HMDLData::NewVertexFormat(ctx, parent.m_hmdlMeta,
GetBooVBO(model, ctx), parent.m_ibo);
}
return m_dynamicVtxFmt;
}
2016-09-09 04:19:19 +00:00
CBooModel::ModelInstance* CBooModel::PushNewModelInstance()
2016-03-29 23:14:14 +00:00
{
2016-09-09 04:19:19 +00:00
if (!x40_24_texturesLoaded)
return nullptr;
if (m_instances.size() >= 256)
Log.report(logvisor::Fatal, "Model buffer overflow");
m_instances.emplace_back();
ModelInstance& newInst = m_instances.back();
size_t skinBankCount = 0;
size_t weightVecCount = 0;
if (const CModel* model = m_model.GetObj())
{
skinBankCount = model->m_hmdlMeta.bankCount;
weightVecCount = model->m_hmdlMeta.weightCount;
}
2016-09-09 04:19:19 +00:00
newInst.m_gfxToken = CGraphics::CommitResources(
[&](boo::IGraphicsDataFactory::Context& ctx) -> bool
2016-03-29 23:14:14 +00:00
{
2016-04-04 02:32:57 +00:00
/* Determine space required by uniform buffer */
std::vector<size_t> skinOffs;
std::vector<size_t> skinSizes;
skinOffs.reserve(std::max(size_t(1), skinBankCount));
skinSizes.reserve(std::max(size_t(1), skinBankCount));
2016-04-04 02:32:57 +00:00
std::vector<size_t> uvOffs;
std::vector<size_t> uvSizes;
uvOffs.reserve(x4_matSet->materials.size());
uvSizes.reserve(x4_matSet->materials.size());
/* Vert transform matrices */
size_t uniBufSize = 0;
if (skinBankCount)
2016-04-04 02:32:57 +00:00
{
/* Skinned */
for (size_t i=0 ; i<skinBankCount ; ++i)
2016-04-04 02:32:57 +00:00
{
size_t thisSz = ROUND_UP_256(sizeof(zeus::CMatrix4f) * (2 * weightVecCount * 4 + 1));
2016-04-04 02:32:57 +00:00
skinOffs.push_back(uniBufSize);
skinSizes.push_back(thisSz);
uniBufSize += thisSz;
}
}
else
{
/* Non-Skinned */
size_t thisSz = ROUND_UP_256(sizeof(zeus::CMatrix4f) * 3);
skinOffs.push_back(uniBufSize);
skinSizes.push_back(thisSz);
uniBufSize += thisSz;
}
/* Animated UV transform matrices */
for (const MaterialSet::Material& mat : x4_matSet->materials)
{
2016-07-31 02:06:47 +00:00
size_t thisSz = ROUND_UP_256(/*mat.uvAnims.size()*/ 8 * (sizeof(zeus::CMatrix4f) * 2));
2016-04-04 02:32:57 +00:00
uvOffs.push_back(uniBufSize);
uvSizes.push_back(thisSz);
uniBufSize += thisSz;
}
/* Lighting uniform */
size_t lightOff = 0;
size_t lightSz = 0;
{
size_t thisSz = ROUND_UP_256(sizeof(CModelShaders::LightingUniform));
lightOff = uniBufSize;
lightSz = thisSz;
uniBufSize += thisSz;
}
2017-03-26 05:53:04 +00:00
/* Surface reflection texmatrix uniform with first identity slot */
size_t reflectOff = uniBufSize;
uniBufSize += 256;
for (const CBooSurface& surf : *x0_surfaces)
{
const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx);
if (mat.flags.samusReflection() || mat.flags.samusReflectionSurfaceEye())
uniBufSize += 256;
}
2016-04-04 02:32:57 +00:00
/* Allocate resident buffer */
m_uniformDataSize = uniBufSize;
2016-09-09 04:19:19 +00:00
newInst.m_uniformBuffer = ctx.newDynamicBuffer(boo::BufferUse::Uniform, uniBufSize, 1);
2016-04-04 02:32:57 +00:00
2017-03-26 05:53:04 +00:00
boo::IGraphicsBuffer* bufs[] = {newInst.m_uniformBuffer,
newInst.m_uniformBuffer,
newInst.m_uniformBuffer,
newInst.m_uniformBuffer};
2016-04-04 02:32:57 +00:00
/* Binding for each surface */
2016-09-09 04:19:19 +00:00
newInst.m_shaderDataBindings.reserve(x0_surfaces->size());
2016-04-04 02:32:57 +00:00
boo::ITexture* texs[8] = {};
2017-03-05 07:57:12 +00:00
boo::ITexture* mbShadowTexs[] = {g_Renderer->m_ballShadowId,
g_Renderer->x220_sphereRamp,
g_Renderer->m_ballFade};
2017-03-26 05:53:04 +00:00
size_t thisOffs[4];
size_t thisSizes[4];
2016-04-04 02:32:57 +00:00
2017-03-26 05:53:04 +00:00
static const boo::PipelineStage stages[4] = {boo::PipelineStage::Vertex,
2016-04-04 06:16:03 +00:00
boo::PipelineStage::Vertex,
2017-03-26 05:53:04 +00:00
boo::PipelineStage::Fragment,
boo::PipelineStage::Vertex};
2016-04-04 06:16:03 +00:00
2016-04-04 02:32:57 +00:00
/* Enumerate surfaces and build data bindings */
2017-03-26 05:53:04 +00:00
size_t curReflect = reflectOff + 256;
2016-04-04 02:32:57 +00:00
for (const CBooSurface& surf : *x0_surfaces)
2016-03-31 02:44:43 +00:00
{
2016-04-04 02:32:57 +00:00
const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx);
u32 texCount = 0;
2016-03-31 02:44:43 +00:00
for (atUint32 idx : mat.textureIdxs)
{
if (boo::ITexture* overtex = m_txtrOverrides[texCount])
{
texs[texCount++] = overtex;
}
else
{
TCachedToken<CTexture>& tex = x1c_textures[idx];
texs[texCount++] = tex.GetObj()->GetBooTexture();
}
2016-03-31 02:44:43 +00:00
}
2016-04-04 02:32:57 +00:00
if (skinBankCount)
2016-04-04 02:32:57 +00:00
{
2016-04-04 05:02:09 +00:00
thisOffs[0] = skinOffs[surf.m_data.skinMtxBankIdx];
thisSizes[0] = skinSizes[surf.m_data.skinMtxBankIdx];
2016-04-04 02:32:57 +00:00
}
else
{
2016-04-04 05:02:09 +00:00
thisOffs[0] = 0;
thisSizes[0] = 256;
2016-04-04 02:32:57 +00:00
}
2016-07-31 02:06:47 +00:00
thisOffs[1] = uvOffs[surf.m_data.matIdx];
thisSizes[1] = uvSizes[surf.m_data.matIdx];
2016-04-04 02:32:57 +00:00
2016-04-04 05:02:09 +00:00
thisOffs[2] = lightOff;
thisSizes[2] = lightSz;
2016-04-04 02:32:57 +00:00
2017-03-26 05:53:04 +00:00
bool useReflection = mat.flags.samusReflection() || mat.flags.samusReflectionSurfaceEye();
if (useReflection)
{
2017-03-26 18:21:33 +00:00
texs[texCount] = g_Renderer->x14c_reflectionTex;
2017-03-26 05:53:04 +00:00
thisOffs[3] = curReflect;
curReflect += 256;
}
else
{
thisOffs[3] = reflectOff;
}
thisSizes[3] = 256;
2016-09-12 04:53:28 +00:00
const std::shared_ptr<hecl::Runtime::ShaderPipelines>& pipelines = m_pipelines->at(surf.m_data.matIdx);
2016-04-04 02:32:57 +00:00
2016-09-09 04:19:19 +00:00
newInst.m_shaderDataBindings.emplace_back();
std::vector<boo::IShaderDataBinding*>& extendeds = newInst.m_shaderDataBindings.back();
2016-09-12 04:53:28 +00:00
extendeds.reserve(pipelines->m_pipelines.size());
2016-04-04 02:32:57 +00:00
2016-07-31 02:06:47 +00:00
int idx = 0;
2016-09-12 04:53:28 +00:00
for (boo::IShaderPipeline* pipeline : pipelines->m_pipelines)
2016-07-31 02:06:47 +00:00
{
2017-03-05 07:57:12 +00:00
size_t texCount;
boo::ITexture** ltexs;
if (idx == EExtendedShader::Thermal)
2017-03-05 07:57:12 +00:00
{
texCount = 8;
texs[7] = g_Renderer->x220_sphereRamp;
ltexs = texs;
2017-03-05 07:57:12 +00:00
}
else if (idx == EExtendedShader::MorphBallShadow)
2017-03-05 07:57:12 +00:00
{
texCount = 3;
ltexs = mbShadowTexs;
}
else if (idx == EExtendedShader::WorldShadow)
{
texCount = 8;
texs[7] = g_shadowMap;
ltexs = texs;
}
2017-03-26 05:53:04 +00:00
else if (useReflection)
{
2017-03-26 18:21:33 +00:00
texCount = mat.textureIdxs.size() + 1;
2017-03-26 05:53:04 +00:00
ltexs = texs;
}
2017-03-05 07:57:12 +00:00
else
{
texCount = mat.textureIdxs.size();
ltexs = texs;
2017-03-05 07:57:12 +00:00
}
2016-04-04 02:32:57 +00:00
extendeds.push_back(
ctx.newShaderDataBinding(pipeline, newInst.GetBooVtxFmt(*this, ctx),
newInst.GetBooVBO(*this, ctx), nullptr, m_staticIbo, 4, bufs,
stages, thisOffs, thisSizes, texCount, ltexs, nullptr, nullptr));
2016-07-31 02:06:47 +00:00
++idx;
}
2016-03-31 02:44:43 +00:00
}
return true;
});
2016-09-09 04:19:19 +00:00
return &newInst;
2016-03-31 02:44:43 +00:00
}
void CBooModel::MakeTexturesFromMats(const MaterialSet& matSet,
std::vector<TCachedToken<CTexture>>& toksOut,
IObjectStore& store)
2016-03-31 02:44:43 +00:00
{
toksOut.reserve(matSet.head.textureIDs.size());
for (const DataSpec::UniqueID32& id : matSet.head.textureIDs)
toksOut.emplace_back(store.GetObj({SBIG('TXTR'), id.toUint32()}));
}
void CBooModel::MakeTexturesFromMats(std::vector<TCachedToken<CTexture>>& toksOut,
IObjectStore& store)
{
MakeTexturesFromMats(*x4_matSet, toksOut, store);
}
2016-04-04 02:32:57 +00:00
void CBooModel::ActivateLights(const std::vector<CLight>& lights)
{
2017-08-08 06:03:57 +00:00
m_lightingData.ActivateLights(lights);
2016-04-04 02:32:57 +00:00
}
2017-05-06 05:21:42 +00:00
void CBooModel::DisableAllLights()
{
m_lightingData.ambient = zeus::CColor::skBlack;
for (size_t curLight = 0 ; curLight<URDE_MAX_LIGHTS ; ++curLight)
{
CModelShaders::Light& lightOut = m_lightingData.lights[curLight];
lightOut.color = zeus::CColor::skClear;
lightOut.linAtt[0] = 1.f;
lightOut.angAtt[0] = 1.f;
}
}
2016-03-31 02:44:43 +00:00
void CBooModel::RemapMaterialData(SShader& shader)
{
x4_matSet = &shader.m_matSet;
m_matSetIdx = shader.m_matSetIdx;
x1c_textures = shader.x0_textures;
2016-03-31 02:44:43 +00:00
m_pipelines = &shader.m_shaders;
x40_24_texturesLoaded = false;
2016-09-09 04:19:19 +00:00
m_instances.clear();
2016-03-29 23:14:14 +00:00
}
2016-03-31 02:44:43 +00:00
bool CBooModel::TryLockTextures() const
2016-03-30 19:16:01 +00:00
{
2016-03-31 02:44:43 +00:00
if (!x40_24_texturesLoaded)
{
bool allLoad = true;
for (TCachedToken<CTexture>& tex : const_cast<std::vector<TCachedToken<CTexture>>&>(x1c_textures))
2016-03-31 02:44:43 +00:00
{
tex.Lock();
if (!tex.IsLoaded())
allLoad = false;
}
2016-08-29 04:22:54 +00:00
const_cast<CBooModel*>(this)->x40_24_texturesLoaded = allLoad;
2016-03-31 02:44:43 +00:00
}
2016-09-09 04:19:19 +00:00
2016-03-31 02:44:43 +00:00
return x40_24_texturesLoaded;
2016-03-30 19:16:01 +00:00
}
void CBooModel::UnlockTextures() const
{
2016-09-09 04:19:19 +00:00
const_cast<CBooModel*>(this)->m_instances.clear();
for (TCachedToken<CTexture>& tex : const_cast<std::vector<TCachedToken<CTexture>>&>(x1c_textures))
2016-03-31 02:44:43 +00:00
tex.Unlock();
2016-08-29 04:22:54 +00:00
const_cast<CBooModel*>(this)->x40_24_texturesLoaded = false;
2016-03-30 19:16:01 +00:00
}
2017-05-06 05:21:42 +00:00
void CBooModel::DrawFlat(ESurfaceSelection sel, EExtendedShader extendedIdx) const
{
const CBooSurface* surf;
CModelFlags flags = {};
flags.m_extendedShader = extendedIdx;
if (sel != ESurfaceSelection::SortedOnly)
{
surf = x38_firstUnsortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
}
if (sel != ESurfaceSelection::UnsortedOnly)
{
surf = x3c_firstSortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
}
}
2016-03-30 19:16:01 +00:00
void CBooModel::DrawAlphaSurfaces(const CModelFlags& flags) const
{
2016-04-01 01:00:37 +00:00
const CBooSurface* surf = x3c_firstSortedSurface;
while (surf)
2016-03-31 02:44:43 +00:00
{
2016-04-01 01:00:37 +00:00
DrawSurface(*surf, flags);
surf = surf->m_next;
2016-03-31 02:44:43 +00:00
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawNormalSurfaces(const CModelFlags& flags) const
{
2016-04-01 01:00:37 +00:00
const CBooSurface* surf = x38_firstUnsortedSurface;
while (surf)
2016-03-31 02:44:43 +00:00
{
2016-04-01 01:00:37 +00:00
DrawSurface(*surf, flags);
surf = surf->m_next;
2016-03-31 02:44:43 +00:00
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawSurfaces(const CModelFlags& flags) const
{
2016-03-31 02:44:43 +00:00
const CBooSurface* surf = x38_firstUnsortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
surf = x3c_firstSortedSurface;
while (surf)
{
DrawSurface(*surf, flags);
surf = surf->m_next;
}
2016-03-30 19:16:01 +00:00
}
void CBooModel::DrawSurface(const CBooSurface& surf, const CModelFlags& flags) const
{
2016-09-09 04:19:19 +00:00
if (m_uniUpdateCount > m_instances.size())
return;
const ModelInstance& inst = m_instances[m_uniUpdateCount-1];
2016-03-31 06:18:56 +00:00
const MaterialSet::Material& data = GetMaterialByIndex(surf.m_data.matIdx);
2016-03-31 02:44:43 +00:00
if (data.flags.shadowOccluderMesh() && !g_DrawingOccluders)
return;
2016-09-09 04:19:19 +00:00
const std::vector<boo::IShaderDataBinding*>& extendeds = inst.m_shaderDataBindings[surf.selfIdx];
2016-04-04 02:32:57 +00:00
boo::IShaderDataBinding* binding = extendeds[0];
2017-03-10 20:52:53 +00:00
if (flags.m_extendedShader < extendeds.size())
binding = extendeds[flags.m_extendedShader];
2016-04-04 02:32:57 +00:00
CGraphics::SetShaderDataBinding(binding);
2016-03-31 02:44:43 +00:00
CGraphics::DrawArrayIndexed(surf.m_data.idxStart, surf.m_data.idxCount);
2016-03-30 19:16:01 +00:00
}
2016-04-04 02:32:57 +00:00
void CBooModel::UVAnimationBuffer::ProcessAnimation(u8*& bufOut, const UVAnimation& anim)
2016-03-31 06:18:56 +00:00
{
2016-07-08 19:57:51 +00:00
zeus::CMatrix4f& texMtxOut = reinterpret_cast<zeus::CMatrix4f&>(*bufOut);
zeus::CMatrix4f& postMtxOut = reinterpret_cast<zeus::CMatrix4f&>(*(bufOut + sizeof(zeus::CMatrix4f)));
2016-07-08 19:57:51 +00:00
texMtxOut = zeus::CMatrix4f();
postMtxOut = zeus::CMatrix4f();
2016-03-31 06:18:56 +00:00
switch (anim.mode)
{
case UVAnimation::Mode::MvInvNoTranslation:
{
2016-07-19 20:12:38 +00:00
texMtxOut = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
2016-07-08 19:57:51 +00:00
texMtxOut.vec[3].zeroOut();
2016-07-19 20:12:38 +00:00
texMtxOut.vec[3].w = 1.f;
2016-07-08 20:25:38 +00:00
postMtxOut.vec[0].x = 0.5f;
2016-07-19 20:12:38 +00:00
postMtxOut.vec[1].y = 0.5f;
2016-07-08 20:25:38 +00:00
postMtxOut.vec[3].x = 0.5f;
postMtxOut.vec[3].y = 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::MvInv:
{
2016-07-19 20:12:38 +00:00
texMtxOut = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
2016-07-08 20:25:38 +00:00
postMtxOut.vec[0].x = 0.5f;
2016-07-19 20:12:38 +00:00
postMtxOut.vec[1].y = 0.5f;
2016-07-08 20:25:38 +00:00
postMtxOut.vec[3].x = 0.5f;
postMtxOut.vec[3].y = 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::Scroll:
{
2016-07-08 19:57:51 +00:00
texMtxOut.vec[3].x = CGraphics::GetSecondsMod900() * anim.vals[2] + anim.vals[0];
texMtxOut.vec[3].y = CGraphics::GetSecondsMod900() * anim.vals[3] + anim.vals[1];
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::Rotation:
{
float angle = CGraphics::GetSecondsMod900() * anim.vals[1] + anim.vals[0];
float acos = std::cos(angle);
float asin = std::sin(angle);
2016-07-08 19:57:51 +00:00
texMtxOut.vec[0].x = acos;
texMtxOut.vec[0].y = asin;
texMtxOut.vec[1].x = -asin;
texMtxOut.vec[1].y = acos;
2016-07-09 22:02:31 +00:00
texMtxOut.vec[3].x = (1.0f - (acos - asin)) * 0.5f;
texMtxOut.vec[3].y = (1.0f - (asin + acos)) * 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::HStrip:
{
2016-07-08 19:57:51 +00:00
float value = anim.vals[0] * anim.vals[2] * (anim.vals[3] + CGraphics::GetSecondsMod900());
2016-07-09 22:02:31 +00:00
texMtxOut.vec[3].x = (float)(short)(float)(anim.vals[1] * fmod(value, 1.0f)) * anim.vals[2];
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::VStrip:
{
2016-07-08 19:57:51 +00:00
float value = anim.vals[0] * anim.vals[2] * (anim.vals[3] + CGraphics::GetSecondsMod900());
2016-07-09 22:02:31 +00:00
texMtxOut.vec[3].y = (float)(short)(float)(anim.vals[1] * fmod(value, 1.0f)) * anim.vals[2];
2016-03-31 06:18:56 +00:00
break;
}
case UVAnimation::Mode::Model:
{
2016-07-08 20:25:38 +00:00
texMtxOut = CGraphics::g_GXModelMatrix.toMatrix4f();
texMtxOut.vec[3].zeroOut();
postMtxOut.vec[0].x = 0.5f;
postMtxOut.vec[2].y = 0.5f;
postMtxOut.vec[3].x = CGraphics::g_GXModelMatrix.origin.x * 0.5f;
postMtxOut.vec[3].y = CGraphics::g_GXModelMatrix.origin.y * 0.5f;
2016-03-31 06:18:56 +00:00
break;
}
2016-09-04 02:29:50 +00:00
case UVAnimation::Mode::CylinderEnvironment:
2016-03-31 06:18:56 +00:00
{
2016-07-08 20:25:38 +00:00
texMtxOut = (CGraphics::g_ViewMatrix.inverse() * CGraphics::g_GXModelMatrix).toMatrix4f();
texMtxOut.vec[3].zeroOut();
const zeus::CVector3f& viewOrigin = CGraphics::g_ViewMatrix.origin;
float xy = (viewOrigin.x + viewOrigin.y) * 0.025f * anim.vals[1];
xy = (xy - (int)xy);
float z = (viewOrigin.z) * 0.05f * anim.vals[1];
z = (z - (int)z);
float halfA = anim.vals[0] * 0.5f;
postMtxOut = zeus::CTransform(zeus::CMatrix3f(halfA, 0.0, 0.0,
2016-09-10 05:39:47 +00:00
0.0, 0.0, halfA,
0.0, 0.0, 0.0),
zeus::CVector3f(xy, z, 1.0)).toMatrix4f();
2016-03-31 06:18:56 +00:00
break;
}
default: break;
}
bufOut += sizeof(zeus::CMatrix4f) * 2;
2016-03-31 06:18:56 +00:00
}
2016-04-04 02:32:57 +00:00
void CBooModel::UVAnimationBuffer::PadOutBuffer(u8*& bufStart, u8*& bufOut)
2016-03-31 06:18:56 +00:00
{
2016-04-04 02:32:57 +00:00
bufOut = bufStart + ROUND_UP_256(bufOut - bufStart);
2016-03-31 06:18:56 +00:00
}
2017-03-06 06:33:51 +00:00
static const zeus::CMatrix4f MBShadowPost0(1.f, 0.f, 0.f, 0.f,
0.f, -1.f, 0.f, 1.f,
0.f, 0.f, 0.f, 1.f,
0.f, 0.f, 0.f, 1.f);
static const zeus::CMatrix4f MBShadowPost1(0.f, 0.f, 0.f, 1.f,
0.f, 0.f, 1.f, -0.0625f,
0.f, 0.f, 0.f, 1.f,
0.f, 0.f, 0.f, 1.f);
2016-07-31 02:06:47 +00:00
void CBooModel::UVAnimationBuffer::Update(u8*& bufOut, const MaterialSet* matSet, const CModelFlags& flags)
2016-03-31 06:18:56 +00:00
{
2016-04-04 02:32:57 +00:00
u8* start = bufOut;
2016-03-31 06:18:56 +00:00
2017-03-06 06:33:51 +00:00
/* Special matrices for MorphBall shadow rendering */
2017-03-10 20:52:53 +00:00
if (flags.m_extendedShader == EExtendedShader::MorphBallShadow)
2017-03-06 06:33:51 +00:00
{
zeus::CMatrix4f texMtx =
(zeus::CTransform::Scale(1.f / (flags.mbShadowBox.max.x - flags.mbShadowBox.min.x),
1.f / (flags.mbShadowBox.max.y - flags.mbShadowBox.min.y),
1.f / (flags.mbShadowBox.max.z - flags.mbShadowBox.min.z)) *
zeus::CTransform::Translate(-flags.mbShadowBox.min.x,
-flags.mbShadowBox.min.y,
-flags.mbShadowBox.min.z) * CGraphics::g_GXModelView).toMatrix4f();
for (const MaterialSet::Material& mat : matSet->materials)
{
std::array<zeus::CMatrix4f, 2>* mtxs = reinterpret_cast<std::array<zeus::CMatrix4f, 2>*>(bufOut);
mtxs[0][0] = texMtx;
mtxs[0][1] = MBShadowPost0;
mtxs[1][0] = texMtx;
mtxs[1][1] = MBShadowPost1;
bufOut += sizeof(zeus::CMatrix4f) * 2 * 8;
PadOutBuffer(start, bufOut);
}
return;
}
std::experimental::optional<std::array<zeus::CMatrix4f, 2>> specialMtxOut;
2017-03-10 20:52:53 +00:00
if (flags.m_extendedShader == EExtendedShader::Thermal)
2016-07-31 02:06:47 +00:00
{
/* Special Mode0 matrix for exclusive Thermal Visor use */
specialMtxOut.emplace();
2016-07-31 02:06:47 +00:00
zeus::CMatrix4f& texMtxOut = (*specialMtxOut)[0];
2016-07-31 02:06:47 +00:00
texMtxOut = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
texMtxOut.vec[3].zeroOut();
texMtxOut.vec[3].w = 1.f;
zeus::CMatrix4f& postMtxOut = (*specialMtxOut)[1];
2016-07-31 02:06:47 +00:00
postMtxOut.vec[0].x = 0.5f;
postMtxOut.vec[1].y = 0.5f;
postMtxOut.vec[3].x = 0.5f;
postMtxOut.vec[3].y = 0.5f;
}
else if (flags.m_extendedShader == EExtendedShader::WorldShadow)
{
/* Special matrix for mapping world shadow */
specialMtxOut.emplace();
zeus::CMatrix4f mat = g_shadowTexXf.toMatrix4f();
zeus::CMatrix4f& texMtxOut = (*specialMtxOut)[0];
texMtxOut[0][0] = mat[0][0];
texMtxOut[1][0] = mat[1][0];
texMtxOut[2][0] = mat[2][0];
texMtxOut[3][0] = mat[3][0];
texMtxOut[0][1] = mat[0][2];
texMtxOut[1][1] = mat[1][2];
texMtxOut[2][1] = mat[2][2];
texMtxOut[3][1] = mat[3][2];
}
2016-07-31 02:06:47 +00:00
2016-03-31 06:18:56 +00:00
for (const MaterialSet::Material& mat : matSet->materials)
{
if (specialMtxOut)
2016-07-31 02:06:47 +00:00
{
std::array<zeus::CMatrix4f, 2>* mtxs = reinterpret_cast<std::array<zeus::CMatrix4f, 2>*>(bufOut);
mtxs[7][0] = (*specialMtxOut)[0];
mtxs[7][1] = (*specialMtxOut)[1];
2016-07-31 02:06:47 +00:00
}
u8* bufOrig = bufOut;
2016-03-31 06:18:56 +00:00
for (const UVAnimation& anim : mat.uvAnims)
2016-04-04 02:32:57 +00:00
ProcessAnimation(bufOut, anim);
2016-07-31 04:46:03 +00:00
bufOut = bufOrig + sizeof(zeus::CMatrix4f) * 2 * 8;
2016-04-04 02:32:57 +00:00
PadOutBuffer(start, bufOut);
2016-03-31 06:18:56 +00:00
}
}
boo::IGraphicsBufferD* CBooModel::UpdateUniformData(const CModelFlags& flags,
const CSkinRules* cskr,
const CPoseAsTransforms* pose) const
2016-03-31 06:18:56 +00:00
{
size_t skinBankCount = 0;
size_t weightVecCount = 0;
if (const CModel* model = m_model.GetObj())
{
skinBankCount = model->m_hmdlMeta.bankCount;
weightVecCount = model->m_hmdlMeta.weightCount;
}
/* Invalidate instances if new shadow being drawn */
if (flags.m_extendedShader == EExtendedShader::WorldShadow &&
m_lastDrawnShadowMap != g_shadowMap)
{
const_cast<CBooModel*>(this)->m_lastDrawnShadowMap = g_shadowMap;
const_cast<CBooModel*>(this)->m_instances.clear();
}
2016-09-09 04:19:19 +00:00
const ModelInstance* inst;
if (m_instances.size() <= m_uniUpdateCount)
2016-09-10 05:39:47 +00:00
{
2016-09-09 04:19:19 +00:00
inst = const_cast<CBooModel*>(this)->PushNewModelInstance();
2016-09-10 05:39:47 +00:00
if (!inst)
return nullptr;
2016-09-10 05:39:47 +00:00
}
2016-09-09 04:19:19 +00:00
else
inst = &m_instances[m_uniUpdateCount];
++const_cast<CBooModel*>(this)->m_uniUpdateCount;
2016-08-31 23:31:12 +00:00
2016-09-09 04:19:19 +00:00
u8* dataOut = reinterpret_cast<u8*>(inst->m_uniformBuffer->map(m_uniformDataSize));
2016-04-04 19:34:54 +00:00
u8* dataCur = dataOut;
2016-03-31 06:18:56 +00:00
if (skinBankCount)
2016-03-31 21:06:41 +00:00
{
2016-04-04 02:32:57 +00:00
/* Skinned */
2016-08-21 20:39:18 +00:00
std::vector<const zeus::CTransform*> bankTransforms;
size_t weightCount = weightVecCount * 4;
2016-09-06 05:52:51 +00:00
bankTransforms.reserve(weightCount);
for (size_t i=0 ; i<skinBankCount ; ++i)
2016-04-04 02:32:57 +00:00
{
2016-08-21 20:39:18 +00:00
if (cskr && pose)
2016-04-04 02:32:57 +00:00
{
2016-08-21 20:39:18 +00:00
cskr->GetBankTransforms(bankTransforms, *pose, i);
2016-09-06 05:52:51 +00:00
for (size_t w=0 ; w<weightCount ; ++w)
2016-08-21 20:39:18 +00:00
{
zeus::CMatrix4f& mv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
if (w >= bankTransforms.size())
mv = CGraphics::g_GXModelView.toMatrix4f();
else
mv = (CGraphics::g_GXModelView * *bankTransforms[w]).toMatrix4f();
dataCur += sizeof(zeus::CMatrix4f);
}
2016-09-06 05:52:51 +00:00
for (size_t w=0 ; w<weightCount ; ++w)
2016-08-21 20:39:18 +00:00
{
zeus::CMatrix4f& mvinv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
if (w >= bankTransforms.size())
mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
else
{
2016-09-06 05:52:51 +00:00
zeus::CTransform xf = (CGraphics::g_GXModelView.basis * bankTransforms[w]->basis);
xf.basis.invert();
2016-08-21 20:39:18 +00:00
xf.basis.transpose();
mvinv = xf.toMatrix4f();
}
dataCur += sizeof(zeus::CMatrix4f);
}
bankTransforms.clear();
2016-04-04 02:32:57 +00:00
}
2016-08-21 20:39:18 +00:00
else
2016-04-04 02:32:57 +00:00
{
2016-09-06 05:52:51 +00:00
for (size_t w=0 ; w<weightCount ; ++w)
2016-08-21 20:39:18 +00:00
{
zeus::CMatrix4f& mv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
mv = CGraphics::g_GXModelView.toMatrix4f();
dataCur += sizeof(zeus::CMatrix4f);
}
2016-09-06 05:52:51 +00:00
for (size_t w=0 ; w<weightCount ; ++w)
2016-08-21 20:39:18 +00:00
{
zeus::CMatrix4f& mvinv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
dataCur += sizeof(zeus::CMatrix4f);
}
2016-04-04 02:32:57 +00:00
}
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& proj = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
proj = CGraphics::GetPerspectiveProjectionMatrix(true);
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
dataCur = dataOut + ROUND_UP_256(dataCur - dataOut);
2016-04-04 02:32:57 +00:00
}
}
else
{
/* Non-Skinned */
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& mv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
mv = CGraphics::g_GXModelView.toMatrix4f();
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& mvinv = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
mvinv = CGraphics::g_GXModelViewInvXpose.toMatrix4f();
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
zeus::CMatrix4f& proj = reinterpret_cast<zeus::CMatrix4f&>(*dataCur);
2016-04-04 02:32:57 +00:00
proj = CGraphics::GetPerspectiveProjectionMatrix(true);
2016-04-04 19:34:54 +00:00
dataCur += sizeof(zeus::CMatrix4f);
2016-04-04 02:32:57 +00:00
2016-04-04 19:34:54 +00:00
dataCur = dataOut + ROUND_UP_256(dataCur - dataOut);
2016-03-31 21:06:41 +00:00
}
2016-04-04 02:32:57 +00:00
2016-07-31 02:06:47 +00:00
UVAnimationBuffer::Update(dataCur, x4_matSet, flags);
2016-04-04 02:32:57 +00:00
2017-03-10 20:52:53 +00:00
if (flags.m_extendedShader == EExtendedShader::Thermal) /* Thermal Model (same as UV Mode 0) */
2016-07-31 02:06:47 +00:00
{
CModelShaders::ThermalUniform& thermalOut = *reinterpret_cast<CModelShaders::ThermalUniform*>(dataCur);
2017-03-20 05:09:53 +00:00
thermalOut.mulColor = flags.x4_color;
2016-07-31 02:06:47 +00:00
thermalOut.addColor = flags.addColor;
}
else if (flags.m_extendedShader >= EExtendedShader::SolidColor &&
flags.m_extendedShader <= EExtendedShader::SolidColorBackfaceCullGreaterAlphaOnly) /* Solid color render */
2017-03-05 07:57:12 +00:00
{
CModelShaders::SolidUniform& solidOut = *reinterpret_cast<CModelShaders::SolidUniform*>(dataCur);
2017-03-20 05:09:53 +00:00
solidOut.solidColor = flags.x4_color;
2017-03-05 07:57:12 +00:00
}
2017-03-10 20:52:53 +00:00
else if (flags.m_extendedShader == EExtendedShader::MorphBallShadow) /* MorphBall shadow render */
2017-03-05 07:57:12 +00:00
{
CModelShaders::MBShadowUniform& shadowOut = *reinterpret_cast<CModelShaders::MBShadowUniform*>(dataCur);
shadowOut.shadowUp = CGraphics::g_GXModelView * zeus::CVector3f::skUp;
2017-03-20 05:09:53 +00:00
shadowOut.shadowUp.w = flags.x4_color.a;
shadowOut.shadowId = flags.x4_color.r;
2017-03-05 07:57:12 +00:00
}
2016-07-31 02:06:47 +00:00
else
{
CModelShaders::LightingUniform& lightingOut = *reinterpret_cast<CModelShaders::LightingUniform*>(dataCur);
lightingOut = m_lightingData;
2017-04-13 19:28:31 +00:00
lightingOut.colorRegs[0] = CGraphics::g_ColorRegs[0];
lightingOut.colorRegs[1] = CGraphics::g_ColorRegs[1];
lightingOut.colorRegs[2] = CGraphics::g_ColorRegs[2];
2017-03-20 05:09:53 +00:00
lightingOut.mulColor = flags.x4_color;
2016-08-08 04:48:18 +00:00
lightingOut.fog = CGraphics::g_Fog;
2016-07-31 02:06:47 +00:00
}
2016-04-04 02:32:57 +00:00
2017-03-26 05:53:04 +00:00
dataCur += sizeof(CModelShaders::LightingUniform);
dataCur = dataOut + ROUND_UP_256(dataCur - dataOut);
/* Reflection texmtx uniform */
zeus::CMatrix4f* identMtxs = reinterpret_cast<zeus::CMatrix4f*>(dataCur);
identMtxs[0] = zeus::CMatrix4f();
identMtxs[1] = zeus::CMatrix4f();
u8* curReflect = dataCur + 256;
for (const CBooSurface& surf : *x0_surfaces)
{
const MaterialSet::Material& mat = x4_matSet->materials.at(surf.m_data.matIdx);
if (mat.flags.samusReflection() || mat.flags.samusReflectionSurfaceEye())
{
zeus::CMatrix4f* mtxs = reinterpret_cast<zeus::CMatrix4f*>(curReflect);
float& alpha = reinterpret_cast<float&>(mtxs[2]);
curReflect += 256;
EnsureViewDepStateCached(*this, mat.flags.samusReflectionSurfaceEye() ? &surf : nullptr, mtxs, alpha);
}
}
2016-09-09 04:19:19 +00:00
inst->m_uniformBuffer->unmap();
return inst->m_dynamicVbo;
2016-07-31 02:06:47 +00:00
}
2016-03-31 06:18:56 +00:00
2016-08-21 20:39:18 +00:00
void CBooModel::DrawAlpha(const CModelFlags& flags,
const CSkinRules* cskr,
const CPoseAsTransforms* pose) const
2016-03-30 19:16:01 +00:00
{
2016-04-01 01:00:37 +00:00
if (TryLockTextures())
{
2016-08-21 20:39:18 +00:00
UpdateUniformData(flags, cskr, pose);
2016-04-01 01:00:37 +00:00
DrawAlphaSurfaces(flags);
}
2016-03-30 19:16:01 +00:00
}
2016-08-21 20:39:18 +00:00
void CBooModel::DrawNormal(const CModelFlags& flags,
const CSkinRules* cskr,
const CPoseAsTransforms* pose) const
2016-03-30 19:16:01 +00:00
{
2016-04-01 01:00:37 +00:00
if (TryLockTextures())
{
2016-08-21 20:39:18 +00:00
UpdateUniformData(flags, cskr, pose);
2016-04-01 01:00:37 +00:00
DrawNormalSurfaces(flags);
}
2016-03-30 19:16:01 +00:00
}
2016-08-21 20:39:18 +00:00
void CBooModel::Draw(const CModelFlags& flags,
const CSkinRules* cskr,
const CPoseAsTransforms* pose) const
2016-03-30 19:16:01 +00:00
{
2016-04-01 01:00:37 +00:00
if (TryLockTextures())
{
2016-08-21 20:39:18 +00:00
UpdateUniformData(flags, cskr, pose);
2016-04-01 01:00:37 +00:00
DrawSurfaces(flags);
}
2016-03-29 23:14:14 +00:00
}
static const u8* MemoryFromPartData(const u8*& dataCur, const u32*& secSizeCur)
2016-03-29 23:14:14 +00:00
{
const u8* ret;
if (*secSizeCur != 0)
2016-03-29 23:14:14 +00:00
ret = dataCur;
else
ret = nullptr;
dataCur += hecl::SBig(*secSizeCur);
++secSizeCur;
return ret;
}
std::unique_ptr<CBooModel> CModel::MakeNewInstance(int shaderIdx, int subInsts, boo::ITexture* txtrOverrides[8])
2016-03-29 23:14:14 +00:00
{
if (shaderIdx >= x18_matSets.size())
shaderIdx = 0;
return std::make_unique<CBooModel>(m_selfToken, &x8_surfaces, x18_matSets[shaderIdx],
m_staticVtxFmt, m_staticVbo, m_ibo,
m_aabb, (m_flags & 0x2) != 0, subInsts, txtrOverrides);
}
CModel::CModel(std::unique_ptr<u8[]>&& in, u32 /* dataLen */, IObjectStore* store, CObjectReference* selfRef)
: m_selfToken(selfRef)
{
x38_lastFrame = CGraphics::GetFrameCounter() - 2;
std::unique_ptr<u8[]> data = std::move(in);
u32 version = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x4));
m_flags = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x8));
2016-03-31 21:06:41 +00:00
if (version != 0x10002)
2016-03-29 23:14:14 +00:00
Log.report(logvisor::Fatal, "invalid CMDL for loading with boo");
u32 secCount = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x24));
u32 matSetCount = hecl::SBig(*reinterpret_cast<u32*>(data.get() + 0x28));
2016-03-29 23:14:14 +00:00
x18_matSets.reserve(matSetCount);
const u8* dataCur = data.get() + ROUND_UP_32(0x2c + secCount * 4);
const u32* secSizeCur = reinterpret_cast<const u32*>(data.get() + 0x2c);
2016-03-29 23:14:14 +00:00
for (u32 i=0 ; i<matSetCount ; ++i)
{
2016-03-31 02:44:43 +00:00
u32 matSetSz = hecl::SBig(*secSizeCur);
2016-03-29 23:14:14 +00:00
const u8* sec = MemoryFromPartData(dataCur, secSizeCur);
x18_matSets.emplace_back(i);
2016-03-31 02:44:43 +00:00
CBooModel::SShader& shader = x18_matSets.back();
athena::io::MemoryReader r(sec, matSetSz);
shader.m_matSet.read(r);
CBooModel::MakeTexturesFromMats(shader.m_matSet, shader.x0_textures, *store);
2016-03-29 23:14:14 +00:00
}
2016-03-30 19:16:01 +00:00
{
2016-03-31 02:44:43 +00:00
u32 hmdlSz = hecl::SBig(*secSizeCur);
2016-03-30 19:16:01 +00:00
const u8* hmdlMetadata = MemoryFromPartData(dataCur, secSizeCur);
2016-03-31 02:44:43 +00:00
athena::io::MemoryReader r(hmdlMetadata, hmdlSz);
m_hmdlMeta.read(r);
2016-03-30 19:16:01 +00:00
}
2016-03-29 23:14:14 +00:00
const u8* vboData = MemoryFromPartData(dataCur, secSizeCur);
const u8* iboData = MemoryFromPartData(dataCur, secSizeCur);
const u8* surfInfo = MemoryFromPartData(dataCur, secSizeCur);
2016-09-12 04:53:28 +00:00
for (CBooModel::SShader& matSet : x18_matSets)
{
matSet.m_shaders.reserve(matSet.m_matSet.materials.size());
for (const MaterialSet::Material& mat : matSet.m_matSet.materials)
{
2017-03-26 05:53:04 +00:00
hecl::Backend::ReflectionType reflectionType;
if (mat.flags.samusReflectionIndirectTexture())
reflectionType = hecl::Backend::ReflectionType::Indirect;
else if (mat.flags.samusReflection())
reflectionType = hecl::Backend::ReflectionType::Simple;
else
reflectionType = hecl::Backend::ReflectionType::None;
2016-09-12 04:53:28 +00:00
hecl::Runtime::ShaderTag tag(mat.heclIr,
m_hmdlMeta.colorCount, m_hmdlMeta.uvCount, m_hmdlMeta.weightCount,
m_hmdlMeta.weightCount * 4, 8, boo::Primitive(m_hmdlMeta.topology),
2017-03-26 05:53:04 +00:00
reflectionType, true, true, true);
2016-09-12 04:53:28 +00:00
matSet.m_shaders.push_back(CModelShaders::g_ModelShaders->buildExtendedShader
(tag, mat.heclIr, "CMDL", *CGraphics::g_BooFactory));
}
}
2016-03-30 19:16:01 +00:00
m_gfxToken = CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) -> bool
{
if (!m_hmdlMeta.bankCount)
{
/* Non-skinned models use static vertex buffers shared with CBooModel instances */
m_staticVbo = ctx.newStaticBuffer(boo::BufferUse::Vertex, vboData,
m_hmdlMeta.vertStride, m_hmdlMeta.vertCount);
m_staticVtxFmt = hecl::Runtime::HMDLData::NewVertexFormat(ctx, m_hmdlMeta, m_staticVbo, m_ibo);
}
else
{
/* Skinned models use per-instance dynamic buffers for vertex manipulation effects */
size_t vboSz = m_hmdlMeta.vertStride * m_hmdlMeta.vertCount;
m_dynamicVertexData.reset(new uint8_t[vboSz]);
memmove(m_dynamicVertexData.get(), vboData, vboSz);
}
/* Index buffer is always static */
m_ibo = ctx.newStaticBuffer(boo::BufferUse::Index, iboData, 4, m_hmdlMeta.indexCount);
return true;
});
2016-03-30 19:16:01 +00:00
2016-03-29 23:14:14 +00:00
u32 surfCount = hecl::SBig(*reinterpret_cast<const u32*>(surfInfo));
x8_surfaces.reserve(surfCount);
for (u32 i=0 ; i<surfCount ; ++i)
{
2016-03-31 02:44:43 +00:00
u32 surfSz = hecl::SBig(*secSizeCur);
2016-03-29 23:14:14 +00:00
const u8* sec = MemoryFromPartData(dataCur, secSizeCur);
x8_surfaces.emplace_back();
2016-03-31 02:44:43 +00:00
CBooSurface& surf = x8_surfaces.back();
2016-04-04 02:32:57 +00:00
surf.selfIdx = i;
2016-03-31 02:44:43 +00:00
athena::io::MemoryReader r(sec, surfSz);
surf.m_data.read(r);
2016-03-29 23:14:14 +00:00
}
const float* aabbPtr = reinterpret_cast<const float*>(data.get() + 0xc);
m_aabb = zeus::CAABox(hecl::SBig(aabbPtr[0]), hecl::SBig(aabbPtr[1]), hecl::SBig(aabbPtr[2]),
hecl::SBig(aabbPtr[3]), hecl::SBig(aabbPtr[4]), hecl::SBig(aabbPtr[5]));
2016-09-12 04:53:28 +00:00
x28_modelInst = MakeNewInstance(0, 1);
2016-03-29 23:14:14 +00:00
}
2016-02-13 00:57:09 +00:00
2016-03-31 02:44:43 +00:00
void CBooModel::SShader::UnlockTextures()
{
for (TCachedToken<CTexture>& tex : x0_textures)
tex.Unlock();
}
void CBooModel::VerifyCurrentShader(int shaderIdx)
2016-03-31 02:44:43 +00:00
{
if (shaderIdx != m_matSetIdx && m_model)
RemapMaterialData(m_model->x18_matSets[shaderIdx]);
}
void CBooModel::Touch(int shaderIdx) const
{
const_cast<CBooModel*>(this)->VerifyCurrentShader(shaderIdx);
TryLockTextures();
2016-03-31 02:44:43 +00:00
}
void CModel::DrawSortedParts(const CModelFlags& flags) const
{
2017-03-20 05:09:53 +00:00
const_cast<CBooModel&>(*x28_modelInst).VerifyCurrentShader(flags.x1_matSetIdx);
2016-08-21 20:39:18 +00:00
x28_modelInst->DrawAlpha(flags, nullptr, nullptr);
2016-03-31 02:44:43 +00:00
}
void CModel::DrawUnsortedParts(const CModelFlags& flags) const
{
2017-03-20 05:09:53 +00:00
const_cast<CBooModel&>(*x28_modelInst).VerifyCurrentShader(flags.x1_matSetIdx);
2016-08-21 20:39:18 +00:00
x28_modelInst->DrawNormal(flags, nullptr, nullptr);
2016-03-31 02:44:43 +00:00
}
2016-02-13 00:57:09 +00:00
void CModel::Draw(const CModelFlags& flags) const
{
2017-03-20 05:09:53 +00:00
const_cast<CBooModel&>(*x28_modelInst).VerifyCurrentShader(flags.x1_matSetIdx);
2016-08-21 20:39:18 +00:00
x28_modelInst->Draw(flags, nullptr, nullptr);
2016-02-13 00:57:09 +00:00
}
2016-03-31 02:44:43 +00:00
bool CModel::IsLoaded(int shaderIdx) const
2016-03-17 02:18:01 +00:00
{
const_cast<CBooModel&>(*x28_modelInst).VerifyCurrentShader(shaderIdx);
std::vector<TCachedToken<CTexture>>& texs = x28_modelInst->x1c_textures;
2016-03-31 06:18:56 +00:00
bool loaded = true;
for (TCachedToken<CTexture>& tex : texs)
2016-03-31 06:18:56 +00:00
{
if (!tex.IsLoaded())
{
loaded = false;
break;
}
}
return loaded;
2016-03-17 02:18:01 +00:00
}
size_t CModel::GetPoolVertexOffset(size_t idx) const
{
return m_hmdlMeta.vertStride * idx;
}
zeus::CVector3f CModel::GetPoolVertex(size_t idx) const
{
auto* floats = reinterpret_cast<const float*>(m_dynamicVertexData.get() + GetPoolVertexOffset(idx));
return {floats[0], floats[1], floats[2]};
}
size_t CModel::GetPoolNormalOffset(size_t idx) const
{
return m_hmdlMeta.vertStride * idx + 12;
}
zeus::CVector3f CModel::GetPoolNormal(size_t idx) const
{
auto* floats = reinterpret_cast<const float*>(m_dynamicVertexData.get() + GetPoolNormalOffset(idx));
return {floats[0], floats[1], floats[2]};
}
void CModel::ApplyVerticesCPU(boo::IGraphicsBufferD* vertBuf,
const std::vector<std::pair<zeus::CVector3f, zeus::CVector3f>>& vn) const
{
u8* data = reinterpret_cast<u8*>(vertBuf->map(m_hmdlMeta.vertStride * m_hmdlMeta.vertCount));
for (u32 i=0 ; i<std::min(u32(vn.size()), m_hmdlMeta.vertCount) ; ++i)
{
const std::pair<zeus::CVector3f, zeus::CVector3f>& avn = vn[i];
float* floats = reinterpret_cast<float*>(data + GetPoolVertexOffset(i));
floats[0] = avn.first.x;
floats[1] = avn.first.y;
floats[2] = avn.first.z;
floats[3] = avn.second.x;
floats[4] = avn.second.y;
floats[5] = avn.second.z;
}
vertBuf->unmap();
}
2016-03-29 23:14:14 +00:00
CFactoryFnReturn FModelFactory(const urde::SObjectTag& tag,
std::unique_ptr<u8[]>&& in, u32 len,
const urde::CVParamTransfer& vparms,
CObjectReference* selfRef)
2016-03-29 23:14:14 +00:00
{
2016-12-23 06:41:39 +00:00
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
CFactoryFnReturn ret = TToken<CModel>::GetIObjObjectFor(std::make_unique<CModel>(std::move(in), len, sp, selfRef));
2016-04-01 01:00:37 +00:00
return ret;
2016-03-29 23:14:14 +00:00
}
2016-02-13 00:57:09 +00:00
}