Moved game objects to their own folder

This commit is contained in:
2018-12-10 10:38:42 -06:00
parent c656686997
commit 7f8f534633
7 changed files with 8 additions and 7 deletions

View File

@@ -0,0 +1,134 @@
class IMICFPS
class Camera
include CommonMethods
include OpenGL
include GLU
attr_accessor :x,:y,:z, :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity
attr_reader :game_object
def initialize(x: 0, y: 0, z: 0, fov: 70.0, distance: 100.0)
@x,@y,@z = x,y,z
@render_pitch = 20.0
@pitch = 20.0
@yaw = 0.0
@roll = 0.0
@field_of_view = fov
@view_distance = distance
@game_object = nil
@distance = 5
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)
@mouse_sensitivity = 20.0
@mouse_captured = true
end
def attach_to(game_object)
raise "Not a game object!" unless game_object.is_a?(GameObject)
@game_object = game_object
end
def detach
@game_object = nil
end
def distance_from_object
@distance
end
def horizontal_distance_from_object
distance_from_object * Math.cos(@pitch)
end
def vertical_distance_from_object
distance_from_object * Math.sin(@pitch)
end
def position_camera
if defined?(@game_object.first_person_view)
if @game_object.first_person_view
@distance = 0
else
@distance = 5
end
end
x_offset = horizontal_distance_from_object * Math.sin(@game_object.y_rotation.degrees_to_radians)
z_offset = horizontal_distance_from_object * Math.cos(@game_object.y_rotation.degrees_to_radians)
# p @game_object.x, @game_object.z;exit
@x = @game_object.x - x_offset
@y = @game_object.y + 2
@z = @game_object.z - z_offset
@yaw = 180 - @game_object.y_rotation
end
def draw
#glMatrixMode(matrix) indicates that following [matrix] is going to get used
glMatrixMode(GL_PROJECTION) # The projection matrix is responsible for adding perspective to our scene.
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
gluPerspective(@field_of_view, $window.width / $window.height, 0.1, @view_distance)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glRotatef(@render_pitch,1,0,0)
glRotatef(@yaw,0,1,0)
glTranslatef(-@x, -@y, -@z)
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
glLoadIdentity
if $debug && @game_object
glBegin(GL_LINES)
glColor3f(1,0,0)
glVertex3f(@x, @y, @z)
glVertex3f(@game_object.x, @game_object.y, @game_object.z)
glEnd
end
end
def update
if @mouse_captured
position_camera if @game_object
@yaw-=Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 unless @game_object
@game_object.y_rotation+=Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70 if @game_object
@render_pitch-=Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70 #unless @game_object
@yaw %= 360.0
@render_pitch = @render_pitch.clamp(-90.0, 90.0)
@pitch = @pitch.clamp(-90.0, 90.0)
self.mouse_x, self.mouse_y = Gosu.screen_width/2.0, Gosu.screen_height/2.0
end
end
def button_up(id)
case id
when Gosu::KbLeftAlt
@mouse_captured = false
$window.needs_cursor = true
when Gosu::MsLeft
@mouse_captured = true
$window.needs_cursor = false
when Gosu::KB_NUMPAD_PLUS
@mouse_sensitivity+=1
@mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0)
when Gosu::KbMinus, Gosu::KB_NUMPAD_MINUS
@mouse_sensitivity-=1
@mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0)
when Gosu::KB_NUMPAD_MULTIPLY
@mouse_sensitivity = 20.0
when Gosu::MsWheelUp
# @field_of_view += 1
# @field_of_view = @field_of_view.clamp(1, 100)
@view_distance += 1
@view_distance = @view_distance.clamp(1, 1000)
when Gosu::MsWheelDown
# @field_of_view -= 1
# @field_of_view = @field_of_view.clamp(1, 100)
@view_distance -= 1
@view_distance = @view_distance.clamp(1, 1000)
end
end
end
end

View File

