Broken mouse input, texture mapping issue persists.

This commit is contained in:
2018-03-19 22:08:20 -05:00
parent a46f3deff9
commit 89c84dbe39
11 changed files with 1768 additions and 7853 deletions

View File

@@ -18,6 +18,8 @@ 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")
require_relative "lib/objects/light" require_relative "lib/objects/light"
require_relative "lib/wavefront/parser" require_relative "lib/wavefront/parser"
require_relative "lib/wavefront/model" require_relative "lib/wavefront/model"
@@ -25,6 +27,6 @@ require_relative "lib/wavefront/object"
require_relative "lib/wavefront/material" require_relative "lib/wavefront/material"
require_relative "lib/window" require_relative "lib/window"
MODEL_METER_SCALE = 0.0009 # 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
IMICFPS::Window.new.show IMICFPS::Window.new.show

View File

@@ -15,19 +15,21 @@ class IMICFPS
def set_texture(texture_path) def set_texture(texture_path)
puts "#{name} texture #{texture_path}" puts "#{name} texture #{texture_path}"
@texture = Gosu::Image.new(texture_path) @texture = Gosu::Image.new(texture_path, retro: true)
array_of_pixels = @texture.to_blob array_of_pixels = @texture.to_blob
if @texture.gl_tex_info if @texture.gl_tex_info
@texture_id = @texture.gl_tex_info.tex_name @texture_id = @texture.gl_tex_info.tex_name
else else
puts "Allocating..."
tex_names_buf = ' ' * 8 tex_names_buf = ' ' * 8
glGenTextures(1, tex_names_buf) glGenTextures(1, tex_names_buf)
@texture_id = tex_names_buf.unpack('L2').first @texture_id = tex_names_buf.unpack('L2').first
end end
glBindTexture(GL_TEXTURE_2D, @texture_id) glBindTexture(GL_TEXTURE_2D, @texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @texture.width, @texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels) unless @texture.gl_tex_info glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @texture.width, @texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glGenerateMipmap(GL_TEXTURE_2D)
end end
def texture_id def texture_id

View File

@@ -25,7 +25,7 @@ class IMICFPS
@faces = [] @faces = []
@smoothing= 0 @smoothing= 0
@bounding_box = BoundingBox.new(0.0,0.0,0.0, 0.0,0.0,0.0) @bounding_box = BoundingBox.new(nil,nil,nil, nil,nil,nil)
@debug_color = Color.new(rand(0.0..1.0), rand(0.0..1.0), rand(0.0..1.0)) @debug_color = Color.new(rand(0.0..1.0), rand(0.0..1.0), rand(0.0..1.0))
start_time = Time.now start_time = Time.now
parse parse
@@ -35,7 +35,7 @@ class IMICFPS
face_count = 0 face_count = 0
@objects.each {|o| face_count+=o.faces.size} @objects.each {|o| face_count+=o.faces.size}
@objects.each_with_index do |o, i| @objects.each_with_index do |o, i|
puts "OBJECT FACES: Name: #{o.name} #{o.faces.size}, array size divided by 3: #{o.faces.size.to_f/3.0}" puts "Model::Object Name: #{o.name} Faces: #{o.faces.size}, array size divided by 3: #{o.faces.size.to_f/3.0}"
end end
$window.number_of_faces+=face_count $window.number_of_faces+=face_count
@model_has_texture = false @model_has_texture = false
@@ -97,9 +97,9 @@ class IMICFPS
end end
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) if ARGV.join("--debug") render_bounding_box(o.bounding_box, o.debug_color) if $debug
end end
render_bounding_box(@bounding_box) if ARGV.join("--debug") render_bounding_box(@bounding_box) if $debug
glPopMatrix glPopMatrix

View File

@@ -1,7 +1,7 @@
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Object class Object
attr_reader :name, :vertices, :textures, :normals, :bounding_box attr_reader :name, :vertices, :textures, :normals, :bounding_box, :debug_color
attr_accessor :faces attr_accessor :faces
def initialize(name) def initialize(name)
@@ -11,6 +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)
# Faces array packs everything: # Faces array packs everything:
# vertex = index[0] # vertex = index[0]
@@ -21,6 +22,8 @@ class IMICFPS
def flattened_vertices def flattened_vertices
unless @vertices_list unless @vertices_list
@debug_color = @faces.first[3].diffuse
list = [] list = []
@faces.each do |face| @faces.each do |face|
[face[0]].each do |v| [face[0]].each do |v|

