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,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