Refactored lights, use light manager

This commit is contained in:
2018-03-20 20:17:33 -05:00
parent 57c52cd725
commit d390d3d728
3 changed files with 58 additions and 40 deletions

View File

@@ -1,4 +1,25 @@
class IMICFPS class IMICFPS
class LightManager class LightManager
MAX_LIGHTS = OpenGL::GL_MAX_LIGHTS
LIGHTS = []
def self.add_light(model)
LIGHTS << model
end
def self.find_light()
end
def self.lights
LIGHTS
end
def self.light_count
LIGHTS.count+1
end
def self.clear_lights
LIGHTS.clear
end
end end
end end

View File

@@ -1,46 +1,43 @@
class IMICFPS class IMICFPS
class Light class Light
include OpenGL include OpenGL
MAX_LIGHTS = GL_MAX_LIGHTS-1 attr_reader :ambient, :diffuse, :specular, :position, :light_id
attr_reader :x, :y, :z, :ambient, :diffuse, :specular, :position attr_accessor :x, :y, :z, :intensity
def self.number_of_lights def initialize(x:,y:,z:, ambient: Vertex.new(0.5, 0.5, 0.5, 1),
@number_of_lights ||= 0 diffuse: Vertex.new(1, 0.5, 0, 1), specular: Vertex.new(0.2, 0.2, 0.2, 1),
end position: Vertex.new(x, y, z, 0), intensity: 1)
@x,@y,@z = x,y,z
@intensity = intensity
# use as Light.number_of_lights+=n self.ambient = ambient
def self.number_of_lights=(int) self.diffuse = diffuse
@number_of_lights = int self.specular = specular
end self.position = position
def initialize(x,y,z)
@ambient = [0.0, 0.0, 0.0, 1].pack("f*")
@diffuse = [1, 0.5, 0, 1].pack("f*")
@specular = [0.0, 0.0, 0.0, 1].pack("f*")
@position = [0, 0, 0, 0].pack("f*")
@light_id = available_light @light_id = available_light
LightManager.add_light(self)
end end
def available_light def available_light
raise "Using to many lights, #{Light.number_of_lights}/#{MAX_LIGHTS}" if Light.number_of_lights > MAX_LIGHTS raise "Using to many lights, #{LightManager.light_count}/#{LightManager::MAX_LIGHTS}" if LightManager.light_count > LightManager::MAX_LIGHTS
Light.number_of_lights+=1 puts "OpenGL::GL_LIGHT#{LightManager.light_count}"
puts "OpenGL::GL_LIGHT#{Light.number_of_lights}" @light_id = Object.const_get "OpenGL::GL_LIGHT#{LightManager.light_count}"
@light_id = Object.const_get "OpenGL::GL_LIGHT#{Light.number_of_lights}"
end end
def ambient=(array) def ambient=(color)
@ambient = array.pack("f*") @ambient = convert(color).pack("f*")
end end
def diffuse=(array) def diffuse=(color)
@diffuse = array.pack("f*") @diffuse = convert(color, true).pack("f*")
end end
def specular=(array) def specular=(color)
@specular = array.pack("f*") @specular = convert(color, true).pack("f*")
end end
def position=(array) def position=(vertex)
@position = array.pack("f*") @position = convert(vertex).pack("f*")
end end
def draw def draw
@@ -52,5 +49,13 @@ class IMICFPS
glEnable(GL_LIGHTING) glEnable(GL_LIGHTING)
glEnable(@light_id) glEnable(@light_id)
end end
def convert(struct, apply_intensity = false)
if apply_intensity
return struct.to_a.compact.map{|i| i*@intensity}
else
return struct.to_a.compact
end
end
end end
end end

View File

@@ -35,16 +35,8 @@ class IMICFPS
@font = Gosu::Font.new(18, name: "DejaVu Sans") @font = Gosu::Font.new(18, name: "DejaVu Sans")
@text = "Hello There" @text = "Hello There"
@ambient_light = [0.5, 0.5, 0.5, 1] Light.new(x: 3, y: -6, z: 6)
@diffuse_light = [1, 0.5, 0, 1] Light.new(x: 0, y: -100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1))
@specular_light = [0.2, 0.2, 0.2, 1]
@light_position = [3, 6, 6, 0]
@camera_light = Light.new(0,0,0)
@camera_light.ambient = @ambient_light
@camera_light.diffuse = @diffuse_light
@camera_light.specular = @specular_light
@camera_light.position = @light_position
end end
def draw def draw
@@ -58,10 +50,10 @@ class IMICFPS
glClearColor(0,0.2,0.5,1) # skyish blue glClearColor(0,0.2,0.5,1) # skyish blue
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen and the depth buffer
LightManager.lights.each do |light|
light.draw
end
@camera.draw @camera.draw
@camera_light.draw
# gluLookAt(@camera.x,@camera.y,@camera.z, @horizontal_angle,@vertical_angle,0, 0,1,0)
ObjectManager.objects.each do |object| ObjectManager.objects.each do |object|
object.draw if object.visible && object.renderable object.draw if object.visible && object.renderable