Example #1
0
 public PointLight(Float3 position, Float3 ambient, Float3 diffuse, Float3 specular, float shininess)
     : base(position, ambient, diffuse, specular, shininess)
 {
 }
Example #2
0
        public static void MathTest()
        {
            Random rand = new Random();

            float x = (float)rand.NextDouble();
            float y = (float)rand.NextDouble();
            float z = (float)rand.NextDouble();
            float w = (float)rand.NextDouble();

            float v = (float)rand.NextDouble();

            Float3   fpuF3  = new(x, y, z);
            Float4   fpuF4  = new(x, y, z, w);
            Float3   fpuF31 = fpuF3 * v;
            Float4   fpuF41 = fpuF4 * v;
            Float4x4 fpuF44 = new(x, y, z, w,
                                  x, y, z, w,
                                  x, y, z, w,
                                  x, y, z, w);

            SseMath.Float3   sseF3  = new(x, y, z);
            SseMath.Float4   sseF4  = new(x, y, z, w);
            SseMath.Float3   sseF31 = sseF3 * v;
            SseMath.Float4   sseF41 = sseF4 * v;
            SseMath.Float4x4 sseF44 = new(x, y, z, w,
                                          x, y, z, w,
                                          x, y, z, w,
                                          x, y, z, w);

            Console.WriteLine($"Add VV FPU: {fpuF31 + fpuF3}");
            Console.WriteLine($"Add VV SSE: {sseF31 + sseF3}");
            Console.WriteLine();
            Console.WriteLine($"Add VS FPU: {fpuF3 + v}");
            Console.WriteLine($"Add VS SSE: {sseF3 + v}");
            Console.WriteLine();
            Console.WriteLine($"Sub VV FPU: {fpuF31 - fpuF3}");
            Console.WriteLine($"Sub VV SSE: {sseF31 - sseF3}");
            Console.WriteLine();
            Console.WriteLine($"Sub VS FPU: {fpuF3 - v}");
            Console.WriteLine($"Sub VS SSE: {sseF3 - v}");
            Console.WriteLine();
            Console.WriteLine($"Mul VV FPU: {fpuF31 * fpuF3}");
            Console.WriteLine($"Mul VV SSE: {sseF31 * sseF3}");
            Console.WriteLine();
            Console.WriteLine($"Mul VS FPU: {fpuF3 * v}");
            Console.WriteLine($"Mul VS SSE: {sseF3 * v}");
            Console.WriteLine();
            Console.WriteLine($"Div VV FPU: {fpuF31 / fpuF3}");
            Console.WriteLine($"Div VV SSE: {sseF31 / sseF3}");
            Console.WriteLine();
            Console.WriteLine($"Div VS FPU: {fpuF3 / v}");
            Console.WriteLine($"Div VS SSE: {sseF3 / v}");
            Console.WriteLine();
            Console.WriteLine($"Dot VV FPU: {fpuF31.Dot(fpuF3)}");
            Console.WriteLine($"Dot VV SSE: {SseMath.Float3.Dot(sseF31, sseF3)}");
            Console.WriteLine();
            Console.WriteLine($"Crs VV FPU: {fpuF31.Cross(fpuF3)}");
            Console.WriteLine($"Crs VV SSE: {SseMath.Float3.Cross(sseF31, sseF3)}");
            Console.WriteLine();
            Console.WriteLine($"Nor VV FPU: {fpuF3.Normalize}");
            Console.WriteLine($"Nor VV SSE: {SseMath.Float3.Normalize(sseF3)}");
            Console.WriteLine();
            Console.WriteLine($"Sat VV FPU: {Light.Saturate(fpuF3)}");
            Console.WriteLine($"Sat VV SSE: {SseMath.Float3.Saturate(sseF3)}");
            Console.WriteLine();
            Console.WriteLine($"Ref VV FPU: {Float3.Reflect(fpuF31, fpuF3)}");
            Console.WriteLine($"Ref VV SSE: {SseMath.Float3.Reflect(sseF31, sseF3)}");
            Console.WriteLine();

            Console.WriteLine($"Add VV FPU: {fpuF41 + fpuF4}");
            Console.WriteLine($"Add VV SSE: {sseF41 + sseF4}");
            Console.WriteLine();
            Console.WriteLine($"Add VS FPU: {fpuF4 + v}");
            Console.WriteLine($"Add VS SSE: {sseF4 + v}");
            Console.WriteLine();
            Console.WriteLine($"Sub VV FPU: {fpuF41 - fpuF4}");
            Console.WriteLine($"Sub VV SSE: {sseF41 - sseF4}");
            Console.WriteLine();
            Console.WriteLine($"Sub VS FPU: {fpuF4 - v}");
            Console.WriteLine($"Sub VS SSE: {sseF4 - v}");
            Console.WriteLine();
            Console.WriteLine($"Mul VV FPU: {fpuF41 * fpuF4}");
            Console.WriteLine($"Mul VV SSE: {sseF41 * sseF4}");
            Console.WriteLine();
            Console.WriteLine($"Mul VS FPU: {fpuF4 * v}");
            Console.WriteLine($"Mul VS SSE: {sseF4 * v}");
            Console.WriteLine();
            Console.WriteLine($"Div VV FPU: {fpuF41 / fpuF4}");
            Console.WriteLine($"Div VV SSE: {sseF41 / sseF4}");
            Console.WriteLine();
            Console.WriteLine($"Div VS FPU: {fpuF4 / v}");
            Console.WriteLine($"Div VS SSE: {sseF4 / v}");
            Console.WriteLine();
            Console.WriteLine($"Dot VV FPU: {fpuF41.Dot(fpuF4)}");
            Console.WriteLine($"Dot VV SSE: {SseMath.Float4.Dot(sseF41, sseF4)}");
            Console.WriteLine();
            Console.WriteLine($"Nor VV FPU: {fpuF4.Normalize}");
            Console.WriteLine($"Nor VV SSE: {SseMath.Float4.Normalize(sseF4)}");
            Console.WriteLine();
            Console.WriteLine($"Mul VM FPU: {fpuF44 * fpuF4}");
            Console.WriteLine($"Mul VM SSE: {sseF44 * sseF4}");
            Console.WriteLine();

            Console.WriteLine($"Mul MM FPU: \n{fpuF44 * fpuF44}");
            Console.WriteLine($"Mul MM SSE: \n{sseF44 * sseF44}");
            Console.WriteLine();
        }
