Exemplo n.º 1
0
 public Quaternion SetFromMatrix(Matrix4f mat)
 {
     SetFromAxes(mat.values[Matrix4f.m00], mat.values[Matrix4f.m10], mat.values[Matrix4f.m20],
                 mat.values[Matrix4f.m01], mat.values[Matrix4f.m11], mat.values[Matrix4f.m21],
                 mat.values[Matrix4f.m02], mat.values[Matrix4f.m12], mat.values[Matrix4f.m22]);
     return(this);
 }
Exemplo n.º 2
0
        private static void GatherMeshes(Node node, List <Mesh> meshes, List <Material> materials, List <Matrix4f> worldTransforms, List <Shader> shaders)
        {
            foreach (GameObject child in node.Children)
            {
                if (child is Node)
                {
                    GatherMeshes((Node)child, meshes, materials, worldTransforms, shaders);
                }
                else if (child is Geometry)
                {
                    meshes.Add(((Geometry)child).Mesh);
                    materials.Add(((Geometry)child).Material);
                    Transform ttransform = new Transform();
                    ttransform.SetTranslation(child.GetUpdatedWorldTranslation());
                    ttransform.SetRotation(child.GetUpdatedWorldRotation());
                    ttransform.SetScale(child.GetUpdatedWorldScale());
                    Matrix4f matrix = ttransform.GetMatrix();
                    worldTransforms.Add(matrix);

                    if (((Geometry)child).GetShader() != null)
                    {
                        shaders.Add(((Geometry)child).GetShader());
                    }
                }
            }
        }
 /// <summary>
 /// Creates a new cylinder cast based wheel shape.
 /// </summary>
 /// <param name="radius">Radius of the wheel.</param>
 /// <param name="width">Width of the wheel.</param>
 /// <param name="localWheelOrientation">Unsteered orientation of the wheel in the vehicle's local space.</param>
 /// <param name="localGraphicTransform">Local graphic transform of the wheel shape.
 /// This transform is applied first when creating the shape's worldTransform.</param>
 /// <param name="includeSteeringTransformInCast">Whether or not to include the steering transform in the wheel shape cast. If false, the casted wheel shape will always point straight forward.
 /// If true, it will rotate with steering. Sometimes, setting this to false is helpful when the cast shape would otherwise become exposed when steering.</param>
 public CylinderCastWheelShape(float radius, float width, Quaternion localWheelOrientation, Matrix4f localGraphicTransform, bool includeSteeringTransformInCast)
 {
     shape = new CylinderShape(width, radius);
     this.LocalWheelOrientation          = localWheelOrientation;
     LocalGraphicTransform               = localGraphicTransform;
     this.IncludeSteeringTransformInCast = includeSteeringTransformInCast;
 }
