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

  1. ' Star field demo 5
  2. '
  3. ' Different coloured stars
  4.  
  5. const maxStars = 500
  6.  
  7. dim stars#(maxStars)(2), starCols(maxStars)(2)
  8. dim i, texture
  9.  
  10. ' Populate star field
  11. for i = 1 to maxStars
  12.     stars#(i) = vec3 (rnd () % 301 - 150, rnd () % 301 - 150, -i)
  13.     
  14.     ' Choose star colour.
  15.     ' The starCols array is an array of 3D INTEGER vectors (no # means integers).
  16.     ' We are going to use the byte version of glColor. With the byte version, each
  17.     ' colour component ranges from 0 (no intensity) to 255 (full intensity)
  18.     starCols(i)(0) = rnd () % 256
  19.     starCols(i)(1) = rnd () % 256
  20.     starCols(i)(2) = rnd () % 256
  21. next
  22.  
  23. ' Setup OpenGL fog.
  24. glEnable (GL_FOG)
  25. glFogi (GL_FOG_MODE, GL_LINEAR)             ' Objects fade out linearly
  26. glFogf (GL_FOG_END, maxStars)               ' Objects past this distance are totally faded
  27. glFogf (GL_FOG_START, 0)                    ' Objects before this distance are totally un-faded
  28. glFogfv (GL_FOG_COLOR, vec3 (0, 0, 0))      ' Fog colour = black
  29.  
  30. ' Load star texture.
  31. texture = LoadTexture ("data\star.bmp")
  32.  
  33. ' Enable texture mapping
  34. glEnable (GL_TEXTURE_2D)
  35.  
  36. ' Main loop
  37. while true    
  38.     glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)    
  39.     glBindTexture (GL_TEXTURE_2D, texture)
  40.  
  41.     for i = 1 to maxStars
  42.     
  43.         ' Move the star forward, by adding 1 to Z
  44.         stars#(i) = stars#(i) + vec3 (0, 0, 1)
  45.  
  46.         ' If the Z goes positive (behind the screen), move it to the back again.
  47.         if stars#(i)(2) >= 0 then stars#(i)(2) = -maxStars endif
  48.         
  49.         ' Draw star
  50.         glBegin (GL_QUADS)
  51.         
  52.             ' Set the star's colour.
  53.             glColor3bv (starCols (i))
  54.             ' Note: Again, the last 3 characters tell us what version of glColor
  55.             ' we are using:
  56.             '   3 = 3 element vector
  57.             '   b = Byte values
  58.             '   v = Array            
  59.             
  60.             glTexCoord2f (0, 1)                         ' Top left
  61.             glVertex3fv (stars#(i) + vec3(-1, 1, 0))
  62.  
  63.             glTexCoord2f (0, 0)                         ' Bottom left
  64.             glVertex3fv (stars#(i) + vec3(-1,-1, 0))
  65.             
  66.             glTexCoord2f (1, 0)                         ' Bottom right
  67.             glVertex3fv (stars#(i) + vec3( 1,-1, 0))
  68.  
  69.             glTexCoord2f (1, 1)                         ' Top right
  70.             glVertex3fv (stars#(i) + vec3( 1, 1, 0))
  71.         glEnd ()
  72.     next
  73.     SwapBuffers ()
  74.     WaitTimer (20)
  75. wend
  76.