Class: Ra::Engine

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ra/engine.rb

Overview

An engine takes a world / camera and generates a PPM.

Constant Summary collapse

PRECISION =
255
PORCESSES =
8
PPM_VERSION =
'P3'
PPM_DEFAULT =
'0 0 0'

Instance Method Summary collapse

Constructor Details

#initialize(world:, camera:) ⇒ Engine

Returns a new instance of Engine.

Parameters:



16
17
18
19
# File 'lib/ra/engine.rb', line 16

def initialize(world:, camera:)
  @world = world
  @camera = camera
end

Instance Method Details

#each {|pixel| ... } ⇒ Object

Yields:

  • (pixel)

Yield Parameters:



23
24
25
26
27
28
# File 'lib/ra/engine.rb', line 23

def each
  @camera.each do |y, x, ray|
    color = @world.color(ray:)
    yield(Ra::Pixel.new(x:, y:, color:))
  end
end

#ppm {|text| ... } ⇒ Object

Yields:

  • (text)

Yield Parameters:

  • text (String)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ra/engine.rb', line 32

def ppm
  yield(<<~PPM)
    #{PPM_VERSION}
    #{@camera.w} #{@camera.h}
    #{Color::PRECISION}
  PPM

  each do |pixel|
    yield(pixel.color ? pixel.color.ppm : PPM_DEFAULT)
  end
end