Exemplo n.º 4
0
        Component ReadNodeComponentTriangles(Chunk root)
        {
            ModelBuilder builder = new ModelBuilder();
            AssetLoader  loader  = root.Loader;
            var          reader  = root.Reader;
            Chunk        chunk;

            Vector3f[] points = null;
            Vector2f[] texels = null;
            int        count;
            Matrix4f   transform = Matrix4f.Identity;

            while (root.ReadSubchunk(out chunk))
            {
                switch (chunk.Id)
                {
                case ChunkId.ComponentTrianglesPointArray:
                    count = (chunk.DataLength - 2) / 12;
                    loader.Expect((ushort)count);
                    points = reader.ReadArrayVector3f(count);
                    break;

                case ChunkId.ComponentTrianglesTexelArray:
                    count = (chunk.DataLength - 2) / 8;
                    loader.Expect((ushort)count);
                    texels = reader.ReadArrayVector2f(count);
                    break;

                case ChunkId.ComponentTrianglesTransform:
                    for (int row = 0; row < 4; row++)
                    {
                        for (int column = 0; column < 3; column++)
                        {
                            transform[row, column] = reader.ReadSingle();
                        }
                    }
                    break;

                case ChunkId.ComponentTrianglesFaceArray:
                    count = reader.ReadUInt16();
                    ushort[] faces        = reader.ReadArrayUInt16(count * 4);
                    string   unknown1Name = chunk.ReadContentString();                           // "0A-"
                    loader.Expect((ushort)0);
                    string materialName = chunk.ReadContentString();

                    count = reader.ReadUInt16();
                    ushort[] unknownIndices = reader.ReadArrayUInt16(count);                     // Seems to just be indices?
                    string   unknown2Name   = chunk.ReadContentString();                         // "PA6"
                    loader.Expect((ushort)0);
                    uint[] smoothingGroups = reader.ReadArrayUInt32(count);

                    break;

                default: root.ReportUnknownSubchunkError(chunk); break;
                }
                chunk.RequireAtEnd();
            }

            return(new RenderModelComponent(builder.Finish()));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Constructs a quaternion from a rotation matrix.
        /// </summary>
        /// <param name="rotation">Rotation matrix to create the quaternion from.</param>
        /// <param name="quaternion">Quaternion based on the rotation matrix.</param>
        public static void FromRotationMatrix(ref Matrix4f rotation, out Quaternion quaternion)
        {
            Matrix3f downsizedMatrix;

            Matrix3f.FromMatrix4f(ref rotation, out downsizedMatrix);
            FromRotationMatrix(ref downsizedMatrix, out quaternion);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Makes this <see cref="GameElement"/> look at the given <paramref name="targetPosition"/>.
        /// </summary>
        /// <param name="targetPosition">The target translation to look at.</param>
        public void LookAt(Point3f targetPosition)
        {
            Matrix4f lookAtMatrix   = Matrix4f.LookAt(Position, targetPosition, UpVector);
            Matrix4f transformation = Matrix4f.CreateTranslation(Translation);
            Matrix4f ret            = (lookAtMatrix * transformation);

            Vector3f direction = Point3f.CreateVector(targetPosition, Position);

            Vector3f rotation = ret.GetEulerAngles();

            if (direction.Z <= 0)
            {
                rotation.Y *= -1;
            }
            if (direction.Z == 0 && direction.Y == 0 && direction.X != 0)
            {
                rotation.Y = MathHelper.PiOver2 * (direction.X < 0 ? 1 : (direction.X > 0 ? -1 : 0));
            }
            if (direction.Z == 0 && direction.X == 0 && direction.Y != 0)
            {
                rotation.X = MathHelper.PiOver2 * (direction.Y < 0 ? 1 : (direction.Y > 0 ? -1 : 0));
            }

            Rotation = rotation;
            //LookAt(Point3f.CreateVector(targetPosition, Position));
        }
Exemplo n.º 7
0
        /// <summary>Transform a Vector by the given Matrix</summary>
        /// <param name="vec">The vector to transform</param>
        /// <param name="mat">The desired transformation</param>
        /// <param name="result">The transformed vector</param>
        public static void Transform(ref Vector3d vec, ref Matrix4f mat, out Vector3d result)
        {
            Vector4d v4 = new Vector4d(vec.X, vec.Y, vec.Z, 1.0f);

            Vector4d.Transform(ref v4, ref mat, out v4);
            result = v4.XYZ;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates an orientation Quaternion from a target Matrix4 rotational matrix.
        /// </summary>
        public static Quaternion FromRotationMatrix(Matrix4f rotation)
        {
            Quaternion toReturn;

            FromRotationMatrix(ref rotation, out toReturn);
            return(toReturn);
        }
Exemplo n.º 9
0
        public static List <Mesh> GatherMeshes(GameObject gameObject, List <Matrix4f> worldTransforms, List <Shader> shaders)
        {
            List <Mesh> meshes = new List <Mesh>();

            if (worldTransforms == null)
            {
                worldTransforms = new List <Matrix4f>();
            }

            if (gameObject is Node)
            {
                GatherMeshes((Node)gameObject, meshes, worldTransforms);
            }
            else if (gameObject is Geometry)
            {
                meshes.Add(((Geometry)gameObject).Mesh);
                Transform ttransform = new Transform();
                ttransform.SetTranslation(gameObject.GetUpdatedWorldTranslation());
                ttransform.SetRotation(gameObject.GetUpdatedWorldRotation());
                ttransform.SetScale(gameObject.GetUpdatedWorldScale());
                Matrix4f matrix = ttransform.GetMatrix();
                worldTransforms.Add(matrix);
                if (((Geometry)gameObject).GetShader() != null)
                {
                    shaders.Add(((Geometry)gameObject).GetShader());
                }
            }

            return(meshes);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Draws all the entities
        /// </summary>
        /// <param name="cameraMatrix"></param>
        private void DrawEntities(Matrix4f cameraMatrix)
        {
            uint currentIndicieCount = 0;

            foreach (IRenderable comp in renderComponents)
            {
                uint quadsCount = (uint)(comp.Vertices.Length / 4);

                if (!comp.Enabled || !comp.Entity.Enabled)
                {
                    currentIndicieCount += quadsCount * 6 * sizeof(int);
                    continue;
                }

                // Set Model view perspective
                Matrix4f mvp = cameraMatrix * comp.Entity.Transform.GetMatrix();

                testShader.SetUniformM4("MVP", mvp);

                testShader.Bind();
                VertexArray.Bind();
                IndexBuffer.Bind();

                for (int i = 0; i < quadsCount; i++)
                {
                    Gl.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, (IntPtr)(currentIndicieCount + (i * 6 * sizeof(int))));
                }

                IndexBuffer.Unbind();
                VertexArray.Unbind();
                testShader.Unbind();

                currentIndicieCount += quadsCount * 6 * sizeof(int);
            }
        }
Exemplo n.º 11
0
    static private Matrix4x4 toMat(Matrix4f m4)
    {
        Matrix4x4 mat = new Matrix4x4();

        mat.m00 = m4.m11;
        mat.m01 = m4.m12;
        mat.m02 = m4.m13;
        mat.m03 = m4.m14;

        mat.m10 = m4.m21;
        mat.m11 = m4.m22;
        mat.m12 = m4.m23;
        mat.m13 = m4.m24;

        mat.m20 = m4.m31;
        mat.m21 = m4.m32;
        mat.m22 = m4.m33;
        mat.m23 = m4.m34;

        mat.m30 = m4.m41;
        mat.m31 = m4.m42;
        mat.m32 = m4.m43;
        mat.m33 = m4.m44;

        return(mat);
    }
Exemplo n.º 12
0
        /// <summary>Transform a Vector3d by the given Matrix, and project the resulting Vector4f back to a Vector3d</summary>
        /// <param name="vec">The vector to transform</param>
        /// <param name="mat">The desired transformation</param>
        /// <returns>The transformed vector</returns>
        public static Vector3d TransformPerspective(Vector3d vec, Matrix4f mat)
        {
            Vector3d result;

            TransformPerspective(ref vec, ref mat, out result);
            return(result);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Multiplies the two matrices.
        /// </summary>
        /// <param name="left">First matrix to multiply.</param>
        /// <param name="right">Second matrix to multiply.</param>
        /// <returns>Product of the multiplication.</returns>
        public static Matrix3x2f Multiply(Matrix4f left, Matrix3x2f right)
        {
            Matrix3x2f res;

            Multiply(ref left, ref right, out res);
            return(res);
        }
Exemplo n.º 14
0
        /// <summary> Sets the value of this axis-angle to the rotational component of
        /// the passed matrix.
        /// If the specified matrix has no rotational component, the value
        /// of this AxisAngle4f is set to an angle of 0 about an axis of (0,1,0).
        /// </summary>
        /// <param name="m1">the matrix4f
        /// </param>
        public void  set_Renamed(Matrix4f m1)
        {
            Matrix3f m3f = new Matrix3f();

            m1.get_Renamed(m3f);

            x = m3f.m21 - m3f.m12;
            y = m3f.m02 - m3f.m20;
            z = m3f.m10 - m3f.m01;
            double mag = x * x + y * y + z * z;

            if (mag > EPS)
            {
                mag = System.Math.Sqrt(mag);
                double sin = 0.5 * mag;
                double cos = 0.5 * (m3f.m00 + m3f.m11 + m3f.m22 - 1.0);

                //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                angle = (float)System.Math.Atan2(sin, cos);
                double invMag = 1.0 / mag;
                //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                x = (float)(x * invMag);
                //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                y = (float)(y * invMag);
                //UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
                z = (float)(z * invMag);
            }
            else
            {
                x     = 0.0f;
                y     = 1.0f;
                z     = 0.0f;
                angle = 0.0f;
            }
        }
Exemplo n.º 15
0
        public override void Update()
        {
            if (_innerRigidBody != null && _shouldUpdate)
            {
                Collider  c = _geCollider;
                Transform t = gameElement.WorldTransform;
                // _innerRigidBody.MotionState.WorldTransform = (Matrix)t.GetTransformation();
                // .ActivationState == ActivationState.ActiveTag ? activeColor : passiveColor
                Matrix4f transform = _rbWorldTransfom;
                //gameElement.LocalTransform.Move(transform.GetTranslate());
                //gameElement.LocalTransform.Rotate(transform.GetEulerAngles());

                Vector3f
                    newT   = transform.GetTranslate(),
                    newR   = transform.GetEulerAngles(),
                    deltaT = newT - _lastTranslate,
                    deltaR = newR - _lastRotate;

                gameElement.LocalTransform.SetTranslation(newT);
                gameElement.LocalTransform.SetRotation(newR);

                _lastRotate    = newR;
                _lastTranslate = newT;

                _shouldUpdate = false;
            }
        }
Exemplo n.º 16
0
        public override void Update()
        {
            this.shadowMap0 = parent.ShadowCams[0].ShadowMap;
            this.shadowMap1 = parent.ShadowCams[1].ShadowMap;
            this.shadowMap2 = parent.ShadowCams[2].ShadowMap;
            this.shadowMap3 = parent.ShadowCams[3].ShadowMap;
            this.shadowMapViewProjTrans0 = parent.ShadowCams[0].ViewProjectionMatrix;
            this.shadowMapViewProjTrans1 = parent.ShadowCams[1].ViewProjectionMatrix;
            this.shadowMapViewProjTrans2 = parent.ShadowCams[2].ViewProjectionMatrix;
            this.shadowMapViewProjTrans3 = parent.ShadowCams[3].ViewProjectionMatrix;
            this.splits = parent.Splits;

            camMat.SetToLookAt(cam.Translation, cam.Translation.Add(new Vector3f(0, 0, -1)), cam.Up);
            camMat.MultiplyStore(cam.ProjectionMatrix);
            shader.SetUniform("u_camMat", camMat);
            Texture.ActiveTextureSlot(0);
            shadowMap0.Use();
            Texture.ActiveTextureSlot(1);
            shadowMap1.Use();
            Texture.ActiveTextureSlot(2);
            shadowMap2.Use();
            Texture.ActiveTextureSlot(3);
            shadowMap3.Use();
            Texture.ActiveTextureSlot(4);
            depthTexture.Use();
            Texture.ActiveTextureSlot(5);
            colorTexture.Use();
            Texture.ActiveTextureSlot(6);
            noiseMap.Use();

            shader.SetUniform("u_shadowMap0", 0);
            shader.SetUniform("u_shadowMap1", 1);
            shader.SetUniform("u_shadowMap2", 2);
            shader.SetUniform("u_shadowMap3", 3);
            shader.SetUniform("u_depthTexture", 4);
            shader.SetUniform("u_sceneTexture", 5);
            shader.SetUniform("u_noiseTexture", 6);
            shader.SetUniform("u_shadowMapViewProjTrans0", shadowMapViewProjTrans0);
            shader.SetUniform("u_shadowMapViewProjTrans1", shadowMapViewProjTrans1);
            shader.SetUniform("u_shadowMapViewProjTrans2", shadowMapViewProjTrans2);
            shader.SetUniform("u_shadowMapViewProjTrans3", shadowMapViewProjTrans3);
            shader.SetUniform("u_cameraPosition", cam.Translation);
            shader.SetUniform("u_invViewProj", cam.InverseViewProjectionMatrix);
            shader.SetUniform("u_view", cam.ViewMatrix);
            shader.SetUniform("u_proj", cam.ProjectionMatrix);

            if (debugMode)
            {
                shader.SetUniform("B_debugMode", 1);
            }
            else
            {
                shader.SetUniform("B_debugMode", 0);
            }
            for (int i = 0; i < splits.Length; i++)
            {
                shader.SetUniform("splits[" + i + "]", splits[i]);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Returns the transformation matrix of this <see cref="GameElement"/>.
        /// </summary>
        public Matrix4f GetTransformation()
        {
            Matrix4f t = Matrix4f.CreateTranslation(_translation);
            Matrix4f r = Matrix4f.CreateRotation(_rotation);
            Matrix4f s = Matrix4f.CreateScale(_scale);

            return(s * r * t);
        }
Exemplo n.º 18
0
 private void UpdateSkinning(Mesh mesh)
 {
     for (int i = 0; i < boneNames.Count; i++)
     {
         Matrix4f boneMat = mesh.GetSkeleton().GetBone(i).GetBoneMatrix();
         SetUniform(boneNames[i], boneMat);
     }
 }
Exemplo n.º 19
0
 private void Transform(ref Vector3f[] inVec, ref Vector3f[] outVec, ref Matrix4f mat)
 {
     for (int i = 0; i < inVec.Length; i++)
     {
         outVec[i].Set(inVec[i]);
         outVec[i].MultiplyStore(mat);
     }
 }
Exemplo n.º 20
0
        /// <summary> Sets the value of this quaternion to the rotational component of
        /// the passed matrix.
        /// </summary>
        /// <param name="m1">the matrix4f
        /// </param>
        public void  set_Renamed(Matrix4f m1)
        {
            double ww = 0.25 * (m1.m00 + m1.m11 + m1.m22 + m1.m33);

            if (ww >= 0)
            {
                if (ww >= EPS2)
                {
                    this.w = System.Math.Sqrt(ww);
                    ww     = 0.25 / this.w;
                    this.x = ((m1.m21 - m1.m12) * ww);
                    this.y = ((m1.m02 - m1.m20) * ww);
                    this.z = ((m1.m10 - m1.m01) * ww);
                    return;
                }
            }
            else
            {
                this.w = 0;
                this.x = 0;
                this.y = 0;
                this.z = 1;
                return;
            }

            this.w = 0;
            ww     = (-0.5) * (m1.m11 + m1.m22);
            if (ww >= 0)
            {
                if (ww >= EPS2)
                {
                    this.x = System.Math.Sqrt(ww);
                    ww     = 1.0 / (2.0 * this.x);
                    this.y = (m1.m10 * ww);
                    this.z = (m1.m20 * ww);
                    return;
                }
            }
            else
            {
                this.x = 0;
                this.y = 0;
                this.z = 1;
                return;
            }

            this.x = 0;
            ww     = 0.5 * (1.0 - m1.m22);
            if (ww >= EPS2)
            {
                this.y = System.Math.Sqrt(ww);
                this.z = (m1.m21) / (2.0 * this.y);
                return;
            }

            this.y = 0;
            this.z = 1;
        }
Exemplo n.º 21
0
        public Vector3f Multiply(Matrix4f mat)
        {
            Vector3f res = new Vector3f();

            res.Set(x * mat.values[Matrix4f.m00] + y * mat.values[Matrix4f.m01] + z * mat.values[Matrix4f.m02] + mat.values[Matrix4f.m03],
                    x * mat.values[Matrix4f.m10] + y * mat.values[Matrix4f.m11] + z * mat.values[Matrix4f.m12] + mat.values[Matrix4f.m13],
                    x * mat.values[Matrix4f.m20] + y * mat.values[Matrix4f.m21] + z * mat.values[Matrix4f.m22] + mat.values[Matrix4f.m23]);
            return(res);
        }
Exemplo n.º 22
0
        /// <summary>Transform a Vector3d by the given Matrix, and project the resulting Vector4f back to a Vector3d</summary>
        /// <param name="vec">The vector to transform</param>
        /// <param name="mat">The desired transformation</param>
        /// <param name="result">The transformed vector</param>
        public static void TransformPerspective(ref Vector3d vec, ref Matrix4f mat, out Vector3d result)
        {
            Vector4d v = new Vector4d(vec, 1);

            Vector4d.Transform(ref v, ref mat, out v);
            result.X = v.X / v.W;
            result.Y = v.Y / v.W;
            result.Z = v.Z / v.W;
        }
Exemplo n.º 23
0
        /// <summary>Transform a Position by the given Matrix</summary>
        /// <param name="pos">The position to transform</param>
        /// <param name="mat">The desired transformation</param>
        /// <returns>The transformed position</returns>
        public static Vector3d TransformPosition(Vector3d pos, Matrix4f mat)
        {
            Vector3d p;

            p.X = Vector3d.Dot(pos, new Vector3d(mat.Column0)) + mat.Row3.X;
            p.Y = Vector3d.Dot(pos, new Vector3d(mat.Column1)) + mat.Row3.Y;
            p.Z = Vector3d.Dot(pos, new Vector3d(mat.Column2)) + mat.Row3.Z;
            return(p);
        }
Exemplo n.º 24
0
        /// <summary>Transform a Normal by the (transpose of the) given Matrix</summary>
        /// <remarks>
        /// This version doesn't calculate the inverse matrix.
        /// Use this version if you already have the inverse of the desired transform to hand
        /// </remarks>
        /// <param name="norm">The normal to transform</param>
        /// <param name="invMat">The inverse of the desired transformation</param>
        /// <returns>The transformed normal</returns>
        public static Vector3d TransformNormalInverse(Vector3d norm, Matrix4f invMat)
        {
            Vector3d n;

            n.X = Vector3d.Dot(norm, new Vector3d(invMat.Row0));
            n.Y = Vector3d.Dot(norm, new Vector3d(invMat.Row1));
            n.Z = Vector3d.Dot(norm, new Vector3d(invMat.Row2));
            return(n);
        }
Exemplo n.º 25
0
        /// <summary>Transform a direction vector by the given Matrix
        /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
        /// </summary>
        /// <param name="vec">The vector to transform</param>
        /// <param name="mat">The desired transformation</param>
        /// <returns>The transformed vector</returns>
        public static Vector3d TransformVector(Vector3d vec, Matrix4f mat)
        {
            Vector3d v;

            v.X = Vector3d.Dot(vec, new Vector3d(mat.Column0));
            v.Y = Vector3d.Dot(vec, new Vector3d(mat.Column1));
            v.Z = Vector3d.Dot(vec, new Vector3d(mat.Column2));
            return(v);
        }
Exemplo n.º 26
0
        public void SetUniform(string name, Matrix4f data)
        {
            int location = _getUniformLocation(name);

            if (location >= 0 && _checkCache(name, data))
            {
                GL.UniformMatrix4(location, data);
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Set a uniform mat4 in the shader.
        /// Uses a cached float[] to reduce memory usage.
        /// </summary>
        /// <param name="location">The location of the uniform in the shader.</param>
        /// <param name="param">The Matrix4f to load into the shader uniform.</param>
        public static void UniformMatrix4(int location, Matrix4f param)
        {
            // use the statically allocated float[] for setting the uniform
            matrix4Float[0]  = param[0].X; matrix4Float[1] = param[0].Y; matrix4Float[2] = param[0].Z; matrix4Float[3] = param[0].W;
            matrix4Float[4]  = param[1].X; matrix4Float[5] = param[1].Y; matrix4Float[6] = param[1].Z; matrix4Float[7] = param[1].W;
            matrix4Float[8]  = param[2].X; matrix4Float[9] = param[2].Y; matrix4Float[10] = param[2].Z; matrix4Float[11] = param[2].W;
            matrix4Float[12] = param[3].X; matrix4Float[13] = param[3].Y; matrix4Float[14] = param[3].Z; matrix4Float[15] = param[3].W;

            GL.UniformMatrix4fv(location, 1, false, matrix4Float);
        }
Exemplo n.º 28
0
        public virtual void SetWorldTransformPhysics(Vector3f trans, Quaternion rot, Vector3f scl)
        {
            worldTransform.SetTranslation(trans);
            worldTransform.SetRotation(rot);
            //  worldTransform.SetScale(scl);
            worldMatrix = worldTransform.GetMatrix();

            UpdateWorldBoundingBox();
            UpdateLocalBoundingBox();
        }
Exemplo n.º 29
0
 protected void UpdateMatrix()
 {
     transMat.SetToTranslation(translation);
     rotMat.SetToRotation(rotation);
     scaleMat.SetToScaling(scale);
     rotScale.Set(rotMat);
     rotScale.MultiplyStore(scaleMat);
     rotScale.MultiplyStore(transMat);
     this.matrix = rotScale;
 }
Exemplo n.º 30
0
        /// <summary>
        /// Creates an orientation Quaternion given the 3 axis.
        /// </summary>
        /// <param name="Axis">An array of 3 axis.</param>
        public static Quaternion FromAxis(Vector3f xvec, Vector3f yvec, Vector3f zvec)
        {
            Matrix4f Rotation = new Matrix4f();

            Rotation.Right    = xvec;
            Rotation.Up       = yvec;
            Rotation.Backward = zvec;

            return(FromRotationMatrix(Rotation));
        }
Exemplo n.º 31
0
        public Camera()            
        {
            FieldOfView = ( float ) ( Math.PI / 4.0 );

            // interpolationKeyFrames = new KeyFrameInterpolator
            Frame = new ManipulatedCameraFrame();

            SceneRadius = 1.0f;
            orthoCoeff = ( float ) ( Math.Tan( FieldOfView / 2.0 ) );
            SceneCenter = Vector3f.Zero;

            CameraType = CameraType.PERSPECTIVE;

            ZNearCoefficient = 0.005f;
            ZClippingCoefficient = ( float ) ( Math.Sqrt( 3.0 ) );

            // dummy values
            screenSize = new Vector2i( 400, 400 );

            viewMatrix = Matrix4f.Identity;
            projectionMatrix = Matrix4f.Zero;

            ComputeProjectionMatrix();
        }
Exemplo n.º 32
0
 public void RenderOpaqueTexture( ShaderResourceView textureSRV, Rect2f rect, Matrix4f pvw, bool yPointsUp )
 {
     RenderTexture( textureSRV, rect, pvw, true, yPointsUp );
 }
Exemplo n.º 33
0
 public void RenderAlphaTexture( ShaderResourceView textureSRV, Rect2f rect, Matrix4f pvw, bool yPointsUp )
 {
     RenderTexture( textureSRV, rect, pvw, false, yPointsUp );
 }
Exemplo n.º 34
0
        public void GetJittered( Vector2f dxdy, float zFocus,
            out Matrix4f jitteredView, out Matrix4f jitteredProjection )
        {
            // var camera = new Camera( this );
            var newEye = Position + dxdy.x * RightVector + dxdy.y * UpVector;
            jitteredView = Matrix4f.LookAt( newEye, newEye + ViewDirection, UpVector );

            // compute new projection matrix
            float halfThetaY = 0.5f * FieldOfView;
            float halfThetaX = 0.5f * HorizontalFieldOfView;

            float topFocus = zFocus * MathUtils.Tan( halfThetaY );
            float newTopFocus = topFocus - dxdy.y;
            float newTop = ZNear * newTopFocus / zFocus;

            float bottomFocus = zFocus * MathUtils.Tan( halfThetaY );
            float newBottomFocus = bottomFocus + dxdy.y;
            float newBottom = -ZNear * newBottomFocus / zFocus;

            float leftFocus = zFocus * MathUtils.Tan( halfThetaX );
            float newLeftFocus = leftFocus + dxdy.x;
            float newLeft = -ZNear * newLeftFocus / zFocus;

            float rightFocus = zFocus * MathUtils.Tan( halfThetaX );
            float newRightFocus = rightFocus - dxdy.x;
            float newRight = ZNear * newRightFocus / zFocus;

            jitteredProjection = Matrix4f.PerspectiveOffCenterD3D( newLeft, newRight, newBottom, newTop, ZNear, ZFar );
        }
Exemplo n.º 35
0
        public Camera( Camera camera )
        {
            // #CONNECTION# Camera constructor
            // interpolationKfi_ = new KeyFrameInterpolator;
            
            // Requires the interpolationKfi_
            Frame = new ManipulatedCameraFrame();

            viewMatrix = Matrix4f.Identity;
            projectionMatrix = Matrix4f.Zero;

            Set( camera );
        }
Exemplo n.º 36
0
        public void ComputeProjectionMatrix()
        {
            switch( CameraType )
            {
                case CameraType.PERSPECTIVE:
                    {                        
                        projectionMatrix = Matrix4f.PerspectiveD3D( FieldOfView, AspectRatio, ZNear, ZFar );

                        break;
                    }
                case CameraType.ORTHOGRAPHIC:
                    {
                        Vector2f wh = GetOrthoWidthHeight();
                        float w = wh.x;
                        float h = wh.y;
                        projectionMatrix = Matrix4f.OrthoD3D( w, h, ZNear, ZFar );

                        break;
                    }
            }

            inverseProjectionMatrix = projectionMatrix.Inverse();
        }
Exemplo n.º 37
0
 public Quaternion SetFromMatrix(Matrix4f mat)
 {
     SetFromAxes(mat.values[Matrix4f.m00], mat.values[Matrix4f.m10], mat.values[Matrix4f.m20],
                 mat.values[Matrix4f.m01], mat.values[Matrix4f.m11], mat.values[Matrix4f.m21],
                 mat.values[Matrix4f.m02], mat.values[Matrix4f.m12], mat.values[Matrix4f.m22]);
     return this;
 }
Exemplo n.º 38
0
 /// <summary>
 /// Crea una nueva instancia inicializados sus valores a los valores de la matriz parámetro
 /// </summary>
 /// <param Name="matrix"></param>
 public Matrix4f(Matrix4f matrix)
 {
     this.CopyFrom(matrix);
 }
Exemplo n.º 39
0
 public void SetUniform(string name, Matrix4f mat)
 {
     RenderManager.Renderer.SetShaderUniform(id, name, mat.values);
 }
Exemplo n.º 40
0
 public void SetTransforms(Matrix4f world, Matrix4f view, Matrix4f proj)
 {
     worldMatrix = world;
     viewMatrix = view;
     projectionMatrix = proj;
 }
Exemplo n.º 41
0
 private void Transform(ref Vector3f[] inVec, ref Vector3f[] outVec, ref Matrix4f mat)
 {
     for (int i = 0; i < inVec.Length; i++)
     {
         outVec[i].Set(inVec[i]);
         outVec[i].MultiplyStore(mat);
     }
 }
 public Matrix4f GetMatrix4f()
 {
     Matrix4f result = new Matrix4f();
                 unsafe { Get(&result.XX, new Vector2i(4, 4), 1); }
                 return result;
 }
Exemplo n.º 43
0
    /**
     * Sets the value of this matrix to the result of multiplying
     * the two argument matrices together.
     *
     * @param m1 the first matrix
     * @param m2 the second matrix
     */
    public void mul(Matrix4f m1, Matrix4f m2)
    {
        // alias-safe way.
        set(
            m1.M00 * m2.M00 + m1.M01 * m2.M10 + m1.M02 * m2.M20 + m1.M03 * m2.M30,
            m1.M00 * m2.M01 + m1.M01 * m2.M11 + m1.M02 * m2.M21 + m1.M03 * m2.M31,
            m1.M00 * m2.M02 + m1.M01 * m2.M12 + m1.M02 * m2.M22 + m1.M03 * m2.M32,
            m1.M00 * m2.M03 + m1.M01 * m2.M13 + m1.M02 * m2.M23 + m1.M03 * m2.M33,

            m1.M10 * m2.M00 + m1.M11 * m2.M10 + m1.M12 * m2.M20 + m1.M13 * m2.M30,
            m1.M10 * m2.M01 + m1.M11 * m2.M11 + m1.M12 * m2.M21 + m1.M13 * m2.M31,
            m1.M10 * m2.M02 + m1.M11 * m2.M12 + m1.M12 * m2.M22 + m1.M13 * m2.M32,
            m1.M10 * m2.M03 + m1.M11 * m2.M13 + m1.M12 * m2.M23 + m1.M13 * m2.M33,

            m1.M20 * m2.M00 + m1.M21 * m2.M10 + m1.M22 * m2.M20 + m1.M23 * m2.M30,
            m1.M20 * m2.M01 + m1.M21 * m2.M11 + m1.M22 * m2.M21 + m1.M23 * m2.M31,
            m1.M20 * m2.M02 + m1.M21 * m2.M12 + m1.M22 * m2.M22 + m1.M23 * m2.M32,
            m1.M20 * m2.M03 + m1.M21 * m2.M13 + m1.M22 * m2.M23 + m1.M23 * m2.M33,

            m1.M30 * m2.M00 + m1.M31 * m2.M10 + m1.M32 * m2.M20 + m1.M33 * m2.M30,
            m1.M30 * m2.M01 + m1.M31 * m2.M11 + m1.M32 * m2.M21 + m1.M33 * m2.M31,
            m1.M30 * m2.M02 + m1.M31 * m2.M12 + m1.M32 * m2.M22 + m1.M33 * m2.M32,
            m1.M30 * m2.M03 + m1.M31 * m2.M13 + m1.M32 * m2.M23 + m1.M33 * m2.M33
            );
    }
Exemplo n.º 44
0
        /// <summary>
        /// Renders a texture "textureSRV" into a rectangle rect.
        /// Rect is always specified such that x points right and y points up,
        /// but can be modified by pvw.
        /// 
        /// opaque specifies whether to use the opaque or alpha pass.
        /// 
        /// yPointsUp specifies that in the *input image* textureSRV, whether y points up
        /// If false, then [0,0] is the top left corner, else it's the bottom left corner
        /// </summary>
        /// <param name="textureSRV"></param>
        /// <param name="rect"></param>
        /// <param name="pvw"></param>
        /// <param name="opaque"></param>
        /// <param name="yPointsUp"></param>
        private void RenderTexture( ShaderResourceView textureSRV, Rect2f rect, Matrix4f pvw, bool opaque, bool yPointsUp )
        {
            // TODO: split: passing in opaque is stupid

            var viewportMatrix = Matrix4f.Identity;
            var textureMatrix = Matrix4f.Identity;

            viewportMatrix.m00 = rect.Width;
            viewportMatrix.m11 = rect.Height;
            viewportMatrix.m03 = rect.Origin.x;
            viewportMatrix.m13 = rect.Origin.y;

            pvw = pvw * viewportMatrix;

            if( yPointsUp )
            {
                textureMatrix.m11 = -1;
                textureMatrix.m13 = 1;
            }            

            effect.GetVariableByName( "pvw" ).AsMatrix().SetMatrix( pvw );
            effect.GetVariableByName( "textureMatrix" ).AsMatrix().SetMatrix( textureMatrix );
            effect.GetVariableByName( "tex" ).AsResource().SetResource( textureSRV );

            d3d.Device.InputAssembler.SetVertexBuffers( 0, textureVertexBuffer.DefaultBinding );
            d3d.Device.InputAssembler.SetPrimitiveTopology( PrimitiveTopology.TriangleList );

            if( opaque )
            {
                d3d.Device.InputAssembler.SetInputLayout( opaqueTexturePassLayout );
                opaqueTexturePass.Apply();
            }
            else
            {
                d3d.Device.InputAssembler.SetInputLayout( alphaTexturePassLayout );
                alphaTexturePass.Apply();
            }            
            
            d3d.Device.Draw( 6, 0 );
        }
Exemplo n.º 45
0
        public void Render( Matrix4f pvw )
        {
            effect.GetVariableByName( "pvw" ).AsMatrix().SetMatrix( pvw );            

            streamState.Begin();

            foreach( BlendType pass in Enum.GetValues( typeof( BlendType ) ) )            
            {
                ApplyPass( pass );
                RenderPass( pass );
            }

            streamState.End();
        }
Exemplo n.º 46
0
 /// <summary>
 /// Copia los valores de esta matriz desde la matriz pasada como parámetro
 /// </summary>
 /// <param Name="matrix"></param>
 public void CopyFrom(Matrix4f matrix)
 {
     matrix.values.CopyTo(values, 0);
 }
Exemplo n.º 47
0
 protected void UpdateMatrix()
 {
     transMat.SetToTranslation(translation);
     rotMat.SetToRotation(rotation);
     scaleMat.SetToScaling(scale);
     rotScale.Set(rotMat);
     rotScale.MultiplyStore(scaleMat);
     rotScale.MultiplyStore(transMat);
     this.matrix = rotScale;
 }
Exemplo n.º 48
0
 public UniversalManipulatorTransform()
 {
     transformType = UniversalManipulatorTransformType.None;
     transform = Matrix4f.Identity;
 }
Exemplo n.º 49
0
        public void ComputeViewMatrix()
        {
            var q = Frame.Orientation;

            float q00 = 2.0f * q[ 0 ] * q[ 0 ];
            float q11 = 2.0f * q[ 1 ] * q[ 1 ];
            float q22 = 2.0f * q[ 2 ] * q[ 2 ];

            float q01 = 2.0f * q[ 0 ] * q[ 1 ];
            float q02 = 2.0f * q[ 0 ] * q[ 2 ];
            float q03 = 2.0f * q[ 0 ] * q[ 3 ];

            float q12 = 2.0f * q[ 1 ] * q[ 2 ];
            float q13 = 2.0f * q[ 1 ] * q[ 3 ];

            float q23 = 2.0f * q[ 2 ] * q[ 3 ];

            viewMatrix[ 0, 0 ] = 1.0f - q11 - q22;
            viewMatrix[ 1, 0 ] = q01 - q23;
            viewMatrix[ 2, 0 ] = q02 + q13;
            viewMatrix[ 3, 0 ] = 0.0f;

            viewMatrix[ 0, 1 ] = q01 + q23;
            viewMatrix[ 1, 1 ] = 1.0f - q22 - q00;
            viewMatrix[ 2, 1 ] = q12 - q03;
            viewMatrix[ 3, 1 ] = 0.0f;

            viewMatrix[ 0, 2 ] = q02 - q13;
            viewMatrix[ 1, 2 ] = q12 + q03;
            viewMatrix[ 2, 2 ] = 1.0f - q11 - q00;
            viewMatrix[ 3, 2 ] = 0.0f;

            var t = q.InverseRotate( Frame.Position );

            viewMatrix[ 0, 3 ] = -t.x;
            viewMatrix[ 1, 3 ] = -t.y;
            viewMatrix[ 2, 3 ] = -t.z;
            viewMatrix[ 3, 3 ] = 1.0f;

            inverseViewMatrix = viewMatrix.Inverse();
        }
Exemplo n.º 50
0
 public Vector3f Multiply(Matrix4f mat)
 {
     Vector3f res = new Vector3f();
     res.Set(x * mat.values[Matrix4f.m00] + y * mat.values[Matrix4f.m01] + z * mat.values[Matrix4f.m02] + mat.values[Matrix4f.m03],
             x * mat.values[Matrix4f.m10] + y * mat.values[Matrix4f.m11] + z * mat.values[Matrix4f.m12] + mat.values[Matrix4f.m13],
             x * mat.values[Matrix4f.m20] + y * mat.values[Matrix4f.m21] + z * mat.values[Matrix4f.m22] + mat.values[Matrix4f.m23]);
     return res;
 }
Exemplo n.º 51
0
        public Matrix4f ScreenToWorld()
        {
            var inverseViewport = new Matrix4f
            (
                2.0f / ScreenSize.x, 0, 0, -1,
                0, 2.0f / ScreenSize.y, 0, -1,
                0, 0, 1, 0,
                0, 0, 0, 1
            );

            return InverseViewMatrix * InverseProjectionMatrix * inverseViewport;
        }
Exemplo n.º 52
0
 public Vector3f MultiplyStore(Matrix4f mat)
 {
     return Set(x * mat.values[Matrix4f.m00] + y * mat.values[Matrix4f.m01] + z * mat.values[Matrix4f.m02] + mat.values[Matrix4f.m03],
             x * mat.values[Matrix4f.m10] + y * mat.values[Matrix4f.m11] + z * mat.values[Matrix4f.m12] + mat.values[Matrix4f.m13],
             x * mat.values[Matrix4f.m20] + y * mat.values[Matrix4f.m21] + z * mat.values[Matrix4f.m22] + mat.values[Matrix4f.m23]);
 }
Exemplo n.º 53
0
        public override void Update()
        {
            this.shadowMap0 = parent.ShadowCams[0].ShadowMap;
            this.shadowMap1 = parent.ShadowCams[1].ShadowMap;
            this.shadowMap2 = parent.ShadowCams[2].ShadowMap;
            this.shadowMap3 = parent.ShadowCams[3].ShadowMap;
            this.shadowMapViewProjTrans0 = parent.ShadowCams[0].ViewProjectionMatrix;
            this.shadowMapViewProjTrans1 = parent.ShadowCams[1].ViewProjectionMatrix;
            this.shadowMapViewProjTrans2 = parent.ShadowCams[2].ViewProjectionMatrix;
            this.shadowMapViewProjTrans3 = parent.ShadowCams[3].ViewProjectionMatrix;
            this.splits = parent.Splits;

            camMat.SetToLookAt(cam.Translation, cam.Translation.Add(new Vector3f(0, 0, -1)), cam.Up);
            camMat.MultiplyStore(cam.ProjectionMatrix);
            shader.SetUniform("u_camMat", camMat);
            Texture.ActiveTextureSlot(0);
            shadowMap0.Use();
            Texture.ActiveTextureSlot(1);
            shadowMap1.Use();
            Texture.ActiveTextureSlot(2);
            shadowMap2.Use();
            Texture.ActiveTextureSlot(3);
            shadowMap3.Use();
            Texture.ActiveTextureSlot(4);
            depthTexture.Use();
            Texture.ActiveTextureSlot(5);
            colorTexture.Use();
            Texture.ActiveTextureSlot(6);
            noiseMap.Use();

            shader.SetUniform("u_shadowMap0", 0);
            shader.SetUniform("u_shadowMap1", 1);
            shader.SetUniform("u_shadowMap2", 2);
            shader.SetUniform("u_shadowMap3", 3);
            shader.SetUniform("u_depthTexture", 4);
            shader.SetUniform("u_sceneTexture", 5);
            shader.SetUniform("u_noiseTexture", 6);
            shader.SetUniform("u_shadowMapViewProjTrans0", shadowMapViewProjTrans0);
            shader.SetUniform("u_shadowMapViewProjTrans1", shadowMapViewProjTrans1);
            shader.SetUniform("u_shadowMapViewProjTrans2", shadowMapViewProjTrans2);
            shader.SetUniform("u_shadowMapViewProjTrans3", shadowMapViewProjTrans3);
            shader.SetUniform("u_cameraPosition", cam.Translation);
            shader.SetUniform("u_invViewProj", cam.InverseViewProjectionMatrix);
            shader.SetUniform("u_view", cam.ViewMatrix);
            shader.SetUniform("u_proj", cam.ProjectionMatrix);

            if (debugMode)
            {
                shader.SetUniform("B_debugMode", 1);
            }
            else
            {
                shader.SetUniform("B_debugMode", 0);
            }
            for (int i = 0; i < splits.Length; i++)
            {
                shader.SetUniform("splits[" + i + "]", splits[i]);
            }
        }
Exemplo n.º 54
0
 public void set(Matrix4f m1)
 {
     M00 = m1.M00; M01 = m1.M01; M02 = m1.M02; M03 = m1.M03;
     M10 = m1.M10; M11 = m1.M11; M12 = m1.M12; M13 = m1.M13;
     M20 = m1.M20; M21 = m1.M21; M22 = m1.M22; M23 = m1.M23;
     M30 = m1.M30; M31 = m1.M31; M32 = m1.M32; M33 = m1.M33;
 }