不知道为什么正交投影什么也没有显示,有人可以帮忙吗? (OpenTK)

时间:2016-12-25 14:38:34

标签: c# opengl opentk orthographic

我最近开始使用OpenTK,而且与OpenGL一样,我对设置感到沮丧。我的代码现在有点乱,但有人能告诉我为什么输出是空白的(背景颜色)这个正交投影?

对Render()函数的外部调用是有效的,并且渲染四边形位于当前原点周围。当我在代码中使用透视投影示例时,几何体会显示,但这不是我的目标。

忽略有关着色器的内容,这些内容是任意的(实际上并未使用)。

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Genesis
{
    class Game : GameWindow
    {
        public static Game Inst;

        public Matrix4 ModelViewMatrix;
        public Matrix4 ProjectionMatrix;

        private int ShaderProgram;

        private int VertexShader;
        private int FragmentShader;

        float rot = 0;

        public GameObject Selector;

        public Multiblock BaseIsland;

        private List<GameObject> gameObjects = new List<GameObject>();

        public Game()
            : base(1000, 650, GraphicsMode.Default, Strings.WINDOW_TITLE)
        {
            VSync = VSyncMode.On;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            GL.ClearColor(0.0f, 0.8f, 0.8f, 0.0f);

            GL.Enable(EnableCap.Texture2D);

            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            GL.Enable(EnableCap.DepthTest);

            ShaderProgram = GL.CreateProgram();

            VertexShader = GL.CreateShader(ShaderType.VertexShader);
            GL.ShaderSource(VertexShader, ShaderSource.Vertex_Default);
            GL.CompileShader(VertexShader);

            FragmentShader = GL.CreateShader(ShaderType.FragmentShader);
            GL.ShaderSource(FragmentShader, ShaderSource.Fragment_Default);
            GL.CompileShader(FragmentShader);

            GL.LinkProgram(ShaderProgram);

            //GL.UseProgram(ShaderProgram);

            Assets.Init();
            Blocks.Init();

            //INIT
            BaseIsland = new Multiblock("Base Island", 7, 7);
            for(int x = 2; x <= 4; x++)
            {
                BaseIsland.SetBlock(x, 0, Blocks.BLOCK_DIRT);
            }
            for (int x = 1; x <= 5; x++)
            {
                BaseIsland.SetBlock(x, 1, Blocks.BLOCK_DIRT);
            }
            for (int x = 0; x < 7; x++)
            {
                BaseIsland.SetBlock(x, 2, Blocks.BLOCK_GRASS);
            }
            for (int y = 3; y < 7; y++)
            {
                BaseIsland.SetBlock(3, y, Blocks.BLOCK_STONE);
            }
            BaseIsland.SetBlock(2, 5, Blocks.BLOCK_STONE);
            BaseIsland.SetBlock(4, 5, Blocks.BLOCK_STONE);

            Selector = new GameObject(Assets.UI_SELECTOR);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            GL.Viewport(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);

            //ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI / 4, Width / (float)Height, 1.0f, 64.0f);
            ProjectionMatrix = Matrix4.CreateOrthographic(Width, Height, 1.0f, 100.0f);
            Console.WriteLine(ProjectionMatrix);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.LoadMatrix(ref ProjectionMatrix);
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            if (Keyboard[Key.Escape])
                Exit();

            //TileSelection sel = Input.GetSelectedTile();

            Vector2 mousePos = Input.GetMousePosition();
            float sx = (mousePos.X * Constants.GLOBAL_SCALE) / (Constants.PIXELS_PER_UNIT * 0.83f);
            float sy = (mousePos.Y * Constants.GLOBAL_SCALE) / (Constants.PIXELS_PER_UNIT * 0.83f);
            Selector.Transform.SetPosition(sx, sy);

            //Console.WriteLine("ProjectionMatrix");
            //Console.WriteLine(ProjectionMatrix);

            rot = (rot + 1.0f) % 360;
            Console.WriteLine(rot);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            ModelViewMatrix = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.LoadMatrix(ref ModelViewMatrix);

            GL.Rotate(rot, 0, 1, 0);
            GL.Translate(0, 0, -5);
            GL.Scale(Constants.GLOBAL_SCALE, Constants.GLOBAL_SCALE, 1);

            //GL.UseProgram(ShaderProgram);

            for (int i = gameObjects.Count - 1; i >= 0; i--)
            {
                GL.PushMatrix();

                gameObjects[i].Render();

                GL.PopMatrix();

            }

            SwapBuffers();
        }

        public void OnGameObjectCreated(GameObject obj)
        {
            gameObjects.Add(obj);

            Console.WriteLine("GAMEOBJECT CREATED: " + obj.Name + " - " + obj.Texture);
        }

        public void OnGameObjectDestroyed(GameObject obj)
        {
            gameObjects.Remove(obj);
        }

        [STAThread]
        static void Main()
        {
            using (Game game = new Game())
            {
                Game.Inst = game;
                game.Run(30.0);
                Game.Inst = null;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

如果你想要正交相机,你应该改变 Inspector中的相机属性。