Skip to content

Commit

Permalink
Switched to using model renderers for the bones, rather than GL.Begin().
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Dec 12, 2024
1 parent 60239ec commit 0aa42f3
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
150 changes: 69 additions & 81 deletions FinModelUtility/Fin/Fin.Ui/src/rendering/gl/model/SkeletonRenderer.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Vector3, OpenTK.Mathematics.Vector3>(ref parentPos));
GL.Vertex3(Unsafe.As<Vector3, OpenTK.Mathematics.Vector3>(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<Vector3, OpenTK.Mathematics.Vector3>(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<Vector3, OpenTK.Mathematics.Vector3>(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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public void Render() {
this.modelRenderer_.Render();

if (FinConfig.ShowSkeleton || this.isBoneSelected_) {
CommonShaderPrograms.TEXTURELESS_SHADER_PROGRAM.Use();
this.SkeletonRenderer.Render();
}

Expand Down
50 changes: 25 additions & 25 deletions FinModelUtility/Fin/Fin/src/model/MaterialInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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; }
Expand Down
51 changes: 51 additions & 0 deletions FinModelUtility/Fin/Fin/src/model/impl/material/BMaterialImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0aa42f3

Please sign in to comment.