Esempio n. 1
0
 static Float3x3()
 {
     Identity             = new Float3x3();
     Identity.coeff[0][0] = 1;
     Identity.coeff[1][1] = 1;
     Identity.coeff[2][2] = 1;
 }
Esempio n. 2
0
        public Float3x3 Clone()
        {
            var r = new Float3x3();

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    r.coeff[i][j] = coeff[i][j];
                }
            }
            return(r);
        }
Esempio n. 3
0
 public Float4x4(Float3x3 f)
 {
     this.M11 = f.M11;
     this.M12 = f.M12;
     this.M13 = f.M13;
     this.M14 = 0.0f;
     this.M21 = f.M21;
     this.M22 = f.M22;
     this.M23 = f.M23;
     this.M24 = 0.0f;
     this.M31 = f.M31;
     this.M32 = f.M32;
     this.M33 = f.M33;
     this.M34 = 0.0f;
     this.M41 = 0.0f;
     this.M42 = 0.0f;
     this.M43 = 0.0f;
     this.M44 = 1.0f;
 }
Esempio n. 4
0
        public Float3x3 Invert()
        {
            var inv    = new Float3x3();
            var matrix = this;

            var det = Determinant();

            if (det.EpsilonEquals(0, 1e-5f))
            {
                det = 1.0f / det;
            }

            inv.coeff[0][0] = (matrix.coeff[1][1] * matrix.coeff[2][2] -
                               matrix.coeff[1][2] * matrix.coeff[2][1]) * det;

            inv.coeff[1][0] = -(matrix.coeff[1][0] * matrix.coeff[2][2] -
                                matrix.coeff[1][2] * matrix.coeff[2][0]) * det;

            inv.coeff[2][0] = (matrix.coeff[1][0] * matrix.coeff[2][1] -
                               matrix.coeff[1][1] * matrix.coeff[2][0]) * det;

            inv.coeff[0][1] = -(matrix.coeff[0][1] * matrix.coeff[2][2] -
                                matrix.coeff[0][2] * matrix.coeff[2][1]) * det;

            inv.coeff[1][1] = (matrix.coeff[0][0] * matrix.coeff[2][2] -
                               matrix.coeff[0][2] * matrix.coeff[2][0]) * det;

            inv.coeff[2][1] = -(matrix.coeff[0][0] * matrix.coeff[2][1] -
                                matrix.coeff[0][1] * matrix.coeff[2][0]) * det;

            inv.coeff[0][2] = (matrix.coeff[0][1] * matrix.coeff[1][2] -
                               matrix.coeff[0][2] * matrix.coeff[1][1]) * det;

            inv.coeff[1][2] = -(matrix.coeff[0][0] * matrix.coeff[1][2] -
                                matrix.coeff[0][2] * matrix.coeff[1][0]) * det;

            inv.coeff[2][2] = (matrix.coeff[0][0] * matrix.coeff[1][1] -
                               matrix.coeff[0][1] * matrix.coeff[1][0]) * det;

            return(inv);
        }
Esempio n. 5
0
        public Float3x3 Multiply(Float3x3 other)
        {
            var tmp = new Float3x3();

            var matrix1 = this;
            var matrix2 = other;

            for (var i = 0; i < 3; i++)
            {
                var t1 = matrix1.coeff[i][0];
                var t2 = matrix1.coeff[i][1];
                var t3 = matrix1.coeff[i][2];

                for (var j = 0; j < 3; j++)
                {
                    tmp.coeff[i][j]  = t1 * matrix2.coeff[0][j];
                    tmp.coeff[i][j] += t2 * matrix2.coeff[1][j];
                    tmp.coeff[i][j] += t3 * matrix2.coeff[2][j];
                }
            }
            return(tmp);
        }
