Beispiel #1
0
        private static void TransformVertex(List <Bone> bones, List <GLNode> nodes, Vector3 position, Vector3 normal, ref Vector3 newPosition, ref Vector3 newNormal, float weight, byte boneIndex)
        {
            var bone       = bones[boneIndex];
            var boneNode   = nodes[bone.NodeIndex];
            var bindMatrix = boneNode.WorldTransform;

            newPosition += Vector3.Transform(Vector3.Transform(position, bone.InverseBindMatrix),
                                             bindMatrix * weight);

            newNormal += Vector3.TransformNormal(Vector3.TransformNormal(normal, bone.InverseBindMatrix),
                                                 bindMatrix * weight);
        }
Beispiel #2
0
        public static void DrawRoadmap(this IDebugCanvas debugCanvas, MotionRoadmap roadmap, MovementComponent movementComponent = null)
        {
            var skip = movementComponent?.PathingRoadmapProgressIndex ?? 0;

            foreach (var(i, action) in roadmap.Plan.Skip(skip).Enumerate())
            {
                switch (action)
                {
                case MotionRoadmapWalkAction walk:
                    debugCanvas.Transform = Matrix4x4.Identity;
                    var s = i == 0 && movementComponent != null
                     ? movementComponent.WorldPosition
                     : Vector3.Transform(new Vector3(walk.Source.X, walk.Source.Y, 0), walk.Node.SectorNodeDescription.WorldTransform).ToOpenMobaVector();
                    var t = Vector3.Transform(new Vector3(walk.Destination.X, walk.Destination.Y, 0), walk.Node.SectorNodeDescription.WorldTransform).ToOpenMobaVector();
                    //                     Console.WriteLine("S: " + s + "\t AND T: " + t);
                    //                     for (var i = 0; i < 100; i++) {
                    //                        debugCanvas.DrawPoint((s * (100 - i) + t * i) / 100, new StrokeStyle(Color.Cyan, 50));
                    //                     }
                    debugCanvas.DrawLine(s, t, PathStroke);
                    break;
                }
            }
        }
Beispiel #3
0
 public static Vector3 Rotate(Vector3 v, Quaternion q)
 {
     return(Vector3.Transform(v, q));
 }
Beispiel #4
0
 private Vector3 CalculateCameraOrbitPosition()
 {
     return(Vector3.Transform(new Vector3(0.0f, 0.0f, mOrbitDistance),
                              Matrix4x4.CreateRotationX(MathHelper.DegreesToRadians(mOrbitRotation.Y)) *
                              Matrix4x4.CreateRotationY(MathHelper.DegreesToRadians(mOrbitRotation.X))));
 }