@@ -0,0 +1,170 @@
require "etc"
class IMICFPS
class Player < GameObject
attr_accessor :speed
attr_reader :name, :bound_model, :first_person_view
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/biped.obj", game_object: self))
@speed = 2.5 # meter's per second
@running_speed = 6.8 # meter's per second
@old_speed = @speed
@mass = 72 # kg
@y_velocity = 0
@floor = 0
@first_person_view = true
@devisor = 500.0
@name_image = Gosu::Image.from_text("#{Etc.getlogin}", 100, font: "Consolas", align: :center)
# @name_image.save("temp.png")
@name_tex = @name_image.gl_tex_info
array_of_pixels = @name_image.to_blob
tex_names_buf = ' ' * 8
glGenTextures(1, tex_names_buf)
@name_texture_id = tex_names_buf.unpack('L2').first
glBindTexture(GL_TEXTURE_2D, @name_texture_id)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, @name_image.width, @name_image.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, array_of_pixels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
glGenerateMipmap(GL_TEXTURE_2D)
end
def draw_nameplate
_height = (@name_image.height/@devisor)
_width = (@name_image.width/@devisor)/2
_y = 2#normalize_bounding_box(model.bounding_box).max_y+0.05
glPushMatrix
glRotatef(180, 0, 1, 0)
glDisable(GL_LIGHTING)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_TEXTURE_2D)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND)
glBindTexture(GL_TEXTURE_2D, @name_texture_id)
glBegin(GL_TRIANGLES)
glColor3f(1.0,1.0,1.0)
# TOP LEFT
glTexCoord2f(0, 0)
glVertex3f(0-_width,_y+_height,0)
# TOP RIGHT
glTexCoord2f(1, 0)
glVertex3f(0+_width, _y+_height,0)
# BOTTOM LEFT
glTexCoord2f(0, 1)
glVertex3f(0-_width,_y,0)
# BOTTOM LEFT
glTexCoord2f(0, 1)
glVertex3f(0-_width,_y,0)
# BOTTOM RIGHT
glTexCoord2f(1, 1)
glVertex3f(0+_width, _y,0)
# TOP RIGHT
glTexCoord2f(1, 0)
glVertex3f(0+_width,_y+_height,0)
glEnd
# glDisable(GL_BLEND)
glDisable(GL_TEXTURE_2D)
glEnable(GL_LIGHTING)
glPopMatrix
end
def draw
if !@first_person_view
super
draw_nameplate
end
end
def update
@floor = @terrain.height_at(self, 4.5)
relative_speed = @speed
if button_down?(Gosu::KbLeftControl)
relative_speed = (@running_speed)*(delta_time)
else
relative_speed = @speed*(delta_time)
end
relative_y_rotation = @y_rotation*-1
if button_down?(Gosu::KbUp) || button_down?(Gosu::KbW)
@z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbDown) || button_down?(Gosu::KbS)
@z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
@x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbA)
@z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbD)
@z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
@x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
end
if button_down?(Gosu::KbLeft)
@y_rotation+=(relative_speed*1000)*delta_time
end
if button_down?(Gosu::KbRight)
@y_rotation-=(relative_speed*1000)*delta_time
end
if @_time_in_air
air_time = ((Gosu.milliseconds-@_time_in_air)/1000.0)
@y_velocity-=(IMICFPS::GRAVITY*air_time)*delta_time
end
if button_down?(Gosu::KbSpace) && !@jumping
@jumping = true
@_time_in_air = Gosu.milliseconds
elsif !@jumping && @y > @floor
@falling = true
@_time_in_air ||= Gosu.milliseconds # FIXME
else
if @jumping
if @y <= @floor
@falling = false; @jumping = false; @y_velocity = 0
end
end
end
if @jumping && !@falling
if button_down?(Gosu::KbSpace)
@y_velocity+=(2*15)*delta_time
@falling = true if @y_velocity >= 2
end
end
@y+=@y_velocity*delta_time
@y = @floor if @y < @floor
# distance = 2.0
# x_offset = distance * Math.cos(@bound_model.y_rotation)
# z_offset = distance * Math.sin(@bound_model.y_rotation)
super
end
def button_up(id)
case id
when Gosu::KbX
@y_rotation = @y_rotation+180
@y_rotation %= 360
when Gosu::KbF
@first_person_view = !@first_person_view
puts "First Person? #{@first_person_view}"
end
end
end
end

View File

@@ -0,0 +1,19 @@
class IMICFPS
class Skydome < GameObject
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/skydome.obj", game_object: self))
end
def draw
glDisable(GL_LIGHTING)
super
glEnable(GL_LIGHTING)
end
def update
@y_rotation+=0.01
@y_rotation%=360
super
end
end
end

View File

