Added basic skydome, now using glDrawArrays :D

This commit is contained in:
2018-03-18 20:31:20 -05:00
parent 47a27c15d3
commit c2aebbf0e8
8 changed files with 2613 additions and 51 deletions

View File

@@ -1,6 +1,7 @@
source "https://rubygems.org" source "https://rubygems.org"
gem "opengl" gem "opengl"
# gem "opengl-bindings"
gem "glu" gem "glu"
gem "glut" # gem "glut"
gem "gosu" gem "gosu"
gem "wavefront" # Gem for opening blender or blender exported object with texturing gem "wavefront" # Gem for opening blender or blender exported object with texturing

View File

@@ -3,10 +3,8 @@ GEM
specs: specs:
glu (8.3.0) glu (8.3.0)
glu (8.3.0-x86-mingw32) glu (8.3.0-x86-mingw32)
glut (8.3.0) gosu (0.13.3)
glut (8.3.0-x86-mingw32) gosu (0.13.3-x86-mingw32)
gosu (0.13.2)
gosu (0.13.2-x86-mingw32)
opengl (0.10.0) opengl (0.10.0)
opengl (0.10.0-x86-mingw32) opengl (0.10.0-x86-mingw32)
wavefront (0.1.2) wavefront (0.1.2)
@@ -17,7 +15,6 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
glu glu
glut
gosu gosu
opengl opengl
wavefront wavefront

View File

@@ -1,12 +1,26 @@
require "opengl" require 'opengl'
require "glu" require 'glu'
# require "glut"
require "gosu" require "gosu"
require "fiddle"
# case OpenGL.get_platform
# when :OPENGL_PLATFORM_WINDOWS
# OpenGL.load_lib('opengl32.dll', 'C:/Windows/System32')
# GLU.load_lib('GLU32.dll', 'C:/Windows/System32')
# when :OPENGL_PLATFORM_MACOSX
# OpenGL.load_lib('libGL.dylib', '/System/Library/Frameworks/OpenGL.framework/Libraries')
# GLU.load_lib('libGLU.dylib', '/System/Library/Frameworks/OpenGL.framework/Libraries')
# when :OPENGL_PLATFORM_LINUX
# OpenGL.load_lib('libGL.so', '/usr/lib/x86_64-linux-gnu')
# GLU.load_lib('libGLU.so', '/usr/lib/x86_64-linux-gnu')
# else
# raise RuntimeError, "Unsupported platform."
# end
require_relative "lib/wavefront/model" require_relative "lib/wavefront/model"
require_relative "lib/wavefront/object" 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
IMICFPS::Window.new.show IMICFPS::Window.new.show

View File

@@ -1,7 +1,7 @@
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Model class Model
include GL include OpenGL
# include GLU # include GLU
TextureCoordinate = Struct.new(:u, :v, :weight) TextureCoordinate = Struct.new(:u, :v, :weight)
Vertex = Struct.new(:x, :y, :z, :weight) Vertex = Struct.new(:x, :y, :z, :weight)
@@ -31,46 +31,55 @@ class IMICFPS
@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 "OBJECT FACES: Name: #{o.name} #{o.faces.size}, array size divided by 3: #{o.faces.size.to_f/3.0}"
end end
$window.number_of_faces+=face_count
end end
def draw(x, y, z, scale = 1) def draw(x, y, z, scale = MODEL_METER_SCALE, back_face_culling = true)
begin # begin
render(x,y,z, scale) render(x,y,z, scale, back_face_culling)
rescue Gl::Error => e # rescue Gl::Error => e
p e # p e
end # end
end end
def render(x,y,z, scale = 1) def render(x,y,z, scale, back_face_culling)
glEnable(GL_NORMALIZE) glEnable(GL_NORMALIZE)
glPushMatrix glPushMatrix
glTranslatef(x,y,z) glTranslatef(x,y,z)
glScalef(scale, scale, scale) glScalef(scale, scale, scale)
@objects.each_with_index do |o, i| @objects.each_with_index do |o, i|
glEnable(GL_CULL_FACE) glEnable(GL_CULL_FACE) if back_face_culling
glEnable(GL_COLOR_MATERIAL) glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glShadeModel(GL_FLAT) unless o.faces.first[4] glShadeModel(GL_FLAT) unless o.faces.first[4]
glShadeModel(GL_SMOOTH) if 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
glBegin(GL_TRIANGLES) # begin drawing model # o.faces.each do |vert|
o.faces.each do |vert| # vertex = vert[0]
vertex = vert[0] # uv = vert[1]
uv = vert[1] # normal = vert[2]
normal = vert[2] # material = vert[3]
material = vert[3] #
# glColor3f(material.diffuse.red, material.diffuse.green, material.diffuse.blue)
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_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].pack("f*"))
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].pack("f*"))
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].pack("f*"))
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, [10.0]) # glNormal3f(normal.x, normal.y, normal.z) # Don't scale normals
glNormal3f(normal.x, normal.y, normal.z) # Don't scale normals # glVertex3f(vertex.x, vertex.y, vertex.z)
# glVertex3f(vertex.x*scale, vertex.y*scale, vertex.z*scale) # end
glVertex3f(vertex.x, vertex.y, vertex.z) # glEnd
end glDisableClientState(GL_VERTEX_ARRAY)
glEnd glDisableClientState(GL_COLOR_ARRAY)
glDisable(GL_CULL_FACE) glDisableClientState(GL_NORMAL_ARRAY)
glDisable(GL_CULL_FACE) if back_face_culling
glDisable(GL_COLOR_MATERIAL) glDisable(GL_COLOR_MATERIAL)
end end
glPopMatrix glPopMatrix
@@ -188,6 +197,7 @@ class IMICFPS
else else
raise raise
end end
@current_object.vertices << vert
@vertices << vert @vertices << vert
end end
@@ -200,6 +210,7 @@ class IMICFPS
else else
raise raise
end end
@current_object.normals << vert
@normals << vert @normals << vert
end end
@@ -212,6 +223,7 @@ class IMICFPS
else else
raise raise
end end
@current_object.textures << texture
@uvs << texture @uvs << texture
end end
end end

