Ejemplo n.º 1
0
        public static Quaternion CalcBodyToImuRotationFromPitch(Quaternion q1, Quaternion q2, bool snapToGrid = true)
        {
            // move to GlmSharp structs
            quat _q1 = new quat((float)q1.X, (float)q1.Y, (float)q1.Z, (float)q1.W);
            quat _q2 = new quat((float)q2.X, (float)q2.Y, (float)q2.Z, (float)q2.W);

            // normalize input quaternions
            _q1 = _q1.Normalized;
            _q2 = _q2.Normalized;

            // find rotation from Body frame to the Inertial frame
            quat q_axis = _q1 * _q2.Inverse;
            vec3 y_bi   = q_axis.Axis;

            if (snapToGrid)
            {
                y_bi[2] = 0.0f;
            }
            y_bi = y_bi.Normalized;
            vec3 z_bi = new vec3(0.0f, 0.0f, 1.0f);
            vec3 x_bi = vec3.Cross(y_bi, z_bi).Normalized;

            mat3 R_ib = new mat3(x_bi, y_bi, z_bi);
            quat q_bi = R_ib.Transposed.ToQuaternion;

            // calculate the (fixed) rotation from the IMU frame to the Body frame
            quat q_bm = (q_bi * _q1).Inverse.Normalized;

            return(new Quaternion(q_bm.x, q_bm.y, q_bm.z, q_bm.w));
        }
