Simplify access to groups internally (#270)

This commit is contained in:
Anders Jenbo 2025-06-09 21:42:24 +02:00 committed by GitHub
parent 7e9acc8d04
commit 08c654bf60
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 37 additions and 63 deletions

View File

@ -101,6 +101,11 @@ HRESULT Direct3DRMMeshImpl::GetGroup(
return DD_OK; return DD_OK;
} }
const MeshGroup& Direct3DRMMeshImpl::GetGroup(DWORD groupIndex)
{
return m_groups[groupIndex];
}
DWORD Direct3DRMMeshImpl::GetGroupCount() DWORD Direct3DRMMeshImpl::GetGroupCount()
{ {
return m_groups.size(); return m_groups.size();

View File

@ -1,5 +1,6 @@
#include "d3drm_impl.h" #include "d3drm_impl.h"
#include "d3drmframe_impl.h" #include "d3drmframe_impl.h"
#include "d3drmmesh_impl.h"
#include "d3drmrenderer.h" #include "d3drmrenderer.h"
#include "d3drmviewport_impl.h" #include "d3drmviewport_impl.h"
#include "ddraw_impl.h" #include "ddraw_impl.h"
@ -197,12 +198,7 @@ bool IsBoxInFrustum(const D3DVECTOR corners[8], const Plane planes[6])
return true; return true;
} }
void Direct3DRMViewportImpl::CollectMeshesFromFrame( void Direct3DRMViewportImpl::CollectMeshesFromFrame(IDirect3DRMFrame* frame, D3DRMMATRIX4D parentMatrix)
IDirect3DRMFrame* frame,
D3DRMMATRIX4D parentMatrix,
std::vector<D3DRMVERTEX>& d3dVerts,
std::vector<DWORD>& indices
)
{ {
Direct3DRMFrameImpl* frameImpl = static_cast<Direct3DRMFrameImpl*>(frame); Direct3DRMFrameImpl* frameImpl = static_cast<Direct3DRMFrameImpl*>(frame);
D3DRMMATRIX4D localMatrix; D3DRMMATRIX4D localMatrix;
@ -226,13 +222,13 @@ void Direct3DRMViewportImpl::CollectMeshesFromFrame(
IDirect3DRMFrame* childFrame = nullptr; IDirect3DRMFrame* childFrame = nullptr;
visual->QueryInterface(IID_IDirect3DRMFrame, (void**) &childFrame); visual->QueryInterface(IID_IDirect3DRMFrame, (void**) &childFrame);
if (childFrame) { if (childFrame) {
CollectMeshesFromFrame(childFrame, worldMatrix, d3dVerts, indices); CollectMeshesFromFrame(childFrame, worldMatrix);
childFrame->Release(); childFrame->Release();
visual->Release(); visual->Release();
continue; continue;
} }
IDirect3DRMMesh* mesh = nullptr; Direct3DRMMeshImpl* mesh = nullptr;
visual->QueryInterface(IID_IDirect3DRMMesh, (void**) &mesh); visual->QueryInterface(IID_IDirect3DRMMesh, (void**) &mesh);
if (!mesh) { if (!mesh) {
visual->Release(); visual->Release();
@ -262,47 +258,32 @@ void Direct3DRMViewportImpl::CollectMeshesFromFrame(
DWORD groupCount = mesh->GetGroupCount(); DWORD groupCount = mesh->GetGroupCount();
for (DWORD gi = 0; gi < groupCount; ++gi) { for (DWORD gi = 0; gi < groupCount; ++gi) {
DWORD vtxCount, indexCount; const MeshGroup& meshGroup = mesh->GetGroup(gi);
mesh->GetGroup(gi, &vtxCount, nullptr, nullptr, &indexCount, nullptr);
d3dVerts.resize(vtxCount);
indices.resize(indexCount);
mesh->GetVertices(gi, 0, vtxCount, d3dVerts.data());
mesh->GetGroup(gi, nullptr, nullptr, nullptr, nullptr, indices.data());
D3DCOLOR color = mesh->GetGroupColor(gi);
D3DRMRENDERQUALITY quality = mesh->GetGroupQuality(gi);
IDirect3DRMTexture* texture = nullptr;
mesh->GetGroupTexture(gi, &texture);
Uint32 textureId = NO_TEXTURE_ID; Uint32 textureId = NO_TEXTURE_ID;
if (texture) { if (meshGroup.texture) {
textureId = m_renderer->GetTextureId(texture); textureId = m_renderer->GetTextureId(meshGroup.texture);
texture->Release();
} }
IDirect3DRMMaterial* material = nullptr;
mesh->GetGroupMaterial(gi, &material);
float shininess = 0.0f; float shininess = 0.0f;
if (material) { if (meshGroup.material) {
shininess = material->GetPower() * shininessFactor; shininess = meshGroup.material->GetPower() * shininessFactor;
material->Release();
} }
m_renderer->SubmitDraw( m_renderer->SubmitDraw(
d3dVerts.data(), meshGroup.vertices.data(),
d3dVerts.size(), meshGroup.vertices.size(),
indices.data(), meshGroup.indices.data(),
indices.size(), meshGroup.indices.size(),
worldMatrix, worldMatrix,
worldMatrixInvert, worldMatrixInvert,
{{static_cast<Uint8>((color >> 16) & 0xFF), {{static_cast<Uint8>((meshGroup.color >> 16) & 0xFF),
static_cast<Uint8>((color >> 8) & 0xFF), static_cast<Uint8>((meshGroup.color >> 8) & 0xFF),
static_cast<Uint8>((color >> 0) & 0xFF), static_cast<Uint8>((meshGroup.color >> 0) & 0xFF),
static_cast<Uint8>((color >> 24) & 0xFF)}, static_cast<Uint8>((meshGroup.color >> 24) & 0xFF)},
shininess, shininess,
textureId, textureId,
quality == D3DRMRENDER_FLAT || quality == D3DRMRENDER_UNLITFLAT} meshGroup.quality == D3DRMRENDER_FLAT || meshGroup.quality == D3DRMRENDER_UNLITFLAT}
); );
} }
mesh->Release(); mesh->Release();
@ -331,10 +312,8 @@ HRESULT Direct3DRMViewportImpl::RenderScene()
return status; return status;
} }
std::vector<D3DRMVERTEX> d3dVerts;
std::vector<DWORD> indices;
ExtractFrustumPlanes(viewProj); ExtractFrustumPlanes(viewProj);
CollectMeshesFromFrame(m_rootFrame, identity, d3dVerts, indices); CollectMeshesFromFrame(m_rootFrame, identity);
return m_renderer->FinalizeFrame(); return m_renderer->FinalizeFrame();
} }
@ -682,31 +661,25 @@ bool RayIntersectsTriangle(
bool RayIntersectsMeshTriangles( bool RayIntersectsMeshTriangles(
const Ray& ray, const Ray& ray,
IDirect3DRMMesh* mesh, Direct3DRMMeshImpl& mesh,
const D3DRMMATRIX4D& worldMatrix, const D3DRMMATRIX4D& worldMatrix,
float& outDistance float& outDistance
) )
{ {
DWORD groupCount = mesh->GetGroupCount(); DWORD groupCount = mesh.GetGroupCount();
for (DWORD g = 0; g < groupCount; ++g) { for (DWORD gi = 0; gi < groupCount; ++gi) {
DWORD vtxCount, indexCount; const MeshGroup& meshGroup = mesh.GetGroup(gi);
mesh->GetGroup(g, &vtxCount, nullptr, nullptr, &indexCount, nullptr);
std::vector<D3DRMVERTEX> vertices(vtxCount);
mesh->GetVertices(g, 0, vtxCount, vertices.data());
std::vector<DWORD> indices(indexCount);
mesh->GetGroup(g, nullptr, nullptr, nullptr, nullptr, indices.data());
// Iterate over each face and do ray-triangle tests // Iterate over each face and do ray-triangle tests
for (DWORD fi = 0; fi < indexCount; fi += 3) { for (DWORD fi = 0; fi < meshGroup.indices.size(); fi += 3) {
DWORD i0 = indices[fi + 0]; DWORD i0 = meshGroup.indices[fi + 0];
DWORD i1 = indices[fi + 1]; DWORD i1 = meshGroup.indices[fi + 1];
DWORD i2 = indices[fi + 2]; DWORD i2 = meshGroup.indices[fi + 2];
// Transform vertices to world space // Transform vertices to world space
D3DVECTOR tri[3]; D3DVECTOR tri[3];
for (int j = 0; j < 3; ++j) { for (int j = 0; j < 3; ++j) {
const D3DVECTOR& v = vertices[(j == 0 ? i0 : (j == 1 ? i1 : i2))].position; const D3DVECTOR& v = meshGroup.vertices[(j == 0 ? i0 : (j == 1 ? i1 : i2))].position;
tri[j] = TransformPoint(v, worldMatrix); tri[j] = TransformPoint(v, worldMatrix);
} }
@ -799,7 +772,7 @@ HRESULT Direct3DRMViewportImpl::Pick(float x, float y, LPDIRECT3DRMPICKEDARRAY*
continue; continue;
} }
IDirect3DRMMesh* mesh = nullptr; Direct3DRMMeshImpl* mesh = nullptr;
visual->QueryInterface(IID_IDirect3DRMMesh, (void**) &mesh); visual->QueryInterface(IID_IDirect3DRMMesh, (void**) &mesh);
if (mesh) { if (mesh) {
D3DRMBOX box; D3DRMBOX box;
@ -811,7 +784,7 @@ HRESULT Direct3DRMViewportImpl::Pick(float x, float y, LPDIRECT3DRMPICKEDARRAY*
float distance = FLT_MAX; float distance = FLT_MAX;
if (RayIntersectsBox(pickRay, worldBox, distance) && if (RayIntersectsBox(pickRay, worldBox, distance) &&
RayIntersectsMeshTriangles(pickRay, mesh, worldMatrix, distance)) { RayIntersectsMeshTriangles(pickRay, *mesh, worldMatrix, distance)) {
auto* arr = new Direct3DRMFrameArrayImpl(); auto* arr = new Direct3DRMFrameArrayImpl();
for (IDirect3DRMFrame* f : path) { for (IDirect3DRMFrame* f : path) {
arr->AddElement(f); arr->AddElement(f);

View File

@ -76,6 +76,7 @@ struct Direct3DRMMeshImpl : public Direct3DRMObjectBaseImpl<IDirect3DRMMesh> {
DWORD* indexCount, DWORD* indexCount,
DWORD* indices DWORD* indices
) override; ) override;
const MeshGroup& GetGroup(DWORD groupIndex);
DWORD GetGroupCount() override; DWORD GetGroupCount() override;
HRESULT SetGroupColor(DWORD groupIndex, D3DCOLOR color) override; HRESULT SetGroupColor(DWORD groupIndex, D3DCOLOR color) override;
HRESULT SetGroupColorRGB(DWORD groupIndex, float r, float g, float b) override; HRESULT SetGroupColorRGB(DWORD groupIndex, float r, float g, float b) override;

View File

@ -37,12 +37,7 @@ struct Direct3DRMViewportImpl : public Direct3DRMObjectBaseImpl<IDirect3DRMViewp
private: private:
HRESULT RenderScene(); HRESULT RenderScene();
void CollectLightsFromFrame(IDirect3DRMFrame* frame, D3DRMMATRIX4D parentMatrix, std::vector<SceneLight>& lights); void CollectLightsFromFrame(IDirect3DRMFrame* frame, D3DRMMATRIX4D parentMatrix, std::vector<SceneLight>& lights);
void CollectMeshesFromFrame( void CollectMeshesFromFrame(IDirect3DRMFrame* frame, D3DRMMATRIX4D parentMatrix);
IDirect3DRMFrame* frame,
D3DRMMATRIX4D parentMatrix,
std::vector<D3DRMVERTEX>& d3dVerts,
std::vector<DWORD>& faces
);
void UpdateProjectionMatrix(); void UpdateProjectionMatrix();
Direct3DRMRenderer* m_renderer; Direct3DRMRenderer* m_renderer;
D3DCOLOR m_backgroundColor = 0xFF000000; D3DCOLOR m_backgroundColor = 0xFF000000;