Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Spooner/ashton
Browse files Browse the repository at this point in the history
Updated to new Opengl version.
  • Loading branch information
bil-bas committed Aug 19, 2014
2 parents c0096e9 + 8b02750 commit ab5691c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 72 deletions.
8 changes: 4 additions & 4 deletions ashton.gemspec
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |s|

s.email = %w<bil.bagpuss@gmail.com>
s.files = Dir.glob %w<CHANGELOG LICENSE Rakefile README.md lib/**/*.* lib examples/**/*.* spec/**/*.* ext/**/*.{c,h,rb}>

s.extensions << 'ext/ashton/extconf.rb'
s.homepage = 'https://github.com/spooner/ashton'
s.licenses = %w<MIT>
Expand All @@ -22,8 +22,8 @@ Gem::Specification.new do |s|
s.has_rdoc = 'yard'
s.required_ruby_version = '>= 1.9.2'

s.add_dependency 'opengl', '~> 0.8.0.pre1'
s.add_dependency 'gosu', '~> 0.7.45'
s.add_dependency 'opengl', '~> 0.9.0'
s.add_dependency 'gosu', '~> 0.7'

s.add_development_dependency 'rake-compiler', '~> 0.8.1'
s.add_development_dependency 'rspec', '~> 2.10.0'
Expand All @@ -33,4 +33,4 @@ Gem::Specification.new do |s|
s.add_development_dependency 'redcarpet', '~> 2.1.1'
s.add_development_dependency 'yard', '~> 0.8.2.1'
s.add_development_dependency 'texplay', '~> 0.4.3'
end
end
4 changes: 2 additions & 2 deletions lib/ashton/gosu_ext/font.rb
Expand Up @@ -16,7 +16,7 @@ def draw(*args)
if shader
shader.enable z
$window.gl z do
glActiveTexture GL_TEXTURE0 # Let's make an assumption :)
Gl.glActiveTexture Gl::GL_TEXTURE0 # Let's make an assumption :)
shader.color = args[6].is_a?(Color) ? args[6] : DEFAULT_DRAW_COLOR
end
end
Expand All @@ -42,7 +42,7 @@ def draw_rel(*args)
if shader
shader.enable z
$window.gl z do
glActiveTexture GL_TEXTURE0 # Let's make an assumption :)
Gl.glActiveTexture GL::GL_TEXTURE0 # Let's make an assumption :)
shader.color = args[8].is_a?(Color) ? args[8] : DEFAULT_DRAW_COLOR
end
end
Expand Down
75 changes: 38 additions & 37 deletions lib/ashton/shader.rb
Expand Up @@ -22,7 +22,7 @@ class Shader
def enabled?; !!@previous_program end

# Is this the currently activated shader program?
def current?; glGetIntegerv(GL_CURRENT_PROGRAM) == @program end
def current?; Gl.glGetIntegerv(Gl::GL_CURRENT_PROGRAM) == @program end

# Instead of passing in source code, a file-name will be loaded or use a symbol to choose a built-in shader.
#
Expand Down Expand Up @@ -53,13 +53,14 @@ def initialize(options = {})
@color = [1, 1, 1, 1]

# Actually compile and link.
@vertex = compile GL_VERTEX_SHADER, @vertex_source
@fragment = compile GL_FRAGMENT_SHADER, @fragment_source

@vertex = compile Gl::GL_VERTEX_SHADER, @vertex_source
@fragment = compile Gl::GL_FRAGMENT_SHADER, @fragment_source
link

# In case we are using '#version 130' or higher, set out own color output.
begin
glBindFragDataLocationEXT @program, 0, "out_FragColor"
Gl.glBindFragDataLocationEXT @program, 0, "out_FragColor"
rescue NotImplementedError
# Might fail on an old system, but they will be fine just running GLSL 1.10 or 1.20
end
Expand Down Expand Up @@ -103,11 +104,11 @@ def dup
def enable(z = nil)
$window.gl z do
raise ShaderError, "This shader already enabled." if enabled?
current_shader = glGetIntegerv GL_CURRENT_PROGRAM
current_shader = Gl.glGetIntegerv GL::GL_CURRENT_PROGRAM
raise ShaderError, "Another shader already enabled." if current_shader > 0

@previous_program = current_shader
glUseProgram @program
Gl.glUseProgram @program
end

result = nil
Expand All @@ -127,7 +128,7 @@ def enable(z = nil)
def disable(z = nil)
$window.gl z do
raise ShaderError, "Shader not enabled." unless enabled?
glUseProgram @previous_program # Disable the shader!
Gl.glUseProgram @previous_program # Disable the shader!
@previous_program = nil
end

