Ran rubocop -a

This commit is contained in:
2020-12-14 16:04:31 -06:00
parent 2447dde1af
commit 26dd688124
59 changed files with 770 additions and 741 deletions

View File

@@ -6,16 +6,17 @@ module CyberarmEngine
attr_reader :light_id
attr_accessor :type, :ambient, :diffuse, :specular, :position, :direction, :intensity
def initialize(
id:,
type: Light::POINT,
ambient: Vector.new(0.5, 0.5, 0.5),
diffuse: Vector.new(1, 1, 1),
specular: Vector.new(0.2, 0.2, 0.2),
position: Vector.new(0, 0, 0),
direction: Vector.new(0, 0, 0),
intensity: 1
)
id:,
type: Light::POINT,
ambient: Vector.new(0.5, 0.5, 0.5),
diffuse: Vector.new(1, 1, 1),
specular: Vector.new(0.2, 0.2, 0.2),
position: Vector.new(0, 0, 0),
direction: Vector.new(0, 0, 0),
intensity: 1
)
@light_id = id
@type = type
@@ -40,9 +41,9 @@ module CyberarmEngine
def convert(struct, apply_intensity = false)
if apply_intensity
return struct.to_a.compact.map{ |i| i * @intensity }
struct.to_a.compact.map { |i| i * @intensity }
else
return struct.to_a.compact
struct.to_a.compact
end
end
end

View File

@@ -2,17 +2,21 @@ module CyberarmEngine
class OrthographicCamera
attr_accessor :position, :orientation, :zoom, :left, :right, :bottom, :top,
:min_view_distance, :max_view_distance
def initialize(
position:, orientation: Vector.new(0, 0, 0),
zoom: 1, left: 0, right:, bottom: 0, top:,
min_view_distance: 0.1, max_view_distance: 200.0
)
position:, right:, top:, orientation: Vector.new(0, 0, 0),
zoom: 1, left: 0, bottom: 0,
min_view_distance: 0.1, max_view_distance: 200.0
)
@position = position
@orientation = orientation
@zoom = zoom
@left, @right, @bottom, @top = left, right, bottom, top
@left = left
@right = right
@bottom = bottom
@top = top
@min_view_distance = min_view_distance
@max_view_distance = max_view_distance

View File

@@ -2,7 +2,9 @@ module CyberarmEngine
class PerspectiveCamera
attr_accessor :position, :orientation, :aspect_ratio, :field_of_view,
:min_view_distance, :max_view_distance
def initialize(position:, orientation: Vector.new(0, 0, 0), aspect_ratio:, field_of_view: 70.0, min_view_distance: 0.1, max_view_distance: 155.0)
def initialize(position:, aspect_ratio:, orientation: Vector.new(0, 0,
0), field_of_view: 70.0, min_view_distance: 0.1, max_view_distance: 155.0)
@position = position
@orientation = orientation

View File

@@ -1,6 +1,7 @@
module CyberarmEngine
class BoundingBoxRenderer
attr_reader :bounding_boxes, :vertex_count
def initialize
@bounding_boxes = {}
@vertex_count = 0
@@ -8,7 +9,7 @@ module CyberarmEngine
def render(entities)
entities.each do |entity|
create_bounding_box(entity,color = nil)
create_bounding_box(entity, color = nil)
draw_bounding_boxes
end
@@ -43,7 +44,7 @@ module CyberarmEngine
colors = mesh_colors(color)
vertices = mesh_vertices(box)
@vertex_count+=vertices.size
@vertex_count += vertices.size
@bounding_boxes[entity_id][:vertices_size] = vertices.size
@bounding_boxes[entity_id][:vertices] = vertices.pack("f*")
@@ -58,7 +59,7 @@ module CyberarmEngine
colors = mesh_colors(mesh.debug_color)
vertices = mesh_vertices(box)
@vertex_count+=vertices.size
@vertex_count += vertices.size
data[:vertices_size] = vertices.size
data[:vertices] = vertices.pack("f*")
@@ -71,48 +72,48 @@ module CyberarmEngine
def mesh_normals
[
0,1,0,
0,1,0,
0,1,0,
0,1,0,
0,1,0,
0,1,0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0,-1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0,-1,0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0,0,1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
1,0,0,
1,0,0,
1,0,0,
1,0,0,
1,0,0,
1,0,0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1,0,0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1,0,0,
-1,0,0,
-1,0,0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1,0,0,
-1,0,0,
-1,0,0
-1, 0, 0,
-1, 0, 0,
-1, 0, 0
]
end
@@ -219,7 +220,7 @@ module CyberarmEngine
bounding_box[:entity].position.z
)
draw_bounding_box(bounding_box)
@bounding_boxes[key][:objects].each {|o| draw_bounding_box(o)}
@bounding_boxes[key][:objects].each { |o| draw_bounding_box(o) }
glPopMatrix
end

