mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Did some reorganizing
This commit is contained in:
11
i-mic-fps.rb
11
i-mic-fps.rb
@@ -18,13 +18,16 @@ end
|
|||||||
|
|
||||||
BoundingBox = Struct.new(:min_x, :min_y, :min_z, :max_x, :max_y, :max_z)
|
BoundingBox = Struct.new(:min_x, :min_y, :min_z, :max_x, :max_y, :max_z)
|
||||||
|
|
||||||
$debug = true if ARGV.join.include?("--debug")
|
$debug = ARGV.join.include?("--debug") ? true : false
|
||||||
|
|
||||||
|
require_relative "lib/managers/object_manager"
|
||||||
|
require_relative "lib/managers/light_manager"
|
||||||
|
|
||||||
require_relative "lib/objects/light"
|
require_relative "lib/objects/light"
|
||||||
require_relative "lib/wavefront/parser"
|
require_relative "lib/objects/model"
|
||||||
|
|
||||||
require_relative "lib/wavefront/model"
|
require_relative "lib/wavefront/model"
|
||||||
require_relative "lib/wavefront/object"
|
|
||||||
require_relative "lib/wavefront/material"
|
|
||||||
require_relative "lib/window"
|
require_relative "lib/window"
|
||||||
|
|
||||||
MODEL_METER_SCALE = 0.001 # Objects exported from blender using the millimeter object scale will be close to 1 GL unit
|
MODEL_METER_SCALE = 0.001 # Objects exported from blender using the millimeter object scale will be close to 1 GL unit
|
||||||
|
|||||||
4
lib/managers/light_manager.rb
Normal file
4
lib/managers/light_manager.rb
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
class IMICFPS
|
||||||
|
class LightManager
|
||||||
|
end
|
||||||
|
end
|
||||||
20
lib/managers/object_manager.rb
Normal file
20
lib/managers/object_manager.rb
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
class IMICFPS
|
||||||
|
TextureCoordinate = Struct.new(:u, :v, :weight)
|
||||||
|
Vertex = Struct.new(:x, :y, :z, :weight)
|
||||||
|
Point = Struct.new(:x, :y)
|
||||||
|
Color = Struct.new(:red, :green, :blue, :alpha)
|
||||||
|
|
||||||
|
class ObjectManager
|
||||||
|
OBJECTS = []
|
||||||
|
def self.add_object(model)
|
||||||
|
OBJECTS << model
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.find_object()
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.objects
|
||||||
|
OBJECTS
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
97
lib/objects/model.rb
Normal file
97
lib/objects/model.rb
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
class IMICFPS
|
||||||
|
class Model
|
||||||
|
def self.supported_models
|
||||||
|
["Wavefront OBJ"]
|
||||||
|
end
|
||||||
|
|
||||||
|
CACHE = {}
|
||||||
|
|
||||||
|
include OpenGL
|
||||||
|
include GLU
|
||||||
|
|
||||||
|
attr_accessor :x, :y, :z, :scale
|
||||||
|
attr_accessor :visible, :renderable
|
||||||
|
attr_accessor :x_rotation, :y_rotation, :z_rotation
|
||||||
|
|
||||||
|
def initialize(type:, file_path:, x: 0, y: 0, z: 0, scale: MODEL_METER_SCALE, backface_culling: true)
|
||||||
|
@type = type
|
||||||
|
@file_path = file_path
|
||||||
|
@x,@y,@z,@scale = x,y,z,scale
|
||||||
|
@backface_culling = backface_culling
|
||||||
|
@visible = true
|
||||||
|
@renderable = true
|
||||||
|
@x_rotation,@y_rotation,@z_rotation = 0,0,0
|
||||||
|
|
||||||
|
@model = nil
|
||||||
|
|
||||||
|
unless load_model_from_cache
|
||||||
|
case type
|
||||||
|
when :obj
|
||||||
|
p file_path
|
||||||
|
@model = Wavefront::Model.new(@file_path)
|
||||||
|
else
|
||||||
|
raise "Unsupported model type, supported models are: #{Model.supported_models.join(', ')}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
cache_model
|
||||||
|
|
||||||
|
ObjectManager.add_object(self)
|
||||||
|
|
||||||
|
setup
|
||||||
|
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
handleGlError
|
||||||
|
|
||||||
|
glEnable(GL_NORMALIZE)
|
||||||
|
glPushMatrix
|
||||||
|
glTranslatef(x,y,z)
|
||||||
|
glScalef(scale, scale, scale)
|
||||||
|
glRotatef(@x_rotation,1.0, 0, 0)
|
||||||
|
glRotatef(@y_rotation,0, 1.0, 0)
|
||||||
|
glRotatef(@z_rotation,0, 0, 1.0)
|
||||||
|
|
||||||
|
handleGlError
|
||||||
|
@model.draw(@x, @y, @z, @scale, @backface_culling)
|
||||||
|
handleGlError
|
||||||
|
|
||||||
|
glPopMatrix
|
||||||
|
handleGlError
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_model_from_cache
|
||||||
|
found = false
|
||||||
|
if CACHE[@type].is_a?(Hash)
|
||||||
|
if CACHE[@type][@file_path]
|
||||||
|
@model = CACHE[@type][@file_path]
|
||||||
|
puts "Used cached model for: #{@file_path.split('/').last}"
|
||||||
|
found = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return found
|
||||||
|
end
|
||||||
|
|
||||||
|
def cache_model
|
||||||
|
CACHE[@type] = {} unless CACHE[@type].is_a?(Hash)
|
||||||
|
CACHE[@type][@file_path] = @model
|
||||||
|
end
|
||||||
|
|
||||||
|
def handleGlError
|
||||||
|
e = glGetError()
|
||||||
|
if e != GL_NO_ERROR
|
||||||
|
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -6,9 +6,9 @@ class IMICFPS
|
|||||||
attr_reader :texture
|
attr_reader :texture
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name
|
||||||
@ambient = Wavefront::Model::Color.new(1, 1, 1, 1)
|
@ambient = Color.new(1, 1, 1, 1)
|
||||||
@diffuse = Wavefront::Model::Color.new(1, 1, 1, 1)
|
@diffuse = Color.new(1, 1, 1, 1)
|
||||||
@specular= Wavefront::Model::Color.new(1, 1, 1, 1)
|
@specular= Color.new(1, 1, 1, 1)
|
||||||
@texture = nil
|
@texture = nil
|
||||||
@texture_id = nil
|
@texture_id = nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
require_relative "parser"
|
||||||
|
require_relative "object"
|
||||||
|
require_relative "material"
|
||||||
|
|
||||||
class IMICFPS
|
class IMICFPS
|
||||||
class Wavefront
|
class Wavefront
|
||||||
class Model
|
class Model
|
||||||
@@ -47,25 +51,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handleGlError
|
def draw(x,y,z, scale, back_face_culling)
|
||||||
e = glGetError()
|
|
||||||
if e != GL_NO_ERROR
|
|
||||||
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def draw(x, y, z, scale = MODEL_METER_SCALE, back_face_culling = true)
|
|
||||||
handleGlError
|
|
||||||
render(x,y,z, scale, back_face_culling)
|
|
||||||
handleGlError
|
|
||||||
end
|
|
||||||
|
|
||||||
def render(x,y,z, scale, back_face_culling)
|
|
||||||
glEnable(GL_NORMALIZE)
|
|
||||||
glPushMatrix
|
|
||||||
glTranslatef(x,y,z)
|
|
||||||
glScalef(scale, scale, scale)
|
|
||||||
@objects.each_with_index do |o, i|
|
@objects.each_with_index do |o, i|
|
||||||
glEnable(GL_CULL_FACE) if back_face_culling
|
glEnable(GL_CULL_FACE) if back_face_culling
|
||||||
glEnable(GL_COLOR_MATERIAL)
|
glEnable(GL_COLOR_MATERIAL)
|
||||||
@@ -95,12 +81,11 @@ class IMICFPS
|
|||||||
# glBindTexture(GL_TEXTURE_2D, 0)
|
# glBindTexture(GL_TEXTURE_2D, 0)
|
||||||
glDisable(GL_TEXTURE_2D)
|
glDisable(GL_TEXTURE_2D)
|
||||||
end
|
end
|
||||||
|
render_bounding_box(o.bounding_box, o.debug_color) if $debug
|
||||||
glDisable(GL_CULL_FACE) if back_face_culling
|
glDisable(GL_CULL_FACE) if back_face_culling
|
||||||
glDisable(GL_COLOR_MATERIAL)
|
glDisable(GL_COLOR_MATERIAL)
|
||||||
render_bounding_box(o.bounding_box, o.debug_color) if $debug
|
|
||||||
end
|
end
|
||||||
render_bounding_box(@bounding_box) if $debug
|
render_bounding_box(@bounding_box) if $debug
|
||||||
glPopMatrix
|
|
||||||
|
|
||||||
|
|
||||||
$window.number_of_faces+=self.faces.size
|
$window.number_of_faces+=self.faces.size
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class IMICFPS
|
|||||||
@normals = []
|
@normals = []
|
||||||
@faces = []
|
@faces = []
|
||||||
@bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil)
|
@bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil)
|
||||||
@debug_color = Parser::Color.new(1.0,0.0,0.0)
|
@debug_color = Color.new(1.0,0.0,0.0)
|
||||||
|
|
||||||
# Faces array packs everything:
|
# Faces array packs everything:
|
||||||
# vertex = index[0]
|
# vertex = index[0]
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
class IMICFPS
|
class IMICFPS
|
||||||
class Wavefront
|
class Wavefront
|
||||||
module Parser
|
module Parser
|
||||||
TextureCoordinate = Struct.new(:u, :v, :weight)
|
|
||||||
Vertex = Struct.new(:x, :y, :z, :weight)
|
|
||||||
Color = Struct.new(:red, :green, :blue, :alpha)
|
|
||||||
|
|
||||||
def parse
|
def parse
|
||||||
lines = 0
|
lines = 0
|
||||||
@file.each_line do |line|
|
@file.each_line do |line|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ class IMICFPS
|
|||||||
include OpenGL
|
include OpenGL
|
||||||
include GLU
|
include GLU
|
||||||
# include GLUT
|
# include GLUT
|
||||||
Point = Struct.new(:x, :y)
|
|
||||||
|
|
||||||
attr_accessor :number_of_faces, :needs_cursor
|
attr_accessor :number_of_faces, :needs_cursor
|
||||||
|
|
||||||
@@ -19,15 +18,16 @@ class IMICFPS
|
|||||||
@delta_time = Gosu.milliseconds
|
@delta_time = Gosu.milliseconds
|
||||||
@number_of_faces = 0
|
@number_of_faces = 0
|
||||||
@draw_skydome = true
|
@draw_skydome = true
|
||||||
@skydome = Wavefront::Model.new("objects/skydome.obj")
|
Model.new(type: :obj, file_path: "objects/skydome.obj", x: 0, y: 0,z: 0, scale: 1, backface_culling: false)
|
||||||
@plane = Wavefront::Model.new("objects/plane.obj")
|
Model.new(type: :obj, file_path: "objects/cube.obj", x: 0,y: 1,z: -2, scale: 0.0005)
|
||||||
@cube = Wavefront::Model.new("objects/cube.obj")
|
Model.new(type: :obj, file_path: "objects/biped.obj", x: 1, y: 0, z: 0)
|
||||||
@model = Wavefront::Model.new("objects/biped.obj")
|
Model.new(type: :obj, file_path: "objects/tree.obj", x: 3)
|
||||||
@tree = Wavefront::Model.new("objects/tree.obj")
|
Model.new(type: :obj, file_path: "objects/tree.obj", z: -5)
|
||||||
@mega_model = Wavefront::Model.new("objects/sponza.obj")
|
Model.new(type: :obj, file_path: "objects/tree.obj", x: -2, z: -6)
|
||||||
|
Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2)
|
||||||
|
|
||||||
@camera = Wavefront::Model::Vertex.new(0,-1,0)
|
@camera = Vertex.new(0,-1,0)
|
||||||
@camera_target = Wavefront::Model::Vertex.new(0,-1,0)
|
@camera_target = Vertex.new(0,-1,0)
|
||||||
@speed = 0.05
|
@speed = 0.05
|
||||||
@old_speed = @speed
|
@old_speed = @speed
|
||||||
@vertical_angle = 0.0 # |
|
@vertical_angle = 0.0 # |
|
||||||
@@ -35,7 +35,7 @@ class IMICFPS
|
|||||||
self.mouse_x, self.mouse_y = Gosu.screen_width/2, Gosu.screen_height/2
|
self.mouse_x, self.mouse_y = Gosu.screen_width/2, Gosu.screen_height/2
|
||||||
@true_mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2)
|
@true_mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2)
|
||||||
@true_mouse_checked = 0
|
@true_mouse_checked = 0
|
||||||
@mouse_sesitivity = 5.0
|
@mouse_sesitivity = 20.0
|
||||||
@initial_fov = 70.0
|
@initial_fov = 70.0
|
||||||
|
|
||||||
@crosshair_size = 10
|
@crosshair_size = 10
|
||||||
@@ -81,22 +81,17 @@ class IMICFPS
|
|||||||
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
|
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
|
||||||
glLoadIdentity
|
glLoadIdentity
|
||||||
|
|
||||||
@camera_light.draw
|
|
||||||
glEnable(GL_DEPTH_TEST)
|
glEnable(GL_DEPTH_TEST)
|
||||||
|
@camera_light.draw
|
||||||
|
|
||||||
glRotatef(@vertical_angle,1,0,0)
|
glRotatef(@vertical_angle,1,0,0)
|
||||||
glRotatef(@horizontal_angle,0,1,0)
|
glRotatef(@horizontal_angle,0,1,0)
|
||||||
glTranslatef(@camera.x, @camera.y, @camera.z)
|
glTranslatef(@camera.x, @camera.y, @camera.z)
|
||||||
# gluLookAt(@camera.x,@camera.y,@camera.z, @horizontal_angle,@vertical_angle,0, 0,1,0)
|
# gluLookAt(@camera.x,@camera.y,@camera.z, @horizontal_angle,@vertical_angle,0, 0,1,0)
|
||||||
|
|
||||||
@skydome.draw(0,0,0, 1, false) if @draw_skydome
|
ObjectManager.objects.each do |object|
|
||||||
@cube.draw(0,1,-2, 0.0005)
|
object.draw if object.visible && object.renderable
|
||||||
@plane.draw(0,-1,-4, 0.0005, false)
|
end
|
||||||
@model.draw(1, 0, 0)
|
|
||||||
@tree.draw(5, 0, 0)
|
|
||||||
@tree.draw(5, 0, 3)
|
|
||||||
@tree.draw(3, 0, 10)
|
|
||||||
# @mega_model.draw(0,0,0, 1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Draw crosshair
|
# Draw crosshair
|
||||||
@@ -109,6 +104,7 @@ class IMICFPS
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
@last_frame_time = Gosu.milliseconds
|
||||||
@text = "OpenGL Vendor: #{glGetString(GL_VENDOR)}~
|
@text = "OpenGL Vendor: #{glGetString(GL_VENDOR)}~
|
||||||
OpenGL Renderer: #{glGetString(GL_RENDERER)} ~
|
OpenGL Renderer: #{glGetString(GL_RENDERER)} ~
|
||||||
OpenGL Version: #{glGetString(GL_VERSION)}~
|
OpenGL Version: #{glGetString(GL_VERSION)}~
|
||||||
@@ -122,7 +118,10 @@ class IMICFPS
|
|||||||
~
|
~
|
||||||
Draw Skydome: #{@draw_skydome}~
|
Draw Skydome: #{@draw_skydome}~
|
||||||
Debug mode: <c=992200>#{$debug}</b>~"
|
Debug mode: <c=992200>#{$debug}</b>~"
|
||||||
@last_frame_time = Gosu.milliseconds
|
|
||||||
|
ObjectManager.objects.each do |object|
|
||||||
|
object.update
|
||||||
|
end
|
||||||
|
|
||||||
if @true_mouse_checked > 2
|
if @true_mouse_checked > 2
|
||||||
@horizontal_angle-=Float(@true_mouse.x-self.mouse_x)/@mouse_sesitivity
|
@horizontal_angle-=Float(@true_mouse.x-self.mouse_x)/@mouse_sesitivity
|
||||||
|
|||||||
Reference in New Issue
Block a user