static void BuildTransform(Vector3 position, Vector2 size, SpriteFlags flags, out Matrix4x4 transform)
        {
            var offset = (flags & SpriteFlags.AlignmentMask) switch
            {
                0 => Vector3.Zero,
                SpriteFlags.MidAligned => new Vector3(0, -0.5f, 0),
                SpriteFlags.BottomAligned => new Vector3(0, -1.0f, 0),
                SpriteFlags.LeftAligned => new Vector3(0.5f, 0, 0),
                SpriteFlags.LeftAligned | SpriteFlags.MidAligned => new Vector3(0.5f, -0.5f, 0),
                SpriteFlags.LeftAligned | SpriteFlags.BottomAligned => new Vector3(0.5f, -1.0f, 0),
                _ => Vector3.Zero
            };

            transform = Matrix4x4.CreateTranslation(offset);

            if (flags.HasFlag(SpriteFlags.Floor))
            {
                transform *= new Matrix4x4(
                    1, 0, 0, 0,
                    0, 0, -1, 0,
                    0, 1, 0, 0,
                    0, 0, 0, 1);
            }

            transform *= Matrix4x4.CreateScale(new Vector3(size.X, size.Y, size.X));
            transform *= Matrix4x4.CreateTranslation(position);
        }
Exemple #2
0
        protected override float[] F(float[] xs)
        {
            var R2 = Radius * Radius;
            var v  = new Vector2(xs[0], xs[1]);

            v = Vector2.Transform(v, Matrix4x4.CreateScale(-1) * M);
            var x    = v.X;
            var y    = v.Y;
            var x2y2 = x * x + y * y;

            var a1 = new[]
            {
                W / x2y2 * y + (1 + R2 / x2y2 - 2 * R2 * x * x / (x2y2 * x2y2)),
                W / x2y2 * -x - (2 * R2 * x * y / (x2y2 * x2y2))
            };

            //v = new Vector2(xs[0], xs[1]);
            //v = Vector2.Transform(v, Matrix4x4.CreateTranslation(-6,0,0));
            //x = v.X;
            //y = v.Y;
            //x2y2 = x * x + y * y;

            //var a2 = new[]
            //{
            //    W/x2y2 * y  + (1 + R2 / x2y2 - 2 * R2 * x * x / (x2y2 * x2y2)),
            //    W/x2y2 * -x - (2 * R2 * x * y / (x2y2 * x2y2))
            //};

            return(new[]
            {
                a1[0], // + a2[0],
                a1[1], // + a2[1]
            });
        }
Exemple #3
0
        static NumMatrix4x4 GetTransformForFrame(NodeAnimationChannel Ch, int Frame)
        {
            VectorKey PosKey = Ch.PositionKeys[Ch.PositionKeys.Count - 1];

            if (Frame < Ch.PositionKeys.Count)
            {
                PosKey = Ch.PositionKeys[Frame];
            }

            QuaternionKey RotKey = Ch.RotationKeys[Ch.RotationKeys.Count - 1];

            if (Frame < Ch.RotationKeys.Count)
            {
                RotKey = Ch.RotationKeys[Frame];
            }

            VectorKey SclKey = Ch.ScalingKeys[Ch.ScalingKeys.Count - 1];

            if (Frame < Ch.ScalingKeys.Count)
            {
                SclKey = Ch.ScalingKeys[Frame];
            }


            NumMatrix4x4 Rot = NumMatrix4x4.CreateFromQuaternion(ConvertQuat(RotKey.Value));
            NumMatrix4x4 Pos = NumMatrix4x4.CreateTranslation(ConvertVec(PosKey.Value));
            NumMatrix4x4 Scl = NumMatrix4x4.CreateScale(ConvertVec(SclKey.Value));

            //return Pos * Rot * Scl;
            return(Scl * Rot * Pos);
        }
Exemple #4
0
        protected override void Draw(float deltaSeconds)
        {
            UpdateAnimation(deltaSeconds);
            UpdateUniforms();
            _cl.Begin();
            _cl.SetFramebuffer(GraphicsDevice.SwapchainFramebuffer);
            _cl.ClearColorTarget(0, RgbaFloat.Black);
            _cl.ClearDepthStencil(1f);

            Matrix4x4 worldMatrix =
                Matrix4x4.CreateTranslation(0, 15000, -5000)
                * Matrix4x4.CreateRotationX(3 * (float)Math.PI / 2)
                * Matrix4x4.CreateScale(0.05f);

            _cl.UpdateBuffer(_worldBuffer, 0, ref worldMatrix);

            DrawMesh();

            worldMatrix =
                Matrix4x4.CreateTranslation(0, 15000, -5000)
                * Matrix4x4.CreateRotationX(3 * (float)Math.PI / 2)
                * Matrix4x4.CreateScale(0.07f);

            _cl.UpdateBuffer(_worldBuffer, 0, ref worldMatrix);
            DrawMesh();

            _cl.End();

            GraphicsDevice.SubmitCommands(_cl);
            GraphicsDevice.SwapBuffers();
        }
