mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 23:52:35 +00:00
Added basic skydome, now using glDrawArrays :D
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
class IMICFPS
|
||||
class Wavefront
|
||||
class Model
|
||||
include GL
|
||||
include OpenGL
|
||||
# include GLU
|
||||
TextureCoordinate = Struct.new(:u, :v, :weight)
|
||||
Vertex = Struct.new(:x, :y, :z, :weight)
|
||||
@@ -31,46 +31,55 @@ class IMICFPS
|
||||
@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}"
|
||||
end
|
||||
$window.number_of_faces+=face_count
|
||||
end
|
||||
|
||||
def draw(x, y, z, scale = 1)
|
||||
begin
|
||||
render(x,y,z, scale)
|
||||
rescue Gl::Error => e
|
||||
p e
|
||||
end
|
||||
def draw(x, y, z, scale = MODEL_METER_SCALE, back_face_culling = true)
|
||||
# begin
|
||||
render(x,y,z, scale, back_face_culling)
|
||||
# rescue Gl::Error => e
|
||||
# p e
|
||||
# end
|
||||
end
|
||||
|
||||
def render(x,y,z, scale = 1)
|
||||
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|
|
||||
glEnable(GL_CULL_FACE)
|
||||
glEnable(GL_CULL_FACE) if back_face_culling
|
||||
glEnable(GL_COLOR_MATERIAL)
|
||||
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
|
||||
glShadeModel(GL_FLAT) unless o.faces.first[4]
|
||||
glShadeModel(GL_SMOOTH) if o.faces.first[4]
|
||||
glEnableClientState(GL_VERTEX_ARRAY)
|
||||
glEnableClientState(GL_COLOR_ARRAY)
|
||||
glEnableClientState(GL_NORMAL_ARRAY)
|
||||
glVertexPointer(3, 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.count/3)
|
||||
# glBegin(GL_TRIANGLES) # begin drawing model
|
||||
glBegin(GL_TRIANGLES) # begin drawing model
|
||||
o.faces.each do |vert|
|
||||
vertex = vert[0]
|
||||
uv = vert[1]
|
||||
normal = vert[2]
|
||||
material = vert[3]
|
||||
|
||||
glColor3f(material.diffuse.red, material.diffuse.green, material.diffuse.blue)
|
||||
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, [material.ambient.red, material.ambient.green, material.ambient.blue, 1.0])
|
||||
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, [material.diffuse.red, material.diffuse.green, material.diffuse.blue, 1.0])
|
||||
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [material.specular.red, material.specular.green, material.specular.blue, 1.0])
|
||||
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, [10.0])
|
||||
glNormal3f(normal.x, normal.y, normal.z) # Don't scale normals
|
||||
# glVertex3f(vertex.x*scale, vertex.y*scale, vertex.z*scale)
|
||||
glVertex3f(vertex.x, vertex.y, vertex.z)
|
||||
end
|
||||
glEnd
|
||||
glDisable(GL_CULL_FACE)
|
||||
# o.faces.each do |vert|
|
||||
# vertex = vert[0]
|
||||
# uv = vert[1]
|
||||
# normal = vert[2]
|
||||
# material = vert[3]
|
||||
#
|
||||
# glColor3f(material.diffuse.red, material.diffuse.green, material.diffuse.blue)
|
||||
# glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, [material.ambient.red, material.ambient.green, material.ambient.blue, 1.0].pack("f*"))
|
||||
# glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, [material.diffuse.red, material.diffuse.green, material.diffuse.blue, 1.0].pack("f*"))
|
||||
# glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [material.specular.red, material.specular.green, material.specular.blue, 1.0].pack("f*"))
|
||||
# glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, [10.0].pack("f*"))
|
||||
# glNormal3f(normal.x, normal.y, normal.z) # Don't scale normals
|
||||
# glVertex3f(vertex.x, vertex.y, vertex.z)
|
||||
# end
|
||||
# glEnd
|
||||
glDisableClientState(GL_VERTEX_ARRAY)
|
||||
glDisableClientState(GL_COLOR_ARRAY)
|
||||
glDisableClientState(GL_NORMAL_ARRAY)
|
||||
glDisable(GL_CULL_FACE) if back_face_culling
|
||||
glDisable(GL_COLOR_MATERIAL)
|
||||
end
|
||||
glPopMatrix
|
||||
@@ -188,6 +197,7 @@ class IMICFPS
|
||||
else
|
||||
raise
|
||||
end
|
||||
@current_object.vertices << vert
|
||||
@vertices << vert
|
||||
end
|
||||
|
||||
@@ -200,6 +210,7 @@ class IMICFPS
|
||||
else
|
||||
raise
|
||||
end
|
||||
@current_object.normals << vert
|
||||
@normals << vert
|
||||
end
|
||||
|
||||
@@ -212,6 +223,7 @@ class IMICFPS
|
||||
else
|
||||
raise
|
||||
end
|
||||
@current_object.textures << texture
|
||||
@uvs << texture
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,16 +1,76 @@
|
||||
class IMICFPS
|
||||
class Wavefront
|
||||
class Object
|
||||
attr_reader :name
|
||||
attr_reader :name, :vertices, :textures, :normals
|
||||
attr_accessor :faces
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@vertexes = []
|
||||
@vertices = []
|
||||
@textures = []
|
||||
@normals = []
|
||||
@faces = []
|
||||
end
|
||||
|
||||
def flattened_vertices
|
||||
unless @vertices_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
[face[0]].each do |v|
|
||||
next unless v
|
||||
list << v.x
|
||||
list << v.y
|
||||
list << v.z
|
||||
# list << v.weight
|
||||
end
|
||||
end
|
||||
|
||||
@vertices_list = list
|
||||
end
|
||||
|
||||
return @vertices_list
|
||||
end
|
||||
|
||||
def flattened_materials
|
||||
unless @materials_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
# p face
|
||||
[face[3]].each do |v|
|
||||
next unless v
|
||||
# p v
|
||||
# exit
|
||||
list << v.diffuse.red
|
||||
list << v.diffuse.green
|
||||
list << v.diffuse.blue
|
||||
# list << v.alpha
|
||||
end
|
||||
end
|
||||
|
||||
@materials_list = list
|
||||
end
|
||||
|
||||
return @materials_list
|
||||
end
|
||||
|
||||
def flattened_normals
|
||||
unless @normals_list
|
||||
list = []
|
||||
@faces.each do |face|
|
||||
[face[2]].each do |v|
|
||||
next unless v
|
||||
list << v.x
|
||||
list << v.y
|
||||
list << v.z
|
||||
# list << v.alpha
|
||||
end
|
||||
end
|
||||
|
||||
@normals_list = list
|
||||
end
|
||||
|
||||
return @normals_list
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
class IMICFPS
|
||||
class Window < Gosu::Window
|
||||
include GL
|
||||
include OpenGL
|
||||
include GLU
|
||||
# include GLUT
|
||||
Point = Struct.new(:x, :y)
|
||||
|
||||
attr_accessor :number_of_faces
|
||||
|
||||
def initialize(window_width = 1280, window_height = 800, fullscreen = false)
|
||||
super(window_width, window_height, fullscreen)
|
||||
# super(Gosu.screen_width, Gosu.screen_height, true)
|
||||
$window = self
|
||||
@number_of_faces = 0
|
||||
@draw_skydome = true
|
||||
@skydome = Wavefront::Model.new("objects/skydome.obj")
|
||||
@model = Wavefront::Model.new("objects/biped.obj")
|
||||
@scene = Wavefront::Model.new("objects/cube.obj")
|
||||
@tree = Wavefront::Model.new("objects/tree.obj")
|
||||
# @model = Wavefront::Model.new("objects/sponza.obj")
|
||||
@camera = Wavefront::Model::Vertex.new(0,-1,0)
|
||||
@@ -34,11 +40,11 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def draw
|
||||
begin
|
||||
# begin
|
||||
render
|
||||
rescue Gl::Error => e
|
||||
p e
|
||||
end
|
||||
# rescue => e
|
||||
# p e
|
||||
# end
|
||||
end
|
||||
|
||||
def render
|
||||
@@ -55,6 +61,10 @@ class IMICFPS
|
||||
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*"))
|
||||
glLightfv(GL_LIGHT0, GL_AMBIENT, @ambient_light)
|
||||
glLightfv(GL_LIGHT0, GL_DIFFUSE, @diffuse_light)
|
||||
glLightfv(GL_LIGHT0, GL_SPECULAR, @specular_light)
|
||||
@@ -66,15 +76,17 @@ class IMICFPS
|
||||
|
||||
glRotatef(@angle_y,1,0,0)
|
||||
glRotatef(@angle_x,0,1,0)
|
||||
glTranslate(@camera.x, @camera.y, @camera.z)
|
||||
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]
|
||||
@model.draw(1, 0, 0, 0.0009)
|
||||
@tree.draw(5, 0, 0, 0.0009)
|
||||
@tree.draw(5, 0, 1, 0.0009)
|
||||
@tree.draw(3, 0, 10, 0.0009)
|
||||
@skydome.draw(0,0,0, 0.4, false) if @draw_skydome
|
||||
@scene.draw(0,0,0, 1)
|
||||
@model.draw(1, 0, 0)
|
||||
@tree.draw(5, 0, 0)
|
||||
@tree.draw(5, 0, 3)
|
||||
@tree.draw(3, 0, 10)
|
||||
|
||||
end
|
||||
|
||||
@@ -91,8 +103,10 @@ class IMICFPS
|
||||
~
|
||||
Angle Y: #{@angle_y} Angle X: #{@angle_x} ~
|
||||
X:#{@camera.x} Y:#{@camera.y} Z:#{@camera.z} ~
|
||||
Model Faces: #{@model.faces_count} ~
|
||||
Last Frame: #{Gosu.milliseconds-@last_frame_time}ms (#{Gosu.fps} fps)"
|
||||
Faces: #{@number_of_faces} ~
|
||||
Last Frame: #{Gosu.milliseconds-@last_frame_time}ms (#{Gosu.fps} fps)~
|
||||
~
|
||||
Draw Skydome: #{@draw_skydome}"
|
||||
@last_frame_time = Gosu.milliseconds
|
||||
|
||||
# $window.caption = "Gosu OBJ object - FPS:#{Gosu.fps}"
|
||||
@@ -127,5 +141,12 @@ class IMICFPS
|
||||
|
||||
$window.close if $window.button_down?(Gosu::KbEscape)
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
case id
|
||||
when Gosu::KbZ
|
||||
@draw_skydome = !@draw_skydome
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user