2019-03-29 00:19:13 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
|
2019-07-16 21:35:21 +00:00
|
|
|
|
namespace Toolbox.Library
|
2019-03-29 00:19:13 +00:00
|
|
|
|
{
|
|
|
|
|
public class STBone : TreeNodeCustom
|
|
|
|
|
{
|
|
|
|
|
private bool visbile = true;
|
|
|
|
|
public bool Visible
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return visbile;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
visbile = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool UseSegmentScaleCompensate;
|
|
|
|
|
|
|
|
|
|
public STSkeleton skeletonParent;
|
|
|
|
|
public BoneRotationType RotationType;
|
2019-04-23 19:23:51 +00:00
|
|
|
|
public short BillboardIndex;
|
2019-03-29 00:19:13 +00:00
|
|
|
|
public short RigidMatrixIndex;
|
|
|
|
|
public short SmoothMatrixIndex;
|
|
|
|
|
|
|
|
|
|
public float[] position = new float[] { 0, 0, 0 };
|
|
|
|
|
public float[] rotation = new float[] { 0, 0, 0 };
|
|
|
|
|
public float[] scale = new float[] { 1, 1, 1 };
|
|
|
|
|
|
|
|
|
|
public Vector3 pos = Vector3.Zero, sca = new Vector3(1f, 1f, 1f);
|
|
|
|
|
public Quaternion rot = Quaternion.FromMatrix(Matrix3.Zero);
|
|
|
|
|
public Matrix4 Transform, invert;
|
|
|
|
|
|
2019-05-16 20:01:22 +00:00
|
|
|
|
//Used for shifting models with the bones in the shader
|
|
|
|
|
public Matrix4 ModelMatrix = Matrix4.Identity;
|
|
|
|
|
|
2019-03-29 00:19:13 +00:00
|
|
|
|
public Vector3 GetPosition()
|
|
|
|
|
{
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Quaternion GetRotation()
|
|
|
|
|
{
|
|
|
|
|
return rot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vector3 GetScale()
|
|
|
|
|
{
|
|
|
|
|
return sca;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-12 19:28:14 +00:00
|
|
|
|
public void FromTransform(Matrix4 Transform)
|
|
|
|
|
{
|
|
|
|
|
var pos = Transform.ExtractTranslation();
|
|
|
|
|
var quat = Transform.ExtractRotation();
|
|
|
|
|
var scale = Transform.ExtractScale();
|
|
|
|
|
|
|
|
|
|
position[0] = pos.X;
|
|
|
|
|
position[1] = pos.X;
|
|
|
|
|
position[2] = pos.Z;
|
|
|
|
|
|
2019-07-16 21:35:21 +00:00
|
|
|
|
var eul = Toolbox.Library.Animations.ANIM.quattoeul(quat);
|
2019-07-12 19:28:14 +00:00
|
|
|
|
rotation = new float[] { eul.X, eul.Y, eul.Z, 1 };
|
|
|
|
|
|
|
|
|
|
scale[0] = scale.X;
|
|
|
|
|
scale[1] = scale.X;
|
|
|
|
|
scale[2] = scale.Z;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 01:55:24 +00:00
|
|
|
|
private void ApplyTransforms()
|
|
|
|
|
{
|
|
|
|
|
position = new float[] { pos.X, pos .Y, pos .Z};
|
|
|
|
|
if (RotationType == BoneRotationType.Quaternion)
|
|
|
|
|
rotation = new float[] { rot.X, rot.Y, rot.Z, rot.W };
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-16 21:35:21 +00:00
|
|
|
|
var eul = Toolbox.Library.Animations.ANIM.quattoeul(rot);
|
2019-05-08 01:55:24 +00:00
|
|
|
|
rotation = new float[] { eul.X, eul.Y, eul.Z, 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scale = new float[] { sca.X, sca.Y, sca.Z };
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-29 00:19:13 +00:00
|
|
|
|
public int GetIndex()
|
|
|
|
|
{
|
|
|
|
|
if (skeletonParent != null)
|
|
|
|
|
return skeletonParent.bones.IndexOf(this);
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConvertToQuaternion()
|
|
|
|
|
{
|
|
|
|
|
if (RotationType == BoneRotationType.Quaternion)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-05-08 01:55:24 +00:00
|
|
|
|
RotationType = STBone.BoneRotationType.Quaternion;
|
|
|
|
|
|
2019-05-08 20:28:04 +00:00
|
|
|
|
ApplyTransforms();
|
|
|
|
|
|
2019-05-08 01:55:24 +00:00
|
|
|
|
//Update matrices
|
|
|
|
|
skeletonParent.reset();
|
|
|
|
|
skeletonParent.update();
|
2019-03-29 00:19:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ConvertToEular()
|
|
|
|
|
{
|
|
|
|
|
if (RotationType == BoneRotationType.Euler)
|
|
|
|
|
return;
|
|
|
|
|
|
2019-05-08 01:55:24 +00:00
|
|
|
|
RotationType = STBone.BoneRotationType.Euler;
|
|
|
|
|
|
2019-05-08 20:28:04 +00:00
|
|
|
|
ApplyTransforms();
|
|
|
|
|
|
2019-05-08 01:55:24 +00:00
|
|
|
|
//Update matrices
|
|
|
|
|
skeletonParent.reset();
|
|
|
|
|
skeletonParent.update();
|
2019-03-29 00:19:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnClick(TreeView treeView)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum BoneRotationType
|
|
|
|
|
{
|
|
|
|
|
Euler,
|
|
|
|
|
Quaternion,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int parentIndex
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Parent != null) Parent.Nodes.Remove(this);
|
|
|
|
|
if (value > -1 && value < skeletonParent.bones.Count)
|
|
|
|
|
{
|
|
|
|
|
skeletonParent.bones[value].Nodes.Add(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Parent == null || !(Parent is STBone))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return skeletonParent.bones.IndexOf((STBone)Parent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<STBone> GetChildren()
|
|
|
|
|
{
|
|
|
|
|
List<STBone> l = new List<STBone>();
|
|
|
|
|
foreach (STBone b in skeletonParent.bones)
|
|
|
|
|
if (b.Parent == this)
|
|
|
|
|
l.Add(b);
|
|
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public STBone(STSkeleton skl)
|
|
|
|
|
{
|
|
|
|
|
skeletonParent = skl;
|
|
|
|
|
ImageKey = "bone";
|
|
|
|
|
SelectedImageKey = "bone";
|
|
|
|
|
|
|
|
|
|
Checked = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public STBone()
|
|
|
|
|
{
|
|
|
|
|
ImageKey = "bone";
|
|
|
|
|
SelectedImageKey = "bone";
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-18 01:09:24 +00:00
|
|
|
|
public void RenderLegacy()
|
2019-03-29 00:19:13 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Runtime.OpenTKInitialized || !Runtime.renderBones)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Vector3 pos_c = Vector3.TransformPosition(Vector3.Zero, Transform);
|
|
|
|
|
|
|
|
|
|
if (IsSelected)
|
|
|
|
|
{
|
|
|
|
|
GL.Color3(Color.Red);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
GL.Color3(Color.GreenYellow);
|
|
|
|
|
|
|
|
|
|
RenderTools.DrawCube(pos_c, 0.1f);
|
|
|
|
|
|
|
|
|
|
// now draw line between parent
|
|
|
|
|
GL.Color3(Color.LightBlue);
|
|
|
|
|
GL.LineWidth(2f);
|
|
|
|
|
|
|
|
|
|
GL.Begin(PrimitiveType.Lines);
|
|
|
|
|
if (Parent != null && Parent is STBone)
|
|
|
|
|
{
|
|
|
|
|
Vector3 pos_p = Vector3.TransformPosition(Vector3.Zero, ((STBone)Parent).Transform);
|
|
|
|
|
GL.Vertex3(pos_c);
|
|
|
|
|
GL.Color3(Color.Blue);
|
|
|
|
|
GL.Vertex3(pos_p);
|
|
|
|
|
}
|
|
|
|
|
GL.End();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|