From 1532a2750cd39e29fc747097b675fde0d19a5ce9 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 7 Dec 2024 18:28:39 +0000 Subject: [PATCH] Almost there object casting --- KeepersCompound.Lightmapper/LightMapper.cs | 98 ++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/KeepersCompound.Lightmapper/LightMapper.cs b/KeepersCompound.Lightmapper/LightMapper.cs index be58999..5a6ee79 100644 --- a/KeepersCompound.Lightmapper/LightMapper.cs +++ b/KeepersCompound.Lightmapper/LightMapper.cs @@ -119,6 +119,8 @@ public class LightMapper var vertices = new List(); var indices = new List(); + + // Worldrep mesh foreach (var cell in worldRep.Cells) { var numPolys = cell.PolyCount; @@ -169,6 +171,102 @@ public class LightMapper } } + // Object meshes?? + // TODO: Should this throw? + if (!_mission.TryGetChunk("BRLIST", out var brList)) + { + return null; + } + + foreach (var brush in brList.Brushes) + { + if (brush.media != BrList.Brush.Media.Object) + { + continue; + } + + var id = (int)brush.brushInfo; + var modelNameProp = _hierarchy.GetProperty(id, "P$ModelName"); + var scaleProp = _hierarchy.GetProperty(id, "P$Scale"); + var renderTypeProp = _hierarchy.GetProperty(id, "P$RenderTyp"); + var jointPosProp = _hierarchy.GetProperty(id, "P$JointPos"); + var immobileProp = _hierarchy.GetProperty(id, "P$Immobile"); + var staticShadowProp = _hierarchy.GetProperty(id, "P$StatShad"); + + var joints = jointPosProp?.Positions ?? [0, 0, 0, 0, 0, 0]; + var castsShadows = (immobileProp?.value ?? false) || (staticShadowProp?.value ?? false); + var renderMode = renderTypeProp?.mode ?? PropRenderType.Mode.Normal; + + // TODO: Check which rendermodes cast shadows :) + if (modelNameProp == null || !castsShadows || renderMode != PropRenderType.Mode.Normal) + { + continue; + } + + // Let's try and place an object :) + var modelName = modelNameProp.value.ToLower() + ".bin"; + var modelPath = _campaign.GetResourcePath(ResourceType.Object, modelName); + if (modelPath == null) + { + continue; + } + + // TODO: Handle failing to find model more gracefully + var pos = brush.position; + var rot = brush.angle; + var scale = scaleProp?.value ?? Vector3.One; + var model = new ModelFile(modelPath); + pos -= model.Header.Center; + + // for each object modify the vertices + // TODO: Something about the transform is fucky + foreach (var subObj in model.Objects) + { + Console.WriteLine($"ID: {subObj.Joint}, l: {joints.Length}"); + var jointTrans = Matrix4x4.Identity; + if (subObj.Joint != -1) + { + var ang = float.DegreesToRadians(joints[subObj.Joint]); + var jointRot = Matrix4x4.CreateFromYawPitchRoll(0, ang, 0); + var objTrans = subObj.Transform; + jointTrans = jointRot * objTrans; + } + var scalePart = Matrix4x4.CreateScale(scale); + var rotPart = Matrix4x4.CreateFromYawPitchRoll(float.DegreesToRadians(rot.X), + float.DegreesToRadians(rot.Y), float.DegreesToRadians(rot.Z)); + var transPart = Matrix4x4.CreateTranslation(pos); + var transform = jointTrans * scalePart * rotPart * transPart; + + var start = subObj.PointIdx; + var end = start + subObj.PointCount; + for (var i = start; i < end; i++) + { + var v = model.Vertices[i]; + model.Vertices[i] = Vector3.Transform(v, transform); + + Console.WriteLine($"Raw: {v}, Pos: {model.Vertices[i]}"); + } + } + + // for each polygon slam it's vertices and indices :) + foreach (var poly in model.Polygons) + { + var indexOffset = vertices.Count; + foreach (var idx in poly.VertexIndices) + { + vertices.Add(model.Vertices[idx]); + } + + for (int i = 1; i < poly.VertexCount - 1; i++) + { + indices.Add(indexOffset); + indices.Add(indexOffset + i); + indices.Add(indexOffset + i + 1); + _triangleTypeMap.Add(SurfaceType.Solid); + } + } + } + var rt = new Raytracer(); rt.AddMesh(new TriangleMesh([.. vertices], [.. indices])); rt.CommitScene();