mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 08:02:36 +00:00
Fixed modern opengl renderer not renderering model textures, framebuffer can be used without crashing, model uv coordinates are now in buffer, minor rendering optimization
This commit is contained in:
20
lib/model.rb
20
lib/model.rb
@@ -44,6 +44,15 @@ class IMICFPS
|
|||||||
|
|
||||||
puts "#{@file_path.split('/').last} took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds to parse" if window.config.get(:debug_options, :stats)
|
puts "#{@file_path.split('/').last} took #{((Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)-start_time)/1000.0).round(2)} seconds to parse" if window.config.get(:debug_options, :stats)
|
||||||
|
|
||||||
|
@has_texture = false
|
||||||
|
|
||||||
|
@materials.each do |key, material|
|
||||||
|
if material.texture_id
|
||||||
|
@has_texture = true
|
||||||
|
@textured_material = key
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if Shader.available?("default")
|
if Shader.available?("default")
|
||||||
allocate_gl_objects
|
allocate_gl_objects
|
||||||
populate_vertex_buffer
|
populate_vertex_buffer
|
||||||
@@ -55,13 +64,6 @@ class IMICFPS
|
|||||||
puts " Model::Object Name: #{o.name}, Vertices: #{o.vertices.size}" if window.config.get(:debug_options, :stats)
|
puts " Model::Object Name: #{o.name}, Vertices: #{o.vertices.size}" if window.config.get(:debug_options, :stats)
|
||||||
end
|
end
|
||||||
window.number_of_vertices+=@vertex_count
|
window.number_of_vertices+=@vertex_count
|
||||||
@has_texture = false
|
|
||||||
@materials.each do |key, material|
|
|
||||||
if material.texture_id
|
|
||||||
@has_texture = true
|
|
||||||
@textured_material = key
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
|
||||||
# build_collision_tree
|
# build_collision_tree
|
||||||
@@ -160,8 +162,8 @@ class IMICFPS
|
|||||||
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id)
|
||||||
glBufferData(GL_ARRAY_BUFFER, uvs.flatten.size * Fiddle::SIZEOF_FLOAT, uvs.flatten.pack("f*"), GL_STATIC_DRAW)
|
glBufferData(GL_ARRAY_BUFFER, uvs.flatten.size * Fiddle::SIZEOF_FLOAT, uvs.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, @textures_buffer_id)
|
# glBindBuffer(GL_ARRAY_BUFFER, @textures_buffer_id)
|
||||||
glBufferData(GL_ARRAY_BUFFER, tex_ids.flatten.size * Fiddle::SIZEOF_FLOAT, tex_ids.flatten.pack("f*"), GL_STATIC_DRAW)
|
# glBufferData(GL_ARRAY_BUFFER, tex_ids.flatten.size * Fiddle::SIZEOF_FLOAT, tex_ids.flatten.pack("f*"), GL_STATIC_DRAW)
|
||||||
end
|
end
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
glBindBuffer(GL_ARRAY_BUFFER, 0)
|
||||||
|
|||||||
@@ -72,6 +72,22 @@ class IMICFPS
|
|||||||
glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
|
glDrawBuffers(draw_buffers.size, draw_buffers.pack("I*"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def bind_for_writing
|
||||||
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, @framebuffer)
|
||||||
|
end
|
||||||
|
|
||||||
|
def bind_for_reading
|
||||||
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, @framebuffer)
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_read_buffer(buffer)
|
||||||
|
glReadBuffer(GL_COLOR_ATTACHMENT0 + @textures.keys.index(buffer))
|
||||||
|
end
|
||||||
|
|
||||||
|
def unbind_framebuffer
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0)
|
||||||
|
end
|
||||||
|
|
||||||
def clean_up
|
def clean_up
|
||||||
glDeleteFramebuffers(@framebuffer)
|
glDeleteFramebuffers(@framebuffer)
|
||||||
|
|
||||||
|
|||||||
@@ -8,31 +8,43 @@ class IMICFPS
|
|||||||
@g_buffer = GBuffer.new
|
@g_buffer = GBuffer.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw_object(camera, lights, object)
|
def render(camera, lights, entities)
|
||||||
if Shader.available?("default")
|
if Shader.available?("default")
|
||||||
|
# @g_buffer.bind_for_writing
|
||||||
|
gl_error?
|
||||||
|
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
||||||
|
|
||||||
Shader.use("default") do |shader|
|
Shader.use("default") do |shader|
|
||||||
shader.uniform_transform("projection", camera.projection_matrix)
|
entities.each do |entity|
|
||||||
shader.uniform_transform("view", camera.view_matrix)
|
next unless entity.visible && entity.renderable
|
||||||
shader.uniform_transform("model", object.model_matrix)
|
|
||||||
shader.uniform_boolean("hasTexture", object.model.has_texture?)
|
|
||||||
shader.uniform_vec3("cameraPosition", camera.position)
|
|
||||||
|
|
||||||
# TODO: Upload and use lights
|
shader.uniform_transform("projection", camera.projection_matrix)
|
||||||
lights.each_with_index do |light, i|
|
shader.uniform_transform("view", camera.view_matrix)
|
||||||
shader.uniform_float("lights[#{i}.end", -1.0);
|
shader.uniform_transform("model", entity.model_matrix)
|
||||||
shader.uniform_float("lights[#{i}.type", light.type);
|
shader.uniform_boolean("hasTexture", entity.model.has_texture?)
|
||||||
shader.uniform_vec3("lights[#{i}].position", light.position)
|
shader.uniform_vec3("cameraPosition", camera.position)
|
||||||
shader.uniform_vec3("lights[#{i}].ambient", light.ambient)
|
|
||||||
shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse)
|
# TODO: Upload and use lights
|
||||||
shader.uniform_vec3("lights[#{i}].specular", light.specular)
|
lights.each_with_index do |light, i|
|
||||||
|
shader.uniform_float("lights[#{i}.end", -1.0);
|
||||||
|
shader.uniform_float("lights[#{i}.type", light.type);
|
||||||
|
shader.uniform_vec3("lights[#{i}].position", light.position)
|
||||||
|
shader.uniform_vec3("lights[#{i}].ambient", light.ambient)
|
||||||
|
shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse)
|
||||||
|
shader.uniform_vec3("lights[#{i}].specular", light.specular)
|
||||||
|
end
|
||||||
|
|
||||||
|
shader.uniform_float("totalLights", lights.size)
|
||||||
|
|
||||||
|
gl_error?
|
||||||
|
draw_model(entity.model, shader)
|
||||||
|
entity.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
shader.uniform_float("totalLights", lights.size)
|
|
||||||
|
|
||||||
gl_error?
|
|
||||||
draw_model(object.model)
|
|
||||||
object.draw
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @g_buffer.unbind_framebuffer
|
||||||
|
gl_error?
|
||||||
else
|
else
|
||||||
puts "Shader 'default' failed to compile, using immediate mode for rendering..." unless @@immediate_mode_warning
|
puts "Shader 'default' failed to compile, using immediate mode for rendering..." unless @@immediate_mode_warning
|
||||||
@@immediate_mode_warning = true
|
@@immediate_mode_warning = true
|
||||||
@@ -42,29 +54,35 @@ class IMICFPS
|
|||||||
camera.draw
|
camera.draw
|
||||||
|
|
||||||
glEnable(GL_NORMALIZE)
|
glEnable(GL_NORMALIZE)
|
||||||
glPushMatrix
|
entities.each do |entity|
|
||||||
|
next unless entity.visible && entity.renderable
|
||||||
|
|
||||||
glTranslatef(object.position.x, object.position.y, object.position.z)
|
glPushMatrix
|
||||||
glScalef(object.scale.x, object.scale.y, object.scale.z)
|
|
||||||
glRotatef(object.orientation.x, 1.0, 0, 0)
|
|
||||||
glRotatef(object.orientation.y, 0, 1.0, 0)
|
|
||||||
glRotatef(object.orientation.z, 0, 0, 1.0)
|
|
||||||
|
|
||||||
gl_error?
|
glTranslatef(entity.position.x, entity.position.y, entity.position.z)
|
||||||
draw_mesh(object.model)
|
glScalef(entity.scale.x, entity.scale.y, entity.scale.z)
|
||||||
object.draw
|
glRotatef(entity.orientation.x, 1.0, 0, 0)
|
||||||
glPopMatrix
|
glRotatef(entity.orientation.y, 0, 1.0, 0)
|
||||||
|
glRotatef(entity.orientation.z, 0, 0, 1.0)
|
||||||
|
|
||||||
|
gl_error?
|
||||||
|
draw_mesh(entity.model)
|
||||||
|
entity.draw
|
||||||
|
glPopMatrix
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
gl_error?
|
gl_error?
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw_model(model)
|
def draw_model(model, shader)
|
||||||
glBindVertexArray(model.vertex_array_id)
|
glBindVertexArray(model.vertex_array_id)
|
||||||
glEnableVertexAttribArray(0)
|
glEnableVertexAttribArray(0)
|
||||||
glEnableVertexAttribArray(1)
|
glEnableVertexAttribArray(1)
|
||||||
glEnableVertexAttribArray(2)
|
glEnableVertexAttribArray(2)
|
||||||
if model.has_texture?
|
if model.has_texture?
|
||||||
|
glBindTexture(GL_TEXTURE_2D, model.materials[model.textured_material].texture_id)
|
||||||
|
|
||||||
glEnableVertexAttribArray(3)
|
glEnableVertexAttribArray(3)
|
||||||
glEnableVertexAttribArray(4)
|
glEnableVertexAttribArray(4)
|
||||||
end
|
end
|
||||||
@@ -86,6 +104,8 @@ class IMICFPS
|
|||||||
if model.has_texture?
|
if model.has_texture?
|
||||||
glDisableVertexAttribArray(4)
|
glDisableVertexAttribArray(4)
|
||||||
glDisableVertexAttribArray(3)
|
glDisableVertexAttribArray(3)
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0)
|
||||||
end
|
end
|
||||||
glDisableVertexAttribArray(2)
|
glDisableVertexAttribArray(2)
|
||||||
glDisableVertexAttribArray(1)
|
glDisableVertexAttribArray(1)
|
||||||
|
|||||||
@@ -13,18 +13,7 @@ class IMICFPS
|
|||||||
glViewport(0, 0, window.width, window.height)
|
glViewport(0, 0, window.width, window.height)
|
||||||
glEnable(GL_DEPTH_TEST)
|
glEnable(GL_DEPTH_TEST)
|
||||||
|
|
||||||
entities.each do |object|
|
@opengl_renderer.render(camera, lights, entities)
|
||||||
if object.visible && object.renderable
|
|
||||||
# Render bounding boxes before transformation is applied
|
|
||||||
# @bounding_box_renderer.create_bounding_box(object, object.model.bounding_box, object.debug_color, object.object_id) if window.config.get(:debug_options, :boundingboxes)
|
|
||||||
|
|
||||||
@opengl_renderer.draw_object(camera, lights, object)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# @bounding_box_renderer.draw_bounding_boxes if window.config.get(:debug_options, :boundingboxes)
|
|
||||||
# window.number_of_vertices+=@bounding_box_renderer.vertex_count if window.config.get(:debug_options, :boundingboxes)
|
|
||||||
# @bounding_box_renderer.bounding_boxes.clear
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def finalize # cleanup
|
def finalize # cleanup
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ in vec3 outColor;
|
|||||||
in vec4 outNormal;
|
in vec4 outNormal;
|
||||||
in vec3 outUV;
|
in vec3 outUV;
|
||||||
in float outTextureID;
|
in float outTextureID;
|
||||||
|
in float outHasTexture;
|
||||||
in Light outLights[MAX_LIGHTS];
|
in Light outLights[MAX_LIGHTS];
|
||||||
in float outTotalLights;
|
in float outTotalLights;
|
||||||
in vec3 outFragPos;
|
in vec3 outFragPos;
|
||||||
@@ -14,6 +15,8 @@ in vec3 outCameraPos;
|
|||||||
in vec3 outInverseNormal;
|
in vec3 outInverseNormal;
|
||||||
in float outDisableLighting;
|
in float outDisableLighting;
|
||||||
|
|
||||||
|
uniform sampler2D diffuse_texture;
|
||||||
|
|
||||||
// optimizing compilers are annoying at this stage of my understanding of GLSL
|
// optimizing compilers are annoying at this stage of my understanding of GLSL
|
||||||
vec4 lokiVar;
|
vec4 lokiVar;
|
||||||
|
|
||||||
@@ -78,10 +81,20 @@ void main() {
|
|||||||
lokiVar = normalize(lokiVar);
|
lokiVar = normalize(lokiVar);
|
||||||
|
|
||||||
vec3 result;
|
vec3 result;
|
||||||
if (outDisableLighting == 1.0) {
|
|
||||||
result = outColor + 0.25;
|
if (outHasTexture == 0) {
|
||||||
|
if (outDisableLighting == 1.0) {
|
||||||
|
result = outColor + 0.25;
|
||||||
|
} else {
|
||||||
|
result = calculateLighting() * outColor;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result = calculateLighting() * outColor;
|
if (outDisableLighting == 1.0) {
|
||||||
|
result = texture(diffuse_texture, outUV.xy).xyz + 0.25;
|
||||||
|
} else {
|
||||||
|
result = calculateLighting() * texture(diffuse_texture, outUV.xy).xyz;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gl_FragColor = vec4(result, 1.0);
|
gl_FragColor = vec4(result, 1.0);
|
||||||
|
|||||||
Reference in New Issue
Block a user