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

  1. ' Star field demo 4
  2. '
  3. ' Using textured stars
  4.  
  5. const maxStars = 500
  6.  
  7. dim stars#(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. next
  14.  
  15. ' Setup OpenGL fog.
  16. glEnable (GL_FOG)
  17. glFogi (GL_FOG_MODE, GL_LINEAR)             ' Objects fade out linearly
  18. glFogf (GL_FOG_END, maxStars)               ' Objects past this distance are totally faded
  19. glFogf (GL_FOG_START, 0)                    ' Objects before this distance are totally un-faded
  20. glFogfv (GL_FOG_COLOR, vec3 (0, 0, 0))      ' Fog colour = black
  21.  
  22. ' Load star texture.
  23. ' This will return an OpenGL texture handle, which is a number that identifies
  24. ' the texture.
  25. texture = LoadTexture ("data\star.bmp")
  26. ' Note: 
  27. '   LoadTexture is actually a Basic4GL function, not an OpenGL function as such,
  28. '   (although it uses the OpenGL texture allocation and loading functions internally).
  29.  
  30. ' Enable texture mapping
  31. glEnable (GL_TEXTURE_2D)
  32.  
  33. ' Note: We are NOT disabling the depth buffer, because we want it to ensure that
  34. ' nearer stars are correctly drawn INFRONT of further stars.
  35. ' Because we are using the depth buffer, we will need to clear it each frame
  36.  
  37. ' Main loop
  38. while true    
  39.     glClear (GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
  40.     ' Clear the colour buffer AND the depth buffer
  41.     ' ("or" combines the two masks into a single bitmask that specifies BOTH buffers)
  42.     
  43.     ' "Bind" the star texture. This tells OpenGL that we are drawing with this texture.
  44.     glBindTexture (GL_TEXTURE_2D, texture)
  45.  
  46.     for i = 1 to maxStars
  47.     
  48.         ' Move the star forward, by adding 1 to Z
  49.         stars#(i) = stars#(i) + vec3 (0, 0, 1)
  50.  
  51.         ' If the Z goes positive (behind the screen), move it to the back again.
  52.         if stars#(i)(2) >= 0 then stars#(i)(2) = -maxStars endif
  53.         
  54.         ' This time we're drawing a quad (a four sided object, in our case a square)
  55.         ' Each corner will correspond to a corner of the texture.
  56.         glBegin (GL_QUADS)
  57.             ' We need to enter 4 vertices, with texture coordinates for each.
  58.             ' A texture coordinate is a 2D vector, where (0, 0) is the bottom
  59.             ' left hand of the texture, and (1, 1) is the top right.
  60.             ' We must specify the texture coordinate FIRST, then the corresponding
  61.             ' vertex
  62.             
  63.             glTexCoord2f (0, 1)                         ' Top left
  64.             glVertex3fv (stars#(i) + vec3(-1, 1, 0))
  65.  
  66.             glTexCoord2f (0, 0)                         ' Bottom left
  67.             glVertex3fv (stars#(i) + vec3(-1,-1, 0))
  68.             
  69.             glTexCoord2f (1, 0)                         ' Bottom right
  70.             glVertex3fv (stars#(i) + vec3( 1,-1, 0))
  71.  
  72.             glTexCoord2f (1, 1)                         ' Top right
  73.             glVertex3fv (stars#(i) + vec3( 1, 1, 0))
  74.         glEnd ()
  75.     next
  76.     SwapBuffers ()
  77.     WaitTimer (20)
  78. wend
  79.