Class: Engine::Components::PerspectiveCamera

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

Instance Attribute Summary collapse

Attributes inherited from Engine::Component

#game_object

Instance Method Summary collapse

Methods included from MatrixHelpers

#look_at, #ortho, #perspective

Methods inherited from Engine::Component

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

Methods included from Serializable

allowed_class?, get_class, included, register_class, #uuid

Instance Attribute Details

#farObject (readonly)

Returns the value of attribute far.



9
10
11
# File 'lib/engine/components/perspective_camera.rb', line 9

def far
  @far
end

#nearObject (readonly)

Returns the value of attribute near.



9
10
11
# File 'lib/engine/components/perspective_camera.rb', line 9

def near
  @near
end

Instance Method Details

#awakeObject



11
12
13
# File 'lib/engine/components/perspective_camera.rb', line 11

def awake
  Engine::Camera.instance = self
end

#destroyObject



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

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

#inverse_vp_matrixObject



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

def inverse_vp_matrix
  # matrix is already transposed for OpenGL column-major format
  # inverse of A^T is (A^-1)^T, so matrix.inverse gives us the inverse in column-major
  @inverse_vp_matrix ||= matrix.inverse
end

#matrixObject



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

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

#positionObject



43
44
45
# File 'lib/engine/components/perspective_camera.rb', line 43

def position
  game_object.pos
end

#projectionObject



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

def projection
  fov_radians = @fov * Math::PI / 180.0
  perspective(fov_radians, @aspect, @near, @far)
end

#update(delta_time) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/engine/components/perspective_camera.rb', line 66

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

#view_matrixObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/engine/components/perspective_camera.rb', line 52

def view_matrix
  right = game_object.right
  up = game_object.up
  forward = game_object.forward
  pos = game_object.pos

  Matrix[
    [right[0], right[1], right[2], -right.dot(pos)],
    [up[0], up[1], up[2], -up.dot(pos)],
    [forward[0], forward[1], forward[2], -forward.dot(pos)],
    [0, 0, 0, 1]
  ].transpose
end