Esempio n. 6
0
        public Bitmap Draw(Mesh mesh, int screenWidth, int screenHeight, params object[] uniforms)
        {
            float    scaleFactor = 300;
            Float3x3 S           = Float3x3.identity * scaleFactor;
            Float3x3 R           = Float3x3.getRotationMatrix(0, 0, 0);

            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                Float3 scaled = mesh.Vertices[i].mul(S);
                scaled = scaled.mul(R);

                mesh.Vertices[i] = new Float3(scaled.x + WIDTH / 2, scaled.y + HEIGHT / 2, scaled.z);
            }

            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                Float3 v = mesh.Vertices[i];
                screen.elDrawPoint(v.x, v.y, color);
            }

            int c = mesh.Triangles.Count;

            for (int i = 0; i < c; i++)
            {
                Triangle t = mesh.Triangles[i];

                Float3 v1 = mesh.Vertices[t[0] - 1];
                Float3 v2 = mesh.Vertices[t[1] - 1];
                Float3 v3 = mesh.Vertices[t[2] - 1];

                screen.elDrawTriangle(v1, v2, v3, getRandomColor());
                //screen.elDrawLine(v1.x, v1.y, v2.x, v2.y, color);
                //screen.elDrawLine(v2.x, v2.y, v3.x, v3.y, color);
                //screen.elDrawLine(v3.x, v3.y, v1.x, v1.y, color);
            }
            return(null);
        }
Esempio n. 7
0
        public void Render(SceneObject sObject, Float3 viewDirection, Float3 lightDirection, bool useProjection = true)
        {
            Mesh       mesh           = sObject.mesh;
            Color      wireFrameColor = Color.LightGreen;
            RenderType renderType     = sObject.material.renderType;

            // Vertex uniforms

            // scale matrix
            Float3x3 S = Float3x3.identity * sObject.uniformScale;
            // rotation matrix
            Float3x3 R = Float3x3.getRotationMatrix(sObject.rotation);
            Float3x3 CombinedLinear = S * R;
            // translation
            Float4x4 Tr = Float4x4.identity;

            Tr.setTranslation(sObject.localPosition);
            // projection
            Float4x4 Pr = useProjection ? Float4x4.getProjectionMatrix(10f, 1300f, 1f, 1f) : Float4x4.identity;

            // BACK FACE CULLING
            if (backFaceCulling)
            {
                for (int i = mesh.Triangles.Count - 1; i >= 0; i--)
                {
                    Triangle t = mesh.Triangles[i];

                    Float3 v1     = mesh.Vertices[t[0] - 1].position;
                    Float3 v2     = mesh.Vertices[t[1] - 1].position;
                    Float3 v3     = mesh.Vertices[t[2] - 1].position;
                    Float3 normal = Utils.getTriangleNormalR(v1, v2, v3);

                    // remove faced back triangles
                    if (viewDirection.dot(normal) >= 0)
                    {
                        mesh.Triangles.Remove(t);
                    }
                }
            }
            // VERTEX SHADER
            for (int i = 0; i < mesh.Vertices.Count; i++)
            {
                Vertex v = mesh.Vertices[i];
                // scale
                var p = v.position.mul(S);
                // rotate
                p = p.mul(R);
                // translate
                p = Tr.transformPoint(p);
                // project
                if (useProjection)
                {
                    p = Pr.transformPoint(p);
                }
                // TODO: Transforming normals while NON UNIFORM TRANSFORMS
                v.normal = v.normal.mul(R);

                // TODO: place to center of screen
                if (useProjection)
                {
                    v.position = new Float3(p.x * Defaults.WIDTH + Defaults.WIDTH / 2f, p.y * Defaults.HEIGHT + Defaults.HEIGHT / 2f, p.z);
                }
                else
                {
                    v.position = new Float3(p.x + Defaults.WIDTH / 2f, p.y + Defaults.HEIGHT / 2f, p.z);
                }
            }

            if ((renderType & RenderType.Regular) != 0)
            {
                RenderRegular(mesh, sObject.material, lightDirection);
            }

            if ((renderType & RenderType.Wireframe) != 0)
            {
                RenderWireframe(mesh, wireFrameColor);
            }

            if ((renderType & RenderType.Normals) != 0)
            {
                DrawVertexNormals(mesh, Color.Red);
            }
        }
