Parent

RubyGL::Mat4

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.

Public Class Methods

new(diagonal = 1.0) click to toggle source
# 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
orthogonal(left, right, bottom, top, z_near = -1.0, z_far = 1.0) click to toggle source
# 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
perspective(fov, aspect, z_near, z_far) click to toggle source
# 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
rotation(x_axis, y_axis, z_axis, theta) click to toggle source

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
translation(x, y, z) click to toggle source
# File lib/rubygl/math.rb, line 189
def self.translation(x, y, z)
    matrix = Mat4.new(1.0)
    
    matrix[3][0] = x
    matrix[3][1] = y
    matrix[3][2] = z
    
    matrix
end

Public Instance Methods

*(other_matrix) click to toggle source
# 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
[](index) click to toggle source
# File lib/rubygl/math.rb, line 282
def [](index)
    @data[index]
end
[]=(index, value) click to toggle source
# File lib/rubygl/math.rb, line 286
def []=(index, value)
    @data[index] = value
end
dim() click to toggle source
# File lib/rubygl/math.rb, line 278
def dim()
    @data.size()
end
to_a() click to toggle source
# File lib/rubygl/math.rb, line 296
def to_a()
    self.to_ary
end
to_ary() click to toggle source
# File lib/rubygl/math.rb, line 290
def to_ary()
    @data.collect { |vec|
        vec.to_ary
    }.flatten!
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.