anewguy's picture

New to OpenTK - change 2D coordinates to more common system?

Here's how 2D coordinates usually are for other programs/languages/whatever
^<-- top left over there is (0,0)
Bottom right over there -->v is something around (640,480), depending on window size

This is a good system and easy to work with

But OpenTK..
^<-- top left is (-1, -1)
Middle is (0,0)
Bottom right -->v is (1, 1)
..This is annoying.

How do I change it to the first system?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zengeek's picture

That is native OpenGL coordinates system. To change it to what you want just change the projection matrix.

If you're new to OpenGL you can do it "legacy mode" by calling :

//Set Viewport before 
GL.ViewPort(0, 0, width , height); 
 
//Set OpenGL matrix state to projection
GL.MatrixMode(MatrixMode.Projection);
 
//Send identity just to be safe
GL.LoadIdentity();
 
//Send ortho matrix 
GL.Ortho(0.0, width , height, 0.0, 0.0, 1.0); // For a 2D Projection (Left,Right,Bottom, Top, Frustum Near , Frustum Far)

If you are already know your way in OpenGL and want to do it the "recommended way":

Send matrix in "shader mode" by creating an ortho matrix :

Matrix4.CreateOrthographic(width, height, 0f, 1f, out projMatrix);

and sending it to your shader by calling

GL.UniformMatrix4(location, 1, false, projMatrix); // 

Judging by the nature of your question i recommend going with first option first. The second method involves many more steps than what is shown. I only put it here so that you know what can be done. If you want a book or two i recommend: The OpenGL Programming Guide 7th edition. And OpenGL Superbible 5th edition.