How to translate along Z axis in OpenTK

Posted by JeremyJAlpha on Game Development See other posts from Game Development or by JeremyJAlpha
Published on 2014-05-12T15:44:02Z Indexed on 2014/06/05 3:41 UTC
Read the original article Hit count: 286

Filed under:
|
|
|

I am playing around with an OpenGL sample application I downloaded for Xamarin-Android. The sample application produces a rotating colored cube I would simply like to edit it so that the rotating cube is translated along the Z axis and disappears into the distance.

I modified the code by:

  • adding an cumulative variable to store my Z distance,
  • adding GL.Enable(All.DepthBufferBit) - unsure if I put it in the right place,
  • adding GL.Translate(0.0f, 0.0f, Depth) - before the rotate functions,

Result: cube rotates a couple of times then disappears, it seems to be getting clipped out of the frustum.

So my question is what is the correct way to use and initialize the Z buffer and get the cube to travel along the Z axis? I am sure I am missing some function calls but am unsure of what they are and where to put them.

I apologise in advance as this is very basic stuff but am still learning :P, I would appreciate it if anyone could show me the best way to get the cube to still rotate but to also move along the Z axis. I have commented all my modifications in the code:

    // This gets called when the drawing surface is ready
    protected override void OnLoad (EventArgs e)
    {
        // this call is optional, and meant to raise delegates
        // in case any are registered
        base.OnLoad (e);

        // UpdateFrame and RenderFrame are called
        // by the render loop. This is takes effect
        // when we use 'Run ()', like below
        UpdateFrame += delegate (object sender, FrameEventArgs args) {
            // Rotate at a constant speed
            for (int i = 0; i < 3; i ++)
                rot [i] += (float) (rateOfRotationPS [i] * args.Time);
        };

        RenderFrame += delegate {
            RenderCube ();
        };

        GL.Enable(All.DepthBufferBit);          //Added by Noob
        GL.Enable(All.CullFace);
        GL.ShadeModel(All.Smooth);

        GL.Hint(All.PerspectiveCorrectionHint, All.Nicest);

        // Run the render loop
        Run (30);
    }


    void RenderCube ()
    {
        GL.Viewport(0, 0, viewportWidth, viewportHeight);

        GL.MatrixMode (All.Projection);
        GL.LoadIdentity ();     

        if ( viewportWidth > viewportHeight )
        {
            GL.Ortho(-1.5f, 1.5f, 1.0f, -1.0f, -1.0f, 1.0f);
        }
        else
        {
            GL.Ortho(-1.0f, 1.0f, -1.5f, 1.5f, -1.0f, 1.0f);
        }

        GL.MatrixMode (All.Modelview);
        GL.LoadIdentity ();
        Depth -= 0.02f;                          //Added by Noob
        GL.Translate(0.0f,0.0f,Depth);           //Added by Noob
        GL.Rotate (rot[0], 1.0f, 0.0f, 0.0f);
        GL.Rotate (rot[1], 0.0f, 1.0f, 0.0f);
        GL.Rotate (rot[2], 0.0f, 1.0f, 0.0f);

        GL.ClearColor (0, 0, 0, 1.0f);
        GL.Clear (ClearBufferMask.ColorBufferBit);

        GL.VertexPointer(3, All.Float, 0, cube);
        GL.EnableClientState (All.VertexArray);
        GL.ColorPointer (4, All.Float, 0, cubeColors);
        GL.EnableClientState (All.ColorArray);
        GL.DrawElements(All.Triangles, 36, All.UnsignedByte, triangles);

        SwapBuffers ();
    }

© Game Development or respective owner

Related posts about opengl

Related posts about c#