rbgl 0.1.0
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 +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE +21 -0
- data/README.md +123 -0
- data/Rakefile +12 -0
- data/examples/array_test.rb +99 -0
- data/examples/chemical_heartbeat.rb +166 -0
- data/examples/color_test.rb +61 -0
- data/examples/cube_spinning.rb +87 -0
- data/examples/dark_transit.rb +166 -0
- data/examples/fractured_orb.rb +428 -0
- data/examples/fractured_orb_rb.rb +598 -0
- data/examples/gradient.rb +84 -0
- data/examples/hexagonal_flow.rb +333 -0
- data/examples/multi_return_test.rb +98 -0
- data/examples/plasma.rb +78 -0
- data/examples/sphere_raymarch.rb +126 -0
- data/examples/teapot.rb +362 -0
- data/examples/teapot_mcu.rb +344 -0
- data/examples/triangle_basic.rb +36 -0
- data/examples/triangle_window.rb +62 -0
- data/examples/window_test.rb +36 -0
- data/lib/rbgl/engine/buffer.rb +160 -0
- data/lib/rbgl/engine/context.rb +157 -0
- data/lib/rbgl/engine/framebuffer.rb +115 -0
- data/lib/rbgl/engine/pipeline.rb +35 -0
- data/lib/rbgl/engine/rasterizer.rb +213 -0
- data/lib/rbgl/engine/shader.rb +324 -0
- data/lib/rbgl/engine/texture.rb +125 -0
- data/lib/rbgl/engine.rb +15 -0
- data/lib/rbgl/gui/backend.rb +76 -0
- data/lib/rbgl/gui/cocoa/backend.rb +121 -0
- data/lib/rbgl/gui/event.rb +34 -0
- data/lib/rbgl/gui/file_backend.rb +91 -0
- data/lib/rbgl/gui/wayland/backend.rb +126 -0
- data/lib/rbgl/gui/wayland/connection.rb +331 -0
- data/lib/rbgl/gui/window.rb +148 -0
- data/lib/rbgl/gui/x11/backend.rb +156 -0
- data/lib/rbgl/gui/x11/connection.rb +344 -0
- data/lib/rbgl/gui.rb +16 -0
- data/lib/rbgl/version.rb +5 -0
- data/lib/rbgl.rb +6 -0
- metadata +114 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5ff09c0ce9d51c2c28146dbcfdb9eecbe2a9982e45a43fbb81cd8bc1de4b96fb
|
|
4
|
+
data.tar.gz: e58db1f718d9e8a8e1fb7cac8e3acffc026eedfc18e64d427f1669e95e7674be
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b6f662d84ba8b4f44063d9d09e3ce7154bea6547ea3a53ad383b06cf719edb62d0e79ae5b85046b408d1ffa7125100d80a2414dc3213c509d41d9d89cb2e0f2c
|
|
7
|
+
data.tar.gz: a04b7ce7b7c20502af090e26fcdf0514cfe37850266bfce5a5ad27885999c3bababe0c6089e8cf2f9f31921cdf5c27c2a1e523e03a07b31f40469e048625c3bd
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yudai Takada
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# RBGL
|
|
2
|
+
|
|
3
|
+
RuBy Graphics Library - A pure Ruby graphics library with software rendering engine and cross-platform GUI support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'rbgl'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
$ bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
$ gem install rbgl
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Requirements
|
|
26
|
+
|
|
27
|
+
- Ruby 3.1.0 or higher
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Basic Window
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
require "rbgl"
|
|
35
|
+
|
|
36
|
+
window = RBGL::GUI::Window.new(width: 800, height: 600, title: "Hello RBGL")
|
|
37
|
+
|
|
38
|
+
window.run do |context, delta_time|
|
|
39
|
+
context.clear(color: Larb::Color.new(0.1, 0.1, 0.1, 1.0))
|
|
40
|
+
end
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Rendering with Pipeline
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
require "rbgl"
|
|
47
|
+
|
|
48
|
+
# Create a window with rendering context
|
|
49
|
+
window = RBGL::GUI::Window.new(width: 800, height: 600, title: "Triangle")
|
|
50
|
+
|
|
51
|
+
# Define vertex shader
|
|
52
|
+
vertex_shader = RBGL::Engine::Shader.new do |input, uniforms|
|
|
53
|
+
{ position: input[:position], color: input[:color] }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Define fragment shader
|
|
57
|
+
fragment_shader = RBGL::Engine::Shader.new do |input, uniforms|
|
|
58
|
+
input[:color]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Create pipeline
|
|
62
|
+
pipeline = RBGL::Engine::Pipeline.new(
|
|
63
|
+
vertex_shader: vertex_shader,
|
|
64
|
+
fragment_shader: fragment_shader
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# Create vertex buffer
|
|
68
|
+
vertices = [
|
|
69
|
+
{ position: Larb::Vec4.new(0.0, 0.5, 0.0, 1.0), color: Larb::Color.red },
|
|
70
|
+
{ position: Larb::Vec4.new(-0.5, -0.5, 0.0, 1.0), color: Larb::Color.green },
|
|
71
|
+
{ position: Larb::Vec4.new(0.5, -0.5, 0.0, 1.0), color: Larb::Color.blue }
|
|
72
|
+
]
|
|
73
|
+
vertex_buffer = RBGL::Engine::VertexBuffer.new(vertices)
|
|
74
|
+
|
|
75
|
+
window.run do |context, delta_time|
|
|
76
|
+
context.clear
|
|
77
|
+
context.bind_pipeline(pipeline)
|
|
78
|
+
context.bind_vertex_buffer(vertex_buffer)
|
|
79
|
+
context.draw_arrays(:triangles, 0, 3)
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Event Handling
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
window.on_key do |event|
|
|
87
|
+
case event.key
|
|
88
|
+
when :escape
|
|
89
|
+
window.stop
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
window.on_mouse do |event|
|
|
94
|
+
puts "Mouse: #{event.x}, #{event.y}"
|
|
95
|
+
end
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Backend Selection
|
|
99
|
+
|
|
100
|
+
RBGL automatically detects the appropriate backend for your platform:
|
|
101
|
+
|
|
102
|
+
- macOS: Cocoa (requires `metaco` gem)
|
|
103
|
+
- Linux: Wayland or X11 (based on environment)
|
|
104
|
+
|
|
105
|
+
To use the Cocoa backend on macOS, install the `metaco` gem:
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
$ gem install metaco
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
You can also specify a backend explicitly:
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
# Use specific backend
|
|
115
|
+
window = RBGL::GUI::Window.new(width: 800, height: 600, backend: :x11)
|
|
116
|
+
|
|
117
|
+
# Use file backend for headless rendering
|
|
118
|
+
window = RBGL::GUI::Window.new(width: 800, height: 600, backend: :file, output_path: "output.png")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Array Test Shader
|
|
4
|
+
# Tests the Ruby-mode array support in RLSL
|
|
5
|
+
|
|
6
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
7
|
+
|
|
8
|
+
require "rbgl"
|
|
9
|
+
require "rlsl"
|
|
10
|
+
|
|
11
|
+
WIDTH = 640
|
|
12
|
+
HEIGHT = 480
|
|
13
|
+
|
|
14
|
+
shader = RLSL.define(:array_test) do
|
|
15
|
+
uniforms do
|
|
16
|
+
float :time
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
functions do
|
|
20
|
+
define :get_color, returns: :vec3, params: { i: :int }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Helper functions in Ruby mode with arrays
|
|
24
|
+
helpers do
|
|
25
|
+
# Color palette as an array
|
|
26
|
+
COLORS = [
|
|
27
|
+
vec3(1.0, 0.0, 0.0), # Red
|
|
28
|
+
vec3(0.0, 1.0, 0.0), # Green
|
|
29
|
+
vec3(0.0, 0.0, 1.0), # Blue
|
|
30
|
+
vec3(1.0, 1.0, 0.0) # Yellow
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
def get_color(i)
|
|
34
|
+
COLORS[i]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
fragment do |frag_coord, resolution, u|
|
|
39
|
+
uv = vec2(frag_coord.x / resolution.x, frag_coord.y / resolution.y)
|
|
40
|
+
|
|
41
|
+
# Pick color based on quadrant
|
|
42
|
+
qx = 0
|
|
43
|
+
qy = 0
|
|
44
|
+
if uv.x > 0.5
|
|
45
|
+
qx = 1
|
|
46
|
+
end
|
|
47
|
+
if uv.y > 0.5
|
|
48
|
+
qy = 2
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
idx = qx + qy
|
|
52
|
+
color = get_color(idx)
|
|
53
|
+
|
|
54
|
+
# Add some animation
|
|
55
|
+
t = sin(u.time) * 0.5 + 0.5
|
|
56
|
+
vec3(color.x * t + 0.2, color.y * t + 0.2, color.z * t + 0.2)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Initialize display
|
|
61
|
+
window = RBGL::GUI::Window.new(width: WIDTH, height: HEIGHT, title: "Array Test")
|
|
62
|
+
|
|
63
|
+
puts "Array Test Shader"
|
|
64
|
+
puts "Tests Ruby-mode array support"
|
|
65
|
+
puts "Press 'q' or Escape to quit"
|
|
66
|
+
|
|
67
|
+
start_time = Time.now
|
|
68
|
+
frame_count = 0
|
|
69
|
+
last_fps_time = start_time
|
|
70
|
+
running = true
|
|
71
|
+
|
|
72
|
+
buffer = "\x00" * (WIDTH * HEIGHT * 4)
|
|
73
|
+
|
|
74
|
+
while running && !window.should_close?
|
|
75
|
+
time = Time.now - start_time
|
|
76
|
+
|
|
77
|
+
shader.render(buffer, WIDTH, HEIGHT, { time: time })
|
|
78
|
+
|
|
79
|
+
window.set_pixels(buffer)
|
|
80
|
+
|
|
81
|
+
events = window.poll_events_raw
|
|
82
|
+
events.each do |e|
|
|
83
|
+
case e[:type]
|
|
84
|
+
when :key_press
|
|
85
|
+
running = false if e[:key] == 12 || e[:key] == "q"
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
frame_count += 1
|
|
90
|
+
now = Time.now
|
|
91
|
+
if now - last_fps_time >= 1.0
|
|
92
|
+
fps = frame_count / (now - last_fps_time)
|
|
93
|
+
puts "FPS: #{fps.round(1)}"
|
|
94
|
+
frame_count = 0
|
|
95
|
+
last_fps_time = now
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
window.close
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# chemical heartbeat
|
|
4
|
+
# Original: https://www.shadertoy.com/view/lXtXD4
|
|
5
|
+
# License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
|
6
|
+
# https://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
|
|
7
|
+
|
|
8
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
9
|
+
|
|
10
|
+
require "rbgl"
|
|
11
|
+
require "rlsl"
|
|
12
|
+
|
|
13
|
+
WIDTH = 640
|
|
14
|
+
HEIGHT = 480
|
|
15
|
+
|
|
16
|
+
# Define shader using pure Ruby DSL
|
|
17
|
+
shader = RLSL.define(:chemical_heartbeat) do
|
|
18
|
+
uniforms do
|
|
19
|
+
float :time
|
|
20
|
+
float :camera_x
|
|
21
|
+
float :camera_y
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
fragment do |frag_coord, resolution, u|
|
|
25
|
+
uv = frag_coord / resolution.y
|
|
26
|
+
|
|
27
|
+
# Constants
|
|
28
|
+
inv_hex_size = 50.0
|
|
29
|
+
sqrt3_div3 = 0.5773502691896257
|
|
30
|
+
one_div3 = 0.3333333333333333
|
|
31
|
+
two_div3 = 0.6666666666666666
|
|
32
|
+
inv_warp_grid = 5.0
|
|
33
|
+
warp_clamp = 1.7
|
|
34
|
+
warp_scale = 4.4
|
|
35
|
+
warp_amplitude = 0.015
|
|
36
|
+
warp_speed = 1.5
|
|
37
|
+
time_dilation = 0.2
|
|
38
|
+
|
|
39
|
+
# Colors
|
|
40
|
+
color_a = vec3(58.0 / 255.0, 1.0 / 255.0, 92.0 / 255.0)
|
|
41
|
+
color_b = vec3(201.0 / 255.0, 100.0 / 255.0, 128.0 / 255.0)
|
|
42
|
+
color_c = vec3(59.0 / 255.0, 206.0 / 255.0, 172.0 / 255.0)
|
|
43
|
+
color_d = vec3(17.0 / 255.0, 0.0 / 255.0, 28.0 / 255.0)
|
|
44
|
+
|
|
45
|
+
# Apply camera offset
|
|
46
|
+
base_ux = uv.x - 0.5 + u.camera_x
|
|
47
|
+
base_uy = uv.y - 0.5 + u.camera_y
|
|
48
|
+
|
|
49
|
+
# Zoomy warp
|
|
50
|
+
pos_x = base_ux * inv_warp_grid
|
|
51
|
+
pos_y = base_uy * inv_warp_grid
|
|
52
|
+
ax = floor(pos_x)
|
|
53
|
+
ay = floor(pos_y)
|
|
54
|
+
|
|
55
|
+
hash_a = hash21(vec2(ax, ay))
|
|
56
|
+
hash_b = hash21(vec2(ax + 1.0, ay))
|
|
57
|
+
hash_c = hash21(vec2(ax, ay + 1.0))
|
|
58
|
+
hash_d = hash21(vec2(ax + 1.0, ay + 1.0))
|
|
59
|
+
|
|
60
|
+
diff_x = pos_x - ax
|
|
61
|
+
diff_y = pos_y - ay
|
|
62
|
+
|
|
63
|
+
warp = hash_a + (hash_b - hash_a) * diff_x
|
|
64
|
+
warp = warp + ((hash_c + (hash_d - hash_c) * diff_x) - warp) * diff_y
|
|
65
|
+
warp = warp + u.time * warp_speed
|
|
66
|
+
|
|
67
|
+
smoothed = (1.0 + cos(warp * TAU)) * 0.5
|
|
68
|
+
clamped = smoothed * warp_scale - warp_clamp
|
|
69
|
+
clamped = clamp(clamped, 0.0, 1.0)
|
|
70
|
+
|
|
71
|
+
ux = base_ux + cos(clamped) * warp_amplitude
|
|
72
|
+
uy = base_uy + sin(clamped) * warp_amplitude
|
|
73
|
+
|
|
74
|
+
# Axial from pixel
|
|
75
|
+
axial_q = (sqrt3_div3 * ux - one_div3 * uy) * inv_hex_size
|
|
76
|
+
axial_r = two_div3 * uy * inv_hex_size
|
|
77
|
+
axial_s = 0.0 - axial_q - axial_r
|
|
78
|
+
|
|
79
|
+
# Axial round
|
|
80
|
+
q = floor(axial_q)
|
|
81
|
+
r = floor(axial_r)
|
|
82
|
+
s = floor(axial_s)
|
|
83
|
+
|
|
84
|
+
q_diff = abs(q - axial_q)
|
|
85
|
+
r_diff = abs(r - axial_r)
|
|
86
|
+
s_diff = abs(s - axial_s)
|
|
87
|
+
|
|
88
|
+
if q_diff > r_diff && q_diff > s_diff
|
|
89
|
+
q = 0.0 - r - s
|
|
90
|
+
elsif r_diff > s_diff
|
|
91
|
+
r = 0.0 - q - s
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Hash for hex cell
|
|
95
|
+
hash_val = hash21(vec2(q, r))
|
|
96
|
+
|
|
97
|
+
# Animated color value
|
|
98
|
+
value = fract(hash_val + u.time * time_dilation)
|
|
99
|
+
value = smoothstep(0.0, 1.0, value)
|
|
100
|
+
value = (1.0 + cos(value * TAU)) * 0.5
|
|
101
|
+
|
|
102
|
+
# Color mixing
|
|
103
|
+
ab = mix(color_a, color_b, value)
|
|
104
|
+
bc = mix(color_b, color_c, value)
|
|
105
|
+
cd = mix(color_c, color_d, value)
|
|
106
|
+
|
|
107
|
+
left = mix(ab, bc, value)
|
|
108
|
+
right = mix(bc, cd, value)
|
|
109
|
+
|
|
110
|
+
mix(left, right, value)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Initialize display
|
|
115
|
+
window = RBGL::GUI::Window.new(width: WIDTH, height: HEIGHT, title: "Chemical Heartbeat")
|
|
116
|
+
|
|
117
|
+
puts "Chemical Heartbeat"
|
|
118
|
+
puts "Original: https://www.shadertoy.com/view/lXtXD4"
|
|
119
|
+
puts "License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0"
|
|
120
|
+
puts "https://creativecommons.org/licenses/by-nc-sa/3.0/deed.en"
|
|
121
|
+
puts "Press 'q' or Escape to quit"
|
|
122
|
+
|
|
123
|
+
start_time = Time.now
|
|
124
|
+
frame_count = 0
|
|
125
|
+
last_fps_time = start_time
|
|
126
|
+
running = true
|
|
127
|
+
|
|
128
|
+
buffer = "\x00" * (WIDTH * HEIGHT * 4)
|
|
129
|
+
|
|
130
|
+
while running && !window.should_close?
|
|
131
|
+
time = Time.now - start_time
|
|
132
|
+
|
|
133
|
+
# Camera movement
|
|
134
|
+
circle_value = time * 0.1
|
|
135
|
+
wave_value = time * 0.33
|
|
136
|
+
radius = 1.0 + Math.cos(wave_value) * 0.25
|
|
137
|
+
camera_x = Math.cos(circle_value) * radius
|
|
138
|
+
camera_y = Math.sin(circle_value) * radius
|
|
139
|
+
|
|
140
|
+
# Render using compiled shader
|
|
141
|
+
shader.render(buffer, WIDTH, HEIGHT, {
|
|
142
|
+
time: time,
|
|
143
|
+
camera_x: camera_x,
|
|
144
|
+
camera_y: camera_y
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
window.set_pixels(buffer)
|
|
148
|
+
|
|
149
|
+
events = window.poll_events_raw
|
|
150
|
+
events.each do |e|
|
|
151
|
+
if e[:type] == :key_press && (e[:key] == 12 || e[:key] == "q")
|
|
152
|
+
running = false
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
frame_count += 1
|
|
157
|
+
now = Time.now
|
|
158
|
+
if now - last_fps_time >= 1.0
|
|
159
|
+
fps = frame_count / (now - last_fps_time)
|
|
160
|
+
puts "FPS: #{fps.round(1)}"
|
|
161
|
+
frame_count = 0
|
|
162
|
+
last_fps_time = now
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
window.close
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Color Test - RGB verification
|
|
4
|
+
# Displays red, green, blue, and yellow squares
|
|
5
|
+
|
|
6
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
7
|
+
|
|
8
|
+
require "rbgl"
|
|
9
|
+
require "rlsl"
|
|
10
|
+
|
|
11
|
+
WIDTH = 640
|
|
12
|
+
HEIGHT = 480
|
|
13
|
+
|
|
14
|
+
shader = RLSL.define(:color_test) do
|
|
15
|
+
uniforms do
|
|
16
|
+
float :time
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Pure Ruby DSL fragment shader
|
|
20
|
+
# 4 quadrants: Top-left Red, Top-right Green, Bottom-left Blue, Bottom-right Yellow
|
|
21
|
+
fragment do |frag_coord, resolution, _u|
|
|
22
|
+
x = frag_coord.x / resolution.y
|
|
23
|
+
y = frag_coord.y / resolution.y
|
|
24
|
+
|
|
25
|
+
if x < 0.5 && y >= 0.5
|
|
26
|
+
vec3(1.0, 0.0, 0.0)
|
|
27
|
+
elsif x >= 0.5 && y >= 0.5
|
|
28
|
+
vec3(0.0, 1.0, 0.0)
|
|
29
|
+
elsif x < 0.5 && y < 0.5
|
|
30
|
+
vec3(0.0, 0.0, 1.0)
|
|
31
|
+
else
|
|
32
|
+
vec3(1.0, 1.0, 0.0)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Initialize display
|
|
38
|
+
window = RBGL::GUI::Window.new(width: WIDTH, height: HEIGHT, title: "Color Test")
|
|
39
|
+
|
|
40
|
+
puts "Color Test"
|
|
41
|
+
puts "Top-left: Red, Top-right: Green"
|
|
42
|
+
puts "Bottom-left: Blue, Bottom-right: Yellow"
|
|
43
|
+
puts "Press 'q' or Escape to quit"
|
|
44
|
+
|
|
45
|
+
running = true
|
|
46
|
+
buffer = "\x00" * (WIDTH * HEIGHT * 4)
|
|
47
|
+
|
|
48
|
+
while running && !window.should_close?
|
|
49
|
+
shader.render(buffer, WIDTH, HEIGHT, { time: 0.0 })
|
|
50
|
+
|
|
51
|
+
window.set_pixels(buffer)
|
|
52
|
+
|
|
53
|
+
events = window.poll_events_raw
|
|
54
|
+
events.each do |e|
|
|
55
|
+
if e[:type] == :key_press && (e[:key] == 12 || e[:key] == "q")
|
|
56
|
+
running = false
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
window.close
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
4
|
+
|
|
5
|
+
require "rbgl"
|
|
6
|
+
|
|
7
|
+
include Larb
|
|
8
|
+
include RBGL::Engine
|
|
9
|
+
|
|
10
|
+
window = RBGL::GUI::Window.new(
|
|
11
|
+
width: 320,
|
|
12
|
+
height: 240,
|
|
13
|
+
title: "Spinning Cube",
|
|
14
|
+
backend: :auto
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
pipeline = Pipeline.create do
|
|
18
|
+
vertex do |input, uniforms, output|
|
|
19
|
+
output.position = uniforms.mvp * input.position.to_vec4
|
|
20
|
+
output.color = input.color
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
fragment do |input, _uniforms, output|
|
|
24
|
+
output.color = input.color
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def create_cube
|
|
29
|
+
layout = VertexLayout.position_color
|
|
30
|
+
buffer = VertexBuffer.new(layout)
|
|
31
|
+
|
|
32
|
+
colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.cyan, Color.magenta]
|
|
33
|
+
|
|
34
|
+
faces = [
|
|
35
|
+
[[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]],
|
|
36
|
+
[[1, -1, -1], [-1, -1, -1], [-1, 1, -1], [1, 1, -1]],
|
|
37
|
+
[[-1, 1, 1], [1, 1, 1], [1, 1, -1], [-1, 1, -1]],
|
|
38
|
+
[[-1, -1, -1], [1, -1, -1], [1, -1, 1], [-1, -1, 1]],
|
|
39
|
+
[[1, -1, 1], [1, -1, -1], [1, 1, -1], [1, 1, 1]],
|
|
40
|
+
[[-1, -1, -1], [-1, -1, 1], [-1, 1, 1], [-1, 1, -1]]
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
faces.each_with_index do |face, i|
|
|
44
|
+
color = colors[i]
|
|
45
|
+
[[0, 1, 2], [0, 2, 3]].each do |tri|
|
|
46
|
+
tri.each do |vi|
|
|
47
|
+
pos = face[vi]
|
|
48
|
+
buffer.add_vertex(
|
|
49
|
+
position: Vec3[pos[0] * 0.5, pos[1] * 0.5, pos[2] * 0.5],
|
|
50
|
+
color: color
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
buffer
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
cube = create_cube
|
|
60
|
+
rotation = 0.0
|
|
61
|
+
|
|
62
|
+
view = Mat4.look_at(
|
|
63
|
+
Vec3[0, 1, 3],
|
|
64
|
+
Vec3[0, 0, 0],
|
|
65
|
+
Vec3.up
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
window.on(:key_press) do |event|
|
|
69
|
+
puts "Key pressed: #{event.key}"
|
|
70
|
+
window.stop if event.key == 12 || event.key == "q" || event.key == "Escape"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
puts "Press 'q' or Escape to quit"
|
|
74
|
+
|
|
75
|
+
window.run do |ctx, dt|
|
|
76
|
+
rotation += dt * 1.0
|
|
77
|
+
|
|
78
|
+
model = Mat4.rotation_y(rotation) * Mat4.rotation_x(rotation * 0.7)
|
|
79
|
+
projection = Mat4.perspective(Math::PI / 4, ctx.aspect_ratio, 0.1, 100)
|
|
80
|
+
mvp = projection * view * model
|
|
81
|
+
|
|
82
|
+
ctx.clear(color: Color.from_hex("#0f0f23"))
|
|
83
|
+
ctx.bind_pipeline(pipeline)
|
|
84
|
+
ctx.bind_vertex_buffer(cube)
|
|
85
|
+
ctx.set_uniform(:mvp, mvp)
|
|
86
|
+
ctx.draw_arrays(:triangles, 0, cube.vertex_count)
|
|
87
|
+
end
|