diff --git a/.vs/Switch_Toolbox/v15/.suo b/.vs/Switch_Toolbox/v15/.suo index 665e5cfc..2a2fc8aa 100644 Binary files a/.vs/Switch_Toolbox/v15/.suo and b/.vs/Switch_Toolbox/v15/.suo differ diff --git a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide index 042ca147..c13cd2c2 100644 Binary files a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide and b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide differ diff --git a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal index a47f19b9..2cd41941 100644 Binary files a/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal and b/.vs/Switch_Toolbox/v15/Server/sqlite3/storage.ide-wal differ diff --git a/Switch_FileFormatsMain/FileFormats/BMD/BMDMaterialWrapper.cs b/Switch_FileFormatsMain/FileFormats/BMD/BMDMaterialWrapper.cs index 23e9ad57..d2cdd232 100644 --- a/Switch_FileFormatsMain/FileFormats/BMD/BMDMaterialWrapper.cs +++ b/Switch_FileFormatsMain/FileFormats/BMD/BMDMaterialWrapper.cs @@ -15,6 +15,14 @@ namespace FirstPlugin public Material Material; SuperBMDLib.Model ParentModel; + public bool isTransparent + { + get + { + return (Material.Flag & 3) == 0; + } + } + public BMDMaterialWrapper(Material mat, SuperBMDLib.Model model) { Material = mat; diff --git a/Switch_FileFormatsMain/GL/BMD_Renderer.cs b/Switch_FileFormatsMain/GL/BMD_Renderer.cs index b209e887..c897c49d 100644 --- a/Switch_FileFormatsMain/GL/BMD_Renderer.cs +++ b/Switch_FileFormatsMain/GL/BMD_Renderer.cs @@ -22,25 +22,54 @@ namespace FirstPlugin } + public override void DrawModels(ShaderProgram shader, GL_ControlModern control) + { + shader.EnableVertexAttributes(); + + List opaque = new List(); + List transparent = new List(); + + for (int m = 0; m < Meshes.Count; m++) + { + if (((BMDMaterialWrapper)Meshes[m].GetMaterial()).isTransparent) + transparent.Add(Meshes[m]); + else + opaque.Add(Meshes[m]); + } + + for (int m = 0; m < transparent.Count; m++) + { + DrawModel(control, Skeleton, transparent[m].GetMaterial(), transparent[m], shader); + } + + for (int m = 0; m < opaque.Count; m++) + { + DrawModel(control, Skeleton, opaque[m].GetMaterial(), opaque[m], shader); + } + shader.DisableVertexAttributes(); + } + public override void SetRenderData(STGenericMaterial mat, ShaderProgram shader, STGenericObject m) { var bmdMaterial = (BMDMaterialWrapper)mat; - switch (bmdMaterial.Material.CullMode) + shader.SetBoolToInt("isTransparent", bmdMaterial.isTransparent); + + GXToOpenGL.SetBlendState(bmdMaterial.Material.BMode); + GXToOpenGL.SetCullState(bmdMaterial.Material.CullMode); + GXToOpenGL.SetDepthState(bmdMaterial.Material.ZMode, false); + GXToOpenGL.SetDitherEnabled(bmdMaterial.Material.Dither); + } + + public static TextureMagFilter GetMagFilter(SuperBMDLib.Materials.BinaryTextureImage.FilterMode fromMode) + { + switch (fromMode) { - case CullMode.None: - GL.Disable(EnableCap.CullFace); - break; - case CullMode.Back: - GL.CullFace(CullFaceMode.Back); - break; - case CullMode.Front: - GL.CullFace(CullFaceMode.Front); - break; - case CullMode.All: - GL.CullFace(CullFaceMode.FrontAndBack); - break; + case SuperBMDLib.Materials.BinaryTextureImage.FilterMode.Nearest: return TextureMagFilter.Nearest; + case SuperBMDLib.Materials.BinaryTextureImage.FilterMode.Linear: return TextureMagFilter.Linear; } + + return TextureMagFilter.Nearest; } public override int BindTexture(STGenericMatTexture tex, ShaderProgram shader) diff --git a/Switch_FileFormatsMain/GL/GXToOpenGL.cs b/Switch_FileFormatsMain/GL/GXToOpenGL.cs new file mode 100644 index 00000000..0e682fc8 --- /dev/null +++ b/Switch_FileFormatsMain/GL/GXToOpenGL.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OpenTK.Graphics.OpenGL; +using OpenTK; +using SuperBMDLib.Materials; +using SuperBMDLib.Materials.Enums; + +namespace FirstPlugin +{ + //From https://github.com/LordNed/JStudio/blob/b9c4eabb1c7e80a8da7f63f8d5003df704de369c/JStudio/GXToOpenGL.cs + public static class GXToOpenGL + { + public static TextureWrapMode GetWrapMode(BinaryTextureImage.WrapModes fromMode) + { + switch (fromMode) + { + case BinaryTextureImage.WrapModes.ClampToEdge: return TextureWrapMode.ClampToEdge; + case BinaryTextureImage.WrapModes.Repeat: return TextureWrapMode.Repeat; + case BinaryTextureImage.WrapModes.MirroredRepeat: return TextureWrapMode.MirroredRepeat; + } + + return TextureWrapMode.Repeat; + } + + public static TextureMinFilter GetMinFilter(BinaryTextureImage.FilterMode fromMode) + { + switch (fromMode) + { + case BinaryTextureImage.FilterMode.Nearest: return TextureMinFilter.Nearest; + case BinaryTextureImage.FilterMode.Linear: return TextureMinFilter.Linear; + case BinaryTextureImage.FilterMode.NearestMipmapNearest: return TextureMinFilter.NearestMipmapNearest; + case BinaryTextureImage.FilterMode.NearestMipmapLinear: return TextureMinFilter.NearestMipmapLinear; + case BinaryTextureImage.FilterMode.LinearMipmapNearest: return TextureMinFilter.LinearMipmapNearest; + case BinaryTextureImage.FilterMode.LinearMipmapLinear: return TextureMinFilter.LinearMipmapLinear; + } + + return TextureMinFilter.Nearest; + } + + public static TextureMagFilter GetMagFilter(BinaryTextureImage.FilterMode fromMode) + { + switch (fromMode) + { + case BinaryTextureImage.FilterMode.Nearest: return TextureMagFilter.Nearest; + case BinaryTextureImage.FilterMode.Linear: return TextureMagFilter.Linear; + } + + return TextureMagFilter.Nearest; + } + + public static void SetBlendState(SuperBMDLib.Materials.BlendMode blendMode) + { + if (blendMode.Type == SuperBMDLib.Materials.Enums.BlendMode.Blend) + { + GL.Enable(EnableCap.Blend); + GL.BlendFunc(GetBlendFactorSrc(blendMode.SourceFact), GetBlendFactorDest(blendMode.DestinationFact)); + } + else if (blendMode.Type == SuperBMDLib.Materials.Enums.BlendMode.None) + { + GL.Disable(EnableCap.Blend); + } + else + { + // Logic, Subtract? + } + } + + public static BlendingFactor GetBlendFactorSrc(BlendModeControl sourceFactor) + { + switch (sourceFactor) + { + case BlendModeControl.Zero: return BlendingFactor.Zero; + case BlendModeControl.One: return BlendingFactor.One; + case BlendModeControl.SrcColor: return BlendingFactor.SrcColor; + case BlendModeControl.InverseSrcColor: return BlendingFactor.OneMinusSrcColor; + case BlendModeControl.SrcAlpha: return BlendingFactor.SrcAlpha; + case BlendModeControl.InverseSrcAlpha: return BlendingFactor.OneMinusSrcAlpha; + case BlendModeControl.DstAlpha: return BlendingFactor.DstAlpha; + case BlendModeControl.InverseDstAlpha: return BlendingFactor.OneMinusDstAlpha; + default: + Console.WriteLine("Unsupported GXBlendModeControl: \"{0}\" in GetOpenGLBlendSrc!", sourceFactor); + return BlendingFactor.SrcAlpha; + + } + } + + public static BlendingFactor GetBlendFactorDest(BlendModeControl destinationFactor) + { + switch (destinationFactor) + { + case BlendModeControl.Zero: return BlendingFactor.Zero; + case BlendModeControl.One: return BlendingFactor.One; + case BlendModeControl.SrcColor: return BlendingFactor.SrcColor; + case BlendModeControl.InverseSrcColor: return BlendingFactor.OneMinusSrcColor; + case BlendModeControl.SrcAlpha: return BlendingFactor.SrcAlpha; + case BlendModeControl.InverseSrcAlpha: return BlendingFactor.OneMinusSrcAlpha; + case BlendModeControl.DstAlpha: return BlendingFactor.DstAlpha; + case BlendModeControl.InverseDstAlpha: return BlendingFactor.OneMinusDstAlpha; + default: + Console.WriteLine("Unsupported GXBlendModeControl: \"{0}\" in GetOpenGLBlendDest!", destinationFactor); + return BlendingFactor.OneMinusSrcAlpha; + } + } + + public static void SetCullState(CullMode cullState) + { + GL.Enable(EnableCap.CullFace); + switch (cullState) + { + case CullMode.None: GL.Disable(EnableCap.CullFace); break; + case CullMode.Front: GL.CullFace(CullFaceMode.Front); break; + case CullMode.Back: GL.CullFace(CullFaceMode.Back); break; + case CullMode.All: GL.CullFace(CullFaceMode.FrontAndBack); break; + } + } + + public static void SetDepthState(ZMode depthState, bool bDepthOnlyPrePass) + { + if (depthState.Enable || bDepthOnlyPrePass) + { + GL.Enable(EnableCap.DepthTest); + GL.DepthMask(depthState.UpdateEnable || bDepthOnlyPrePass); + switch (depthState.Function) + { + case CompareType.Never: GL.DepthFunc(DepthFunction.Never); break; + case CompareType.Less: GL.DepthFunc(DepthFunction.Less); break; + case CompareType.Equal: GL.DepthFunc(DepthFunction.Equal); break; + case CompareType.LEqual: GL.DepthFunc(DepthFunction.Lequal); break; + case CompareType.Greater: GL.DepthFunc(DepthFunction.Greater); break; + case CompareType.NEqual: GL.DepthFunc(DepthFunction.Notequal); break; + case CompareType.GEqual: GL.DepthFunc(DepthFunction.Gequal); break; + case CompareType.Always: GL.DepthFunc(DepthFunction.Always); break; + default: Console.WriteLine("Unsupported GXCompareType: \"{0}\" in GetOpenGLDepthFunc!", depthState.Function); break; + } + } + else + { + GL.Disable(EnableCap.DepthTest); + GL.DepthMask(false); + } + } + + public static void SetDitherEnabled(bool ditherEnabled) + { + if (ditherEnabled) + GL.Enable(EnableCap.Dither); + else + GL.Disable(EnableCap.Dither); + } + } +} diff --git a/Switch_FileFormatsMain/GUI/BFRES/TexturePattern/BfresTexturePatternEditor.cs b/Switch_FileFormatsMain/GUI/BFRES/TexturePattern/BfresTexturePatternEditor.cs index d1d360bf..39d7ab5f 100644 --- a/Switch_FileFormatsMain/GUI/BFRES/TexturePattern/BfresTexturePatternEditor.cs +++ b/Switch_FileFormatsMain/GUI/BFRES/TexturePattern/BfresTexturePatternEditor.cs @@ -173,6 +173,7 @@ namespace FirstPlugin.Forms public List KeyFrames = new List(); //Used for getting the frame of the list item public bool IsLoading = false; + private Thread Thread; private void LoadAniamtion(MaterialAnimation anim, MaterialAnimation.SamplerKeyGroup activeSampler) { if (activeSampler == null || IsLoading) @@ -200,7 +201,7 @@ namespace FirstPlugin.Forms } } - Thread Thread = new Thread((ThreadStart)(() => + Thread = new Thread((ThreadStart)(() => { for (int Frame = 0; Frame <= anim.FrameCount; Frame++) { diff --git a/Switch_FileFormatsMain/Switch_FileFormatsMain.csproj b/Switch_FileFormatsMain/Switch_FileFormatsMain.csproj index 5bbb311f..97bb2cac 100644 --- a/Switch_FileFormatsMain/Switch_FileFormatsMain.csproj +++ b/Switch_FileFormatsMain/Switch_FileFormatsMain.csproj @@ -256,6 +256,7 @@ + Form diff --git a/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache b/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache index e8541865..ad46ee7d 100644 Binary files a/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache and b/Switch_FileFormatsMain/obj/Release/DesignTimeResolveAssemblyReferences.cache differ diff --git a/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache b/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache index 47db3ddf..d55bff34 100644 Binary files a/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache and b/Switch_FileFormatsMain/obj/Release/Switch_FileFormatsMain.csprojAssemblyReference.cache differ diff --git a/Switch_Toolbox_Library/Rendering/GenericModelRenderer/GenericModelRenderer.cs b/Switch_Toolbox_Library/Rendering/GenericModelRenderer/GenericModelRenderer.cs index 0cffed3e..2dff5984 100644 --- a/Switch_Toolbox_Library/Rendering/GenericModelRenderer/GenericModelRenderer.cs +++ b/Switch_Toolbox_Library/Rendering/GenericModelRenderer/GenericModelRenderer.cs @@ -378,7 +378,7 @@ namespace Switch_Toolbox.Library.Rendering shader.SetBoolToInt("renderVertColor", Runtime.renderVertColor); } - private void DrawModels(ShaderProgram shader, GL_ControlModern control) + public virtual void DrawModels(ShaderProgram shader, GL_ControlModern control) { shader.EnableVertexAttributes(); foreach (STGenericObject shp in Meshes) @@ -405,7 +405,7 @@ namespace Switch_Toolbox.Library.Rendering GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo_elements); } - private void DrawModel(GLControl control, STSkeleton Skeleton, STGenericMaterial Material, STGenericObject m, ShaderProgram shader) + public void DrawModel(GLControl control, STSkeleton Skeleton, STGenericMaterial Material, STGenericObject m, ShaderProgram shader) { if (m.PolygonGroups.Count > 0) {