Broken support for textures, trying to switch to opengl-bindings gem

This commit is contained in:
2018-03-18 22:38:16 -05:00
parent 3e9f1c47e7
commit 62364b2145
7 changed files with 105 additions and 39 deletions

View File

@@ -1,6 +1,6 @@
source "https://rubygems.org" source "https://rubygems.org"
gem "opengl" # gem "opengl"
# gem "opengl-bindings" gem "opengl-bindings"
gem "glu" gem "glu"
# gem "glut" # gem "glut"
gem "gosu" gem "gosu"

View File

@@ -5,8 +5,7 @@ GEM
glu (8.3.0-x86-mingw32) glu (8.3.0-x86-mingw32)
gosu (0.13.3) gosu (0.13.3)
gosu (0.13.3-x86-mingw32) gosu (0.13.3-x86-mingw32)
opengl (0.10.0) opengl-bindings (1.6.6)
opengl (0.10.0-x86-mingw32)
wavefront (0.1.2) wavefront (0.1.2)
PLATFORMS PLATFORMS
@@ -16,7 +15,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
glu glu
gosu gosu
opengl opengl-bindings
wavefront wavefront
BUNDLED WITH BUNDLED WITH

View File

@@ -2,19 +2,19 @@ require 'opengl'
require 'glu' require 'glu'
require "gosu" require "gosu"
# case OpenGL.get_platform case OpenGL.get_platform
# when :OPENGL_PLATFORM_WINDOWS when :OPENGL_PLATFORM_WINDOWS
# OpenGL.load_lib('opengl32.dll', 'C:/Windows/System32') OpenGL.load_lib('opengl32.dll', 'C:/Windows/System32')
# GLU.load_lib('GLU32.dll', 'C:/Windows/System32') GLU.load_lib('GLU32.dll', 'C:/Windows/System32')
# when :OPENGL_PLATFORM_MACOSX when :OPENGL_PLATFORM_MACOSX
# OpenGL.load_lib('libGL.dylib', '/System/Library/Frameworks/OpenGL.framework/Libraries') OpenGL.load_lib('libGL.dylib', '/System/Library/Frameworks/OpenGL.framework/Libraries')
# GLU.load_lib('libGLU.dylib', '/System/Library/Frameworks/OpenGL.framework/Libraries') GLU.load_lib('libGLU.dylib', '/System/Library/Frameworks/OpenGL.framework/Libraries')
# when :OPENGL_PLATFORM_LINUX when :OPENGL_PLATFORM_LINUX
# OpenGL.load_lib('libGL.so', '/usr/lib/x86_64-linux-gnu') OpenGL.load_lib('libGL.so', '/usr/lib/x86_64-linux-gnu')
# GLU.load_lib('libGLU.so', '/usr/lib/x86_64-linux-gnu') GLU.load_lib('libGLU.so', '/usr/lib/x86_64-linux-gnu')
# else else
# raise RuntimeError, "Unsupported platform." raise RuntimeError, "Unsupported platform."
# end end
require_relative "lib/wavefront/model" require_relative "lib/wavefront/model"
require_relative "lib/wavefront/object" require_relative "lib/wavefront/object"

View File

@@ -1,12 +1,37 @@
class IMICFPS class IMICFPS
class Wavefront class Wavefront
class Material class Material
include OpenGL
attr_accessor :name, :ambient, :diffuse, :specular attr_accessor :name, :ambient, :diffuse, :specular
attr_reader :texture
def initialize(name) def initialize(name)
@name = name @name = name
@ambient = Wavefront::Model::Color.new(1, 1, 1, 1) @ambient = Wavefront::Model::Color.new(1, 1, 1, 1)
@diffuse = Wavefront::Model::Color.new(1, 1, 1, 1) @diffuse = Wavefront::Model::Color.new(1, 1, 1, 1)
@specular= Wavefront::Model::Color.new(1, 1, 1, 1) @specular= Wavefront::Model::Color.new(1, 1, 1, 1)
@texture = nil
@texture_id = nil
end
def set_texture(texture_path)
puts "#{name} texture #{texture_path}"
@texture = Gosu::Image.new(texture_path)
array_of_pixels = @texture.to_blob
if @texture.gl_tex_info
@texture_id = @texture.gl_tex_info.tex_name
else
tex_names_buf = ' ' * 8
glGenTextures(1, tex_names_buf)
@texture_id = tex_names_buf.unpack('L2').first
end
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
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
end
def texture_id
@texture_id
end end
end end
end end

View File