Exemple #5
0
        private void Render(object sender, GlControlEventArgs e)
        {
            if (_shader == null)
            {
                return;
            }

            var v = _cameraHelper.CameraMatrix;

            _shader.M = v;

            _shader.ShadingLevel = 0;
            FiguresHelper.Draw3DCross(4, 1);
            _shader.ShadingLevel = 1;

            _shader.LightPos = new Vector3(1f, 1f, 1f);

            var r1     = Matrix4x4.CreateRotationY(Mathf.ToRadian(_phi));
            var t      = Matrix4x4.CreateTranslation(new Vector3(_radius, 2f, 0f));
            var r2     = Matrix4x4.CreateRotationX(Mathf.ToRadian(90)) * Matrix4x4.CreateRotationY(Mathf.ToRadian(90)) * Matrix4x4.CreateRotationZ(Mathf.ToRadian(45));
            var scaleM = Matrix4x4.CreateScale(0.2f);
            var r3     = Matrix4x4.CreateRotationY(Mathf.ToRadian(-_phi * 10));
            var r4     = Matrix4x4.CreateRotationZ(Mathf.ToRadian(25));
            var m      = r3 * r2 * t * r1 * scaleM * r4;

            _shader.M = m * v;

            Figure3DHelper.DrawMesh(_boomerang, Colors.Chocolate);

            _phi += _step;
        }
Exemple #6
0
        // TODO: Tehdäänkö tällä enää mitään?
        /// <summary>
        /// Muuntaa matriisin Jypelin ruutukoordinaateista XNA:n ruutukoordinaatteihin.
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="screenSize"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        internal static Matrix ToXnaCoords(ref Matrix matrix, Vector screenSize, Vector scale)
        {
            // Keskitetään sprite ruudulla, mutta toteutetaan alkuperäinen muunnos Jypelin koordinaateissa.
            var centralize = Matrix.CreateTranslation((screenSize - scale) / 2);
            var toXna      = Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(screenSize / 2);

            Matrix.Invert(toXna, out Matrix toJypeli);

            return(centralize * toJypeli * matrix * toXna);
        }
Exemple #7
0
 /// <summary>
 /// Palauttaa käänteisen transformaatiomatriisin jolla voi ottaa huomioon ruudun kokoon,
 /// kiertoon ja paikkaan tehdyt muutokset.
 /// Ennen transformaatiota: paikkavektori RenderTargetin koordinaateissa
 /// Transformaation jälkeen: paikkavektori ikkunan koordinaateissa
 /// </summary>
 /// <returns></returns>
 internal Matrix GetScreenInverse()
 {
     return(Matrix.CreateScale(scaleInv)
            * Matrix.CreateRotationZ(-angle)
            * Matrix.CreateTranslation(center.X, center.Y, 0));
 }
