Cache bounding box (#225)

This commit is contained in:
Anders Jenbo 2025-06-03 19:30:25 +02:00 committed by GitHub
parent d5a6da31e3
commit 85e8c2e42a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 16 deletions

View File

@ -63,6 +63,9 @@ HRESULT Direct3DRMMeshImpl::AddGroup(
group.faces.assign(src, src + faceCount * vertexPerFace); group.faces.assign(src, src + faceCount * vertexPerFace);
m_groups.push_back(std::move(group)); m_groups.push_back(std::move(group));
UpdateBox(newIndex);
return DD_OK; return DD_OK;
} }
@ -142,6 +145,11 @@ HRESULT Direct3DRMMeshImpl::SetGroupMaterial(DWORD groupIndex, IDirect3DRMMateri
return DDERR_INVALIDPARAMS; return DDERR_INVALIDPARAMS;
} }
auto& group = m_groups[groupIndex];
if (group.material) {
group.material->Release();
}
material->AddRef(); material->AddRef();
m_groups[groupIndex].material = material; m_groups[groupIndex].material = material;
return DD_OK; return DD_OK;
@ -244,6 +252,9 @@ HRESULT Direct3DRMMeshImpl::SetVertices(DWORD groupIndex, int offset, int count,
} }
std::copy(vertices, vertices + count, vertList.begin() + offset); std::copy(vertices, vertices + count, vertList.begin() + offset);
UpdateBox();
return DD_OK; return DD_OK;
} }
@ -263,24 +274,32 @@ HRESULT Direct3DRMMeshImpl::GetVertices(DWORD groupIndex, int startIndex, int co
return DD_OK; return DD_OK;
} }
/** void Direct3DRMMeshImpl::UpdateBox()
* @todo Maybe a good idea to cache this {
*/ const float INF = std::numeric_limits<float>::max();
m_box.min = {INF, INF, INF};
m_box.max = {-INF, -INF, -INF};
for (size_t i = 0; i < m_groups.size(); ++i) {
UpdateBox(i);
}
}
void Direct3DRMMeshImpl::UpdateBox(DWORD groupIndex)
{
for (const D3DRMVERTEX& v : m_groups[groupIndex].vertices) {
m_box.min.x = std::min(m_box.min.x, v.position.x);
m_box.min.y = std::min(m_box.min.y, v.position.y);
m_box.min.z = std::min(m_box.min.z, v.position.z);
m_box.max.x = std::max(m_box.max.x, v.position.x);
m_box.max.y = std::max(m_box.max.y, v.position.y);
m_box.max.z = std::max(m_box.max.z, v.position.z);
}
}
HRESULT Direct3DRMMeshImpl::GetBox(D3DRMBOX* box) HRESULT Direct3DRMMeshImpl::GetBox(D3DRMBOX* box)
{ {
box->min.x = box->min.y = box->min.z = std::numeric_limits<float>::max(); *box = m_box;
box->max.x = box->max.y = box->max.z = std::numeric_limits<float>::min();
for (const auto& group : m_groups) {
for (const D3DRMVERTEX& v : group.vertices) {
box->min.x = std::min(box->min.x, v.position.x);
box->min.y = std::min(box->min.y, v.position.y);
box->min.z = std::min(box->min.z, v.position.z);
box->max.x = std::max(box->max.x, v.position.x);
box->max.y = std::max(box->max.y, v.position.y);
box->max.z = std::max(box->max.z, v.position.z);
}
}
return DD_OK; return DD_OK;
} }

View File

@ -93,5 +93,9 @@ struct Direct3DRMMeshImpl : public Direct3DRMObjectBaseImpl<IDirect3DRMMesh> {
HRESULT GetBox(D3DRMBOX* box) override; HRESULT GetBox(D3DRMBOX* box) override;
private: private:
void UpdateBox();
void UpdateBox(DWORD groupIndex);
std::vector<MeshGroup> m_groups; std::vector<MeshGroup> m_groups;
D3DRMBOX m_box;
}; };