Class: CyberarmEngine::Transform
- Inherits:
-
Object
- Object
- CyberarmEngine::Transform
- Defined in:
- lib/cyberarm_engine/transform.rb
Overview
Basic 4x4 matrix operations
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Class Method Summary collapse
- .concat(left, right) ⇒ Object
- .identity ⇒ Object
- .perspective(fov_y, aspect_ratio, near, far) ⇒ Object
-
.rotate(angle, rotate_around = nil) ⇒ Object
2d rotate operation, replicates Gosu’s Gosu.rotate function.
- .rotate_3d(vector, order = "zyx") ⇒ Object
-
.rotate_gl(angle, axis) ⇒ Object
Implements glRotatef www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml.
-
.scale(vector, center_around = nil) ⇒ Object
2d scale operation, replicates Gosu’s Gosu.rotate function.
- .scale_3d(vector) ⇒ Object
-
.translate(vector) ⇒ Object
2d translate operation, replicates Gosu’s Gosu.translate function.
-
.translate_3d(vector) ⇒ Object
3D Operations meant for OpenGL ###.
- .view(eye, orientation) ⇒ Object
Instance Method Summary collapse
- #*(other) ⇒ Object
- #get(x, y) ⇒ Object
-
#initialize(matrix) ⇒ Transform
constructor
A new instance of Transform.
- #multiply_matrices(other) ⇒ Object
-
#to_gl ⇒ Object
arranges Matrix in column major form.
Constructor Details
#initialize(matrix) ⇒ Transform
Returns a new instance of Transform.
5 6 7 8 9 10 |
# File 'lib/cyberarm_engine/transform.rb', line 5 def initialize(matrix) @elements = matrix raise "Transform is wrong size! Got #{@elements.size}, expected 16" if 16 != @elements.size raise "Invalid value for matrix, must all be numeric!" if @elements.any? { |e| e.nil? || !e.is_a?(Numeric)} end |
Instance Attribute Details
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
4 5 6 |
# File 'lib/cyberarm_engine/transform.rb', line 4 def elements @elements end |
Class Method Details
.concat(left, right) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cyberarm_engine/transform.rb', line 87 def self.concat(left, right) matrix = Array.new(left.elements.size) rows = 4 matrix.size.times do |i| matrix[i] = 0 rows.times do |j| matrix[i] += left.elements[i / rows * rows + j] * right.elements[i % rows + j * rows] end end Transform.new(matrix) end |
.identity ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/cyberarm_engine/transform.rb', line 12 def self.identity Transform.new( [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] ) end |
.perspective(fov_y, aspect_ratio, near, far) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cyberarm_engine/transform.rb', line 184 def self.perspective(fov_y, aspect_ratio, near, far) f = 1.0 / Math.tan(fov_y.degrees_to_radians / 2.0) # cotangent zn = (far + near.to_f) / (near - far.to_f) zf = (2.0 * far * near.to_f) / (near - far.to_f) Transform.new( [ f / aspect_ratio, 0.0, 0.0, 0.0, 0.0, f, 0.0, 0.0, 0.0, 0.0, zn, zf, 0.0, 0.0, -1.0, 0.0 ] ) end |
.rotate(angle, rotate_around = nil) ⇒ Object
2d rotate operation, replicates Gosu’s Gosu.rotate function
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cyberarm_engine/transform.rb', line 26 def self.rotate(angle, rotate_around = nil) double c = Math.cos(angle).degrees_to_radians double s = Math.sin(angle).degrees_to_radians matrix = [ +c, +s, 0, 0, -s, +c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, ] rotate_matrix = Transform.new(matrix, rows: 4, columns: 4) if rotate_around && (rotate_around.x != 0.0 || rotate_around.y != 0.0) negative_rotate_around = Vector.new(-rotate_around.x, -rotate_around.y, -rotate_around.z) rotate_matrix = concat( concat(translate(negative_rotate_around), rotate_matrix), translate(rotate_around) ) end return rotate_matrix end |
.rotate_3d(vector, order = "zyx") ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cyberarm_engine/transform.rb', line 116 def self.rotate_3d(vector, order = "zyx") x, y, z = vector.to_a[0..2].map { |axis| axis * Math::PI / 180.0 } rotation_x = Transform.new( [ 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 = Transform.new( [ 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 = Transform.new( [ 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_z * rotation_y * rotation_x end |
.rotate_gl(angle, axis) ⇒ Object
Implements glRotatef www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/cyberarm_engine/transform.rb', line 151 def self.rotate_gl(angle, axis) radians = angle * Math::PI / 180.0 s = Math.sin(radians) c = Math.cos(radians) axis = axis.normalized x, y, z = axis.to_a[0..2] n = (1.0 - c) Transform.new( [ x * x * n + c, x * y * n - z * s, x * z * n + y * s, 0, y * x * n + z * s, y * y * n + c, y * z * n - x * s, 0, x * z * n - y * s, y * z * n + x * s, z * z * n + c, 0, 0, 0, 0, 1.0 ] ) end |
.scale(vector, center_around = nil) ⇒ Object
2d scale operation, replicates Gosu’s Gosu.rotate function
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cyberarm_engine/transform.rb', line 64 def self.scale(vector, center_around = nil) scale_x, scale_y, scale_z = vector.to_a[0..2] matrix = [ scale_x, 0, 0, 0, 0, scale_y, 0, 0, 0, 0, scale_z, 0, 0, 0, 0, 1, ] scale_matrix = Transform.new(matrix) if center_around && (center_around.x != 0.0 || center_around.y != 0.0) negative_center_around = Vector.new(-center_around.x, -center_around.y, -center_around.z) scale_matrix = concat( concat(translate(negative_center_around), scale_matrix), translate(center_around) ) end return scale_matrix end |
.scale_3d(vector) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cyberarm_engine/transform.rb', line 171 def self.scale_3d(vector) x, y, z = vector.to_a[0..2] Transform.new( [ x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 ] ) end |
.translate(vector) ⇒ Object
2d translate operation, replicates Gosu’s Gosu.translate function
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cyberarm_engine/transform.rb', line 51 def self.translate(vector) x, y, z = vector.to_a[0..2] matrix = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1, ] Transform.new(matrix) end |
.translate_3d(vector) ⇒ Object
3D Operations meant for OpenGL ###
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cyberarm_engine/transform.rb', line 104 def self.translate_3d(vector) x, y, z = vector.to_a[0..2] matrix = [ 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1, ] Transform.new(matrix) end |
.view(eye, orientation) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/cyberarm_engine/transform.rb', line 199 def self.view(eye, orientation) # https://www.3dgep.com/understanding-the-view-matrix/#The_View_Matrix cosPitch = Math.cos(orientation.z * Math::PI / 180.0) sinPitch = Math.sin(orientation.z * Math::PI / 180.0) cosYaw = Math.cos(orientation.y * Math::PI / 180.0) sinYaw = Math.sin(orientation.y * Math::PI / 180.0) x_axis = Vector.new(cosYaw, 0, -sinYaw) y_axis = Vector.new(sinYaw * sinPitch, cosPitch, cosYaw * sinPitch) z_axis = Vector.new(sinYaw * cosPitch, -sinPitch, cosPitch * cosYaw) Transform.new( [ x_axis.x, y_axis.y, z_axis.z, 0, x_axis.x, y_axis.y, z_axis.z, 0, x_axis.x, y_axis.y, z_axis.z, 0, -x_axis.dot(eye), -y_axis.dot(eye), -z_axis.dot(eye), 1 ] ) end |
Instance Method Details
#*(other) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/cyberarm_engine/transform.rb', line 220 def *(other) case other when CyberarmEngine::Vector matrix = @elements.clone list = other.to_a @elements.each_with_index do |e, i| matrix[i] = e + list[i % 4] end Transform.new(matrix) when CyberarmEngine::Transform return multiply_matrices(other) else p other.class raise TypeError, "Expected CyberarmEngine::Vector or CyberarmEngine::Transform got #{other.class}" end end |
#get(x, y) ⇒ Object
241 242 243 244 245 246 |
# File 'lib/cyberarm_engine/transform.rb', line 241 def get(x, y) width = 4 # puts "Transform|#{self.object_id} -> #{@elements[width * y + x].inspect} (index: #{width * y + x})" @elements[width * y + x] end |
#multiply_matrices(other) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/cyberarm_engine/transform.rb', line 248 def multiply_matrices(other) matrix = Array.new(16, 0) 4.times do |x| 4.times do |y| 4.times do |k| matrix[4 * y + x] += get(x, k) * other.get(k, y) end end end return Transform.new(matrix) end |
#to_gl ⇒ Object
arranges Matrix in column major form
263 264 265 266 267 268 269 270 271 |
# File 'lib/cyberarm_engine/transform.rb', line 263 def to_gl e = @elements [ e[0], e[4], e[8], e[12], e[1], e[5], e[9], e[13], e[2], e[6], e[10], e[14], e[3], e[7], e[11], e[15] ] end |