1
\$\begingroup\$

I am currently developing a GUI app using OpenGL with OpenTK as interface in C#.

The viewport is set to the entire window space (0,0,width,height) and an ortho transformation is applied like this:

GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, width, height, 0, 0f, 1f);

With this I can draw objects just like I would do in GDI+ using X,Y coords.

The issue appears when I want to clip elements within their parent's area. I am doing it using glScissor. I am using a stack to keep trace of the scissored areas like so: static Stack stack = new Stack();

internal static void Scissor(Rectangle rect)
{
    applyScissor(rect);
    if (stack.Count == 0) GL.Enable(EnableCap.ScissorTest);
    stack.Push(rect);
}

internal static void RemoveScissor()
{
    if (stack.Count == 0) throw new Exception("Unblanaced stack.");
    stack.Pop();
    if (stack.Count == 0)
    {
        GL.Disable(EnableCap.ScissorTest);
    }
    else
    {
        var rect = stack.Peek();
        applyScissor(rect);
    }
}

static void applyScissor(Rectangle rect)
{
    GL.Scissor(rect.X, rect.Y, rect.Width, rect.Height);
}

It works fine whenever the height is "big" enough but if the values for the height given to the scissor are "small" it doesn't work.

Example without scissors:

enter image description here

Example with scissor

enter image description here

Example when I scissor with half the needed height:

(In the last case I cannot understand why it shows the bottom half instead of the top half.)

Basically there is an element (the blue box and the text) within the red box with a blue border that should be clipped within and there should be only a small portion of the A visible but at the very least the blue rectangle should be visible. However it is not. :/ The scissor test does work fine however if the height is "big" enough as I mentioned, being able to move the red box with blue borders outside the area of its parent and it wouldn't be visible.

I need to know why that is and how to fix it.

PS. The issue is not only on OS X so it's not the platform.

PS2. I did check the values given to the scissor and they are correct. The area of the red box with blue border is 175,20 with size 36x36.

\$\endgroup\$
1
\$\begingroup\$

Turns out the Scissor function uses the y value wrong for whatever reason. In my case I would need to alter the y value to

windowHeight - scissorHeight - y

The actual call would look like this

internal static void ApplyScissor(Rectangle rect)
{
    GL.Scissor(rect.X, windowHeight - rect.Height - rect.Y, rect.Width, rect.Height);
}

If anyone has any clue of why that is please let me know. In a sense it wants me to give the bottom left corner, but that really doesn't make any sense.

\$\endgroup\$
  • \$\begingroup\$ In OpenGL the bottom-left corner is the {0,0} point for everything. \$\endgroup\$ – Maximus Minimus Mar 1 '17 at 14:08
  • \$\begingroup\$ But me using ortho should change that. Shouldn't it? \$\endgroup\$ – Raedok Mar 1 '17 at 16:09
  • \$\begingroup\$ Nope; Ortho only specifies a transformation of vertices which scissor rect dimensions are not affected by. \$\endgroup\$ – Maximus Minimus Mar 1 '17 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.