From 2607c16972f3a5fc418e8e7f316ce9837df3e5ca Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:33:34 +0200 Subject: [PATCH 1/9] static class Linear: Solve linear systems --- org.niclasundharald.engine/Linear.cs | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 org.niclasundharald.engine/Linear.cs diff --git a/org.niclasundharald.engine/Linear.cs b/org.niclasundharald.engine/Linear.cs new file mode 100644 index 0000000..30988dd --- /dev/null +++ b/org.niclasundharald.engine/Linear.cs @@ -0,0 +1,65 @@ +using System; +using OpenTK; + +namespace org.niclasundharald.engine +{ + public static class Linear + { + static public bool Solve4x3(Vector3 X,Vector3 Y,Vector3 Z,Vector3 W,out Vector3 c) + { + Vector4[] rows = new Vector4[3]; + rows[0] = new Vector4(X.X,Y.X,Z.X,W.X); + rows[1] = new Vector4(X.Y,Y.Y,Z.Y,W.Y); + rows[2] = new Vector4(X.Z,Y.Z,Z.Z,W.Z); + c = new Vector3(); + + Console.WriteLine("Solver Unsolved:"); + Console.WriteLine("X: {0}",rows[0]); + Console.WriteLine("Y: {0}",rows[1]); + Console.WriteLine("Z: {0}",rows[2]); + + + if (rows[0].Z == 0){ + Vector4 t = rows[0]; + rows[0] = rows[2]; + rows[2] = t; + } + if (rows[0].Z == 0){ + Vector4 t = rows[0]; + rows[0] = rows[1]; + rows[1] = t; + } + + if (rows[0].Z == 0){ + return false; + } else { + rows[1] -= rows[0] * (rows[1].Z / rows[0].Z); + rows[2] -= rows[0] * (rows[2].Z / rows[0].Z); + } + + if (rows[1].Y == 0){ + Vector4 t = rows[1]; + rows[1] = rows[2]; + rows[2] = t; + } + + if (rows[1].Y == 0){ + return false; + } else { + rows[2] -= rows[1] * (rows[2].Y / rows[1].Y); + } + + c.X = rows[2].W / rows[2].X; + c.Y = (rows[1].W - (rows[1].X * c.X)) / rows[1].Y; + c.Z = (rows[0].W - (rows[0].X * c.X) - (rows[0].Y * c.Y)) / rows[0].Y; + + Console.WriteLine("Solver Solved:"); + Console.WriteLine("X: {0}",rows[0]); + Console.WriteLine("Y: {0}",rows[1]); + Console.WriteLine("Z: {0}",rows[2]); + Console.WriteLine("P: {0}",c); + + return true; + } + } +} From 8f39f12549779e776faa68ae8bf933990fbd25e5 Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:34:21 +0200 Subject: [PATCH 2/9] static class Geometry: static Geometric helper methods (e.g. intersection) --- org.niclasundharald.engine/Geometry.cs | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 org.niclasundharald.engine/Geometry.cs diff --git a/org.niclasundharald.engine/Geometry.cs b/org.niclasundharald.engine/Geometry.cs new file mode 100644 index 0000000..2578ae0 --- /dev/null +++ b/org.niclasundharald.engine/Geometry.cs @@ -0,0 +1,56 @@ +using System; + +using OpenTK; + +namespace org.niclasundharald.engine +{ + public static class Geometry + { + + /** + * Intersect Triangle and Ray: + * + * A + i * (B-A) + j * (C-A) = P + n * V + * A-P + i * (B-A) + j * (C-A) = n * V + * i * (B-A) + j * (C-A) - n * V = P-A + * + * => + * + * -n * V + i * (B-A) + j * (C-A) = P-A + * + * + **/ + public static bool intersectTriangle(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + Vector3 BA,CA,PA; + + BA = B-A; + CA = C-A; + PA = P-A; + + Console.WriteLine("Interesction:"); + Console.WriteLine("Ray: {0} + i * {1}",P,V); + Console.WriteLine("Triangle: {0}",A); + Console.WriteLine(" {0}",B); + Console.WriteLine(" {0}",C); + + + if (!Linear.Solve4x3(V,BA,CA,PA,out p)){ + return false; + } + + p.X = -p.X; + if (p.X < 0){ + return false; + } + + if ((p.Y < 0) || (p.Y > 1.0f) || (p.Z < 0) || (p.Z > 1.0f)){ + return false; + } + + p = P + (p.X * V); + + return true; + } + + } +} From 088453675f088bfc6bff1e71ce3c2d91ee794bb5 Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:34:38 +0200 Subject: [PATCH 3/9] ArrayHelper.Fill(..) --- org.niclasundharald.engine/ArrayHelper.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 org.niclasundharald.engine/ArrayHelper.cs diff --git a/org.niclasundharald.engine/ArrayHelper.cs b/org.niclasundharald.engine/ArrayHelper.cs new file mode 100644 index 0000000..98fedff --- /dev/null +++ b/org.niclasundharald.engine/ArrayHelper.cs @@ -0,0 +1,13 @@ +using System; + + +namespace org.niclasundharald.engine +{ + public static class ArrayHelper + { + public static void Fill(this T[] array,T value){ + for (int n=0;n Date: Sat, 6 May 2017 12:35:14 +0200 Subject: [PATCH 4/9] fix ballistisch.obj Zmax=0 --- NHEngine/models/ballistisch.obj | 198 ++++++++++++++++---------------- 1 file changed, 99 insertions(+), 99 deletions(-) diff --git a/NHEngine/models/ballistisch.obj b/NHEngine/models/ballistisch.obj index 6d35ef3..00ba638 100644 --- a/NHEngine/models/ballistisch.obj +++ b/NHEngine/models/ballistisch.obj @@ -2,70 +2,70 @@ # www.blender.org mtllib ballistisch.mtl o Cylinder -v 0.000000 -1.000000 -0.250000 -v 0.000000 1.000000 -0.250000 -v 0.048773 -1.000000 -0.245196 -v 0.048773 1.000000 -0.245196 -v 0.095671 -1.000000 -0.230970 -v 0.095671 1.000000 -0.230970 -v 0.138893 -1.000000 -0.207867 -v 0.138893 1.000000 -0.207867 -v 0.176777 -1.000000 -0.176777 -v 0.176777 1.000000 -0.176777 -v 0.207867 -1.000000 -0.138893 -v 0.207867 1.000000 -0.138893 -v 0.230970 -1.000000 -0.095671 -v 0.230970 1.000000 -0.095671 -v 0.245196 -1.000000 -0.048773 -v 0.245196 1.000000 -0.048773 -v 0.250000 -1.000000 -0.000000 -v 0.250000 1.000000 -0.000000 -v 0.245196 -1.000000 0.048773 -v 0.245196 1.000000 0.048773 -v 0.230970 -1.000000 0.095671 -v 0.230970 1.000000 0.095671 -v 0.207867 -1.000000 0.138893 -v 0.207867 1.000000 0.138893 -v 0.176777 -1.000000 0.176777 -v 0.176777 1.000000 0.176777 -v 0.138893 -1.000000 0.207867 -v 0.138893 1.000000 0.207867 -v 0.095671 -1.000000 0.230970 -v 0.095671 1.000000 0.230970 -v 0.048773 -1.000000 0.245196 -v 0.048773 1.000000 0.245196 -v -0.000000 -1.000000 0.250000 -v -0.000000 1.000000 0.250000 -v -0.048773 -1.000000 0.245196 -v -0.048773 1.000000 0.245196 -v -0.095671 -1.000000 0.230970 -v -0.095671 1.000000 0.230970 -v -0.138893 -1.000000 0.207867 -v -0.138893 1.000000 0.207867 -v -0.176777 -1.000000 0.176777 -v -0.176777 1.000000 0.176777 -v -0.207868 -1.000000 0.138892 -v -0.207868 1.000000 0.138892 -v -0.230970 -1.000000 0.095671 -v -0.230970 1.000000 0.095671 -v -0.245196 -1.000000 0.048772 -v -0.245196 1.000000 0.048772 -v -0.250000 -1.000000 -0.000000 -v -0.250000 1.000000 -0.000000 -v -0.245196 -1.000000 -0.048773 -v -0.245196 1.000000 -0.048773 -v -0.230970 -1.000000 -0.095671 -v -0.230970 1.000000 -0.095671 -v -0.207867 -1.000000 -0.138893 -v -0.207867 1.000000 -0.138893 -v -0.176776 -1.000000 -0.176777 -v -0.176776 1.000000 -0.176777 -v -0.138892 -1.000000 -0.207868 -v -0.138892 1.000000 -0.207868 -v -0.095671 -1.000000 -0.230970 -v -0.095671 1.000000 -0.230970 -v -0.048772 -1.000000 -0.245196 -v -0.048772 1.000000 -0.245196 +v 0.000000 -4.000000 -0.250000 +v 0.000000 -2.000000 -0.250000 +v 0.048773 -4.000000 -0.245196 +v 0.048773 -2.000000 -0.245196 +v 0.095671 -4.000000 -0.230970 +v 0.095671 -2.000000 -0.230970 +v 0.138893 -4.000000 -0.207867 +v 0.138893 -2.000000 -0.207867 +v 0.176777 -4.000000 -0.176777 +v 0.176777 -2.000000 -0.176777 +v 0.207867 -4.000000 -0.138893 +v 0.207867 -2.000000 -0.138893 +v 0.230970 -4.000000 -0.095671 +v 0.230970 -2.000000 -0.095671 +v 0.245196 -4.000000 -0.048773 +v 0.245196 -2.000000 -0.048773 +v 0.250000 -4.000000 -0.000000 +v 0.250000 -2.000000 -0.000000 +v 0.245196 -4.000000 0.048773 +v 0.245196 -2.000000 0.048773 +v 0.230970 -4.000000 0.095671 +v 0.230970 -2.000000 0.095671 +v 0.207867 -4.000000 0.138893 +v 0.207867 -2.000000 0.138893 +v 0.176777 -4.000000 0.176777 +v 0.176777 -2.000000 0.176777 +v 0.138893 -4.000000 0.207867 +v 0.138893 -2.000000 0.207867 +v 0.095671 -4.000000 0.230970 +v 0.095671 -2.000000 0.230970 +v 0.048773 -4.000000 0.245196 +v 0.048773 -2.000000 0.245196 +v -0.000000 -4.000000 0.250000 +v -0.000000 -2.000000 0.250000 +v -0.048773 -4.000000 0.245196 +v -0.048773 -2.000000 0.245196 +v -0.095671 -4.000000 0.230970 +v -0.095671 -2.000000 0.230970 +v -0.138893 -4.000000 0.207867 +v -0.138893 -2.000000 0.207867 +v -0.176777 -4.000000 0.176777 +v -0.176777 -2.000000 0.176777 +v -0.207868 -4.000000 0.138892 +v -0.207868 -2.000000 0.138892 +v -0.230970 -4.000000 0.095671 +v -0.230970 -2.000000 0.095671 +v -0.245196 -4.000000 0.048772 +v -0.245196 -2.000000 0.048772 +v -0.250000 -4.000000 -0.000000 +v -0.250000 -2.000000 -0.000000 +v -0.245196 -4.000000 -0.048773 +v -0.245196 -2.000000 -0.048773 +v -0.230970 -4.000000 -0.095671 +v -0.230970 -2.000000 -0.095671 +v -0.207867 -4.000000 -0.138893 +v -0.207867 -2.000000 -0.138893 +v -0.176776 -4.000000 -0.176777 +v -0.176776 -2.000000 -0.176777 +v -0.138892 -4.000000 -0.207868 +v -0.138892 -2.000000 -0.207868 +v -0.095671 -4.000000 -0.230970 +v -0.095671 -2.000000 -0.230970 +v -0.048772 -4.000000 -0.245196 +v -0.048772 -2.000000 -0.245196 vn 0.0980 0.0000 -0.9952 vn 0.2903 0.0000 -0.9569 vn 0.4714 0.0000 -0.8819 @@ -96,7 +96,7 @@ vn -0.8819 0.0000 -0.4714 vn -0.7730 0.0000 -0.6344 vn -0.6344 0.0000 -0.7730 vn -0.4714 0.0000 -0.8819 -vn 0.0000 1.0000 0.0000 +vn 0.0000 1.0000 -0.0000 vn -0.2903 0.0000 -0.9569 vn -0.0980 0.0000 -0.9952 vn 0.0000 -1.0000 -0.0000 @@ -137,39 +137,39 @@ f 61//32 62//32 64//32 63//32 f 63//33 64//33 2//33 1//33 f 1//34 3//34 5//34 7//34 9//34 11//34 13//34 15//34 17//34 19//34 21//34 23//34 25//34 27//34 29//34 31//34 33//34 35//34 37//34 39//34 41//34 43//34 45//34 47//34 49//34 51//34 53//34 55//34 57//34 59//34 61//34 63//34 o Cone -v -0.000000 0.939394 -0.250000 -v 0.048773 0.939394 -0.245196 -v 0.095671 0.939394 -0.230970 -v 0.138893 0.939394 -0.207867 -v 0.176777 0.939394 -0.176777 -v 0.207867 0.939394 -0.138892 -v 0.230970 0.939394 -0.095671 -v 0.245196 0.939394 -0.048773 -v 0.250000 0.939394 0.000000 -v -0.000000 2.939394 0.000000 -v 0.245196 0.939394 0.048773 -v 0.230970 0.939394 0.095671 -v 0.207867 0.939394 0.138893 -v 0.176777 0.939394 0.176777 -v 0.138893 0.939394 0.207867 -v 0.095671 0.939394 0.230970 -v 0.048772 0.939394 0.245196 -v -0.000000 0.939394 0.250000 -v -0.048773 0.939394 0.245196 -v -0.095671 0.939394 0.230970 -v -0.138893 0.939394 0.207867 -v -0.176777 0.939394 0.176777 -v -0.207868 0.939394 0.138892 -v -0.230970 0.939394 0.095671 -v -0.245196 0.939394 0.048772 -v -0.250000 0.939394 -0.000000 -v -0.245196 0.939394 -0.048773 -v -0.230970 0.939394 -0.095671 -v -0.207867 0.939394 -0.138893 -v -0.176776 0.939394 -0.176777 -v -0.138892 0.939394 -0.207868 -v -0.095671 0.939394 -0.230970 -v -0.048772 0.939394 -0.245196 +v -0.000000 -2.060606 -0.250000 +v 0.048773 -2.060606 -0.245196 +v 0.095671 -2.060606 -0.230970 +v 0.138893 -2.060606 -0.207867 +v 0.176777 -2.060606 -0.176777 +v 0.207867 -2.060606 -0.138892 +v 0.230970 -2.060606 -0.095671 +v 0.245196 -2.060606 -0.048773 +v 0.250000 -2.060606 0.000000 +v -0.000000 -0.060606 0.000000 +v 0.245196 -2.060606 0.048773 +v 0.230970 -2.060606 0.095671 +v 0.207867 -2.060606 0.138893 +v 0.176777 -2.060606 0.176777 +v 0.138893 -2.060606 0.207867 +v 0.095671 -2.060606 0.230970 +v 0.048772 -2.060606 0.245196 +v -0.000000 -2.060606 0.250000 +v -0.048773 -2.060606 0.245196 +v -0.095671 -2.060606 0.230970 +v -0.138893 -2.060606 0.207867 +v -0.176777 -2.060606 0.176777 +v -0.207868 -2.060606 0.138892 +v -0.230970 -2.060606 0.095671 +v -0.245196 -2.060606 0.048772 +v -0.250000 -2.060606 -0.000000 +v -0.245196 -2.060606 -0.048773 +v -0.230970 -2.060606 -0.095671 +v -0.207867 -2.060606 -0.138893 +v -0.176776 -2.060606 -0.176777 +v -0.138892 -2.060606 -0.207868 +v -0.095671 -2.060606 -0.230970 +v -0.048772 -2.060606 -0.245196 vn 0.0973 0.1234 -0.9876 vn 0.2881 0.1234 -0.9496 vn 0.4678 0.1234 -0.8752 @@ -202,7 +202,7 @@ vn -0.6295 0.1234 -0.7671 vn -0.4678 0.1234 -0.8752 vn -0.2881 0.1234 -0.9496 vn -0.0973 0.1234 -0.9876 -vn 0.0000 -1.0000 0.0000 +vn 0.0000 -1.0000 -0.0000 usemtl None s off f 65//35 74//35 66//35 From d2a6f886b2941fc65acc353e409d752849ac332e Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:36:00 +0200 Subject: [PATCH 5/9] Project File update (ArrayHelper,Geometry,Linear) --- org.niclasundharald.engine/org.niclasundharald.engine.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/org.niclasundharald.engine/org.niclasundharald.engine.csproj b/org.niclasundharald.engine/org.niclasundharald.engine.csproj index 1882432..b6a0f87 100644 --- a/org.niclasundharald.engine/org.niclasundharald.engine.csproj +++ b/org.niclasundharald.engine/org.niclasundharald.engine.csproj @@ -68,6 +68,9 @@ + + + From 1f0b9909891406b4146a23e0eed95ccaa215cfe0 Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:36:31 +0200 Subject: [PATCH 6/9] Change Position of demo object on map --- NHEngine/BootStrap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NHEngine/BootStrap.cs b/NHEngine/BootStrap.cs index 6dec481..ad6ea25 100644 --- a/NHEngine/BootStrap.cs +++ b/NHEngine/BootStrap.cs @@ -70,7 +70,7 @@ namespace nhengine DumpObject dobj = new DumpObject(); dobj.Model3D = ModelManager.instance.loadModel("alfa147",0.0254f); - Vector3 v3 = map.ground(new Vector2(40,40)); + Vector3 v3 = map.ground(new Vector2(24,24)); v3.Z *= 2; dobj.Position = v3; From 18b2099542f74e53192c679fc6d47dbf1ded7c8d Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Sat, 6 May 2017 12:37:18 +0200 Subject: [PATCH 7/9] Model3D improvements --- .../graphics/LoadedModel.cs | 10 +- .../graphics/Model3D.cs | 216 ++++++++++----- .../graphics/SquaredMap.cs | 245 +++++++++--------- .../graphics/primitives/Cube.cs | 36 +-- 4 files changed, 297 insertions(+), 210 deletions(-) diff --git a/org.niclasundharald.engine/graphics/LoadedModel.cs b/org.niclasundharald.engine/graphics/LoadedModel.cs index 98190e8..3b3b8dd 100644 --- a/org.niclasundharald.engine/graphics/LoadedModel.cs +++ b/org.niclasundharald.engine/graphics/LoadedModel.cs @@ -27,7 +27,6 @@ namespace org.niclasundharald.engine.graphics } private void load(TextReader reader,float scale){ List vertexes,normals; - List colors; List triangles; string line; @@ -36,7 +35,6 @@ namespace org.niclasundharald.engine.graphics vertexes = new List(); normals = new List(); - colors = new List(); triangles = new List(); vertexes.Add(new Vector3()); @@ -66,7 +64,7 @@ namespace org.niclasundharald.engine.graphics max[2] = max[2] < z ? z : max[2]; break; case "vn": - normals.Add( -(new Vector3( + normals.Add( (new Vector3( float.Parse(fields[1],CultureInfo.InvariantCulture), float.Parse(fields[2],CultureInfo.InvariantCulture), float.Parse(fields[3],CultureInfo.InvariantCulture) @@ -107,8 +105,10 @@ namespace org.niclasundharald.engine.graphics normals.Add( t.nc ); } - // bind(vertexes.ToArray(),null,normals.ToArray()); - bind(vertexes.ToArray(),null,normals.ToArray()); + this.vertexes = vertexes.ToArray(); + this.normals = normals.ToArray(); + + bind(); } diff --git a/org.niclasundharald.engine/graphics/Model3D.cs b/org.niclasundharald.engine/graphics/Model3D.cs index ff3fadd..a1cc622 100644 --- a/org.niclasundharald.engine/graphics/Model3D.cs +++ b/org.niclasundharald.engine/graphics/Model3D.cs @@ -3,6 +3,8 @@ using System; using OpenTK.Graphics.OpenGL; using OpenTK; +using org.niclasundharald.engine; + namespace org.niclasundharald.engine.graphics { public class Model3D { @@ -14,9 +16,11 @@ namespace org.niclasundharald.engine.graphics { protected int nTriangles; - Vector3[] vertexes; - Vector4[] colors; - Vector3[] normals; + public Vector3[] vertexes = null; + public Vector4[] colors = null; + public Vector3[] normals = null; + + public Triangle[] triangles = new Triangle[0]; protected Model3D(){ @@ -60,65 +64,96 @@ namespace org.niclasundharald.engine.graphics { GL.BindVertexArray(0); } - protected void rebind(){ - + public void prepareBuffers(int nTriangles){ + this.vertexes = new Vector3[nTriangles * 3]; + this.normals = new Vector3[nTriangles * 3]; + this.colors = new Vector4[nTriangles * 3]; + + this.vertexes.Fill(new Vector3(0,0,0)); + this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f)); + + this.nTriangles = nTriangles; + rebind(); } - protected void bind(Vector3[] vertexes){ - bind( vertexes, null, null, null ); - } - - protected void bind(Vector3[] vertexes,Vector4[] colors){ - bind( vertexes, colors, null, null ); - } - - protected void bind(Vector3[] vertexes,Vector4[] colors,Vector3[] normals){ - bind( vertexes, colors, normals, null ); - } - - protected void bind(Vector3[] vertexes,Vector4[] colors,Vector3[] normals,Vector2[] uvs){ -/* float[] fv = new float[vertexes.Length * 3]; - - for (int n=0;n(ref this.triangles,nTriangles); - for (int n=0;n=width)||(xy.Y>=height)){ throw new OutOfWorldException(); } - float h = heightMap[i1] + - heightMap[i1 + 1] + - heightMap[i2] + - heightMap[i2 + 1]; + int i1 = tileIndex((int)xy.X,(int)xy.Y); - h /= 4; + for (int n=0;n<4;n++){ + Vector3 A,B,C,P,V; + Vector3 p = new Vector3(); - Vector3 v3 = new Vector3(_xy.X,_xy.Y,h); - Console.WriteLine("Grounding: {0}",v3); + A = model.triangles[i1+n].VertexA; + B = model.triangles[i1+n].VertexB; + C = model.triangles[i1+n].VertexC; - return v3; + P = new Vector3(_xy); + V = Vector3.UnitZ; + + if (Geometry.intersectTriangle(P,V,A,B,C,out p)){ + Console.WriteLine("Grounded to: {0}",p); + return p; + } else { + Console.WriteLine("No Ground at triangle {0}",n); + } + + } + + throw new OutOfWorldException(); } } class MapModel3D : Model3D { - int nDraw; - public MapModel3D(Vector3[] vertexes){ + SquaredMap map; - Vector4[] colors = new Vector4[ vertexes.Length ]; + public MapModel3D(SquaredMap map,int width,int height){ + this.map = map; + prepareBuffers(width * height * 4); + colorMap(); + } + + public void colorMap(){ + for (int n=0; n < vertexes.Length; n++){ + colors[n] = new Vector4( + 0.50f + (0.2f * vertexes[n].Z / SquaredMap.maxHeight), + 0.25f + (0.50f * vertexes[n].Z / SquaredMap.maxHeight), + 0.10f + (0.80f * vertexes[n].Z / SquaredMap.maxHeight), + 1.0f + ); + } + } + + + /* Vector4[] colors = new Vector4[ vertexes.Length ]; for (int n=0; n< vertexes.Length; n++){ @@ -240,20 +252,7 @@ namespace org.niclasundharald.engine.graphics bind(vertexes, colors, normals); } - - public override void draw() - { - nDraw+= nTriangles >> 8; - if (nDraw > nTriangles){ - nDraw = 0; - } - - nDraw = nTriangles; - - GL.BindVertexArray(this.vao); - GL.DrawArrays(PrimitiveType.Triangles, 0, nDraw * 3); - GL.BindVertexArray(0); - } +*/ } diff --git a/org.niclasundharald.engine/graphics/primitives/Cube.cs b/org.niclasundharald.engine/graphics/primitives/Cube.cs index 754bc82..a3b9df7 100644 --- a/org.niclasundharald.engine/graphics/primitives/Cube.cs +++ b/org.niclasundharald.engine/graphics/primitives/Cube.cs @@ -21,7 +21,7 @@ namespace org.niclasundharald.engine.graphics.primitives class CubeModel : Model3D { - public static Vector4[] colors = new Vector4[]{ + public static Vector4[] ccolors = new Vector4[]{ new Vector4(1.0f,0.0f,0.0f,1.0f), new Vector4(1.0f,0.0f,0.0f,1.0f), new Vector4(1.0f,0.0f,0.0f,1.0f), @@ -55,30 +55,30 @@ namespace org.niclasundharald.engine.graphics.primitives g = dx + dy + dz; h = - dx + dy + dz; - Vector3[] vertexes = new Vector3[36]; + prepareBuffers(12); - Model3D.setTriangle(vertexes, 0, a, b, c); - Model3D.setTriangle(vertexes, 1, c, d, a); - Model3D.setTriangle(vertexes, 2, f, e, h); - Model3D.setTriangle(vertexes, 3, h, g, f); + Model3D.setTriangle(vertexes, 0, c, b, a); + Model3D.setTriangle(vertexes, 1, a, d, c); + Model3D.setTriangle(vertexes, 2, h, e, f); + Model3D.setTriangle(vertexes, 3, f, g, h); - Model3D.setTriangle(vertexes, 4, e, a, d); - Model3D.setTriangle(vertexes, 5, d, h, e); - Model3D.setTriangle(vertexes, 6, b, f, g); - Model3D.setTriangle(vertexes, 7, g, c, b); + Model3D.setTriangle(vertexes, 4, d, a, e); + Model3D.setTriangle(vertexes, 5, e, h, d); + Model3D.setTriangle(vertexes, 6, g, f, b); + Model3D.setTriangle(vertexes, 7, b, c, g); - Model3D.setTriangle(vertexes, 8, a, e, f); - Model3D.setTriangle(vertexes, 9, f, b, a); - Model3D.setTriangle(vertexes, 10, g, h, d); - Model3D.setTriangle(vertexes, 11, d, c, g); - - Vector4[] colors = new Vector4[ vertexes.Length ]; + Model3D.setTriangle(vertexes, 8, f, e, a); + Model3D.setTriangle(vertexes, 9, a, b, f); + Model3D.setTriangle(vertexes, 10, d, h, g); + Model3D.setTriangle(vertexes, 11, g, c, d); for (int n=0;n Date: Sat, 6 May 2017 12:37:36 +0200 Subject: [PATCH 8/9] BallisticActor: Recolor demo model --- org.niclasundharald.engine/BallisticActor.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/org.niclasundharald.engine/BallisticActor.cs b/org.niclasundharald.engine/BallisticActor.cs index 90fd0de..946b730 100644 --- a/org.niclasundharald.engine/BallisticActor.cs +++ b/org.niclasundharald.engine/BallisticActor.cs @@ -12,6 +12,12 @@ namespace org.niclasundharald.engine :base(id) { this.Model3D = ModelManager.instance.findModel("ballistisch"); + + for (int n=0;n < this.Model3D.vertexes.Length;n++){ + this.Model3D.colors[n] = new Vector4(1.0f,0.2f + (0.5f * this.Model3D.vertexes[n].Z / -4.0f),0,1.0f); + } + + this.Model3D.rebind(); } public override void update(float timespan) From 93246b70a0ad5d17010aaa13e6471c68d40ea41a Mon Sep 17 00:00:00 2001 From: Harald Christian Joachim Wolff Date: Mon, 8 May 2017 14:30:41 +0200 Subject: [PATCH 9/9] WIP --- NHEngine/BootStrap.cs | 1 - NHEngine/OpenGLWindow.cs | 67 +++++-- NHEngine/nhengine.csproj | 4 + nhengine.sln | 6 + org.hwo.contracts/Conditional.cs | 78 ++++++++ org.hwo.contracts/Properties/AssemblyInfo.cs | 26 +++ org.hwo.contracts/org.hwo.contracts.csproj | 37 ++++ org.niclasundharald.engine/Geometry.cs | 78 ++++++-- org.niclasundharald.engine/Linear.cs | 13 +- .../graphics/GLCamera.cs | 48 ++++- .../graphics/GlobalDefaults.cs | 11 +- .../graphics/LoadedModel.cs | 6 +- .../graphics/Model3D.cs | 44 +++++ .../graphics/SquaredMap.cs | 169 ++++++++++++------ .../org.niclasundharald.engine.csproj | 6 + 15 files changed, 496 insertions(+), 98 deletions(-) create mode 100644 org.hwo.contracts/Conditional.cs create mode 100644 org.hwo.contracts/Properties/AssemblyInfo.cs create mode 100644 org.hwo.contracts/org.hwo.contracts.csproj diff --git a/NHEngine/BootStrap.cs b/NHEngine/BootStrap.cs index ad6ea25..f9068a5 100644 --- a/NHEngine/BootStrap.cs +++ b/NHEngine/BootStrap.cs @@ -71,7 +71,6 @@ namespace nhengine DumpObject dobj = new DumpObject(); dobj.Model3D = ModelManager.instance.loadModel("alfa147",0.0254f); Vector3 v3 = map.ground(new Vector2(24,24)); - v3.Z *= 2; dobj.Position = v3; glWindow.Scene.RootObject.addChild( dobj ); diff --git a/NHEngine/OpenGLWindow.cs b/NHEngine/OpenGLWindow.cs index e42f8aa..b54598b 100644 --- a/NHEngine/OpenGLWindow.cs +++ b/NHEngine/OpenGLWindow.cs @@ -27,12 +27,13 @@ namespace nhengine private Cube cube, c1, c2, c3; + private string titleMsg = ""; GLCamera sceneCamera; GLScene scene; public OpenGLWindow() - :base(800, 600, + :base(600, 600, GraphicsMode.Default, "nhEngine", GameWindowFlags.Default, @@ -59,14 +60,11 @@ namespace nhengine c2 = new Cube(new Vector3(0,150,150), 8.0f); c3 = new Cube(new Vector3(175,175,150), 8.0f); - lookAt = new Vector3(0,0,128.0f); + lookAt = new Vector3(0,0,SquaredMap.maxHeight); lookDistance = 2048; arcUpDown = MathHelper.PiOver6; arcLeftRight = MathHelper.PiOver4; -// sceneCamera.Position = new Vector3(1000,-1000,1000); -// sceneCamera.View = new Vector3(0f,1.0f,0.0f); - sceneCamera.Top = new Vector3(0,0,1); sceneCamera.setFoV(MathHelper.PiOver4); setupCamera(); @@ -95,7 +93,7 @@ namespace nhengine SwapBuffers(); - Title = string.Format("{0:F}/s {1:F}/s [{2:F}s]", RenderFrequency,UpdateFrequency,UpdatePeriod); + Title = string.Format("{0:F}/s {1:F}/s [{2:F}s] {3}", RenderFrequency,UpdateFrequency,UpdatePeriod,titleMsg); } protected override void OnUpdateFrame(FrameEventArgs e) @@ -129,6 +127,8 @@ namespace nhengine Point delta = new Point(e.X - mouseCapturePosition.X, e.Y - mouseCapturePosition.Y); onMouseCapturedMove(delta); mouseCapturePosition = e.Position; + } else { + markMouse(e.X,e.Y); } } @@ -141,6 +141,7 @@ namespace nhengine { switch (e.Button){ case OpenTK.Input.MouseButton.Left: + markMouse(e.X,e.Y); break; case OpenTK.Input.MouseButton.Right: captureMouse(); @@ -148,6 +149,23 @@ namespace nhengine } } + protected void markMouse(float x,float y){ + try { + Vector3 P = sceneCamera.Position; + Vector3 V = sceneCamera.pickRay(x,y); + + Vector3 isect = SquaredMap.activeMap.intersect(P,V); + Vector2 tile = SquaredMap.activeMap.toTile(isect.Xy); + + SquaredMap.activeMap.highlight(tile); + + + } catch (OutOfWorldException owe){ + Console.WriteLine(owe); + } + + } + protected override void OnMouseUp(OpenTK.Input.MouseButtonEventArgs e) { releaseMouse(); @@ -156,7 +174,7 @@ namespace nhengine protected override void OnMouseWheel(OpenTK.Input.MouseWheelEventArgs e) { lookDistance *= 1.0f - ( e.DeltaPrecise / 16 ); - MathHelper.Clamp( lookDistance, 512, 8192 ); + lookDistance = MathHelper.Clamp( lookDistance, 4, 8192 ); setupCamera(); } @@ -186,16 +204,34 @@ namespace nhengine walk(e.Shift ? -50.0f : -5.0f); break; case OpenTK.Input.Key.A: - strafe(e.Shift ? -50.0f : -5.0f); + strafe(e.Shift ? 50.0f : 5.0f); break; case OpenTK.Input.Key.D: - strafe(e.Shift ? 50.0f : 5.0f); + strafe(e.Shift ? -50.0f : -5.0f); break; case OpenTK.Input.Key.Space: fireBallistic(); break; - + + case OpenTK.Input.Key.Number1: + lookAt = new Vector3(0,0,SquaredMap.maxHeight); + arcLeftRight = 0; + arcUpDown = MathHelper.PiOver2; + setupCamera(); + break; + case OpenTK.Input.Key.Number2: + lookAt = new Vector3(0,0,SquaredMap.maxHeight); + arcLeftRight = MathHelper.PiOver2; + arcUpDown = MathHelper.PiOver2; + setupCamera(); + break; + case OpenTK.Input.Key.Number3: + lookAt = new Vector3(0,0,SquaredMap.maxHeight); + sceneCamera.View = new Vector3(1,1,-1); + sceneCamera.Top = new Vector3(0,0,1); + break; + } } @@ -204,7 +240,7 @@ namespace nhengine BallisticActor ba = new BallisticActor(0); ba.Position = BootStrap.instance().SquaredMap.ground(new Vector2(10,10)); - ba.Velocity = new Vector3(1,1,1);// + (Matrix4.CreateRotationZ(arcLeftRight) * Vector4.UnitY).Xyz; + ba.Velocity = new Vector3(0,1,1);// + (Matrix4.CreateRotationZ(arcLeftRight) * Vector4.UnitY).Xyz; ba.Velocity.Normalize(); ba.Velocity *= 50; } @@ -213,7 +249,7 @@ namespace nhengine public void rotateUpDown(float arc) { arcUpDown += arc; - arcUpDown = MathHelper.Clamp(arcUpDown,MathHelper.Pi / 8,MathHelper.PiOver2-0.00001f); + arcUpDown = MathHelper.Clamp(arcUpDown,MathHelper.Pi / 16,MathHelper.PiOver2); setupCamera(); } public void rotateLeftRight(float arc) @@ -238,12 +274,17 @@ namespace nhengine } private void setupCamera(){ - Vector3 view = (Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown) * Vector4.UnitY).Xyz; + Matrix4 mr = (Matrix4.CreateRotationZ(arcLeftRight) * Matrix4.CreateRotationX(arcUpDown)); + Vector3 view = (mr * Vector4.UnitY).Xyz; + Vector3 top = (mr * Vector4.UnitZ).Xyz; + view.Normalize(); + top.Normalize(); Vector3 pos = lookAt - (view * lookDistance); sceneCamera.View = view; + sceneCamera.Top = top; sceneCamera.Position = pos; } diff --git a/NHEngine/nhengine.csproj b/NHEngine/nhengine.csproj index d25d4b8..17c528f 100644 --- a/NHEngine/nhengine.csproj +++ b/NHEngine/nhengine.csproj @@ -88,6 +88,10 @@ {3E812F66-D5F3-4599-8360-97F355B6CC1B} org.niclasundharald.engine + + {56733EC1-7D97-48D0-AA4C-98EA624A5A21} + org.hwo.contracts + \ No newline at end of file diff --git a/nhengine.sln b/nhengine.sln index 226eb9d..6768fb0 100644 --- a/nhengine.sln +++ b/nhengine.sln @@ -9,6 +9,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "contrib", "contrib", "{9BA2 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageSharp", "ImageSharp\ImageSharp.csproj", "{4D1A7C04-2C4E-4C74-AD81-FE0DCA9C6DF0}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "org.hwo.contracts", "org.hwo.contracts\org.hwo.contracts.csproj", "{56733EC1-7D97-48D0-AA4C-98EA624A5A21}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {4D1A7C04-2C4E-4C74-AD81-FE0DCA9C6DF0}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D1A7C04-2C4E-4C74-AD81-FE0DCA9C6DF0}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D1A7C04-2C4E-4C74-AD81-FE0DCA9C6DF0}.Release|Any CPU.Build.0 = Release|Any CPU + {56733EC1-7D97-48D0-AA4C-98EA624A5A21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {56733EC1-7D97-48D0-AA4C-98EA624A5A21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {56733EC1-7D97-48D0-AA4C-98EA624A5A21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {56733EC1-7D97-48D0-AA4C-98EA624A5A21}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {4D1A7C04-2C4E-4C74-AD81-FE0DCA9C6DF0} = {9BA2E63F-E39C-465C-AC48-45B30B532A0C} diff --git a/org.hwo.contracts/Conditional.cs b/org.hwo.contracts/Conditional.cs new file mode 100644 index 0000000..2d51975 --- /dev/null +++ b/org.hwo.contracts/Conditional.cs @@ -0,0 +1,78 @@ +using System; +using System.Threading; + +namespace org.hwo.contracts +{ + public delegate void operDelegate(); + public delegate T assignDelegate(); + + public class Conditional + { + T valSuccess, + valFail; + + bool success; + + public Conditional(T valSuccess,T valFail) + { + this.valSuccess = valSuccess; + this.valFail = valFail; + this.success = true; + } + + public Conditional requires(bool condition){ + if (success){ + success = condition; + } + return this; + } + + public Conditional sets(ref ST target,ST value){ + if (success){ + target = value; + } + return this; + } + + public Conditional does(operDelegate operation){ + if (this.success){ + operation(); + } + return this; + } + + public Conditional assigns(ref AT target,assignDelegate assigner){ + if (success){ + target = assigner(); + } + return this; + } + + public bool isSuccess(){ + return this.success; + } + + public Conditional throws(string message) where E: Exception{ + if (!success){ + E ex = (E)typeof(E).GetConstructor(new Type[]{typeof(string)}).Invoke(new object[]{message}); + throw ex; + } + return this; + } + + public Conditional throws() where E: Exception{ + if (!success){ + E ex = (E)typeof(E).GetConstructor(new Type[]{}).Invoke(new object[]{}); + throw ex; + } + return this; + } + + } + + public class BooleanConditional : Conditional{ + public BooleanConditional() + :base(true,false){ + } + } +} diff --git a/org.hwo.contracts/Properties/AssemblyInfo.cs b/org.hwo.contracts/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f8080c1 --- /dev/null +++ b/org.hwo.contracts/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("org.hwo.contracts")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("${AuthorCopyright}")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/org.hwo.contracts/org.hwo.contracts.csproj b/org.hwo.contracts/org.hwo.contracts.csproj new file mode 100644 index 0000000..ee3bbdc --- /dev/null +++ b/org.hwo.contracts/org.hwo.contracts.csproj @@ -0,0 +1,37 @@ + + + + Debug + AnyCPU + {56733EC1-7D97-48D0-AA4C-98EA624A5A21} + Library + org.hwo.contracts + org.hwo.contracts + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + + + + + + \ No newline at end of file diff --git a/org.niclasundharald.engine/Geometry.cs b/org.niclasundharald.engine/Geometry.cs index 2578ae0..b1cec39 100644 --- a/org.niclasundharald.engine/Geometry.cs +++ b/org.niclasundharald.engine/Geometry.cs @@ -1,4 +1,5 @@ using System; +using org.hwo.contracts; using OpenTK; @@ -8,7 +9,7 @@ namespace org.niclasundharald.engine { /** - * Intersect Triangle and Ray: + * Intersect Plane and Ray: * * A + i * (B-A) + j * (C-A) = P + n * V * A-P + i * (B-A) + j * (C-A) = n * V @@ -18,38 +19,79 @@ namespace org.niclasundharald.engine * * -n * V + i * (B-A) + j * (C-A) = P-A * + * out p : receives the cooefficients of the solved equation (n,i,j) * **/ - public static bool intersectTriangle(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + public static bool intersectPlainCoeff(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ Vector3 BA,CA,PA; + Vector3 coeff = new Vector3(); + p = coeff; BA = B-A; CA = C-A; PA = P-A; - +/* Console.WriteLine("Interesction:"); Console.WriteLine("Ray: {0} + i * {1}",P,V); - Console.WriteLine("Triangle: {0}",A); - Console.WriteLine(" {0}",B); - Console.WriteLine(" {0}",C); + Console.WriteLine("A: {0}",A); + Console.WriteLine("B: {0}",B); + Console.WriteLine("C: {0}",C); +*/ + return new BooleanConditional() + .requires(Linear.Solve4x3(V,BA,CA,PA,out coeff)) + .does(delegate { coeff.X = -coeff.X;}) + .sets(ref p,coeff) + .isSuccess(); + } + public static bool intersectPlain(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + Vector3 coeff = new Vector3(); + p = new Vector3(); - if (!Linear.Solve4x3(V,BA,CA,PA,out p)){ - return false; - } + return new BooleanConditional() + .requires(intersectPlainCoeff(P,V,A,B,C,out coeff)) + .assigns(ref p, delegate { return A + (coeff.Y * (B-A)) + (coeff.Z * (C-A)); }) + .isSuccess(); + } - p.X = -p.X; - if (p.X < 0){ - return false; - } + public static bool intersectPlainForward(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + Vector3 coeff = new Vector3(); + p = new Vector3(); - if ((p.Y < 0) || (p.Y > 1.0f) || (p.Z < 0) || (p.Z > 1.0f)){ - return false; - } + return new BooleanConditional() + .requires(intersectPlainCoeff(P,V,A,B,C,out coeff)) + .requires(coeff.X > 0) + .assigns(ref p, delegate { return P + (coeff.X * V); }) + .isSuccess(); + } - p = P + (p.X * V); + public static bool intersectPlainRect(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + Vector3 coeff = new Vector3(); + p = new Vector3(); - return true; + return new BooleanConditional() + .requires(intersectPlainCoeff(P,V,A,B,C,out coeff)) + .requires(coeff.Y >= 0) + .requires(coeff.Z >= 0) + .requires(coeff.Y <= 1) + .requires(coeff.Z <= 1) + .assigns(ref p, delegate { return P + (coeff.X * V); }) + .isSuccess(); + } + + public static bool intersectTriangle(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ + Vector3 coeff = new Vector3(); + p = new Vector3(); + + return new BooleanConditional() + .requires(intersectPlainCoeff(P,V,A,B,C,out coeff)) + .requires(coeff.Y >= 0) + .requires(coeff.Z >= 0) + .requires(coeff.Y <= 1) + .requires(coeff.Z <= 1) + .requires(coeff.Y + coeff.Z <= 1) + .assigns(ref p, delegate { return A + (coeff.Y * (B-A)) + (coeff.Z * (C-A)); }) + .isSuccess(); } } diff --git a/org.niclasundharald.engine/Linear.cs b/org.niclasundharald.engine/Linear.cs index 30988dd..e044587 100644 --- a/org.niclasundharald.engine/Linear.cs +++ b/org.niclasundharald.engine/Linear.cs @@ -13,11 +13,12 @@ namespace org.niclasundharald.engine rows[2] = new Vector4(X.Z,Y.Z,Z.Z,W.Z); c = new Vector3(); + /* Console.WriteLine("Solver Unsolved:"); Console.WriteLine("X: {0}",rows[0]); Console.WriteLine("Y: {0}",rows[1]); Console.WriteLine("Z: {0}",rows[2]); - +*/ if (rows[0].Z == 0){ Vector4 t = rows[0]; @@ -49,16 +50,18 @@ namespace org.niclasundharald.engine rows[2] -= rows[1] * (rows[2].Y / rows[1].Y); } - c.X = rows[2].W / rows[2].X; + rows[2] /= rows[2].X; + + c.X = rows[2].W; c.Y = (rows[1].W - (rows[1].X * c.X)) / rows[1].Y; - c.Z = (rows[0].W - (rows[0].X * c.X) - (rows[0].Y * c.Y)) / rows[0].Y; - + c.Z = (rows[0].W - (rows[0].X * c.X) - (rows[0].Y * c.Y)) / rows[0].Z; + /* Console.WriteLine("Solver Solved:"); Console.WriteLine("X: {0}",rows[0]); Console.WriteLine("Y: {0}",rows[1]); Console.WriteLine("Z: {0}",rows[2]); Console.WriteLine("P: {0}",c); - + */ return true; } } diff --git a/org.niclasundharald.engine/graphics/GLCamera.cs b/org.niclasundharald.engine/graphics/GLCamera.cs index bb3ff44..60d999e 100644 --- a/org.niclasundharald.engine/graphics/GLCamera.cs +++ b/org.niclasundharald.engine/graphics/GLCamera.cs @@ -10,6 +10,7 @@ namespace org.niclasundharald.engine.graphics { float fov; float width, height; + float aspect; Vector3 vPosition, vView, @@ -20,6 +21,7 @@ namespace org.niclasundharald.engine.graphics fov = MathHelper.PiOver2; width = 100; height = 100; + aspect = 1; vPosition = new Vector3(0,0,1000); vView = new Vector3(0,0,-1); @@ -42,12 +44,14 @@ namespace org.niclasundharald.engine.graphics } private void buildProjection(){ + aspect = width / height; + _mProjection = Matrix4.CreatePerspectiveFieldOfView( fov, - width / height, + aspect, 1.0f, 100000.0f - ); + ); } public Vector3 Position @@ -87,6 +91,46 @@ namespace org.niclasundharald.engine.graphics _mCamera = mTranslation * mRotation; } + public Vector3 pickRay(float x,float y){ + float y_arc,x_arc; + + x_arc = (( (2*x) ) - width) * aspect; + y_arc = height - ( (2*y) ); + + x_arc /= width; + y_arc /= height; + + Vector4 ray = new Vector4(x_arc,y_arc,1,1); + + Console.WriteLine("Pick Ray: {0}",ray); + Matrix4 mpi = _mProjection.Inverted(); + mpi.Transpose(); + ray = mpi * ray; + ray.Normalize(); + Console.WriteLine("Pick Ray (PROJ): {0}",ray); + +// ray.Z = -1; +// ray.W = 0; + + //Console.WriteLine("Pick Ray (CAM): {0}",ray); + Matrix4 mci = _mCamera.Inverted(); + mci.Transpose(); + ray = mci * ray; + + Console.WriteLine("Pick Ray (CAM): {0}",ray); + + //Console.WriteLine("Pick Ray Angles: x:{0} / y:{1}",x_arc,y_arc); + //ray = Matrix4.CreateRotationY(-y_arc) * Matrix4.CreateRotationX( x_arc ) * ray; + + Vector3 pr = ray.Xyz.Normalized(); + if (pr.Z > 0){ + pr *= -1; + } + + Console.WriteLine("Pick Ray (World): {0}",pr); + Console.WriteLine("--------------------------------------------------"); + return pr; + } } } diff --git a/org.niclasundharald.engine/graphics/GlobalDefaults.cs b/org.niclasundharald.engine/graphics/GlobalDefaults.cs index 860405c..8342630 100644 --- a/org.niclasundharald.engine/graphics/GlobalDefaults.cs +++ b/org.niclasundharald.engine/graphics/GlobalDefaults.cs @@ -52,10 +52,11 @@ namespace org.niclasundharald.engine.graphics private string defVertexShaderSource = @"#version 330 -in vec3 iv_position; -in vec4 iv_color; -in vec3 iv_normal; -in vec2 iv_uv; +layout(location = 0) in vec3 iv_position; +layout(location = 1) in vec4 iv_color; +layout(location = 2) in vec3 iv_normal; +layout(location = 3) in vec4 iv_col2; +layout(location = 4) in vec2 iv_uv; uniform mat4 mObject; uniform mat4 mCamera; @@ -87,7 +88,7 @@ void main() float sight = clamp( idist * idist * idist, 0.1, 1.0); shading = clamp(0.3 + (cosTheta * 0.7), 0.1, 0.9) * sight; - color = clamp( vec4( iv_color.xyz, fading ), 0, 1); + color = clamp( vec4( iv_color.xyz, fading ) * iv_col2, 0, 1); coord = camray; } diff --git a/org.niclasundharald.engine/graphics/LoadedModel.cs b/org.niclasundharald.engine/graphics/LoadedModel.cs index 3b3b8dd..9f43618 100644 --- a/org.niclasundharald.engine/graphics/LoadedModel.cs +++ b/org.niclasundharald.engine/graphics/LoadedModel.cs @@ -100,9 +100,9 @@ namespace org.niclasundharald.engine.graphics vertexes.Add( t.a ); vertexes.Add( t.b ); vertexes.Add( t.c ); - normals.Add( t.na ); - normals.Add( t.nb ); - normals.Add( t.nc ); + normals.Add( -t.na ); + normals.Add( -t.nb ); + normals.Add( -t.nc ); } this.vertexes = vertexes.ToArray(); diff --git a/org.niclasundharald.engine/graphics/Model3D.cs b/org.niclasundharald.engine/graphics/Model3D.cs index a1cc622..8e3d738 100644 --- a/org.niclasundharald.engine/graphics/Model3D.cs +++ b/org.niclasundharald.engine/graphics/Model3D.cs @@ -12,12 +12,14 @@ namespace org.niclasundharald.engine.graphics { protected int vao, vbo, // Vertex Buffer cbo, // Color Buffer + cbo2, // Second Color Buffer nbo; // Normals Buffer protected int nTriangles; public Vector3[] vertexes = null; public Vector4[] colors = null; + public Vector4[] colors2 = null; public Vector3[] normals = null; public Triangle[] triangles = new Triangle[0]; @@ -33,6 +35,7 @@ namespace org.niclasundharald.engine.graphics { protected void prepare(){ vbo = GL.GenBuffer(); cbo = GL.GenBuffer(); + cbo2 = GL.GenBuffer(); nbo = GL.GenBuffer(); GL.BindVertexArray(vao); @@ -61,6 +64,14 @@ namespace org.niclasundharald.engine.graphics { 0, 0); + GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); + GL.VertexAttribPointer(3, + 4, + VertexAttribPointerType.Float, + false, + 0, + 0); + GL.BindVertexArray(0); } @@ -68,9 +79,11 @@ namespace org.niclasundharald.engine.graphics { this.vertexes = new Vector3[nTriangles * 3]; this.normals = new Vector3[nTriangles * 3]; this.colors = new Vector4[nTriangles * 3]; + this.colors2 = new Vector4[nTriangles * 3]; this.vertexes.Fill(new Vector3(0,0,0)); this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f)); + this.colors2.Fill(new Vector4(0.0f,2.0f,0.0f,1.0f)); this.nTriangles = nTriangles; rebind(); @@ -89,6 +102,10 @@ namespace org.niclasundharald.engine.graphics { this.colors = new Vector4[nTriangles * 3]; this.colors.Fill(new Vector4(0.5f,0.5f,0.5f,1.0f)); } + if (this.colors2 == null){ + this.colors2 = new Vector4[nTriangles * 3]; + this.colors2.Fill(new Vector4(0.0f,2.0f,0.0f,1.0f)); + } rebind(); } @@ -96,6 +113,7 @@ namespace org.niclasundharald.engine.graphics { bindVertexes(); bindNormals(); bindColors(); + bindColors2(); } protected void bindVertexes(){ @@ -141,6 +159,17 @@ namespace org.niclasundharald.engine.graphics { GL.BindVertexArray(0); } + protected void bindColors2(){ + GL.BindVertexArray(vao); + + GL.BindBuffer(BufferTarget.ArrayBuffer, cbo2); + GL.BufferData(BufferTarget.ArrayBuffer, 16 * colors2.Length, colors2, BufferUsageHint.StaticDraw); + + GL.EnableVertexAttribArray(3); + + GL.BindVertexArray(0); + } + protected void bind(Vector3[] vertexes,Vector4[] colors,Vector3[] normals,Vector2[] uvs){ if (vertexes != null){ this.vertexes = vertexes; @@ -253,6 +282,21 @@ namespace org.niclasundharald.engine.graphics { set { model.colors[(3*n)+2] = value; } } + public Vector4 Color2A { + get { return model.colors2[(3*n)]; } + set { model.colors2[(3*n)] = value; } + } + + public Vector4 Color2B { + get { return model.colors2[(3*n)+1]; } + set { model.colors2[(3*n)+1] = value; } + } + + public Vector4 Color2C { + get { return model.colors2[(3*n)+2]; } + set { model.colors2[(3*n)+2] = value; } + } + public void updateNormals(){ Vector3 normal = Vector3.Cross( model.vertexes[(3*n)+1] - model.vertexes[(3*n)], model.vertexes[(3*n)+2] - model.vertexes[(3*n)] ); normal.Normalize(); diff --git a/org.niclasundharald.engine/graphics/SquaredMap.cs b/org.niclasundharald.engine/graphics/SquaredMap.cs index 81e17f0..69ccee7 100644 --- a/org.niclasundharald.engine/graphics/SquaredMap.cs +++ b/org.niclasundharald.engine/graphics/SquaredMap.cs @@ -2,13 +2,14 @@ using System.IO; using System.Collections.Generic; - using ImageSharp; using ImageSharp.PixelFormats; using OpenTK.Graphics.OpenGL4; using OpenTK; +using org.hwo.contracts; + using org.niclasundharald.engine; namespace org.niclasundharald.engine.graphics @@ -23,6 +24,8 @@ namespace org.niclasundharald.engine.graphics private int width, height; + public Vector3[] corners; + private MapModel3D model; public SquaredMap(int width,int height) @@ -57,6 +60,16 @@ namespace org.niclasundharald.engine.graphics protected void generateBaseMap(){ this.model = new MapModel3D(this,width,height); + this.corners = new Vector3[8]; + this.corners[0] = new Vector3(); + this.corners[1] = new Vector3(width * edge,0,0); + this.corners[2] = new Vector3(width * edge,height * edge,0); + this.corners[3] = new Vector3(0,height * edge,0); + this.corners[4] = new Vector3(0,0,maxHeight); + this.corners[5] = new Vector3(width * edge,0,maxHeight); + this.corners[6] = new Vector3(width * edge,height * edge,maxHeight); + this.corners[7] = new Vector3(0,height * edge,maxHeight); + for (int y=0;y= 0) + .requires(tile.Y >= 0) + .requires(tile.X < width) + .requires(tile.Y < height) + .throws(); + + return tile; + } + + public Vector2 intersectTile(Vector3 P,Vector3 V){ + return toTile( intersect(P,V).Xy ); + } + + public Vector3 intersect(Vector3 P,Vector3 V){ + Vector3 + A = new Vector3(), // Lower plane intersection + B = new Vector3(); // Upper plane intersection + + Console.WriteLine("P: {0} V: {1}",P,V); + + if (!Geometry.intersectPlain(P,V,corners[0],corners[1],corners[3],out A)){ + throw new OutOfWorldException(); + } + + if (!Geometry.intersectPlain(P,V,corners[4],corners[5],corners[7],out B)){ + throw new OutOfWorldException(); + } + + Console.WriteLine("Lower Plane Intersection (LIN): {0}",A); + A = P + ( V * ( -P.Z / V.Z) ); + Console.WriteLine("Lower Plane Intersection: {0}",A); + + Console.WriteLine("Higher Plane Intersection (LIN): {0}",B); + B = P + ( V * ( (SquaredMap.maxHeight-P.Z) / V.Z) ); + Console.WriteLine("Higher Plane Intersection: {0}",B); + + Vector2 a,b,min,max; + + a = toTileBorderless(A.Xy); + b = toTileBorderless(B.Xy); + + min = Vector2.ComponentMax(Vector2.ComponentMin(a,b)-new Vector2(1,1),new Vector2()); + max = Vector2.ComponentMin(Vector2.ComponentMax(a,b)+new Vector2(1,1),new Vector2(width,height)); + + Console.WriteLine("Checking Intersections: {0} -> {1}",min,max); + + for (int nx = (int)min.X; nx < max.X; nx++){ + for (int ny = (int)min.Y; ny < max.Y; ny++){ + Vector3 isect = new Vector3(); + //Console.WriteLine("Check Intersection: {0}/{1}",nx,ny); + + for (int n=0;n<4;n++){ + try { + Model3D.Triangle t = model.triangles[tileIndex(nx,ny)]; + if (Geometry.intersectTriangle( + P, + V, + t.VertexA, + t.VertexB, + t.VertexC, + out isect + )){ + return isect; + } + + } catch (OutOfWorldException owe){ + } + } + } + } + + throw new OutOfWorldException(); + } + + public void highlight(Vector2 tile){ + new BooleanConditional() + .requires(tile.X >= 0) + .requires(tile.Y >= 0) + .requires(tile.X < width) + .requires(tile.Y < height) + .does(delegate { + + for (int n=0;n<4;n++){ + Model3D.Triangle t = model.triangles[tileIndex((int)tile.X,(int)tile.Y)+n]; + t.ColorA *= 2; + t.ColorB *= 2; + t.ColorC *= 2; + } + + model.rebind(); + }); + } + + } @@ -204,56 +321,6 @@ namespace org.niclasundharald.engine.graphics ); } } - - - /* Vector4[] colors = new Vector4[ vertexes.Length ]; - - for (int n=0; n< vertexes.Length; n++){ - - if (vertexes[n].Z >= 256 ){ - Console.WriteLine("Max. Height : {0}",vertexes[n].Z / 256); - } - - colors[n] = new Vector4( - 0.50f + (0.2f * vertexes[n].Z / 256), - 0.25f + (0.50f * vertexes[n].Z / 256), - 0.10f + (0.80f * vertexes[n].Z / 256), - 1.0f - ); - } - - Vector3[] normals = Model3D.computeNormals(vertexes); - - Dictionary> dNormals = new Dictionary>(); - - foreach (Vector3 v in vertexes){ - if (!dNormals.ContainsKey(v)){ - dNormals.Add(v,new List()); - } - } - - for (int n=0;n + + + {56733EC1-7D97-48D0-AA4C-98EA624A5A21} + org.hwo.contracts + + \ No newline at end of file