Example #3
0
        public Float3 Tr_obj2view3(Float3 v)
        {
            Float4 u = obj2view * new Float4(v, 0f); //! Float4x4 * Float3

            return(new Float3(u.X, u.Y, u.Z));
        }
Example #4
0
 public Float4(Float3 v, float w) : this(v.X, v.Y, v.Z, w)
 {
 }
Example #5
0
        public Float3 Tr_obj2view4(Float3 v)
        {
            Float4 u = obj2view * new Float4(v, 1f);

            return(new Float3(u.X, u.Y, u.Z) / u.W);
        }
Example #6
0
        public Float3 Tr(Float3 v)
        {
            Float4 u = obj2proj * new Float4(v, 1f);

            return(new Float3(u.X, u.Y, u.Z) / u.W);
        }
Example #7
0
        public static Float3 Reflect(Float3 I, Float3 N)
        {
            Float3 Nn = N.Normalize;

            return(I - Nn * Nn.Dot(I) * 2.0f);
        }
Example #8
0
 public float Dot(Float3 v) => X * v.X + Y * v.Y + Z * v.Z;
Example #9
0
        public void Triangle(Float3 v1, Float3 v2, Float3 v3, Float3 n1, Float3 n2, Float3 n3, ref Light light, ref VertexProcessor proc)
        {
            float x1 = (v1.X + 1) * buff.width * .5f;
            float y1 = (v1.Y + 1) * buff.height * .5f;
            float x2 = (v2.X + 1) * buff.width * .5f;
            float y2 = (v2.Y + 1) * buff.height * .5f;
            float x3 = (v3.X + 1) * buff.width * .5f;
            float y3 = (v3.Y + 1) * buff.height * .5f;

            int minx = Min(x1, x2, x3);
            int miny = Min(y1, y2, y3);
            int maxx = Max(x1, x2, x3);
            int maxy = Max(y1, y2, y3);

            minx = Math.Max(minx, 0);
            maxx = Math.Min(maxx, buff.width - 1);
            miny = Math.Max(miny, 0);
            maxy = Math.Min(maxy, buff.height - 1);

            float dy12 = y1 - y2;
            float dy23 = y2 - y3;
            float dy31 = y3 - y1;
            float dx12 = x1 - x2;
            float dx23 = x2 - x3;
            float dx31 = x3 - x1;

            bool tl1 = false;
            bool tl2 = false;
            bool tl3 = false;

            if (dy12 < 0 || (dy12 == 0 && dx12 > 0))
            {
                tl1 = true;
            }
            if (dy23 < 0 || (dy23 == 0 && dx23 > 0))
            {
                tl2 = true;
            }
            if (dy31 < 0 || (dy31 == 0 && dx31 > 0))
            {
                tl3 = true;
            }

            for (int x = minx; x <= maxx; x++)
            {
                for (int y = miny; y <= maxy; y++)
                {
                    float hs1 = dx12 * (y - y1) - dy12 * (x - x1);
                    float hs2 = dx23 * (y - y2) - dy23 * (x - x2);
                    float hs3 = dx31 * (y - y3) - dy31 * (x - x3);

                    if (((hs1 > 0 && !tl1) || (hs1 >= 0 && tl1)) &&
                        ((hs2 > 0 && !tl2) || (hs2 >= 0 && tl2)) &&
                        ((hs3 > 0 && !tl3) || (hs3 >= 0 && tl3)))
                    {
                        float l1 = (((y2 - y3) * (x - x3)) + ((x3 - x2) * (y - y3))) /
                                   (((y2 - y3) * (x1 - x3)) + ((x3 - x2) * (y1 - y3)));
                        float l2 = (((y3 - y1) * (x - x3)) + ((x1 - x3) * (y - y3))) /
                                   (((y3 - y1) * (x2 - x3)) + ((x1 - x3) * (y2 - y3)));
                        float l3 = 1 - l1 - l2;

                        float depth = l1 * v1.Z + l2 * v2.Z + l3 * v3.Z;

                        if (depth < depthBuff.GetDepth(x, y))
                        {
                            Float3 position = v1 * l1 + v2 * l2 + v3 * l3;
                            Float3 normal   = n1 * l1 + n2 * l2 + n3 * l3;
                            Float3 col      = light.Calculate(in position, in normal, ref proc);
                            buff.SetPixel(x, y, (Color)col);
                            depthBuff.SetDepth(x, y, depth);
                        }
                    }
                }
            }
        }