From f2aa8a93244d7e6a6053ad322f1c1cb051355c84 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Sun, 15 Oct 2023 17:20:05 +0200 Subject: [PATCH] [WIP] Added Vertex struct for better data management, Updated shaders and renderer --- PCK-Studio/Classes/Utils/Ray.cs | 485 +------- .../Forms/Skins-And-Textures/SkinPreview.cs | 24 +- .../generateModel.Designer.cs | 12 - PCK-Studio/Forms/TestGL.Designer.cs | 12 - PCK-Studio/PckStudio.csproj | 1 + PCK-Studio/Rendering/Renderer.cs | 11 +- PCK-Studio/Rendering/Renderer3D.cs | 1069 ++--------------- PCK-Studio/Rendering/Vertex.cs | 27 + PCK-Studio/Rendering/VertexArray.cs | 2 +- PCK-Studio/Rendering/VertexBuffer.cs | 9 +- PCK-Studio/Rendering/VertexBufferLayout.cs | 2 +- PCK-Studio/Resources/fragment.glsl | 8 +- PCK-Studio/Resources/vertexShader.glsl | 9 +- 13 files changed, 190 insertions(+), 1481 deletions(-) create mode 100644 PCK-Studio/Rendering/Vertex.cs diff --git a/PCK-Studio/Classes/Utils/Ray.cs b/PCK-Studio/Classes/Utils/Ray.cs index 8470a353..d831a2a4 100644 --- a/PCK-Studio/Classes/Utils/Ray.cs +++ b/PCK-Studio/Classes/Utils/Ray.cs @@ -19,495 +19,32 @@ namespace PckStudio.Classes.Utils public Point Pos { get; set; } - // TODO: remove Renderer3D from Ray - public Renderer3D Renderer { get; set; } - public struct RayResult { public float Distance { get; set; } - public enum Is + public enum eAxis { X, Y, Z } - public Is IsWhat { get; set; } + public eAxis Axis { get; set; } - public RayResult(float Distance, Is IsWhat) + public RayResult(float distance, eAxis axis) { - this.Distance = Distance; - this.IsWhat = IsWhat; + Distance = distance; + Axis = axis; } } - public Vector3 MouseHit - { - get - { - - Update(Pos.X, Pos.Y); - var HeadIndex = new float[2]; - var BodyIndex = new float[2]; - HeadIndex[0] = (4f - CamPos.Z) / CurrentRay.Z; - BodyIndex[0] = (2f - CamPos.Z) / CurrentRay.Z; - HeadIndex[1] = (-4 - CamPos.Z) / CurrentRay.Z; - BodyIndex[1] = (-2 - CamPos.Z) / CurrentRay.Z; - - var HeadXIndex = new float[2]; - var BodyXIndex = new float[4]; - var LegXIndex = new float[3]; - HeadXIndex[0] = (4f - CamPos.X) / CurrentRay.X; - if (Renderer.Model == Renderer3D.Models.Steve) - { - BodyXIndex[0] = (8f - CamPos.X) / CurrentRay.X; - BodyXIndex[1] = (-8 - CamPos.X) / CurrentRay.X; - } - else - { - BodyXIndex[0] = (7f - CamPos.X) / CurrentRay.X; - BodyXIndex[1] = (-7 - CamPos.X) / CurrentRay.X; - } - HeadXIndex[1] = (-4 - CamPos.X) / CurrentRay.X; - BodyXIndex[2] = (-4 - CamPos.X) / CurrentRay.X; - BodyXIndex[3] = (4f - CamPos.X) / CurrentRay.X; - LegXIndex[0] = (4f - CamPos.X) / CurrentRay.X; - LegXIndex[1] = (-4 - CamPos.X) / CurrentRay.X; - LegXIndex[2] = -CamPos.X / CurrentRay.X; - - var HeadYIndex = new float[2]; - var BodyYIndex = new float[2]; - var LegYIndex = new float[2]; - HeadYIndex[0] = (16f - CamPos.Y) / CurrentRay.Y; - BodyYIndex[0] = (8f - CamPos.Y) / CurrentRay.Y; - HeadYIndex[1] = (8f - CamPos.Y) / CurrentRay.Y; - BodyYIndex[1] = (-4 - CamPos.Y) / CurrentRay.Y; - LegYIndex[0] = (-4 - CamPos.Y) / CurrentRay.Y; - LegYIndex[1] = (-16 - CamPos.Y) / CurrentRay.Y; - - var PointsDis = new List(); - - Vector3 HeadPoint, BodyPoint, HeadXPoint, BodyXPoint, LegXPoint, HeadYPoint, BodyYPoint, LegYPoint; - - for (byte I = 0; I <= 3; I++) - { - - if (!(I > 1)) - { - HeadPoint = GetPointOnRay(CurrentRay, HeadIndex[I]); - } - else - { - HeadPoint = new Vector3(20f, 20f, 20f); - } - if (!(I > 1)) - { - BodyPoint = GetPointOnRay(CurrentRay, BodyIndex[I]); - } - else - { - BodyPoint = new Vector3(20f, 20f, 20f); - } - - if (!(I > 1)) - { - HeadXPoint = GetPointOnRay(CurrentRay, HeadXIndex[I]); - } - else - { - HeadXPoint = new Vector3(20f, 20f, 20f); - } - BodyXPoint = GetPointOnRay(CurrentRay, BodyXIndex[I]); - if (!(I > 2)) - { - LegXPoint = GetPointOnRay(CurrentRay, LegXIndex[I]); - } - else - { - LegXPoint = new Vector3(20f, 20f, 20f); - } - - if (!(I > 1)) - { - HeadYPoint = GetPointOnRay(CurrentRay, HeadYIndex[I]); - } - else - { - HeadYPoint = new Vector3(20f, 20f, 20f); - } - if (!(I > 1)) - { - BodyYPoint = GetPointOnRay(CurrentRay, BodyYIndex[I]); - } - else - { - BodyYPoint = new Vector3(20f, 20f, 20f); - } - if (!(I > 1)) - { - LegYPoint = GetPointOnRay(CurrentRay, LegYIndex[I]); - } - else - { - LegYPoint = new Vector3(20f, 20f, 20f); - } - - if (Renderer.ShowHead && HeadPoint.X < 4f && HeadPoint.X > -4 && HeadPoint.Y < 16f && HeadPoint.Y > 8f) - { - PointsDis.Add(new RayResult(HeadIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowBody && BodyPoint.X < 4f && BodyPoint.X > -4 && BodyPoint.Y < 8f && BodyPoint.Y > -4) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowRightArm && BodyPoint.Y > -4 && BodyPoint.Y < 8f) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyPoint.X > -8 && BodyPoint.X < -4 || Renderer.Model == Renderer3D.Models.Alex && BodyPoint.X > -7 && BodyPoint.X < -4) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - } - if (Renderer.ShowLeftArm && BodyPoint.Y < 8f && BodyPoint.Y > -4) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyPoint.X < 8f && BodyPoint.X > 4f || Renderer.Model == Renderer3D.Models.Alex && BodyPoint.X < 7f && BodyPoint.X > 4f) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - } - if (Renderer.ShowRightLeg && BodyPoint.X < 0f && BodyPoint.X > -4 && BodyPoint.Y < -4 && BodyPoint.Y > -16) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowLeftLeg && BodyPoint.X < 4f && BodyPoint.X > 0f && BodyPoint.Y < -4 && BodyPoint.Y > -16) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowHead && HeadXPoint.Z < 4f && HeadXPoint.Z > -4 && HeadXPoint.Y < 16f && HeadXPoint.Y > 8f) - { - PointsDis.Add(new RayResult(HeadXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowRightArm && (Convert.ToInt32(BodyXPoint.X) == -8 || Convert.ToInt32(BodyXPoint.X) == -7) && BodyXPoint.Z < 2f && BodyXPoint.Z > -2 && BodyXPoint.Y < 8f && BodyXPoint.Y > -4) - { - PointsDis.Add(new RayResult(BodyXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowLeftArm && (Convert.ToInt32(BodyXPoint.X) == 8 || Convert.ToInt32(BodyXPoint.X) == 7) && BodyXPoint.Z < 2f && BodyXPoint.Z > -2 && BodyXPoint.Y < 8f && BodyXPoint.Y > -4) - { - PointsDis.Add(new RayResult(BodyXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowRightArm ^ Renderer.ShowBody && Convert.ToInt32(BodyXPoint.X) == -4 && BodyXPoint.Z < 2f && BodyXPoint.Z > -2 && BodyXPoint.Y < 8f && BodyXPoint.Y > -4) - { - PointsDis.Add(new RayResult(BodyXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowLeftArm ^ Renderer.ShowBody && Convert.ToInt32(BodyXPoint.X) == 4 && BodyXPoint.Z < 2f && BodyXPoint.Z > -2 && BodyXPoint.Y < 8f && BodyXPoint.Y > -4) - { - PointsDis.Add(new RayResult(BodyXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowRightLeg && Convert.ToInt32(LegXPoint.X) == -4 && LegXPoint.Z < 2f && LegXPoint.Z > -2 && LegXPoint.Y < -4 && LegXPoint.Y > -16) - { - PointsDis.Add(new RayResult(LegXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowLeftLeg && Convert.ToInt32(LegXPoint.X) == 4 && LegXPoint.Z < 2f && LegXPoint.Z > -2 && LegXPoint.Y < -4 && LegXPoint.Y > -16) - { - PointsDis.Add(new RayResult(LegXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowRightLeg ^ Renderer.ShowLeftLeg && Convert.ToInt32(LegXPoint.X) == 0 && LegXPoint.Z < 2f && LegXPoint.Z > -2 && LegXPoint.Y < -4 && LegXPoint.Y > -16) - { - PointsDis.Add(new RayResult(LegXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowHead && HeadYPoint.Z < 4f && HeadYPoint.Z > -4 && HeadYPoint.X < 4f && HeadYPoint.X > -4) - { - PointsDis.Add(new RayResult(HeadYIndex[I], RayResult.Is.Y)); - } - if (Renderer.ShowBody && BodyYPoint.Z < 2f && BodyYPoint.Z > -2 && BodyYPoint.X < 4f && BodyYPoint.X > -4) - { - PointsDis.Add(new RayResult(BodyYIndex[I], RayResult.Is.Y)); - } - if (Renderer.ShowRightArm && BodyYPoint.Z < 2f && BodyYPoint.Z > -2) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyYPoint.X > -8 && BodyYPoint.X < -4 || Renderer.Model == Renderer3D.Models.Alex && BodyYPoint.X > -7 && BodyYPoint.X < -4) - { - PointsDis.Add(new RayResult(BodyYIndex[I], RayResult.Is.Y)); - } - } - if (Renderer.ShowLeftArm && BodyYPoint.Z < 2f && BodyYPoint.Z > -2) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyYPoint.X < 8f && BodyYPoint.X > 4f || Renderer.Model == Renderer3D.Models.Alex && BodyYPoint.X < 7f && BodyYPoint.X > 4f) - { - PointsDis.Add(new RayResult(BodyYIndex[I], RayResult.Is.Y)); - } - } - if (Renderer.ShowRightLeg && LegYPoint.Z < 2f && LegYPoint.Z > -2 && LegYPoint.X < 0f && LegYPoint.X > -4) - { - PointsDis.Add(new RayResult(LegYIndex[I], RayResult.Is.Y)); - } - if (Renderer.ShowLeftLeg && LegYPoint.Z < 2f && LegYPoint.Z > -2 && LegYPoint.X < 4f && LegYPoint.X > 0f) - { - PointsDis.Add(new RayResult(LegYIndex[I], RayResult.Is.Y)); - } - - } - - if (PointsDis.Count == 0) - return default; - - var Smallest = new RayResult(1000f, RayResult.Is.X); - - foreach (RayResult value in PointsDis) - { - if (value.Distance < Smallest.Distance) - Smallest = value; - } - - var Result = GetPointOnRay(CurrentRay, Smallest.Distance); - if (Smallest.IsWhat == RayResult.Is.X) - { - Result.X = (int)Math.Round(Result.X); - } - else if (Smallest.IsWhat == RayResult.Is.Y) - { - Result.Y = (int)Math.Round(Result.Y); - } - else if (Smallest.IsWhat == RayResult.Is.Z) - { - Result.Z = (int)Math.Round(Result.Z); - } - - return Result; - } - } - - public Vector3 Mouse2ndHit - { - get - { - Update(Pos.X, Pos.Y); - var HeadIndex = new float[2]; - var BodyIndex = new float[2]; - HeadIndex[0] = (float)((4.24d - CamPos.Z) / CurrentRay.Z); - BodyIndex[0] = (float)((2.12d - CamPos.Z) / CurrentRay.Z); - HeadIndex[1] = (float)((-4.24d - CamPos.Z) / CurrentRay.Z); - BodyIndex[1] = (float)((-2.12d - CamPos.Z) / CurrentRay.Z); - - var HeadXIndex = new float[2]; - var BodyXIndex = new float[2]; - var RArmXIndex = new float[2]; - var LArmXIndex = new float[2]; - var RLegXIndex = new float[2]; - var LLegXIndex = new float[2]; - HeadXIndex[0] = (float)((4.24d - CamPos.X) / CurrentRay.X); - HeadXIndex[1] = (float)((-4.24d - CamPos.X) / CurrentRay.X); - BodyXIndex[0] = (float)((-4.24d - CamPos.X) / CurrentRay.X); - BodyXIndex[1] = (float)((4.24d - CamPos.X) / CurrentRay.X); - if (Renderer.Model == Renderer3D.Models.Steve) - { - RArmXIndex[0] = (float)((-3.88d - CamPos.X) / CurrentRay.X); - RArmXIndex[1] = (float)((-8.12d - CamPos.X) / CurrentRay.X); - LArmXIndex[0] = (float)((3.88d - CamPos.X) / CurrentRay.X); - LArmXIndex[1] = (float)((8.12d - CamPos.X) / CurrentRay.X); - } - else - { - RArmXIndex[0] = (float)((-3.91d - CamPos.X) / CurrentRay.X); - RArmXIndex[1] = (float)((-7.09d - CamPos.X) / CurrentRay.X); - LArmXIndex[0] = (float)((3.91d - CamPos.X) / CurrentRay.X); - LArmXIndex[1] = (float)((7.09d - CamPos.X) / CurrentRay.X); - } - LLegXIndex[0] = (float)((4.24d - CamPos.X) / CurrentRay.X); - RLegXIndex[0] = (float)((-4.24d - CamPos.X) / CurrentRay.X); - RLegXIndex[1] = (float)((0.12d - CamPos.X) / CurrentRay.X); - LLegXIndex[1] = (float)((-0.12d - CamPos.X) / CurrentRay.X); - - var HeadYIndex = new float[2]; - var BodyYIndex = new float[2]; - var LegYIndex = new float[2]; - HeadYIndex[0] = (float)((16.24d - CamPos.Y) / CurrentRay.Y); - BodyYIndex[0] = (float)((8.36d - CamPos.Y) / CurrentRay.Y); - HeadYIndex[1] = (float)((7.76d - CamPos.Y) / CurrentRay.Y); - BodyYIndex[1] = (float)((-4.36d - CamPos.Y) / CurrentRay.Y); - LegYIndex[0] = (float)((-3.64d - CamPos.Y) / CurrentRay.Y); - LegYIndex[1] = (float)((-16.36d - CamPos.Y) / CurrentRay.Y); - - var PointsDis = new List(); - - Vector3 HeadPoint, BodyPoint, HeadXPoint, BodyXPoint, LArmXPoint, RArmXPoint, LLegXPoint, RLegXPoint, HeadYPoint, BodyYPoint, LegYPoint; - - for (byte I = 0; I <= 1; I++) - { - - HeadPoint = GetPointOnRay(CurrentRay, HeadIndex[I]); - BodyPoint = GetPointOnRay(CurrentRay, BodyIndex[I]); - - HeadXPoint = GetPointOnRay(CurrentRay, HeadXIndex[I]); - BodyXPoint = GetPointOnRay(CurrentRay, BodyXIndex[I]); - RArmXPoint = GetPointOnRay(CurrentRay, RArmXIndex[I]); - LArmXPoint = GetPointOnRay(CurrentRay, LArmXIndex[I]); - RLegXPoint = GetPointOnRay(CurrentRay, RLegXIndex[I]); - LLegXPoint = GetPointOnRay(CurrentRay, LLegXIndex[I]); - - HeadYPoint = GetPointOnRay(CurrentRay, HeadYIndex[I]); - BodyYPoint = GetPointOnRay(CurrentRay, BodyYIndex[I]); - LegYPoint = GetPointOnRay(CurrentRay, LegYIndex[I]); - - if (Renderer.ShowHeadOverlay && HeadPoint.X < 4.24d && HeadPoint.X > -4.24d && HeadPoint.Y < 16.24d && HeadPoint.Y > 7.76d) - { - PointsDis.Add(new RayResult(HeadIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowBodyOverlay && BodyPoint.X < 4.24d && BodyPoint.X > -4.24d && BodyPoint.Y < 8.36d && BodyPoint.Y > -4.36d) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowRightArmOverlay && BodyPoint.Y < 8.36d && BodyPoint.Y > -4.36d) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyPoint.X < -3.88d && BodyPoint.X > -8.12d || Renderer.Model == Renderer3D.Models.Alex && BodyPoint.X < -3.91d && BodyPoint.X > -7.09d) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - } - if (Renderer.ShowLeftArmOverlay && BodyPoint.Y < 8.36d && BodyPoint.Y > -4.36d) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyPoint.X < 8.12d && BodyPoint.X > 3.88d || Renderer.Model == Renderer3D.Models.Alex && BodyPoint.X < 7.09d && BodyPoint.X > 3.91d) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - } - if (Renderer.ShowRightLegOverlay && BodyPoint.X < 0.12d && BodyPoint.X > -4.12d && BodyPoint.Y < -3.64d && BodyPoint.Y > -16.36d) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowLeftLegOverlay && BodyPoint.X < 4.12d && BodyPoint.X > -0.12d && BodyPoint.Y < -3.64d && BodyPoint.Y > -16.36d) - { - PointsDis.Add(new RayResult(BodyIndex[I], RayResult.Is.Z)); - } - if (Renderer.ShowHeadOverlay && HeadXPoint.Z < 4.24d && HeadXPoint.Z > -4.24d && HeadXPoint.Y < 16.24d && HeadXPoint.Y > 7.76d) - { - PointsDis.Add(new RayResult(HeadXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowBodyOverlay && BodyXPoint.Z < 2.12d && BodyXPoint.Z > -2.12d && BodyXPoint.Y < 8.36d && BodyXPoint.Y > -4.36d) - { - PointsDis.Add(new RayResult(BodyXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowRightArmOverlay && RArmXPoint.Z < 2.12d && RArmXPoint.Z > -2.12d && RArmXPoint.Y < 8.36d && RArmXPoint.Y > -4.36d) - { - PointsDis.Add(new RayResult(RArmXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowLeftArmOverlay && LArmXPoint.Z < 2.12d && LArmXPoint.Z > -2.12d && LArmXPoint.Y < 8.36d && LArmXPoint.Y > -4.36d) - { - PointsDis.Add(new RayResult(LArmXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowRightLegOverlay && RLegXPoint.Z < 2.12d && RLegXPoint.Z > -2.12d && RLegXPoint.Y < -3.64d && RLegXPoint.Y > -16.36d) - { - PointsDis.Add(new RayResult(RLegXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowLeftLegOverlay && LLegXPoint.Z < 2.12d && LLegXPoint.Z > -2.12d && LLegXPoint.Y < -3.64d && LLegXPoint.Y > -16.36d) - { - PointsDis.Add(new RayResult(LLegXIndex[I], RayResult.Is.X)); - } - if (Renderer.ShowHeadOverlay && HeadYPoint.Z < 4.24d && HeadYPoint.Z > -4.24d && HeadYPoint.X < 4.24d && HeadYPoint.X > -4.24d) - { - PointsDis.Add(new RayResult(HeadYIndex[I], RayResult.Is.Y)); - } - if (Renderer.ShowBodyOverlay && BodyYPoint.Z < 2.12d && BodyYPoint.Z > -2.12d && BodyYPoint.X < 4.24d && BodyYPoint.X > -4.24d) - { - PointsDis.Add(new RayResult(BodyYIndex[I], RayResult.Is.Y)); - } - if (Renderer.ShowRightArmOverlay && BodyYPoint.Z < 2.12d && BodyYPoint.Z > -2.12d) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyYPoint.X < -3.88d && BodyYPoint.X > -8.12d || Renderer.Model == Renderer3D.Models.Alex && BodyYPoint.X < -3.91d && BodyYPoint.X > -7.09d) - { - PointsDis.Add(new RayResult(BodyYIndex[I], RayResult.Is.Y)); - } - } - if (Renderer.ShowLeftArmOverlay && BodyYPoint.Z < 2.12d && BodyYPoint.Z > -2.12d) - { - if (Renderer.Model == Renderer3D.Models.Steve && BodyYPoint.X < 8.12d && BodyYPoint.X > 3.88d || Renderer.Model == Renderer3D.Models.Alex && BodyYPoint.X < 7.09d && BodyYPoint.X > 3.91d) - { - PointsDis.Add(new RayResult(BodyYIndex[I], RayResult.Is.Y)); - } - } - if (Renderer.ShowRightLegOverlay && LegYPoint.Z < 2.12d && LegYPoint.Z > -2.12d && LegYPoint.X < 0.12d && LegYPoint.X > -4.12d) - { - PointsDis.Add(new RayResult(LegYIndex[I], RayResult.Is.Y)); - } - if (Renderer.ShowLeftLegOverlay && LegYPoint.Z < 2.12d && LegYPoint.Z > -2.12d && LegYPoint.X < 4.12d && LegYPoint.X > -0.12d) - { - PointsDis.Add(new RayResult(LegYIndex[I], RayResult.Is.Y)); - } - - } - - if (PointsDis.Count == 0) - return new Vector3(100f, 100f, 100f); - - var Smallest = new RayResult(1000f, RayResult.Is.Z); - - foreach (RayResult value in PointsDis) - { - if (value.Distance < Smallest.Distance) - Smallest = value; - } - - var Result = GetPointOnRay(CurrentRay, Smallest.Distance); - - float[] ZIndex = new[] { 4.24f, 2.12f, (float)-4.24d, (float)-2.12d }; - float[] XIndex; - if (Renderer.Model == Renderer3D.Models.Steve) - { - XIndex = new[] { 4.24f, 8.12f, (float)-4.24d, (float)-8.12d, (float)-3.88d, 3.88f, 0.12f, (float)-0.12d }; - } - else - { - XIndex = new[] { 4.24f, 7.09f, (float)-4.24d, (float)-7.09d, (float)-3.91d, 3.91f, 0.12f, (float)-0.12d }; - } - float[] YIndex = new[] { 16.24f, 8.36f, 7.76f, (float)-4.36d, (float)-3.64d, (float)-16.36d }; - - float AResult = 20f; - - if (Smallest.IsWhat == RayResult.Is.X) - { - foreach (float value in XIndex) - { - if (Math.Abs(value - Result.X) < Math.Abs(AResult - Result.X)) - { - AResult = value; - } - } - Result.X = AResult; - } - else if (Smallest.IsWhat == RayResult.Is.Y) - { - foreach (float value in YIndex) - { - if (Math.Abs(value - Result.Y) < Math.Abs(AResult - Result.Y)) - { - AResult = value; - } - } - Result.Y = AResult; - } - else if (Smallest.IsWhat == RayResult.Is.Z) - { - foreach (float value in ZIndex) - { - if (Math.Abs(value - Result.Z) < Math.Abs(AResult - Result.Z)) - { - AResult = value; - } - } - Result.Z = AResult; - } - - return Result; - } - } - - public Ray(ref Matrix4 viewMatrix, ref Matrix4 projectionMatrix, Size size, Vector3 cameraPosition, Renderer3D renderer) + public Ray(ref Matrix4 viewMatrix, ref Matrix4 projectionMatrix, Size size, Vector3 cameraPosition) { ProjectionMatrix = projectionMatrix; ViewMatrix = viewMatrix; Size = size; CamPos = cameraPosition; - Renderer = renderer; } private void Update(int X, int Y) @@ -542,15 +79,17 @@ namespace PckStudio.Classes.Utils private Vector2 GetNormalisedDeviceCoordinates(int x, int y) { - float x2 = 2.0f * x / Size.Width - 1.0f; - float y2 = -(2.0f * y / Size.Height - 1.0f); - return new Vector2(x2, y2); + return new Vector2() + { + X = 2.0f * x / Size.Width - 1.0f, + Y = -(2.0f * y / Size.Height - 1.0f) + }; } private Vector3 GetPointOnRay(Vector3 ray, float distance) { var start = new Vector3(CamPos); - var scaledRay = new Vector3(ray.X * distance, ray.Y * distance, ray.Z * distance); + var scaledRay = ray * distance; return start + scaledRay; } } diff --git a/PCK-Studio/Forms/Skins-And-Textures/SkinPreview.cs b/PCK-Studio/Forms/Skins-And-Textures/SkinPreview.cs index 185ccabf..c0ee08a3 100644 --- a/PCK-Studio/Forms/Skins-And-Textures/SkinPreview.cs +++ b/PCK-Studio/Forms/Skins-And-Textures/SkinPreview.cs @@ -16,20 +16,20 @@ namespace PckStudio.Forms Texture = texture; ModelView.Model = anim.GetFlag(SkinAnimFlag.SLIM_MODEL) ? Renderer3D.Models.Alex : Renderer3D.Models.Steve; - ModelView.ShowHead = !anim.GetFlag(SkinAnimFlag.HEAD_DISABLED); - ModelView.ShowHeadOverlay = !anim.GetFlag(SkinAnimFlag.HEAD_OVERLAY_DISABLED); - ModelView.ShowBody = !anim.GetFlag(SkinAnimFlag.BODY_DISABLED); - ModelView.ShowBodyOverlay = !anim.GetFlag(SkinAnimFlag.BODY_OVERLAY_DISABLED); + //ModelView.ShowHead = !anim.GetFlag(SkinAnimFlag.HEAD_DISABLED); + //ModelView.ShowHeadOverlay = !anim.GetFlag(SkinAnimFlag.HEAD_OVERLAY_DISABLED); + //ModelView.ShowBody = !anim.GetFlag(SkinAnimFlag.BODY_DISABLED); + //ModelView.ShowBodyOverlay = !anim.GetFlag(SkinAnimFlag.BODY_OVERLAY_DISABLED); - ModelView.ShowLeftArm = !anim.GetFlag(SkinAnimFlag.LEFT_ARM_DISABLED); - ModelView.ShowLeftArmOverlay = !anim.GetFlag(SkinAnimFlag.LEFT_ARM_OVERLAY_DISABLED); - ModelView.ShowRightArm = !anim.GetFlag(SkinAnimFlag.RIGHT_ARM_DISABLED); - ModelView.ShowRightArmOverlay = !anim.GetFlag(SkinAnimFlag.RIGHT_ARM_OVERLAY_DISABLED); + //ModelView.ShowLeftArm = !anim.GetFlag(SkinAnimFlag.LEFT_ARM_DISABLED); + //ModelView.ShowLeftArmOverlay = !anim.GetFlag(SkinAnimFlag.LEFT_ARM_OVERLAY_DISABLED); + //ModelView.ShowRightArm = !anim.GetFlag(SkinAnimFlag.RIGHT_ARM_DISABLED); + //ModelView.ShowRightArmOverlay = !anim.GetFlag(SkinAnimFlag.RIGHT_ARM_OVERLAY_DISABLED); - ModelView.ShowLeftLeg = !anim.GetFlag(SkinAnimFlag.LEFT_LEG_DISABLED); - ModelView.ShowLeftLegOverlay = !anim.GetFlag(SkinAnimFlag.LEFT_LEG_OVERLAY_DISABLED); - ModelView.ShowRightLeg = !anim.GetFlag(SkinAnimFlag.RIGHT_LEG_DISABLED); - ModelView.ShowRightLegOverlay = !anim.GetFlag(SkinAnimFlag.RIGHT_LEG_OVERLAY_DISABLED); + //ModelView.ShowLeftLeg = !anim.GetFlag(SkinAnimFlag.LEFT_LEG_DISABLED); + //ModelView.ShowLeftLegOverlay = !anim.GetFlag(SkinAnimFlag.LEFT_LEG_OVERLAY_DISABLED); + //ModelView.ShowRightLeg = !anim.GetFlag(SkinAnimFlag.RIGHT_LEG_DISABLED); + //ModelView.ShowRightLegOverlay = !anim.GetFlag(SkinAnimFlag.RIGHT_LEG_OVERLAY_DISABLED); } private void SkinPreview_Load(object sender, EventArgs e) => RenderModel(Texture); diff --git a/PCK-Studio/Forms/Skins-And-Textures/generateModel.Designer.cs b/PCK-Studio/Forms/Skins-And-Textures/generateModel.Designer.cs index 34a220a8..477cd96c 100644 --- a/PCK-Studio/Forms/Skins-And-Textures/generateModel.Designer.cs +++ b/PCK-Studio/Forms/Skins-And-Textures/generateModel.Designer.cs @@ -547,18 +547,6 @@ this.renderer3D1.Model = PckStudio.Rendering.Renderer3D.Models.Steve; this.renderer3D1.Name = "renderer3D1"; this.renderer3D1.Rotation = ((OpenTK.Vector2)(resources.GetObject("renderer3D1.Rotation"))); - this.renderer3D1.ShowBody = true; - this.renderer3D1.ShowBodyOverlay = true; - this.renderer3D1.ShowHead = true; - this.renderer3D1.ShowHeadOverlay = true; - this.renderer3D1.ShowLeftArm = true; - this.renderer3D1.ShowLeftArmOverlay = true; - this.renderer3D1.ShowLeftLeg = true; - this.renderer3D1.ShowLeftLegOverlay = true; - this.renderer3D1.ShowRightArm = true; - this.renderer3D1.ShowRightArmOverlay = true; - this.renderer3D1.ShowRightLeg = true; - this.renderer3D1.ShowRightLegOverlay = true; this.renderer3D1.Texture = null; this.renderer3D1.VSync = true; this.renderer3D1.FieldOfView = 1D; diff --git a/PCK-Studio/Forms/TestGL.Designer.cs b/PCK-Studio/Forms/TestGL.Designer.cs index 984b4022..22c2192e 100644 --- a/PCK-Studio/Forms/TestGL.Designer.cs +++ b/PCK-Studio/Forms/TestGL.Designer.cs @@ -38,18 +38,6 @@ this.renderer3D1.Location = new System.Drawing.Point(0, 0); this.renderer3D1.Model = PckStudio.Rendering.Renderer3D.Models.Steve; this.renderer3D1.Name = "renderer3D1"; - this.renderer3D1.ShowBodyOverlay = true; - this.renderer3D1.ShowHeadOverlay = true; - this.renderer3D1.ShowLeftArmOverlay = true; - this.renderer3D1.ShowLeftLegOverlay = true; - this.renderer3D1.ShowRightArmOverlay = true; - this.renderer3D1.ShowRightLegOverlay = true; - this.renderer3D1.ShowBody = true; - this.renderer3D1.ShowHead = true; - this.renderer3D1.ShowLeftArm = true; - this.renderer3D1.ShowLeftLeg = true; - this.renderer3D1.ShowRightArm = true; - this.renderer3D1.ShowRightLeg = true; this.renderer3D1.Size = new System.Drawing.Size(426, 428); this.renderer3D1.Texture = global::PckStudio.Properties.Resources.classic_template; this.renderer3D1.TabIndex = 8; diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index d5dcc65c..671a47cd 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -156,6 +156,7 @@ + diff --git a/PCK-Studio/Rendering/Renderer.cs b/PCK-Studio/Rendering/Renderer.cs index f38f81c0..85e6c37b 100644 --- a/PCK-Studio/Rendering/Renderer.cs +++ b/PCK-Studio/Rendering/Renderer.cs @@ -9,19 +9,12 @@ namespace PckStudio.Rendering { internal static class Renderer { - public static void Draw(VertexArray va, IndexBuffer ib, Shader shader) + public static void Draw(Shader shader, VertexArray va, IndexBuffer ib, PrimitiveType type) { shader.Bind(); va.Bind(); ib.Bind(); - GL.DrawElements(PrimitiveType.Triangles, ib.GetCount(), DrawElementsType.UnsignedInt, 0); - } - - public static void Draw(VertexArray va, Shader shader) - { - shader.Bind(); - va.Bind(); - GL.DrawArrays(PrimitiveType.Triangles, 0, 6); + GL.DrawElements(type, ib.GetCount(), DrawElementsType.UnsignedInt, 0); } } } diff --git a/PCK-Studio/Rendering/Renderer3D.cs b/PCK-Studio/Rendering/Renderer3D.cs index 43300c81..f9443b2c 100644 --- a/PCK-Studio/Rendering/Renderer3D.cs +++ b/PCK-Studio/Rendering/Renderer3D.cs @@ -40,6 +40,8 @@ using OpenTK.Graphics.OpenGL; using System.Diagnostics; using PckStudio.Properties; using System.Runtime.InteropServices; +using PckStudio.Extensions; +using Newtonsoft.Json.Linq; namespace PckStudio.Rendering { @@ -64,240 +66,11 @@ namespace PckStudio.Rendering shader.SetUniform1("u_Texture", 1); Refresh(); } - TextureScaleValue = new Vector2d(1d / value.Width, 1d / value.Height); + TextureScaleValue = new Vector2(1f / value.Width, 1f / value.Height); _texture = value; } } - private bool _showhead = true; - /// - /// Show the head or not - /// - /// Show the head or not - [Description("Shows the head layer or not")] - [Category("Appearance")] - public bool ShowHead - { - set - { - Refresh(); - _showhead = value; - } - get - { - return _showhead; - } - } - private bool _showHeadOverlay = true; - /// - /// Show the second head layer or not - /// - /// Show the second head layer or not - [Description("Shows the second head layer or not")] - [Category("Appearance")] - public bool ShowHeadOverlay - { - set - { - Refresh(); - _showHeadOverlay = value; - } - get - { - return _showHeadOverlay; - } - } - private bool _showbody = true; - /// - /// Show the body or not - /// - /// Show the body or not - [Description("Shows the body layer or not")] - [Category("Appearance")] - public bool ShowBody - { - set - { - Refresh(); - _showbody = value; - } - get - { - return _showbody; - } - } - private bool _showBodyOverlay = true; - /// - /// Show the second body layer or not - /// - /// Show the second body layer or not - [Description("Shows the second body layer or not")] - [Category("Appearance")] - public bool ShowBodyOverlay - { - set - { - Refresh(); - _showBodyOverlay = value; - } - get - { - return _showBodyOverlay; - } - } - private bool _showRightArm = true; - /// - /// Show the right arm or not - /// - /// Show the right arm or not - [Description("Shows the right arm layer or not")] - [Category("Appearance")] - public bool ShowRightArm - { - set - { - Refresh(); - _showRightArm = value; - } - get - { - return _showRightArm; - } - } - private bool _showRightArmOverlay = true; - /// - /// Show the second right arm layer or not - /// - /// Show the second right arm layer or not - [Description("Shows the second right arm layer or not")] - [Category("Appearance")] - public bool ShowRightArmOverlay - { - set - { - Refresh(); - _showRightArmOverlay = value; - } - get - { - return _showRightArmOverlay; - } - } - private bool _showLeftArm = true; - /// - /// Show the left arm or not - /// - /// Show the leftht arm or not - [Description("Shows the left arm layer or not")] - [Category("Appearance")] - public bool ShowLeftArm - { - set - { - Refresh(); - _showLeftArm = value; - } - get - { - return _showLeftArm; - } - } - private bool _showLeftArmOverlay = true; - /// - /// Show the second left arm layer or not - /// - /// Show the second left arm layer or not - [Description("Shows the second left arm layer or not")] - [Category("Appearance")] - public bool ShowLeftArmOverlay - { - set - { - Refresh(); - _showLeftArmOverlay = value; - } - get - { - return _showLeftArmOverlay; - } - } - private bool _showRightLeg = true; - /// - /// Show the right leg or not - /// - /// Show the right leg or not - [Description("Shows the right leg layer or not")] - [Category("Appearance")] - public bool ShowRightLeg - { - set - { - Refresh(); - _showRightLeg = value; - } - get - { - return _showRightLeg; - } - } - private bool _showRightLegOverlay = true; - /// - /// Show the second right leg layer or not - /// - /// Show the second right leg layer or not - [Description("Shows the second right leg layer or not")] - [Category("Appearance")] - public bool ShowRightLegOverlay - { - set - { - Refresh(); - _showRightLegOverlay = value; - } - get - { - return _showRightLegOverlay; - } - } - private bool _showLeftLeg = true; - /// - /// Show the left leg or not - /// - /// Show the leftht leg or not - [Description("Shows the left leg layer or not")] - [Category("Appearance")] - public bool ShowLeftLeg - { - set - { - Refresh(); - _showLeftLeg = value; - } - get - { - return _showLeftLeg; - } - } - private bool _showLeftLegOverlay = true; - /// - /// Show the second left leg layer or not - /// - /// Show the second left leg layer or not - [Description("Shows the second left leg layer or not")] - [Category("Appearance")] - public bool ShowLeftLegOverlay - { - set - { - Refresh(); - _showLeftLegOverlay = value; - } - get - { - return _showLeftLegOverlay; - } - } - public enum Models { Steve, @@ -317,7 +90,7 @@ namespace PckStudio.Rendering /// Rotation /// /// Rotation - [Description("The rotation of the model")] + [Description("The rotation of the camera")] [Category("Appearance")] public Vector2 Rotation { @@ -330,16 +103,27 @@ namespace PckStudio.Rendering } } - private double _fieldOfView = MinFOV; - private const double MinFOV = 30d; - private const double MaxFOV = 90d; + private double _fieldOfViewRadians = MinFOV; - [Description("The zoom value")] + private const double MinFOVDegrees = 30d; + private const double MaxFOVDegrees = 90d; + + private static double MinFOV = MathHelper.DegreesToRadians(MinFOVDegrees); + private static double MaxFOV = MathHelper.DegreesToRadians(MaxFOVDegrees); + + [Description("The Field of View value (in Radians)")] + private double FieldOfViewRadians + { + get => _fieldOfViewRadians; + set => _fieldOfViewRadians = MathHelper.Clamp(value, MinFOV, MaxFOV); + } + + [Description("The Field of View (in Degrees)")] [Category("Appearance")] public double FieldOfView { - get => _fieldOfView; - set => _fieldOfView = MathHelper.Clamp(value, MinFOV, MaxFOV); + get => MathHelper.RadiansToDegrees(_fieldOfViewRadians); + set => FieldOfViewRadians = MathHelper.DegreesToRadians(MathHelper.Clamp(value, MinFOVDegrees, MaxFOVDegrees)); } private Vector2 _lookAngle = Vector2.Zero; @@ -356,13 +140,13 @@ namespace PckStudio.Rendering } } - public float CameraDistance { get; set; } = 36f; + public float CameraDistance { get; set; } = 18f; private Matrix4 projection; private Matrix4 view; - private Vector2d TextureScaleValue = new Vector2d(1d / 64d); + private Vector2 TextureScaleValue = new Vector2(1f / 64); private void GLDebugMessage(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr message, IntPtr userParam) { @@ -410,38 +194,74 @@ namespace PckStudio.Rendering shader.Bind(); Texture = Resources.slim_template; - - shader.SetUniform4("u_Color", Color.Blue); - - var data = new float[] + var bodyVertexData = new Vertex[] { - 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, - 10.0f, 0.0f, 0.0f, 1.0f, 1.0f, - 10.0f, 10.0f, 10.0f, 1.0f, 0.0f, - 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, + // Face 1 + new Vertex(new Vector3(-4, 8, 2), Color.Red, new Vector2(TextureScaleValue.X * 20f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, 8, 2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, -4, 2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 32f)), + new Vertex(new Vector3(-4, -4, 2), Color.Red, new Vector2(TextureScaleValue.X * 20f, TextureScaleValue.Y * 32f)), + + // Face 2 + new Vertex(new Vector3(-4, 8, -2), Color.Red, new Vector2(TextureScaleValue.X * 40f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, 8, -2), Color.Red, new Vector2(TextureScaleValue.X * 32f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, -4, -2), Color.Red, new Vector2(TextureScaleValue.X * 32f, TextureScaleValue.Y * 32f)), + new Vertex(new Vector3(-4, -4, -2), Color.Red, new Vector2(TextureScaleValue.X * 40f, TextureScaleValue.Y * 32f)), + + // Face 3 + new Vertex(new Vector3(-4, 8, 2), Color.Red, new Vector2(TextureScaleValue.X * 20f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3(-4, 8, -2), Color.Red, new Vector2(TextureScaleValue.X * 16f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3(-4, -4, -2), Color.Red, new Vector2(TextureScaleValue.X * 16f, TextureScaleValue.Y * 32f)), + new Vertex(new Vector3(-4, -4, 2), Color.Red, new Vector2(TextureScaleValue.X * 20f, TextureScaleValue.Y * 32f)), + + // Face 4 + new Vertex(new Vector3( 4, 8, 2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, 8, -2), Color.Red, new Vector2(TextureScaleValue.X * 32f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, -4, -2), Color.Red, new Vector2(TextureScaleValue.X * 32f, TextureScaleValue.Y * 32f)), + new Vertex(new Vector3( 4, -4, 2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 32f)), + + // Face 5 + new Vertex(new Vector3(-4, 8, 2), Color.Red, new Vector2(TextureScaleValue.X * 20f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, 8, 2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3( 4, 8, -2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 16f)), + new Vertex(new Vector3(-4, 8, -2), Color.Red, new Vector2(TextureScaleValue.X * 20f, TextureScaleValue.Y * 16f)), + + // Face 6 + new Vertex(new Vector3(-4, -4, -2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 16f)), + new Vertex(new Vector3( 4, -4, -2), Color.Red, new Vector2(TextureScaleValue.X * 36f, TextureScaleValue.Y * 16f)), + new Vertex(new Vector3( 4, -4, 2), Color.Red, new Vector2(TextureScaleValue.X * 36f, TextureScaleValue.Y * 20f)), + new Vertex(new Vector3(-4, -4, 2), Color.Red, new Vector2(TextureScaleValue.X * 28f, TextureScaleValue.Y * 20f)), }; + indexBuffer = new IndexBuffer( + 0, 1, 2, 3, + 4, 5, 6, 7, + 8, 9, 10, 11, + 12, 13, 14, 15, + 16, 17, 18, 19, + 20, 21, 22, 23 + ); + vertexArray = new VertexArray(); - var buffer = new VertexBuffer(data, data.Length * sizeof(float)); + int vertexSize = Marshal.SizeOf(typeof(Vertex)); + + var buffer = new VertexBuffer(bodyVertexData, bodyVertexData.Length * vertexSize); var layout = new VertexBufferLayout(); layout.Add(3); + layout.Add(4); layout.Add(2); vertexArray.AddBuffer(buffer, layout); - indexBuffer = new IndexBuffer( - 0, 1, 2, - 2, 3, 0 - ); - + GLErrorCheck(); + } + [Conditional("DEBUG")] + private void GLErrorCheck() + { var error = GL.GetError(); - while (error != ErrorCode.NoError) - { - Debug.WriteLine(error); - error = GL.GetError(); - } + Debug.Assert(error == ErrorCode.NoError, error.ToString()); } private void UpdateView() @@ -450,14 +270,14 @@ namespace PckStudio.Rendering var target = new Vector3(LookAngle); - var up = Vector3.UnitY + Vector3.UnitZ; + var up = new Vector3(0, 1f, 1f); view = Matrix4.LookAt(camera, target, up); } private void UpdateProjection() { - projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.Pow(FieldOfView, -1), Width / (float)Height, 0.1f, 1000f); + projection = Matrix4.CreatePerspectiveFieldOfView((float)Math.Pow(FieldOfViewRadians, -1), Width / (float)Height, 1f, 100f); } private void DrawBox(Vector3 scale, Vector3 position, Vector2 uv) @@ -520,63 +340,59 @@ namespace PckStudio.Rendering UpdateView(); MakeCurrent(); - GL.Viewport(Size); - var rot = Matrix4.CreateFromAxisAngle(new Vector3(-0.5f, 0f, 0f), Rotation.X) - + Matrix4.CreateFromAxisAngle(new Vector3(0f, 0.5f, 0f), Rotation.Y); + Matrix4 transform = Matrix4.Identity; + Matrix4 scale = Matrix4.Identity; + Matrix4 rot = Matrix4.CreateFromAxisAngle(new Vector3(-1f, 0f, 0f), MathHelper.DegreesToRadians(Rotation.X)) + + Matrix4.CreateFromAxisAngle(new Vector3(0f, 1f, 0f), MathHelper.DegreesToRadians(Rotation.Y)); - mvp = view * projection * rot; + var model = transform * rot * scale; + + mvp = model * view * projection; #if DEBUG - debugLabel.Text = $"Rotation: {Rotation}\nZoom: {_fieldOfView}\nLookAt:\n{view}\nProjection:\n{projection}\nMVP:\n{mvp}"; + debugLabel.Text = $"Rotation: {Rotation}\nFOV: {FieldOfView}\nView:\n{view}\nProjection:\n{projection}\nMVP:\n{mvp}"; #endif shader.SetUniformMat4("u_MVP", ref mvp); + GL.Enable(EnableCap.Texture2D); // Enable textures + //GL.Enable(EnableCap.DepthTest); // Enable correct Z Drawings + //GL.DepthFunc(DepthFunction.Less); // Enable correct Z Drawings + GL.Disable(EnableCap.Blend); // Disable transparent + GL.Disable(EnableCap.AlphaTest); // Disable transparent + GL.ClearColor(BackColor); GL.Clear(ClearBufferMask.ColorBufferBit); + GL.Clear(ClearBufferMask.DepthBufferBit); - GL.Enable(EnableCap.Blend); - GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); + //GL.Enable(EnableCap.Blend); + //GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); - Renderer.Draw(vertexArray, indexBuffer, shader); + //GL.Enable(EnableCap.AlphaTest); // Enable transparent + //GL.AlphaFunc(AlphaFunction.Greater, 0.4f); + + Renderer.Draw(shader, vertexArray, indexBuffer, PrimitiveType.Quads); SwapBuffers(); return; - - GL.PushMatrix(); - - GL.ClearColor(BackColor); - // First Clear Buffers - GL.Clear(ClearBufferMask.ColorBufferBit); - GL.Clear(ClearBufferMask.DepthBufferBit); - // Basic Setup for viewing - UpdateProjection(); - UpdateView(); - GL.MatrixMode(MatrixMode.Projection); // Load Perspective - GL.LoadIdentity(); - GL.LoadMatrix(ref projection); - GL.MatrixMode(MatrixMode.Modelview); // Load Camera - GL.LoadIdentity(); - GL.LoadMatrix(ref view); - GL.Viewport(0, 0, Width, Height); // Size of window - GL.Enable(EnableCap.DepthTest); // Enable correct Z Drawings - GL.Enable(EnableCap.Texture2D); // Enable textures - GL.DepthFunc(DepthFunction.Lequal); // Enable correct Z Drawings - // GL.Disable(EnableCap.Blend) 'Disable transparent GL.Disable(EnableCap.AlphaTest); // Disable transparent } +#if DEBUG protected override bool ProcessDialogKey(Keys keyData) { -#if DEBUG switch (keyData) { case Keys.Escape: debugLabel.Visible = !debugLabel.Visible; return true; + case Keys.R: + Rotation = Vector2.Zero; + Refresh(); + return true; case Keys.F1: var fileDialog = new OpenFileDialog() { @@ -588,9 +404,9 @@ namespace PckStudio.Rendering } return true; } -#endif return base.ProcessDialogKey(keyData); } +#endif protected override void OnMouseDown(MouseEventArgs e) { @@ -645,25 +461,25 @@ namespace PckStudio.Rendering // Move the model if (IsRightMouseDown) { - float deltaX = -(Cursor.Position.X - MouseLoc.X) * 0.5f / (float)FieldOfView; - float deltaY = (Cursor.Position.Y - MouseLoc.Y) * 0.5f / (float)FieldOfView; + float deltaX = -(Cursor.Position.X - MouseLoc.X) * 0.05f / (float)FieldOfViewRadians; + float deltaY = (Cursor.Position.Y - MouseLoc.Y) * 0.05f / (float)FieldOfViewRadians; LookAngle += new Vector2(deltaX, deltaY); Refresh(); - Cursor.Position = new Point((int)Math.Round(Screen.PrimaryScreen.Bounds.Height / 2d), (int)Math.Round(Screen.PrimaryScreen.Bounds.Height / 2d)); + Cursor.Position = new Point((int)Math.Round(Screen.PrimaryScreen.Bounds.Width / 2d), (int)Math.Round(Screen.PrimaryScreen.Bounds.Height / 2d)); MouseLoc = Cursor.Position; } } protected override void OnMouseWheel(MouseEventArgs e) { - FieldOfView += e.Delta * 0.005d; + FieldOfViewRadians += e.Delta * 0.005d; Refresh(); base.OnMouseWheel(e); } - private Vector3 GetCameraPosition(Matrix4 modelview) + private Vector3 GetCameraPosition(Matrix4 view) { - return Matrix4.Invert(modelview).ExtractTranslation(); + return Matrix4.Invert(view).ExtractTranslation(); } private List MousePoints = new List(); @@ -672,665 +488,6 @@ namespace PckStudio.Rendering public event TextureChangedEventHandler TextureChanged; public delegate void TextureChangedEventHandler(object sender, bool IsLeft); - - public Point Get2DFrom3D(Vector3 Vector, ref Vector3 XUp, ref Vector3 YUp) - { - var Result = default(Point); - if (Vector.X < 4f && Vector.X > -4 && Vector.Y < 16f && Vector.Y > 8f && Vector.Z == 4f) - { - // ZHead - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 8f)), (int)Math.Round(Conversion.Int(-Vector.Y + 16f + 8f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 4f && Vector.X > -4 && Vector.Y < 16f && Vector.Y > 8f && Vector.Z == -4) - { - // ZHead - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 24f)), (int)Math.Round(Conversion.Int(-Vector.Y + 16f + 8f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 4f && Vector.X > -4 && Vector.Y < 8f && Vector.Y > -4 && Vector.Z == 2f) - { - // ZBody - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 20f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 20f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 4f && Vector.X > -4 && Vector.Y < 8f && Vector.Y > -4 && Vector.Z == -2) - { - // ZBody - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 32f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 20f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < -4 && Vector.X > -8 && Vector.Y < 8f && Vector.Y > -4 && Vector.Z == 2f) - { - // ZArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 48f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 20f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 47f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 20f))); - } - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < -4 && Vector.X > -8 && Vector.Y < 8f && Vector.Y > -4 && Vector.Z == -2) - { - // ZArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 44f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 20f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 43f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 20f))); - } - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 8f && Vector.X > 4f && Vector.Y < 8f && Vector.Y > -4 && Vector.Z == 2f) - { - // ZArms - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 28f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 52f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 8f && Vector.X > 4f && Vector.Y < 8f && Vector.Y > -4 && Vector.Z == -2) - { - // ZArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 48f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 52f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 46f)), (int)Math.Round(Conversion.Int(-Vector.Y + 8f + 52f))); - } - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 0f && Vector.X > -4 && Vector.Y < -4 && Vector.Y > -16 && Vector.Z == 2f) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 4f)), (int)Math.Round(Conversion.Int(-Vector.Y + 16f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 0f && Vector.X > -4 && Vector.Y < -4 && Vector.Y > -16 && Vector.Z == -2) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 8f)), (int)Math.Round(Conversion.Int(-Vector.Y + 16f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 4f && Vector.X > 0f && Vector.Y < -4 && Vector.Y > -16 && Vector.Z == 2f) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 4f + 16f)), (int)Math.Round(Conversion.Int(-Vector.Y + 48f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.X < 4f && Vector.X > 0f && Vector.Y < -4 && Vector.Y > -16 && Vector.Z == -2) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int(-Vector.X + 4f + 28f)), (int)Math.Round(Conversion.Int(-Vector.Y + 48f))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 4f && Vector.Z > -4 && Vector.Y < 16f && Vector.Y > 8f && Vector.X == 4f) - { - // XHead - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 20f)), (int)Math.Round(Conversion.Int(-Vector.Y + 24f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 4f && Vector.Z > -4 && Vector.Y < 16f && Vector.Y > 8f && Vector.X == -4) - { - // XHead - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 4f)), (int)Math.Round(Conversion.Int(-Vector.Y + 24f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < 8f && Vector.Y > -4 && Vector.X == 4f) - { - // XBody - if (ShowBody && !ShowLeftArm) - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 30f)), (int)Math.Round(Conversion.Int(-Vector.Y + 28f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 34f)), (int)Math.Round(Conversion.Int(-Vector.Y + 60f))); - } - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < 8f && Vector.Y > -4 && Vector.X == -4) - { - // XBody - if (ShowBody && !ShowRightArm) - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 18f)), (int)Math.Round(Conversion.Int(-Vector.Y + 28f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 50f)), (int)Math.Round(Conversion.Int(-Vector.Y + 28f))); - } - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < 8f && Vector.Y > -4 && Vector.X == 8f) - { - // XArms - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 42f)), (int)Math.Round(Conversion.Int(-Vector.Y + 60f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < 8f && Vector.Y > -4 && Vector.X == -8) - { - // XArms - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 42f)), (int)Math.Round(Conversion.Int(-Vector.Y + 28f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < 8f && Vector.Y > -4 && Vector.X == 7f) - { - // XArms - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 41f)), (int)Math.Round(Conversion.Int(-Vector.Y + 60f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < 8f && Vector.Y > -4 && Vector.X == -7) - { - // XArms - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 42f)), (int)Math.Round(Conversion.Int(-Vector.Y + 28f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < -4 && Vector.Y > -16 && Vector.X == 4f) - { - // XLeg - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 26f)), (int)Math.Round(Conversion.Int(-Vector.Y + 48f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < -4 && Vector.Y > -16 && Vector.X == -4) - { - // XLeg - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 2f)), (int)Math.Round(Conversion.Int(-Vector.Y + 16f))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.Y < -4 && Vector.Y > -16 && Vector.X == 0f) - { - // XLeg - if (ShowLeftLeg) - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.Z + 18f)), (int)Math.Round(Conversion.Int(-Vector.Y + 48f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(-Vector.Z + 10f)), (int)Math.Round(Conversion.Int(-Vector.Y + 16f))); - } - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (Vector.Z < 4f && Vector.Z > -4 && Vector.X < 4f && Vector.X > -4 && Vector.Y == 16f) - { - // YHead - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 12f)), (int)Math.Round(Conversion.Int(Vector.Z + 4f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowHead && Vector.Z < 4f && Vector.Z > -4 && Vector.X < 4f && Vector.X > -4 && Vector.Y == 8f) - { - // YHead - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 20f)), (int)Math.Round(Conversion.Int(Vector.Z + 4f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < 4f && Vector.X > -4 && Vector.Y == 8f) - { - // YBody - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 24f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowBody && (!ShowLeftLeg || !ShowRightLeg) && Vector.Z < 2f && Vector.Z > -2 && Vector.X < 4f && Vector.X > -4 && Vector.Y == -4) - { - // YBody - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 32f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < 8f && Vector.X > 4f && Vector.Y == 8f) - { - // YArms - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 32f)), (int)Math.Round(Conversion.Int(Vector.Z + 50f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < 8f && Vector.X > 4f && Vector.Y == -4) - { - // YArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 36f)), (int)Math.Round(Conversion.Int(Vector.Z + 50f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 35f)), (int)Math.Round(Conversion.Int(Vector.Z + 50f))); - } - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < -4 && Vector.X > -8 && Vector.Y == 8f) - { - // YArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 52f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 51f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - } - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < -4 && Vector.X > -8 && Vector.Y == -4) - { - // YArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 56f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 54f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - } - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowLeftLeg && !ShowBody && Vector.Z < 2f && Vector.Z > -2 && Vector.X < 4f && Vector.X > 0f && Vector.Y == -4) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 20f)), (int)Math.Round(Conversion.Int(Vector.Z + 50f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowRightLeg && !ShowBody && Vector.Z < 2f && Vector.Z > -2 && Vector.X < 0f && Vector.X > -4 && Vector.Y == -4) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 8f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < 4f && Vector.X > 0f && Vector.Y == -16) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 24f)), (int)Math.Round(Conversion.Int(Vector.Z + 50f))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (Vector.Z < 2f && Vector.Z > -2 && Vector.X < 0f && Vector.X > -4 && Vector.Y == -16) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int(Vector.X + 12f)), (int)Math.Round(Conversion.Int(Vector.Z + 18f))); - XUp.X = 1f; - YUp.Z = 1f; - } - - return Result; - } - - public Point Get2nd2DFrom3D(Vector3 Vector, ref Vector3 XUp, ref Vector3 YUp) - { - var Result = default(Point); - if (ShowHeadOverlay && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y < 16.24d && Vector.Y > 7.76d && Vector.Z == 4.24f) - { - // ZHead - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 40d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 16.24d) / 1.06d + 8d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowHeadOverlay && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y < 16.24d && Vector.Y > 7.76d && Vector.Z == -4.24f) - { - // ZHead - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 4.24d) / 1.06d + 56d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 16.24d) / 1.06d + 8d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowBodyOverlay && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.Z == 2.12f) - { - // ZBody - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 20d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowBodyOverlay && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.Z == -2.12f) - { - // ZBody - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 4.24d) / 1.06d + 32d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowRightArmOverlay && Vector.X < -3.88d && Vector.X > -8.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.Z == 2.12f) - { - // ZArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 3.88d) / 1.06d + 48d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 3.88d) / 1.06d + 47d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - } - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowRightArmOverlay && Vector.X < -3.88d && Vector.X > -8.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.Z == -2.12f) - { - // ZArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 3.88d) / 1.06d + 45d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 3.88d) / 1.06d + 44d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - } - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowLeftArmOverlay && Vector.X < 8.12d && Vector.X > 3.88d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.Z == 2.12f) - { - // ZArms - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 3.88d) / 1.06d + 45d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 52d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowLeftArmOverlay && Vector.X < 8.12d && Vector.X > 3.88d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.Z == -2.12f) - { - // ZArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 3.88d) / 1.06d + 64d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 52d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 3.88d) / 1.06d + 62d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 52d))); - } - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowRightLegOverlay && Vector.X < 0.12d && Vector.X > -4.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.Z == 2.12f) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.12d) / 1.06d + 4d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 16.36d) / 1.06d + 48d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowRightLegOverlay && Vector.X < 0.12d && Vector.X > -4.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.Z == -2.12f) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 4.12d) / 1.06d + 8d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 16.36d) / 1.06d + 48d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowLeftLegOverlay && Vector.X < 4.12d && Vector.X > -0.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.Z == 2.12f) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 0.12d) / 1.06d + 4d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 3.64d) / 1.06d + 52d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowLeftLegOverlay && Vector.X < 4.12d && Vector.X > -0.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.Z == -2.12f) - { - // ZLegs - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.X + 0.12d) / 1.06d + 16d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 3.64d) / 1.06d + 52d))); - XUp.X = 1f; - YUp.Y = 1f; - } - else if (ShowHeadOverlay && Vector.Z < 4.24d && Vector.Z > -4.24d && Vector.Y < 16.24d && Vector.Y > 7.76d && Vector.X == 4.24f) - { - // XHead - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 4.24d) / 1.06d + 48d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 16.24d) / 1.06d + 8d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowHeadOverlay && Vector.Z < 4.24d && Vector.Z > -4.24d && Vector.Y < 16.24d && Vector.Y > 7.76d && Vector.X == -4.24f) - { - // XHead - Result = new Point((int)Math.Round(Conversion.Int((Vector.Z + 4.24d) / 1.06d + 32d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 16.24d) / 1.06d + 8d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowBodyOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.X == 4.24f) - { - // XBody - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 28d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowBodyOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && Vector.X == -4.24f) - { - // XBody - Result = new Point((int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 16d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowLeftArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && (Vector.X == 8.12f || Vector.X == 7.09f)) - { - // XArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 56d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 52d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 55d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 52d))); - } - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowRightArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && (Vector.X == -8.12f || Vector.X == -7.09f)) - { - // XArms - Result = new Point((int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 40d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowLeftArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && (Vector.X == 3.88f || Vector.X == 3.91f)) - { - // XArms - Result = new Point((int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 48d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 52d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowRightArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < 8.36d && Vector.Y > -4.36d && (Vector.X == -3.88f || Vector.X == -3.91f)) - { - // XArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 48d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 47d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y + 8.36d) / 1.06d + 36d))); - } - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowLeftLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.X == 4.24f) - { - // XLeg - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 8d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 3.64d) / 1.06d + 52d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowRightLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.X == -4.24f) - { - // XLeg - Result = new Point((int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 3.64d) / 1.06d + 36d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowRightLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.X == 0.12f) - { - // XLeg - Result = new Point((int)Math.Round(Conversion.Int(((double)-Vector.Z + 2.12d) / 1.06d + 8d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 3.64d) / 1.06d + 36d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowLeftLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.Y < -3.64d && Vector.Y > -16.36d && Vector.X == -0.12f) - { - // XLeg - Result = new Point((int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d)), (int)Math.Round(Conversion.Int(((double)-Vector.Y - 3.64d) / 1.06d + 52d))); - XUp.Z = 1f; - YUp.Y = 1f; - } - else if (ShowHeadOverlay && Vector.Z < 4.24d && Vector.Z > -4.24d && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y == 16.24f) - { - // YHead - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 40d)), (int)Math.Round(Conversion.Int((Vector.Z + 4.24d) / 1.06d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowHeadOverlay && Vector.Z < 4.24d && Vector.Z > -4.24d && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y == 7.76f) - { - // YHead - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 48d)), (int)Math.Round(Conversion.Int((Vector.Z + 4.24d) / 1.06d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowBodyOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y == 8.36f) - { - // YBody - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 20d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowBodyOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 4.24d && Vector.X > -4.24d && Vector.Y == -4.36f) - { - // YBody - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 28d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowLeftArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 8.12d && Vector.X > 3.88d && Vector.Y == 8.36f) - { - // YArms - Result = new Point((int)Math.Round(Conversion.Int((Vector.X - 3.88d) / 1.06d + 52d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 48d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowLeftArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 8.12d && Vector.X > 3.88d && Vector.Y == -4.36f) - { - // YArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X - 3.88d) / 1.06d + 56d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 48d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X - 3.88d) / 1.06d + 55d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 48d))); - } - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowRightArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < -3.88d && Vector.X > -8.12d && Vector.Y == 8.36f) - { - // YArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 8.12d) / 1.06d + 44d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 8.12d) / 1.06d + 43d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - } - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowRightArmOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < -3.88d && Vector.X > -8.12d && Vector.Y == -4.36f) - { - // YArms - if (Model == Models.Steve) - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 8.12d) / 1.06d + 48d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - } - else - { - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 8.12d) / 1.06d + 46d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - } - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowLeftLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 4.24d && Vector.X > -0.12d && Vector.Y == -3.64f) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 0.12d) / 1.06d + 4d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 48d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowRightLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 0.12d && Vector.X > -4.24d && Vector.Y == -3.64f) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 4d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowLeftLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 4.24d && Vector.X > -0.12d && Vector.Y == -16.36f) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 0.12d) / 1.06d + 8d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 48d))); - XUp.X = 1f; - YUp.Z = 1f; - } - else if (ShowRightLegOverlay && Vector.Z < 2.12d && Vector.Z > -2.12d && Vector.X < 0.12d && Vector.X > -4.24d && Vector.Y == -16.36f) - { - // YLeg - Result = new Point((int)Math.Round(Conversion.Int((Vector.X + 4.24d) / 1.06d + 8d)), (int)Math.Round(Conversion.Int((Vector.Z + 2.12d) / 1.06d + 32d))); - XUp.X = 1f; - YUp.Z = 1f; - } - - return Result; - } - - public object Get2DFrom3D(Vector3 Vector) - { - var argXUp = new Vector3(); - var argYUp = new Vector3(); - return Get2DFrom3D(Vector, ref argXUp, ref argYUp); - } - public object Get2nd2DFrom3D(Vector3 Vector) - { - var argXUp = new Vector3(); - var argYUp = new Vector3(); - return Get2nd2DFrom3D(Vector, ref argXUp, ref argYUp); - } - - public enum SkinPlace - { - Head, - Body, - RArm, - LArm, - RLeg, - LLeg, - Head2, - Body2, - RArm2, - LArm2, - RLeg2, - LLeg2 - } } } diff --git a/PCK-Studio/Rendering/Vertex.cs b/PCK-Studio/Rendering/Vertex.cs new file mode 100644 index 00000000..8880a23f --- /dev/null +++ b/PCK-Studio/Rendering/Vertex.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using OpenTK; +using OpenTK.Graphics; + +namespace PckStudio.Rendering +{ + [StructLayout(LayoutKind.Sequential, Pack = 4)] + internal struct Vertex + { + public Vertex(Vector3 position, Color4 color, Vector2 texPosition) + { + Position = position; + Color = color; + TexPosition = texPosition; + } + + internal Vector3 Position { get; set; } + internal Color4 Color { get; set; } + internal Vector2 TexPosition { get; set; } + } +} diff --git a/PCK-Studio/Rendering/VertexArray.cs b/PCK-Studio/Rendering/VertexArray.cs index 9fc45a46..fe90a6e7 100644 --- a/PCK-Studio/Rendering/VertexArray.cs +++ b/PCK-Studio/Rendering/VertexArray.cs @@ -28,7 +28,7 @@ namespace PckStudio.Rendering var element = elements[i]; GL.EnableVertexAttribArray(i); GL.VertexAttribPointer(i, element.Count, element.Type, element.Normalize, layout.GetStride(), offset); - offset += layout.GetStride(); + offset += element.Count * VertexBufferElement.GetStrideSize(element.Type); } } diff --git a/PCK-Studio/Rendering/VertexBuffer.cs b/PCK-Studio/Rendering/VertexBuffer.cs index 38e4dd7d..88cf2402 100644 --- a/PCK-Studio/Rendering/VertexBuffer.cs +++ b/PCK-Studio/Rendering/VertexBuffer.cs @@ -10,15 +10,22 @@ using OpenTK.Graphics.OpenGL; namespace PckStudio.Rendering { - internal class VertexBuffer : IDisposable where T : struct + internal struct VertexBuffer : IDisposable where T : struct { private int _id; +#if DEBUG + private T[] __data; +#endif + public VertexBuffer(T[] data, int size) { _id = GL.GenBuffer(); Bind(); GL.BufferData(BufferTarget.ArrayBuffer, size, data, BufferUsageHint.StaticDraw); +#if DEBUG + __data = data; +#endif } public void Bind() diff --git a/PCK-Studio/Rendering/VertexBufferLayout.cs b/PCK-Studio/Rendering/VertexBufferLayout.cs index 33fc0fb3..c9f902ac 100644 --- a/PCK-Studio/Rendering/VertexBufferLayout.cs +++ b/PCK-Studio/Rendering/VertexBufferLayout.cs @@ -33,7 +33,7 @@ namespace PckStudio.Rendering } } - internal class VertexBufferLayout + internal struct VertexBufferLayout { private List elements; private int stride; diff --git a/PCK-Studio/Resources/fragment.glsl b/PCK-Studio/Resources/fragment.glsl index 5c13ed1c..0e178897 100644 --- a/PCK-Studio/Resources/fragment.glsl +++ b/PCK-Studio/Resources/fragment.glsl @@ -2,14 +2,18 @@ layout(location = 0) out vec4 color; -uniform vec4 u_Color; uniform sampler2D u_Texture; in vec2 v_TexCoord; +in vec4 v_VertexPos; +in vec4 v_Color; void main() { vec4 texColor = texture(u_Texture, v_TexCoord); - color = u_Color; + // color = vec4(v_VertexPos.xyz, 1.0); + // color = vec4(v_TexCoord, 0.0, 1.0) * vec4(v_VertexPos.xyz, 1.0); + // color = v_Color; + // color = vec4(v_TexCoord, 0.0, 1.0); color = texColor; }; \ No newline at end of file diff --git a/PCK-Studio/Resources/vertexShader.glsl b/PCK-Studio/Resources/vertexShader.glsl index a9ee9052..439dc0d1 100644 --- a/PCK-Studio/Resources/vertexShader.glsl +++ b/PCK-Studio/Resources/vertexShader.glsl @@ -1,14 +1,19 @@ #version 330 core layout(location = 0) in vec4 vertexPosition; -layout(location = 1) in vec2 texCoord; +layout(location = 1) in vec4 color; +layout(location = 2) in vec2 texCoord; uniform mat4 u_MVP; out vec2 v_TexCoord; +out vec4 v_VertexPos; +out vec4 v_Color; void main() { - gl_Position = u_MVP * vertexPosition; v_TexCoord = texCoord; + v_Color = color; + v_VertexPos = vertexPosition; + gl_Position = u_MVP * vertexPosition; }; \ No newline at end of file