ashton 0.0.1alpha → 0.0.2alpha
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -21
- data/README.md +95 -68
- data/Rakefile +41 -23
- data/examples/bloom_example.rb +59 -0
- data/examples/lighting_example.rb +127 -0
- data/examples/media/SmallStar.png +0 -0
- data/examples/media/Starfighter.png +0 -0
- data/examples/media/simple.png +0 -0
- data/examples/noise_example.rb +94 -0
- data/examples/outline_example.rb +86 -0
- data/examples/particle_emitter_example.rb +114 -0
- data/examples/pixelate_example.rb +51 -49
- data/examples/pixelated_texture_example.rb +69 -0
- data/examples/radial_blur_example.rb +60 -62
- data/examples/shader_image_example.rb +74 -41
- data/examples/{shockwave2_example.rb → shockwave_example.rb} +74 -75
- data/examples/stencil_shader_example.rb +104 -0
- data/examples/{framebuffer_example.rb → texture_render_example.rb} +53 -49
- data/examples/{tv_screen_and_noise_example.rb → tv_screen_and_static_example.rb} +59 -59
- data/ext/ashton/GLee.c +18170 -0
- data/ext/ashton/GLee.h +17647 -0
- data/ext/ashton/ashton.c +42 -0
- data/ext/ashton/ashton.h +31 -0
- data/ext/ashton/color.c +45 -0
- data/ext/ashton/color.h +25 -0
- data/ext/ashton/common.h +41 -0
- data/ext/ashton/extconf.rb +42 -0
- data/ext/ashton/fast_math.c +30 -0
- data/ext/ashton/fast_math.h +30 -0
- data/ext/ashton/font.c +8 -0
- data/ext/ashton/font.h +16 -0
- data/ext/ashton/gosu.c +18 -0
- data/ext/ashton/gosu.h +19 -0
- data/ext/ashton/image.c +8 -0
- data/ext/ashton/image.h +16 -0
- data/ext/ashton/particle_emitter.c +788 -0
- data/ext/ashton/particle_emitter.h +171 -0
- data/ext/ashton/pixel_cache.c +237 -0
- data/ext/ashton/pixel_cache.h +58 -0
- data/ext/ashton/shader.c +9 -0
- data/ext/ashton/shader.h +16 -0
- data/ext/ashton/texture.c +442 -0
- data/ext/ashton/texture.h +63 -0
- data/ext/ashton/window.c +8 -0
- data/ext/ashton/window.h +16 -0
- data/lib/ashton.rb +38 -26
- data/lib/ashton/1.9/ashton.so +0 -0
- data/lib/ashton/gosu_ext/color.rb +24 -11
- data/lib/ashton/gosu_ext/font.rb +58 -0
- data/lib/ashton/gosu_ext/gosu_module.rb +16 -0
- data/lib/ashton/gosu_ext/image.rb +95 -31
- data/lib/ashton/gosu_ext/window.rb +78 -35
- data/lib/ashton/image_stub.rb +32 -36
- data/lib/ashton/lighting/light_source.rb +146 -0
- data/lib/ashton/lighting/manager.rb +98 -0
- data/lib/ashton/mixins/version_checking.rb +23 -0
- data/lib/ashton/particle_emitter.rb +87 -0
- data/lib/ashton/pixel_cache.rb +24 -0
- data/lib/ashton/shader.rb +353 -35
- data/lib/ashton/shaders/bloom.frag +41 -0
- data/lib/ashton/shaders/color_inversion.frag +11 -0
- data/lib/ashton/{post_process → shaders}/contrast.frag +16 -16
- data/lib/ashton/{shader → shaders}/default.frag +22 -19
- data/lib/ashton/{shader → shaders}/default.vert +13 -13
- data/lib/ashton/shaders/fade.frag +14 -0
- data/lib/ashton/shaders/grayscale.frag +15 -0
- data/lib/ashton/shaders/include/classicnoise2d.glsl +113 -0
- data/lib/ashton/shaders/include/classicnoise3d.glsl +177 -0
- data/lib/ashton/shaders/include/classicnoise4d.glsl +302 -0
- data/lib/ashton/{include/simplex.glsl → shaders/include/noise2d.glsl} +70 -63
- data/lib/ashton/shaders/include/noise3d.glsl +102 -0
- data/lib/ashton/shaders/include/noise4d.glsl +128 -0
- data/lib/ashton/shaders/include/rand.glsl +5 -0
- data/lib/ashton/shaders/lighting/distort.frag +57 -0
- data/lib/ashton/shaders/lighting/draw_shadows.frag +60 -0
- data/lib/ashton/shaders/lighting/shadow_blur.frag +60 -0
- data/lib/ashton/shaders/mezzotint.frag +22 -0
- data/lib/ashton/shaders/multitexture2.vert +19 -0
- data/lib/ashton/shaders/outline.frag +45 -0
- data/lib/ashton/{post_process → shaders}/pixelate.frag +48 -48
- data/lib/ashton/shaders/radial_blur.frag +63 -0
- data/lib/ashton/shaders/sepia.frag +26 -0
- data/lib/ashton/{post_process/shockwave2.frag → shaders/shockwave.frag} +38 -35
- data/lib/ashton/shaders/signed_distance_field.frag +80 -0
- data/lib/ashton/{post_process/noise.frag → shaders/static.frag} +25 -27
- data/lib/ashton/shaders/stencil.frag +27 -0
- data/lib/ashton/shaders/tv_screen.frag +23 -0
- data/lib/ashton/signed_distance_field.rb +151 -0
- data/lib/ashton/texture.rb +186 -0
- data/lib/ashton/version.rb +2 -2
- data/lib/ashton/window_buffer.rb +16 -0
- data/spec/ashton/ashton_spec.rb +22 -0
- data/spec/ashton/gosu_ext/color_spec.rb +34 -0
- data/spec/ashton/gosu_ext/font_spec.rb +57 -0
- data/spec/ashton/gosu_ext/gosu_spec.rb +11 -0
- data/spec/ashton/gosu_ext/image_spec.rb +66 -0
- data/spec/ashton/gosu_ext/window_spec.rb +71 -0
- data/spec/ashton/image_stub_spec.rb +46 -0
- data/spec/ashton/particle_emitter_spec.rb +123 -0
- data/spec/ashton/pixel_cache_spec.rb +153 -0
- data/spec/ashton/shader_spec.rb +152 -0
- data/spec/ashton/signed_distance_field_spec.rb +163 -0
- data/spec/ashton/texture_spec.rb +347 -0
- data/spec/helper.rb +12 -0
- metadata +159 -28
- data/examples/output/README.txt +0 -1
- data/lib/ashton/base_shader.rb +0 -172
- data/lib/ashton/framebuffer.rb +0 -183
- data/lib/ashton/post_process.rb +0 -83
- data/lib/ashton/post_process/default.vert +0 -9
- data/lib/ashton/post_process/fade.frag +0 -11
- data/lib/ashton/post_process/mezzotint.frag +0 -24
- data/lib/ashton/post_process/radial_blur.frag +0 -31
- data/lib/ashton/post_process/sepia.frag +0 -19
- data/lib/ashton/post_process/shockwave.frag +0 -40
- data/lib/ashton/post_process/tv_screen.frag +0 -32
data/examples/output/README.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Output from the examples will be saved here, to prove that some functionality works.
|
data/lib/ashton/base_shader.rb
DELETED
@@ -1,172 +0,0 @@
|
|
1
|
-
module Ashton
|
2
|
-
# @abstract
|
3
|
-
class BaseShader
|
4
|
-
INVALID_LOCATION = -1
|
5
|
-
MIN_OPENGL_VERSION = 2.0
|
6
|
-
|
7
|
-
attr_reader :fragment_source, :vertex_source
|
8
|
-
|
9
|
-
# Todo: Pass in a filename (String) or name of built-in pp shader (Symbol)
|
10
|
-
#
|
11
|
-
# @option options [String] :vertex Source code for vertex shader.
|
12
|
-
# @option options [String] :vert equivalent to :vertex
|
13
|
-
# @option options [String] :fragment Source code for fragment shader.
|
14
|
-
# @option options [String] :frag equivalent to :fragment
|
15
|
-
def initialize(vertex_source, fragment_source)
|
16
|
-
raise "Can't instantiate abstract class" if self.class == BaseShader
|
17
|
-
|
18
|
-
unless GL.version_supported? MIN_OPENGL_VERSION
|
19
|
-
raise NotSupportedError, "Ashton requires OpenGL #{MIN_OPENGL_VERSION} support to utilise shaders"
|
20
|
-
end
|
21
|
-
|
22
|
-
@uniform_locations = {}
|
23
|
-
@attribute_locations = {}
|
24
|
-
@program = nil
|
25
|
-
|
26
|
-
@vertex_source = vertex_source
|
27
|
-
@fragment_source = fragment_source
|
28
|
-
|
29
|
-
@vertex = compile GL_VERTEX_SHADER, vertex_source
|
30
|
-
@fragment = compile GL_FRAGMENT_SHADER, fragment_source
|
31
|
-
|
32
|
-
link
|
33
|
-
|
34
|
-
glBindFragDataLocationEXT @program, 0, "out_FragColor"
|
35
|
-
end
|
36
|
-
|
37
|
-
|
38
|
-
# Creates a copy of the shader program, recompiling the source,
|
39
|
-
# but not preserving the uniform values.
|
40
|
-
def dup
|
41
|
-
self.class.new :vertex => @vertex_source, :fragment => @fragment_source
|
42
|
-
end
|
43
|
-
|
44
|
-
# Make this the current shader program.
|
45
|
-
def use
|
46
|
-
previous_program = glGetIntegerv GL_CURRENT_PROGRAM
|
47
|
-
glUseProgram @program
|
48
|
-
|
49
|
-
if block_given?
|
50
|
-
result = yield self
|
51
|
-
$window.flush # TODO: need to work out how to make shader affect delayed draws.
|
52
|
-
glUseProgram previous_program
|
53
|
-
end
|
54
|
-
|
55
|
-
result
|
56
|
-
end
|
57
|
-
|
58
|
-
# Disable the shader program (not needed in block version of #use).
|
59
|
-
def disable
|
60
|
-
glUseProgram 0 # Disable the shader!
|
61
|
-
end
|
62
|
-
|
63
|
-
# Is this the current shader program?
|
64
|
-
def current?
|
65
|
-
glGetIntegerv(GL_CURRENT_PROGRAM) == @program
|
66
|
-
end
|
67
|
-
|
68
|
-
# Set the value of a uniform.
|
69
|
-
def []=(name, value)
|
70
|
-
use do
|
71
|
-
case value
|
72
|
-
when true, GL_TRUE
|
73
|
-
glUniform1i uniform(name), 1
|
74
|
-
|
75
|
-
when false, GL_FALSE
|
76
|
-
glUniform1i uniform(name), 0
|
77
|
-
|
78
|
-
when Float
|
79
|
-
glUniform1f uniform(name), value
|
80
|
-
|
81
|
-
when Integer
|
82
|
-
glUniform1i uniform(name), value
|
83
|
-
|
84
|
-
when Array
|
85
|
-
size = value.size
|
86
|
-
|
87
|
-
raise ArgumentError, "Empty array not supported for uniform data" if size.zero?
|
88
|
-
raise ArgumentError, "Only support uniforms up to 4 elements" if size > 4
|
89
|
-
|
90
|
-
case value[0]
|
91
|
-
when Float
|
92
|
-
GL.send "glUniform#{size}f", uniform(name), *value
|
93
|
-
|
94
|
-
when Integer
|
95
|
-
GL.send "glUniform#{size}i", uniform(name), *value
|
96
|
-
|
97
|
-
else
|
98
|
-
raise ArgumentError, "Uniform data type not supported for element of type: #{value[0].class}"
|
99
|
-
end
|
100
|
-
|
101
|
-
else
|
102
|
-
raise ArgumentError, "Uniform data type not supported for type: #{value.class}"
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
def uniform(name)
|
110
|
-
location = @uniform_locations[name]
|
111
|
-
if location
|
112
|
-
location
|
113
|
-
else
|
114
|
-
location = glGetUniformLocation @program, name.to_s
|
115
|
-
raise ShaderUniformError, "No #{name} uniform specified in program" if location == INVALID_LOCATION
|
116
|
-
@uniform_locations[name] = location
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
protected
|
121
|
-
def attribute(name)
|
122
|
-
location = @attribute_locations[name]
|
123
|
-
if location
|
124
|
-
location
|
125
|
-
else
|
126
|
-
location = glGetAttribLocation @program, name.to_s
|
127
|
-
raise ShaderAttributeError, "No #{name} attribute specified in program" if location == INVALID_LOCATION
|
128
|
-
@attribute_locations[name] = location
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
protected
|
133
|
-
def compile(type, source)
|
134
|
-
shader = glCreateShader type
|
135
|
-
glShaderSource shader, source
|
136
|
-
glCompileShader shader
|
137
|
-
|
138
|
-
unless glGetShaderiv shader, GL_COMPILE_STATUS
|
139
|
-
error = glGetShaderInfoLog shader
|
140
|
-
error_lines = error.scan(/0\((\d+)\)+/m).map {|num| num.first.to_i }.uniq
|
141
|
-
|
142
|
-
if type == GL_VERTEX_SHADER
|
143
|
-
type_name = "Vertex"
|
144
|
-
source = @vertex_source
|
145
|
-
else
|
146
|
-
type_name = "Fragment"
|
147
|
-
source = @fragment_source
|
148
|
-
end
|
149
|
-
|
150
|
-
source_lines = source.split("\n")
|
151
|
-
lines = error_lines.map {|i| "#{i.to_s.rjust 3}: #{source_lines[i - 1].rstrip}" }.join "\n"
|
152
|
-
raise ShaderCompileError, "#{type_name} shader error: #{glGetShaderInfoLog(shader)}\n#{lines}"
|
153
|
-
end
|
154
|
-
|
155
|
-
shader
|
156
|
-
end
|
157
|
-
|
158
|
-
protected
|
159
|
-
def link
|
160
|
-
@program = glCreateProgram
|
161
|
-
glAttachShader @program, @vertex
|
162
|
-
glAttachShader @program, @fragment
|
163
|
-
glLinkProgram @program
|
164
|
-
|
165
|
-
unless glGetProgramiv @program, GL_LINK_STATUS
|
166
|
-
raise ShaderLinkError, "Shader link error: #{glGetProgramInfoLog(@program)}"
|
167
|
-
end
|
168
|
-
|
169
|
-
nil
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
data/lib/ashton/framebuffer.rb
DELETED
@@ -1,183 +0,0 @@
|
|
1
|
-
module Ashton
|
2
|
-
class Framebuffer
|
3
|
-
TEXTURE_COORDINATES = [
|
4
|
-
[0, 1], # TL
|
5
|
-
[0, 0], # BL
|
6
|
-
[1, 0], # TR
|
7
|
-
[1, 1], # BR
|
8
|
-
]
|
9
|
-
|
10
|
-
TEXTURE_COORDINATES_FLIPPED = [
|
11
|
-
[0, 0], # BL
|
12
|
-
[0, 1], # TL
|
13
|
-
[1, 1], # BR
|
14
|
-
[1, 0], # TR
|
15
|
-
]
|
16
|
-
|
17
|
-
attr_reader :width, :height, :texture
|
18
|
-
|
19
|
-
def initialize(width, height)
|
20
|
-
@width, @height = width.to_i, height.to_i
|
21
|
-
@fbo, @texture = init_framebuffer
|
22
|
-
|
23
|
-
status = glCheckFramebufferStatusEXT GL_FRAMEBUFFER_EXT
|
24
|
-
raise unless status == GL_FRAMEBUFFER_COMPLETE_EXT
|
25
|
-
|
26
|
-
clear
|
27
|
-
|
28
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0
|
29
|
-
glBindRenderbufferEXT GL_RENDERBUFFER_EXT, 0
|
30
|
-
end
|
31
|
-
|
32
|
-
# Clears the buffer to transparent.
|
33
|
-
def clear(options = {})
|
34
|
-
options = {
|
35
|
-
color: [0.0, 0.0, 0.0, 0.0],
|
36
|
-
}.merge! options
|
37
|
-
|
38
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, @fbo
|
39
|
-
|
40
|
-
glClearColor *options[:color]
|
41
|
-
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
|
42
|
-
|
43
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0
|
44
|
-
end
|
45
|
-
|
46
|
-
# Enable the framebuffer to use (e.g. to draw or convert it).
|
47
|
-
# BUG: This will force all draws performed in, AND BEFORE, the block!
|
48
|
-
def use
|
49
|
-
raise ArgumentError, "block required" unless block_given?
|
50
|
-
|
51
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, @fbo
|
52
|
-
result = yield self
|
53
|
-
|
54
|
-
$window.flush
|
55
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0
|
56
|
-
|
57
|
-
result
|
58
|
-
end
|
59
|
-
|
60
|
-
# Draw the image, _immediately_ (no z-ordering by Gosu).
|
61
|
-
#
|
62
|
-
# This is not as versatile as converting the Framebuffer into a Gosu::Image and then
|
63
|
-
# drawing it, but it is many times faster, so use it when you are updating the buffer
|
64
|
-
# every frame, rather than just composing an image.
|
65
|
-
#
|
66
|
-
# Drawing in Gosu orientation will be flipped in standard OpenGL and visa versa.
|
67
|
-
#
|
68
|
-
# @param x [Number]
|
69
|
-
# @param y [Number]
|
70
|
-
# @option options :orientation [:gosu, :opengl] (:gosu)
|
71
|
-
def draw x, y, options = {}
|
72
|
-
options = {
|
73
|
-
:orientation => :gosu,
|
74
|
-
}.merge! options
|
75
|
-
|
76
|
-
glEnable GL_TEXTURE_2D
|
77
|
-
glBindTexture GL_TEXTURE_2D, @texture
|
78
|
-
|
79
|
-
coords = case options[:orientation]
|
80
|
-
when :gosu then TEXTURE_COORDINATES_FLIPPED
|
81
|
-
when :opengl then TEXTURE_COORDINATES
|
82
|
-
else raise ArgumentError, ":orientation option expected to be either :opengl or :gosu"
|
83
|
-
end
|
84
|
-
|
85
|
-
glBegin GL_QUADS do
|
86
|
-
glTexCoord2d *coords[0]
|
87
|
-
glVertex2d x, y + @height # BL
|
88
|
-
|
89
|
-
glTexCoord2d *coords[1]
|
90
|
-
glVertex2d x, y # TL
|
91
|
-
|
92
|
-
glTexCoord2d *coords[2]
|
93
|
-
glVertex2d x + @width, y # TR
|
94
|
-
|
95
|
-
glTexCoord2d *coords[3]
|
96
|
-
glVertex2d x + @width, y + @height # BR
|
97
|
-
end
|
98
|
-
|
99
|
-
glBindTexture GL_TEXTURE_2D, 0
|
100
|
-
end
|
101
|
-
|
102
|
-
# Convert the current contents of the buffer into a Gosu::Image
|
103
|
-
#
|
104
|
-
# @option options [Boolean] :caching (true) TexPlay behaviour.
|
105
|
-
# @option options [Boolean] :tileable (false) Standard Gosu behaviour.
|
106
|
-
# @option options [Array<Integer>] :rect ([0, 0, width, height]) Rectangular area of buffer to use to create the image [x, y, w, h]
|
107
|
-
def to_image(options = {})
|
108
|
-
options = {
|
109
|
-
rect: [0, 0, @width, @height],
|
110
|
-
tileable: false,
|
111
|
-
}.merge! options
|
112
|
-
|
113
|
-
rect = options[:rect]
|
114
|
-
|
115
|
-
# Draw onto the clean flip buffer, in order to flip before saving.
|
116
|
-
@fbo_flip, @fbo_flip_texture = init_framebuffer unless defined? @fbo_flip
|
117
|
-
|
118
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, @fbo_flip
|
119
|
-
glClearColor 0, 0, 0, 0
|
120
|
-
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
|
121
|
-
draw 0, 0
|
122
|
-
|
123
|
-
# Read the data in the flip-buffer.
|
124
|
-
glBindTexture GL_TEXTURE_2D, @fbo_flip_texture
|
125
|
-
blob = glReadPixels *rect, GL_RGBA, GL_UNSIGNED_BYTE
|
126
|
-
|
127
|
-
# Clean up.
|
128
|
-
glBindTexture GL_TEXTURE_2D, 0
|
129
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0
|
130
|
-
|
131
|
-
# Create a new Image from the flipped pixel data.
|
132
|
-
stub = ImageStub.new blob, rect[2], rect[3]
|
133
|
-
if defined? TexPlay
|
134
|
-
Gosu::Image.new $window, stub, options[:tileable], options
|
135
|
-
else
|
136
|
-
Gosu::Image.new $window, stub, options[:tileable]
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
# Create an fbo and its texture
|
142
|
-
def init_framebuffer
|
143
|
-
fbo = glGenFramebuffersEXT(1)[0]
|
144
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, fbo
|
145
|
-
|
146
|
-
texture = init_framebuffer_texture
|
147
|
-
|
148
|
-
glFramebufferTexture2DEXT GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0
|
149
|
-
|
150
|
-
[fbo, texture]
|
151
|
-
end
|
152
|
-
|
153
|
-
protected
|
154
|
-
# Called by init_framebuffer.
|
155
|
-
def init_framebuffer_texture
|
156
|
-
texture = glGenTextures(1)[0]
|
157
|
-
glBindTexture GL_TEXTURE_2D, texture
|
158
|
-
|
159
|
-
glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE
|
160
|
-
glTexParameterf GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE
|
161
|
-
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST
|
162
|
-
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST
|
163
|
-
|
164
|
-
glTexImage2D GL_TEXTURE_2D, 0, GL_RGBA8, @width, @height,
|
165
|
-
0, GL_RGBA, GL_UNSIGNED_BYTE, nil
|
166
|
-
|
167
|
-
glBindTexture GL_TEXTURE_2D, 0 # Unbind the texture
|
168
|
-
|
169
|
-
texture
|
170
|
-
end
|
171
|
-
|
172
|
-
protected
|
173
|
-
def delete
|
174
|
-
glDeleteFramebuffersEXT @fbo
|
175
|
-
glDeleteTextures @texture
|
176
|
-
|
177
|
-
glDeleteFramebuffersEXT @fbo_flip if defined? @fbo_flip
|
178
|
-
glDeleteTextures @fbo_flip_texture if defined? @fbo_flip_texture
|
179
|
-
|
180
|
-
glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
data/lib/ashton/post_process.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
require "ashton/base_shader"
|
2
|
-
|
3
|
-
module Ashton
|
4
|
-
# Process the entire screen.
|
5
|
-
class PostProcess < BaseShader
|
6
|
-
DEFAULT_VERTEX_SOURCE = File.read File.expand_path("../post_process/default.vert", __FILE__)
|
7
|
-
|
8
|
-
class << self
|
9
|
-
# Canvas used to copy out the screen before post-processing it back onto the screen.
|
10
|
-
# We only need one of these, but only create it after a PostProcessor has
|
11
|
-
# actually been used.
|
12
|
-
def texture
|
13
|
-
@texture ||= begin
|
14
|
-
texture = glGenTextures(1).first
|
15
|
-
glBindTexture GL_TEXTURE_2D, texture
|
16
|
-
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR
|
17
|
-
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR
|
18
|
-
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE
|
19
|
-
glTexParameteri GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE
|
20
|
-
glTexImage2D GL_TEXTURE_2D, 0, GL_RGB8, $window.width, $window.height, 0,
|
21
|
-
GL_RGB, GL_UNSIGNED_BYTE, "\0" * ($window.width * $window.height * 3)
|
22
|
-
texture
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# Todo: Pass in a filename (String) or name of built-in pp shader (Symbol)
|
28
|
-
def initialize(fragment)
|
29
|
-
super DEFAULT_VERTEX_SOURCE, fragment
|
30
|
-
|
31
|
-
# Set up defaults that we won't need to change at run-time.
|
32
|
-
use do
|
33
|
-
# Width/height are optional.
|
34
|
-
glUniform1i glGetUniformLocation(@program, "in_WindowWidth"), $window.width
|
35
|
-
glUniform1i glGetUniformLocation(@program, "in_WindowHeight"), $window.height
|
36
|
-
|
37
|
-
self["in_Texture"] = 0 # GL_TEXTURE0 will be activated.
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# Full screen post-processing using a fragment shader.
|
42
|
-
#
|
43
|
-
# It will force all previous draw operations to be flushed to the
|
44
|
-
# screen, so that you can, or example, process the "game world" before you draw the GUI.
|
45
|
-
#
|
46
|
-
# Variables set for you in the fragment shader:
|
47
|
-
# uniform sampler2D in_Texture; // Texture containing the screen image.
|
48
|
-
# uniform int in_WindowWidth;
|
49
|
-
# uniform int in_WindowHeight;
|
50
|
-
def process
|
51
|
-
$window.gl do
|
52
|
-
width, height = $window.width, $window.height
|
53
|
-
texture = PostProcess.texture
|
54
|
-
|
55
|
-
# Copy window contents into a frame-buffer.
|
56
|
-
glBindTexture GL_TEXTURE_2D, texture
|
57
|
-
glCopyTexImage2D GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, width, height, 0
|
58
|
-
|
59
|
-
# clear screen and set "normal" openGL coordinates.
|
60
|
-
glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
|
61
|
-
glColor4f 1.0, 1.0, 1.0, 1.0
|
62
|
-
glMatrixMode GL_PROJECTION
|
63
|
-
glLoadIdentity
|
64
|
-
glViewport 0, 0, width, height
|
65
|
-
glOrtho 0, width, height, 0, -1, 1
|
66
|
-
|
67
|
-
# Assign the canvas, which was a copy of the screen.
|
68
|
-
glActiveTexture GL_TEXTURE0
|
69
|
-
glBindTexture GL_TEXTURE_2D, texture
|
70
|
-
raise unless glGetIntegerv(GL_ACTIVE_TEXTURE) == GL_TEXTURE0
|
71
|
-
|
72
|
-
use do
|
73
|
-
glBegin GL_QUADS do
|
74
|
-
glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 0.0)
|
75
|
-
glTexCoord2f(1.0, 1.0); glVertex2f(width, 0.0)
|
76
|
-
glTexCoord2f(1.0, 0.0); glVertex2f(width, height)
|
77
|
-
glTexCoord2f(0.0, 0.0); glVertex2f(0.0, height)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|