isle-portable/LEGO1/tgl/d3drm/mesh.cpp
Mark Langen 260772e374
Bootstrap decomp of Tgl rendering library (#293)
* Bootstrap decomp of D3DRM rendering code

* This PR kicks off work on decompiling the D3D Retained Mode (D3DRM)
  rendering part of the codebase.

* High level overview:

* There is a base IMxDirect3DRMObject class which all of the D3DRM
  rendering objects inherit from. Its only virtual method is one to get
  the underlying object handle.

* A hierarchy of abstract classes inherits from this base class, which
  I've called "IMxDirect3DRM<class>". These classes only have pure
  virtual methods on them and don't contain any data.

* Each one of the abstract classes has exactly one concrete
  implementation, which I've called "MxDirect3DRM<class>". These classes
  have exactly one piece of data, which is a pointer to the underlying
  D3D Retained Mode object.

* If the classes need to store additional data, they store it in a
  userdata blob which is attached to the D3DRM object rather than the
  additional data being stored in the class itself.

* I've worked out about twice this many classes related to D3DRM
  rendering so far but the PR was getting large enough as is, so I'm
  cutting it here for now.

* I decomped sufficiently many methods of these classe to convince
  myself that the above observations are correct. About 60% of the
  decomped methods here are perfect matches, including at least one
  non-trivial method per class.

* Formatting

* Restructure changes using Tgl naming / details

* Restructure the changes to use the naming that we know from Tgl.

* Fill in some parts of the implementation I couldn't initially figure
  out using the details from Tgl (got more 100% matches).

* Move d3drm link requirement

* Fixups FloatMatrix -> FloatMatrix4

* Fix order

* Full fix for ordering problems

* Put back accidentally removed include.

* Fix call which should have been Release

* Use new and delete for DeepClone

* Missing Tgl:: on CreateRenderer

* Revert change to bool return value.

* Rename Something -> Unk

* Return paramter naming convention to what Tgl used

* Add scalar ddtor to verify inline destructor

* Fix order

* Change malloc/free -> new/delete in Tgl

* Remove duplicate destructor.

* Check all inline destructors

* Fix dtor comments

* Third time's the charm

* Alphabetical sort

* Decomp adjustments

* Add d3drm files to clang-format

---------

Co-authored-by: Christian Semmler <mail@csemmler.com>
2023-12-07 13:10:42 +01:00

158 lines
4.8 KiB
C++

#include "impl.h"
using namespace TglImpl;
DECOMP_SIZE_ASSERT(D3DRMVERTEX, 0x24);
DECOMP_SIZE_ASSERT(Mesh, 0x4);
DECOMP_SIZE_ASSERT(MeshImpl, 0x8);
// SYNTHETIC: LEGO1 0x100a3d80
// TglImpl::MeshImpl::`scalar deleting destructor'
// FUNCTION: LEGO1 0x100a3ed0
void* MeshImpl::ImplementationDataPtr()
{
return reinterpret_cast<void*>(&m_data);
}
// FUNCTION: LEGO1 0x100a3ee0
Result MeshImpl::SetColor(float r, float g, float b, float a)
{
// The first instruction makes no sense here:
// cmp dword ptr [esp + 0x10], 0
// This compares a, which we know is a float because it immediately
// gets passed into D3DRMCreateColorRGBA, but does the comparison
// as though it's an int??
if (*reinterpret_cast<int*>(&a) > 0) {
D3DCOLOR color = D3DRMCreateColorRGBA(r, g, b, a);
return ResultVal(m_data->groupMesh->SetGroupColor(m_data->groupIndex, color));
}
else {
return ResultVal(m_data->groupMesh->SetGroupColorRGB(m_data->groupIndex, r, g, b));
}
}
// FUNCTION: LEGO1 0x100a3f50
Result MeshImpl::SetTexture(const Texture* pTexture)
{
IDirect3DRMTexture* texture = pTexture ? static_cast<const TextureImpl*>(pTexture)->ImplementationData() : NULL;
return ResultVal(m_data->groupMesh->SetGroupTexture(m_data->groupIndex, texture));
}
// FUNCTION: LEGO1 0x100a3f80
Result MeshImpl::SetTextureMappingMode(ProjectionType projType)
{
if (projType == Perspective) {
return ResultVal(m_data->groupMesh->SetGroupMapping(m_data->groupIndex, D3DRMMAP_PERSPCORRECT));
}
else {
return ResultVal(m_data->groupMesh->SetGroupMapping(m_data->groupIndex, 0));
}
}
// FUNCTION: LEGO1 0x100a3fc0
Result MeshImpl::SetShadingModel(ShadingModel model)
{
D3DRMRENDERQUALITY mode;
switch (model) {
case Wireframe:
mode = D3DRMRENDER_WIREFRAME;
break;
case UnlitFlat:
mode = D3DRMRENDER_UNLITFLAT;
break;
case Flat:
mode = D3DRMRENDER_FLAT;
break;
case Gouraud:
mode = D3DRMRENDER_GOURAUD;
break;
case Phong:
mode = D3DRMRENDER_PHONG;
break;
}
return ResultVal(m_data->groupMesh->SetGroupQuality(m_data->groupIndex, mode));
}
// FUNCTION: LEGO1 0x100a4030
Mesh* MeshImpl::DeepClone(Unk* pUnk)
{
// Create group
MeshImpl* newMesh = new MeshImpl();
MeshData* data = new MeshData();
newMesh->m_data = data;
// Query information from old group
DWORD dataSize;
unsigned int vcount, fcount, vperface;
m_data->groupMesh->GetGroup(m_data->groupIndex, &vcount, &fcount, &vperface, &dataSize, NULL);
unsigned int* faceBuffer = new unsigned int[dataSize];
m_data->groupMesh->GetGroup(m_data->groupIndex, &vcount, &fcount, &vperface, &dataSize, faceBuffer);
// We expect vertex to be sized 0x24, checked at start of file.
D3DRMVERTEX* vertexBuffer = new D3DRMVERTEX[vcount];
m_data->groupMesh->GetVertices(m_data->groupIndex, 0, vcount, vertexBuffer);
LPDIRECT3DRMTEXTURE textureRef;
m_data->groupMesh->GetGroupTexture(m_data->groupIndex, &textureRef);
D3DRMMAPPING mapping = m_data->groupMesh->GetGroupMapping(m_data->groupIndex);
D3DRMRENDERQUALITY quality = m_data->groupMesh->GetGroupQuality(m_data->groupIndex);
D3DCOLOR color = m_data->groupMesh->GetGroupColor(m_data->groupIndex);
// Push information to new group
UnkImpl* target = static_cast<UnkImpl*>(pUnk);
D3DRMGROUPINDEX index;
target->ImplementationData()->AddGroup(vcount, fcount, vperface, faceBuffer, &index);
newMesh->m_data->groupIndex = index;
target->ImplementationData()->SetVertices(index, 0, vcount, vertexBuffer);
target->ImplementationData()->SetGroupTexture(index, textureRef);
target->ImplementationData()->SetGroupMapping(index, mapping);
target->ImplementationData()->SetGroupQuality(index, quality);
Result result = ResultVal(target->ImplementationData()->SetGroupColor(index, color));
// Cleanup
delete[] faceBuffer;
delete[] vertexBuffer;
if (result == Error) {
delete newMesh;
newMesh = NULL;
}
return newMesh;
}
// FUNCTION: LEGO1 0x100a4240
Mesh* MeshImpl::ShallowClone(Unk* pUnk)
{
MeshImpl* newGroup = new MeshImpl();
MeshData* newData = new MeshData();
newGroup->m_data = newData;
if (newData) {
newData->groupIndex = m_data->groupIndex;
newData->groupMesh = static_cast<UnkImpl*>(pUnk)->ImplementationData();
}
else {
delete newGroup;
newGroup = NULL;
}
return newGroup;
}
// FUNCTION: LEGO1 0x100a4330
Result MeshImpl::GetTexture(Texture*& rpTexture)
{
IDirect3DRMTexture* texture;
TextureImpl* holder = new TextureImpl();
Result result = ResultVal(m_data->groupMesh->GetGroupTexture(m_data->groupIndex, &texture));
if (result) {
// Seems to actually call the first virtual method of holder here
// but that doesn't make any sense since it passes three arguments
// to the method (self + string constant? + an offset?).
// This line makes the start of the function match and is what I
// would expect to see there but it clearly isn't what's actually
// there.
holder->SetImplementation(texture);
}
rpTexture = holder;
return Success;
}