Fixed immediate mode lighting, improved-ish modern gl lighting

This commit is contained in:
2019-12-09 11:48:54 -06:00
parent ff1d34deec
commit e4333a82b8
6 changed files with 146 additions and 27 deletions

View File

@@ -1,26 +1,35 @@
class IMICFPS
class Light
DIRECTIONAL = 0
POINT = 1
attr_reader :light_id
attr_accessor :ambient, :diffuse, :specular, :position, :intensity
def initialize(id:,
ambient: Vector.new(0.5, 0.5, 0.5, 1),
diffuse: Vector.new(1, 0.5, 0, 1), specular: Vector.new(0.2, 0.2, 0.2, 1),
position: Vector.new(0, 0, 0, 0), intensity: 1
attr_accessor :type, :ambient, :diffuse, :specular, :position, :intensity
def initialize(
id:,
type: Light::POINT,
ambient: Vector.new(0.5, 0.5, 0.5, 1),
diffuse: Vector.new(1, 0.5, 0, 1),
specular: Vector.new(0.2, 0.2, 0.2, 1),
position: Vector.new(0, 0, 0, 0),
intensity: 1
)
@light_id = id
@intensity = intensity
@type = type
@ambient = ambient
@diffuse = diffuse
@specular = specular
@position = position
@intensity = intensity
end
def draw
glLightfv(@light_id, GL_AMBIENT, @ambient)
glLightfv(@light_id, GL_DIFFUSE, @diffuse)
glLightfv(@light_id, GL_SPECULAR, @specular)
glLightfv(@light_id, GL_POSITION, @position)
glLightfv(@light_id, GL_AMBIENT, convert(@ambient).pack("f*"))
glLightfv(@light_id, GL_DIFFUSE, convert(@diffuse, true).pack("f*"))
glLightfv(@light_id, GL_SPECULAR, convert(@specular, true).pack("f*"))
glLightfv(@light_id, GL_POSITION, convert(@position).pack("f*"))
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)
glEnable(GL_LIGHTING)
glEnable(@light_id)
@@ -28,7 +37,7 @@ class IMICFPS
def convert(struct, apply_intensity = false)
if apply_intensity
return struct.to_a.compact.map{|i| i*@intensity}
return struct.to_a.compact.map{ |i| i * @intensity }
else
return struct.to_a.compact
end

View File

@@ -30,7 +30,7 @@ class IMICFPS
# TODO: Load lights from MapLoader
add_light(Light.new(id: available_light, position: Vector.new(30, 10.0, 30)))
# add_light(Light.new(id: available_light, x: 0, y: 100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1)))
add_light(Light.new(id: available_light, position: Vector.new(0, 100, 0), diffuse: Color.new(1.0, 0.5, 0.1)))
end
def data

View File

@@ -225,5 +225,11 @@ class IMICFPS
def has_texture?
@has_texture
end
def release_gl_resources
if @vertex_array_id
end
end
end
end

View File

@@ -9,13 +9,23 @@ class IMICFPS
def draw_object(camera, lights, object)
if Shader.available?("default")
Shader.use("default") do |shader|
shader.set_uniform("projection", camera.projection_matrix)
shader.set_uniform("view", camera.view_matrix)
shader.set_uniform("model", object.model_matrix)
shader.set_uniform("hasTexture", object.model.has_texture?)
shader.uniform_transform("projection", camera.projection_matrix)
shader.uniform_transform("view", camera.view_matrix)
shader.uniform_transform("model", object.model_matrix)
shader.uniform_boolean("hasTexture", object.model.has_texture?)
shader.uniform_vec3("cameraPosition", camera.position)
# TODO: Upload and use lights
shader.set_uniform("lightPos", lights.first.position)
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.uniform_float("totalLights", lights.size)
handleGlError
draw_model(object.model)