【发布时间】:2016-12-25 14:38:34
【问题描述】:
我最近开始使用 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;
}
}
}
}
【问题讨论】:
-
澄清一下,我知道几何图形仍在某处渲染。我只是不知道如何正确配置它,以便几何图形出现在视口中(并正确转换)。
标签: c# opengl opentk orthographic