ravi.joshi53's picture

Setting up GL.Ortho() for higher bounds

Hi,

I am calculating points in following manner (which needs to be displayed in 2D) -

Xpixel = (Xortho * X) / ScreenWidth

Here x and ScreenWidth values are in mm.

My OpenTK app is configured in following way-

protected override void OnResize(EventArgs e)
{
    GL.Viewport(0, 0, ScreenWidthResolution, ScreenHeightResolution);
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();
    GL.Ortho(0.0, 12.0, 9.0, 0.0, 1.0, -1.0);
}

So as per the design and the mathematical expression, Xpixel lies between 0 to 12. However the value X is much higher than this. The small deviation in this X is making a significant change in Xpixel, which is making fluctuation in the animation.

To overcome with the issue, I though of increasing the GL.Ortho() bounds to GL.Ortho(0, ScreenWidthResolution, 0, ScreenHeightResolution, 1.0,  -1.0); but it doesn't worked...

Note that, there is no control over X value.

Any help would be greatly appreciated!


Comments

Comment viewing options

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

If I understand correctly, you are trying to display all points without cutting anything off - is that correct?

If so, you simply need to calculate a bounding box for all points and use that to setup your projection matrix:

void UpdateProjection(IEnumerable<Point> points)
{
    var min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
    var max = new Vector3(float.MinValue, float.MinValue, float.MinValue);
    foreach (var p in points)
    {
        if (p.X > max.X)
            max.X = p.X;
        else if (p.X < min.X)
            min.X = p.X;
        if (p.Y > max.Y)
            max.Y = p.Y;
        else if (p.Y < min.Y)
            min.Y = p.Y;
        if (p.Z > max.Z)
            max.Z = p.Z;
        else if (p.Z < min.Z)
            min.Z = p.Z;
    }
 
    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();
    GL.Ortho(min.X, max.X, max.Y, min.Y, min.Z, max.Z);
}

Note that GL.Viewport() should correspond to the size of your OpenGL window in pixels, not the size of the desktop:

protected override void OnResize(EventArgs e)
{
    GL.Viewport(0, 0, Width, Height);
}
ravi.joshi53's picture

Fiddler,

Thanks for the quick response. I forgot to mention that the app works on full screen mode, hence following should be correct-

protected override void OnResize(EventArgs e)
{
    GL.Viewport(0, 0, Width, Height);
}

Anyway let me tell you a scenario of my original post. Consider following two sets for values-

X = 400 mm
ScreenWidth = 800mm
Xortho = 12

This gives Xpixel = 6

X = 450 mm
ScreenWidth = 800mm
Xortho = 12

This gives Xpixel = 6.75

My points was that even though I am making small change in the value of X, the final value of Xpixel is getting changed by a significant factor in UI.

To overcome with this issue, I thought of setting GL.Ortho() to GL.Ortho(0, ScreenWidthResolution, 0, ScreenHeightResolution, 1.0,  -1.0);. By setting this, the variation in Xpixel can't be detect easily. But that doesn't work.

And, In this case, all the points lies between 0 to 12. (Since the mathematical expression is designed to do so)