mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Restructured deferred lighting/rendering
This commit is contained in:
@@ -2,9 +2,10 @@ class IMICFPS
|
||||
class Light
|
||||
DIRECTIONAL = 0
|
||||
POINT = 1
|
||||
SPOT = 2
|
||||
|
||||
attr_reader :light_id
|
||||
attr_accessor :type, :ambient, :diffuse, :specular, :position, :intensity
|
||||
attr_accessor :type, :ambient, :diffuse, :specular, :position, :direction, :intensity
|
||||
def initialize(
|
||||
id:,
|
||||
type: Light::POINT,
|
||||
@@ -12,6 +13,7 @@ class IMICFPS
|
||||
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
|
||||
@@ -21,6 +23,7 @@ class IMICFPS
|
||||
@diffuse = diffuse
|
||||
@specular = specular
|
||||
@position = position
|
||||
@direction = direction
|
||||
|
||||
@intensity = intensity
|
||||
end
|
||||
|
||||
15
lib/model.rb
15
lib/model.rb
@@ -119,11 +119,6 @@ class IMICFPS
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@uvs_buffer_id = buffer.unpack('L2').first
|
||||
|
||||
@textures_buffer_id = nil
|
||||
buffer = " " * 4
|
||||
glGenBuffers(1, buffer)
|
||||
@textures_buffer_id = buffer.unpack('L2').first
|
||||
end
|
||||
|
||||
def populate_vertex_buffer
|
||||
@@ -131,7 +126,6 @@ class IMICFPS
|
||||
colors = []
|
||||
norms = []
|
||||
uvs = []
|
||||
tex_ids = []
|
||||
|
||||
@faces.each do |face|
|
||||
pos << face.vertices.map { |vert| [vert.x, vert.y, vert.z] }
|
||||
@@ -140,7 +134,6 @@ class IMICFPS
|
||||
|
||||
if has_texture?
|
||||
uvs << face.uvs.map { |vert| [vert.x, vert.y, vert.z] }
|
||||
tex_ids << face.material.texture_id ? face.material.texture_id.to_f : -1.0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -156,9 +149,6 @@ class IMICFPS
|
||||
if has_texture?
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||
glBufferData(GL_ARRAY_BUFFER, uvs.flatten.size * Fiddle::SIZEOF_FLOAT, uvs.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||
|
||||
# glBindBuffer(GL_ARRAY_BUFFER, @textures_buffer_id)
|
||||
# glBufferData(GL_ARRAY_BUFFER, tex_ids.flatten.size * Fiddle::SIZEOF_FLOAT, tex_ids.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||
end
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||
@@ -193,11 +183,6 @@ class IMICFPS
|
||||
# inUV
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
# texture ids
|
||||
glBindBuffer(GL_ARRAY_BUFFER, @textures_buffer_id)
|
||||
# inTextureID
|
||||
glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 0, nil)
|
||||
gl_error?
|
||||
end
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||
|
||||
@@ -127,6 +127,9 @@ class IMICFPS
|
||||
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)
|
||||
glEnableVertexAttribArray(1)
|
||||
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
|
||||
|
||||
@@ -16,27 +16,16 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def render(camera, lights, entities)
|
||||
if window.config.get(:debug_options, :use_shaders) && Shader.available?("default") && Shader.available?("render_screen")
|
||||
if window.config.get(:debug_options, :use_shaders) && Shader.available?("g_buffer") && Shader.available?("deferred_lighting")
|
||||
@g_buffer.bind_for_writing
|
||||
gl_error?
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0)
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||
|
||||
Shader.use("default") do |shader|
|
||||
lights.each_with_index do |light, i|
|
||||
shader.uniform_float("lights[#{i}.end", -1.0);
|
||||
shader.uniform_float("lights[#{i}.type", light.type);
|
||||
shader.uniform_vec3("lights[#{i}].position", light.position)
|
||||
shader.uniform_vec3("lights[#{i}].ambient", light.ambient)
|
||||
shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse)
|
||||
shader.uniform_vec3("lights[#{i}].specular", light.specular)
|
||||
end
|
||||
Shader.use("g_buffer") do |shader|
|
||||
gl_error?
|
||||
|
||||
|
||||
shader.uniform_integer("totalLights", lights.size)
|
||||
|
||||
entities.each do |entity|
|
||||
next unless entity.visible && entity.renderable
|
||||
|
||||
@@ -54,7 +43,6 @@ class IMICFPS
|
||||
@g_buffer.unbind_framebuffer
|
||||
gl_error?
|
||||
|
||||
|
||||
@g_buffer.bind_for_reading
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
|
||||
|
||||
@@ -65,7 +53,7 @@ class IMICFPS
|
||||
@g_buffer.unbind_framebuffer
|
||||
gl_error?
|
||||
else
|
||||
puts "Shader 'default' failed to compile, using immediate mode for rendering..." unless @@immediate_mode_warning
|
||||
puts "Shaders are disabled or failed to compile, using immediate mode for rendering..." unless @@immediate_mode_warning
|
||||
@@immediate_mode_warning = true
|
||||
|
||||
gl_error?
|
||||
@@ -94,7 +82,7 @@ class IMICFPS
|
||||
gl_error?
|
||||
end
|
||||
|
||||
def lighting(lights)
|
||||
def copy_g_buffer_to_screen
|
||||
@g_buffer.set_read_buffer(:position)
|
||||
glBlitFramebuffer(0, 0, @g_buffer.width, @g_buffer.height,
|
||||
0, 0, @g_buffer.width / 2, @g_buffer.height / 2,
|
||||
@@ -116,15 +104,50 @@ class IMICFPS
|
||||
GL_COLOR_BUFFER_BIT, GL_LINEAR)
|
||||
end
|
||||
|
||||
def lighting(lights)
|
||||
Shader.use("deferred_lighting") do |shader|
|
||||
lights.each_with_index do |light, i|
|
||||
shader.uniform_float("light.type", light.type);
|
||||
shader.uniform_vec3("light.direction", light.direction)
|
||||
shader.uniform_vec3("light.position", light.position)
|
||||
shader.uniform_vec3("light.ambient", light.ambient)
|
||||
shader.uniform_vec3("light.diffuse", light.diffuse)
|
||||
shader.uniform_vec3("light.specular", light.specular)
|
||||
|
||||
glBindVertexArray(@g_buffer.screen_vbo)
|
||||
|
||||
glDisable(GL_DEPTH_TEST)
|
||||
glEnable(GL_BLEND)
|
||||
|
||||
glActiveTexture(GL_TEXTURE0)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:diffuse))
|
||||
|
||||
glActiveTexture(GL_TEXTURE1)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:position))
|
||||
|
||||
glActiveTexture(GL_TEXTURE2)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:texcoord))
|
||||
|
||||
glActiveTexture(GL_TEXTURE3)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:normal))
|
||||
|
||||
glActiveTexture(GL_TEXTURE4)
|
||||
glBindTexture(GL_TEXTURE_2D, @g_buffer.texture(:depth))
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def post_processing
|
||||
end
|
||||
|
||||
def render_framebuffer
|
||||
if Shader.available?("render_screen")
|
||||
Shader.use("render_screen") do |shader|
|
||||
if Shader.available?("deferred_lighting")
|
||||
Shader.use("deferred_lighting") do |shader|
|
||||
glBindVertexArray(@g_buffer.screen_vbo)
|
||||
glEnableVertexAttribArray(0)
|
||||
glEnableVertexAttribArray(1)
|
||||
|
||||
glDisable(GL_DEPTH_TEST)
|
||||
glEnable(GL_BLEND)
|
||||
@@ -134,8 +157,6 @@ class IMICFPS
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, @g_buffer.vertices.size)
|
||||
|
||||
glDisableVertexAttribArray(1)
|
||||
glDisableVertexAttribArray(0)
|
||||
glBindVertexArray(0)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,7 +10,7 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def preload_default_shaders
|
||||
shaders = ["default", "render_screen"]
|
||||
shaders = ["g_buffer", "deferred_lighting"]
|
||||
shaders.each do |shader|
|
||||
Shader.new(
|
||||
name: shader,
|
||||
|
||||
@@ -8,15 +8,6 @@ class IMICFPS
|
||||
window.needs_cursor = false
|
||||
@manifest = @options[:manifest]
|
||||
|
||||
if window.config.get(:debug_options, :use_shaders) && !Shader.available?("default")
|
||||
Shader.new(
|
||||
name: "default",
|
||||
includes_dir: "shaders/include",
|
||||
vertex: "shaders/vertex/default.glsl",
|
||||
fragment: "shaders/fragment/default.glsl"
|
||||
)
|
||||
end
|
||||
|
||||
@map = ProtoMap.new
|
||||
Publisher.new
|
||||
|
||||
@@ -27,7 +18,7 @@ class IMICFPS
|
||||
@crosshair = Crosshair.new(color: Gosu::Color.rgba(100, 200, 255, 100))
|
||||
|
||||
@lights = []
|
||||
@light = Light.new(id: available_light, position: Vector.new, diffuse: Vector.new(1, 1, 1, 1))
|
||||
@light = Light.new(type: Light::DIRECTIONAL, id: available_light, position: Vector.new, diffuse: Vector.new(1, 1, 1, 1))
|
||||
@lights << @light
|
||||
|
||||
@camera = Camera.new(position: Vector.new(0, 1.5, 5), orientation: Vector.forward)
|
||||
|
||||
Reference in New Issue
Block a user