View File

@@ -1,16 +1,76 @@
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Object class Object
attr_reader :name attr_reader :name, :vertices, :textures, :normals
attr_accessor :faces attr_accessor :faces
def initialize(name) def initialize(name)
@name = name @name = name
@vertexes = [] @vertices = []
@textures = [] @textures = []
@normals = [] @normals = []
@faces = [] @faces = []
end 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 end
end end

View File

@@ -1,15 +1,21 @@
class IMICFPS class IMICFPS
class Window < Gosu::Window class Window < Gosu::Window
include GL include OpenGL
include GLU include GLU
# include GLUT # include GLUT
Point = Struct.new(:x, :y) Point = Struct.new(:x, :y)
attr_accessor :number_of_faces
def initialize(window_width = 1280, window_height = 800, fullscreen = false) def initialize(window_width = 1280, window_height = 800, fullscreen = false)
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
@number_of_faces = 0
@draw_skydome = true
@skydome = Wavefront::Model.new("objects/skydome.obj")
@model = Wavefront::Model.new("objects/biped.obj") @model = Wavefront::Model.new("objects/biped.obj")
@scene = Wavefront::Model.new("objects/cube.obj")
@tree = Wavefront::Model.new("objects/tree.obj") @tree = Wavefront::Model.new("objects/tree.obj")
# @model = Wavefront::Model.new("objects/sponza.obj") # @model = Wavefront::Model.new("objects/sponza.obj")
@camera = Wavefront::Model::Vertex.new(0,-1,0) @camera = Wavefront::Model::Vertex.new(0,-1,0)
@@ -34,11 +40,11 @@ class IMICFPS
end end
def draw def draw
begin # begin
render render
rescue Gl::Error => e # rescue => e
p e # p e
end # end
end end
def render def render
@@ -55,6 +61,10 @@ 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
# Think 3-d coordinate system (x,y,z). +- on each movies on that axis # 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_AMBIENT, @ambient_light)
glLightfv(GL_LIGHT0, GL_DIFFUSE, @diffuse_light) glLightfv(GL_LIGHT0, GL_DIFFUSE, @diffuse_light)
glLightfv(GL_LIGHT0, GL_SPECULAR, @specular_light) glLightfv(GL_LIGHT0, GL_SPECULAR, @specular_light)
@@ -66,15 +76,17 @@ class IMICFPS
glRotatef(@angle_y,1,0,0) glRotatef(@angle_y,1,0,0)
glRotatef(@angle_x,0,1,0) glRotatef(@angle_x,0,1,0)
glTranslate(@camera.x, @camera.y, @camera.z) glTranslatef(@camera.x, @camera.y, @camera.z)
# glPointSize(5.0) # glPointSize(5.0)
# gluLookAt(@camera.x,@camera.y,@camera.z, @angle_x,@angle_y,0, 0,1,0) # gluLookAt(@camera.x,@camera.y,@camera.z, @angle_x,@angle_y,0, 0,1,0)
color = [@c1, @c2, @c3] color = [@c1, @c2, @c3]
@model.draw(1, 0, 0, 0.0009) @skydome.draw(0,0,0, 0.4, false) if @draw_skydome
@tree.draw(5, 0, 0, 0.0009) @scene.draw(0,0,0, 1)
@tree.draw(5, 0, 1, 0.0009) @model.draw(1, 0, 0)
@tree.draw(3, 0, 10, 0.0009) @tree.draw(5, 0, 0)
@tree.draw(5, 0, 3)
@tree.draw(3, 0, 10)
end end
@@ -91,8 +103,10 @@ class IMICFPS
~ ~
Angle Y: #{@angle_y} Angle X: #{@angle_x} ~ Angle Y: #{@angle_y} Angle X: #{@angle_x} ~
X:#{@camera.x} Y:#{@camera.y} Z:#{@camera.z} ~ X:#{@camera.x} Y:#{@camera.y} Z:#{@camera.z} ~
Model Faces: #{@model.faces_count} ~ Faces: #{@number_of_faces} ~
Last Frame: #{Gosu.milliseconds-@last_frame_time}ms (#{Gosu.fps} fps)" Last Frame: #{Gosu.milliseconds-@last_frame_time}ms (#{Gosu.fps} fps)~
~
Draw Skydome: #{@draw_skydome}"
@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}"
@@ -127,5 +141,12 @@ class IMICFPS
$window.close if $window.button_down?(Gosu::KbEscape) $window.close if $window.button_down?(Gosu::KbEscape)
end end
def button_up(id)
case id
when Gosu::KbZ
@draw_skydome = !@draw_skydome
end
end
end end
end end

13
objects/skydome.mtl Normal file
View File

@@ -0,0 +1,13 @@
# Blender MTL File: 'skydome.blend'
# Material Count: 1
newmtl Material.001
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.129836 0.406902 0.640000
Ks 0.000000 0.000000 0.000000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
map_Kd C:\Users\cyber\Documents\Blender\skydome\grid.png

2444
objects/skydome.obj Normal file

File diff suppressed because it is too large Load Diff