Stored In Column Order For Interoperability With OpenGL. Subscript Operator Will Return A Vector That Represents A Column Within The Matrix. This Matrix Class Only Supports Matrices Where The Number Of Rows Equals The Number Of Columns; Operations Are Constrained As Such.
# File lib/rubygl/math.rb, line 180 def initialize(diagonal = 1.0) @data = Array.new(4) { |index| column = Vec4.new() column[index] = diagonal column } end
# File lib/rubygl/math.rb, line 251 def self.orthogonal(left, right, bottom, top, z_near = -1.0, z_far = 1.0) mat = Mat4.new(1.0) mat[0][0] = 2.0 / (right - left) mat[1][1] = 2.0 / (top - bottom) mat[2][2] = 2.0 / (z_near - z_far) mat[3][3] = 1.0 mat[3][0] = -(right + left) / (right - left) mat[3][1] = -(top + bottom) / (top - bottom) mat[3][2] = -(z_far + z_near) / (z_far - z_near) mat end
# File lib/rubygl/math.rb, line 237 def self.perspective(fov, aspect, z_near, z_far) top = Math::tan(Conversion::deg_to_rad(fov) / 2) * z_near right = top * aspect mat = Mat4.new(1.0) mat[0][0] = z_near / right mat[1][1] = z_near / top mat[2][2] = -(z_far + z_near) / (z_far - z_near) mat[3][2] = -2.0 * z_far * z_near / (z_far - z_near) mat[2][3] = -1.0 mat end
The axis value should be 0 if that axis should not be rotated around; any other value indicates its priority (higher priority axis is rotated around before the lower priority axis).
# File lib/rubygl/math.rb, line 202 def self.rotation(x_axis, y_axis, z_axis, theta) rad_angle = Conversion::deg_to_rad(theta) sin_angle = Math::sin(rad_angle) cos_angle = Math::cos(rad_angle) # Multiply Lower -> Higher To Get Ordering Correct axis_priority = [[:x, x_axis], [:y, y_axis], [:z, z_axis]] axis_priority.delete_if { |(_, val)| val == 0 }.sort!.reverse! rot_matrix = Mat4.new(1.0) axis_priority.each { |(axis, _)| mat = Mat4.new(1.0) if axis == :x then mat[1][1] = mat[2][2] = cos_angle mat[1][2] = sin_angle mat[2][1] = -sin_angle elsif axis == :y then mat[0][0] = mat[2][2] = cos_angle mat[0][2] = -sin_angle mat[2][0] = sin_angle else mat[0][0] = mat[1][1] = cos_angle mat[0][1] = sin_angle mat[1][0] = -sin_angle end rot_matrix *= mat } rot_matrix end
# File lib/rubygl/math.rb, line 264 def *(other_matrix) new_matrix = Mat4.new(0) for i in 0...self.dim for j in 0...self.dim for k in 0...self.dim new_matrix[j][i] += self[k][i] * other_matrix[j][k] end end end new_matrix end
Generated with the Darkfish Rdoc Generator 2.