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] 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