Class: Engine::Components::PerspectiveCamera

Inherits:
Engine::Component show all
Defined in:
lib/engine/components/perspective_camera.rb

Instance Attribute Summary

Attributes inherited from Engine::Component

#game_object

Instance Method Summary collapse

Methods inherited from Engine::Component

#_erase!, #destroy!, #destroyed?, destroyed_components, erase_destroyed_components, method_added, #renderer?, #set_game_object, #start, #ui_renderer?

Constructor Details

#initialize(fov:, aspect:, near:, far:) ⇒ PerspectiveCamera

Returns a new instance of PerspectiveCamera.



5
6
7
8
9
10
11
12
# File 'lib/engine/components/perspective_camera.rb', line 5

def initialize(fov:, aspect:, near:, far:)
  @fov = fov
  @aspect = aspect
  @near = near
  @far = far

  Engine::Camera.instance = self
end

Instance Method Details

#destroyObject



14
15
16
# File 'lib/engine/components/perspective_camera.rb', line 14

def destroy
  Engine::Camera.instance = nil if Engine::Camera.instance == self
end

#matrixObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/engine/components/perspective_camera.rb', line 18

def matrix
  @matrix ||=
    begin
      right = game_object.right
      up = game_object.up
      forward = game_object.forward

      transformation_matrix = Matrix[
        [right[0], right[1], right[2], -right.dot(game_object.pos)],
        [up[0], up[1], up[2], -up.dot(game_object.pos)],
        [forward[0], forward[1], forward[2], -forward.dot(game_object.pos)],
        [0, 0, 0, 1]
      ]

      (projection * transformation_matrix).transpose
    end
end

#projectionObject



36
37
38
39
40
41
42
43
44
# File 'lib/engine/components/perspective_camera.rb', line 36

def projection
  fov = @fov * Math::PI / 180
  Matrix[
    [1 / (Math.tan(fov / 2) * @aspect), 0, 0, 0],
    [0, 1 / Math.tan(fov / 2), 0, 0],
    [0, 0, (@far + @near) / (@near - @far), 2 * @far * @near / (@near - @far)],
    [0, 0, -1, 0]
  ]
end

#update(delta_time) ⇒ Object



46
47
48
49
50
51
# File 'lib/engine/components/perspective_camera.rb', line 46

def update(delta_time)
  @matrix = nil if game_object.rotation != @rotation || game_object.pos != @pos || game_object.scale != @scale
  @rotation = game_object.rotation.dup
  @pos = game_object.pos.dup
  @scale = game_object.scale.dup
end