Class: Mat4

Inherits:
Struct
  • Object
show all
Defined in:
lib/belts_engine/components/mat4.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#mObject

Returns the value of attribute m

Returns:

  • (Object)

    the current value of m



1
2
3
# File 'lib/belts_engine/components/mat4.rb', line 1

def m
  @m
end

Class Method Details

.identityObject



3
# File 'lib/belts_engine/components/mat4.rb', line 3

def identity = Matrix.identity(4)

.look_at(eye, target, up) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/belts_engine/components/mat4.rb', line 75

def look_at(eye, target, up)
  z_axis = (eye - target).normalize
  x_axis = up.cross(z_axis).normalize
  y_axis = z_axis.cross(x_axis)

  Matrix[
    [x_axis[0], x_axis[1], x_axis[2], -x_axis.dot(eye)],
    [y_axis[0], y_axis[1], y_axis[2], -y_axis.dot(eye)],
    [z_axis[0], z_axis[1], z_axis[2], -z_axis.dot(eye)],
    [0, 0, 0, 1]
  ]
end

.orthographic(left, right, bottom, top, near, far) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/belts_engine/components/mat4.rb', line 67

def orthographic(left, right, bottom, top, near, far)
  scale = scale(2.0 / (right - left), 2.0 / (top - bottom), 2.0 / (far - near))
  centerer = translation(- (right + left) / (right - left), - (top + bottom) / (top - bottom), (far - near) / 2.0)
  inverter = scale(1, 1, -1)

  scale * centerer# * inverter
end

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



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

def perspective(fov, aspect, near, far)
  fov_rad = fov * Math::PI / 180

  y_scale = 1 / Math.tan(fov_rad)
  x_scale = y_scale / aspect
  frustumLength = far - near

  Matrix[
    [x_scale, 0, 0, 0],
    [0, y_scale, 0, 0],
    [0, 0, -(far + near) / frustumLength, -1],
    [0, 0, 2 * -(far * near) / frustumLength, 0]
  ].transpose
end

.rotation(x, y, z) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/belts_engine/components/mat4.rb', line 14

def rotation(x, y, z)
  x = x * Math::PI / 180
  y = y * Math::PI / 180
  z = z * Math::PI / 180

  rotation_x = Matrix[
    [1, 0, 0, 0],
    [0, Math.cos(x), -Math.sin(x), 0],
    [0, Math.sin(x), Math.cos(x), 0],
    [0, 0, 0, 1]
  ]

  rotation_y = Matrix[
    [Math.cos(y), 0, Math.sin(y), 0],
    [0, 1, 0, 0],
    [-Math.sin(y), 0, Math.cos(y), 0],
    [0, 0, 0, 1]
  ]

  rotation_z = Matrix[
    [Math.cos(z), -Math.sin(z), 0, 0],
    [Math.sin(z), Math.cos(z), 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1]
  ]

  rotation_x * rotation_y * rotation_z
end

.scale(x, y, z) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/belts_engine/components/mat4.rb', line 43

def scale(x, y, z)
  Matrix[
    [x, 0, 0, 0],
    [0, y, 0, 0],
    [0, 0, z, 0],
    [0, 0, 0, 1]
  ]
end

.translation(x, y, z) ⇒ Object



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

def translation(x, y, z)
  Matrix[
    [1, 0, 0, x],
    [0, 1, 0, y],
    [0, 0, 1, z],
    [0, 0, 0, 1]
  ]
end