I've recently started working with OpenTK, and as always with OpenGL, I'm frustrated by the setup. My code is a bit of a mess right now, but can anyone tell me why the output is blank (background color) with this orthographic projection?
The external calls to a Render() function are functional, and render quads positioned around the current origin. The geometry displays when I use the perspective projection example in the code, but that isn't my goal.
Ignore the stuff about shaders, that's just there arbitrarily (not actually used).
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;
}
}
}
}
If you want orthographic camera, you should change camera property in Inspector.