//Celshader

        public Vector3 CelShading(VertexProcessor vert, Vector3 normal)
        {
            shininess = 2f;
            int _CelTone = 10;

            Vector3 diffuseColor  = new Vector3(0, 0, 200);
            Vector3 specularColor = new Vector3(10, 10, 10);

            Vector3 N = vert.tr(normal).Normalize();
            Vector3 V = vert.tr(position).Normalize();
            Vector3 L = (V - position).Normalize();
            Vector3 R = L.Reflect(L, N);

            float  diffuseValue  = Math.Max(0, L.Dot(N));
            double celShading    = Math.Floor((diffuseValue * _CelTone) / (_CelTone - 0.5));
            float  specularValue = (float)Math.Pow(R.Dot(V), shininess);

            diffuseColor  *= diffuseValue;
            diffuseColor  *= (float)celShading;
            specularColor *= specularValue;

            Vector3 col = diffuseColor + specularColor;

            return(col);
        }
Exemplo n.º 2
0
 public void Light(Light l, VertexProcessor processor)
 {
     foreach (Vertex v in vertexes)
     {
         v.light = l.Calculate(processor, v);
     }
 }
        //shader Gooch
        public Vector3 GoochShading(VertexProcessor vert, Vector3 normal)
        {
            shininess = 2f;
            float alfa = 0.5f;
            float beta = 0.25f;

            Vector3 coolColor = new Vector3(0, 0, 255);
            Vector3 warmColor = new Vector3(255, 0, 0);

            Vector3 diffuseColor  = new Vector3(0, 0, 0);
            Vector3 specularColor = new Vector3(50, 50, 50);

            Vector3 N = vert.tr(normal).Normalize();
            Vector3 V = vert.tr(position).Normalize();
            Vector3 L = (V - position).Normalize();
            Vector3 R = L.Reflect(L, N);

            float diffuseValue = Math.Max(0, L.Dot(N));
            float wsp          = 0.5f * (1.0f + diffuseValue);

            float specularValue = (float)Math.Pow(R.Dot(V), shininess);

            diffuseColor  *= diffuseValue;
            specularColor *= specularValue;

            coolColor = coolColor + diffuseColor * alfa;
            warmColor = warmColor + diffuseColor * beta;

            Vector3 gooch = (warmColor * (1.0f - wsp)) + (coolColor * wsp) + specularColor;

            return(gooch);
        }
Exemplo n.º 4
0
        public Bitmap Run()
        {
            var bitmap = new Bitmap(640, 480, PixelFormat.Format32bppArgb);

            var r = new Rasterizer();
            var v = new VertexProcessor(r);

            var pixelShader  = new PixelShader();
            var vertexShader = new VertexShader();

            r.SetScissorRect(0, 0, 640, 480);
            v.SetViewport(100, 100, 640 - 200, 480 - 200);
            v.SetCullMode(CullMode.None);

            var indices = new List <int> {
                0, 1, 2
            };
            var vertices = new List <VertexData>();

            var vertex = new VertexData();

            vertex.x = 0.0f;
            vertex.y = 0.5f;
            vertex.z = 0.0f;
            vertex.r = 1.0f;
            vertex.g = 0.0f;
            vertex.b = 0.0f;
            vertices.Add(vertex);

            vertex   = new VertexData();
            vertex.x = -1.5f;
            vertex.y = -0.5f;
            vertex.z = 0.0f;
            vertex.r = 0.0f;
            vertex.g = 1.0f;
            vertex.b = 0.0f;
            vertices.Add(vertex);

            vertex   = new VertexData();
            vertex.x = 1.5f;
            vertex.y = -0.5f;
            vertex.z = 0.0f;
            vertex.r = 0.0f;
            vertex.g = 0.0f;
            vertex.b = 1.0f;
            vertices.Add(vertex);

            pixelShader.Bitmap      = bitmap;
            vertexShader.VertexData = vertices;

            r.SetPixelShader(pixelShader);
            v.SetVertexShader(vertexShader);

            v.DrawElements(DrawMode.Triangle, 3, indices);

            return(bitmap);
        }
        public Vector3 GoraudShading(VertexProcessor vert, Vertex v1, Vertex v2, Vertex v3)
        {
            Vector3 col  = Lerp(v1.light, v2.light, 0.5f);
            Vector3 col2 = Lerp(v1.light, v3.light, 0.5f);

            col = Lerp(col, col2, 0.5f);

            return(col);
        }
