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"
gem "opengl"
# gem "opengl-bindings"
# gem "opengl"
gem "opengl-bindings"
gem "glu"
# gem "glut"
gem "gosu"

View File

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

View File

@@ -2,19 +2,19 @@ require 'opengl'
require 'glu'
require "gosu"
# 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
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/object"

View File

@@ -1,12 +1,37 @@
class IMICFPS
class Wavefront
class Material
include OpenGL
attr_accessor :name, :ambient, :diffuse, :specular
attr_reader :texture
def initialize(name)
@name = name
@ambient = 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)
@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

View File

@@ -2,7 +2,7 @@ class IMICFPS
class Wavefront
class Model
include OpenGL
# include GLU
include GLU
TextureCoordinate = Struct.new(:u, :v, :weight)
Vertex = Struct.new(:x, :y, :z, :weight)
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}"
end
$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
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
e = glGetError()
if e != GL_NO_ERROR
$stderr.puts "OpenGL error in \"#{desc}\": #{gluErrorString(e)} (#{e})\n"
exit
end
render(x,y,z, scale, back_face_culling)
end
def render(x,y,z, scale, back_face_culling)
@@ -56,10 +64,16 @@ class IMICFPS
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)
if false#@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*"))
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
# o.faces.each do |vert|
# vertex = vert[0]
@@ -79,10 +93,16 @@ class IMICFPS
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_COLOR_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_COLOR_MATERIAL)
end
glPopMatrix
$window.number_of_faces+=self.faces.size
end
def parse
@@ -156,6 +176,8 @@ class IMICFPS
when 'Ni' # Unknown (Blender Specific?)
when 'd' # Dissolved (Transparency)
when 'illum' # Illumination model
when 'map_Kd'
# @materials[@current_material].set_texture(array[1])
end
end
end

View File

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

View File

@@ -43,7 +43,7 @@ class IMICFPS
def draw
# begin
render
# rescue => e
# rescue Gl::Error => e
# p e
# end
end
@@ -62,14 +62,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)
glLightfv(GL_LIGHT0, GL_POSITION, @light_postion)
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)
@@ -82,7 +78,7 @@ class IMICFPS
# gluLookAt(@camera.x,@camera.y,@camera.z, @angle_x,@angle_y,0, 0,1,0)
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)
@model.draw(1, 0, 0)
@tree.draw(5, 0, 0)
@@ -141,6 +137,7 @@ class IMICFPS
@camera.y-=@speed if $window.button_down?(Gosu::KbSpace)
$window.close if $window.button_down?(Gosu::KbEscape)
@number_of_faces = 0
end
def button_up(id)