Expand Down Expand Up @@ -175,28 +176,28 @@ def set_uniform(location, value)
return if location == INVALID_LOCATION # Not for end-users :)

case value
when true, GL_TRUE
glUniform1i location, 1
when true, Gl::GL_TRUE
Gl.glUniform1i location, 1

when false, GL_FALSE
glUniform1i location, 0
when false, Gl::GL_FALSE
Gl.glUniform1i location, 0

when Float
begin
glUniform1f location, value
Gl.glUniform1f location, value
rescue
glUniform1i location, value.to_i
Gl.glUniform1i location, value.to_i
end

when Integer
begin
glUniform1i location, value
Gl.glUniform1i location, value
rescue
glUniform1f location, value.to_f
Gl.glUniform1f location, value.to_f
end

when Gosu::Color
glUniform4f location, *value.to_opengl
Gl.glUniform4f location, *value.to_opengl

when Array
size = value.size
Expand All @@ -207,16 +208,16 @@ def set_uniform(location, value)
case value[0]
when Float
begin
GL.send "glUniform#{size}f", location, *value.map(&:to_f)
Gl.send "glUniform#{size}f", location, *value.map(&:to_f)
rescue
GL.send "glUniform#{size}i", location, *value.map(&:to_i)
Gl.send "glUniform#{size}i", location, *value.map(&:to_i)
end

when Integer
begin
GL.send "glUniform#{size}i", location, *value.map(&:to_i)
Gl.send "glUniform#{size}i", location, *value.map(&:to_i)
rescue
GL.send "glUniform#{size}f", location, *value.map(&:to_f)
Gl.send "glUniform#{size}f", location, *value.map(&:to_f)
end

else
Expand All @@ -240,7 +241,7 @@ def uniform_location(name, options = {})
if location
location
else
location = glGetUniformLocation @program, name.to_s
location = Gl.glGetUniformLocation @program, name.to_s
if options[:required] && location == INVALID_LOCATION
raise ShaderUniformError, "No #{name.inspect} uniform specified in program"
end
Expand All @@ -255,8 +256,8 @@ def image=(image)
if image
info = image.gl_tex_info

glActiveTexture GL_TEXTURE0
glBindTexture GL_TEXTURE_2D, info.tex_name
Gl.glActiveTexture Gl::GL_TEXTURE0
Gl.glBindTexture Gl::GL_TEXTURE_2D, info.tex_name
end

set_uniform uniform_location("in_TextureEnabled", required: false), !!image
Expand All @@ -279,8 +280,8 @@ def color=(color)

needs_use = !current?
enable if needs_use
location = glGetAttribLocation @program, "in_Color"
glVertexAttrib4f location, *opengl_color unless location == INVALID_LOCATION
location = Gl.glGetAttribLocation @program, "in_Color"
Gl.glVertexAttrib4f location, *opengl_color unless location == INVALID_LOCATION
disable if needs_use

@color = opengl_color
Expand All @@ -292,23 +293,23 @@ def attribute(name)
if location
location
else
location = glGetAttribLocation @program, name.to_s
location = Gl.glGetAttribLocation @program, name.to_s
raise ShaderAttributeError, "No #{name} attribute specified in program" if location == INVALID_LOCATION
@attribute_locations[name] = location
end
end

protected
def compile(type, source)
shader = glCreateShader type
glShaderSource shader, source
glCompileShader shader
shader = Gl.glCreateShader type
Gl.glShaderSource shader, source
Gl.glCompileShader shader

unless glGetShaderiv shader, GL_COMPILE_STATUS
error = glGetShaderInfoLog shader
unless Gl.glGetShaderiv shader, Gl::GL_COMPILE_STATUS
error = Gl.glGetShaderInfoLog shader
error_lines = error.scan(/0\((\d+)\)+/m).map {|num| num.first.to_i }.uniq

if type == GL_VERTEX_SHADER
if type == Gl::GL_VERTEX_SHADER
type_name = "Vertex"
source = @vertex_source
else
Expand All @@ -326,12 +327,12 @@ def compile(type, source)

protected
def link
@program = glCreateProgram
glAttachShader @program, @vertex
glAttachShader @program, @fragment
glLinkProgram @program
@program = Gl.glCreateProgram
Gl.glAttachShader @program, @vertex
Gl.glAttachShader @program, @fragment
Gl.glLinkProgram @program

