mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Refactored lights, use light manager
This commit is contained in:
@@ -1,4 +1,25 @@
|
||||
class IMICFPS
|
||||
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
|
||||
|
||||
@@ -1,46 +1,43 @@
|
||||
class IMICFPS
|
||||
class Light
|
||||
include OpenGL
|
||||
MAX_LIGHTS = GL_MAX_LIGHTS-1
|
||||
attr_reader :x, :y, :z, :ambient, :diffuse, :specular, :position
|
||||
def self.number_of_lights
|
||||
@number_of_lights ||= 0
|
||||
end
|
||||
attr_reader :ambient, :diffuse, :specular, :position, :light_id
|
||||
attr_accessor :x, :y, :z, :intensity
|
||||
def initialize(x:,y:,z:, ambient: Vertex.new(0.5, 0.5, 0.5, 1),
|
||||
diffuse: Vertex.new(1, 0.5, 0, 1), specular: Vertex.new(0.2, 0.2, 0.2, 1),
|
||||
position: Vertex.new(x, y, z, 0), intensity: 1)
|
||||
@x,@y,@z = x,y,z
|
||||
@intensity = intensity
|
||||
|
||||
# use as Light.number_of_lights+=n
|
||||
def self.number_of_lights=(int)
|
||||
@number_of_lights = int
|
||||
end
|
||||
|
||||
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*")
|
||||
self.ambient = ambient
|
||||
self.diffuse = diffuse
|
||||
self.specular = specular
|
||||
self.position = position
|
||||
@light_id = available_light
|
||||
|
||||
LightManager.add_light(self)
|
||||
end
|
||||
|
||||
def available_light
|
||||
raise "Using to many lights, #{Light.number_of_lights}/#{MAX_LIGHTS}" if Light.number_of_lights > MAX_LIGHTS
|
||||
Light.number_of_lights+=1
|
||||
puts "OpenGL::GL_LIGHT#{Light.number_of_lights}"
|
||||
@light_id = Object.const_get "OpenGL::GL_LIGHT#{Light.number_of_lights}"
|
||||
raise "Using to many lights, #{LightManager.light_count}/#{LightManager::MAX_LIGHTS}" if LightManager.light_count > LightManager::MAX_LIGHTS
|
||||
puts "OpenGL::GL_LIGHT#{LightManager.light_count}"
|
||||
@light_id = Object.const_get "OpenGL::GL_LIGHT#{LightManager.light_count}"
|
||||
end
|
||||
|
||||
def ambient=(array)
|
||||
@ambient = array.pack("f*")
|
||||
def ambient=(color)
|
||||
@ambient = convert(color).pack("f*")
|
||||
end
|
||||
|
||||
def diffuse=(array)
|
||||
@diffuse = array.pack("f*")
|
||||
def diffuse=(color)
|
||||
@diffuse = convert(color, true).pack("f*")
|
||||
end
|
||||
|
||||
def specular=(array)
|
||||
@specular = array.pack("f*")
|
||||
def specular=(color)
|
||||
@specular = convert(color, true).pack("f*")
|
||||
end
|
||||
|
||||
def position=(array)
|
||||
@position = array.pack("f*")
|
||||
def position=(vertex)
|
||||
@position = convert(vertex).pack("f*")
|
||||
end
|
||||
|
||||
def draw
|
||||
@@ -52,5 +49,13 @@ class IMICFPS
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(@light_id)
|
||||
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
|
||||
|
||||
@@ -35,16 +35,8 @@ class IMICFPS
|
||||
@font = Gosu::Font.new(18, name: "DejaVu Sans")
|
||||
@text = "Hello There"
|
||||
|
||||
@ambient_light = [0.5, 0.5, 0.5, 1]
|
||||
@diffuse_light = [1, 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
|
||||
Light.new(x: 3, y: -6, z: 6)
|
||||
Light.new(x: 0, y: -100, z: 0, diffuse: Color.new(1.0, 0.5, 0.1))
|
||||
end
|
||||
|
||||
def draw
|
||||
@@ -58,10 +50,10 @@ class IMICFPS
|
||||
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
|
||||
|
||||
LightManager.lights.each do |light|
|
||||
light.draw
|
||||
end
|
||||
@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|
|
||||
object.draw if object.visible && object.renderable
|
||||
|
||||
Reference in New Issue
Block a user