Implement LegoLOD::Read and related (#634)

* WIP

* Fix

* Remove bogus

* Fix

* Match

* Fix

* Remove TODO
This commit is contained in:
Christian Semmler
2024-03-07 14:57:17 -05:00
committed by GitHub
parent a12146f52f
commit 53e2353f78
16 changed files with 652 additions and 126 deletions

View File

@@ -0,0 +1,317 @@
#include "legolod.h"
#include "geom/legomesh.h"
#include "legoroi.h"
#include "misc/legocontainer.h"
#include "misc/legostorage.h"
#include "tgl/d3drm/impl.h"
DECOMP_SIZE_ASSERT(LODObject, 0x04)
DECOMP_SIZE_ASSERT(ViewLOD, 0x0c)
DECOMP_SIZE_ASSERT(LegoLOD, 0x20)
DECOMP_SIZE_ASSERT(LegoLOD::Mesh, 0x08)
// GLOBAL: LEGO1 0x101013d4
LPDIRECT3DRMMATERIAL g_unk0x101013d4 = NULL;
inline IDirect3DRM2* GetD3DRM(Tgl::Renderer* pRenderer);
inline BOOL GetMeshData(IDirect3DRMMesh*& mesh, D3DRMGROUPINDEX& index, Tgl::Mesh* pMesh);
// TODO: Find out which unit this belongs to
// STUB: LEGO1 0x10065f60
BOOL SetGroupTexture(Tgl::Mesh* pMesh, LegoTextureInfo* p_textureInfo)
{
TglImpl::MeshImpl::MeshData* data = ((TglImpl::MeshImpl*) pMesh)->ImplementationData();
data->groupMesh->SetGroupTexture(data->groupIndex, p_textureInfo->m_texture);
return TRUE;
}
// FUNCTION: LEGO1 0x100aa380
LegoLOD::LegoLOD(Tgl::Renderer* p_renderer) : ViewLOD(p_renderer)
{
if (g_unk0x101013d4 == NULL) {
GetD3DRM(p_renderer)->CreateMaterial(10.0, &g_unk0x101013d4);
}
m_meshes = NULL;
m_numMeshes = 0;
m_numVertices = 0;
m_numPolys = 0;
m_unk0x1c = 0;
}
// STUB: LEGO1 0x100aa450
LegoLOD::~LegoLOD()
{
// TODO
}
// FUNCTION: LEGO1 0x100aa510
LegoResult LegoLOD::Read(Tgl::Renderer* p_renderer, LegoTextureContainer* p_textureContainer, LegoStorage* p_storage)
{
float(*normals)[3] = NULL;
float(*vertices)[3] = NULL;
float(*textureVertices)[2] = NULL;
LegoS32 numVerts = 0;
LegoS32 numNormals = 0;
LegoS32 numTextureVertices = 0;
LegoMesh* mesh = NULL;
LegoU32(*polyIndices)[3] = NULL;
LegoU32(*textureIndices)[3] = NULL;
LegoTextureInfo* textureInfo = NULL;
LegoU32 i, meshUnd1, meshUnd2, tempNumVertsAndNormals;
unsigned char paletteEntries[256];
if (p_storage->Read(&m_unk0x08, sizeof(m_unk0x08)) != SUCCESS) {
goto done;
}
if (GetUnknown0x08Test4()) {
return SUCCESS;
}
m_unk0x04 = p_renderer->CreateUnk();
if (p_storage->Read(&m_numMeshes, sizeof(m_numMeshes)) != SUCCESS) {
goto done;
}
if (m_numMeshes == 0) {
ClearFlag(c_bit4);
return SUCCESS;
}
SetFlag(c_bit4);
m_meshes = new Mesh[m_numMeshes];
memset(m_meshes, 0, sizeof(*m_meshes) * m_numMeshes);
meshUnd1 = m_numMeshes - 1;
meshUnd2 = 0;
if (p_storage->Read(&tempNumVertsAndNormals, sizeof(tempNumVertsAndNormals)) != SUCCESS) {
goto done;
}
numVerts = *((LegoU16*) &tempNumVertsAndNormals) & MAXSHORT;
numNormals = (*((LegoU16*) &tempNumVertsAndNormals + 1) >> 1) & MAXSHORT;
if (p_storage->Read(&numTextureVertices, sizeof(numTextureVertices)) != SUCCESS) {
goto done;
}
if (numVerts > 0) {
vertices = new float[numVerts][_countof(*vertices)];
if (p_storage->Read(vertices, numVerts * sizeof(*vertices)) != SUCCESS) {
goto done;
}
}
if (numNormals > 0) {
normals = new float[numNormals][_countof(*normals)];
if (p_storage->Read(normals, numNormals * sizeof(*normals)) != SUCCESS) {
goto done;
}
}
if (numTextureVertices > 0) {
textureVertices = new float[numTextureVertices][_countof(*textureVertices)];
if (p_storage->Read(textureVertices, numTextureVertices * sizeof(*textureVertices)) != SUCCESS) {
goto done;
}
}
for (i = 0; i < m_numMeshes; i++) {
LegoU32 numPolys, numVertices, numTextureIndices, meshIndex;
const LegoChar *textureName, *materialName;
Tgl::ShadingModel shadingModel;
if (p_storage->Read(&numPolys, 2) != SUCCESS) {
goto done;
}
m_numPolys += numPolys & MAXWORD;
if (p_storage->Read(&numVertices, 2) != SUCCESS) {
goto done;
}
polyIndices = new LegoU32[numPolys & MAXWORD][_countof(*polyIndices)];
if (p_storage->Read(polyIndices, (numPolys & MAXWORD) * sizeof(*polyIndices)) != SUCCESS) {
goto done;
}
if (p_storage->Read(&numTextureIndices, sizeof(numTextureIndices)) != SUCCESS) {
goto done;
}
if (numTextureIndices > 0) {
textureIndices = new LegoU32[numPolys & MAXWORD][_countof(*textureIndices)];
if (p_storage->Read(textureIndices, (numPolys & MAXWORD) * sizeof(*textureIndices)) != SUCCESS) {
goto done;
}
}
else {
textureIndices = NULL;
}
mesh = new LegoMesh();
if (mesh->Read(p_storage) != SUCCESS) {
goto done;
}
switch (mesh->GetShading()) {
case LegoMesh::e_flat:
shadingModel = Tgl::Flat;
break;
case LegoMesh::e_wireframe:
shadingModel = Tgl::Wireframe;
break;
default:
shadingModel = Tgl::Gouraud;
}
m_numVertices += numVertices & MAXWORD;
textureName = mesh->GetTextureName();
materialName = mesh->GetMaterialName();
if (FUN_100aae20(textureName) || FUN_100aae20(materialName)) {
meshIndex = meshUnd1;
meshUnd1--;
}
else {
meshIndex = meshUnd2;
meshUnd2++;
}
m_meshes[meshIndex].m_tglMesh = m_unk0x04->CreateMesh(
numPolys & MAXWORD,
numVertices & MAXWORD,
vertices,
normals,
textureVertices,
polyIndices,
textureIndices,
shadingModel
);
if (m_meshes[meshIndex].m_tglMesh == NULL) {
goto done;
}
m_meshes[meshIndex].m_tglMesh->SetShadingModel(shadingModel);
if (textureName != NULL) {
if (mesh->GetUnknown0x21()) {
LegoROI::FUN_100a9cf0(textureName, paletteEntries, _countof(paletteEntries));
}
textureInfo = p_textureContainer->Get(mesh->GetTextureName());
if (textureInfo == NULL) {
goto done;
}
m_meshes[meshIndex].m_tglMesh->SetColor(1.0F, 1.0F, 1.0F, 0.0F);
SetGroupTexture(m_meshes[meshIndex].m_tglMesh, textureInfo);
m_meshes[meshIndex].m_unk0x04 = TRUE;
}
else {
LegoFloat red = 1.0F;
LegoFloat green = 0.0F;
LegoFloat blue = 1.0F;
LegoFloat alpha = 0.0F;
if (mesh->GetUnknown0x21()) {
LegoROI::FUN_100a9bf0(materialName, red, green, blue, alpha);
}
else {
red = mesh->GetColor().GetRed() / 255.0;
green = mesh->GetColor().GetGreen() / 255.0;
blue = mesh->GetColor().GetBlue() / 255.0;
alpha = mesh->GetAlpha();
}
m_meshes[meshIndex].m_tglMesh->SetColor(red, green, blue, alpha);
}
if (mesh->GetUnknown0x0d() > 0) {
IDirect3DRMMesh* mesh;
D3DRMGROUPINDEX index;
GetMeshData(mesh, index, m_meshes[meshIndex].m_tglMesh);
mesh->SetGroupMaterial(index, g_unk0x101013d4);
}
if (mesh != NULL) {
delete mesh;
mesh = NULL;
}
if (polyIndices != NULL) {
delete[] polyIndices;
polyIndices = NULL;
}
if (textureIndices != NULL) {
delete[] textureIndices;
textureIndices = NULL;
}
}
m_unk0x1c = meshUnd2;
if (textureVertices != NULL) {
delete[] textureVertices;
}
if (normals != NULL) {
delete[] normals;
}
if (vertices != NULL) {
delete[] vertices;
}
return SUCCESS;
done:
if (normals != NULL) {
delete[] normals;
}
if (vertices != NULL) {
delete[] vertices;
}
if (textureVertices != NULL) {
delete[] textureVertices;
}
if (mesh != NULL) {
delete mesh;
}
if (polyIndices != NULL) {
delete[] polyIndices;
}
if (textureIndices != NULL) {
delete[] textureIndices;
}
return FAILURE;
}
// STUB: LEGO1 0x100aae20
LegoBool LegoLOD::FUN_100aae20(const LegoChar*)
{
// TODO
return FALSE;
}
inline BOOL GetMeshData(IDirect3DRMMesh*& mesh, D3DRMGROUPINDEX& index, Tgl::Mesh* pMesh)
{
mesh = ((TglImpl::MeshImpl*) pMesh)->ImplementationData()->groupMesh;
index = ((TglImpl::MeshImpl*) pMesh)->ImplementationData()->groupIndex;
return FALSE;
}
inline IDirect3DRM2* GetD3DRM(Tgl::Renderer* pRenderer)
{
return ((TglImpl::RendererImpl*) pRenderer)->ImplementationData();
}

View File

@@ -0,0 +1,45 @@
#ifndef LEGOLOD_H
#define LEGOLOD_H
#include "misc/legotypes.h"
#include "viewmanager/viewlod.h"
class LegoTextureContainer;
struct LegoTextureInfo;
class LegoStorage;
// VTABLE: LEGO1 0x100dbf10
// SIZE 0x20
class LegoLOD : public ViewLOD {
public:
// SIZE 0x08
struct Mesh {
Tgl::Mesh* m_tglMesh; // 0x00
BOOL m_unk0x04; // 0x04
};
LegoLOD(Tgl::Renderer*);
~LegoLOD() override;
// FUNCTION: LEGO1 0x100aae70
int NumPolys() const override { return m_numPolys; } // vtable+0x0c
// FUNCTION: LEGO1 0x100aae80
float VTable0x10() override { return 0.0; } // vtable+0x10
LegoResult Read(Tgl::Renderer*, LegoTextureContainer* p_textureContainer, LegoStorage* p_storage);
static LegoBool FUN_100aae20(const LegoChar*);
// SYNTHETIC: LEGO1 0x100aa430
// LegoLOD::`scalar deleting destructor'
protected:
Mesh* m_meshes; // 0x0c
LegoU32 m_numMeshes; // 0x10
LegoU32 m_numVertices; // 0x14
LegoU32 m_numPolys; // 0x18
undefined4 m_unk0x1c; // 0x1c
};
#endif // LEGOLOD_H

View File

@@ -2,20 +2,15 @@
#include "geom/legobox.h"
#include "geom/legosphere.h"
#include "legolod.h"
#include "misc/legocontainer.h"
#include "misc/legostorage.h"
#include "tgl/d3drm/impl.h"
#include <string.h>
#include <vec.h>
DECOMP_SIZE_ASSERT(LegoROI, 0x108)
DECOMP_SIZE_ASSERT(TimeROI, 0x10c)
DECOMP_SIZE_ASSERT(LODObject, 0x04)
DECOMP_SIZE_ASSERT(ViewLOD, 0x0c)
DECOMP_SIZE_ASSERT(LegoLOD, 0x20)
inline IDirect3DRM2* GetD3DRM(Tgl::Renderer* pRenderer);
// SIZE 0x14
typedef struct {
@@ -23,7 +18,7 @@ typedef struct {
int m_red;
int m_green;
int m_blue;
int m_unk0x10;
int m_alpha;
} ROIColorAlias;
// GLOBAL: LEGO1 0x100dbe28
@@ -59,9 +54,6 @@ const char* g_unk0x10101390[] = {"rcuser", "jsuser", "dunebugy", "chtrblad", "ch
// GLOBAL: LEGO1 0x101013ac
ROIHandler g_unk0x101013ac = NULL;
// GLOBAL: LEGO1 0x101013d4
LPDIRECT3DRMMATERIAL g_unk0x101013d4 = NULL;
// FUNCTION: LEGO1 0x100a81c0
void LegoROI::configureLegoROI(int p_roiConfig)
{
@@ -251,7 +243,7 @@ LegoResult LegoROI::Read(
}
if (j == 0) {
if (surplusLODs != 0 && lod->GetUnknown0x08Test()) {
if (surplusLODs != 0 && lod->GetUnknown0x08Test8()) {
numLODs++;
}
}
@@ -268,7 +260,7 @@ LegoResult LegoROI::Read(
}
if (i == 0) {
if (surplusLODs != 0 && lod->GetUnknown0x08Test()) {
if (surplusLODs != 0 && lod->GetUnknown0x08Test8()) {
numLODs++;
}
}
@@ -306,9 +298,9 @@ LegoResult LegoROI::Read(
LegoFloat red = 1.0F;
LegoFloat green = 0.0F;
LegoFloat blue = 1.0F;
LegoFloat other = 0.0F;
FUN_100a9bf0(textureName, red, green, blue, other);
FUN_100a9170(red, green, blue, other);
LegoFloat alpha = 0.0F;
FUN_100a9bf0(textureName, red, green, blue, alpha);
FUN_100a9170(red, green, blue, alpha);
}
}
@@ -364,7 +356,7 @@ TimeROI::TimeROI(Tgl::Renderer* p_renderer, ViewLODList* p_lodList, LegoTime p_t
}
// FUNCTION: LEGO1 0x100a9bf0
unsigned char LegoROI::FUN_100a9bf0(const char* p_param, float& p_red, float& p_green, float& p_blue, float& p_other)
LegoBool LegoROI::FUN_100a9bf0(const LegoChar* p_param, float& p_red, float& p_green, float& p_blue, float& p_alpha)
{
// TODO
if (p_param == NULL) {
@@ -378,17 +370,11 @@ unsigned char LegoROI::FUN_100a9bf0(const char* p_param, float& p_red, float& p_
}
}
return ColorAliasLookup(p_param, p_red, p_green, p_blue, p_other);
return ColorAliasLookup(p_param, p_red, p_green, p_blue, p_alpha);
}
// FUNCTION: LEGO1 0x100a9c50
unsigned char LegoROI::ColorAliasLookup(
const char* p_param,
float& p_red,
float& p_green,
float& p_blue,
float& p_other
)
LegoBool LegoROI::ColorAliasLookup(const LegoChar* p_param, float& p_red, float& p_green, float& p_blue, float& p_alpha)
{
// TODO: this seems awfully hacky for these devs. is there a dynamic way
// to represent `the end of this array` that would improve this?
@@ -398,7 +384,7 @@ unsigned char LegoROI::ColorAliasLookup(
p_red = g_roiColorAliases[i].m_red * g_normalizeByteToFloat;
p_green = g_roiColorAliases[i].m_green * g_normalizeByteToFloat;
p_blue = g_roiColorAliases[i].m_blue * g_normalizeByteToFloat;
p_other = g_roiColorAliases[i].m_unk0x10 * g_normalizeByteToFloat;
p_alpha = g_roiColorAliases[i].m_alpha * g_normalizeByteToFloat;
return TRUE;
}
i++;
@@ -407,6 +393,13 @@ unsigned char LegoROI::ColorAliasLookup(
return FALSE;
}
// STUB: LEGO1 0x100a9cf0
LegoBool LegoROI::FUN_100a9cf0(const LegoChar* p_param, unsigned char* paletteEntries, LegoU32 p_numEntries)
{
// TODO
return FALSE;
}
// FUNCTION: LEGO1 0x100a9d30
void LegoROI::FUN_100a9d30(ROIHandler p_func)
{
@@ -430,35 +423,3 @@ void LegoROI::UpdateWorldBoundingVolumes()
{
// TODO
}
// FUNCTION: LEGO1 0x100aa380
LegoLOD::LegoLOD(Tgl::Renderer* p_renderer) : ViewLOD(p_renderer)
{
if (g_unk0x101013d4 == NULL) {
GetD3DRM(p_renderer)->CreateMaterial(10.0, &g_unk0x101013d4);
}
m_unk0x0c = 0;
m_unk0x10 = 0;
m_unk0x14 = 0;
m_numPolys = 0;
m_unk0x1c = 0;
}
// STUB: LEGO1 0x100aa450
LegoLOD::~LegoLOD()
{
// TODO
}
// STUB: LEGO1 0x100aa510
LegoResult LegoLOD::Read(Tgl::Renderer* p_renderer, LegoTextureContainer* p_textureContainer, LegoStorage* p_storage)
{
// TODO
return SUCCESS;
}
inline IDirect3DRM2* GetD3DRM(Tgl::Renderer* pRenderer)
{
return ((TglImpl::RendererImpl*) pRenderer)->ImplementationData();
}

View File

@@ -2,7 +2,6 @@
#define LEGOROI_H
#include "misc/legotypes.h"
#include "viewmanager/viewlod.h"
#include "viewmanager/viewroi.h"
typedef unsigned char (*ROIHandler)(const char*, char*, unsigned int);
@@ -13,33 +12,6 @@ struct LegoTextureInfo;
class LegoStorage;
class LegoAnim;
// VTABLE: LEGO1 0x100dbf10
// SIZE 0x20
class LegoLOD : public ViewLOD {
public:
LegoLOD(Tgl::Renderer*);
~LegoLOD() override;
// FUNCTION: LEGO1 0x100aae70
int NumPolys() const override { return m_numPolys; } // vtable+0x0c
// FUNCTION: LEGO1 0x100aae80
float VTable0x10() override { return 0.0; } // vtable+0x10
LegoResult Read(Tgl::Renderer*, LegoTextureContainer* p_textureContainer, LegoStorage* p_storage);
// SYNTHETIC: LEGO1 0x100aa430
// LegoLOD::`scalar deleting destructor'
protected:
// TODO: Review 1996 version
undefined4 m_unk0x0c; // 0x0c
undefined4 m_unk0x10; // 0x10
undefined4 m_unk0x14; // 0x14
LegoU32 m_numPolys; // 0x18
undefined4 m_unk0x1c; // 0x1c
};
// VTABLE: LEGO1 0x100dbe38
// SIZE 0x108
class LegoROI : public ViewROI {
@@ -66,14 +38,15 @@ public:
static void configureLegoROI(int p_roi);
static void FUN_100a9d30(ROIHandler p_func);
static unsigned char FUN_100a9bf0(const char* p_param, float& p_red, float& p_green, float& p_blue, float& p_other);
static unsigned char ColorAliasLookup(
const char* p_param,
static LegoBool FUN_100a9bf0(const LegoChar* p_param, float& p_red, float& p_green, float& p_blue, float& p_alpha);
static LegoBool ColorAliasLookup(
const LegoChar* p_param,
float& p_red,
float& p_green,
float& p_blue,
float& p_other
float& p_alpha
);
static LegoBool FUN_100a9cf0(const LegoChar* p_param, unsigned char* paletteEntries, LegoU32 p_numEntries);
inline const LegoChar* GetName() const { return m_name; }
inline LegoEntity* GetEntity() { return m_entity; }