From 0aa42f3cb8c85a06f9129fba7f901114443bc8bf Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Thu, 12 Dec 2024 03:29:01 -0600 Subject: [PATCH] Switched to using model renderers for the bones, rather than GL.Begin(). --- .../MergedMaterialPrimitivesByMeshRenderer.cs | 18 +-- .../rendering/gl/model/SkeletonRenderer.cs | 150 ++++++++---------- .../rendering/gl/scene/SceneModelRenderer.cs | 1 - .../Fin/Fin/src/model/MaterialInterfaces.cs | 50 +++--- .../src/model/impl/material/BMaterialImpl.cs | 51 ++++++ .../material/FixedFunctionMaterialImpl.cs | 51 ------ 6 files changed, 153 insertions(+), 168 deletions(-) diff --git a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/MergedMaterialPrimitivesByMeshRenderer.cs b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/MergedMaterialPrimitivesByMeshRenderer.cs index 01325855a..4bb9ba9d8 100644 --- a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/MergedMaterialPrimitivesByMeshRenderer.cs +++ b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/MergedMaterialPrimitivesByMeshRenderer.cs @@ -63,16 +63,14 @@ public void Render() { private void RenderImpl_() { this.materialShader_?.Use(); - if (this.material_ is IReadOnlyFixedFunctionMaterial - fixedFunctionMaterial) { - GlUtil.SetBlendingSeparate( - fixedFunctionMaterial.ColorBlendEquation, - fixedFunctionMaterial.ColorSrcFactor, - fixedFunctionMaterial.ColorDstFactor, - fixedFunctionMaterial.AlphaBlendEquation, - fixedFunctionMaterial.AlphaSrcFactor, - fixedFunctionMaterial.AlphaDstFactor, - fixedFunctionMaterial.LogicOp); + if (this.material_ != null) { + GlUtil.SetBlendingSeparate(this.material_.ColorBlendEquation, + this.material_.ColorSrcFactor, + this.material_.ColorDstFactor, + this.material_.AlphaBlendEquation, + this.material_.AlphaSrcFactor, + this.material_.AlphaDstFactor, + this.material_.LogicOp); } else { GlUtil.ResetBlending(); } diff --git a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/SkeletonRenderer.cs b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/SkeletonRenderer.cs index 8bb1e351f..bb16b45a6 100644 --- a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/SkeletonRenderer.cs +++ b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/SkeletonRenderer.cs @@ -1,12 +1,13 @@ -using System.Numerics; -using System.Runtime.CompilerServices; +using System.Drawing; +using System.Numerics; using fin.math; using fin.model; +using fin.model.impl; using OpenTK.Graphics.OpenGL; -using PrimitiveType = OpenTK.Graphics.OpenGL.PrimitiveType; +using LogicOp = fin.model.LogicOp; namespace fin.ui.rendering.gl.model; @@ -23,89 +24,76 @@ public class SkeletonRenderer( IReadOnlySkeleton skeleton, IReadOnlyBoneTransformManager boneTransformManager) : ISkeletonRenderer { + private static readonly IModelRenderer BONE_RENDERER_; + private static float BONE_SCALE_ = 5; + + static SkeletonRenderer() { + var model = ModelImpl.CreateForViewer(); + + var material = model.MaterialManager.AddNullMaterial(); + material.DepthMode = DepthMode.WRITE_ONLY; + material.SetBlending(BlendEquation.ADD, + BlendFactor.CONST_COLOR, + BlendFactor.SRC_ALPHA, + LogicOp.SET); + + var skin = model.Skin; + + var from = skin.AddVertex(new Vector3(0, 0, 0)); + var to = skin.AddVertex(new Vector3(BONE_SCALE_, 0, 0)); + + var midpoint = .25f * BONE_SCALE_; + var radius = .2f * BONE_SCALE_; + var middle1 = skin.AddVertex(new Vector3(midpoint, -radius, 0)); + var middle2 = skin.AddVertex(new Vector3(midpoint, 0, -radius)); + var middle3 = skin.AddVertex(new Vector3(midpoint, radius, 0)); + var middle4 = skin.AddVertex(new Vector3(midpoint, 0, radius)); + + skin.AddMesh() + .AddLines([ + from, middle1, + from, middle2, + from, middle3, + from, middle4, + + middle1, middle2, + middle2, middle3, + middle3, middle4, + middle4, middle1, + + middle1, to, + middle2, to, + middle3, to, + middle4, to, + ]) + .SetMaterial(material); + + BONE_RENDERER_ = new ModelRendererV2(model); + } + public IReadOnlySkeleton Skeleton { get; } = skeleton; public IReadOnlyBone? SelectedBone { get; set; } public float Scale { get; set; } = 1; public void Render() { - GlTransform.PassMatricesIntoGl(); - - GlUtil.SetDepth(DepthMode.NONE); - - var rootBone = this.Skeleton.Root; - - // Renders lines from each bone to its parent. - { - GL.LineWidth(1); - GL.Begin(PrimitiveType.Lines); - - GL.Color4(0, 0, 1f, 1); - - var boneQueue = new Queue<(IReadOnlyBone, Vector3?)>(); - boneQueue.Enqueue((this.Skeleton.Root, null)); - while (boneQueue.Any()) { - var (bone, parentLocation) = boneQueue.Dequeue(); - - Vector3? location = null; - - if (bone != rootBone) { - var xyz = new Vector3(); - - boneTransformManager.ProjectPosition(bone, ref xyz); - - if (parentLocation != null) { - var parentPos = parentLocation.Value; - GL.Vertex3(Unsafe.As(ref parentPos)); - GL.Vertex3(Unsafe.As(ref xyz)); - } - - location = xyz; - } - - foreach (var child in bone.Children) { - boneQueue.Enqueue((child, location)); - } - } - - GL.End(); - } - - // Renders points at the start of each bone. - { - GL.PointSize(8); - GL.Begin(PrimitiveType.Points); - - GL.Color4(1f, 0, 0, 1); - - foreach (var bone in this.Skeleton) { - if (bone == rootBone || bone == this.SelectedBone) { - continue; - } - - var from = new Vector3(); - boneTransformManager.ProjectPosition(bone, ref from); - - GL.Vertex3(Unsafe.As(ref from)); - } - - GL.End(); - - if (this.SelectedBone != null) { - GL.PointSize(11); - GL.Begin(PrimitiveType.Points); - - GL.Color4(1f, 1f, 1f, 1); - - var from = new Vector3(); - boneTransformManager.ProjectPosition(this.SelectedBone, ref from); - - GL.Vertex3(Unsafe.As(ref from)); - - GL.End(); - } - } + GL.LineWidth(1); + this.RenderBone_(this.Skeleton.Root, this.Skeleton); + } + + private void RenderBone_(IReadOnlyBone bone, IReadOnlySkeleton skeleton) { + GlTransform.PushMatrix(); + GlTransform.MultMatrix(boneTransformManager.GetWorldMatrix(bone).Impl); + + if (skeleton.Root != bone) { + GlUtil.SetBlendColor(bone == this.SelectedBone + ? Color.White + : Color.Blue); + BONE_RENDERER_.Render(); + } + GlTransform.PopMatrix(); - GL.Color4(1f, 1, 1, 1); - GL.Enable(EnableCap.DepthTest); + foreach (var child in bone.Children) { + this.RenderBone_(child, skeleton); } + } } \ No newline at end of file diff --git a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/scene/SceneModelRenderer.cs b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/scene/SceneModelRenderer.cs index a2e6aed1a..5129e22c9 100644 --- a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/scene/SceneModelRenderer.cs +++ b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/scene/SceneModelRenderer.cs @@ -142,7 +142,6 @@ public void Render() { this.modelRenderer_.Render(); if (FinConfig.ShowSkeleton || this.isBoneSelected_) { - CommonShaderPrograms.TEXTURELESS_SHADER_PROGRAM.Use(); this.SkeletonRenderer.Render(); } diff --git a/FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs b/FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs index 580df78f2..d542aa2e0 100644 --- a/FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs +++ b/FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs @@ -91,6 +91,30 @@ public partial interface IMaterial { bool UpdateColorChannel { get; set; } bool UpdateAlphaChannel { get; set; } + + // TODO: Merge this into a single type + BlendEquation ColorBlendEquation { get; } + BlendFactor ColorSrcFactor { get; } + BlendFactor ColorDstFactor { get; } + BlendEquation AlphaBlendEquation { get; } + BlendFactor AlphaSrcFactor { get; } + BlendFactor AlphaDstFactor { get; } + LogicOp LogicOp { get; } + + IMaterial SetBlending( + BlendEquation blendEquation, + BlendFactor srcFactor, + BlendFactor dstFactor, + LogicOp logicOp); + + IMaterial SetBlendingSeparate( + BlendEquation colorBlendEquation, + BlendFactor colorSrcFactor, + BlendFactor colorDstFactor, + BlendEquation alphaBlendEquation, + BlendFactor alphaSrcFactor, + BlendFactor alphaDstFactor, + LogicOp logicOp); } [GenerateReadOnly] @@ -296,31 +320,7 @@ IFixedFunctionMaterial SetTextureSource(int textureIndex, IReadOnlyTexture texture); IReadOnlyTexture? CompiledTexture { get; set; } - - // TODO: Merge this into a single type - BlendEquation ColorBlendEquation { get; } - BlendFactor ColorSrcFactor { get; } - BlendFactor ColorDstFactor { get; } - BlendEquation AlphaBlendEquation { get; } - BlendFactor AlphaSrcFactor { get; } - BlendFactor AlphaDstFactor { get; } - LogicOp LogicOp { get; } - - IFixedFunctionMaterial SetBlending( - BlendEquation blendEquation, - BlendFactor srcFactor, - BlendFactor dstFactor, - LogicOp logicOp); - - IFixedFunctionMaterial SetBlendingSeparate( - BlendEquation colorBlendEquation, - BlendFactor colorSrcFactor, - BlendFactor colorDstFactor, - BlendEquation alphaBlendEquation, - BlendFactor alphaSrcFactor, - BlendFactor alphaDstFactor, - LogicOp logicOp); - + // TODO: Merge this into a single type AlphaOp AlphaOp { get; } AlphaCompareType AlphaCompareType0 { get; } diff --git a/FinModelUtility/Fin/Fin/src/model/impl/material/BMaterialImpl.cs b/FinModelUtility/Fin/Fin/src/model/impl/material/BMaterialImpl.cs index 0842e73af..de34fac69 100644 --- a/FinModelUtility/Fin/Fin/src/model/impl/material/BMaterialImpl.cs +++ b/FinModelUtility/Fin/Fin/src/model/impl/material/BMaterialImpl.cs @@ -26,5 +26,56 @@ private abstract class BMaterialImpl : IMaterial { public bool UpdateColorChannel { get; set; } = true; public bool UpdateAlphaChannel { get; set; } = true; + + public IMaterial SetBlending( + BlendEquation blendEquation, + BlendFactor srcFactor, + BlendFactor dstFactor, + LogicOp logicOp) + => this.SetBlendingSeparate(blendEquation, + srcFactor, + dstFactor, + blendEquation, + srcFactor, + dstFactor, + logicOp); + + public IMaterial SetBlendingSeparate( + BlendEquation colorBlendEquation, + BlendFactor colorSrcFactor, + BlendFactor colorDstFactor, + BlendEquation alphaBlendEquation, + BlendFactor alphaSrcFactor, + BlendFactor alphaDstFactor, + LogicOp logicOp) { + this.ColorBlendEquation = colorBlendEquation; + this.ColorSrcFactor = colorSrcFactor; + this.ColorDstFactor = colorDstFactor; + this.AlphaBlendEquation = alphaBlendEquation; + this.AlphaSrcFactor = alphaSrcFactor; + this.AlphaDstFactor = alphaDstFactor; + this.LogicOp = logicOp; + return this; + } + + public BlendEquation ColorBlendEquation { get; private set; } = + BlendEquation.ADD; + + public BlendFactor ColorSrcFactor { get; private set; } = + BlendFactor.SRC_ALPHA; + + public BlendFactor ColorDstFactor { get; private set; } = + BlendFactor.ONE_MINUS_SRC_ALPHA; + + public BlendEquation AlphaBlendEquation { get; private set; } = + BlendEquation.ADD; + + public BlendFactor AlphaSrcFactor { get; private set; } = + BlendFactor.ONE; + + public BlendFactor AlphaDstFactor { get; private set; } = + BlendFactor.ONE; + + public LogicOp LogicOp { get; private set; } = LogicOp.UNDEFINED; } } \ No newline at end of file diff --git a/FinModelUtility/Fin/Fin/src/model/impl/material/FixedFunctionMaterialImpl.cs b/FinModelUtility/Fin/Fin/src/model/impl/material/FixedFunctionMaterialImpl.cs index 554a67508..79e48f456 100644 --- a/FinModelUtility/Fin/Fin/src/model/impl/material/FixedFunctionMaterialImpl.cs +++ b/FinModelUtility/Fin/Fin/src/model/impl/material/FixedFunctionMaterialImpl.cs @@ -56,57 +56,6 @@ public IFixedFunctionMaterial SetTextureSource( public IReadOnlyTexture? CompiledTexture { get; set; } - public IFixedFunctionMaterial SetBlending( - BlendEquation blendEquation, - BlendFactor srcFactor, - BlendFactor dstFactor, - LogicOp logicOp) - => this.SetBlendingSeparate(blendEquation, - srcFactor, - dstFactor, - blendEquation, - srcFactor, - dstFactor, - logicOp); - - public IFixedFunctionMaterial SetBlendingSeparate( - BlendEquation colorBlendEquation, - BlendFactor colorSrcFactor, - BlendFactor colorDstFactor, - BlendEquation alphaBlendEquation, - BlendFactor alphaSrcFactor, - BlendFactor alphaDstFactor, - LogicOp logicOp) { - this.ColorBlendEquation = colorBlendEquation; - this.ColorSrcFactor = colorSrcFactor; - this.ColorDstFactor = colorDstFactor; - this.AlphaBlendEquation = alphaBlendEquation; - this.AlphaSrcFactor = alphaSrcFactor; - this.AlphaDstFactor = alphaDstFactor; - this.LogicOp = logicOp; - return this; - } - - public BlendEquation ColorBlendEquation { get; private set; } = - BlendEquation.ADD; - - public BlendFactor ColorSrcFactor { get; private set; } = - BlendFactor.SRC_ALPHA; - - public BlendFactor ColorDstFactor { get; private set; } = - BlendFactor.ONE_MINUS_SRC_ALPHA; - - public BlendEquation AlphaBlendEquation { get; private set; } = - BlendEquation.ADD; - - public BlendFactor AlphaSrcFactor { get; private set; } = - BlendFactor.ONE; - - public BlendFactor AlphaDstFactor { get; private set; } = - BlendFactor.ONE; - - public LogicOp LogicOp { get; private set; } = LogicOp.UNDEFINED; - public IFixedFunctionMaterial SetAlphaCompare( AlphaOp alphaOp,