mirror of
https://github.com/cyberarm/cyberarm_engine.git
synced 2025-12-16 21:22:33 +00:00
Added Transform.perspective and .view matrix helpers, fixed array index out of bounds for Transform * Vector
This commit is contained in:
@@ -189,7 +189,8 @@ module CyberarmEngine
|
|||||||
attr_loc = location ? location : attribute_location(variable)
|
attr_loc = location ? location : attribute_location(variable)
|
||||||
|
|
||||||
case value.class.to_s.split("::").last.downcase.to_sym
|
case value.class.to_s.split("::").last.downcase.to_sym
|
||||||
when :integer
|
when :integer, :falseclass, :trueclass
|
||||||
|
value = value ? 1 : 0 if value.is_a?(TrueClass) or value.is_a?(FalseClass)
|
||||||
glUniform1i(attr_loc, value)
|
glUniform1i(attr_loc, value)
|
||||||
when :float
|
when :float
|
||||||
glUniform1f(attr_loc, value)
|
glUniform1f(attr_loc, value)
|
||||||
@@ -197,7 +198,7 @@ module CyberarmEngine
|
|||||||
when :transform
|
when :transform
|
||||||
glUniformMatrix4fv(attr_loc, 1, GL_FALSE, value.elements.pack("F16"))
|
glUniformMatrix4fv(attr_loc, 1, GL_FALSE, value.elements.pack("F16"))
|
||||||
when :vector
|
when :vector
|
||||||
glUniformVec4f(attr_loc, *value.to_a)
|
glUniform4f(attr_loc, *value.to_a)
|
||||||
else
|
else
|
||||||
raise NotImplementedError, "Shader support for #{value.class.inspect} not implemented."
|
raise NotImplementedError, "Shader support for #{value.class.inspect} not implemented."
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ module CyberarmEngine
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.rotate_3d(vector, order = "xyz")
|
def self.rotate_3d(vector, order = "xyz")
|
||||||
x, y, z = vector.to_a[0..2]
|
x, y, z = vector.to_a[0..2].map { |axis| axis.degrees_to_radians }
|
||||||
|
|
||||||
rotation_x = Transform.new(
|
rotation_x = Transform.new(
|
||||||
[
|
[
|
||||||
@@ -146,15 +146,64 @@ module CyberarmEngine
|
|||||||
rotation_x * rotation_y * rotation_z
|
rotation_x * rotation_y * rotation_z
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def self.view(eye, orientation)
|
||||||
|
# https://www.3dgep.com/understanding-the-view-matrix/#The_View_Matrix
|
||||||
|
cosPitch = Math.cos(orientation.z.degrees_to_radians)
|
||||||
|
sinPitch = Math.sin(orientation.z.degrees_to_radians)
|
||||||
|
cosYaw = Math.cos(orientation.y.degrees_to_radians)
|
||||||
|
sinYaw = Math.sin(orientation.y.degrees_to_radians)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def *(other)
|
def *(other)
|
||||||
|
|
||||||
case other
|
case other
|
||||||
when CyberarmEngine::Vector
|
when CyberarmEngine::Vector
|
||||||
matrix = Array.new(@elements.size)
|
matrix = @elements.clone
|
||||||
list = other.to_a
|
list = other.to_a
|
||||||
|
|
||||||
@elements.each_with_index do |e, i|
|
@elements.each_with_index do |e, i|
|
||||||
matrix[i] = e + list[i]
|
matrix[i] = e + list[i % 4]
|
||||||
end
|
end
|
||||||
|
|
||||||
Transform.new(matrix)
|
Transform.new(matrix)
|
||||||
|
|||||||
Reference in New Issue
Block a user