rubygl 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +13 -5
- data/.travis/push-rdoc-to-gh-pages.sh +22 -0
- data/.travis.yml +18 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +12 -0
- data/LICENSE +21 -21
- data/README.md +40 -0
- data/Rakefile +98 -83
- data/bin/ffi_code_gen.rb +166 -166
- data/bin/gl_code_gen.rb +458 -458
- data/examples/faceted_example.rb +71 -64
- data/examples/instanced_example.rb +135 -127
- data/examples/phong_example.rb +80 -71
- data/ext/windows/RubyGL.so +0 -0
- data/lib/rubygl/event.rb +64 -0
- data/lib/{RubyGL → rubygl}/geometry.rb +216 -211
- data/lib/{RubyGL → rubygl}/math.rb +300 -300
- data/lib/{RubyGL → rubygl}/memory.rb +125 -121
- data/lib/rubygl/native/all_enums.rb +641 -0
- data/lib/{RubyGL/Native → rubygl/native}/glcontext.rb +23 -47
- data/lib/{RubyGL/Native → rubygl/native}/include/GLContext.h +36 -36
- data/lib/{RubyGL/Native → rubygl/native}/include/Input.h +15 -15
- data/lib/{RubyGL/Native → rubygl/native}/include/Window.h +27 -27
- data/lib/rubygl/native/input.rb +247 -0
- data/lib/{RubyGL/Native → rubygl/native}/opengl.rb +2808 -2032
- data/lib/{RubyGL/Native → rubygl/native}/src/GLContext.c +72 -72
- data/lib/{RubyGL/Native → rubygl/native}/src/Input.c +25 -25
- data/lib/{RubyGL/Native → rubygl/native}/src/Window.c +57 -57
- data/lib/{RubyGL/Native → rubygl/native}/window.rb +24 -24
- data/lib/{RubyGL → rubygl}/setup.rb +50 -50
- data/lib/{RubyGL → rubygl}/shader.rb +203 -203
- data/lib/{RubyGL → rubygl}/util.rb +77 -77
- data/lib/rubygl.rb +49 -48
- data/{RubyGL.gemspec → rubygl.gemspec} +20 -23
- data/test/test_util.rb +19 -0
- metadata +36 -33
- data/lib/RubyGL/Native/input.rb +0 -13
- data/lib/RubyGL/callback.rb +0 -10
data/examples/faceted_example.rb
CHANGED
@@ -1,65 +1,72 @@
|
|
1
|
-
require '../lib/rubygl'
|
2
|
-
|
3
|
-
# Default Setup (Window + OpenGL Context), OpenGL Calls Are Valid After This Is Created
|
4
|
-
config = RubyGL::DefaultSetup.new({:width => 400, :height => 400})
|
5
|
-
puts RubyGL::Native::glGetString(RubyGL::Native::GL_VERSION)
|
6
|
-
|
7
|
-
# Generate Vertices For A Diamond And Push Them To GPU Memory
|
8
|
-
diamond_vertices = RubyGL::ComplexShape.gen_diamond(0.6, 0.6, 14)
|
9
|
-
diamond_buff = RubyGL::VertexArray.new(diamond_vertices)
|
10
|
-
|
11
|
-
# Set Up Our Perspective Matrix And Pre-Programmed Shader
|
12
|
-
persp_mat = RubyGL::Mat4.perspective(90, 500.0 / 500.0, -0.001, -500)
|
13
|
-
shader = RubyGL::ShaderGenerator.faceted_shader()
|
14
|
-
shader.use_program()
|
15
|
-
|
16
|
-
# Get The Locations Of Our Input "Attribute" Variables To The Shader
|
17
|
-
position_loc = shader.attrib_location("position")
|
18
|
-
|
19
|
-
# Associate Our position Variable With Our Diamond GPU Buffer (3 Components Per Vertex)
|
20
|
-
diamond_buff.vertex_attrib_ptr(position_loc, 3)
|
21
|
-
|
22
|
-
# Powerful Shorthand For Specifying An OpenGL Uniform Function, Uniform Variable
|
23
|
-
# Name, Input Data, And Any Extra Parameters Required By The Given Function Symbol
|
24
|
-
shader.send_uniform(:glUniformMatrix4fv, "perspective", persp_mat.to_a, 1, RubyGL::Native::GL_FALSE)
|
25
|
-
shader.send_uniform(:glUniform3fv, "light", [-1.0, 0.0, -0.6], 1)
|
26
|
-
shader.send_uniform(:glUniform4fv, "color", [1.0, 0.0, 0.0, 0.2], 1)
|
27
|
-
|
28
|
-
# Standard Depth Test So That Z-Buffer Testing Is Used
|
29
|
-
RubyGL::Native.glEnable(RubyGL::Native::GL_DEPTH_TEST)
|
30
|
-
|
31
|
-
# Track The Frame Rate And Get A Counter For Rotation
|
32
|
-
frames, counter = 0, 0
|
33
|
-
time = Time.now.strftime("%s").to_i
|
34
|
-
|
35
|
-
#
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
|
1
|
+
require '../lib/rubygl'
|
2
|
+
|
3
|
+
# Default Setup (Window + OpenGL Context), OpenGL Calls Are Valid After This Is Created
|
4
|
+
config = RubyGL::DefaultSetup.new({:width => 400, :height => 400})
|
5
|
+
puts RubyGL::Native::glGetString(RubyGL::Native::GL_VERSION)
|
6
|
+
|
7
|
+
# Generate Vertices For A Diamond And Push Them To GPU Memory
|
8
|
+
diamond_vertices = RubyGL::ComplexShape.gen_diamond(0.6, 0.6, 14)
|
9
|
+
diamond_buff = RubyGL::VertexArray.new(diamond_vertices)
|
10
|
+
|
11
|
+
# Set Up Our Perspective Matrix And Pre-Programmed Shader
|
12
|
+
persp_mat = RubyGL::Mat4.perspective(90, 500.0 / 500.0, -0.001, -500)
|
13
|
+
shader = RubyGL::ShaderGenerator.faceted_shader()
|
14
|
+
shader.use_program()
|
15
|
+
|
16
|
+
# Get The Locations Of Our Input "Attribute" Variables To The Shader
|
17
|
+
position_loc = shader.attrib_location("position")
|
18
|
+
|
19
|
+
# Associate Our position Variable With Our Diamond GPU Buffer (3 Components Per Vertex)
|
20
|
+
diamond_buff.vertex_attrib_ptr(position_loc, 3)
|
21
|
+
|
22
|
+
# Powerful Shorthand For Specifying An OpenGL Uniform Function, Uniform Variable
|
23
|
+
# Name, Input Data, And Any Extra Parameters Required By The Given Function Symbol
|
24
|
+
shader.send_uniform(:glUniformMatrix4fv, "perspective", persp_mat.to_a, 1, RubyGL::Native::GL_FALSE)
|
25
|
+
shader.send_uniform(:glUniform3fv, "light", [-1.0, 0.0, -0.6], 1)
|
26
|
+
shader.send_uniform(:glUniform4fv, "color", [1.0, 0.0, 0.0, 0.2], 1)
|
27
|
+
|
28
|
+
# Standard Depth Test So That Z-Buffer Testing Is Used
|
29
|
+
RubyGL::Native.glEnable(RubyGL::Native::GL_DEPTH_TEST)
|
30
|
+
|
31
|
+
# Track The Frame Rate And Get A Counter For Rotation
|
32
|
+
frames, counter = 0, 0
|
33
|
+
time = Time.now.strftime("%s").to_i
|
34
|
+
|
35
|
+
# Callback Triggered When Application Is Closed
|
36
|
+
RubyGL::Event.register(:quit) {
|
37
|
+
diamond_buff.release
|
38
|
+
config.teardown
|
39
|
+
abort("Application Closed")
|
40
|
+
}
|
41
|
+
|
42
|
+
# Main Program Loop
|
43
|
+
loop {
|
44
|
+
RubyGL::Native.glClearColor(1.0, 1.0, 1.0, 1.0)
|
45
|
+
RubyGL::Native.glClear(RubyGL::Native::GL_COLOR_BUFFER_BIT | RubyGL::Native::GL_DEPTH_BUFFER_BIT)
|
46
|
+
|
47
|
+
# Matrix Operations Are Always Applied In Reverse Order They Were Multiplied
|
48
|
+
t1 = RubyGL::Mat4.translation(0.0, -0.25, -0.30) # 4: Translate
|
49
|
+
r1 = RubyGL::Mat4.rotation(0.0, 1.0, 0.0, counter)
|
50
|
+
r2 = RubyGL::Mat4.rotation(0.0, 0.0, 1.0, 45)
|
51
|
+
r3 = RubyGL::Mat4.rotation(0.0, 1.0, 0.0, -counter)
|
52
|
+
t1 *= r1 # 3: Spin Around New Y Axis
|
53
|
+
t1 *= r2 # 2: Tilt (45 Degrees)
|
54
|
+
t1 *= r3 # 1: Spin Around Initial Y Axis
|
55
|
+
shader.send_uniform(:glUniformMatrix4fv, "modelview", t1.to_a, 1, RubyGL::Native::GL_FALSE)
|
56
|
+
|
57
|
+
frames += 1
|
58
|
+
counter += 0.8
|
59
|
+
|
60
|
+
# Draw Our Diamond (3 Components Per Vertex)
|
61
|
+
diamond_buff.draw(3)
|
62
|
+
|
63
|
+
# Calculate Frame Rate
|
64
|
+
if (((Time.now.strftime("%s").to_i - time) * 1000.0).to_i > 1) then
|
65
|
+
puts frames
|
66
|
+
frames = 0
|
67
|
+
time = Time.now.strftime("%s").to_i
|
68
|
+
end
|
69
|
+
|
70
|
+
# Updates Screen And Executes Input Callbacks
|
71
|
+
config.end_frame()
|
65
72
|
}
|
@@ -1,128 +1,136 @@
|
|
1
|
-
require '../lib/rubygl'
|
2
|
-
|
3
|
-
# Default Setup (Window + OpenGL Context), OpenGL Calls Are Valid After This Is Created
|
4
|
-
config = RubyGL::DefaultSetup.new({:width =>
|
5
|
-
puts RubyGL::Native::glGetString(RubyGL::Native::GL_VERSION)
|
6
|
-
|
7
|
-
# Create A Custom Faceted Shader
|
8
|
-
shader = RubyGL::Shader.new('''
|
9
|
-
#version 130
|
10
|
-
uniform mat4 perspective;
|
11
|
-
uniform mat4 modelview;
|
12
|
-
in vec3 position;
|
13
|
-
in vec3 offset;
|
14
|
-
out vec3 vPosition;
|
15
|
-
|
16
|
-
void main() {
|
17
|
-
vec4 hPosition = modelview * vec4(offset + position, 1);
|
18
|
-
vPosition = hPosition.xyz;
|
19
|
-
|
20
|
-
gl_Position = perspective * hPosition;
|
21
|
-
}
|
22
|
-
''','''
|
23
|
-
#version 130
|
24
|
-
in vec3 vPosition;
|
25
|
-
uniform vec4 color;
|
26
|
-
uniform vec3 light;
|
27
|
-
out vec4 fColor;
|
28
|
-
|
29
|
-
void main() {
|
30
|
-
vec3 dx = dFdy(vPosition);
|
31
|
-
vec3 dy = dFdx(vPosition);
|
32
|
-
vec3 triangle_norm = normalize(cross(dx, dy));
|
33
|
-
float factor = clamp(dot(triangle_norm, light), 0, 1);
|
34
|
-
|
35
|
-
fColor = vec4(color.xyz * factor, color.w);
|
36
|
-
}
|
37
|
-
''')
|
38
|
-
shader.use_program()
|
39
|
-
|
40
|
-
# Generate A Sphere With Radius 0.2 And 0 Extra Rings (Double Tetrahedron)
|
41
|
-
sphere = RubyGL::ComplexShape.gen_sphere(0.2, 0)
|
42
|
-
|
43
|
-
# Generate Positions For Multiple Copies Of Our Shape
|
44
|
-
pos = []
|
45
|
-
for x in 1..60
|
46
|
-
for y in 1..60
|
47
|
-
for z in 1..60
|
48
|
-
pos.push([x * 2, y * 2, -z * 2])
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
pos.flatten!
|
53
|
-
puts pos.size / 3
|
54
|
-
# Allocate Vertex Attribute Arrays
|
55
|
-
vertices = RubyGL::VertexArray.new(sphere)
|
56
|
-
positions = RubyGL::VertexArray.new(pos)
|
57
|
-
|
58
|
-
persp_mat = RubyGL::Mat4.perspective(90, 500.0 / 500.0, -0.001, -500)
|
59
|
-
|
60
|
-
# Associate in Values With Our Attribute Arrays
|
61
|
-
vertices.vertex_attrib_ptr(shader.attrib_location("position"), 3)
|
62
|
-
positions.vertex_attrib_ptr(shader.attrib_location("offset"), 3)
|
63
|
-
|
64
|
-
# Increment Our Position Vector Once Every Instance
|
65
|
-
positions.vertex_attrib_div(shader.attrib_location("offset"), 1)
|
66
|
-
|
67
|
-
# Send Uniforms To Their Respective Shaders
|
68
|
-
shader.send_uniform(:glUniformMatrix4fv, "perspective", persp_mat.to_a, 1, RubyGL::Native::GL_FALSE)
|
69
|
-
shader.send_uniform(:glUniform3fv, "light", [-1.0, -1.0, -1.0], 1)
|
70
|
-
shader.send_uniform(:glUniform4fv, "color", [1.0, 0.0, 0.0, 1.0], 1)
|
71
|
-
|
72
|
-
# Standard Depth Test So That Z-Buffer Testing Is Used
|
73
|
-
RubyGL::Native.glEnable(RubyGL::Native::GL_DEPTH_TEST)
|
74
|
-
|
75
|
-
# Used To Reduce Graininess Between Clustered Fragments On The Screen
|
76
|
-
RubyGL::Native.glEnable(RubyGL::Native::GL_BLEND)
|
77
|
-
RubyGL::Native.glBlendFunc(RubyGL::Native::GL_ONE, RubyGL::Native::
|
78
|
-
|
79
|
-
# Track The Frame Rate And Get A Counter For Rotation
|
80
|
-
frames, counter = 0, 0
|
81
|
-
time = Time.now.strftime("%s").to_i
|
82
|
-
|
83
|
-
# Center Our Grid Of Shapes
|
84
|
-
t1 = RubyGL::Mat4.translation(-61, -60, -40)
|
85
|
-
focused, angle = false, 150
|
86
|
-
|
87
|
-
#
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
1
|
+
require '../lib/rubygl'
|
2
|
+
|
3
|
+
# Default Setup (Window + OpenGL Context), OpenGL Calls Are Valid After This Is Created
|
4
|
+
config = RubyGL::DefaultSetup.new({:width => 360, :height => 360})
|
5
|
+
puts RubyGL::Native::glGetString(RubyGL::Native::GL_VERSION)
|
6
|
+
|
7
|
+
# Create A Custom Faceted Shader
|
8
|
+
shader = RubyGL::Shader.new('''
|
9
|
+
#version 130
|
10
|
+
uniform mat4 perspective;
|
11
|
+
uniform mat4 modelview;
|
12
|
+
in vec3 position;
|
13
|
+
in vec3 offset;
|
14
|
+
out vec3 vPosition;
|
15
|
+
|
16
|
+
void main() {
|
17
|
+
vec4 hPosition = modelview * vec4(offset + position, 1);
|
18
|
+
vPosition = hPosition.xyz;
|
19
|
+
|
20
|
+
gl_Position = perspective * hPosition;
|
21
|
+
}
|
22
|
+
''','''
|
23
|
+
#version 130
|
24
|
+
in vec3 vPosition;
|
25
|
+
uniform vec4 color;
|
26
|
+
uniform vec3 light;
|
27
|
+
out vec4 fColor;
|
28
|
+
|
29
|
+
void main() {
|
30
|
+
vec3 dx = dFdy(vPosition);
|
31
|
+
vec3 dy = dFdx(vPosition);
|
32
|
+
vec3 triangle_norm = normalize(cross(dx, dy));
|
33
|
+
float factor = clamp(dot(triangle_norm, light), 0.3, 1);
|
34
|
+
|
35
|
+
fColor = vec4(color.xyz * factor, color.w);
|
36
|
+
}
|
37
|
+
''')
|
38
|
+
shader.use_program()
|
39
|
+
|
40
|
+
# Generate A Sphere With Radius 0.2 And 0 Extra Rings (Double Tetrahedron)
|
41
|
+
sphere = RubyGL::ComplexShape.gen_sphere(0.2, 0)
|
42
|
+
|
43
|
+
# Generate Positions For Multiple Copies Of Our Shape
|
44
|
+
pos = []
|
45
|
+
for x in 1..60
|
46
|
+
for y in 1..60
|
47
|
+
for z in 1..60
|
48
|
+
pos.push([x * 2, y * 2, -z * 2])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
pos.flatten!
|
53
|
+
puts pos.size / 3
|
54
|
+
# Allocate Vertex Attribute Arrays
|
55
|
+
vertices = RubyGL::VertexArray.new(sphere)
|
56
|
+
positions = RubyGL::VertexArray.new(pos)
|
57
|
+
|
58
|
+
persp_mat = RubyGL::Mat4.perspective(90, 500.0 / 500.0, -0.001, -500)
|
59
|
+
|
60
|
+
# Associate in Values With Our Attribute Arrays
|
61
|
+
vertices.vertex_attrib_ptr(shader.attrib_location("position"), 3)
|
62
|
+
positions.vertex_attrib_ptr(shader.attrib_location("offset"), 3)
|
63
|
+
|
64
|
+
# Increment Our Position Vector Once Every Instance
|
65
|
+
positions.vertex_attrib_div(shader.attrib_location("offset"), 1)
|
66
|
+
|
67
|
+
# Send Uniforms To Their Respective Shaders
|
68
|
+
shader.send_uniform(:glUniformMatrix4fv, "perspective", persp_mat.to_a, 1, RubyGL::Native::GL_FALSE)
|
69
|
+
shader.send_uniform(:glUniform3fv, "light", [-1.0, -1.0, -1.0], 1)
|
70
|
+
shader.send_uniform(:glUniform4fv, "color", [1.0, 0.0, 0.0, 1.0], 1)
|
71
|
+
|
72
|
+
# Standard Depth Test So That Z-Buffer Testing Is Used
|
73
|
+
RubyGL::Native.glEnable(RubyGL::Native::GL_DEPTH_TEST)
|
74
|
+
|
75
|
+
# Used To Reduce Graininess Between Clustered Fragments On The Screen
|
76
|
+
RubyGL::Native.glEnable(RubyGL::Native::GL_BLEND)
|
77
|
+
RubyGL::Native.glBlendFunc(RubyGL::Native::GL_ONE, RubyGL::Native::GL_ZERO)
|
78
|
+
|
79
|
+
# Track The Frame Rate And Get A Counter For Rotation
|
80
|
+
frames, counter = 0, 0
|
81
|
+
time = Time.now.strftime("%s").to_i
|
82
|
+
|
83
|
+
# Center Our Grid Of Shapes
|
84
|
+
t1 = RubyGL::Mat4.translation(-61, -60, -40)
|
85
|
+
focused, angle = false, 150
|
86
|
+
|
87
|
+
# Callback Triggered When Application Is Closed
|
88
|
+
RubyGL::Event.register(:quit) {
|
89
|
+
vertices.release
|
90
|
+
positions.release
|
91
|
+
config.teardown
|
92
|
+
abort("Application Closed")
|
93
|
+
}
|
94
|
+
|
95
|
+
# Main Program Loop
|
96
|
+
loop {
|
97
|
+
RubyGL::Native.glClearColor(1.0, 1.0, 1.0, 1.0)
|
98
|
+
RubyGL::Native.glClear(RubyGL::Native::GL_COLOR_BUFFER_BIT | RubyGL::Native::GL_DEPTH_BUFFER_BIT)
|
99
|
+
|
100
|
+
# Cube Is Rotated Into Place And Zoomed In To In A Spiral
|
101
|
+
r1 = nil
|
102
|
+
if !focused then
|
103
|
+
r1 = RubyGL::Mat4.rotation(0.0, 1.0, 0.0, angle)
|
104
|
+
angle -= 1.0
|
105
|
+
|
106
|
+
if angle <= 0 then
|
107
|
+
focused = true
|
108
|
+
end
|
109
|
+
|
110
|
+
r1 *= t1
|
111
|
+
else
|
112
|
+
r1 = RubyGL::Mat4.rotation(0.0, 0.0, 1.0, counter)
|
113
|
+
|
114
|
+
r1 *= t1
|
115
|
+
|
116
|
+
t1[3][2] += 0.28
|
117
|
+
counter += 0.3
|
118
|
+
end
|
119
|
+
|
120
|
+
shader.send_uniform(:glUniformMatrix4fv, "modelview", r1.to_a, 1, RubyGL::Native::GL_FALSE)
|
121
|
+
|
122
|
+
frames += 1
|
123
|
+
|
124
|
+
# Draw Shapes (3 Components Per Vertex & (pos.size / 3) Number Of Instances)
|
125
|
+
vertices.draw_instanced(3, pos.size / 3)
|
126
|
+
|
127
|
+
# Calculate Frame Rate
|
128
|
+
if (((Time.now.strftime("%s").to_i - time) * 1000.0).to_i > 1) then
|
129
|
+
puts frames
|
130
|
+
frames = 0
|
131
|
+
time = Time.now.strftime("%s").to_i
|
132
|
+
end
|
133
|
+
|
134
|
+
# Updates Screen And Executes Input Callbacks
|
135
|
+
config.end_frame()
|
128
136
|
}
|
data/examples/phong_example.rb
CHANGED
@@ -1,72 +1,81 @@
|
|
1
|
-
require '../lib/rubygl'
|
2
|
-
|
3
|
-
# Default Setup (Window + OpenGL Context), OpenGL Calls Are Valid After This Is Created
|
4
|
-
config = RubyGL::DefaultSetup.new({:width =>
|
5
|
-
puts RubyGL::Native::glGetString(RubyGL::Native::GL_VERSION)
|
6
|
-
|
7
|
-
# Generate Indices, Vertices, And Vertex Normals For A Diamond
|
8
|
-
(vertices, indices) = RubyGL::Util.gen_index_arrays(RubyGL::ComplexShape.gen_diamond(0.6, 0.6, 50), 3)
|
9
|
-
normals = RubyGL::Util.gen_vertex_normals(indices, vertices)
|
10
|
-
|
11
|
-
# Push All Of Our Data To The GPU
|
12
|
-
index_buff = RubyGL::IndexArray.new(indices)
|
13
|
-
vertex_buff = RubyGL::VertexArray.new(vertices)
|
14
|
-
normal_buff = RubyGL::VertexArray.new(normals)
|
15
|
-
|
16
|
-
# Set Up Our Perspective Matrix And Pre-Programmed Shader
|
17
|
-
persp_mat = RubyGL::Mat4.perspective(90, 500.0 / 500.0, -0.001, -500)
|
18
|
-
shader = RubyGL::ShaderGenerator.phong_shader()
|
19
|
-
shader.use_program()
|
20
|
-
|
21
|
-
# Get The Locations Of Our Input "Attribute" Variables To The Shader
|
22
|
-
position_loc = shader.attrib_location("position")
|
23
|
-
normal_loc = shader.attrib_location("normal")
|
24
|
-
|
25
|
-
# Associate Our position Variable With Our Diamond GPU Buffer (3 Components Per Vertex)
|
26
|
-
vertex_buff.vertex_attrib_ptr(position_loc, 3)
|
27
|
-
normal_buff.vertex_attrib_ptr(normal_loc, 3)
|
28
|
-
|
29
|
-
# Powerful Shorthand For Specifying An OpenGL Uniform Function, Uniform Variable
|
30
|
-
# Name, Input Data, And Any Extra Parameters Required By The Given Function Symbol
|
31
|
-
shader.send_uniform(:glUniformMatrix4fv, "perspective", persp_mat.to_a, 1, RubyGL::Native::GL_FALSE)
|
32
|
-
shader.send_uniform(:glUniform3fv, "light", [-1.0, -1.0, -5.0], 1)
|
33
|
-
shader.send_uniform(:glUniform4fv, "color", [1.0, 0.0, 0.0, 1.0], 1)
|
34
|
-
|
35
|
-
# Standard Depth Test So That Z-Buffer Testing Is Used
|
36
|
-
RubyGL::Native.glEnable(RubyGL::Native::GL_DEPTH_TEST)
|
37
|
-
|
38
|
-
# Track The Frame Rate And Get A Counter For Rotation
|
39
|
-
frames, counter = 0, 0
|
40
|
-
time = Time.now.strftime("%s").to_i
|
41
|
-
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
1
|
+
require '../lib/rubygl'
|
2
|
+
|
3
|
+
# Default Setup (Window + OpenGL Context), OpenGL Calls Are Valid After This Is Created
|
4
|
+
config = RubyGL::DefaultSetup.new({:width => 360, :height => 360})
|
5
|
+
puts RubyGL::Native::glGetString(RubyGL::Native::GL_VERSION)
|
6
|
+
|
7
|
+
# Generate Indices, Vertices, And Vertex Normals For A Diamond
|
8
|
+
(vertices, indices) = RubyGL::Util.gen_index_arrays(RubyGL::ComplexShape.gen_diamond(0.6, 0.6, 50), 3)
|
9
|
+
normals = RubyGL::Util.gen_vertex_normals(indices, vertices)
|
10
|
+
|
11
|
+
# Push All Of Our Data To The GPU
|
12
|
+
index_buff = RubyGL::IndexArray.new(indices)
|
13
|
+
vertex_buff = RubyGL::VertexArray.new(vertices)
|
14
|
+
normal_buff = RubyGL::VertexArray.new(normals)
|
15
|
+
|
16
|
+
# Set Up Our Perspective Matrix And Pre-Programmed Shader
|
17
|
+
persp_mat = RubyGL::Mat4.perspective(90, 500.0 / 500.0, -0.001, -500)
|
18
|
+
shader = RubyGL::ShaderGenerator.phong_shader()
|
19
|
+
shader.use_program()
|
20
|
+
|
21
|
+
# Get The Locations Of Our Input "Attribute" Variables To The Shader
|
22
|
+
position_loc = shader.attrib_location("position")
|
23
|
+
normal_loc = shader.attrib_location("normal")
|
24
|
+
|
25
|
+
# Associate Our position Variable With Our Diamond GPU Buffer (3 Components Per Vertex)
|
26
|
+
vertex_buff.vertex_attrib_ptr(position_loc, 3)
|
27
|
+
normal_buff.vertex_attrib_ptr(normal_loc, 3)
|
28
|
+
|
29
|
+
# Powerful Shorthand For Specifying An OpenGL Uniform Function, Uniform Variable
|
30
|
+
# Name, Input Data, And Any Extra Parameters Required By The Given Function Symbol
|
31
|
+
shader.send_uniform(:glUniformMatrix4fv, "perspective", persp_mat.to_a, 1, RubyGL::Native::GL_FALSE)
|
32
|
+
shader.send_uniform(:glUniform3fv, "light", [-1.0, -1.0, -5.0], 1)
|
33
|
+
shader.send_uniform(:glUniform4fv, "color", [1.0, 0.0, 0.0, 1.0], 1)
|
34
|
+
|
35
|
+
# Standard Depth Test So That Z-Buffer Testing Is Used
|
36
|
+
RubyGL::Native.glEnable(RubyGL::Native::GL_DEPTH_TEST)
|
37
|
+
|
38
|
+
# Track The Frame Rate And Get A Counter For Rotation
|
39
|
+
frames, counter = 0, 0
|
40
|
+
time = Time.now.strftime("%s").to_i
|
41
|
+
|
42
|
+
# Callback Triggered When Application Is Closed
|
43
|
+
RubyGL::Event.register(:quit) {
|
44
|
+
index_buff.release
|
45
|
+
vertex_buff.release
|
46
|
+
normal_buff.release
|
47
|
+
config.teardown
|
48
|
+
abort("Application Closed")
|
49
|
+
}
|
50
|
+
|
51
|
+
# Main Program Loop
|
52
|
+
loop {
|
53
|
+
RubyGL::Native.glClearColor(1.0, 1.0, 1.0, 1.0)
|
54
|
+
RubyGL::Native.glClear(RubyGL::Native::GL_COLOR_BUFFER_BIT | RubyGL::Native::GL_DEPTH_BUFFER_BIT)
|
55
|
+
|
56
|
+
# Matrix Operations Are Always Applied In Reverse Order They Were Multiplied
|
57
|
+
t1 = RubyGL::Mat4.translation(0.0, -0.25, -0.30) # 4: Translate
|
58
|
+
r1 = RubyGL::Mat4.rotation(0.0, 1.0, 0.0, counter)
|
59
|
+
r2 = RubyGL::Mat4.rotation(0.0, 0.0, 1.0, 45)
|
60
|
+
r3 = RubyGL::Mat4.rotation(0.0, 1.0, 0.0, -counter)
|
61
|
+
t1 *= r1 # 3: Spin Around New Y Axis
|
62
|
+
t1 *= r2 # 2: Tilt (45 Degrees)
|
63
|
+
t1 *= r3 # 1: Spin Around Initial Y Axis
|
64
|
+
shader.send_uniform(:glUniformMatrix4fv, "modelview", t1.to_a, 1, RubyGL::Native::GL_FALSE)
|
65
|
+
|
66
|
+
frames += 1
|
67
|
+
counter += 0.8
|
68
|
+
|
69
|
+
# Draw Our Diamond (3 Components Per Vertex)
|
70
|
+
index_buff.draw(3)
|
71
|
+
|
72
|
+
# Calculate Frame Rate
|
73
|
+
if (((Time.now.strftime("%s").to_i - time) * 1000.0).to_i > 1) then
|
74
|
+
puts frames
|
75
|
+
frames = 0
|
76
|
+
time = Time.now.strftime("%s").to_i
|
77
|
+
end
|
78
|
+
|
79
|
+
# Updates Screen And Executes Input Callbacks
|
80
|
+
config.end_frame()
|
72
81
|
}
|
data/ext/windows/RubyGL.so
CHANGED
Binary file
|
data/lib/rubygl/event.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require_relative './native/all_enums'
|
2
|
+
|
3
|
+
module RubyGL
|
4
|
+
|
5
|
+
class Event
|
6
|
+
@@callbacks = {}
|
7
|
+
|
8
|
+
# Accepts an event_type symbol (see native/all_enums.rb) and a block that
|
9
|
+
# will be passed a struct containing event information (see native/input.rb)
|
10
|
+
def self.register(event_type, &block)
|
11
|
+
callback = FFI::Function.new(:int, [:pointer, :pointer]) do |_, event_ptr|
|
12
|
+
event_union = Native::Event.new(event_ptr)
|
13
|
+
|
14
|
+
# Makes Our Case Statement Less Verbose
|
15
|
+
events = Native::ALL_EVENTS
|
16
|
+
|
17
|
+
# Map Multiple Events To The Same Struct
|
18
|
+
if (event_union[:common][:type] == events[event_type]) then
|
19
|
+
case events[event_type]
|
20
|
+
when events[:quit]
|
21
|
+
block.call(event_union[:quit])
|
22
|
+
when events[:window_event]
|
23
|
+
block.call(event_union[:window])
|
24
|
+
when events[:key_down], events[:key_up]
|
25
|
+
block.call(event_union[:key])
|
26
|
+
when events[:text_editing]
|
27
|
+
block.call(event_union[:edit])
|
28
|
+
when events[:text_input]
|
29
|
+
block.call(event_union[:text])
|
30
|
+
when events[:mouse_motion]
|
31
|
+
block.call(event_union[:motion])
|
32
|
+
when events[:mouse_button_down], events[:mouse_button_up]
|
33
|
+
block.call(event_union[:button])
|
34
|
+
when events[:mouse_wheel]
|
35
|
+
block.call(event_union[:wheel])
|
36
|
+
when events[:joy_axis_motion]
|
37
|
+
block.call(event_union[:jaxis])
|
38
|
+
when events[:joy_ball_motion]
|
39
|
+
block.call(event_union[:jball])
|
40
|
+
when events[:joy_hat_motion]
|
41
|
+
block.call(event_union[:jhat])
|
42
|
+
when events[:joy_button_down], events[:joy_button_up]
|
43
|
+
block.call(event_union[:jbutton])
|
44
|
+
when events[:joy_device_added], events[:joy_device_removed]
|
45
|
+
block.call(event_union[:jdevice])
|
46
|
+
when events[:controller_axis_motion]
|
47
|
+
block.call(event_union[:caxis])
|
48
|
+
when events[:controller_button_down], events[:controller_button_up]
|
49
|
+
block.call(event_union[:cbutton])
|
50
|
+
when events[:controller_device_added], events[:controller_device_removed], events[:controller_device_remapped]
|
51
|
+
block.call(event_union[:cdevice])
|
52
|
+
when events[:drop_file]
|
53
|
+
block.call(event_union[:drop])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
@@callbacks[event_type] = callback
|
58
|
+
|
59
|
+
Native.addEventWatch(callback, nil)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|