View File

@@ -1,11 +1,13 @@
module CyberarmEngine
class GBuffer
attr_reader :screen_vbo, :vertices, :uvs
def initialize(width:, height:)
@width, @height = width, height
@width = width
@height = height
@framebuffer = nil
@buffers = [:position, :diffuse, :normal, :texcoord]
@buffers = %i[position diffuse normal texcoord]
@textures = {}
@screen_vbo = nil
@ready = false
@@ -16,9 +18,9 @@ module CyberarmEngine
-1.0, 1.0, 0,
-1.0, 1.0, 0,
1.0, -1.0, 0,
1.0, 1.0, 0,
].freeze
1.0, -1.0, 0,
1.0, 1.0, 0
].freeze
@uvs = [
0, 0,
@@ -35,9 +37,9 @@ module CyberarmEngine
end
def create_framebuffer
buffer = ' ' * 4
buffer = " " * 4
glGenFramebuffers(1, buffer)
@framebuffer = buffer.unpack('L2').first
@framebuffer = buffer.unpack1("L2")
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, @framebuffer)
@@ -48,20 +50,20 @@ module CyberarmEngine
if status != GL_FRAMEBUFFER_COMPLETE
message = ""
case status
when GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
message = "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"
when GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
message = "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"
when GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER
message = "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"
when GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER
message = "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"
when GL_FRAMEBUFFER_UNSUPPORTED
message = "GL_FRAMEBUFFER_UNSUPPORTED"
else
message = "Unknown error!"
end
message = case status
when GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
"GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"
when GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
"GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"
when GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER
"GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"
when GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER
"GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"
when GL_FRAMEBUFFER_UNSUPPORTED
"GL_FRAMEBUFFER_UNSUPPORTED"
else
"Unknown error!"
end
puts "Incomplete framebuffer: #{status}\nError: #{message}"
else
@ready = true
@@ -72,9 +74,9 @@ module CyberarmEngine
def create_textures
@buffers.size.times do |i|
buffer = ' ' * 4
buffer = " " * 4
glGenTextures(1, buffer)
texture_id = buffer.unpack('L2').first
texture_id = buffer.unpack1("L2")
@textures[@buffers[i]] = texture_id
glBindTexture(GL_TEXTURE_2D, texture_id)
@@ -84,39 +86,39 @@ module CyberarmEngine
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture_id, 0)
end
buffer = ' ' * 4
buffer = " " * 4
glGenTextures(1, buffer)
texture_id = buffer.unpack('L2').first
texture_id = buffer.unpack1("L2")
@textures[:depth] = texture_id
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, @width, @height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nil)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0)
draw_buffers = [ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 ]
draw_buffers = [GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3]
glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
end
def create_screen_vbo
buffer = ' ' * 4
buffer = " " * 4
glGenVertexArrays(1, buffer)
@screen_vbo = buffer.unpack('L2').first
@screen_vbo = buffer.unpack1("L2")
buffer = " " * 4
glGenBuffers(1, buffer)
@positions_buffer_id = buffer.unpack('L2').first
@positions_buffer_id = buffer.unpack1("L2")
buffer = " " * 4
glGenBuffers(1, buffer)
@uvs_buffer_id = buffer.unpack('L2').first
@uvs_buffer_id = buffer.unpack1("L2")
glBindVertexArray(@screen_vbo)
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id)
glBufferData(GL_ARRAY_BUFFER, @vertices.size * Fiddle::SIZEOF_FLOAT, @vertices.pack("f*"), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, @vertices.size * Fiddle::SIZEOF_FLOAT, @vertices.pack("f*"), GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil)
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
glBufferData(GL_ARRAY_BUFFER, @uvs.size * Fiddle::SIZEOF_FLOAT, @uvs.pack("f*"), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, @uvs.size * Fiddle::SIZEOF_FLOAT, @uvs.pack("f*"), GL_STATIC_DRAW)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nil)
glEnableVertexAttribArray(0)
@@ -159,4 +161,4 @@ module CyberarmEngine
gl_error?
end
end
end
end

