Jonas B's picture

Noob problem: Viewport clipping

Hi,
just started experimenting with OpenTK - seems like a very nice API!
I haven't worked with OpenGL before, and only superficially with other 3D APIs and engines (using them for 2D rendering mainly), so I'm not that good with the concepts.

I have a scene set up using the GameWindow, with orthographic projection, to which I render textured quads. Everything looks good, but the quads are only rendered in a (square) portion of the window, and are clipped outside of it.

I think it's set up properly, basically:
w = window width, h = window height

GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
Glu.Ortho2D(0,w,0,h);
GL.Viewport(0, 0, w, h);

Seems like the clipping occurs at approximately w/2 for both width and height.

Anything obvious I have missed here?

Thanks!


Comments

Comment viewing options

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

Edit: I think the problem is that you specify the viewport after the projection matrix, not before.

Does this only occur always, or only when you change window size? If it's the latter, you should hook the Resize event and execute the code you listed.

I tend to use something like the following:

protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{
    if (Height == 0 || Width == 0)
        return;
 
    if (GraphicsContext.CurrentContext != null)
    {
        GL.Viewport(0, 0, Width, Height);
 
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadIdentity();
        GL.Ortho(0, Width, 0 Height, -1, 1);
    }
}
Jonas B's picture

Thanks for the quick answer!
Tried changing the order, but same result - however, after resizing the window manually (by just 1 pixel) it works properly! It occurs to me now that I do my setup directly after the GameWindow's construction, I don't wait for the Load event, which I know is a big no-no. My initialization is a bit complicated, that's why I didn't notice it before
Will try changing that and report back!

Jonas B's picture

To initialize on Load didn't make any difference - but when I programmatically re-set the size (to the same size it already is), it works fine!

the Fiddler's picture

Interesting, the window Width and Height are supposed to be set before the Load event fires. I will have to ensure this works reliably in the gw-next branch.