Objectified lights, cleanup and messification.

This commit is contained in:
2018-03-19 08:44:07 -05:00
parent 62364b2145
commit 4818d5a67e
4 changed files with 52 additions and 40 deletions

View File

@@ -16,6 +16,7 @@ else
raise RuntimeError, "Unsupported platform."
end
require_relative "lib/objects/light"
require_relative "lib/wavefront/model"
require_relative "lib/wavefront/object"
require_relative "lib/wavefront/material"

View File

@@ -44,7 +44,7 @@ class IMICFPS
def draw(x, y, z, scale = MODEL_METER_SCALE, back_face_culling = true)
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in \"#{desc}\": #{gluErrorString(e)} (#{e})\n"
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit
end
render(x,y,z, scale, back_face_culling)
@@ -64,16 +64,16 @@ class IMICFPS
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY)
if false#@model_has_texture
if @model_has_texture
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, @materials[@textured_material].texture_id)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_textures.pack("i*"))
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_textures)
end
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices.pack("i*"))
glColorPointer(3, GL_FLOAT, 0, o.flattened_materials.pack("i*"))
glNormalPointer(GL_FLOAT, 0, o.flattened_normals.pack("i*"))
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices.count/4)
glVertexPointer(4, GL_FLOAT, 0, o.flattened_vertices)
glColorPointer(3, GL_FLOAT, 0, o.flattened_materials)
glNormalPointer(GL_FLOAT, 0, o.flattened_normals)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices_size/4)
# glBegin(GL_TRIANGLES) # begin drawing model
# o.faces.each do |vert|
# vertex = vert[0]
@@ -93,9 +93,9 @@ class IMICFPS
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
if false#@model_has_texture
if @model_has_texture
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glBindTexture(GL_TEXTURE_2D, 0)
# glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
end
glDisable(GL_CULL_FACE) if back_face_culling

View File

@@ -31,12 +31,17 @@ class IMICFPS
end
end
@vertices_list = list
@vertices_list_size = list.size
@vertices_list = list.pack("i*")
end
return @vertices_list
end
def flattened_vertices_size
@vertices_list_size
end
def flattened_materials
unless @materials_list
list = []
@@ -53,7 +58,8 @@ class IMICFPS
end
end
@materials_list = list
@materials_list_size = list.size
@materials_list = list.pack("i*")
end
return @materials_list
@@ -68,11 +74,12 @@ class IMICFPS
list << v.x
list << v.y
list << v.z
# list << v.alpha
# list << v.weight
end
end
@normals_list = list
@normals_list_size = list.size
@normals_list = list.pack("i*")
end
return @normals_list
@@ -89,7 +96,8 @@ class IMICFPS
end
end
@textures_list = list
@textures_list_size = list.size
@textures_list = list.pack("i*")
end
return @textures_list

View File

@@ -38,14 +38,21 @@ class IMICFPS
@diffuse_light = [1, 0.5, 0, 1]
@specular_light = [0.2, 0.2, 0.2, 1]
@light_postion = [1, 1, 1, 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.specular = @specular_light
end
def draw
# begin
render
# rescue Gl::Error => e
# p e
# end
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in: #{gluErrorString(e)} (#{e})\n"
exit
end
render
end
def render
@@ -61,20 +68,13 @@ class IMICFPS
gluPerspective(90.0, width / height, 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
glLoadIdentity
# Think 3-d coordinate system (x,y,z). +- on each movies on that axis
glLightfv(GL_LIGHT0, GL_AMBIENT, @ambient_light.pack("f*"))
glLightfv(GL_LIGHT0, GL_DIFFUSE, @diffuse_light.pack("f*"))
glLightfv(GL_LIGHT0, GL_SPECULAR, @specular_light.pack("f*"))
glLightfv(GL_LIGHT0, GL_POSITION, @light_postion.pack("f*"))
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
@camera_light.draw
glEnable(GL_DEPTH_TEST)
glRotatef(@angle_y,1,0,0)
glRotatef(@angle_x,0,1,0)
glTranslatef(@camera.x, @camera.y, @camera.z)
# glPointSize(5.0)
# gluLookAt(@camera.x,@camera.y,@camera.z, @angle_x,@angle_y,0, 0,1,0)
color = [@c1, @c2, @c3]
@@ -98,8 +98,8 @@ class IMICFPS
OpenGL Version: #{glGetString(GL_VERSION)}~
OpenGL Shader Language Version: #{glGetString(GL_SHADING_LANGUAGE_VERSION)}~
~
Angle Y: #{@angle_y} Angle X: #{@angle_x} ~
X:#{@camera.x} Y:#{@camera.y} Z:#{@camera.z} ~
Angle Y: #{@angle_y.round(2)} Angle X: #{@angle_x.round(2)} ~
X:#{@camera.x.round(2)} Y:#{@camera.y.round(2)} Z:#{@camera.z.round(2)} ~
Faces: #{@number_of_faces} ~
Last Frame: #{Gosu.milliseconds-@last_frame_time}ms (#{Gosu.fps} fps)~
~
@@ -114,27 +114,30 @@ class IMICFPS
self.mouse_x, self.mouse_y = Gosu.screen_width/2, Gosu.screen_height/2
@light_postion = [@camera.x, @camera.y, @camera.z, 0]
@camera_light.postion = @light_postion
# @light_postion = [0.0, 10, 0, 0]
relative_speed = @speed#*delta_time
if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW)
@camera.z+=Math.cos(@angle_x * Math::PI / 180)*@speed
@camera.x-=Math.sin(@angle_x * Math::PI / 180)*@speed
@camera.z+=Math.cos(@angle_x * Math::PI / 180)*relative_speed
@camera.x-=Math.sin(@angle_x * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS)
@camera.z-=Math.cos(@angle_x * Math::PI / 180)*@speed
@camera.x+=Math.sin(@angle_x * Math::PI / 180)*@speed
@camera.z-=Math.cos(@angle_x * Math::PI / 180)*relative_speed
@camera.x+=Math.sin(@angle_x * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbLeft) || button_down?(Gosu::KbA)
@camera.z+=Math.sin(@angle_x * Math::PI / 180)*@speed
@camera.x+=Math.cos(@angle_x * Math::PI / 180)*@speed
@camera.z+=Math.sin(@angle_x * Math::PI / 180)*relative_speed
@camera.x+=Math.cos(@angle_x * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbRight) || button_down?(Gosu::KbD)
@camera.z-=Math.sin(@angle_x * Math::PI / 180)*@speed
@camera.x-=Math.cos(@angle_x * Math::PI / 180)*@speed
@camera.z-=Math.sin(@angle_x * Math::PI / 180)*relative_speed
@camera.x-=Math.cos(@angle_x * Math::PI / 180)*relative_speed
end
@camera.y+=@speed if $window.button_down?(Gosu::KbLeftShift)
@camera.y-=@speed if $window.button_down?(Gosu::KbSpace)
@camera.y+=relative_speed if $window.button_down?(Gosu::KbLeftShift)
@camera.y-=relative_speed if $window.button_down?(Gosu::KbSpace)
$window.close if $window.button_down?(Gosu::KbEscape)
@number_of_faces = 0