c# - How to use GL.Ortho in OpenTK correctly? -


i paint figures using glcontrol (opentk) in windows forms. however, problem cannot figure out, how use gl.ortho() method.

here code 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();     } } 

this result without gl.ortho:

this result gl.ortho

but if uncomment gl.ortho code, this:

at first thought, use 2d objects only, should use ortho2d that. however, found out gl.ortho2d not exist in opentk. using official documentation found out, there isn't of difference between these 2, except fact when ortho2d used near , far parameters implicitly set -1 , 1 respectively.

having set parameters, white screen. know, doing wrong?

disclaimer: don't need exact coordinates achieve result in screenshot. use in order see trying do. idea why window blank when use gl.ortho2d.

the problem gl.ortho() multiplies current matrix orthographic matrix. each rendered frame keep multiplying matrix new orthographic matrix, , view drifts away somewhere cant see anything.

change that, , see nice animation:

gl.ortho(-0.99, 1, -0.99, 1, -1, 1); 

add 2 lines in front of gl.ortho() call, way matrix identity matrix before multiply orthographic matrix.

gl.matrixmode(matrixmode.projection); gl.loadidentity(); 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -