Class: Quake::Renderer::GLSky

Inherits:
Object
  • Object
show all
Defined in:
lib/quake/renderer/gl_sky.rb

Overview

Renders Quake sky using the two-layer scrolling technique. Sky textures are 256x128, split into:

- Left half (128x128): solid background layer
- Right half (128x128): alpha foreground layer (index 0 = transparent)

Instance Method Summary collapse

Constructor Details

#initialize(level, palette, texture_manager) ⇒ GLSky

Returns a new instance of GLSky.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/quake/renderer/gl_sky.rb', line 12

def initialize(level, palette, texture_manager)
  @level = level
  @palette = palette
  @texture_manager = texture_manager
  @sky_surfaces = []
  @solid_tex = nil
  @alpha_tex = nil
  @time = 0.0

  find_sky_texture
  precompute_sky_surfaces
end

Instance Method Details

#render(camera, visible_face_set: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/quake/renderer/gl_sky.rb', line 29

def render(camera, visible_face_set: nil)
  return if @sky_surfaces.empty? || @solid_tex.nil?

  # Filter to PVS-visible sky surfaces only. Without this, distant
  # sky polys (e.g. the level's outdoor exit area) draw even when the
  # camera is enclosed in a cave - and underwater they alpha-blend
  # through translucent water as bright cloud shapes.
  visible_surfaces = if visible_face_set
                       @sky_surfaces.select { |s| visible_face_set.include?(s[:face_index]) }
                     else
                       @sky_surfaces
                     end
  return if visible_surfaces.empty?

  # Render solid background layer
  GL.BindTexture(GL::TEXTURE_2D, @solid_tex)
  speed_scale = @time * 8.0
  speed_scale -= speed_scale.floor & ~127

  visible_surfaces.each do |surf|
    GL.Begin(GL::TRIANGLE_FAN)
    surf[:vertices].each do |v|
      s, t = sky_texcoord(v, camera.position, speed_scale)
      GL.TexCoord2f(s, t)
      GL.Vertex3f(v.x, v.y, v.z)
    end
    GL.End
  end

  # Render alpha foreground layer with blending
  GL.Enable(GL::BLEND)
  GL.BlendFunc(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA)
  GL.BindTexture(GL::TEXTURE_2D, @alpha_tex)
  speed_scale2 = @time * 16.0
  speed_scale2 -= speed_scale2.floor & ~127

  visible_surfaces.each do |surf|
    GL.Begin(GL::TRIANGLE_FAN)
    surf[:vertices].each do |v|
      s, t = sky_texcoord(v, camera.position, speed_scale2)
      GL.TexCoord2f(s, t)
      GL.Vertex3f(v.x, v.y, v.z)
    end
    GL.End
  end

  GL.Disable(GL::BLEND)
end

#update(dt) ⇒ Object



25
26
27
# File 'lib/quake/renderer/gl_sky.rb', line 25

def update(dt)
  @time += dt
end