class GLBackground

The only really new class here. Draws a scrolling, repeating texture with a randomized height map.

Constants

POINTS_X

Height map size

POINTS_Y
SCROLLING_SPEED

TEMP USING THIS, CANNOT FIND SCROLLING SPEED

SCROLLS_PER_STEP

Scrolling speed

Public Class Methods

new() click to toggle source
# File gosu-test/gosu-test.rb, line 80
def initialize
  @image = Gosu::Image.new("#{MEDIA_DIRECTORY}/earth.png", :tileable => true)
  @scrolls = 0
  @height_map = Array.new(POINTS_Y) { Array.new(POINTS_X) { rand } }
end

Public Instance Methods

draw(z) click to toggle source
# File gosu-test/gosu-test.rb, line 95
def draw(z)
  # gl will execute the given block in a clean OpenGL environment, then reset
  # everything so Gosu's rendering can take place again.
  Gosu.gl(z) { exec_gl }
end
scroll() click to toggle source
# File gosu-test/gosu-test.rb, line 86
def scroll
  @scrolls += 1
  if @scrolls == SCROLLS_PER_STEP
    @scrolls = 0
    @height_map.shift
    @height_map.push Array.new(POINTS_X) { rand }
  end
end

Private Instance Methods

exec_gl() click to toggle source
# File gosu-test/gosu-test.rb, line 105
def exec_gl
  glClearColor(0.0, 0.2, 0.5, 1.0)
  glClearDepth(0)
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  
  # Get the name of the OpenGL texture the Image resides on, and the
  # u/v coordinates of the rect it occupies.
  # gl_tex_info can return nil if the image was too large to fit onto
  # a single OpenGL texture and was internally split up.
  info = @image.gl_tex_info
  return unless info

  # Pretty straightforward OpenGL code.
  
  glDepthFunc(GL_GEQUAL)
  glEnable(GL_DEPTH_TEST)
  glEnable(GL_BLEND)

  glMatrixMode(GL_PROJECTION)
  glLoadIdentity
  glFrustum(-0.10, 0.10, -0.075, 0.075, 1, 100)

  glMatrixMode(GL_MODELVIEW)
  glLoadIdentity
  glTranslate(0, 0, -4)

  glEnable(GL_TEXTURE_2D)
  glBindTexture(GL_TEXTURE_2D, info.tex_name)
  
  offs_y = 1.0 * @scrolls / SCROLLS_PER_STEP
  
  0.upto(POINTS_Y - 2) do |y|
    0.upto(POINTS_X - 2) do |x|
      glBegin(GL_TRIANGLE_STRIP)
        z = @height_map[y][x]
        glColor4d(1, 1, 1, z)
        glTexCoord2d(info.left, info.top)
        glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)

        z = @height_map[y+1][x]
        glColor4d(1, 1, 1, z)
        glTexCoord2d(info.left, info.bottom)
        glVertex3d(-0.5 + (x - 0.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
      
        z = @height_map[y][x + 1]
        glColor4d(1, 1, 1, z)
        glTexCoord2d(info.right, info.top)
        glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y - 0.0) / (POINTS_Y-2), z)

        z = @height_map[y+1][x + 1]
        glColor4d(1, 1, 1, z)
        glTexCoord2d(info.right, info.bottom)
        glVertex3d(-0.5 + (x + 1.0) / (POINTS_X-1), -0.5 + (y - offs_y + 1.0) / (POINTS_Y-2), z)
      glEnd
    end
  end
end