Module: Engine::MatrixHelpers

Instance Method Summary collapse

Instance Method Details

#look_at(eye, center, up) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/engine/matrix_helpers.rb', line 5

def look_at(eye, center, up)
  f = (center - eye).normalize
  s = f.cross(up).normalize
  u = s.cross(f)

  Matrix[
    [s[0], s[1], s[2], -s.dot(eye)],
    [u[0], u[1], u[2], -u.dot(eye)],
    [-f[0], -f[1], -f[2], f.dot(eye)],
    [0, 0, 0, 1]
  ]
end

#ortho(left, right, bottom, top, near, far) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/engine/matrix_helpers.rb', line 29

def ortho(left, right, bottom, top, near, far)
  Matrix[
    [2.0 / (right - left), 0, 0, -(right + left) / (right - left)],
    [0, 2.0 / (top - bottom), 0, -(top + bottom) / (top - bottom)],
    [0, 0, -2.0 / (far - near), -(far + near) / (far - near)],
    [0, 0, 0, 1]
  ]
end

#perspective(fov, aspect, near, far) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/engine/matrix_helpers.rb', line 18

def perspective(fov, aspect, near, far)
  tan_half_fov = Math.tan(fov / 2.0)

  Matrix[
    [1.0 / (aspect * tan_half_fov), 0, 0, 0],
    [0, 1.0 / tan_half_fov, 0, 0],
    [0, 0, -(far + near) / (far - near), -(2.0 * far * near) / (far - near)],
    [0, 0, -1, 0]
  ]
end