Esempio n. 8
0
 public static void Clip(Float3x3 x) => throw new InvalidExecutionContextException($"{typeof(Hlsl)}.{nameof(Clip)}({typeof(Float3x3)})");
Esempio n. 9
0
File: GL.cs Progetto: mortend/uno
 public void UniformMatrix3(int location, bool transpose, Float3x3 value)
 {
     TKGL.UniformMatrix3(location, 1, transpose, ref value.M11);
 }
Esempio n. 10
0
        public static Float3x3 Perspective(float x, float y, float width, float height, Float2 t1, Float2 t2, Float2 t3, Float2 t4)
        {
            var scalex = 1.0f;
            var scaley = 1.0f;

            if (width > 0)
            {
                scalex = 1.0f / width;
            }

            if (height > 0)
            {
                scaley = 1.0f / height;
            }

            var matrix = Float3x3.Identity;

            matrix = matrix.Translate(-x, -y);
            matrix = matrix.Scale(scalex, scaley);

            var trafo = new Float3x3();

            {
                var t_x1 = t1.X;
                var t_y1 = t1.Y;
                var t_x2 = t2.X;
                var t_y2 = t2.Y;
                var t_x3 = t3.X;
                var t_y3 = t3.Y;
                var t_x4 = t4.X;
                var t_y4 = t4.Y;
                var dx1  = t_x2 - t_x4;
                var dx2  = t_x3 - t_x4;
                var dx3  = t_x1 - t_x2 + t_x4 - t_x3;
                var dy1  = t_y2 - t_y4;
                var dy2  = t_y3 - t_y4;
                var dy3  = t_y1 - t_y2 + t_y4 - t_y3;
                /*  Is the mapping affine?  */
                var epsilon = 1e-4f;
                if ((dx3.EpsilonEquals(0, epsilon)) && (dy3.EpsilonEquals(0, epsilon)))
                {
                    trafo.coeff[0][0] = t_x2 - t_x1;
                    trafo.coeff[0][1] = t_x4 - t_x2;
                    trafo.coeff[0][2] = t_x1;
                    trafo.coeff[1][0] = t_y2 - t_y1;
                    trafo.coeff[1][1] = t_y4 - t_y2;
                    trafo.coeff[1][2] = t_y1;
                    trafo.coeff[2][0] = 0.0f;
                    trafo.coeff[2][1] = 0.0f;
                }
                else
                {
                    var det1 = dx3 * dy2 - dy3 * dx2;
                    var det2 = dx1 * dy2 - dy1 * dx2;

                    trafo.coeff[2][0] = det2.EpsilonEquals(0, epsilon) ? 1.0f : det1 / det2;

                    det1 = dx1 * dy3 - dy1 * dx3;

                    trafo.coeff[2][1] = det2.EpsilonEquals(0, epsilon) ? 1.0f : det1 / det2;

                    trafo.coeff[0][0] = t_x2 - t_x1 + trafo.coeff[2][0] * t_x2;
                    trafo.coeff[0][1] = t_x3 - t_x1 + trafo.coeff[2][1] * t_x3;
                    trafo.coeff[0][2] = t_x1;

                    trafo.coeff[1][0] = t_y2 - t_y1 + trafo.coeff[2][0] * t_y2;
                    trafo.coeff[1][1] = t_y3 - t_y1 + trafo.coeff[2][1] * t_y3;
                    trafo.coeff[1][2] = t_y1;
                }
                trafo.coeff[2][2] = 1.0f;
            }

            return(trafo.Multiply(matrix));
        }
Esempio n. 11
0
 public void UniformMatrix3(int location, bool transpose, Float3x3 value)
 {
 }