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

  1. ' Heightmap demo 5
  2. '
  3. ' Using a display list to speed things up
  4. '
  5. const heightScale# = 10 ' Height map scale factor
  6. const texScale# = 0.1   ' Number of textures per grid square
  7. dim xSize, ySize
  8. dim xMask, yMask        ' bitmasks to apply to X and Y coordinates
  9. dim file                ' File handle
  10. dim x, y                ' Working variables
  11. dim angle#              ' Viewing angle (used for animation)
  12. dim texture             ' OpenGL texture handle for our texture
  13. dim tx#, ty#            ' Texture coordinates
  14. dim a#(2), b#(2), c#(2), d#(2), norm#(2) ' Temporary vectors used for lighting
  15. dim intensity#
  16. dim displayList         ' Our display list handle
  17.  
  18. goto Start
  19.  
  20. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  21. ' Routines
  22. CheckError:
  23.     ' Check for file error
  24.     if FileError () <> "" then
  25.         print FileError ()
  26.         CloseFile (file)
  27.         end
  28.     endif
  29.     return
  30.  
  31. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  32. ' Main program stars here    
  33. Start:
  34.  
  35. ' Open file
  36. file = OpenFileRead ("Files\Hills01.dat")
  37. gosub CheckError
  38.  
  39. ' Read X and Y size
  40. xSize = Val (ReadLine (file))
  41. ySize = Val (ReadLine (file))
  42. gosub CheckError
  43.  
  44. ' Now that size is known, we can dim our 2D array
  45. dim h#(xSize - 1)(ySize - 1)        
  46. xMask = xSize - 1
  47. yMask = ySize - 1
  48.  
  49. for y = 0 to ySize - 1
  50.     for x = 0 to xSize - 1
  51.         h#(x)(y) = val (ReadText (file, true)) * heightScale#
  52.         gosub CheckError
  53.     next
  54. next          
  55. CloseFile (file)
  56.  
  57. ' Create light map
  58. dim lightSource#(2)
  59. lightSource# = vec3(0, -1, 0)           ' Light shines straight down.
  60. lightSource# = Normalize (lightSource#) ' Light source must be length 1
  61.  
  62. ' Create light map
  63. dim l# (xSize - 1)(ySize - 1)
  64. for x = 0 to xSize - 1
  65.     for y = 0 to ySize - 1
  66.         a# = vec3 (x,   h#(x)(y),                             y)
  67.         b# = vec3 (x+1, h#((x+1) and xMask)(y),               y)
  68.         c# = vec3 (x  , h#(x              )((y+1) and yMask), y+1)        
  69.         d# = vec3 (x+1, h#((x+1) and xMask)((y+1) and yMask), y+1)
  70.  
  71.         norm# = Normalize (CrossProduct (d# - a#, b# - c#))        
  72.  
  73.         intensity# = norm# * -lightSource#
  74.  
  75.         intensity# = intensity# * intensity# * intensity#
  76.         if intensity# < 0.2 then intensity# = 0.2 endif
  77.             
  78.         l#(x)(y) = intensity#
  79.     next
  80. next
  81.  
  82. ' Load texture file 
  83. texture = LoadMipmapTexture ("textures\00005.jpg")
  84.  
  85. ' Enable texture mapping, and bind our texture
  86. glEnable (GL_TEXTURE_2D)
  87. glBindTexture (GL_TEXTURE_2D, texture)
  88.  
  89. ' Create display list
  90. ' A dispay list is a list of OpenGL commands that we will be executing often.
  91. ' Instead of executing ALL the OpenGL commands one-by-one each time, we can
  92. ' place them all into a display list, and then tell OpenGL to run the display
  93. ' list.
  94.  
  95. ' This will usually speed things up, especially in situations like this one
  96. ' where our humble virtual machine struggles to output several thousand 
  97. ' vertex, colour and texture commands each frame.
  98.  
  99. ' Create our display list handle
  100. displayList = glGenLists (1)
  101.  
  102. ' Compile the display list.
  103. ' Instead of performing the drawing commands, OpenGL will save them into the
  104. ' display list.
  105. glNewList (displayList, GL_COMPILE)
  106.  
  107.     ' Draw the heightmap points
  108.     for x = 0 to xSize - 2
  109.         for y = 0 to ySize - 2
  110.         
  111.             ' Calculate the texture coordinates
  112.             tx# = x * texScale#
  113.             ty# = y * texScale#
  114.  
  115.             ' Draw a grid square.
  116.             ' Apply the lightmap, by setting the colour at each corner.
  117.             ' We will multiply the colour white (1,1,1) as a vector by the light
  118.             ' intensity. Then use the resulting vector as our colour coordinates
  119.             glBegin (GL_QUADS)
  120.                 intensity# = l#(x)(y)
  121.                 glColor3f (intensity#, intensity#, intensity#)
  122.                 glTexCoord2f (tx#            , ty#)
  123.                 glVertex3f (x  , h#(x  )(y  ), y  )
  124.                 
  125.                 intensity# = l#(x+1)(y)
  126.                 glColor3f (intensity#, intensity#, intensity#)
  127.                 glTexCoord2f (tx# + texScale#, ty#)
  128.                 glVertex3f (x+1, h#(x+1)(y  ), y  )
  129.  
  130.                 intensity# = l#(x+1)(y+1)
  131.                 glColor3f (intensity#, intensity#, intensity#)
  132.                 glTexCoord2f (tx# + texScale#, ty# + texScale#)
  133.                 glVertex3f (x+1, h#(x+1)(y+1), y+1)
  134.  
  135.                 intensity# = l#(x)(y+1)
  136.                 glColor3f (intensity#, intensity#, intensity#)
  137.                 glTexCoord2f (tx#            , ty# + texScale#)
  138.                 glVertex3f (x  , h#(x  )(y+1), y+1)
  139.             glEnd ()
  140.         next
  141.     next
  142. glEndList ()
  143. ' Now everytime we want to draw the entire grid, we can call glCallList (displayList)
  144.  
  145. while true
  146.     
  147.     ' Clear the screen
  148.     glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  149.         
  150.     ' Setup the transformations
  151.     glLoadIdentity ()                       ' Clear out any existing transformations
  152.     glTranslatef (0, 0, -50)                ' "push" map away
  153.     glRotatef (20, 1, 0, 0)                 ' Elevation rotation
  154.     glRotatef (angle#, 0, 1, 0)             ' Animation rotation
  155.     glTranslatef (-xSize/2, 0, -ySize/2)    ' Centre the map    
  156.  
  157.     ' Draw the heightmap points, using our display list
  158.     glCallList (displayList)
  159.  
  160.     ' Show the result
  161.     SwapBuffers ()
  162.     
  163.     ' Animation
  164.     while SyncTimer (10)
  165.         angle# = angle# + 0.4
  166.     wend
  167. wend