Ron's picture

Drawing order of shapes incorrect

I have an odd thing happening when I draw out different shapes and then rotate the scene. When the shapes are redrawn, they are drawn in the order they were created. So even tho I have rotated the image by say, 160 degrees, the shape that should be in front of all other shapes are instead drawn behind the shapes. They are scaled as bigger because they are closer to the camera, just drawn behind the other shapes.

Camera Init

            location = new Vector3(0f, -150f, -2000f);
            ObjectSelection(new Devices.Camera(htDefaultSettings, location), eACTION.CREATE);
            cameraMatrix = new Camera(htDefaultSettings, location);
            rotation = new Vector3(55,0,45);
            cameraMatrix.Bank = rotation.X;
            cameraMatrix.Heading = rotation.Y;
            cameraMatrix.Attitude = rotation.Z;

Redraw

        private void Render()
        {
            if (cameraMatrix == null) return;
            try
            {
                GL.MatrixMode(MatrixMode.Modelview);
                GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                Matrix4 m = cameraMatrix.Transformation;
                GL.LoadMatrix(ref m);
                cameraMatrix.Transformation = m;
 
                DrawGrid();
                ICollection vPrimitiveCollection = htObjects.Values;
                foreach (object o in vPrimitiveCollection)
                {
                    GL.PushMatrix();
                    if (o is Primitive.Box)             { DrawBox((Primitive.Box)o); }
                    else if (o is Primitive.Circle)     { DrawCircle((Primitive.Circle)o); }
                    else if (o is Primitive.Curve)      { DrawCurve(o); }
                    else if (o is Primitive.Font)       { DrawFont(o); }
                    else if (o is Primitive.Line)       { DrawLine(o); }
                    else if (o is Primitive.Plane)      { DrawPlane((Primitive.Plane)o); }
                    else if (o is Primitive.Sphere)     { DrawSphere((Primitive.Sphere)o); }
                    else if (o is Primitive.Torus)      { DrawTorus(o); }
                    else if (o is Primitive.Triangle)   { DrawTriangle(o); }
                    else if (o is Primitive.Tube)       { DrawTube((Primitive.Tube)o); }
                    GL.PopMatrix();
                }
            }
            catch (Exception em) { MessageBox.Show(em.Message); }
            glControl.SwapBuffers();
        }

Box Object

        private void DrawBox(Primitive.Box p)
        {
            GL.Translate(p.TranslationX, p.TranslationY, p.TranslationZ);
            GL.Rotate(p.Heading, Vector3.UnitY);
            GL.Rotate(p.Attitude, Vector3.UnitZ);
            GL.Rotate(p.Bank, Vector3.UnitX);
            GL.Begin(BeginMode.Quads);
            for (int i = 0; i < p.Verticies.Length; i++)
            {
                GL.Color4(p.Color[i / 12]);
                GL.Vertex3(p.Verticies[i], p.Verticies[++i], p.Verticies[++i]);
                GL.Vertex3(p.Verticies[++i], p.Verticies[++i], p.Verticies[++i]);
                GL.Vertex3(p.Verticies[++i], p.Verticies[++i], p.Verticies[++i]);
                GL.Vertex3(p.Verticies[++i], p.Verticies[++i], p.Verticies[++i]);
            }
            GL.End();
            GL.Begin(BeginMode.Lines);
            for (int i = 0; i < p.Verticies.Length; i++)
            {
                GL.Color4(Color.Black);
                GL.Vertex3(p.Verticies[i], p.Verticies[++i], p.Verticies[++i]);
                GL.Vertex3(p.Verticies[++i], p.Verticies[++i], p.Verticies[++i]);
                GL.Vertex3(p.Verticies[++i], p.Verticies[++i], p.Verticies[++i]);
                GL.Vertex3(p.Verticies[++i], p.Verticies[++i], p.Verticies[++i]);
            }
            GL.End();
            if (oCurrentSelection == htObjects[p]) { DrawObjectCoordinate(p.Transformation); }
        }

Keyboard Nav Control

        private void glControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (!bGLControlLoaded) return;
            if (sender == null) return;
            try
            {   // Camera Control
                #region Pan Left/Right
                if (e.KeyCode == Keys.A)
                {
                    location.X -= 5f;
                }
                if (e.KeyCode == Keys.D)
                {
                    location.X += 5f;
                }
                #endregion
                #region Pan Up/Down
                if (e.KeyCode == Keys.W)
                {
                    location.Y += 5f;
                }
                if (e.KeyCode == Keys.S)
                {
                    location.Y -= 5f;
                }
                #endregion
                #region Zoom In/Out
                if (e.KeyCode == Keys.Q)
                {
                    location.Z += 10f;
                }
                if (e.KeyCode == Keys.Z)
                {
                    location.Z -= 10f;
                }
                #endregion
                #region Rotate Around Y
                if (e.KeyCode == Keys.R)
                {
                    rotation.X -= 2f;
                }
                if (e.KeyCode == Keys.T)
                {
                    rotation.X += 2f;
                }
                #endregion
                #region Rotate Around X
                if (e.KeyCode == Keys.F)
                {
                    rotation.Y -= 2f;
                }
                if (e.KeyCode == Keys.G)
                {
                    rotation.Y += 2f;
                }
                #endregion
                #region Rotate Around Z
                if (e.KeyCode == Keys.V)
                {
                    rotation.Z -= 2f;
                }
                if (e.KeyCode == Keys.B)
                {
                    rotation.Z += 2f;
                }
                #endregion
            }
            catch { }
 
            // Setup camera position in space
            cameraMatrix.TranslationX = location.X;
            cameraMatrix.TranslationY = location.Y;
            cameraMatrix.TranslationZ = location.Z; 
            cameraMatrix.Heading = rotation.Y;
            cameraMatrix.Attitude = rotation.Z;
            cameraMatrix.Bank = rotation.X;
            glControl.Invalidate();
 
        }

So the camera is init first. Then different shape objects are added to the scene. Then the keyboard keys are used to rotate the image. Then the render will redraw the objects in the scene.

But not able to see why the images do not redraw correctly as scene is rotated.

Any ideas?

Thanks!
Ron Lindsey


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Have you enabled depth testing?

GL.Enable(EnableCap.DepthTest);
Ron's picture

wow... how silly do I feel now! I did not even know about that.

Works like a champ!

Thank you for your reply.

Ron Lindsey