unless glGetProgramiv @program, GL_LINK_STATUS
unless Gl.glGetProgramiv @program, Gl::GL_LINK_STATUS
raise ShaderLinkError, "Shader link error: #{glGetProgramInfoLog(@program)}"
end

Expand Down
52 changes: 26 additions & 26 deletions lib/ashton/texture.rb
Expand Up @@ -46,23 +46,23 @@ def initialize(*args)
# TODO: Ideally we'd draw the image in replacement mode, but Gosu doesn't support that.
$window.gl do
info = image.gl_tex_info
glEnable GL_TEXTURE_2D
glBindTexture GL_TEXTURE_2D, info.tex_name
glEnable GL_BLEND
glBlendFunc GL_ONE, GL_ZERO
Gl.glEnable Gl::GL_TEXTURE_2D
Gl.glBindTexture Gl::GL_TEXTURE_2D, info.tex_name
Gl.glEnable Gl::GL_BLEND
Gl.glBlendFunc Gl::GL_ONE, Gl::GL_ZERO

glBegin GL_QUADS do
glTexCoord2d info.left, info.top
glVertex2d 0, height # BL
Gl.glBegin Gl::GL_QUADS do
Gl.glTexCoord2d info.left, info.top
Gl.glVertex2d 0, height # BL

glTexCoord2d info.left, info.bottom
glVertex2d 0, 0 # TL
Gl.glTexCoord2d info.left, info.bottom
Gl.glVertex2d 0, 0 # TL

glTexCoord2d info.right, info.bottom
glVertex2d width, 0 # TR
Gl.glTexCoord2d info.right, info.bottom
Gl.glVertex2d width, 0 # TR

glTexCoord2d info.right, info.top
glVertex2d width, height # BR
Gl.glTexCoord2d info.right, info.top
Gl.glVertex2d width, height # BR
end
end
end
Expand Down Expand Up @@ -98,14 +98,14 @@ def clear(options = {})
color = options[:color]
color = color.to_opengl if color.is_a? Gosu::Color

glBindFramebufferEXT GL_FRAMEBUFFER_EXT, fbo_id unless rendering?
Gl.glBindFramebufferEXT Gl::GL_FRAMEBUFFER_EXT, fbo_id unless rendering?

glDisable GL_BLEND # Need to replace the alpha too.
glClearColor(*color)
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
glEnable GL_BLEND
Gl.glDisable Gl::GL_BLEND # Need to replace the alpha too.
Gl.glClearColor(*color)
Gl.glClear Gl::GL_COLOR_BUFFER_BIT | Gl::GL_DEPTH_BUFFER_BIT
Gl.glEnable Gl::GL_BLEND

glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0 unless rendering?
Gl.glBindFramebufferEXT Gl::GL_FRAMEBUFFER_EXT, 0 unless rendering?

nil
end
Expand All @@ -123,21 +123,21 @@ def render
@rendering = true

# Project onto the texture itself, using Gosu (inverted) coordinates.
glPushMatrix
glMatrixMode GL_PROJECTION
glLoadIdentity
glViewport 0, 0, width, height
glOrtho 0, width, height, 0, -1, 1
Gl.glPushMatrix
Gl.glMatrixMode Gl::GL_PROJECTION
Gl.glLoadIdentity
Gl.glViewport 0, 0, width, height
Gl.glOrtho 0, width, height, 0, -1, 1

begin
yield self
ensure
$window.flush # Force all the drawing to draw now!
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0
Gl.glBindFramebufferEXT Gl::GL_FRAMEBUFFER_EXT, 0

@rendering = false

glPopMatrix
Gl.glPopMatrix

cache.refresh # Force lazy reloading of the cache.
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ashton/version.rb
@@ -1,3 +1,3 @@
module Ashton
VERSION = "0.1.3"
VERSION = "0.1.4"
end
4 changes: 2 additions & 2 deletions lib/ashton/window_buffer.rb
Expand Up @@ -8,8 +8,8 @@ def initialize
public
# Copy the window contents into the buffer.
def capture
glBindTexture GL_TEXTURE_2D, id
glCopyTexImage2D GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, width, height, 0
Gl.glBindTexture Gl::GL_TEXTURE_2D, id
Gl.glCopyTexImage2D Gl::GL_TEXTURE_2D, 0, Gl::GL_RGBA8, 0, 0, width, height, 0
self
end
end
Expand Down

0 comments on commit ab5691c

Please sign in to comment.