0

I am trying to correct the translation and rotation behavior of my model after I rotate the image. This seems to be a common issue. I've seen this asked before, but I have not seen it answered in such a way that I understand how to apply it to my code. I simply want the translation and rotation behavior to remain how it is before I rotate the image. After the first rotation the translations and rotations are forever skewed. Also, I am very green with OpenGL so please talk slowly.

Private Sub GlControl1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles GlControl1.MouseMove
    If _MouseButtonLeft Then
        '
        ' get new position of mouse
        _XposEnd = e.X - _Xpos
        _YposEnd = _Ypos - e.Y
        '
        ' reset the mouse start position
        _Xpos = e.X
        _Ypos = e.Y
        '
        ' change projection
        GL.MatrixMode(MatrixMode.Projection)
        GL.Translate(_XposEnd * _MouseSpeed, _YposEnd * _MouseSpeed, 0)
        GlControl1.Invalidate()
        '
    End If
    '
    If _MouseButtonRight Then
        '
        ' get new position of mouse
        _XposEnd = e.X - _Xpos
        _YposEnd = _Ypos - e.Y
        '
        ' reset the mouse start position
        _Xpos = e.X
        _Ypos = e.Y
        '
        ' change projection
        GL.MatrixMode(MatrixMode.Projection)
        GL.Rotate(_XposEnd * _MouseSpeed, 1, 0, 0)
        GL.Rotate(_YposEnd * _MouseSpeed, 0, 1, 0)
        GlControl1.Invalidate()
        '
    End If
End Sub

EDIT 1:

My draw loop is:

    GL.Clear(ClearBufferMask.ColorBufferBit)
    GL.Clear(ClearBufferMask.DepthBufferBit)
    GL.MatrixMode(MatrixMode.Modelview)
    GL.LoadIdentity()
    drawStuff()
    GlControl1.SwapBuffers()

Projection Matrix: Let me back up for a second. I am drawing a CAD model with thousands of vertices in 3D space [Vertex3]. I am viewing them in 2D space [Ortho]. I modify the projection matrix to view the model from different perspectives. I thought that would be easier than redrawing the entire model and transforming all the vertices. In my mind moving the camera is easier than recalculating the position of thousands of vertices. I figured that was standard. If not, I'm all ears.

twegner
  • 443
  • 1
  • 5
  • 21
  • Hey, do you use glLoadIdentity anywhere in your draw loop? I assume you don't, since you want the glRotate to stack. If this is the problem, Ill elaborate. Also, I find it very strange that you want to modify your Projection matrix, can you explain? – Henk De Boer Apr 13 '16 at 20:28

1 Answers1

1

From your code I can't tell exactly what behavior your expecting but I suspect you're trying to continuously read in mouse input and augment the "camera".

With the version of GL you're using the model and view matrices are combined into the ModelView matrix. This (in my opinion) makes things more difficult to understand when starting out.

If they were separated you could transform your object and camera independently using the model and view matrices respectively. Since they're combined you'll need to "save" the state of the ModelView matrix prior to making your view transformations.

This is where glPushMatrix and glPopMatrix come in handy.

Think of a typical stack. By pushing a new matrix onto the stack you inherit everything below (the current state of the matrix) but subsequent transformations will be isolated to this new matrix. Once you've made your transformations and used the matrix (e.g. to render), you can then pop that matrix back off the stack which will return you to the original matrix (that contains your model transformations). You'll do this each time your program loops.

I've not written VB.NET or fixed-function (pre-GL3) code in a very long time so bear with me:

GL.MatrixMode(MatrixMode.ModelView)
GL.PushMatrix()
GL.Rotate(viewXRotation, 1, 0, 0)
GL.Rotate(viewYRotation, 0, 1, 0)
GL.Rotate(viewZRotation, 0, 0, 1)
... draw your scene ...
GL.PopMatrix()

Since I see you using the Projection matrix, here is a little extra info with regard to both matrices and how they interact.

The Projection matrix is used to transform view coordinates (which is what local coordinates are transformed into when the ModelView matrix is applied) into clip coordinates (normalized device coordinates).

Here is an illustration from learnopengl.com: OpenGL's coordinate systems

Community
  • 1
  • 1
Exide
  • 859
  • 8
  • 24