Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created July 12, 2012 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bil-bas/3098868 to your computer and use it in GitHub Desktop.
Save bil-bas/3098868 to your computer and use it in GitHub Desktop.
Gosu Window#gl segfaults with shader when deferring drawing.
# Failing case of Window#gl used with shaders.
require "opengl" # Gem must be "opengl" gem, not "ruby-opengl" gem
require 'gosu'
def media_path(file); File.expand_path "media/#{file}", File.dirname(__FILE__) end
class TestWindow < Gosu::Window
def initialize
super 640, 480, false
@star = Gosu::Image.new(self, media_path("LargeStar.png"), true)
# Vertex shader
@vertex = glCreateShader GL_VERTEX_SHADER
glShaderSource @vertex, <<-END
#version 110
varying vec2 var_TexCoord;
void main()
{
gl_Position = ftransform();
var_TexCoord = gl_MultiTexCoord0.xy;
}
END
glCompileShader @vertex
#
@fragment = glCreateShader GL_FRAGMENT_SHADER
glShaderSource @fragment, <<-END
#version 110
uniform sampler2D in_Texture;
varying vec2 var_TexCoord;
void main()
{
gl_FragColor = texture2D(in_Texture, var_TexCoord);
gl_FragColor.g = 1.0;
}
END
glCompileShader @fragment
# Shader program
@program = glCreateProgram
glAttachShader @program, @vertex
glAttachShader @program, @fragment
glLinkProgram @program
end
def draw_with_shader(x)
glUseProgram @program
glActiveTexture GL_TEXTURE0
glBindTexture GL_TEXTURE_2D, @star.gl_tex_info.tex_name
@star.draw x, 0, 0
flush # Force drawing before we un-attach the shader.
glUseProgram 0
end
def draw
puts "Draw immediately, outside #gl (OK)"
draw_with_shader 0
gl do
puts "Draw immediately in #gl (OK)"
glEnable GL_BLEND
draw_with_shader 200
end
gl 5 do
puts "Draw deferred with #gl (SEGFAULT!!!)"
glEnable GL_BLEND
draw_with_shader 400
end
end
end
TestWindow.new.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment