From 0da677980f8b7d6f6fe7e5eef9729419009a1737 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Sun, 20 Oct 2019 16:41:58 -0400 Subject: [PATCH] Handle all rotations from matrix in shaders so they render accurate. --- .../FileFormats/Layout/BxlytShader.cs | 7 ++++++ .../FileFormats/Layout/CAFE/BflytShader.cs | 21 +--------------- .../FileFormats/Layout/Common.cs | 13 ++++++++++ File_Format_Library/GUI/BFLYT/LayoutViewer.cs | 24 +++++++++---------- .../Forms/Animation/Rewrite/AnimationPanel.cs | 5 ++-- 5 files changed, 36 insertions(+), 34 deletions(-) diff --git a/File_Format_Library/FileFormats/Layout/BxlytShader.cs b/File_Format_Library/FileFormats/Layout/BxlytShader.cs index 48f037fa..1a53980a 100644 --- a/File_Format_Library/FileFormats/Layout/BxlytShader.cs +++ b/File_Format_Library/FileFormats/Layout/BxlytShader.cs @@ -71,6 +71,13 @@ namespace LayoutBXLYT } } + //For non material panes + public void SetBasic(BasePane pane) + { + var rotationMatrix = pane.GetRotationMatrix(); + SetMatrix("rotationMatrix", ref rotationMatrix); + } + public void SetVec4(string name, Vector4 value) { if (uniforms.ContainsKey(name)) diff --git a/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs b/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs index 1acb196e..0dc48490 100644 --- a/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs +++ b/File_Format_Library/FileFormats/Layout/CAFE/BflytShader.cs @@ -47,26 +47,7 @@ namespace LayoutBXLYT public void SetMaterials(BasePane pane, Dictionary textures) { - var paneRotate = pane.GetRotation(); - - Matrix4 rotationX = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(paneRotate.X)); - Matrix4 rotationY = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(paneRotate.Y)); - Matrix4 rotationZ = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(paneRotate.Z)); - var rotationMatrix = rotationX * rotationY * rotationZ; - - //Get parent rotation if using material rotation transforms - //If it's a null pane, it would rotate automatically with GL.Rotate - if (pane.Parent is IPicturePane || pane.Parent is IWindowPane) - { - var parentRotate = pane.Parent.GetRotation(); - - Matrix4 parentRotationX = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(parentRotate.X)); - Matrix4 parentRotationY = Matrix4.CreateRotationY(MathHelper.DegreesToRadians(parentRotate.Y)); - Matrix4 parentRotationZ = Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(parentRotate.Z)); - var parentRotationMatrix = parentRotationX * parentRotationY * parentRotationZ; - rotationMatrix = parentRotationMatrix * rotationMatrix; - } - + var rotationMatrix = pane.GetRotationMatrix(); SetMatrix("rotationMatrix", ref rotationMatrix); STColor8 WhiteColor = material.WhiteColor; diff --git a/File_Format_Library/FileFormats/Layout/Common.cs b/File_Format_Library/FileFormats/Layout/Common.cs index a35f0d3d..e6d941e6 100644 --- a/File_Format_Library/FileFormats/Layout/Common.cs +++ b/File_Format_Library/FileFormats/Layout/Common.cs @@ -131,6 +131,19 @@ namespace LayoutBXLYT get { return Childern.Count > 0; } } + public OpenTK.Matrix4 GetRotationMatrix() + { + var paneRotate = GetRotation(); + OpenTK.Matrix4 rotationX = OpenTK.Matrix4.CreateRotationX(OpenTK.MathHelper.DegreesToRadians(paneRotate.X)); + OpenTK.Matrix4 rotationY = OpenTK.Matrix4.CreateRotationY(OpenTK.MathHelper.DegreesToRadians(paneRotate.Y)); + OpenTK.Matrix4 rotationZ = OpenTK.Matrix4.CreateRotationZ(OpenTK.MathHelper.DegreesToRadians(paneRotate.Z)); + var rotationMatrix = rotationX * rotationY * rotationZ; + + if (Parent != null) + rotationMatrix = Parent.GetRotationMatrix() * rotationMatrix; + return rotationMatrix; + } + public Vector3F GetTranslation() { var posX = Translate.X; diff --git a/File_Format_Library/GUI/BFLYT/LayoutViewer.cs b/File_Format_Library/GUI/BFLYT/LayoutViewer.cs index 255db6ba..d0976651 100644 --- a/File_Format_Library/GUI/BFLYT/LayoutViewer.cs +++ b/File_Format_Library/GUI/BFLYT/LayoutViewer.cs @@ -443,19 +443,9 @@ namespace LayoutBXLYT } + //Note rotation matrix done by shaders + GL.Translate(translate.X, translate.Y, 0); - - //Rotate normally unless the object uses shaders/materials - //Rotation matrix + shaders works accurately with X/Y rotation axis - //Todo, do everything by shaders - bool HasMaterials = pane is IWindowPane || pane is IPicturePane; - if (!HasMaterials) - { - GL.Rotate(rotate.X, 1, 0, 0); - GL.Rotate(rotate.Y, 0, 1, 0); - GL.Rotate(rotate.Z, 0, 0, 1); - } - GL.Scale(scale.X, scale.Y, 1); byte alpha = pane.Alpha; @@ -479,7 +469,12 @@ namespace LayoutBXLYT else if (pane is IWindowPane) BxlytToGL.DrawWindowPane(pane, GameWindow, effectiveAlpha, Textures, isSelected); else if (pane is IBoundryPane) + { + shader.Enable(); + shader.SetBasic(pane); BxlytToGL.DrawBoundryPane(pane, GameWindow, effectiveAlpha, isSelected); + shader.Disable(); + } else if (pane is ITextPane && Runtime.LayoutEditor.DisplayTextPane) { var textPane = (ITextPane)pane; @@ -553,6 +548,9 @@ namespace LayoutBXLYT if (!Runtime.LayoutEditor.DisplayNullPane || GameWindow || Runtime.LayoutEditor.IsGamePreview) return; + shader.Enable(); + shader.SetBasic(pane); + Vector2[] TexCoords = new Vector2[] { new Vector2(1,1), new Vector2(0,1), @@ -572,6 +570,8 @@ namespace LayoutBXLYT }; BxlytToGL.DrawRectangle(pane, GameWindow, pane.Rectangle, TexCoords, Colors, true, 255, isSelectionBox); + + shader.Disable(); } private void DrawPartsPane(BxlytShader shader, BFLYT.PRT1 pane, byte effectiveAlpha,bool isSelected, bool parentInfluenceAlpha) diff --git a/Switch_Toolbox_Library/Forms/Animation/Rewrite/AnimationPanel.cs b/Switch_Toolbox_Library/Forms/Animation/Rewrite/AnimationPanel.cs index c22cd4b4..d32331b9 100644 --- a/Switch_Toolbox_Library/Forms/Animation/Rewrite/AnimationPanel.cs +++ b/Switch_Toolbox_Library/Forms/Animation/Rewrite/AnimationPanel.cs @@ -247,7 +247,7 @@ namespace Toolbox.Library { animationTimer = new Timer { - Interval = 1000 / 60 + Interval = (int)(1000.0f / 60.0f) }; animationTimer.Tick += new EventHandler(animationTimer_Tick); } @@ -434,8 +434,9 @@ namespace Toolbox.Library } private void frameSpeedUD_ValueChanged(object sender, EventArgs e) { + if (animationTimer != null) - animationTimer.Interval = (int)(1000 / frameSpeedUD.Value); + animationTimer.Interval = (int)(1000.0f / (float)frameSpeedUD.Value); } } }