Panning in SharpGL distorts image

175 Views Asked by At

I have this code in SharpGL where zoom and pan were implemented. This is de draw method:

private void OpenGLDraw(object sender, SharpGL.WPF.OpenGLRoutedEventArgs args)
{
    // Enable finish button
    if (openFileOk && dispCBOk) { finish_btn.IsEnabled = true; }
    else { finish_btn.IsEnabled = false; }

    //  Get the OpenGL object
    OpenGL gl = GLControl.OpenGL;

    //  Set The Material Color
    float[] glfMaterialColor = new float[] { 0.4f, 0.2f, 0.8f, 1.0f };

    float w = (float)GLControl.ActualWidth;
    float h = (float)GLControl.ActualHeight;

    //  Perspective projection
    gl.Viewport(0, 0, (int)w, (int)h);

    gl.Ortho(0, w, 0, h, -100, 100);

    //  Clear The Screen And The Depth Buffer
    gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
    gl.Material(OpenGL.GL_FRONT_AND_BACK, OpenGL.GL_AMBIENT_AND_DIFFUSE, glfMaterialColor);

    gl.MatrixMode(MatrixMode.Modelview);
    gl.LoadIdentity();

    gl.Scale(scale,scale,1);

    //  Camera Position
    gl.LookAt(_position[0], _position[1], _position[2],
        _viewPoint[0], _viewPoint[1], _viewPoint[2],
        _upVector[0], _upVector[1], _upVector[2]);

    // Draw the helix
    float[][] points = vertexes.ToArray();
    Helix(points.Length, points);
        }

What I have here is a perspective projection using Viewport (the ortho projection and scale are used for zooming). This is the code for panning whatever is drawn on the screen:

//  pan
//  y axis
//  Up
if (keyCode == 1)
{
    _viewPoint[1] += _moveDistance;
    _position[1] += _moveDistance;
}
//  Down
else if (keyCode == 2)
{
    _viewPoint[1] += -_moveDistance;
    _position[1] += -_moveDistance;
}

//  x axis
//  Left
else if (keyCode == 3)
{
    _viewPoint[2] += _moveDistance;
    _position[2] += _moveDistance;
}
//  Right
else if (keyCode == 4)
{
    _viewPoint[2] += -_moveDistance;
    _position[2] += -_moveDistance;
}

The thing is that when I move too much to any one of the sides (up, down, left, right), the drawing gets really distorted. I didn't notice it at first because I was working with the image very zoomed in and couldn't see the difference, but now I see that the distortion always happens, it's just more intense the more you move away from the image. All I want to know is why this happens, and if possible, a solution for it, thank you.

Here is an example of what happens:

Normal view

Distorted view

0

There are 0 best solutions below