コード例 #1
0
        static void Init()
        {
            int screenW = Screen.Width;
            int screenH = Screen.Height;

            Texture.DefaultMinFilter = TextureFilter.Nearest;
            Texture.DefaultMagFilter = TextureFilter.Nearest;

            //Load assets
            var shader2D    = Shader.FromFile("Assets/basic_2d.glsl");
            var shader3D    = Shader.FromFile("Assets/basic_3d_nobuffer.glsl");
            var shaderG     = Shader.FromFile("Assets/basic_3d_gbuffer.glsl");
            var shaderDepth = Shader.FromFile("Assets/basic_3d_depth.glsl");
            var shaderPos   = Shader.FromFile("Assets/basic_3d_position.glsl");
            var pinkSquare  = new Texture2D("Assets/pink_square.png", true);
            var mesh        = Mesh3D.CreateBox(Vector3.One, Color4.White);
            //var mesh = Mesh3D.CreateSphere(0.5f, 10, Color4.White);
            //var mesh = Mesh3D.CreateCylinder(0.5f, 1f, 10, true);
            //var mesh = Mesh3D.CreateCone(0.5f, 2f, 10);

            //Create the g-buffer
            var gDepth  = new Texture2D(screenW, screenH, TextureFormat.Depth);
            var gColor  = new Texture2D(screenW, screenH, TextureFormat.RGB);
            var gNormal = new Texture2D(screenW, screenH, TextureFormat.RGB16F);
            var gZ      = new Texture2D(screenW, screenH, TextureFormat.R32F);
            var gBuffer = new RenderTarget(screenW, screenH, gDepth, gColor, gNormal, gZ);

            //View
            var scene      = new Scene(screenW, screenH);
            var viewRot    = false;
            var viewRotPos = Vector2.Zero;

            //Create a model
            var cube = new Model(mesh, pinkSquare);

            cube.Scale = new Vector3(2f, 0.75f, 1f);
            scene.Add(cube);

            //Create a model
            cube          = new Model(mesh, pinkSquare);
            cube.Scale    = new Vector3(0.75f);
            cube.Position = new Vector3(0f, 0.5f, -2f);
            scene.Add(cube);

            //Draw to buffer
            var draw = new DrawCall(gBuffer, new Material(shaderG));

            //Draw to screen
            int w        = screenW / 2;
            int h        = screenH / 2;
            var toScreen = new DrawCall(new Material(shaderPos));

            toScreen.Mesh = Mesh2D.CreateRect(new Rectangle(0, 0, w, h));
            toScreen.Material.SetMatrix4x4("Matrix", Matrix4x4.CreateOrthographic(0f, screenW, 0, screenH, -1f, 1f));
            toScreen.Material.SetTexture("ZBuffer", gDepth);

            App.OnUpdate += Update;
            App.OnRender += Render;
            Update();
            void Update()
            {
                //Pivoting
                if (viewRot)
                {
                    var move = Mouse.Position - viewRotPos;
                    if (move != Vector2.Zero)
                    {
                        scene.ViewAngle += move * 6f * Time.Delta;
                        viewRotPos       = Mouse.Position;
                    }
                    if (!Mouse.LeftDown)
                    {
                        viewRot = false;
                    }
                }
                else if (Mouse.LeftPressed)
                {
                    viewRot    = true;
                    viewRotPos = Mouse.Position;
                }

                //Zooming
                if (Mouse.ScrollY != 0)
                {
                    scene.ViewDist -= Mouse.ScrollY * 10f * Time.Delta;
                }
            }

            void Render()
            {
                int modelID   = draw.Material.GetIndex("ModelMatrix");
                int mvpID     = draw.Material.GetIndex("ModelViewProjectionMatrix");
                int textureID = draw.Material.GetIndex("Texture");

                draw.Clear(0x1f171fff);
                foreach (var model in scene.Models)
                {
                    draw.Material.SetMatrix4x4(modelID, model.Matrix);
                    draw.Material.SetMatrix4x4(mvpID, model.MvpMatrix);
                    draw.Material.SetTexture(textureID, model.Texture);
                    draw.Mesh = model.Mesh;
                    draw.Perform(PrimitiveType.Triangles);
                }

                //toScreen.Perform(Color3.Black);
                gBuffer.BlitTextureTo(null, 0, BlitFilter.Linear, new RectangleI(0, h, w, h));
                gBuffer.BlitTextureTo(null, 1, BlitFilter.Linear, new RectangleI(w, h, w, h));
                gBuffer.BlitTextureTo(null, 2, BlitFilter.Linear, new RectangleI(w, 0, w, h));

                toScreen.Material.SetMatrix4x4("ProjMatrix", scene.ProjMatrix);
                toScreen.Material.SetMatrix4x4("ViewMatrix", scene.ViewMatrix);
                toScreen.Perform(PrimitiveType.Triangles);
            }
        }