Ejemplo n.º 2
0
        public static int Main(string[] args)
        {
            mat2 m = (1, 2, 0, -1);

            poly p1 = (0, 0, 0, 2);
            poly p2 = (0, 0, 3);

            var r0 = p2.Derivative;
            var r1 = p1 << 1;
            var r2 = p1 >> 1;
            var r3 = p1 + p2;
            var r4 = p1 - p2;
            var r5 = p1 * p2;
            var r6 = p1 / p2;
            var r7 = p1 % p2;


            mat3 mat = (
                1, 1, 1,
                1, 0, 1,
                1, 0, 1
                );
            vec3 b = (0, 4, 2);
            var  x = mat.Solve(b);

            poly p3 = (-1, 1);
            poly p4 = (-1, 0, 1);
            poly p5 = (-1, 0, 0, 1);
            poly p6 = (0, 0, 0, 0, 0, 0, 1);
            poly p7 = (2, 1, 8, 3);

            return(0);
        }
        /// <summary>
        /// </summary>
        /// <param name="uniformName"></param>
        /// <param name="matrixes"></param>
        internal int glUniform(string uniformName, mat3[] matrixes)
        {
            int location = GetUniformLocation(uniformName);

            if (location >= 0)
            {
                if (glUniformMatrix3fv == null)
                {
                    glUniformMatrix3fv = GL.Instance.GetDelegateFor("glUniformMatrix3fv", GLDelegates.typeof_void_int_int_bool_floatN) as GLDelegates.void_int_int_bool_floatN;
                }

                var values = new float[matrixes.Length * 9];
                for (int index = 0, i = 0; i < matrixes.Length; i++)
                {
                    mat3 matrix = matrixes[i];
                    values[index++] = matrix.col0.x;
                    values[index++] = matrix.col0.y;
                    values[index++] = matrix.col0.z;
                    values[index++] = matrix.col1.x;
                    values[index++] = matrix.col1.y;
                    values[index++] = matrix.col1.z;
                    values[index++] = matrix.col2.x;
                    values[index++] = matrix.col2.y;
                    values[index++] = matrix.col2.z;
                }
                glUniformMatrix3fv(location, matrixes.Length, false, values);
            }
            return(location);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This test shows that glm.rotate() and Quaternion.ToRotationMatrix() give the same result.
        /// </summary>
        public static void Test()
        {
            using (var writer = new StreamWriter("test-quaternion.txt"))
            {
                int length = 5;
                for (int angleDegree = 1; angleDegree < 361; angleDegree++)
                {
                    for (int x = -length; x < length; x++)
                    {
                        for (int y = -length; y < length; y++)
                        {
                            for (int z = -length; z < length; z++)
                            {
                                var  quaternion = new Quaternion(angleDegree, new vec3(x, y, z));
                                mat3 matrix1    = quaternion.ToRotationMatrix();
                                //mat4 tmp = glm.rotate((float)(angleDegree * Math.PI / 180.0f), new vec3(x, y, z));
                                mat4 tmp     = glm.rotate(angleDegree, new vec3(x, y, z));
                                mat3 matrix2 = tmp.to_mat3();
                                writer.WriteLine("====================");
                                writer.WriteLine("{3}° x[{0}] y[{1}] z[{2}]", x, y, z, angleDegree);
                                writer.WriteLine(matrix1.ToArray().PrintVectors(3, ",", ";" + Environment.NewLine));
                                writer.WriteLine("------------");
                                writer.WriteLine(matrix2.ToArray().PrintVectors(3, ",", ";" + Environment.NewLine));
                                //}
                            }
                        }
                    }
                }

                writer.WriteLine("Test finished.");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 把连续9个float值按照列优先的顺序转换为mat3
        /// </summary>
        /// <param name="values"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static mat3 ToMat3(this float[] values, int startIndex = 0)
        {
            mat3 result = new mat3(
                values.ToVec3(startIndex + 0), values.ToVec3(startIndex + 3), values.ToVec3(startIndex + 6));

            return result;
        }
Ejemplo n.º 6
0
        public void CheckMatrixReferenceEquality()
        {
            vec2 v2_1 = new vec2(1, 0);
            vec2 v2_2 = new vec2(1, 0);
            vec2 v2_3 = new vec2(0, 0);
            mat2 m2_1 = new mat2(v2_1, v2_2);
            mat2 m2_2 = new mat2(v2_1, v2_2);
            mat2 m2_3 = new mat2(v2_1, v2_3);

            vec3 v3_1 = new vec3(1, 0, 1);
            vec3 v3_2 = new vec3(1, 0, 1);
            vec3 v3_3 = new vec3(0, 0, 1);
            mat3 m3_1 = new mat3(v3_1, v3_2, v3_2);
            mat3 m3_2 = new mat3(v3_1, v3_2, v3_2);
            mat3 m3_3 = new mat3(v3_1, v3_2, v3_3);


            vec4 v4_1 = new vec4(1, 0, 1, 1);
            vec4 v4_2 = new vec4(1, 0, 1, 1);
            vec4 v4_3 = new vec4(0, 0, 1, 1);
            mat4 m4_1 = new mat4(v4_1, v4_2, v4_2, v4_2);
            mat4 m4_2 = new mat4(v4_1, v4_2, v4_2, v4_2);
            mat4 m4_3 = new mat4(v4_1, v4_2, v4_2, v4_3);

            Assert.AreEqual(m2_1.GetHashCode(), m2_2.GetHashCode(), "Error Hashcodes are not the Same");
            Assert.AreNotEqual(m2_1.GetHashCode(), m2_3.GetHashCode(), "Error Hashcodes are the Same");
            Assert.AreEqual(m3_1.GetHashCode(), m3_2.GetHashCode(), "Error Hashcodes are not the Same");
            Assert.AreNotEqual(m3_1.GetHashCode(), m3_3.GetHashCode(), "Error Hashcodes are the Same");
            Assert.AreEqual(m4_1.GetHashCode(), m4_2.GetHashCode(), "Error Hashcodes are not the Same");
            Assert.AreNotEqual(m4_1.GetHashCode(), m4_3.GetHashCode(), "Error Hashcodes are the Same");
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Calculates the rotation matrix around a given axis with a angle in degree
        /// </summary>
        /// <param name="rotationAxis"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public static mat3 calculateRotationMatrix(vec4 rotationAxis, float angle)
        {
            mat3  m = new mat3(1);
            float s = glm.sin(glm.radians(angle));

            s = (Math.Abs(s) < Constants.Epsilon) ? 0 : s;
            float c = glm.cos(glm.radians(angle));

            c = (Math.Abs(c) < Constants.Epsilon) ? 0 : c;
            float t = 1 - glm.cos(glm.radians(angle));

            t = (Math.Abs(t) < Constants.Epsilon) ? 0 : t;

            float x = rotationAxis[0];
            float y = rotationAxis[1];
            float z = rotationAxis[2];

            m[0, 0] = t * x * x + c;
            m[0, 1] = t * x * y + s * z;
            m[0, 2] = t * x * z - s * y;
            m[1, 0] = t * x * y - s * z;
            m[1, 1] = t * y * y + c;
            m[1, 2] = t * z * y + x * s;
            m[2, 0] = t * x * z + s * y;
            m[2, 1] = t * y * z - s * x;
            m[2, 2] = t * z * z + c;

            return(m);
        }
        public void OnCreate(ECSWorld world)
        {
            var fragShader = Asset.Load <ShaderAsset>("mesh_instanced_default.frag");
            var vertShader = Asset.Load <ShaderAsset>("mesh_instanced_default.vert");
            var shader     = new ShaderPipeline(fragShader, vertShader, ShaderType.Instanced);

            defaultShader = shader.ShaderPair;

            defaultMaterial = new Material(GraphicsContext.graphicsDevice,
                                           GraphicsContext.uniform0, defaultShader);
            defaultMaterial.wireframe = true;

            instanceMatrix = new DeviceBuffer(GraphicsContext.graphicsDevice, (ulong)Unsafe.SizeOf <ObjectToWorld>(),
                                              BufferUsageFlags.VertexBuffer, BufferMemoryUsageHint.Dynamic);


            var matrix       = mat4.Identity;
            var normalMatrix = new mat3(matrix);

            ObjectToWorld matrices = new ObjectToWorld()
            {
                model  = matrix,
                normal = normalMatrix
            };

            instanceMatrix.SetData(matrices, 0);

            MeshData initialData = new MeshData();

            initialData.subMeshes    = new SubMeshData[1];
            initialData.subMeshes[0] = new SubMeshData(vertices, indices);

            defaultMesh = new Mesh(GraphicsContext.graphicsDevice, initialData, true);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// </summary>
        public static void Test2()
        {
            using (var writer = new StreamWriter("test-quaternion2.txt"))
            {
                int length = 5;
                for (int angleDegree = 1; angleDegree < 361; angleDegree++)
                {
                    for (int x = -length; x < length; x++)
                    {
                        for (int y = -length; y < length; y++)
                        {
                            for (int z = -length; z < length; z++)
                            {
                                var        quaternion  = new Quaternion(angleDegree, new vec3(x, y, z));
                                mat3       matrix1     = quaternion.ToRotationMatrix();
                                Quaternion quaternion2 = matrix1.ToQuaternion();
                                writer.WriteLine("====================");
                                writer.WriteLine("{3}° x[{0}] y[{1}] z[{2}]", x, y, z, angleDegree);
                                writer.WriteLine(quaternion);
                                writer.WriteLine("------------");
                                writer.WriteLine(quaternion2);
                                //}
                            }
                        }
                    }
                }

                writer.WriteLine("Test finished.");
            }
        }
Ejemplo n.º 10
0
        /*
         * 3x3 matrix determinant
         */
        private double det3(mat3 m)
        {
            double res = m.matrix[0, 0] * (m.matrix[1, 1] * m.matrix[2, 2] - m.matrix[1, 2] * m.matrix[2, 1])
                         - m.matrix[0, 1] * (m.matrix[1, 0] * m.matrix[2, 2] - m.matrix[1, 2] * m.matrix[2, 0])
                         + m.matrix[0, 2] * (m.matrix[1, 0] * m.matrix[2, 1] - m.matrix[1, 1] * m.matrix[2, 0]);

            return(res);
        }
Ejemplo n.º 11
0
        public override void UpdatePos(vec2 pos)
        {
            base.UpdatePos(pos);
            float r = R;

            _translate   = Transforms.Translate(pos.x, pos.y);
            VertexStrPos = new vec2(VertexStr.Length == 1 ? pos.x - r / 2f + 2f : pos.x - r + 6f, pos.y - r / 2f);
            Lines        = _circle.Lines.Select(p => (vec2)(_translate * p)).ToArray();
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Applies a scale transformation to matrix <paramref name="m"/> by vector <paramref name="v"/>.
        /// </summary>
        /// <param name="m">The matrix to transform.</param>
        /// <param name="v">The vector to scale by.</param>
        /// <returns><paramref name="m"/> scaled by <paramref name="v"/>.</returns>
        public static mat3 scale(mat3 m, vec2 v)
        {
            mat3 result = m;
            result.col0 = m.col0 * v.x;
            result.col1 = m.col1 * v.y;
            result.col2 = m.col2;

            return result;
        }
Ejemplo n.º 13
0
        internal override void SetValue(ValueType value)
        {
            if (value.GetType() != typeof(mat3))
            {
                throw new ArgumentException(string.Format("[{0}] not match [{1}]'s value.",
                                                          value.GetType().Name, this.GetType().Name));
            }

            this.Value = (mat3)value;
        }
Ejemplo n.º 14
0
        private static void rotateXalignment()
        {
            mat3 _rotationMatrix = Calculation.calculateRotationMatrix(cubeXaxis, cubeRotationAngle);
            mat4 _transformation = Calculation.calculateTransformationMatrix(new vec4(0), _rotationMatrix, new vec3(1));

            foreach (var _o in groupXalignmentLeft)
            {
                _o.updateObject(_transformation);
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Creates the modelview and normal matrix. Also rotates the sceen by a specified amount.
 /// </summary>
 /// <param name="rotationAngle">The rotation angle, in radians.</param>
 public void CreateModelviewAndNormalMatrix(float rotationAngle)
 {
     //  Create the modelview and normal matrix. We'll also rotate the scene
     //  by the provided rotation angle, which means things that draw it
     //  can make the scene rotate easily.
     mat4 rotation = glm.rotate(mat4.identity(), rotationAngle, new vec3(0, 1, 0));
     mat4 translation = glm.translate(mat4.identity(), new vec3(0, 0, -4));
     modelviewMatrix = rotation * translation;
     normalMatrix = modelviewMatrix.to_mat3();
 }
Ejemplo n.º 16
0
        public override void SetDeclarations(ref ShaderVariables shaderVars)
        {
            Matrix3 rot = new Matrix3();

            rot.MakeFromRPY(_Rotation.x, _Rotation.y, _Rotation.z);
            mat3 glRot = rot.GetGLMat3();

            shaderVars.Add("fracScale" + _Iteration, _Scale);
            shaderVars.Add("fracRot" + _Iteration, glRot);
            shaderVars.Add("fracMinRad" + _Iteration, (float)_MinRadius);
        }
Ejemplo n.º 17
0
        public GenObject(string name)
        {
            IsSelectable = true;
            IsVisible    = true;
            IsRenderable = true;
            Name         = name;
            IsSelected   = false;

            TransformationMatrix  = new mat4(1.0f);
            NormalTransformMatrix = new mat3(1.0f);
        }
Ejemplo n.º 18
0
        vec3 PerturbNormal(vec3 N, vec3 V, vec2 texcoord)
        {
            // N, la normale interpolée et
            // V, le vecteur vue (vertex dirigé vers l'œil)
            vec3 map = texture(materialState.textureNormal, texcoord).rgb;

            map = map * 255f / 127f - 128f / 127f;
            mat3 TBN = CotangentFrame(N, -V, texcoord);

            return(normalize(TBN * map));
        }
Ejemplo n.º 19
0
 public void Set(string name, mat3 val, bool transpose = false, bool keep_layout = true)
 {
     if (Values.TryGetValue(name, out var v))
     {
         v.Set(val, transpose, keep_layout);
     }
     else
     {
         Values.Add(name, new Value(val, transpose)); Keys.AddLast(name);
     }
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Gets the transposed matrix.
        /// <para>获取转置矩阵。</para>
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static mat3 transpose(mat3 m)
        {
            vec3 col0 = m.col0;
            vec3 col1 = m.col1;
            vec3 col2 = m.col2;

            return(new mat3(
                       new vec3(col0.x, col1.x, col2.x),
                       new vec3(col0.y, col1.y, col2.y),
                       new vec3(col0.z, col1.z, col2.z)
                       ));
        }
Ejemplo n.º 21
0
Archivo: vmath.cs Proyecto: maihd/vmath
 public DebuggerProxy(mat3 m)
 {
     m00 = m.m00;
     m01 = m.m01;
     m02 = m.m02;
     m10 = m.m10;
     m11 = m.m11;
     m12 = m.m12;
     m20 = m.m20;
     m21 = m.m21;
     m22 = m.m22;
 }
Ejemplo n.º 22
0
        private void UpdateInertia()
        {
            var t = Parent.Transform;

            t.Translation = vec3.Zero;
            var worldspaceMat = new mat3()
            {
                Row0 = t * vec3.UnitX, Row1 = t * vec3.UnitY, Row2 = t * vec3.UnitZ
            };

            InverseInertiaWorldspace = worldspaceMat.Transposed * InverseInertia * worldspaceMat;
        }
Ejemplo n.º 23
0
 public static float[] serialize(this mat3 value, float[] array = null)
 {
     array = array ?? new float[9];
     if (array.Length != 9)
     {
         throw new ArgumentException("Invalid array length.");
     }
     for (var c = 0; c < 9; ++c)
     {
         array[c] = value[c / 3, c % 3];
     }
     return(array);
 }
Ejemplo n.º 24
0
        public static mat3 Transpose(ref mat3 m)
        {
            mat3 trans = new mat3(1.0f);

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    trans[j, i] = m[i, j];
                }
            }
            return(trans);
        }
Ejemplo n.º 25
0
        public void DetermineAdjugateComponents(double[] a, double[] result)
        {
            var matA     = new mat3(a);
            var expected = new mat3(result);

            for (int r = 0; r < 3; r++)
            {
                for (int c = 0; c < 3; c++)
                {
                    matA.AdjugateComponent(r, c).Should().BeApproximately(expected[r, c], double.Epsilon);
                }
            }
        }
Ejemplo n.º 26
0
        /// Builds a rotation 3 * 3 matrix created from an angle.
        /// </summary>
        /// <param name="m">The m.</param>
        /// <param name="angleDegree">ANgle in Degree.</param>
        /// <returns></returns>
        public static mat3 rotate(mat3 m, float angleDegree)
        {
            float c = (float)Math.Cos(angleDegree * Math.PI / 180.0);
            float s = (float)Math.Sin(angleDegree * Math.PI / 180.0);

            mat3 rotate = mat3.identity();
            rotate.col0 = new vec2(c, s);
            rotate.col1 = new vec2(-s, c);

            mat3 result = rotate * m;

            return result;
        }
Ejemplo n.º 27
0
        public static unsafe void glusRaytraceLookAtf(this float[] positionBuffer, float[] directionBuffer, float[] originDirectionBuffer, byte padding, int width, int height, float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
        {
            //float forward[3], side[3], up[3];
            //float rotation[9];
            vec3 forward, side, up;

            float[] rotation;
            int     i, k;

            forward = new vec3(centerX - eyeX, centerY - eyeY, centerZ - eyeZ);
            forward = forward.normalize();

            up = new vec3(upX, upY, upZ);

            side = forward.cross(up);
            side = side.normalize();

            up = side.cross(forward);

            mat3 matrix = new mat3(side, up, -forward);

            rotation = matrix.ToArray();

            for (i = 0; i < width * height; i++)
            {
                positionBuffer[i * 4 + 0] = eyeX;
                positionBuffer[i * 4 + 1] = eyeY;
                positionBuffer[i * 4 + 2] = eyeZ;
                positionBuffer[i * 4 + 3] = 1.0f;

                //glusMatrix3x3MultiplyVector3f(&directionBuffer[i * (3 + padding)], rotation, &originDirectionBuffer[i * (3 + padding)]);
                float[] result = new float[3];
                result[0] = directionBuffer[i * (3 + padding) + 0];
                result[1] = directionBuffer[i * (3 + padding) + 1];
                result[2] = directionBuffer[i * (3 + padding) + 2];
                float[] vector = new float[3];
                vector[0] = originDirectionBuffer[i * (3 + padding) + 0];
                vector[1] = originDirectionBuffer[i * (3 + padding) + 1];
                vector[2] = originDirectionBuffer[i * (3 + padding) + 2];
                glusMatrix3x3MultiplyVector3f(result, rotation, vector);
                directionBuffer[i * (3 + padding) + 0] = result[0];
                directionBuffer[i * (3 + padding) + 1] = result[1];
                directionBuffer[i * (3 + padding) + 2] = result[2];

                for (k = 0; k < padding; k++)
                {
                    directionBuffer[i * (3 + padding) + 3 + k] = originDirectionBuffer[i * (3 + padding) + 3 + k];
                }
            }
        }
Ejemplo n.º 28
0
        /// <summary>
        /// </summary>
        /// <param name="uniformName"></param>
        /// <param name="m"></param>
        public int glUniform(string uniformName, mat3 m)
        {
            int location = GetUniformLocation(uniformName);

            if (location >= 0)
            {
                if (glUniformMatrix3fv == null)
                {
                    glUniformMatrix3fv = OpenGL.GetDelegateFor <OpenGL.glUniformMatrix3fv>();
                }
                float[] array = m.ToArray();
                glUniformMatrix3fv(location, 1, false, array);
            }
            return(location);
        }
Ejemplo n.º 29
0
        public mat3 GetGLMat3()
        {
            mat3 ret = new mat3(1);

            ret[0, 0] = (float)_Component[0][0];
            ret[0, 1] = (float)_Component[0][1];
            ret[0, 2] = (float)_Component[0][2];
            ret[1, 0] = (float)_Component[1][0];
            ret[1, 1] = (float)_Component[1][1];
            ret[1, 2] = (float)_Component[1][2];
            ret[2, 0] = (float)_Component[2][0];
            ret[2, 1] = (float)_Component[2][1];
            ret[2, 2] = (float)_Component[2][2];
            return(ret);
        }
Ejemplo n.º 30
0
        public RigidBody(Entity parent, string collision)
        {
            Parent    = parent;
            Collision = Model.LoadCollisionMesh(collision);
            AABB      = new BoundingBox(vec3.Zero, vec3.Zero);
            //aabbMesh = Model.LoadCollisionMesh(collision);
            UpdateAABB();

            float tr = 1f / 6f * Parent.Transform.Scale.x;
            float of = 0;

            Inertia                  = new mat3(tr, of, of, of, tr, of, of, of, tr);
            InverseInertia           = Inertia.Inverse;
            InverseInertiaWorldspace = InverseInertia;
        }
Ejemplo n.º 31
0
        public void ClipmapFragmentMain()
        {
            var light = Normalize(new vec3(0.0f, 1.0f, 0.6f));
            var n     = Normalize(Normal);


            var n2 = Normalize(Cross(DeriveTowardsX(_finalPos.xyz), DeriveTowardsY(_finalPos.xyz)));

            n2.xy = -n2.xy;
            var light2 = new mat3(NormalMatrix) * (light);

            var i = Max(Dot(light2, n2), 0.0f) * 0.8f + 0.2f; // Dot(light2, n2);

            FragColor = Color * i;
        }
        /// <summary>
        /// </summary>
        /// <param name="uniformName"></param>
        /// <param name="m"></param>
        internal int glUniform(string uniformName, mat3 m)
        {
            int location = GetUniformLocation(uniformName);

            if (location >= 0)
            {
                if (glUniformMatrix3fv == null)
                {
                    glUniformMatrix3fv = GL.Instance.GetDelegateFor("glUniformMatrix3fv", GLDelegates.typeof_void_int_int_bool_floatN) as GLDelegates.void_int_int_bool_floatN;
                }
                float[] array = m.ToArray();
                glUniformMatrix3fv(location, 1, false, array);
            }
            return(location);
        }
Ejemplo n.º 33
0
        private static void loadRoom(ref SceneManager sManager)
        {
            var            triangleList         = Parser.getTriangleListFromFile(Constants.Filepath_objectdata, "Room");
            vec4           position             = new vec4(0, 30, 0, 1);
            vec3           scale                = new vec3(3, 3, 3);
            vec4           rotationAxis         = new vec4(0, 1, 0, 1);
            float          angle                = 0;
            mat3           rotationMatrix       = Calculation.calculateRotationMatrix(rotationAxis, angle);
            var            transformationMatrix = Calculation.calculateTransformationMatrix(position, rotationMatrix, scale);
            TriangleObject room = new TriangleObject(objID, transformationMatrix, triangleList);

            objID++;

            sManager.addContent(room);
        }
Ejemplo n.º 34
0
        public void CanMultiplyByVector()
        {
            var m = new mat3(new[]
            {
                new vec3(1f, 2f, 3f),
                new vec3(4f, 5f, 6f),
                new vec3(7f, 8f, 9f),
            });
            var v = new vec3(10f, 11f, 12f);

            //  1  4  7   10   10 + 44 + 84    138
            //  2  5  8 * 11 = 20 + 55 + 96  = 171
            //  3  6  9   12   30 + 66 + 108   204

            Assert.AreEqual(new vec3(138f, 171f, 204f), m * v, "Rank 2 Matrix * Vector results are incorrect.");
        }
Ejemplo n.º 35
0
        public override void SetDeclarations(ref ShaderVariables shaderVars)
        {
            Matrix3 preRot = new Matrix3();

            preRot.MakeFromRPY(_PreRotation.x, _PreRotation.y, _PreRotation.z);
            mat3    glPreRot = preRot.GetGLMat3();
            Matrix3 postRot  = new Matrix3();

            postRot.MakeFromRPY(_PostRotation.x, _PostRotation.y, _PostRotation.z);
            mat3 glPostRot = postRot.GetGLMat3();

            shaderVars.Add("fracScale" + _Iteration, (float)_Scale);
            shaderVars.Add("fracOffset" + _Iteration, _Offset);
            shaderVars.Add("fracPreRot" + _Iteration, glPreRot);
            shaderVars.Add("fracPostRot" + _Iteration, glPostRot);
        }
Ejemplo n.º 36
0
        public bool GetUniformMat3ArrayValue(string varNameInShader, out mat3[] value)
        {
            value = null;
            bool gotUniform = false;

            foreach (var item in this.uniformVariables)
            {
                if (item.VarName == varNameInShader)
                {
                    value = (item as UniformMat3Array).Value;
                    gotUniform = true;
                    break;
                }
            }

            return gotUniform;
        }
Ejemplo n.º 37
0
        /// <summary>
        /// Gets the inversed matrix.
        /// <para>获取逆矩阵。</para>
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static mat3 inverse(mat3 m)
        {
            float OneOverDeterminant = (1f) / (
                +m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])
                - m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2])
                + m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]));

            mat3 Inverse = new mat3(0);
            Inverse[0, 0] = +(m[1][1] * m[2][2] - m[2][1] * m[1][2]) * OneOverDeterminant;
            Inverse[1, 0] = -(m[1][0] * m[2][2] - m[2][0] * m[1][2]) * OneOverDeterminant;
            Inverse[2, 0] = +(m[1][0] * m[2][1] - m[2][0] * m[1][1]) * OneOverDeterminant;
            Inverse[0, 1] = -(m[0][1] * m[2][2] - m[2][1] * m[0][2]) * OneOverDeterminant;
            Inverse[1, 1] = +(m[0][0] * m[2][2] - m[2][0] * m[0][2]) * OneOverDeterminant;
            Inverse[2, 1] = -(m[0][0] * m[2][1] - m[2][0] * m[0][1]) * OneOverDeterminant;
            Inverse[0, 2] = +(m[0][1] * m[1][2] - m[1][1] * m[0][2]) * OneOverDeterminant;
            Inverse[1, 2] = -(m[0][0] * m[1][2] - m[1][0] * m[0][2]) * OneOverDeterminant;
            Inverse[2, 2] = +(m[0][0] * m[1][1] - m[1][0] * m[0][1]) * OneOverDeterminant;

            return Inverse;
        }
Ejemplo n.º 38
0
        /// <summary>
        /// Build a look at view matrix.
        /// transform object's coordinate from world's space to camera's space.
        /// </summary>
        /// <param name="eye">The eye.</param>
        /// <param name="center">The center.</param>
        /// <param name="up">Up.</param>
        /// <returns></returns>
        public static mat3 lookAt(vec2 eye, vec2 center, bool up)
        {
            // camera's back in world space coordinate system
            vec2 back = (eye - center).normalize();
            // camera's right in world space coordinate system
            vec2 right = up.cross(back).normalize();
            if (!up) { right = -right; }

            mat3 viewMatrix = new mat3(1);
            viewMatrix.col0.x = right.x;
            viewMatrix.col1.x = right.y;
            viewMatrix.col0.y = back.x;
            viewMatrix.col1.y = back.y;

            // Translation in world space coordinate system
            viewMatrix.col3.x = -eye.dot(right);
            viewMatrix.col3.y = -eye.dot(back);

            return viewMatrix;
        }
Ejemplo n.º 39
0
        public static mat4 RotateMatrix(float angle, vec3 axis)
        {
            vec3 n = normalize(axis);
            float theta = radians(angle);
            float c = cos(theta);
            float s = sin(theta);
            mat3 R = new mat3();

            R[0] = n * n.x * (1.0f - c) + new vec3(c, n.z * s, -n.y * s);
            R[1] = n * n.y * (1.0f - c) + new vec3(-n.z * s, c, n.x * s);
            R[2] = n * n.z * (1.0f - c) + new vec3(n.y * s, -n.x * s, c);

            mat4 rValue = new mat4(
                new vec4(R[0], 0.0f),
                new vec4(R[1], 0.0f),
                new vec4(R[2], 0.0f),
                new vec4(0, 0, 0, 1));

            return rValue;
        }
Ejemplo n.º 40
0
        public void MatrixIsColumnMajor()
        {
            //  A pretty critical test as this is fundamental.
            var m = new mat3(new [] {
                new vec3(1f, 2f, 3f),
                new vec3(4f, 5f, 6f),
                new vec3(7f, 8f, 9f)
            });

            //  1 4 7
            //  2 5 8
            //  3 6 9

            Assert.AreEqual(1f, m[0][0], "Matrix does not implement column major semantics");
            Assert.AreEqual(2f, m[0][1], "Matrix does not implement column major semantics");
            Assert.AreEqual(3f, m[0][2], "Matrix does not implement column major semantics");
            Assert.AreEqual(4f, m[1][0], "Matrix does not implement column major semantics");
            Assert.AreEqual(5f, m[1][1], "Matrix does not implement column major semantics");
            Assert.AreEqual(6f, m[1][2], "Matrix does not implement column major semantics");
            Assert.AreEqual(7f, m[2][0], "Matrix does not implement column major semantics");
            Assert.AreEqual(8f, m[2][1], "Matrix does not implement column major semantics");
            Assert.AreEqual(9f, m[2][2], "Matrix does not implement column major semantics");

            Assert.AreEqual(1f, m[0, 0], "Matrix does not implement column major semantics");
            Assert.AreEqual(2f, m[0, 1], "Matrix does not implement column major semantics");
            Assert.AreEqual(3f, m[0, 2], "Matrix does not implement column major semantics");
            Assert.AreEqual(4f, m[1, 0], "Matrix does not implement column major semantics");
            Assert.AreEqual(5f, m[1, 1], "Matrix does not implement column major semantics");
            Assert.AreEqual(6f, m[1, 2], "Matrix does not implement column major semantics");
            Assert.AreEqual(7f, m[2, 0], "Matrix does not implement column major semantics");
            Assert.AreEqual(8f, m[2, 1], "Matrix does not implement column major semantics");
            Assert.AreEqual(9f, m[2, 2], "Matrix does not implement column major semantics");

            Assert.AreEqual(new vec3(1f, 2f, 3f), m[0], "Matrix does not implement column major semantics");
            Assert.AreEqual(new vec3(4f, 5f, 6f), m[1], "Matrix does not implement column major semantics");
            Assert.AreEqual(new vec3(7f, 8f, 9f), m[2], "Matrix does not implement column major semantics");
        }
 /// <summary>
 /// Multiply matrix x by matrix y component-wise, i.e., result[i][j] is the scalar product of x[i][j] and y[i][j].
 /// Note: to get linear algebraic matrix multiplication, use the multiply operator (*).
 /// </summary>
 protected mat3 matrixCompMult(mat3 x, mat3 y)
 {
     throw _invalidAccess;
 }
 /// <summary>
 /// Simplify the creation of the normal matrix by using a modelView.
 /// </summary>
 /// <param name="modelView"></param>
 public void CreateFromModelView(ModelView modelView)
 {
     _normalMatrix = modelView.ModelviewMatrix.to_mat3();
 }
Ejemplo n.º 43
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="varNameInShader"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool GetUniformMat3ArrayValue(string varNameInShader, out mat3[] value)
        {
            //if ((!this.Initialized) && (!this.Initializing)) { this.Initialize(); }

            value = null;
            bool gotUniform = false;

            foreach (UniformVariable item in this.uniformVariables)
            {
                if (item.VarName == varNameInShader)
                {
                    value = (item as UniformMat3Array).Value.Array;
                    gotUniform = true;
                    break;
                }
            }

            return gotUniform;
        }
Ejemplo n.º 44
0
 /// <summary>
 /// </summary>
 /// <param name="uniformName"></param>
 /// <param name="m"></param>
 public int SetUniformMatrix3(string uniformName, mat3[] m)
 {
     int location = GetUniformLocation(uniformName);
     if (location >= 0)
     {
         if (glUniformMatrix3fv == null) { glUniformMatrix3fv = OpenGL.GetDelegateFor<OpenGL.glUniformMatrix3fv>(); }
         var values = new float[m.Length * 9];
         for (int index = 0, i = 0; i < m.Length; i++)
         {
             float[] array = m[i].ToArray();
             for (int j = 0; j < 9; j++)
             {
                 values[index++] = array[j];
             }
         }
         glUniformMatrix3fv(location, m.Length / 9, false, values);
     }
     return location;
 }
Ejemplo n.º 45
0
 /// <summary>initialized the matrix with the upperleft part of m
 /// sets the lower right diagonal component(s) to 1, everything else to 0</summary>
 public mat3x4(mat3 m)
 {
     throw _invalidAccess;
 }
Ejemplo n.º 46
0
 /// <summary>
 /// Gets the transposed matrix.
 /// <para>获取转置矩阵。</para>
 /// </summary>
 /// <param name="m"></param>
 /// <returns></returns>
 public static mat3 transpose(mat3 m)
 {
     vec3 col0 = m.col0;
     vec3 col1 = m.col1;
     vec3 col2 = m.col2;
     return new mat3(
         new vec3(col0.x, col1.x, col2.x),
         new vec3(col0.y, col1.y, col2.y),
         new vec3(col0.z, col1.z, col2.z)
         );
 }
Ejemplo n.º 47
0
 /// <summary>
 /// Applies a translation transformation to matrix <paramref name="m"/> by vector <paramref name="v"/>.
 /// </summary>
 /// <param name="m">The matrix to transform.</param>
 /// <param name="v">The vector to translate by.</param>
 /// <returns><paramref name="m"/> translated by <paramref name="v"/>.</returns>
 public static mat3 translate(mat3 m, vec2 v)
 {
     mat3 result = m;
     result.col2 = m.col0 * v.x + m.col1 * v.y + m.col2;
     return result;
 }
Ejemplo n.º 48
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="varNameInShader"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SetUniform(string varNameInShader, mat3[] value)
        {
            //if ((!this.Initialized) && (!this.Initializing)) { this.Initialize(); }

            bool gotUniform = false;
            bool updated = false;
            if (value.Length <= 0) { return updated; }

            foreach (UniformVariable item in this.uniformVariables)
            {
                if (item.VarName == varNameInShader)
                {
                    (item as UniformMat3Array).Value = new NoisyArray<mat3>(value);
                    updated = true;
                    gotUniform = true;
                    break;
                }
            }

            if (!gotUniform)
            {
                //int location = Program.GetUniformLocation(varNameInShader);
                //if (location < 0)
                {
                    //throw new Exception(string.Format(
                    //"niform variable [{0}] not exists! Remember to invoke RendererBase.Initialize(); before this method.", varNameInShader));
                }

                var variable = GetVariableArray(value, varNameInShader) as UniformMat3Array;
                variable.Value = new NoisyArray<mat3>(value);
                this.uniformVariables.Add(variable);
                updated = true;
            }

            return updated;
        }
Ejemplo n.º 49
0
        public bool SetUniform(string varNameInShader, mat3[] value)
        {
            bool gotUniform = false;
            bool updated = false;
            if (value.Length <= 0) { return updated; }

            foreach (var item in this.uniformVariables)
            {
                if (item.VarName == varNameInShader)
                {
                    (item as UniformMat3Array).Value = value;
                    updated = true;
                    gotUniform = true;
                    break;
                }
            }

            if (!gotUniform)
            {
                int location = ShaderProgram.GetUniformLocation(varNameInShader);
                if (location < 0)
                {
                    throw new Exception(string.Format(
                        "uniform variable [{0}] not exists!", varNameInShader));
                }

                var variable = GetVariableArray(value, varNameInShader) as UniformMat3Array;
                variable.Value = value;
                this.uniformVariables.Add(variable);
                updated = true;
            }

            return updated;
        }
Ejemplo n.º 50
0
        public void ClipmapFragmentMain()
        {
            var light = Normalize(new vec3(0.0f, 1.0f, 0.6f));
            var n = Normalize(Normal);

            var n2 = Normalize(Cross(DeriveTowardsX(_finalPos.xyz), DeriveTowardsY(_finalPos.xyz)));
            n2.xy = -n2.xy;
            var light2 = new mat3(NormalMatrix) * (light);

            var i = Max(Dot(light2, n2), 0.0f) * 0.8f + 0.2f; // Dot(light2, n2);

            FragColor = Color * i;
        }
Ejemplo n.º 51
0
 /// <summary>initialized the matrix with the upperleft part of m
 /// sets the lower right diagonal component(s) to 1, everything else to 0</summary>
 public mat4x3(mat3 m)
 {
     throw _invalidAccess;
 }
 /// <summary>
 /// Returns a matrix that is the transpose of m. 
 /// The input matrix m is not modified.
 /// </summary>
 protected mat3 transpose(mat3 m)
 {
     throw _invalidAccess;
 }
 /// <summary>Returns the determinant of m. </summary>
 protected float determinant(mat3 m)
 {
     throw _invalidAccess;
 }