Exemple #1
0
        void Update()
        {
            var baseModel       = CreateMatrixFromTranslation(0.0f, 0.0f, 5.0f) * CreateMatrixFromRotation(rotation, 0.0f, 1.0f, 0.0f);
            var baseMv          = viewMatrix * baseModel;
            var modelViewMatrix = baseMv * CreateMatrixFromRotation(rotation, 1.0f, 1.0f, 1.0f);

#if NET
            Matrix4.Invert(Matrix4.Transpose(modelViewMatrix), out uniformBuffer.NormalMatrix);
#else
            uniformBuffer.NormalMatrix = Matrix4.Invert(Matrix4.Transpose(modelViewMatrix));
#endif
            uniformBuffer.ModelviewProjectionMatrix = Matrix4.Transpose(projectionMatrix * modelViewMatrix);

            // Copy uniformBuffer's content into dynamicConstantBuffer.Contents
            int    rawsize = Marshal.SizeOf(typeof(Uniforms));
            var    rawdata = new byte [rawsize];
            IntPtr ptr     = Marshal.AllocHGlobal(rawsize);
            Marshal.StructureToPtr(uniformBuffer, ptr, false);
            Marshal.Copy(ptr, rawdata, 0, rawsize);
            Marshal.FreeHGlobal(ptr);

            Marshal.Copy(rawdata, 0, dynamicConstantBuffer.Contents + rawsize * constantDataBufferIndex, rawsize);

            rotation += 0.01f;
        }
Exemple #2
0
        internal void SetWorldMatrix(Matrix4x4 worldMatrix)
        {
            var worldTransposed      = Matrix4x4.Transpose(worldMatrix);
            var viewTransposed       = Matrix4x4.Transpose(viewMatrix);
            var projectionTransposed = Matrix4x4.Transpose(projectionMatrix);

            MatricesBuffer matricesBuffer = new MatricesBuffer(ref worldTransposed, ref viewTransposed, ref projectionTransposed);

            deviceContext.UpdateSubresource <MatricesBuffer>(ref matricesBuffer, this.worldViewProjectionMatrixBuffer);
            deviceContext.VertexShader.SetConstantBuffer(0, this.worldViewProjectionMatrixBuffer);
        }
Exemple #3
0
        static NumMatrix4x4 ConvertMatrix(AssMatrix4x4 Mat)
        {
            return(NumMatrix4x4.Transpose(*(NumMatrix4x4 *)&Mat));

            /*Mat.Decompose(out Vector3D Scaling, out AssQuaternion Rotation, out Vector3D Translation);
             *
             * NumMatrix4x4 Rot = NumMatrix4x4.CreateFromQuaternion(ConvertQuat(Rotation));
             * NumMatrix4x4 Pos = NumMatrix4x4.CreateTranslation(ConvertVec(Translation));
             * NumMatrix4x4 Scl = NumMatrix4x4.CreateScale(ConvertVec(Scaling));
             * return Scl * Rot * Pos;
             * //*/
        }
Exemple #4
0
        public Bounds GetBoundingBox(Mesh mesh)
        {
            var entry = _meshes.FirstOrDefault(m => m.Mesh == mesh);

            if (entry == null)
            {
                return(new Bounds());
            }

            var matrix = Matrix4x4.Transpose(entry.GLMesh.ModelMatrix);

            return(mesh.BoundingBox.ToBounds() * matrix);
        }
        /// <summary>
        /// Transforms a normalized plane by a matrix.
        /// </summary>
        /// <param name="plane">The normalized plane to transform.</param>
        /// <param name="matrix">The transformation matrix.</param>
        /// <param name="result">The transformed plane.</param>
        public static void Transform(ref Plane plane, ref Matrix4x4 matrix, out Plane result)
        {
            // See "Transforming Normals" in http://www.glprogramming.com/red/appendixf.html
            // for an explanation of how this works.

            Matrix4x4 transformedMatrix;

            Matrix4x4.Invert(matrix, out transformedMatrix);
            transformedMatrix = Matrix4x4.Transpose(transformedMatrix);

            var vector = new Vector4(plane.Normal, plane.D);

            Vector4 transformedVector = Vector4.Transform(vector, transformedMatrix);

            result = new Plane(transformedVector);
        }
Exemple #6
0
        public void LoadMesh(Mesh mesh, Matrix4x4 transform)
        {
            UnloadMesh(mesh);

            var textureCoords = mesh.TextureCoordinateChannelCount > 0
                ? mesh.TextureCoordinateChannels[0].Select(uv => uv.AsUvPoint())
                : null;

            var triangleIndices = new List<int>(mesh.FaceCount * 4);
            foreach (var face in mesh.Faces)
            {
                triangleIndices.Add(face.Indices[0]);
                triangleIndices.Add(face.Indices[1]);
                triangleIndices.Add(face.Indices[2]);
                if (face.IndexCount == 4)
                {
                    triangleIndices.Add(face.Indices[0]);
                    triangleIndices.Add(face.Indices[2]);
                    triangleIndices.Add(face.Indices[3]);
                }

                if (face.IndexCount > 4)
                {
                    Debug.WriteLine($"Found {face.IndexCount}gon, only generating quad");
                }
            }

            var geometry = new MeshGeometry3D
            {
                Positions = new Point3DCollection(
                    mesh.Vertices.Select(v => new Point3D(v.X, v.Y, v.Z))),
                Normals = new Vector3DCollection(
                    mesh.Normals.Select(n => new Vector3D(n.X, n.Y, n.Z))),
                TriangleIndices = new Int32Collection(triangleIndices),
                TextureCoordinates = textureCoords != null ? new PointCollection(textureCoords) : null
            };
            var diffuse = _textureProvider.GetTexture(mesh, TextureType.Diffuse);

            // the ViewPortUnits is very important, or the brush will map MaxU x MaxV to 1 x 1
            // see https://books.google.no/books?id=ubgRAAAAQBAJ&pg=PA582&lpg=PA582
            // TileMode also seems necessary
            var brush = diffuse != null
                ? new ImageBrush(diffuse)
                {
                    ViewportUnits = BrushMappingMode.Absolute,
                    TileMode = TileMode.Tile
                }
                : (Brush) Brushes.Pink;

            // because reasons?
            transform = Matrix4x4.Transpose(transform);
            
            var geometryModel = new GeometryModel3D
            {
                Material = new MaterialGroup
                {
                    Children = new MaterialCollection
                    {
                        new DiffuseMaterial(brush),
                    }
                },
                Geometry = geometry,
            };


            var group = new Model3DGroup()
            {
                Transform = transform.ToTransform3D(),
            };
            group.Children.Add(geometryModel);
            _meshModelGroup.Children.Add(group);

            var (wireFrame, wireFrameGeometry) = CreateWireFrame(mesh);
            wireFrame.Transform = transform.ToTransform3D();
            
            _wireFrameModelGroup.Children.Add(wireFrame);
            _meshes[mesh] = new MeshEntry(group, wireFrame, geometry, wireFrameGeometry);
            
        }