Skip to content

Commit

Permalink
Merge pull request #107 from MilchRatchet/dev-branch
Browse files Browse the repository at this point in the history
Fixed some bugs.
  • Loading branch information
MilchRatchet authored Sep 4, 2024
2 parents d1c0f23 + 92ab5f0 commit b9ca5be
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 40 deletions.
7 changes: 4 additions & 3 deletions LibReplanetizer/Level Objects/Gameplay/Moby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,11 @@ public void UpdateFromMemory(byte[] mobyMemory, int offset, List<Model> models)
modelMatrix.M32 = memory.transformation.M32 * memory.scale;
modelMatrix.M33 = memory.transformation.M33 * memory.scale;
modelMatrix.M34 = 0.0f;
modelMatrix.M41 = memory.transformation.M14 + position.X;
modelMatrix.M42 = memory.transformation.M24 + position.Y;
modelMatrix.M43 = memory.transformation.M34 + position.Z;
modelMatrix.M41 = memory.transformation.M14 + memory.position.X;
modelMatrix.M42 = memory.transformation.M24 + memory.position.Y;
modelMatrix.M43 = memory.transformation.M34 + memory.position.Z;
modelMatrix.M44 = 1.0f;

position = modelMatrix.ExtractTranslation();
rotation = modelMatrix.ExtractRotation();
scale = modelMatrix.ExtractScale();
Expand Down
6 changes: 3 additions & 3 deletions LibReplanetizer/LibReplanetizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.90.1.1" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="ImGui.NET" Version="1.91.0.1" />
<PackageReference Include="NLog" Version="5.3.3" />
<PackageReference Include="OpenTK.Mathematics" Version="4.8.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
</ItemGroup>

Expand Down
8 changes: 8 additions & 0 deletions LibReplanetizer/Models/Animation/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ public Frame(FileStream fs, GameType game, int offset, int boneCount)
// Constructor for Deadlocked
public Frame(byte[] frameBlock, int offset, int numRotations, int numScalings, int numTranslations)
{
// This code is not correct and is causing crashes.
// TODO: (Milch) Implement support for Deadlocked animations.
#if true
rotations = new List<FrameBoneRotation>();
scalings = new List<FrameBoneScaling>();
translations = new List<FrameBoneTranslation>();
#else
int rotationOffset = 0;
rotations = new List<FrameBoneRotation>();
for (int i = 0; i < numRotations; i++)
Expand Down Expand Up @@ -284,6 +291,7 @@ public Frame(byte[] frameBlock, int offset, int numRotations, int numScalings, i
byte bone = frameBlock[offset + translationOffset + i * 8 + 0x07];
translations.Add(new FrameBoneTranslation(x, y, z, bone, unk));
}
#endif
}

public byte[] Serialize()
Expand Down
8 changes: 4 additions & 4 deletions LibReplanetizer/Serializers/Exporters/ColladaExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,10 @@ private void WriteData(string fileName, Level level, Model model, bool includeSk
for (int x = 0; x < vertexCount; x++)
{
byte[] colors = BitConverter.GetBytes(model.vertexBuffer[(x * bufferStride) + vcOffset + 0x00]);
float a = ((float) colors[0]) / 255.0f;
float b = ((float) colors[1]) / 255.0f;
float g = ((float) colors[2]) / 255.0f;
float r = ((float) colors[3]) / 255.0f;
float a = ((float) colors[3]) / 255.0f;
float b = ((float) colors[2]) / 255.0f;
float g = ((float) colors[1]) / 255.0f;
float r = ((float) colors[0]) / 255.0f;
colladaStream.Write(r.ToString("G", en_US) + " " + g.ToString("G", en_US) + " " + b.ToString("G", en_US) + " " + a.ToString("G", en_US) + " ");
}
}
Expand Down
8 changes: 4 additions & 4 deletions LibReplanetizer/Serializers/Exporters/GLTFExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,10 @@ public GLTFDataObject(Level level, Model model, bool includeSkeleton, ExporterMo
for (int i = 0; i < model.vertexCount; i++)
{
byte[] colors = BitConverter.GetBytes(model.vertexBuffer[(i * model.vertexStride) + vcOffset + 0x00]);
float a = ((float) colors[0]) / 255.0f;
float b = ((float) colors[1]) / 255.0f;
float g = ((float) colors[2]) / 255.0f;
float r = ((float) colors[3]) / 255.0f;
float r = ((float) colors[0]) / 255.0f;
float g = ((float) colors[1]) / 255.0f;
float b = ((float) colors[2]) / 255.0f;
float a = ((float) colors[3]) / 255.0f;
vertexColors[i] = new Vector4(r, g, b, a);
}
}
Expand Down
23 changes: 20 additions & 3 deletions LibReplanetizer/Serializers/Exporters/WavefrontExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ private class VertexColorContainer
Vector3 staticColor = new Vector3();
bool colorIsPerVertex = false;

