Shogun's picture

How to use GL.Ortho in OpenTK correctly?

Hello guys,

To begin with, I'm new in this forum as well as with OpenTK, so be merciful when I don't know things, that are obvious for you. :)

I've actually posted this question on SO but to no avail. Despite reading a lot, I couldn't solve this issue on my own. My last hope are you guys.

I paint some figures using GLControl (OpenTK) in Windows forms. However, the problem is that I cannot figure out, how to use GL.Ortho() method.

Here is the code I have written:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
 
    private void GlControlPaint(object sender, PaintEventArgs e)
    {
        GlControl.MakeCurrent();
        GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
 
        GL.Viewport(150, 150, 300, 300);
        //GL.Ortho(0, 1, 0, 1, -1, 1);
        GL.ClearColor(Color.White);
 
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.2, 0.2, 0.45, 0.2, 0.45, -0.2, 0.2, -0.2);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.1, -0.1, 0.1, 0.1, 0.2, 0.2, 0.2, -0.2);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.2, -0.2, -0.45, -0.2, -0.45, 0.2, -0.2, 0.2);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.1, 0.1, -0.1, -0.1, -0.2, -0.2, -0.2, 0.2);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.1, 0.1, -0.2, 0.2, 0.2, 0.2, 0.1, 0.1);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.1, -0.1, 0.2, -0.2, -0.2, -0.2, -0.1, -0.1);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, -0.2, 0.2, -0.2, 0.45, 0.2, 0.45, 0.2, 0.2);
        PaintSquareOrBorder(BeginMode.Quads, Color.Cyan, 0.2, -0.2, 0.2, -0.45, -0.2, -0.45, -0.2, -0.2);
        PaintSquareOrBorder(BeginMode.LineLoop, Color.Black, -0.1, -0.1, 0.1, -0.1, 0.1, 0.1, -0.1, 0.1);
 
        PaintBordersForMainFigure();
 
 
 
        GlControl.SwapBuffers();
        GlControl.Refresh();
 
    }
 
    private void PaintBordersForMainFigure()
    {
        PaintLine(Color.Black, 0.2, 0.2, 0.45, 0.2);
        PaintLine(Color.Black, 0.45, 0.2, 0.45, -0.2);
        PaintLine(Color.Black, 0.45, -0.2, 0.2, -0.2);
        PaintLine(Color.Black, 0.2, -0.2, 0.2, -0.45);
        PaintLine(Color.Black, 0.2, -0.45, -0.2, -0.45);
        PaintLine(Color.Black, -0.2, -0.45, -0.2, -0.2);
        PaintLine(Color.Black, -0.2, -0.2, -0.45, -0.2);
        PaintLine(Color.Black, -0.45, -0.2, -0.45, 0.2);
        PaintLine(Color.Black, -0.45, 0.2, -0.2, 0.2);
        PaintLine(Color.Black, -0.2, 0.2, -0.2, 0.45);
        PaintLine(Color.Black, -0.2, 0.45, 0.2, 0.45);
        PaintLine(Color.Black, 0.2, 0.45, 0.2, 0.2);
    }
 
    private static void PaintLine(Color color, double x1, double y1, double x2, double y2)
    {
        GL.Color3(color);
 
        GL.Begin(BeginMode.Lines);
 
        GL.Vertex2(x1, y1);
        GL.Vertex2(x2, y2);
 
        GL.End();
    }
 
    private static void PaintSquareOrBorder(BeginMode beginMode, Color color, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
    {
        GL.Color3(color);
 
        GL.Begin(beginMode);
 
        GL.Vertex2(x1, y1);
        GL.Vertex2(x2, y2);
        GL.Vertex2(x3, y3);
        GL.Vertex2(x4, y4);
 
        GL.End();
    }
}



At first I thought, that as I use 2d objects only, I should use Ortho2D for that. However, I found out that Gl.Ortho2d does not exist in OpenTK. Using official documentation I found out, that there isn't that much of a difference between these 2, except for the fact that when Ortho2d is used near and far parameters are implicitly set to -1 and 1 respectively.

Having set those parameters, I get a white screen. I would like to know, what am I doing wrong?

DISCLAIMER: I don't need the exact coordinates to achieve the result in the screenshot. I just use it in order for you to see what I am trying to do. What I would like to get the idea why my window is completely blank when I use Gl.Ortho2d.

Thanks in advance.


Comments

Comment viewing options

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

I was actually wondering the same thing myself and found this.

You use GL.Ortho for converting the opengl scene coordinates to regular screen coordinates(eg. 200, 400).
This is a part of my code

protected override void OnLoad(EventArgs e)
{
	GL.MatrixMode(MatrixMode.Projection);
	GL.LoadIdentity ();
	GL.Ortho(0, this.Width, this.Height, 0, -1, 1);
 
	GL.MatrixMode(MatrixMode.Modelview);
	GL.LoadIdentity ();
 
	GL.Disable(EnableCap.DepthTest);
 
	GL.ClearColor(Color.MidnightBlue);
 
}

So it means that if you want to draw triangles or something you dont have to use GL.Vertex2(-1, 0.5); to define the vertices you can use GL.Vertex2(200, 200); instead.

Shogun's picture

Thank you very much. I adjusted loaded identity before using Gl.Ortho and it worked exactly as I wanted.

Thanks again.