From d839811cfde31cc89733601faa916a5426aa41d3 Mon Sep 17 00:00:00 2001 From: Cyberarm Date: Tue, 24 Mar 2020 10:32:32 -0500 Subject: [PATCH] Fixed reloading shader which failed to compile at start up but succeeded at runtime caused crash due to Model expecting access to attribute location of shader inputs --- lib/model.rb | 25 ++++++++++++------------ lib/renderer/opengl_renderer.rb | 2 ++ lib/ui/commands/reload_shader_command.rb | 5 +++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/model.rb b/lib/model.rb index e4d22bf..2c90255 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -53,11 +53,9 @@ class IMICFPS end end - if Shader.available?("default") - allocate_gl_objects - populate_vertex_buffer - configure_vao - end + allocate_gl_objects + populate_vertex_buffer + configure_vao @objects.each {|o| @vertex_count+=o.vertices.size} @objects.each_with_index do |o, i| @@ -172,30 +170,33 @@ class IMICFPS def configure_vao glBindVertexArray(@vertex_array_id) - program = Shader.get("default").program - # index, size, type, normalized, stride, pointer # vertices (positions) glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id) - glVertexAttribPointer(glGetAttribLocation(program, "inPosition"), 3, GL_FLOAT, GL_FALSE, 0, nil) + # inPosition + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nil) gl_error? # colors glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id) - glVertexAttribPointer(glGetAttribLocation(program, "inColor"), 3, GL_FLOAT, GL_FALSE, 0, nil) + # inColor + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nil) gl_error? # normals glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id) - glVertexAttribPointer(glGetAttribLocation(program, "inNormal"), 4, GL_FLOAT, GL_FALSE, 0, nil) + # inNormal + glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, nil) gl_error? if has_texture? # uvs glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id) - glVertexAttribPointer(glGetAttribLocation(program, "inUV"), 3, GL_FLOAT, GL_FALSE, 0, nil) + # inUV + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, nil) gl_error? # texture ids glBindBuffer(GL_ARRAY_BUFFER, @textures_buffer_id) - glVertexAttribPointer(glGetAttribLocation(program, "inTextureID"), 1, GL_FLOAT, GL_FALSE, 0, nil) + # inTextureID + glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, 0, nil) gl_error? end diff --git a/lib/renderer/opengl_renderer.rb b/lib/renderer/opengl_renderer.rb index 3e9f613..7d56fed 100644 --- a/lib/renderer/opengl_renderer.rb +++ b/lib/renderer/opengl_renderer.rb @@ -24,6 +24,8 @@ class IMICFPS shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse) shader.uniform_vec3("lights[#{i}].specular", light.specular) end + gl_error? + shader.uniform_integer("totalLights", lights.size) diff --git a/lib/ui/commands/reload_shader_command.rb b/lib/ui/commands/reload_shader_command.rb index cf47f45..73af304 100644 --- a/lib/ui/commands/reload_shader_command.rb +++ b/lib/ui/commands/reload_shader_command.rb @@ -25,6 +25,8 @@ class IMICFPS return when 1 name = arguments.first + Shader.delete(name) + shader = Shader.new( name: name, includes_dir: "shaders/include", @@ -34,6 +36,8 @@ class IMICFPS when 2 vertex = arguments.first fragment = arguments.last + Shader.remove(vertex) + shader = Shader.new( name: vertex, includes_dir: "shaders/include", @@ -52,6 +56,7 @@ class IMICFPS end ensure $stdout = stdout + puts string if string end def usage