Beispiel #5
0
        void RenderDeferredPathway()
        {
            var screenDim = vec2(Width, Height) / 2;
            var projView  = FpsCamera.Matrix * ProjectionMat;

            Matrix4x4.Invert(projView, out var invProjView);
            Vector2 screenPos(Vector3 wpos)
            {
                var ipos = Vector4.Transform(vec4(wpos, 1), projView);

                return((ipos.XY() / ipos.W).Add(1) * screenDim);
            }

            Profile("- G-buffer render", () => {
                GL.Viewport(0, 0, Width, Height);
                FBO.Bind();
                GL.ClearColor(0, 0, 0, 1);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

                GL.Enable(EnableCap.CullFace);
                GL.Enable(EnableCap.DepthTest);
                GL.Disable(EnableCap.Blend);

                Mesh.SetProjectionView(projView);

                Models.ForEach(model => model.Draw(translucent: false));

                FrameBuffer.Unbind();
                GL.Flush();
            });

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            const int tileSize = 256;
            var       tw       = (int)Math.Ceiling((float)Width / tileSize);
            var       th       = (int)Math.Ceiling((float)Height / tileSize);
            IReadOnlyList <IEnumerable <(double Dist, PointLight Light)> > tiles = null;

            Profile("- Tile determination", () => {
                var cforward  = Vector3.Transform(FpsCamera.Forward, Camera.LookRotation).Normalized();
                var cp        = cforward.Y != 0 || cforward.Z != 0 ? vec3(1, 0, 0) : vec3(0, 1, 0);
                var perp      = cforward.Cross(cp).Normalized();
                var tileLists = Enumerable.Range(0, tw * th).Select(i => new List <(double Dist, PointLight Light)>()).ToArray();
                foreach (var light in Lights)
                {
                    var toLight = Camera.Position - light.Position;
                    var tll     = toLight.Length();
                    if (tll > light.Radius)
                    {
                        var lspos   = screenPos(light.Position);
                        var ppos    = Camera.Position + cforward * tll;
                        var pradius = (screenDim - screenPos(ppos + perp * light.Radius)).Length();
                        if (lspos.X + pradius < 0 || lspos.Y + pradius < 0 || lspos.X - pradius > Width || lspos.Y - pradius > Height)
                        {
                            continue;
                        }
                        pradius *= pradius;
                        for (var x = 0; x < tw; ++x)
                        {
                            for (var y = 0; y < th; ++y)
                            {
                                var tilePos = (x * tileSize, y * tileSize);
                                var delta   = (lspos.X - max(tilePos.Item1, min(lspos.X, tilePos.Item1 + tileSize)), lspos.Y - max(tilePos.Item2, min(lspos.Y, tilePos.Item2 + tileSize)));
                                if (delta.Item1 * delta.Item1 + delta.Item2 * delta.Item2 < pradius)
                                {
                                    tileLists[x * th + y].Add((tll, light));
                                }
                            }
                        }
                    }
                    else
                    {
                        tileLists.ForEach(tile => tile.Add((tll, light)));
                    }
                }

                tiles = tileLists.Select(tile => tile.Count <= maxLights ? tile : tile.OrderBy(x => x.Item1).Take(maxLights)).ToList();
            });
Beispiel #6
0
        public GLMesh(Mesh mesh, Matrix4x4 modelMatrix, List <Bone> bones, List <GLNode> nodes, Dictionary <string, GLMaterial> materials)
        {
            Mesh = mesh;

            var vertices = mesh.Vertices;
            var normals  = mesh.Normals;

            if (mesh.VertexWeights != null)
            {
                vertices = new Vector3[mesh.VertexCount];
                normals  = null;

                if (mesh.Normals != null)
                {
                    normals = new Vector3[mesh.VertexCount];
                }

                Matrix4x4.Invert(modelMatrix, out var modelMatrixInv);

                for (int i = 0; i < mesh.VertexCount; i++)
                {
                    var position = mesh.Vertices[i];
                    var normal   = mesh.Normals?[i] ?? Vector3.Zero;

                    var newPosition = Vector3.Zero;
                    var newNormal   = Vector3.Zero;

                    for (int j = 0; j < 4; j++)
                    {
                        var weight = mesh.VertexWeights[i].Weights[j];
                        if (weight == 0)
                        {
                            continue;
                        }

                        var boneIndex = mesh.VertexWeights[i].Indices[j];
                        TransformVertex(bones, nodes, position, normal, ref newPosition, ref newNormal, weight, boneIndex);
                    }

                    vertices[i] = Vector3.Transform(newPosition, modelMatrixInv);

                    if (normals != null)
                    {
                        normals[i] =
                            Vector3.Normalize(Vector3.TransformNormal(newNormal, modelMatrixInv));
                    }
                }
            }

            var indices = new uint[mesh.Triangles.Length * 3];

            for (int i = 0; i < mesh.Triangles.Length; i++)
            {
                indices[(i * 3) + 0] = mesh.Triangles[i].A;
                indices[(i * 3) + 1] = mesh.Triangles[i].B;
                indices[(i * 3) + 2] = mesh.Triangles[i].C;
            }

            VertexArray = new GLVertexArray(vertices, normals, mesh.TexCoordsChannel0, indices, PrimitiveType.Triangles);

            // material
            if (mesh.MaterialName != null && materials != null)
            {
                if (materials.TryGetValue(mesh.MaterialName, out var material))
                {
                    Material = material;
                }
                else
                {
                    Trace.TraceError($"Mesh referenced material \"{mesh.MaterialName}\" which does not exist in the model");
                    Material = new GLMaterial();
                }
            }
            else
            {
                Material = new GLMaterial();
            }

            IsVisible = true;
        }