View File

@@ -141,9 +141,9 @@ class IMICFPS
def add_texture_coordinate(array) def add_texture_coordinate(array)
texture = nil texture = nil
if array.size == 4 if array.size == 4
texture = Vertex.new(Float(array[1]), Float(array[2]), Float(array[3])) texture = Vertex.new(Float(array[1]), 1-Float(array[2]), Float(array[3]))
elsif array.size == 3 elsif array.size == 3
texture = Vertex.new(Float(array[1]), Float(array[2]), 0.0) texture = Vertex.new(Float(array[1]), 1-Float(array[2]), 0.0)
else else
raise raise
end end

View File

@@ -11,29 +11,28 @@ class IMICFPS
super(window_width, window_height, fullscreen) super(window_width, window_height, fullscreen)
# super(Gosu.screen_width, Gosu.screen_height, true) # super(Gosu.screen_width, Gosu.screen_height, true)
$window = self $window = self
@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") # @skydome = Wavefront::Model.new("objects/skydome.obj")
@model = Wavefront::Model.new("objects/biped.obj") @cube = Wavefront::Model.new("objects/cube.obj")
# @scene = Wavefront::Model.new("objects/cube.obj") # @model = Wavefront::Model.new("objects/biped.obj")
@tree = Wavefront::Model.new("objects/tree.obj") # @tree = Wavefront::Model.new("objects/tree.obj")
@mega_model = Wavefront::Model.new("objects/sponza.obj") # @mega_model = Wavefront::Model.new("objects/sponza.obj")
@camera = Wavefront::Model::Vertex.new(0,-1,0) @camera = Wavefront::Model::Vertex.new(0,-1,0)
@camera_target = Wavefront::Model::Vertex.new(0,-1,0) @camera_target = Wavefront::Model::Vertex.new(0,-1,0)
@speed = 0.05 @speed = 0.05
@angle_y = 0.0 # | @old_speed = @speed
@angle_x = 0.0 # _ @vertical_angle = 0.0 # |
@mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2) @horizontal_angle = 0.0 # _
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
@mouse = Point.new(Gosu.screen_width/2, Gosu.screen_height/2)
@mouse_sesitivity = 5.0 @mouse_sesitivity = 5.0
@font = Gosu::Font.new(18, name: "DejaVu Sans") @font = Gosu::Font.new(18, name: "DejaVu Sans")
@text = "Hello There" @text = "Hello There"
@last_frame_time = 0
@tick = 0
@c1, @c2, @c3 = rand(0.0..1.0), rand(0.0..1.0), rand(0.0..1.0)
@ambient_light = [0.5, 0.5, 0.5, 1] @ambient_light = [0.5, 0.5, 0.5, 1]
@diffuse_light = [1, 0.5, 0, 1] @diffuse_light = [1, 0.5, 0, 1]
@@ -65,7 +64,8 @@ class IMICFPS
glMatrixMode(GL_PROJECTION) # The projection matrix is responsible for adding perspective to our scene. glMatrixMode(GL_PROJECTION) # The projection matrix is responsible for adding perspective to our scene.
glLoadIdentity # Resets current modelview matrix glLoadIdentity # Resets current modelview matrix
# Calculates aspect ratio of the window. Gets perspective view. 45 is degree viewing angle, (0.1, 100) are ranges how deep can we draw into the screen # Calculates aspect ratio of the window. Gets perspective view. 45 is degree viewing angle, (0.1, 100) are ranges how deep can we draw into the screen
gluPerspective(90.0, width / height, 0.1, 1000.0) gluPerspective(70.0, width / height, 0.1, 1000.0)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
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
@@ -73,18 +73,17 @@ class IMICFPS
@camera_light.draw @camera_light.draw
glEnable(GL_DEPTH_TEST) glEnable(GL_DEPTH_TEST)
glRotatef(@angle_y,1,0,0) glRotatef(@vertical_angle,1,0,0)
glRotatef(@angle_x,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, @angle_x,@angle_y,0, 0,1,0) # gluLookAt(@camera.x,@camera.y,@camera.z, @horizontal_angle,@vertical_angle,0, 0,1,0)
color = [@c1, @c2, @c3] # @skydome.draw(0,0,0, 0.005, false) if @draw_skydome
@skydome.draw(0,0,0, 1, false) if @draw_skydome @cube.draw(0,1,0)
# @scene.draw(0,0,0, 1) # @model.draw(1, 0, 0)
@model.draw(1, 0, 0) # @tree.draw(5, 0, 0)
@tree.draw(5, 0, 0) # @tree.draw(5, 0, 3)
@tree.draw(5, 0, 3) # @tree.draw(3, 0, 10)
@tree.draw(3, 0, 10)
# @mega_model.draw(0,0,0, 1) # @mega_model.draw(0,0,0, 1)
end end
@@ -99,62 +98,85 @@ class IMICFPS
OpenGL Version: #{glGetString(GL_VERSION)}~ OpenGL Version: #{glGetString(GL_VERSION)}~
OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~ OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~
~ ~
Angle Y: #{@angle_y.round(2)} Angle X: #{@angle_x.round(2)} ~ Vertical Angle: #{@vertical_angle.round(2)} Horizontal Angle: #{@horizontal_angle.round(2)} ~
X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~ X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~
Faces: #{@number_of_faces} ~ Faces: #{@number_of_faces} ~
Last Frame: #{Gosu.milliseconds-@last_frame_time}ms (#{Gosu.fps} fps)~ Last Frame: #{delta_time}ms (#{Gosu.fps} fps)~
~ ~
Draw Skydome: #{@draw_skydome}" Draw Skydome: #{@draw_skydome}~
Debug mode: <c=992200>#{$debug}</b>~"
@last_frame_time = Gosu.milliseconds @last_frame_time = Gosu.milliseconds
# $window.caption = "Gosu OBJ object - FPS:#{Gosu.fps}" # $window.caption = "Gosu OBJ object - FPS:#{Gosu.fps}"
@angle_x-=Float(@mouse.x-self.mouse_x)/@mouse_sesitivity puts "#{@mouse}"
@angle_y-=Float(@mouse.y-self.mouse_y)/@mouse_sesitivity @horizontal_angle-=Float(@mouse.x-self.mouse_x)/@mouse_sesitivity
@angle_x %= 360.0 @vertical_angle-=Float(@mouse.y-self.mouse_y)/@mouse_sesitivity
@angle_y = @angle_y.clamp(-90.0, 90.0) @horizontal_angle %= 360.0
self.mouse_x, self.mouse_y = Gosu.screen_width/2, Gosu.screen_height/2 @vertical_angle = @vertical_angle.clamp(-90.0, 90.0)
@light_postion = [@camera.x, @camera.y, @camera.z, 0] self.mouse_x, self.mouse_y = Gosu.screen_width/2.0, Gosu.screen_height/2.0
# @camera_light.position = @light_position # @mouse.x, @mouse.y = self.mouse_x, self.mouse_y
# @light_postion = [0.0, 10, 0, 0] puts "#{@mouse.x}-#{self.mouse_x}" if @mouse.x != self.mouse_x
puts "#{@mouse.y}-#{self.mouse_y}" if @mouse.y != self.mouse_y
relative_speed = @speed*(delta_time/60.0) relative_speed = @speed
if button_down?(Gosu::KbLeftControl)
relative_speed = (@speed*10.0)*(delta_time/60.0)
else
relative_speed = @speed*(delta_time/60.0)
end
if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW) if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW)
@camera.z+=Math.cos(@angle_x * Math::PI / 180)*relative_speed @camera.z+=Math.cos(@horizontal_angle * Math::PI / 180)*relative_speed
@camera.x-=Math.sin(@angle_x * Math::PI / 180)*relative_speed @camera.x-=Math.sin(@horizontal_angle * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS) if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS)
@camera.z-=Math.cos(@angle_x * Math::PI / 180)*relative_speed @camera.z-=Math.cos(@horizontal_angle * Math::PI / 180)*relative_speed
@camera.x+=Math.sin(@angle_x * Math::PI / 180)*relative_speed @camera.x+=Math.sin(@horizontal_angle * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbLeft) || button_down?(Gosu::KbA) if button_down?(Gosu::KbA)
@camera.z+=Math.sin(@angle_x * Math::PI / 180)*relative_speed @camera.z+=Math.sin(@horizontal_angle * Math::PI / 180)*relative_speed
@camera.x+=Math.cos(@angle_x * Math::PI / 180)*relative_speed @camera.x+=Math.cos(@horizontal_angle * Math::PI / 180)*relative_speed
end end
if button_down?(Gosu::KbRight) || button_down?(Gosu::KbD) if button_down?(Gosu::KbD)
@camera.z-=Math.sin(@angle_x * Math::PI / 180)*relative_speed @camera.z-=Math.sin(@horizontal_angle * Math::PI / 180)*relative_speed
@camera.x-=Math.cos(@angle_x * Math::PI / 180)*relative_speed @camera.x-=Math.cos(@horizontal_angle * Math::PI / 180)*relative_speed
end end
@camera.y+=relative_speed if $window.button_down?(Gosu::KbLeftShift) if button_down?(Gosu::KbLeft)
@horizontal_angle-=relative_speed*100
end
if button_down?(Gosu::KbRight)
@horizontal_angle+=relative_speed*100
end
@camera.y+=relative_speed if $window.button_down?(Gosu::KbC) || $window.button_down?(Gosu::KbLeftShift)
@camera.y-=relative_speed if $window.button_down?(Gosu::KbSpace) @camera.y-=relative_speed if $window.button_down?(Gosu::KbSpace)
$window.close if $window.button_down?(Gosu::KbEscape) $window.close if $window.button_down?(Gosu::KbEscape)
@number_of_faces = 0 @number_of_faces = 0
@delta_time = Gosu.milliseconds
end end
def button_up(id) def button_up(id)
case id case id
when Gosu::KbZ when Gosu::KbZ
@draw_skydome = !@draw_skydome @draw_skydome = !@draw_skydome
when Gosu::KbBacktick
$debug = !$debug
end end
end end
def needs_cursor?
true
end
def lose_focus
puts 'Bye'
end
def delta_time def delta_time
t = Gosu.milliseconds-@delta_time Gosu.milliseconds-@delta_time
@delta_time = Gosu.milliseconds
return t
end end
end end
end end