@@ -2,7 +2,7 @@ class IMICFPS
class Wavefront class Wavefront
class Model class Model
include OpenGL 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)
Color = Struct.new(:red, :green, :blue, :alpha) Color = Struct.new(:red, :green, :blue, :alpha)
@@ -32,14 +32,22 @@ class IMICFPS
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 $window.number_of_faces+=face_count
@model_has_texture = false
@materials.each do |key, material|
if material.texture_id
@model_has_texture = true
@textured_material = key
end
end
end end
def draw(x, y, z, scale = MODEL_METER_SCALE, back_face_culling = true) def draw(x, y, z, scale = MODEL_METER_SCALE, back_face_culling = true)
# begin e = glGetError()
render(x,y,z, scale, back_face_culling) if e != GL_NO_ERROR
# rescue Gl::Error => e $stderr.puts "OpenGL error in \"#{desc}\": #{gluErrorString(e)} (#{e})\n"
# p e exit
# end end
render(x,y,z, scale, back_face_culling)
end end
def render(x,y,z, scale, back_face_culling) def render(x,y,z, scale, back_face_culling)
@@ -56,10 +64,16 @@ class IMICFPS
glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY) glEnableClientState(GL_COLOR_ARRAY)
glEnableClientState(GL_NORMAL_ARRAY) glEnableClientState(GL_NORMAL_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, o.flattened_vertices) if false#@model_has_texture
glColorPointer(3, GL_FLOAT, 0, o.flattened_materials) glEnable(GL_TEXTURE_2D)
glNormalPointer(GL_FLOAT, 0, o.flattened_normals) glBindTexture(GL_TEXTURE_2D, @materials[@textured_material].texture_id)
glDrawArrays(GL_TRIANGLES, 0, o.flattened_vertices.count/3) glEnableClientState(GL_TEXTURE_COORD_ARRAY)
glTexCoordPointer(3, GL_FLOAT, 0, o.flattened_textures.pack("i*"))
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)
# 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]
@@ -79,10 +93,16 @@ class IMICFPS
glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY) glDisableClientState(GL_NORMAL_ARRAY)
if false#@model_has_texture
glDisableClientState(GL_TEXTURE_COORD_ARRAY)
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
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)
end end
glPopMatrix glPopMatrix
$window.number_of_faces+=self.faces.size
end end
def parse def parse
@@ -156,6 +176,8 @@ class IMICFPS
when 'Ni' # Unknown (Blender Specific?) when 'Ni' # Unknown (Blender Specific?)
when 'd' # Dissolved (Transparency) when 'd' # Dissolved (Transparency)
when 'illum' # Illumination model when 'illum' # Illumination model
when 'map_Kd'
# @materials[@current_material].set_texture(array[1])
end end
end end
end end

View File

@@ -10,6 +10,12 @@ class IMICFPS
@textures = [] @textures = []
@normals = [] @normals = []
@faces = [] @faces = []
# Faces array packs everything:
# vertex = index[0]
# uv = index[1]
# normal = index[2]
# material = index[3]
end end
def flattened_vertices def flattened_vertices
@@ -21,7 +27,7 @@ class IMICFPS
list << v.x list << v.x
list << v.y list << v.y
list << v.z list << v.z
# list << v.weight list << v.weight
end end
end end
@@ -71,6 +77,23 @@ class IMICFPS
return @normals_list return @normals_list
end end
def flattened_textures
unless @textures_list
list = []
@faces.each do |face|
[face[1]].each do |v|
next unless v
list << v.x
list << v.y
end
end
@textures_list = list
end
return @textures_list
end
end end
end end
end end

View File

@@ -43,7 +43,7 @@ class IMICFPS
def draw def draw
# begin # begin
render render
# rescue => e # rescue Gl::Error => e
# p e # p e
# end # end
end end
@@ -62,14 +62,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_AMBIENT, @ambient_light.pack("f*"))
# glLightfv(GL_LIGHT0, GL_DIFFUSE, @diffuse_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_SPECULAR, @specular_light.pack("f*"))
# glLightfv(GL_LIGHT0, GL_POSITION, @light_postion.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)
glLightfv(GL_LIGHT0, GL_POSITION, @light_postion)
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1) glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1)
glEnable(GL_LIGHTING) glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0) glEnable(GL_LIGHT0)
@@ -82,7 +78,7 @@ class IMICFPS
# 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]
@skydome.draw(0,0,0, 0.4, false) if @draw_skydome @skydome.draw(0,0,0, 0.004, false) if @draw_skydome
@scene.draw(0,0,0, 1) @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)
@@ -141,6 +137,7 @@ class IMICFPS
@camera.y-=@speed if $window.button_down?(Gosu::KbSpace) @camera.y-=@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
end end
def button_up(id) def button_up(id)