Nav3D: Class for basic 3D navigation (Ruby code)

Hi, in my learning exercises with Ruby and OpenGL, I coded this Nav3D class to provide some basic 3D functionality to navigate in the 3D space “box”. If you are interested I can post the theory behind it. Improvements suggestion are welcome.


The file with the Ruby class can be downloaded from:

Basic functionality:

d key = swap “on-the-fly” between Ortho and Perspective and vice-versa.
z,x keys = increase or decrease the perspective angle.
c,v keys = increase or decrease the deep of the “box”.
a key = show the box dimensions (just for curiosity).

Drag mouse around the screen with:

  • Left-button down = Drag on X and Y.
  • Right-button down = Rotation around X and Y, regard the center of the box.
  • Right-button down and CTRL-key = Rotation around Z, regard the center of the box.
  • Right-button down and Alt-key = Zoom


  1. A small green point fixed in the center of the box.
  2. An inspection light that is always pointing from YOU to the center of the box.

To provide this functionality you need:

  1. include ( require ‘nav3D’ ) to call the file where is the Nav3D class.
  2. include a line to initiate the Nav3D class.
    obs: remove any reference to glOrtho() or glFrustum() because the Nav3D class will handle that.
  3. Replace your Reshape callback code to one that just call the Nav3D Reshape method.
  4. Include in your Keyboard callback code to call some Nav3D methods by keyboard.
    5,6. Replace or Include the mouse callbacks (MouseFunc and MotionFunc) to allow mouse manipulations.
  5. Include a RenderScene function (must be this name) with your render code.

More details to execute the changes:

  1. require ‘nav3D_1’

  2. myInit

$,5.0,0.0) # (Xm, Z, Tan) ( Tan = 0.0 = Orthogonal)

  1. Callback of glutReshapeFunc(Reshape)

Reshape = proc do |w, h|

  1. Callback of glutKeyboardFunc(keyboard)

keyboard = do |key, x, y|
case (key)
when ?d # Swap Ortho to Perspective and vice-versa
when ?z
when ?x
when ?c # Increase Z ( deep of the box)
when ?v # Decrease Z (deep of the box)
when ?a # key a - Show parameters

  1. Callback of glutMouseFunc(mouse)

mouse = lambda do |button,state,x,y|

  1. Callback of glutMotionFunc(motion)

motion = lambda do |x,y|
when GLUT_RIGHT_BUTTON; $nav3D.Rotate(x,y)
when GLUT_LEFT_BUTTON; $nav3D.Move(x,y)

  1. def RenderScene()
    your code here

Optional1: Box Central Green Point

  1. on RenderScene
    $nav3D.PtCentral() # green point in the center of the box

Optional2: Inspection Light

  1. on myInit
    $nav3D.SetInspectionLight(7,10.0) # (Light Number, distance)
  2. on RenderScene
    $nav3D.InspectionLight() # must be here to re-compute the “inspection” direction