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

This commit is contained in:
2020-03-24 10:32:32 -05:00
parent 578c00673d
commit d839811cfd
3 changed files with 20 additions and 12 deletions

View File

@@ -53,11 +53,9 @@ class IMICFPS
end end
end end
if Shader.available?("default")
allocate_gl_objects allocate_gl_objects
populate_vertex_buffer populate_vertex_buffer
configure_vao configure_vao
end
@objects.each {|o| @vertex_count+=o.vertices.size} @objects.each {|o| @vertex_count+=o.vertices.size}
@objects.each_with_index do |o, i| @objects.each_with_index do |o, i|
@@ -172,30 +170,33 @@ class IMICFPS
def configure_vao def configure_vao
glBindVertexArray(@vertex_array_id) glBindVertexArray(@vertex_array_id)
program = Shader.get("default").program
# index, size, type, normalized, stride, pointer # index, size, type, normalized, stride, pointer
# vertices (positions) # vertices (positions)
glBindBuffer(GL_ARRAY_BUFFER, @positions_buffer_id) 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? gl_error?
# colors # colors
glBindBuffer(GL_ARRAY_BUFFER, @colors_buffer_id) 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? gl_error?
# normals # normals
glBindBuffer(GL_ARRAY_BUFFER, @normals_buffer_id) 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? gl_error?
if has_texture? if has_texture?
# uvs # uvs
glBindBuffer(GL_ARRAY_BUFFER, @uvs_buffer_id) 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? gl_error?
# texture ids # texture ids
glBindBuffer(GL_ARRAY_BUFFER, @textures_buffer_id) 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? gl_error?
end end

View File

@@ -24,6 +24,8 @@ class IMICFPS
shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse) shader.uniform_vec3("lights[#{i}].diffuse", light.diffuse)
shader.uniform_vec3("lights[#{i}].specular", light.specular) shader.uniform_vec3("lights[#{i}].specular", light.specular)
end end
gl_error?
shader.uniform_integer("totalLights", lights.size) shader.uniform_integer("totalLights", lights.size)

View File

@@ -25,6 +25,8 @@ class IMICFPS
return return
when 1 when 1
name = arguments.first name = arguments.first
Shader.delete(name)
shader = Shader.new( shader = Shader.new(
name: name, name: name,
includes_dir: "shaders/include", includes_dir: "shaders/include",
@@ -34,6 +36,8 @@ class IMICFPS
when 2 when 2
vertex = arguments.first vertex = arguments.first
fragment = arguments.last fragment = arguments.last
Shader.remove(vertex)
shader = Shader.new( shader = Shader.new(
name: vertex, name: vertex,
includes_dir: "shaders/include", includes_dir: "shaders/include",
@@ -52,6 +56,7 @@ class IMICFPS
end end
ensure ensure
$stdout = stdout $stdout = stdout
puts string if string
end end
def usage def usage