Search code examples
c#openglopentk

Zoom in / out in OpenGL C#


I'm trying to create a OpenGL C# project to zoom in and out for a scene. I want to enter and exit fom zoom mode when I press the Z key. And then, when I'm in zoom mode ( Z key pressed first time ) to use the mouse wheel to zoom in / out the scene. When I finished the zoom action, I want to be able to exit the zoom mode ( press Z again ) and then the mouse wheel will stop zooming my scene. Thanks for help!

protected override void OnUpdateFrame(FrameEventArgs e)
    {
        base.OnUpdateFrame(e);

        Matrix4 lookat = Matrix4.LookAt(eyeVector, targetVector, upVector);
        GL.MatrixMode(MatrixMode.Modelview);
        GL.LoadMatrix(ref lookat);

        KeyboardState keyboard = Keyboard.GetState();

        if (keyboard[Key.Z] && !keyboard.Equals(lastKeyPress))
        {

        }

        if (keyboard[Key.Z] && !keyboard.Equals(lastKeyPress))
        {

        }

        lastKeyPress = keyboard;
    }

This is the function for zoomOut:

public Vector3 zoomOut(Vector3 actual)
    {
        if(zoomLimits[1]<=actual.X || zoomLimits[1] <= actual.Y || zoomLimits[1] <= actual.Z) {
            Console.WriteLine("Limita de zoomOut a fost atinsa!");
            return actual;
        }

        Vector3 nou = new Vector3(actual.X + 5, actual.Y + 5, actual.Z + 5);
        return nou;
    }

Here I just change the eyeVector from the lookat matrix. But I have some limits. If I exceed those limits the scene will dissapear. Do you have any ideas to solve this? I dont want limits for zoom.


Solution

  • There's only so much help I can give without seeing your full code, but have you checked the clipping planes? That sounds like a textbook case of having a clipping plane too close; check your perspective matrix.

    When you call Matrix4.CreatePerspectiveFieldOfView, increase depthFar, that should fix your issue.