@@ -0,0 +1,174 @@
class IMICFPS
class Terrain < GameObject
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/randomish_terrain.obj", game_object: self))
# bind_model(ModelLoader.new(type: :obj, file_path: "/home/cyberarm/Documents/blends/untitled.obj", game_object: self))
self.scale = 1
@nearest_vertex_lookup = {}
generate_optimized_lists
end
def generate_optimized_lists
x_slot,y_slot = 0,0
model.vertices.each do |vert|
x_slot = vert.x.round
y_slot = vert.y.round
@nearest_vertex_lookup[x_slot] = {} unless @nearest_vertex_lookup[x_slot]
@nearest_vertex_lookup[x_slot][y_slot] = [] unless @nearest_vertex_lookup[x_slot][y_slot]
@nearest_vertex_lookup[x_slot][y_slot] << vert
end
p model.faces.first
end
def height_at(vertex, max_distance = Float::INFINITY)
if vert = find_nearest_vertex(vertex, max_distance)
return vert.y
else
-1
end
end
def find_nearest_vertex(vertex, max_distance)
nearest = nil
smaller_list = []
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round-1, vertex.y.round-1)
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round, vertex.y.round)
smaller_list << @nearest_vertex_lookup.dig(vertex.x.round+1, vertex.y.round+1)
smaller_list.flatten!
smaller_list.each do |vert|
next if vert.nil?
if nearest
if distance(vert, vertex) < distance(vert, nearest) && distance(vert, vertex) <= max_distance
nearest = vert
end
end
nearest = vert unless nearest && distance(vert, vertex) > max_distance
end
return nearest
end
def distance(vertex, other)
return Math.sqrt((vertex.x-other.x)**2 + (vertex.y-other.y)**2 + (vertex.z-other.z)**2)
end
end
end
# class IMICFPS
# class Terrain
# TILE_SIZE = 0.5
# include OpenGL
# def initialize(size:, height: nil, width: nil, length: nil, heightmap: nil)
# @size = size
# @heightmap = heightmap
# @map = []
# @height = height ? height : 1
# @width = width ? width : @size
# @length = length ? length : @size
# @vertices = []
# @normals = []
# @colors = []
# generate
# end
# def generate
# #@width.times do |x|
# # @length.times do |z|
# # # TRIANGLE STRIP (BROKEN)
# # @map << Vertex.new((x+1)-@width.to_f/2, 0, z-@legth.to_f/2)
# # @map << Vertex.new(x-@width.to_f/2, 0, (z+1)-@length.to_f/2)
# # end
# #end
# @width.times do |x|
# @length.times do |z|
# # WORKING TRIANGLES
# @map << Vertex.new(x-@width.to_f/2, @height, z-@length.to_f/2)
# @map << Vertex.new((x+1)-@width.to_f/2, @height, z-@length.to_f/2)
# @map << Vertex.new(x-@width.to_f/2, @height, (z+1)-@length.to_f/2)
# #
# @map << Vertex.new(x-@width.to_f/2, @height, (z+1)-@length.to_f/2)
# @map << Vertex.new((x+1)-@width.to_f/2, @height, z-@length.to_f/2)
# @map << Vertex.new((x+1)-@width.to_f/2, @height, (z+1)-@length.to_f/2)
# end
# end
# @map.size.times do |i|
# @vertices << @map[i].x
# @vertices << @map[i].y
# @vertices << @map[i].z
# normal = Vertex.new(0,1,0)
# @normals << normal.x
# @normals << normal.y
# @normals << normal.z
# color = Color.new(rand(0.10..0.30),0,0)
# @colors << color.red
# @colors << color.green
# @colors << color.blue
# end
# @vertices_packed = @vertices.pack("f*")
# @normals_packed = @normals.pack("f*")
# @colors_packed = @colors.pack("f*")
# end
# def draw
# new_draw
# # old_draw
# end
# def old_draw
# glEnable(GL_COLOR_MATERIAL)
# # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
# glPointSize(5)
# # glBegin(GL_LINES)
# # glBegin(GL_POINTS)
# glBegin(GL_TRIANGLES)
# @map.each_with_index do |vertex, index|
# glNormal3f(0,1,0)
# glColor3f(0.0, 0.5, 0) if index.even?
# glColor3f(0, 1.0, 0) if index.odd?
# glVertex3f(vertex.x, vertex.y, vertex.z)
# end
# glEnd
# glDisable(GL_COLOR_MATERIAL)
# glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
# end
# def new_draw
# glEnable(GL_NORMALIZE)
# glPushMatrix
# glEnable(GL_COLOR_MATERIAL)
# glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
# glShadeModel(GL_FLAT)
# glEnableClientState(GL_VERTEX_ARRAY)
# glEnableClientState(GL_NORMAL_ARRAY)
# glEnableClientState(GL_COLOR_ARRAY)
# glVertexPointer(3, GL_FLOAT, 0, @vertices_packed)
# glNormalPointer(GL_FLOAT, 0, @normals_packed)
# glColorPointer(3, GL_FLOAT, 0, @colors_packed)
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
# glDrawArrays(GL_TRIANGLES, 0, @vertices.size/3)
# glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
# # glDrawArrays(GL_TRIANGLE_STRIP, 0, @vertices.size/3)
# $window.number_of_faces+=@vertices.size/3
# glDisableClientState(GL_VERTEX_ARRAY)
# glDisableClientState(GL_NORMAL_ARRAY)
# glDisableClientState(GL_COLOR_ARRAY)
# glPopMatrix
# end
# end
# end

View File

@@ -0,0 +1,7 @@
class IMICFPS
class TestObject < GameObject
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/sponza.obj", game_object: self))
end
end
end

View File

@@ -0,0 +1,18 @@
class IMICFPS
class Tree < GameObject
def setup
bind_model(ModelLoader.new(type: :obj, file_path: "objects/tree.obj", game_object: self))
vert = @terrain.find_nearest_vertex(self, 4.5)
if vert
self.x = vert.x
self.y = vert.y
self.z = vert.z
end
end
# def update
# super
# @y_rotation+=0.005
# end
end
end