Exemple #8
0
 /// <summary>
 /// Palauttaa transformaatiomatriisin jolla voi ottaa huomioon ruudun kokoon,
 /// kiertoon ja paikkaan tehdyt muutokset.
 /// Ennen transformaatiota: paikkavektori ikkunan koordinaateissa
 /// Transformaation jälkeen: paikkavektori RenderTargetin koordinaateissa
 /// </summary>
 /// <returns></returns>
 internal Matrix GetScreenTransform()
 {
     return(Matrix.CreateScale(scale)
            * Matrix.CreateRotationZ(angle)
            * Matrix.CreateTranslation(-center.X, -center.Y, 0));
 }
        protected override void CreateResources(ResourceFactory factory)
        {
            _projectionBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            _viewBuffer       = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            _worldBuffer      = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            Matrix4x4 worldMatrix =
                Matrix4x4.CreateTranslation(0, 15000, -5000)
                * Matrix4x4.CreateRotationX(3 * (float)Math.PI / 2)
                * Matrix4x4.CreateScale(0.05f);

            GraphicsDevice.UpdateBuffer(_worldBuffer, 0, ref worldMatrix);

            ResourceLayout layout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                     new ResourceLayoutElementDescription("Projection", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                     new ResourceLayoutElementDescription("View", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                     new ResourceLayoutElementDescription("World", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                     new ResourceLayoutElementDescription("Bones", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                                                                     new ResourceLayoutElementDescription("SurfaceTex", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                     new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            Texture texture;

            using (Stream ktxStream = OpenEmbeddedAssetStream("goblin_bc3_unorm.ktx"))
            {
                texture = KtxFile.LoadTexture(
                    GraphicsDevice,
                    factory,
                    ktxStream,
                    PixelFormat.BC3_UNorm);
            }
            _texView = ResourceFactory.CreateTextureView(texture);

            VertexLayoutDescription vertexLayouts = new VertexLayoutDescription(
                new[]
            {
                new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3),
                new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("BoneWeights", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4),
                new VertexElementDescription("BoneIndices", VertexElementSemantic.TextureCoordinate, VertexElementFormat.UInt4),
            });

            GraphicsPipelineDescription gpd = new GraphicsPipelineDescription(
                BlendStateDescription.SingleOverrideBlend,
                DepthStencilStateDescription.DepthOnlyLessEqual,
                new RasterizerStateDescription(FaceCullMode.Back, PolygonFillMode.Solid, FrontFace.CounterClockwise, true, false),
                PrimitiveTopology.TriangleList,
                new ShaderSetDescription(
                    new[] { vertexLayouts },
                    factory.CreateFromSpirv(
                        new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(VertexCode), "main"),
                        new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(FragmentCode), "main"))),
                layout,
                GraphicsDevice.SwapchainFramebuffer.OutputDescription);

            _pipeline = factory.CreateGraphicsPipeline(ref gpd);

            AssimpContext ac = new AssimpContext();

            using (Stream modelStream = OpenEmbeddedAssetStream("goblin.dae"))
            {
                _scene = ac.ImportFileFromStream(modelStream, "dae");
            }
            _rootNodeInverseTransform = _scene.RootNode.Transform;
            _rootNodeInverseTransform.Inverse();

            _firstMesh = _scene.Meshes[0];
            AnimatedVertex[] vertices = new AnimatedVertex[_firstMesh.VertexCount];
            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].Position = new Vector3(_firstMesh.Vertices[i].X, _firstMesh.Vertices[i].Y, _firstMesh.Vertices[i].Z);
                vertices[i].UV       = new Vector2(_firstMesh.TextureCoordinateChannels[0][i].X, _firstMesh.TextureCoordinateChannels[0][i].Y);
            }

            _animation = _scene.Animations[0];

            List <int> indices = new List <int>();

            foreach (Face face in _firstMesh.Faces)
            {
                if (face.IndexCount == 3)
                {
                    indices.Add(face.Indices[0]);
                    indices.Add(face.Indices[1]);
                    indices.Add(face.Indices[2]);
                }
            }

            for (uint boneID = 0; boneID < _firstMesh.BoneCount; boneID++)
            {
                Bone bone = _firstMesh.Bones[(int)boneID];
                _boneIDsByName.Add(bone.Name, boneID);
                foreach (VertexWeight weight in bone.VertexWeights)
                {
                    vertices[weight.VertexID].AddBone(boneID, weight.Weight);
                }
            }
            Array.Resize(ref _boneTransformations, _firstMesh.BoneCount);

            _bonesBuffer = ResourceFactory.CreateBuffer(new BufferDescription(
                                                            64 * 64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));

            _rs = factory.CreateResourceSet(new ResourceSetDescription(layout,
                                                                       _projectionBuffer, _viewBuffer, _worldBuffer, _bonesBuffer, _texView, GraphicsDevice.Aniso4xSampler));

            _indexCount = (uint)indices.Count;

            _vertexBuffer = ResourceFactory.CreateBuffer(new BufferDescription(
                                                             (uint)(vertices.Length * Unsafe.SizeOf <AnimatedVertex>()), BufferUsage.VertexBuffer));
            GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, vertices);

            _indexBuffer = ResourceFactory.CreateBuffer(new BufferDescription(
                                                            _indexCount * 4, BufferUsage.IndexBuffer));
            GraphicsDevice.UpdateBuffer(_indexBuffer, 0, indices.ToArray());

            _cl = factory.CreateCommandList();
            _camera.Position    = new Vector3(110, -87, -532);
            _camera.Yaw         = 0.45f;
            _camera.Pitch       = -0.55f;
            _camera.MoveSpeed   = 1000f;
            _camera.FarDistance = 100000;
        }