Search code examples
openglmatrixrenderingopentkbullet

Unexpected flying boxes while trying to render


So for a quick sample in pictures:

This is normal: normal

this is after rotating 180 deg on either the X or Y axis: rotated

I don't see why this is happening at all. I'm using OpenTK to render a simple Bullet physics scene. The code is straight forward and it almost seems like there's something wrong in the way the matrix is handled. It's straight-forward render code:

        GL.PushMatrix();
        GL.MultMatrix(Body.MotionState.WorldTransform.ToArray());
        GL.Scale(HalfX * 2, HalfY * 2, HalfZ * 2);
        GL.Color4(Color4.Blue);
        GL.Begin(PrimitiveType.Lines);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 0, 10);
        GL.End();
        GL.Color4(Color4.Yellow);
        GL.Begin(PrimitiveType.Lines);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, 10, 0);
        GL.End();
        GL.Color4(Color4.Green);
        GL.Begin(PrimitiveType.Lines);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(10, 0, 0);
        GL.End();
        if (Body.ActivationState == ActivationState.ActiveTag)
        {
            GL.Color4(Color4.Blue);
        }
        else if (Mass == 0)
        {
            GL.Color4(Color4.Red);
        }
        else
        {
            GL.Color4(Color4.Green);
        }
        model.Draw();
        GL.PopMatrix();

I've tried breaking it down to it's components: the translation vector is fine, scaling is fine, rotating on the Z axis appears fine... it's when you add rotations on the X or Y axis that it starts flying. I have console output going: the box is at exactly 6.9999 on the Z axis in both images.

Where am I going wrong? What am I missing? How do I fix this?!


Solution

  • Okay so... PushAttrib(AttribMask.AllAttribBits); PushMatrix(); in my /TEXTFONT LOADING CODE/ fixed it. Somehow, some weird attribute set in the code that loads fonts to later render made GL.Rotate rotate around (0, 0, 1) instead of (0, 0, 0)... OpenGL sure is temperamental...

    So the lesson here is... never assume unrelated code is truly unrelated when dealing with OpenGL.