Class: Ashton::Lighting::LightSource

Inherits:
Object
  • Object
show all
Includes:
Mixins::VersionChecking
Defined in:
lib/ashton/lighting/light_source.rb

Overview

Based on Catalin Zima’s shader based dynamic shadows system. www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::VersionChecking

#check_opengl_extension, #check_opengl_version

Constructor Details

#initialize(x, y, z, radius, options = {}) ⇒ LightSource

Returns a new instance of LightSource.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ashton/lighting/light_source.rb', line 20

def initialize(x, y, z, radius, options = {})
  #check_opengl_extension PIXEL_BUFFER_EXTENSION

  @x, @y, @z, @radius = x, y, z, radius.to_i
  @color = options[:color] || Gosu::Color::WHITE

  @shadow_casters = Ashton::Texture.new width, height
  @shadow_map = Ashton::Texture.new 2, height
  @shadows = Ashton::Texture.new width, height
  @blurred = Ashton::Texture.new width, height

  load_shaders
end

Class Attribute Details

.blur_shaderObject

Returns the value of attribute blur_shader.



11
12
13
# File 'lib/ashton/lighting/light_source.rb', line 11

def blur_shader
  @blur_shader
end

.distort_shaderObject

Returns the value of attribute distort_shader.



11
12
13
# File 'lib/ashton/lighting/light_source.rb', line 11

def distort_shader
  @distort_shader
end

.draw_shadows_shaderObject

Returns the value of attribute draw_shadows_shader.



11
12
13
# File 'lib/ashton/lighting/light_source.rb', line 11

def draw_shadows_shader
  @draw_shadows_shader
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



15
16
17
# File 'lib/ashton/lighting/light_source.rb', line 15

def color
  @color
end

#radiusObject (readonly)

Returns the value of attribute radius.



14
15
16
# File 'lib/ashton/lighting/light_source.rb', line 14

def radius
  @radius
end

#xObject

Returns the value of attribute x.



15
16
17
# File 'lib/ashton/lighting/light_source.rb', line 15

def x
  @x
end

#yObject

Returns the value of attribute y.



15
16
17
# File 'lib/ashton/lighting/light_source.rb', line 15

def y
  @y
end

#zObject

Returns the value of attribute z.



15
16
17
# File 'lib/ashton/lighting/light_source.rb', line 15

def z
  @z
end

Instance Method Details

#draw(options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/ashton/lighting/light_source.rb', line 96

def draw(options = {})
  options = {
      mode: :add,
      color: @color,
  }.merge! options

  @blurred.draw @x - @radius, @y - @radius, @z, options
  @shadow_casters.draw @x - @radius, @y - @radius, @z, options
  nil
end

#draw_debugObject

Draw some quadrant lines (for debugging).



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ashton/lighting/light_source.rb', line 109

def draw_debug
  color = @color.dup
  color.alpha = 75

  $window.translate(-@radius, -@radius) do
    $window.draw_line x, y, color, x + width, y, color, z
    $window.draw_line x + width, y, color, x + width, y + height, color, z
    $window.draw_line x + width, y + height, color, x, y + height, color, z
    $window.draw_line x, y + height, color, x, y, color, z

    $window.draw_line x, y, color, x + width, y + height, color, z
    $window.draw_line x, y + height, color, x + width, y, color, z
  end
end

#heightObject



18
# File 'lib/ashton/lighting/light_source.rb', line 18

def height; @radius * 2 end

#render_shadows(&block) ⇒ Object

Only need to render shadows again if anything has actually changed!



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ashton/lighting/light_source.rb', line 36

def render_shadows(&block)
  raise "block required" unless block_given?

  render_shadow_casters(&block)

  # Distort the shadow casters and reduce into a a 2-pixel wide shadow-map of the blockages.
  LightSource.distort_shader.enable { distort }

  # Render the shadows themselves, before blurring.
  LightLightSource.draw_shadows_shader.enable { draw_shadows }

  # Finally blur it up and apply the radial lighting.
  LightSource.blur_shader.enable { blur }

  nil
end

#widthObject



17
# File 'lib/ashton/lighting/light_source.rb', line 17

def width; @radius * 2 end