Search code examples
openglcameraclippingdepthopentk

OpenGL Clipping, depth and camera


Having trouble with clipping/view not sure

Im simply trying to rotate and scale a rectangular box (of lines not quads) in a windows form (using OpenTK) This is simple enough but When i zoom/scale parts of the line get clipped when they shouldn't

Tried playing with perspective and depth range etc as well as lookat, scaling drawing instead of moving camera etc. But i can't seem to get the behavior I want.

Video of problem: http://www.youtube.com/watch?v=sDV4_LKOgVc&feature=youtu.be

Private Sub GlControl1_Paint(sender As Object, e As PaintEventArgs) Handles GlControl1.Paint
    If (_STARTED = False) Then Return

    'Set Up
    GL.Clear(ClearBufferMask.ColorBufferBit)
    GL.Clear(ClearBufferMask.DepthBufferBit)


    Dim eye = New Vector3(cam_x, cam_y, cam_z)
    Dim lookat = New Vector3(cam_x - Pan_X / GlControl1.Width, cam_y - pan_Y / GlControl1.Height, cam_z + look_z)
    Dim camera = Matrix4.LookAt(eye, lookat, Vector3.UnitY)

    TextBox1.Text = cam_x.ToString + ", " + cam_y.ToString + ", " + cam_z.ToString
    GL.MatrixMode(MatrixMode.Modelview)

    GL.LoadIdentity()
    GL.LoadMatrix(camera)


    'Draw Items

    'Perform panning action
    'Applies to all items in GL window
    GL.Translate(Pan_X / GlControl1.Width, pan_Y / GlControl1.Height, 0.0)

    'Initial View Angle
    'GL.Rotate(45, 1.0, 1.0, 0.0)

    If show_axes Then draw_axes()


    GL.PushMatrix() 'Save matrix

    'Perform rotations
    GL.Rotate(CType(_Part.XRotation, Single), 1.0F, 0.0F, 0.0F)
    GL.Rotate(CType(_Part.YRotation, Single), 0.0F, 1.0F, 0.0F)
    GL.Rotate(CType(_Part.ZRotation, Single), 0.0F, 0.0F, 1.0F)

    'Resize
    'GL.Scale(Zoom, Zoom, Zoom)

    GL.PushMatrix() ' Save Matrix for center translation
    GL.Translate(-_Part.Length / 2, -_Part.Thickness / 2, _Part.Width / 2)   'Center Object

    'Draw the box
    draw_object(CSng(_Part.Length), CSng(_Part.Width), CSng(_Part.Thickness))


    GL.PopMatrix()


    GlControl1.SwapBuffers()

End Sub

    Private Sub GlControl1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
    If e.Delta > 0 Then
        'Zoom = CSng(Zoom + Zoom_inc)
        cam_z -= cam_inc
    Else
        'Zoom = CSng(Zoom - Zoom_inc)
        cam_z += cam_inc
    End If
    GlControl1.Invalidate()
End Sub

Solution

  • I figured it out. I had it right but OpenTK wants you to declare the perspective like so

    Dim perspective1 As Matrix4 = OpenTK.Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, _CSng((GlControl1.Width) / (GlControl1.Height)), 0.1, 1000)

    GL.LoadMatrix(perspective1)

    It seems odd. Using MathHelper.PiOver4 instead of 45.0f solved the problem