public VertexColorContainer(SkyboxModel skybox)
{
colorIsPerVertex = true;

int vertexCount = skybox.vertexBuffer.Length / 0x06;
for (int i = 0; i < vertexCount; i++)
{
byte[] colors = BitConverter.GetBytes(skybox.vertexBuffer[i * skybox.vertexStride + 0x05]);
float r = ((float) colors[0]) / 255.0f;
float g = ((float) colors[1]) / 255.0f;
float b = ((float) colors[2]) / 255.0f;
vertexColors.Add(new Vector3(r, g, b));
}
}

public VertexColorContainer(ModelObject t)
{
if (t is TerrainFragment || t is Tie)
Expand Down Expand Up @@ -201,7 +216,7 @@ private int WriteData(StreamWriter objfs, Model? model, int faceOffset, Matrix4
{
if (model == null) return 0;

// skybox model has no normals and does the vertex buffer has a different layout
// skybox model has no normals and thus the vertex buffer has a different layout
// if we see other cases like this, it may be advisable to generalize this
bool skyboxModel = (model is SkyboxModel);

Expand All @@ -222,7 +237,7 @@ private int WriteData(StreamWriter objfs, Model? model, int faceOffset, Matrix4
Vector3 pos = (new Vector4(px, py, pz, 1.0f) * modelMatrix).Xyz;
ChangeOrientation(ref pos, modelSettings.orientation);
vertices[vertIdx] = pos;
if (levelSettings.writeColors && vColors != null)
if ((levelSettings.writeColors || modelSettings.extendedFeatures) && vColors != null)
{
Vector3 color = vColors.GetColor(vertIdx);
objfs.WriteLine("v " + pos.X.ToString("G", en_US) + " " + pos.Y.ToString("G", en_US) + " " + pos.Z.ToString("G", en_US) + " " + color.X.ToString("G", en_US) + " " + color.Y.ToString("G", en_US) + " " + color.Z.ToString("G", en_US));
Expand Down Expand Up @@ -324,7 +339,9 @@ public override void ExportModel(string fileName, Level level, Model model, List

Matrix4 scale = Matrix4.CreateScale(model.size);

WriteData(objfs, model, 0, scale);
VertexColorContainer? vColors = (model is SkyboxModel) ? new VertexColorContainer((SkyboxModel)model) : null;

WriteData(objfs, model, 0, scale, vColors);
}
}

Expand Down
11 changes: 5 additions & 6 deletions Replanetizer/Frames/LevelFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,10 @@ private void UpdateWindowSize()
{
int prevWidth = width, prevHeight = height;

System.Numerics.Vector2 vMin = ImGui.GetWindowContentRegionMin();
System.Numerics.Vector2 vMax = ImGui.GetWindowContentRegionMax();
System.Numerics.Vector2 avail = ImGui.GetContentRegionAvail();

width = (int) (vMax.X - vMin.X);
height = (int) (vMax.Y - vMin.Y);
width = (int) avail.X;
height = (int) avail.Y;

if (width <= 0 || height <= 0) return;

Expand All @@ -456,8 +455,8 @@ private void UpdateWindowSize()
OnResize();
}

System.Numerics.Vector2 windowPos = ImGui.GetWindowPos();
Vector2 windowZero = new Vector2(windowPos.X + vMin.X, windowPos.Y + vMin.Y);
System.Numerics.Vector2 cursorScreenPos = ImGui.GetCursorScreenPos();
Vector2 windowZero = new Vector2(cursorScreenPos.X, cursorScreenPos.Y);
mousePos = wnd.MousePosition - windowZero;
contentRegion = new Rectangle((int) windowZero.X, (int) windowZero.Y, width, height);
}
Expand Down
36 changes: 24 additions & 12 deletions Replanetizer/Frames/ModelFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ public override void Render(float deltaTime)
// Collada specific settings
if (exportSettings.format == ExporterModelSettings.Format.Collada)
{
if (selectedModel is MobyModel mobyModel && mobyModel.animations.Count > 0)
// TODO: (Milch) Enable animations for Deadlocked once they are implemented.
if (level.game != GameType.DL && selectedModel is MobyModel mobyModel && mobyModel.animations.Count > 0)
{
int animationChoice = (int) exportSettings.animationChoice;
if (ImGui.Combo("Animations", ref animationChoice, ExporterModelSettings.ANIMATION_CHOICE_STRINGS, ExporterModelSettings.ANIMATION_CHOICE_STRINGS.Length - 1))
Expand All @@ -378,6 +379,19 @@ public override void Render(float deltaTime)
{
ImGui.Checkbox("Include MTL File", ref exportSettings.exportMtlFile);
ImGui.Checkbox("Extended Features", ref exportSettings.extendedFeatures);

// TODO: (Milch) Write a separate function for this so we can start using tooltips in more places without copy pasting the code all the time.
ImGui.SameLine();
ImGui.TextDisabled("(?)");
if (ImGui.IsItemHovered())
{
ImGui.BeginTooltip();
ImGui.PushTextWrapPos(ImGui.GetFontSize() * 40.0f);
ImGui.TextUnformatted("Enables UV clamping modes and vertex colors.");
ImGui.PopTextWrapPos();
ImGui.EndTooltip();
}

int orientation = (int) exportSettings.orientation;
if (ImGui.Combo("Orientation", ref orientation, ExporterModelSettings.ORIENTATION_STRINGS, ExporterModelSettings.ORIENTATION_STRINGS.Length))
{
Expand All @@ -388,7 +402,8 @@ public override void Render(float deltaTime)
// glTF specific settings
if (exportSettings.format == ExporterModelSettings.Format.glTF)
{
if (selectedModel is MobyModel mobyModel && mobyModel.animations.Count > 0)
// TODO: (Milch) Enable animations for Deadlocked once they are implemented.
if (level.game != GameType.DL && selectedModel is MobyModel mobyModel && mobyModel.animations.Count > 0)
{
bool includeAnimations = (exportSettings.animationChoice != ExporterModelSettings.AnimationChoice.None);

Expand Down Expand Up @@ -421,7 +436,8 @@ public override void Render(float deltaTime)

ImGui.Separator();

if (selectedModel is MobyModel mobModel && mobModel.animations.Count > 0)
// TODO: (Milch) Enable animations for Deadlocked once they are implemented.
if (level.game != GameType.DL && selectedModel is MobyModel mobModel && mobModel.animations.Count > 0)
{
ImGui.Checkbox("Show Animations", ref rendererPayload.visibility.enableAnimations);

Expand Down Expand Up @@ -463,14 +479,10 @@ private void UpdateWindowSize()
{
int prevWidth = width, prevHeight = height;

System.Numerics.Vector2 vMin = ImGui.GetWindowContentRegionMin();
System.Numerics.Vector2 vMax = ImGui.GetWindowContentRegionMax();

vMin.X += 250;
vMax.X -= PROPERTIES_WIDTH - 20;
System.Numerics.Vector2 avail = ImGui.GetContentRegionAvail();

width = (int) (vMax.X - vMin.X);
height = (int) (vMax.Y - vMin.Y);
width = (int) avail.X - PROPERTIES_WIDTH - 230;
height = (int) avail.Y;

if (width <= 0 || height <= 0)
{
Expand All @@ -484,8 +496,8 @@ private void UpdateWindowSize()
OnResize();
}

System.Numerics.Vector2 windowPos = ImGui.GetWindowPos();
Vector2 windowZero = new Vector2(windowPos.X + vMin.X, windowPos.Y + vMin.Y);
System.Numerics.Vector2 cursorScreenPos = ImGui.GetCursorScreenPos();
Vector2 windowZero = new Vector2(cursorScreenPos.X, cursorScreenPos.Y);
mousePos = wnd.MousePosition - windowZero;
contentRegion = new Rectangle((int) windowZero.X, (int) windowZero.Y, width, height);
}
Expand Down
2 changes: 1 addition & 1 deletion Replanetizer/Frames/TextureFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TextureFrame(Window wnd, LevelFrame levelFrame) : base(wnd, levelFrame)

public static void RenderTextureList(List<Texture> textures, float itemSizeX, Dictionary<Texture, GLTexture> textureIds, string prefix = "", int additionalOffset = 0)
{
var width = ImGui.GetWindowContentRegionMax().X - ImGui.GetWindowContentRegionMin().X - additionalOffset;
var width = ImGui.GetContentRegionAvail().X - additionalOffset;
var itemsPerRow = (int) Math.Floor(width / itemSizeX);

if (itemsPerRow == 0) return;
Expand Down
6 changes: 3 additions & 3 deletions Replanetizer/Replanetizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.90.1.1" />
<PackageReference Include="ImGui.NET" Version="1.91.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.2.8" />
<PackageReference Include="NLog" Version="5.3.3" />
<PackageReference Include="OpenTK" Version="4.8.2" />
<PackageReference Include="OpenTK.Mathematics" Version="4.8.2" />
<PackageReference Include="OpenTK.Windowing.GraphicsLibraryFramework" Version="4.8.2" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 10 additions & 1 deletion Replanetizer/Utils/TextureIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ public static void ExportTexture(Texture texture, string path, bool includeTrans
image.SaveAsJpeg(path);
break;
default:
image.SaveAsPng(path);
PngEncoder pngEncoder = new PngEncoder() {
Gamma = 1.0f / 2.2f, // Textures are sRGB encoded
ColorType = PngColorType.RgbWithAlpha,
TransparentColorMode = PngTransparentColorMode.Preserve, // Some textures in RaC 3 are fully transparent but are rendered without transparency.
BitDepth = PngBitDepth.Bit8,
InterlaceMethod = PngInterlaceMode.None, // Interlacing may not be supported by all PNG importers.
FilterMethod = PngFilterMethod.Paeth, // Combined filter method may not be supported by all PNG importers.
CompressionLevel = PngCompressionLevel.BestCompression // Textures are small so compression speed is not a huge issue.
};
image.SaveAsPng(path, pngEncoder);
break;
}
}
Expand Down

0 comments on commit b9ca5be

Please sign in to comment.