View File

@@ -3,8 +3,10 @@ module CyberarmEngine
@@immediate_mode_warning = false
attr_accessor :show_wireframe
def initialize(width:, height:, show_wireframe: false)
@width, @height = width, height
@width = width
@height = height
@show_wireframe = show_wireframe
@g_buffer = GBuffer.new(width: @width, height: @height)
@@ -63,7 +65,9 @@ module CyberarmEngine
@g_buffer.unbind_framebuffer
gl_error?
else
puts "Shaders are disabled or failed to compile, using immediate mode for rendering..." unless @@immediate_mode_warning
unless @@immediate_mode_warning
puts "Shaders are disabled or failed to compile, using immediate mode for rendering..."
end
@@immediate_mode_warning = true
gl_error?
@@ -141,8 +145,8 @@ module CyberarmEngine
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
shader.uniform_integer("depth", 4)
lights.each_with_index do |light, i|
shader.uniform_integer("light[0].type", light.type);
lights.each_with_index do |light, _i|
shader.uniform_integer("light[0].type", light.type)
shader.uniform_vec3("light[0].direction", light.direction)
shader.uniform_vec3("light[0].position", light.position)
shader.uniform_vec3("light[0].diffuse", light.diffuse)
@@ -229,7 +233,7 @@ module CyberarmEngine
end
def draw_mesh(model)
model.objects.each_with_index do |o, i|
model.objects.each_with_index do |o, _i|
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glShadeModel(GL_FLAT) unless o.faces.first[4]
@@ -255,16 +259,16 @@ module CyberarmEngine
glPolygonOffset(2, 0.5)
glLineWidth(3)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
glLineWidth(1)
glPolygonOffset(0, 0)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glEnable(GL_LIGHTING)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
else
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size / 4)
end
# glBindBuffer(GL_ARRAY_BUFFER, 0)

View File