Exemplo n.º 6
0
 public void DrawMesh(Rasterization rasterizer, VertexProcessor processor, Light l)
 {
     //Представление вершины в пространстве.
     for (int i = 0; i < indexes.Count; i += 3)
     {
         rasterizer.Triangle(processor.tr(vertexes[indexes[i]].position), processor.tr(vertexes[indexes[i + 1]].position),
                             processor.tr(vertexes[indexes[i + 2]].position), vertexes[indexes[i]], vertexes[indexes[i + 1]], vertexes[indexes[i + 2]], l, processor);
     }
 }
Exemplo n.º 7
0
        public void Run()
        {
            var r = new Rasterizer();
            var v = new VertexProcessor(r);

            var pixelShader  = new PixelShader();
            var vertexShader = new VertexShader();

            r.SetScissorRect(0, 0, 640, 480);
            v.SetViewport(0, 0, 640, 480);
            v.SetCullMode(CullMode.None);

            var indices  = new List <int>();
            var vertices = new List <VertexData>();

            var random = new Random(0);

            for (int i = 0; i < 4096 * 10; i++)
            {
                VertexData CreateVertex()
                {
                    var vertex = new VertexData();

                    vertex.x = (float)random.NextDouble();
                    vertex.y = (float)random.NextDouble();
                    vertex.z = (float)random.NextDouble();
                    vertex.r = (float)random.NextDouble();
                    vertex.g = (float)random.NextDouble();
                    vertex.b = (float)random.NextDouble();
                    return(vertex);
                }

                var offset = vertices.Count;

                vertices.Add(CreateVertex());
                vertices.Add(CreateVertex());
                vertices.Add(CreateVertex());

                indices.Add(offset + 0);
                indices.Add(offset + 1);
                indices.Add(offset + 2);
            }

            pixelShader.Buffer      = new int[640 * 480];
            pixelShader.Width       = 640;
            pixelShader.Height      = 480;
            vertexShader.VertexData = vertices;

            r.SetPixelShader(pixelShader);
            v.SetVertexShader(vertexShader);

            var sw = Stopwatch.StartNew();

            v.DrawElements(DrawMode.Triangle, indices.Count, indices);
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
Exemplo n.º 8
0
        public static void TransformVertices <T>(Mesh <T> mesh, VertexProcessor <T> processor) where T : unmanaged
        {
            var vertices = mesh.Vertices;

            for (int i = 0; i < vertices.Length; i++)
            {
                processor(ref vertices[i]);
            }
            mesh.DirtyFlags |= MeshDirtyFlags.Vertices;
        }
Exemplo n.º 9
0
        public void DrawMesh(Rasterization rasterizer, VertexProcessor processor, Light l)
        {
            System.Console.WriteLine("Ilosc indeksow: " + indexes.Count);

            for (int i = 0; i < indexes.Count; i += 3)
            {
                rasterizer.Triangle(processor.tr(vertexes[indexes[i]].position), processor.tr(vertexes[indexes[i + 1]].position), processor.tr(vertexes[indexes[i + 2]].position), vertexes[indexes[i]], vertexes[indexes[i + 1]], vertexes[indexes[i + 2]], l, processor);
                stream.WriteLine("Indeks: " + indexes[i] + " " + indexes[i + 1] + " " + indexes[i + 2]);
                stream.WriteLine("Wierzchołki: " + vertexes[indexes[i]].position.ToString() + " | " + vertexes[indexes[i + 1]].position.ToString() + " | " + vertexes[indexes[i + 2]].position.ToString());
            }
            stream.Close();
        }
Exemplo n.º 10
0
        public static void TransformVertices <T>(IMesh mesh, VertexProcessor <T> processor) where T : struct
        {
            if (mesh.VertexBuffers.Length != 1 || !(mesh.VertexBuffers[0] is VertexBuffer <T>))
            {
                throw new InvalidOperationException();
            }
            var vertices = ((VertexBuffer <T>)mesh.VertexBuffers[0]).Data;

            for (int i = 0; i < vertices.Length; i++)
            {
                processor(ref vertices[i]);
            }
            mesh.VertexBuffers[0].Dirty = true;
        }
Exemplo n.º 11
0
        public Bitmap Run()
        {
            var screen  = new Bitmap(640, 480, PixelFormat.Format32bppArgb);
            var texture = new Bitmap("Data/box.png");

            var objLoaderFactory = new ObjLoaderFactory();
            var objLoader        = objLoaderFactory.Create(new IgnoreMaterial());
            var box = objLoader.Load(File.OpenRead("Data/box.obj"));

            (var idata, var vdata) = CreateVertexArray(box);

            var lookAt      = Matrix4x4.CreateLookAt(new Vector3(3.0f, 2.0f, 5.0f), new Vector3(0.0f), new Vector3(0.0f, 1.0f, 0.0f));
            var fieldOfView = (float)(60.0 * Math.PI / 180.0);
            var aspect      = 4.0f / 3.0f;
            var perspective = Matrix4x4.CreatePerspectiveFieldOfView(fieldOfView, aspect, 0.1f, 10.0f);

            var r = new Rasterizer();
            var v = new VertexProcessor(r);

            r.SetRasterMode(RasterMode.Span);
            r.SetScissorRect(0, 0, 640, 480);

            v.SetViewport(0, 0, 640, 480);
            v.SetCullMode(CullMode.CW);

            var pixelShader = new PixelShader();

            pixelShader.Screen  = screen;
            pixelShader.Texture = texture;
            r.SetPixelShader(pixelShader);

            var vertexShader = new VertexShader();

            vertexShader.Data = vdata;
            vertexShader.ModelViewProjectionMatrix = lookAt * perspective;
            v.SetVertexShader(vertexShader);

            v.DrawElements(DrawMode.Triangle, idata.Count, idata);

            return(screen);
        }
        public Vector3 Calculate(VertexProcessor vert, Vertex v)
        {
            shininess = 1f;
            Vector3 diffuseColor  = new Vector3(0, 0, 100);
            Vector3 specularColor = new Vector3(255, 0, 0);

            Vector3 N = vert.tr(v.normal).Normalize();
            Vector3 V = vert.tr(position).Normalize();
            Vector3 L = (V - position).Normalize();
            Vector3 R = L.Reflect(L, N);

            float diffuseValue  = Math.Max(0.0f, L.Dot(N));
            float specularValue = (float)Math.Pow(R.Dot(V), shininess);

            diffuseColor  *= diffuseValue;
            specularColor *= specularValue;

            Vector3 col = diffuseColor + specularColor;

            return(col);
        }
Exemplo n.º 13
0
 public void DrawMesh(Rasterization rasterizer, VertexProcessor processor)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 14
0
 public SkinFilter(VertexProcessor processor) : base(processor)
 {
 }
 public void Light(Light l, VertexProcessor v)
 {
     throw new NotImplementedException();
 }
 public void DrawMesh(Rasterization rasterizer, VertexProcessor processor)
 {
     //rasterizer.Triangle(processor.tr(vertices[0].position), processor.tr(vertices[1].position), processor.tr(vertices[2].position));
     //rasterizer.Triangle(vertices[0].position, vertices[1].position, vertices[2].position);
 }
Exemplo n.º 17
0
 public PhysicsFilter(VertexProcessor processor) : base(processor)
 {
 }