home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Camera.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  4.4 KB  |  113 lines

  1. ' Camera demo
  2. '
  3. ' Demonstrates:
  4. '   Storing a camera position and direction in a matrix
  5. '   Moving the camera around (using a basic aeroplane like mechanism)
  6.  
  7. ' Camera variables
  8. dim camera#(3)(3)               ' Camera position and direction 
  9. dim speed#                      ' Camera speed
  10.  
  11. ' Working variables
  12. dim x, y, temp#(3), xaxis#, yaxis#
  13.  
  14. ' Setup camera
  15. camera# = MatrixTranslate (0, 5, 0)
  16. speed# = 0.02 
  17.                
  18. TextMode (TEXT_OVERLAID)
  19. color (200, 200, 255)
  20. print "Use arrow keys to fly camera"
  21.  
  22. ' Main loop
  23. while true
  24.  
  25.     ' Render frame
  26.     glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
  27.     
  28.     ' Render from the camera's viewpoint
  29.     ' Notes:
  30.     ' * The camera stores the position and direction of the 
  31.     '   CAMERA relative to the WORLD.
  32.     ' * We want to render the WORLD relative to the CAMERA, 
  33.     '   therefore we need to invert the matrix.
  34.     ' * The camera is built from rotations and translations
  35.     '   therefore we can use RTInvert to correctly invert 
  36.     '   it.
  37.     glLoadMatrixf (RTInvert (camera#))
  38.     
  39.     ' Draw some quads
  40.     for x = -10 to 10
  41.         for y = -10 to 10
  42.             glPushMatrix ()
  43.             glTranslatef (x * 3, 0, y * 3)
  44.             glBegin (GL_QUADS)
  45.                 glColor3f ( 1, 0, 0): glVertex2f ( 1, 3)
  46.                 glColor3f ( 0, 1, 0): glVertex2f (-1, 3)
  47.                 glColor3f ( 0, 0, 1): glVertex2f (-1, 0)
  48.                 glColor3f ( 1, 1, 1): glVertex2f ( 1, 0)
  49.             glEnd ()
  50.             glPopMatrix ()
  51.         next
  52.     next
  53.     DrawText ()
  54.     SwapBuffers ()    
  55.     
  56.     ' Update the camera position
  57.     while SyncTimer (10)
  58.     
  59.         ' Move the camera forward.
  60.  
  61.         ' We can do this by noticing that camera# is a rotation and translation matrix.
  62.         ' Therefore if we split the matrix into columns, we know that the first three 
  63.         ' compose the basis axes of the rotation component.
  64.         ' To put it in simpler terms:
  65.         '   camera#(0) = The camera's LEFT vector
  66.         '   camera#(1) = The camera's UP vector
  67.         '   camera#(2) = The camera's BACKWARD vector
  68.         ' We also know that the right hand column vector camera#(3) contains the
  69.         ' camera's POSITION.
  70.         
  71.         ' Therefore to move the camera forward, we just need to add the camera's 
  72.         ' FORWARD vector (scaled by the current speed) to the camera's POSITION vector.
  73.         ' (Note: We can get the camera's FORWARD vector by negating it's BACKWARD vector
  74.         '  i.e by calculating -camera# (2))
  75.         camera# (3) = camera# (3) + -camera# (2) * speed#    
  76.         
  77.         ' Turn the camera
  78.         
  79.         ' We do this by multiplying the camera matrix by a rotation matrix.
  80.         ' The rotation matrix is multiplied on the RIGHT HAND SIDE of the camera matrix.
  81.         ' Therefore the rotation happens in CAMERA SPACE (i.e relative to the camera).
  82.         
  83.         ' If we rotate around the Z axis (points out of the screen) the camera will bank
  84.         ' If we rotate around the X axis (points left) the camera will pitch.
  85.  
  86.         xaxis# = -Mouse_XD () * 30
  87.         yaxis# =  Mouse_YD () * 30
  88.         if ScanKeyDown (VK_LEFT)  then xaxis# =  1: endif
  89.         if ScanKeyDown (VK_RIGHT) then xaxis# = -1: endif
  90.         if ScanKeyDown (VK_UP)    then yaxis# = -1: endif
  91.         if ScanKeyDown (VK_DOWN)  then yaxis# =  1: endif
  92.         camera# = camera# * MatrixRotateZ (xaxis#) * MatrixRotateX (yaxis#) 
  93.         
  94.         ' Turn the camera 
  95.         temp# = camera#(3)
  96.         camera#(3) = vec4 (0, 0, 0, 1)
  97.         camera# = MatrixRotateY (camera# (0)(1)) * camera#
  98.         camera#(3) = temp#
  99.         
  100.         ' Orthonormalize the camera matrix.
  101.         
  102.         ' Everytime we multiply the matrix by a rotation, we introduce rounding error.
  103.         ' This rounding error is insignificant for a reasonable number of transformations.
  104.         ' However, as we are updating the camera 100 times per second the camera matrix
  105.         ' will quickly become the result of 1000s of transformations, and eventually the
  106.         ' rounding error can build up to the point where the matrix collapses or explodes.
  107.         
  108.         ' Orthonormalize performs a sequence of cross-products and vector normalizations
  109.         ' to ensure the rotation component of the matrix is orthonormal, and minimize any
  110.         ' accumulated rounding error.
  111.         camera# = Orthonormalize (camera#)
  112.     wend
  113. wend