View File

@@ -1,42 +1,13 @@
# Blender MTL File: 'cube.blend' # Blender MTL File: 'None'
# Material Count: 4 # Material Count: 1
newmtl Material
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.000000 0.800000 0.000071
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
newmtl Material.001 newmtl Material.001
Ns 96.078431 Ns 94.117647
Ka 1.000000 1.000000 1.000000 Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.007390 0.000000 Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000 Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
newmtl Material.002
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.069712 0.069712 0.069712
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
newmtl Material.003
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.166102 0.640000 0.127010
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000 Ke 0.000000 0.000000 0.000000
Ni 1.000000 Ni 1.000000
d 1.000000 d 1.000000
illum 2 illum 2
map_Kd objects/skydome.png

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,13 @@
# Blender MTL File: 'skydome.blend' # Blender MTL File: 'None'
# Material Count: 1 # Material Count: 1
newmtl Material.001 newmtl Material.001
Ns 96.078431 Ns 94.117647
Ka 0.000000 0.000000 0.000000 Ka 0.000000 0.000000 0.000000
Kd 0.129836 0.406902 0.640000 Kd 0.129836 0.406902 0.640000
Ks 0.000000 0.000000 0.000000 Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000 Ke 0.000000 0.000000 0.000000
Ni 1.000000 Ni 1.000000
d 1.000000 d 1.000000
illum 1 illum 2
map_Kd objects/skydome.png map_Kd objects/skydome.png

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 392 KiB

After

Width:  |  Height:  |  Size: 379 KiB