@@ -22,9 +22,7 @@ module CyberarmEngine
if shader
@@shaders.delete(name)
if shader.compiled?
glDeleteProgram(shader.program)
end
glDeleteProgram(shader.program) if shader.compiled?
end
end
@@ -68,15 +66,15 @@ module CyberarmEngine
# returns currently active {Shader}, if one is active
#
# @return [Shader?]
def self.active_shader
@active_shader
class << self
attr_reader :active_shader
end
# sets currently active {Shader}
#
# @param instance [Shader] instance of {Shader} to set as active
def self.active_shader=(instance)
@active_shader = instance
class << self
attr_writer :active_shader
end
# stops using currently active {Shader}
@@ -94,7 +92,8 @@ module CyberarmEngine
#
# @param variable [String]
def self.attribute_location(variable)
raise RuntimeError, "No active shader!" unless Shader.active_shader
raise "No active shader!" unless Shader.active_shader
Shader.active_shader.attribute_location(variable)
end
@@ -103,12 +102,14 @@ module CyberarmEngine
# @param variable [String]
# @param value
def self.set_uniform(variable, value)
raise RuntimeError, "No active shader!" unless Shader.active_shader
raise "No active shader!" unless Shader.active_shader
Shader.active_shader.set_uniform(variable, value)
end
attr_reader :name, :program
def initialize(name:, includes_dir: nil, vertex: "shaders/default.vert", fragment:)
def initialize(name:, fragment:, includes_dir: nil, vertex: "shaders/default.vert")
raise "Shader name can not be blank" if name.length == 0
@name = name
@@ -120,7 +121,7 @@ module CyberarmEngine
@error_buffer_size = 1024 * 8
@variable_missing = {}
@data = {shaders: {}}
@data = { shaders: {} }
unless shader_files_exist?(vertex: vertex, fragment: fragment)
raise ArgumentError, "Shader files not found: #{vertex} or #{fragment}"
@@ -133,7 +134,7 @@ module CyberarmEngine
compile_shader(type: :fragment)
link_shaders
@data[:shaders].each { |key, id| glDeleteShader(id) }
@data[:shaders].each { |_key, id| glDeleteShader(id) }
# Only add shader if it successfully compiles
if @compiled
@@ -175,7 +176,7 @@ module CyberarmEngine
_size = [processed_source.length].pack("I")
glShaderSource(_shader, 1, _source, _size)
@data[:shaders][type] =_shader
@data[:shaders][type] = _shader
end
# evaluates shader preprocessors
@@ -199,22 +200,25 @@ module CyberarmEngine
lines = source.lines
lines.each_with_index do |line, i|
if line.start_with?(PREPROCESSOR_CHARACTER)
preprocessor = line.strip.split(" ")
lines.delete(line)
next unless line.start_with?(PREPROCESSOR_CHARACTER)
case preprocessor.first
when "@include"
raise ArgumentError, "Shader preprocessor include directory was not given for shader #{@name}" unless @includes_dir
preprocessor = line.strip.split(" ")
lines.delete(line)
preprocessor[1..preprocessor.length - 1].join.scan(/"([^"]*)"/).flatten.each do |file|
source = File.read("#{@includes_dir}/#{file}.glsl")
lines.insert(i, source)
end
else
warn "Unsupported preprocessor #{preprocessor.first} for #{@name}"
case preprocessor.first
when "@include"
unless @includes_dir
raise ArgumentError,
"Shader preprocessor include directory was not given for shader #{@name}"
end
preprocessor[1..preprocessor.length - 1].join.scan(/"([^"]*)"/).flatten.each do |file|
source = File.read("#{@includes_dir}/#{file}.glsl")
lines.insert(i, source)
end
else
warn "Unsupported preprocessor #{preprocessor.first} for #{@name}"
end
end
@@ -230,12 +234,12 @@ module CyberarmEngine
raise ArgumentError, "No shader for #{type.inspect}" unless _shader
glCompileShader(_shader)
buffer = ' '
buffer = " "
glGetShaderiv(_shader, GL_COMPILE_STATUS, buffer)
compiled = buffer.unpack('L')[0]
compiled = buffer.unpack1("L")
if compiled == 0
log = ' ' * @error_buffer_size
log = " " * @error_buffer_size
glGetShaderInfoLog(_shader, @error_buffer_size, nil, log)
puts "Shader Error: Program \"#{@name}\""
puts " #{type.to_s.capitalize} Shader InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
@@ -246,7 +250,7 @@ module CyberarmEngine
_compiled = true
end
return _compiled
_compiled
end
# link compiled OpenGL Shaders in to a OpenGL Program
@@ -261,18 +265,18 @@ module CyberarmEngine
end
glLinkProgram(@program)
buffer = ' '
buffer = " "
glGetProgramiv(@program, GL_LINK_STATUS, buffer)
linked = buffer.unpack('L')[0]
linked = buffer.unpack1("L")
if linked == 0
log = ' ' * @error_buffer_size
log = " " * @error_buffer_size
glGetProgramInfoLog(@program, @error_buffer_size, nil, log)
puts "Shader Error: Program \"#{@name}\""
puts " Program InfoLog:", " #{log.strip.split("\n").join("\n ")}\n\n"
end
@compiled = linked == 0 ? false : true
@compiled = !(linked == 0)
end
# Returns the location of a uniform _variable_
@@ -281,18 +285,22 @@ module CyberarmEngine
# @return [Integer] location of uniform
def variable(variable)
loc = glGetUniformLocation(@program, variable)
if (loc == -1)
puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"", " Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?" unless @variable_missing[variable]
if loc == -1
unless @variable_missing[variable]
puts "Shader Error: Program \"#{@name}\" has no such uniform named \"#{variable}\"",
" Is it used in the shader? GLSL may have optimized it out.", " Is it miss spelled?"
end
@variable_missing[variable] = true
end
return loc
loc
end
# @see Shader.use Shader.use
def use(&block)
return unless compiled?
raise "Another shader is already in use! #{Shader.active_shader.name.inspect}" if Shader.active_shader
Shader.active_shader=self
Shader.active_shader = self
glUseProgram(@program)
@@ -331,7 +339,7 @@ module CyberarmEngine
# @param location [Integer]
# @return [void]
def uniform_transform(variable, value, location = nil)
attr_loc = location ? location : attribute_location(variable)
attr_loc = location || attribute_location(variable)
glUniformMatrix4fv(attr_loc, 1, GL_FALSE, value.to_gl.pack("F16"))
end
@@ -343,7 +351,7 @@ module CyberarmEngine
# @param location [Integer]
# @return [void]
def uniform_boolean(variable, value, location = nil)
attr_loc = location ? location : attribute_location(variable)
attr_loc = location || attribute_location(variable)
glUniform1i(attr_loc, value ? 1 : 0)
end
@@ -354,7 +362,7 @@ module CyberarmEngine
# @param location [Integer]
# @return [void]
def uniform_integer(variable, value, location = nil)
attr_loc = location ? location : attribute_location(variable)
attr_loc = location || attribute_location(variable)
glUniform1i(attr_loc, value)
end
@@ -366,7 +374,7 @@ module CyberarmEngine
# @param location [Integer]
# @return [void]
def uniform_float(variable, value, location = nil)
attr_loc = location ? location : attribute_location(variable)
attr_loc = location || attribute_location(variable)
glUniform1f(attr_loc, value)
end
@@ -378,7 +386,7 @@ module CyberarmEngine
# @param location [Integer]
# @return [void]
def uniform_vec3(variable, value, location = nil)
attr_loc = location ? location : attribute_location(variable)
attr_loc = location || attribute_location(variable)
glUniform3f(attr_loc, *value.to_a[0..2])
end
@@ -390,7 +398,7 @@ module CyberarmEngine
# @param location [Integer]
# @return [void]
def uniform_vec4(variable, value, location = nil)
attr_loc = location ? location : attribute_location(variable)
attr_loc = location || attribute_location(variable)
glUniform4f(attr_loc, *value.to_a)
end

View File

@@ -1,6 +1,6 @@
module CyberarmEngine
class Texture
DEFAULT_TEXTURE = "#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/default.png"
DEFAULT_TEXTURE = "#{CYBERARM_ENGINE_ROOT_PATH}/assets/textures/default.png".freeze
CACHE = {}
@@ -11,12 +11,14 @@ module CyberarmEngine
end
def self.from_cache(path, retro)
return CACHE.dig("#{path}?retro=#{retro}")
CACHE.dig("#{path}?retro=#{retro}")
end
attr_reader :id
def initialize(path: nil, image: nil, retro: false)
raise "keyword :path or :image must be provided!" if path.nil? && image.nil?
@retro = retro
@path = path
@@ -47,9 +49,9 @@ module CyberarmEngine
def create_from_image(image)
array_of_pixels = image.to_blob
tex_names_buf = ' ' * 4
tex_names_buf = " " * 4
glGenTextures(1, tex_names_buf)
texture_id = tex_names_buf.unpack('L2').first
texture_id = tex_names_buf.unpack1("L2")
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
@@ -61,7 +63,7 @@ module CyberarmEngine
glGenerateMipmap(GL_TEXTURE_2D)
gl_error?
return texture_id
texture_id
end
end
end
end