ruby_rpg 0.0.4 → 0.0.5
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 +4 -4
- data/README.md +21 -21
- data/bin/build.bash +13 -13
- data/bin/import +44 -44
- data/glfw-3.3.9.bin.MACOS/LICENSE.md +23 -23
- data/glfw-3.3.9.bin.MACOS/README.md +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/build_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/build_guide.html +199 -199
- data/glfw-3.3.9.bin.MACOS/docs/html/compat_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/compat_guide.html +153 -153
- data/glfw-3.3.9.bin.MACOS/docs/html/compile_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/compile_guide.html +223 -223
- data/glfw-3.3.9.bin.MACOS/docs/html/context_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/context_guide.html +259 -259
- data/glfw-3.3.9.bin.MACOS/docs/html/deprecated.html +88 -88
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_08423ce7729c4554356a7de7171ba263.html +85 -85
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_2dcebcdf14b42e92a6ed85c61bda6b9d.html +93 -93
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_62ce056722ee97c199075aa1d6eca309.html +95 -95
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_dddf4cf62655095a666cf715de3a31a2.html +91 -91
- data/glfw-3.3.9.bin.MACOS/docs/html/doc.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/docd.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/doxygen.css +1685 -1685
- data/glfw-3.3.9.bin.MACOS/docs/html/doxygen.svg +28 -28
- data/glfw-3.3.9.bin.MACOS/docs/html/dynsections.js +192 -192
- data/glfw-3.3.9.bin.MACOS/docs/html/extra.css +1 -1
- data/glfw-3.3.9.bin.MACOS/docs/html/files.html +91 -91
- data/glfw-3.3.9.bin.MACOS/docs/html/folderclosed.svg +11 -11
- data/glfw-3.3.9.bin.MACOS/docs/html/folderclosedd.svg +11 -11
- data/glfw-3.3.9.bin.MACOS/docs/html/folderopen.svg +17 -17
- data/glfw-3.3.9.bin.MACOS/docs/html/folderopend.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3_8h.html +1661 -1661
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3_8h_source.html +1175 -1175
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3native_8h.html +167 -167
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3native_8h_source.html +303 -303
- data/glfw-3.3.9.bin.MACOS/docs/html/group__buttons.html +282 -282
- data/glfw-3.3.9.bin.MACOS/docs/html/group__context.html +304 -304
- data/glfw-3.3.9.bin.MACOS/docs/html/group__errors.html +304 -304
- data/glfw-3.3.9.bin.MACOS/docs/html/group__gamepad__axes.html +202 -202
- data/glfw-3.3.9.bin.MACOS/docs/html/group__gamepad__buttons.html +410 -410
- data/glfw-3.3.9.bin.MACOS/docs/html/group__hat__state.html +234 -234
- data/glfw-3.3.9.bin.MACOS/docs/html/group__init.html +570 -570
- data/glfw-3.3.9.bin.MACOS/docs/html/group__input.html +2255 -2255
- data/glfw-3.3.9.bin.MACOS/docs/html/group__joysticks.html +362 -362
- data/glfw-3.3.9.bin.MACOS/docs/html/group__keys.html +2034 -2034
- data/glfw-3.3.9.bin.MACOS/docs/html/group__mods.html +198 -198
- data/glfw-3.3.9.bin.MACOS/docs/html/group__monitor.html +848 -848
- data/glfw-3.3.9.bin.MACOS/docs/html/group__native.html +812 -812
- data/glfw-3.3.9.bin.MACOS/docs/html/group__shapes.html +198 -198
- data/glfw-3.3.9.bin.MACOS/docs/html/group__vulkan.html +359 -359
- data/glfw-3.3.9.bin.MACOS/docs/html/group__window.html +3434 -3434
- data/glfw-3.3.9.bin.MACOS/docs/html/index.html +100 -100
- data/glfw-3.3.9.bin.MACOS/docs/html/input_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/input_guide.html +570 -570
- data/glfw-3.3.9.bin.MACOS/docs/html/internal_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/internals_guide.html +132 -132
- data/glfw-3.3.9.bin.MACOS/docs/html/intro_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/intro_guide.html +351 -351
- data/glfw-3.3.9.bin.MACOS/docs/html/jquery.js +33 -33
- data/glfw-3.3.9.bin.MACOS/docs/html/main_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/menu.js +136 -136
- data/glfw-3.3.9.bin.MACOS/docs/html/menudata.js +29 -29
- data/glfw-3.3.9.bin.MACOS/docs/html/minus.svg +8 -8
- data/glfw-3.3.9.bin.MACOS/docs/html/minusd.svg +8 -8
- data/glfw-3.3.9.bin.MACOS/docs/html/monitor_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/monitor_guide.html +229 -229
- data/glfw-3.3.9.bin.MACOS/docs/html/moving_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/moving_guide.html +374 -374
- data/glfw-3.3.9.bin.MACOS/docs/html/news.html +634 -634
- data/glfw-3.3.9.bin.MACOS/docs/html/news_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/pages.html +99 -99
- data/glfw-3.3.9.bin.MACOS/docs/html/plus.svg +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/plusd.svg +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/quick_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/quick_guide.html +397 -397
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_0.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_10.js +65 -65
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_11.js +26 -26
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_12.js +44 -44
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_13.js +34 -34
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_14.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_15.js +52 -52
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_16.js +69 -69
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_17.js +51 -51
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_18.js +14 -14
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_19.js +36 -36
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1a.js +98 -98
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1b.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_2.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_3.js +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_4.js +57 -57
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_5.js +22 -22
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_6.js +103 -103
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_7.js +23 -23
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_8.js +35 -35
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_9.js +63 -63
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_a.js +505 -505
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_b.js +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_c.js +50 -50
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_d.js +17 -17
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_e.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_f.js +35 -35
- data/glfw-3.3.9.bin.MACOS/docs/html/search/classes_0.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/close.svg +18 -18
- data/glfw-3.3.9.bin.MACOS/docs/html/search/defines_0.js +34 -34
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_1.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_3.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_4.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_5.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_7.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_8.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/functions_0.js +146 -146
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_0.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_10.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_2.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_3.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_4.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_5.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_7.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_8.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_9.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_a.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_b.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_c.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_d.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_e.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_f.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag.svg +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_d.svg +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_sel.svg +31 -31
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_seld.svg +31 -31
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_10.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_3.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_4.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_5.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_7.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_8.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_9.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_a.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_b.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_c.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_d.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_e.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_f.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/search.css +291 -291
- data/glfw-3.3.9.bin.MACOS/docs/html/search/search.js +840 -840
- data/glfw-3.3.9.bin.MACOS/docs/html/search/searchdata.js +39 -39
- data/glfw-3.3.9.bin.MACOS/docs/html/search/typedefs_0.js +32 -32
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_1.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_3.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_4.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_5.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_7.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/spaces.svg +877 -877
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wgamepadstate.html +134 -134
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wgammaramp.html +170 -170
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wimage.html +151 -151
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wvidmode.html +204 -204
- data/glfw-3.3.9.bin.MACOS/docs/html/vulkan_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/vulkan_guide.html +196 -196
- data/glfw-3.3.9.bin.MACOS/docs/html/window_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/window_guide.html +763 -763
- data/glfw-3.3.9.bin.MACOS/include/GLFW/glfw3.h +5932 -5932
- data/glfw-3.3.9.bin.MACOS/include/GLFW/glfw3native.h +634 -634
- data/glfw-3.3.9.bin.MACOS/lib-arm64/libglfw.3.dylib +0 -0
- data/glfw-3.3.9.bin.MACOS/lib-universal/libglfw.3.dylib +0 -0
- data/glfw-3.3.9.bin.MACOS/lib-x86_64/libglfw.3.dylib +0 -0
- data/lib/engine/autoloader.rb +11 -11
- data/lib/engine/camera.rb +12 -12
- data/lib/engine/component.rb +63 -63
- data/lib/engine/components/audio_source.rb +41 -41
- data/lib/engine/components/direction_light.rb +22 -22
- data/lib/engine/components/font_renderer.rb +134 -134
- data/lib/engine/components/mesh_renderer.rb +31 -31
- data/lib/engine/components/orthographic_camera.rb +54 -54
- data/lib/engine/components/perspective_camera.rb +53 -53
- data/lib/engine/components/point_light.rb +23 -23
- data/lib/engine/components/sprite_renderer.rb +141 -141
- data/lib/engine/components/ui_font_renderer.rb +140 -140
- data/lib/engine/components/ui_sprite_clickbox.rb +62 -0
- data/lib/engine/components/ui_sprite_renderer.rb +101 -101
- data/lib/engine/compute_shader.rb +93 -0
- data/lib/engine/cursor.rb +43 -43
- data/lib/engine/debugging.rb +41 -41
- data/lib/engine/engine.rb +131 -131
- data/lib/engine/font.rb +74 -74
- data/lib/engine/game_object.rb +234 -234
- data/lib/engine/importers/font_importer.rb +69 -69
- data/lib/engine/importers/obj_file.rb +244 -244
- data/lib/engine/importers/obj_importer.rb +33 -33
- data/lib/engine/input.rb +105 -104
- data/lib/engine/material.rb +79 -79
- data/lib/engine/mesh.rb +43 -43
- data/lib/engine/path.rb +92 -92
- data/lib/engine/physics/collision.rb +12 -12
- data/lib/engine/physics/components/cube_collider.rb +6 -6
- data/lib/engine/physics/components/rigidbody.rb +103 -103
- data/lib/engine/physics/components/sphere_collider.rb +93 -93
- data/lib/engine/physics/physics_resolver.rb +37 -37
- data/lib/engine/polygon_mesh.rb +45 -45
- data/lib/engine/quaternion.rb +234 -234
- data/lib/engine/rendering/instance_renderer.rb +153 -153
- data/lib/engine/rendering/render_pipeline.rb +35 -35
- data/lib/engine/screenshoter.rb +34 -34
- data/lib/engine/shader.rb +119 -91
- data/lib/engine/shaders/colour_frag.glsl +8 -8
- data/lib/engine/shaders/colour_vertex.glsl +13 -13
- data/lib/engine/shaders/mesh_frag.glsl +102 -102
- data/lib/engine/shaders/mesh_vertex.glsl +25 -25
- data/lib/engine/shaders/skybox_frag.glsl +12 -12
- data/lib/engine/shaders/skybox_vert.glsl +14 -14
- data/lib/engine/shaders/sprite_frag.glsl +17 -17
- data/lib/engine/shaders/sprite_vertex.glsl +15 -15
- data/lib/engine/shaders/text_frag.glsl +15 -15
- data/lib/engine/shaders/text_vertex.glsl +31 -31
- data/lib/engine/shaders/ui_sprite_frag.glsl +15 -15
- data/lib/engine/shaders/ui_sprite_vertex.glsl +15 -15
- data/lib/engine/shaders/vertex_lit_frag.glsl +89 -89
- data/lib/engine/shaders/vertex_lit_vertex.glsl +28 -28
- data/lib/engine/tangent_calculator.rb +42 -42
- data/lib/engine/texture.rb +53 -53
- data/lib/engine/video_mode.rb +37 -37
- data/lib/engine/window.rb +126 -126
- data/lib/ruby_rpg.rb +58 -56
- metadata +5 -6
@@ -1,153 +1,153 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Rendering
|
4
|
-
class InstanceRenderer
|
5
|
-
attr_reader :mesh, :material
|
6
|
-
|
7
|
-
def initialize(mesh, material)
|
8
|
-
@mesh = mesh
|
9
|
-
@material = material
|
10
|
-
@mesh_renderers = []
|
11
|
-
@mesh_matrix_data = []
|
12
|
-
|
13
|
-
setup_vertex_attribute_buffer
|
14
|
-
setup_vertex_buffer
|
15
|
-
setup_index_buffer
|
16
|
-
generate_instance_vbo_buf
|
17
|
-
end
|
18
|
-
|
19
|
-
def add_instance(mesh_renderer)
|
20
|
-
@mesh_renderers << mesh_renderer
|
21
|
-
@mesh_matrix_data += mesh_renderer.game_object.model_matrix.to_a.flatten
|
22
|
-
end
|
23
|
-
|
24
|
-
def remove_instance(mesh_renderer)
|
25
|
-
index = @mesh_renderers.index(mesh_renderer)
|
26
|
-
@mesh_renderers.delete_at(index)
|
27
|
-
@mesh_matrix_data[index * 16, 16] = []
|
28
|
-
end
|
29
|
-
|
30
|
-
def update_instance(mesh_renderer)
|
31
|
-
index = @mesh_renderers.index(mesh_renderer)
|
32
|
-
@mesh_matrix_data[index * 16, 16] = mesh_renderer.game_object.model_matrix.to_a.flatten
|
33
|
-
end
|
34
|
-
|
35
|
-
def draw_all
|
36
|
-
set_material_per_frame_data
|
37
|
-
|
38
|
-
GL.BindVertexArray(@vao)
|
39
|
-
update_vbo_buf
|
40
|
-
GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, @ebo)
|
41
|
-
GL.BindBuffer(GL::ARRAY_BUFFER, @instance_vbo)
|
42
|
-
|
43
|
-
GL.DrawElementsInstanced(GL::TRIANGLES, mesh.index_data.length, GL::UNSIGNED_INT, 0, @mesh_renderers.count)
|
44
|
-
GL.BindVertexArray(0)
|
45
|
-
GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, 0)
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def set_material_per_frame_data
|
51
|
-
material.set_mat4("camera", Engine::Camera.instance.matrix)
|
52
|
-
material.set_vec3("cameraPos", Engine::Camera.instance.game_object.pos)
|
53
|
-
|
54
|
-
update_light_data
|
55
|
-
material.update_shader
|
56
|
-
end
|
57
|
-
|
58
|
-
def update_light_data
|
59
|
-
Engine::Components::PointLight.point_lights.each_with_index do |light, i|
|
60
|
-
material.set_float("pointLights[#{i}].sqrRange", light.range * light.range)
|
61
|
-
material.set_vec3("pointLights[#{i}].position", light.game_object.pos)
|
62
|
-
material.set_vec3("pointLights[#{i}].colour", light.colour)
|
63
|
-
end
|
64
|
-
Engine::Components::DirectionLight.direction_lights.each_with_index do |light, i|
|
65
|
-
material.set_vec3("directionalLights[#{i}].direction", light.game_object.forward)
|
66
|
-
material.set_vec3("directionalLights[#{i}].colour", light.colour)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def setup_index_buffer
|
71
|
-
indices = mesh.index_data
|
72
|
-
|
73
|
-
ebo_buf = ' ' * 4
|
74
|
-
GL.GenBuffers(1, ebo_buf)
|
75
|
-
@ebo = ebo_buf.unpack('L')[0]
|
76
|
-
GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, @ebo)
|
77
|
-
GL.BufferData(
|
78
|
-
GL::ELEMENT_ARRAY_BUFFER, indices.length * Fiddle::SIZEOF_INT,
|
79
|
-
indices.pack('I*'), GL::STATIC_DRAW
|
80
|
-
)
|
81
|
-
end
|
82
|
-
|
83
|
-
def setup_vertex_attribute_buffer
|
84
|
-
vao_buf = ' ' * 4
|
85
|
-
GL.GenVertexArrays(1, vao_buf)
|
86
|
-
@vao = vao_buf.unpack('L')[0]
|
87
|
-
GL.BindVertexArray(@vao)
|
88
|
-
end
|
89
|
-
|
90
|
-
def setup_vertex_buffer
|
91
|
-
vbo_buf = ' ' * 4
|
92
|
-
GL.GenBuffers(1, vbo_buf)
|
93
|
-
vbo = vbo_buf.unpack('L')[0]
|
94
|
-
points = mesh.vertex_data
|
95
|
-
|
96
|
-
GL.BindBuffer(GL::ARRAY_BUFFER, vbo)
|
97
|
-
GL.BufferData(
|
98
|
-
GL::ARRAY_BUFFER, @mesh.vertex_data.length * Fiddle::SIZEOF_FLOAT,
|
99
|
-
points.pack('F*'), GL::STATIC_DRAW
|
100
|
-
)
|
101
|
-
vertex_data_size = 20 * Fiddle::SIZEOF_FLOAT
|
102
|
-
|
103
|
-
GL.VertexAttribPointer(0, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 0)
|
104
|
-
GL.VertexAttribPointer(1, 2, GL::FLOAT, GL::FALSE, vertex_data_size, 3 * Fiddle::SIZEOF_FLOAT)
|
105
|
-
GL.VertexAttribPointer(2, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 5 * Fiddle::SIZEOF_FLOAT)
|
106
|
-
GL.VertexAttribPointer(3, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 8 * Fiddle::SIZEOF_FLOAT)
|
107
|
-
GL.VertexAttribPointer(4, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 11 * Fiddle::SIZEOF_FLOAT)
|
108
|
-
GL.VertexAttribPointer(5, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 14 * Fiddle::SIZEOF_FLOAT)
|
109
|
-
GL.VertexAttribPointer(6, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 17 * Fiddle::SIZEOF_FLOAT)
|
110
|
-
GL.EnableVertexAttribArray(0)
|
111
|
-
GL.EnableVertexAttribArray(1)
|
112
|
-
GL.EnableVertexAttribArray(2)
|
113
|
-
GL.EnableVertexAttribArray(3)
|
114
|
-
GL.EnableVertexAttribArray(4)
|
115
|
-
GL.EnableVertexAttribArray(5)
|
116
|
-
GL.EnableVertexAttribArray(6)
|
117
|
-
end
|
118
|
-
|
119
|
-
def generate_instance_vbo_buf
|
120
|
-
instance_vbo_buf = ' ' * 4
|
121
|
-
GL.GenBuffers(1, instance_vbo_buf)
|
122
|
-
@instance_vbo = instance_vbo_buf.unpack('L')[0]
|
123
|
-
update_vbo_buf
|
124
|
-
end
|
125
|
-
|
126
|
-
def update_vbo_buf
|
127
|
-
vertex_data = @mesh_matrix_data
|
128
|
-
|
129
|
-
GL.BindBuffer(GL::ARRAY_BUFFER, @instance_vbo)
|
130
|
-
GL.BufferData(
|
131
|
-
GL::ARRAY_BUFFER, vertex_data.length * Fiddle::SIZEOF_FLOAT,
|
132
|
-
vertex_data.pack('F*'), GL::STATIC_DRAW
|
133
|
-
)
|
134
|
-
|
135
|
-
vec4_size = Fiddle::SIZEOF_FLOAT * 4
|
136
|
-
|
137
|
-
GL.EnableVertexAttribArray(7)
|
138
|
-
GL.EnableVertexAttribArray(8)
|
139
|
-
GL.EnableVertexAttribArray(9)
|
140
|
-
GL.EnableVertexAttribArray(10)
|
141
|
-
|
142
|
-
GL.VertexAttribPointer(7, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 0)
|
143
|
-
GL.VertexAttribPointer(8, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 1 * vec4_size)
|
144
|
-
GL.VertexAttribPointer(9, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 2 * vec4_size)
|
145
|
-
GL.VertexAttribPointer(10, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 3 * vec4_size)
|
146
|
-
|
147
|
-
GL.VertexAttribDivisor(7, 1)
|
148
|
-
GL.VertexAttribDivisor(8, 1)
|
149
|
-
GL.VertexAttribDivisor(9, 1)
|
150
|
-
GL.VertexAttribDivisor(10, 1)
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rendering
|
4
|
+
class InstanceRenderer
|
5
|
+
attr_reader :mesh, :material
|
6
|
+
|
7
|
+
def initialize(mesh, material)
|
8
|
+
@mesh = mesh
|
9
|
+
@material = material
|
10
|
+
@mesh_renderers = []
|
11
|
+
@mesh_matrix_data = []
|
12
|
+
|
13
|
+
setup_vertex_attribute_buffer
|
14
|
+
setup_vertex_buffer
|
15
|
+
setup_index_buffer
|
16
|
+
generate_instance_vbo_buf
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_instance(mesh_renderer)
|
20
|
+
@mesh_renderers << mesh_renderer
|
21
|
+
@mesh_matrix_data += mesh_renderer.game_object.model_matrix.to_a.flatten
|
22
|
+
end
|
23
|
+
|
24
|
+
def remove_instance(mesh_renderer)
|
25
|
+
index = @mesh_renderers.index(mesh_renderer)
|
26
|
+
@mesh_renderers.delete_at(index)
|
27
|
+
@mesh_matrix_data[index * 16, 16] = []
|
28
|
+
end
|
29
|
+
|
30
|
+
def update_instance(mesh_renderer)
|
31
|
+
index = @mesh_renderers.index(mesh_renderer)
|
32
|
+
@mesh_matrix_data[index * 16, 16] = mesh_renderer.game_object.model_matrix.to_a.flatten
|
33
|
+
end
|
34
|
+
|
35
|
+
def draw_all
|
36
|
+
set_material_per_frame_data
|
37
|
+
|
38
|
+
GL.BindVertexArray(@vao)
|
39
|
+
update_vbo_buf
|
40
|
+
GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, @ebo)
|
41
|
+
GL.BindBuffer(GL::ARRAY_BUFFER, @instance_vbo)
|
42
|
+
|
43
|
+
GL.DrawElementsInstanced(GL::TRIANGLES, mesh.index_data.length, GL::UNSIGNED_INT, 0, @mesh_renderers.count)
|
44
|
+
GL.BindVertexArray(0)
|
45
|
+
GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, 0)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def set_material_per_frame_data
|
51
|
+
material.set_mat4("camera", Engine::Camera.instance.matrix)
|
52
|
+
material.set_vec3("cameraPos", Engine::Camera.instance.game_object.pos)
|
53
|
+
|
54
|
+
update_light_data
|
55
|
+
material.update_shader
|
56
|
+
end
|
57
|
+
|
58
|
+
def update_light_data
|
59
|
+
Engine::Components::PointLight.point_lights.each_with_index do |light, i|
|
60
|
+
material.set_float("pointLights[#{i}].sqrRange", light.range * light.range)
|
61
|
+
material.set_vec3("pointLights[#{i}].position", light.game_object.pos)
|
62
|
+
material.set_vec3("pointLights[#{i}].colour", light.colour)
|
63
|
+
end
|
64
|
+
Engine::Components::DirectionLight.direction_lights.each_with_index do |light, i|
|
65
|
+
material.set_vec3("directionalLights[#{i}].direction", light.game_object.forward)
|
66
|
+
material.set_vec3("directionalLights[#{i}].colour", light.colour)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def setup_index_buffer
|
71
|
+
indices = mesh.index_data
|
72
|
+
|
73
|
+
ebo_buf = ' ' * 4
|
74
|
+
GL.GenBuffers(1, ebo_buf)
|
75
|
+
@ebo = ebo_buf.unpack('L')[0]
|
76
|
+
GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, @ebo)
|
77
|
+
GL.BufferData(
|
78
|
+
GL::ELEMENT_ARRAY_BUFFER, indices.length * Fiddle::SIZEOF_INT,
|
79
|
+
indices.pack('I*'), GL::STATIC_DRAW
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
def setup_vertex_attribute_buffer
|
84
|
+
vao_buf = ' ' * 4
|
85
|
+
GL.GenVertexArrays(1, vao_buf)
|
86
|
+
@vao = vao_buf.unpack('L')[0]
|
87
|
+
GL.BindVertexArray(@vao)
|
88
|
+
end
|
89
|
+
|
90
|
+
def setup_vertex_buffer
|
91
|
+
vbo_buf = ' ' * 4
|
92
|
+
GL.GenBuffers(1, vbo_buf)
|
93
|
+
vbo = vbo_buf.unpack('L')[0]
|
94
|
+
points = mesh.vertex_data
|
95
|
+
|
96
|
+
GL.BindBuffer(GL::ARRAY_BUFFER, vbo)
|
97
|
+
GL.BufferData(
|
98
|
+
GL::ARRAY_BUFFER, @mesh.vertex_data.length * Fiddle::SIZEOF_FLOAT,
|
99
|
+
points.pack('F*'), GL::STATIC_DRAW
|
100
|
+
)
|
101
|
+
vertex_data_size = 20 * Fiddle::SIZEOF_FLOAT
|
102
|
+
|
103
|
+
GL.VertexAttribPointer(0, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 0)
|
104
|
+
GL.VertexAttribPointer(1, 2, GL::FLOAT, GL::FALSE, vertex_data_size, 3 * Fiddle::SIZEOF_FLOAT)
|
105
|
+
GL.VertexAttribPointer(2, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 5 * Fiddle::SIZEOF_FLOAT)
|
106
|
+
GL.VertexAttribPointer(3, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 8 * Fiddle::SIZEOF_FLOAT)
|
107
|
+
GL.VertexAttribPointer(4, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 11 * Fiddle::SIZEOF_FLOAT)
|
108
|
+
GL.VertexAttribPointer(5, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 14 * Fiddle::SIZEOF_FLOAT)
|
109
|
+
GL.VertexAttribPointer(6, 3, GL::FLOAT, GL::FALSE, vertex_data_size, 17 * Fiddle::SIZEOF_FLOAT)
|
110
|
+
GL.EnableVertexAttribArray(0)
|
111
|
+
GL.EnableVertexAttribArray(1)
|
112
|
+
GL.EnableVertexAttribArray(2)
|
113
|
+
GL.EnableVertexAttribArray(3)
|
114
|
+
GL.EnableVertexAttribArray(4)
|
115
|
+
GL.EnableVertexAttribArray(5)
|
116
|
+
GL.EnableVertexAttribArray(6)
|
117
|
+
end
|
118
|
+
|
119
|
+
def generate_instance_vbo_buf
|
120
|
+
instance_vbo_buf = ' ' * 4
|
121
|
+
GL.GenBuffers(1, instance_vbo_buf)
|
122
|
+
@instance_vbo = instance_vbo_buf.unpack('L')[0]
|
123
|
+
update_vbo_buf
|
124
|
+
end
|
125
|
+
|
126
|
+
def update_vbo_buf
|
127
|
+
vertex_data = @mesh_matrix_data
|
128
|
+
|
129
|
+
GL.BindBuffer(GL::ARRAY_BUFFER, @instance_vbo)
|
130
|
+
GL.BufferData(
|
131
|
+
GL::ARRAY_BUFFER, vertex_data.length * Fiddle::SIZEOF_FLOAT,
|
132
|
+
vertex_data.pack('F*'), GL::STATIC_DRAW
|
133
|
+
)
|
134
|
+
|
135
|
+
vec4_size = Fiddle::SIZEOF_FLOAT * 4
|
136
|
+
|
137
|
+
GL.EnableVertexAttribArray(7)
|
138
|
+
GL.EnableVertexAttribArray(8)
|
139
|
+
GL.EnableVertexAttribArray(9)
|
140
|
+
GL.EnableVertexAttribArray(10)
|
141
|
+
|
142
|
+
GL.VertexAttribPointer(7, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 0)
|
143
|
+
GL.VertexAttribPointer(8, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 1 * vec4_size)
|
144
|
+
GL.VertexAttribPointer(9, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 2 * vec4_size)
|
145
|
+
GL.VertexAttribPointer(10, 4, GL::FLOAT, GL::FALSE, 4 * vec4_size, 3 * vec4_size)
|
146
|
+
|
147
|
+
GL.VertexAttribDivisor(7, 1)
|
148
|
+
GL.VertexAttribDivisor(8, 1)
|
149
|
+
GL.VertexAttribDivisor(9, 1)
|
150
|
+
GL.VertexAttribDivisor(10, 1)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -1,35 +1,35 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Rendering
|
4
|
-
module RenderPipeline
|
5
|
-
def self.draw
|
6
|
-
Engine::GameObject.mesh_renderers.each do |mesh_renderer|
|
7
|
-
mesh_renderer.update(0)
|
8
|
-
end
|
9
|
-
|
10
|
-
instance_renderers.values.each do |renderer|
|
11
|
-
renderer.draw_all
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.add_instance(mesh_renderer)
|
16
|
-
instance_renderers[[mesh_renderer.mesh, mesh_renderer.material]].add_instance(mesh_renderer)
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.remove_instance(mesh_renderer)
|
20
|
-
instance_renderers[[mesh_renderer.mesh, mesh_renderer.material]].remove_instance(mesh_renderer)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.update_instance(mesh_renderer)
|
24
|
-
instance_renderers[[mesh_renderer.mesh, mesh_renderer.material]].update_instance(mesh_renderer)
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def self.instance_renderers
|
30
|
-
@instance_renderers ||= Hash.new do |hash, key|
|
31
|
-
hash[key] = InstanceRenderer.new(key[0], key[1])
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rendering
|
4
|
+
module RenderPipeline
|
5
|
+
def self.draw
|
6
|
+
Engine::GameObject.mesh_renderers.each do |mesh_renderer|
|
7
|
+
mesh_renderer.update(0)
|
8
|
+
end
|
9
|
+
|
10
|
+
instance_renderers.values.each do |renderer|
|
11
|
+
renderer.draw_all
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.add_instance(mesh_renderer)
|
16
|
+
instance_renderers[[mesh_renderer.mesh, mesh_renderer.material]].add_instance(mesh_renderer)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.remove_instance(mesh_renderer)
|
20
|
+
instance_renderers[[mesh_renderer.mesh, mesh_renderer.material]].remove_instance(mesh_renderer)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.update_instance(mesh_renderer)
|
24
|
+
instance_renderers[[mesh_renderer.mesh, mesh_renderer.material]].update_instance(mesh_renderer)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def self.instance_renderers
|
30
|
+
@instance_renderers ||= Hash.new do |hash, key|
|
31
|
+
hash[key] = InstanceRenderer.new(key[0], key[1])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/engine/screenshoter.rb
CHANGED
@@ -1,34 +1,34 @@
|
|
1
|
-
module Engine
|
2
|
-
class Screenshoter
|
3
|
-
def self.screenshot(&block)
|
4
|
-
@after_screenshot = block
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.scheduled_screenshot
|
8
|
-
@after_screenshot != nil
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.take_screenshot
|
12
|
-
@scheduled_screenshot = nil
|
13
|
-
width = Engine::Window.framebuffer_width
|
14
|
-
height = Engine::Window.framebuffer_height
|
15
|
-
pixels = ' ' * (width * height * 3)
|
16
|
-
GL.ReadPixels(0, 0, width, height, GL::RGB, GL::UNSIGNED_BYTE, pixels)
|
17
|
-
png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
18
|
-
pixels = pixels.bytes.map { |b| b.to_i }
|
19
|
-
x = 0
|
20
|
-
y = 0
|
21
|
-
pixels.each_slice(3) do |r, g, b|
|
22
|
-
png[x, height - y - 1] = ChunkyPNG::Color.rgb(r, g, b)
|
23
|
-
x += 1
|
24
|
-
if x >= width
|
25
|
-
x = 0
|
26
|
-
y += 1
|
27
|
-
end
|
28
|
-
end
|
29
|
-
block = @after_screenshot
|
30
|
-
@after_screenshot = nil
|
31
|
-
block.call(png)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
1
|
+
module Engine
|
2
|
+
class Screenshoter
|
3
|
+
def self.screenshot(&block)
|
4
|
+
@after_screenshot = block
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.scheduled_screenshot
|
8
|
+
@after_screenshot != nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.take_screenshot
|
12
|
+
@scheduled_screenshot = nil
|
13
|
+
width = Engine::Window.framebuffer_width
|
14
|
+
height = Engine::Window.framebuffer_height
|
15
|
+
pixels = ' ' * (width * height * 3)
|
16
|
+
GL.ReadPixels(0, 0, width, height, GL::RGB, GL::UNSIGNED_BYTE, pixels)
|
17
|
+
png = ChunkyPNG::Image.new(width, height, ChunkyPNG::Color::TRANSPARENT)
|
18
|
+
pixels = pixels.bytes.map { |b| b.to_i }
|
19
|
+
x = 0
|
20
|
+
y = 0
|
21
|
+
pixels.each_slice(3) do |r, g, b|
|
22
|
+
png[x, height - y - 1] = ChunkyPNG::Color.rgb(r, g, b)
|
23
|
+
x += 1
|
24
|
+
if x >= width
|
25
|
+
x = 0
|
26
|
+
y += 1
|
27
|
+
end
|
28
|
+
end
|
29
|
+
block = @after_screenshot
|
30
|
+
@after_screenshot = nil
|
31
|
+
block.call(png)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/engine/shader.rb
CHANGED
@@ -1,91 +1,119 @@
|
|
1
|
-
module Engine
|
2
|
-
class Shader
|
3
|
-
def
|
4
|
-
@
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
GL.
|
36
|
-
GL.
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
1
|
+
module Engine
|
2
|
+
class Shader
|
3
|
+
def self.default
|
4
|
+
@default ||= Shader.new('./shaders/mesh_vertex.glsl', './shaders/mesh_frag.glsl')
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.vertex_lit
|
8
|
+
@vertex_lit ||= Engine::Shader.new('./shaders/vertex_lit_vertex.glsl', './shaders/vertex_lit_frag.glsl')
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.skybox
|
12
|
+
@skybox ||= Shader.new('./shaders/skybox_vert.glsl', './shaders/skybox_frag.glsl')
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.sprite
|
16
|
+
@sprite ||= Engine::Shader.new('./shaders/sprite_vertex.glsl', './shaders/sprite_frag.glsl')
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.text
|
20
|
+
@text ||= Engine::Shader.new('./shaders/text_vertex.glsl', './shaders/text_frag.glsl')
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.ui_text
|
24
|
+
@ui_text ||= Engine::Shader.new('./shaders/text_vertex.glsl', './shaders/text_frag.glsl')
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.ui_sprite
|
28
|
+
@ui_sprite ||= Engine::Shader.new('./shaders/ui_sprite_vertex.glsl', './shaders/ui_sprite_frag.glsl')
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(vertex_shader, fragment_shader)
|
32
|
+
@vertex_shader = compile_shader(vertex_shader, GL::VERTEX_SHADER)
|
33
|
+
@fragment_shader = compile_shader(fragment_shader, GL::FRAGMENT_SHADER)
|
34
|
+
@program = GL.CreateProgram
|
35
|
+
GL.AttachShader(@program, @vertex_shader)
|
36
|
+
GL.AttachShader(@program, @fragment_shader)
|
37
|
+
GL.LinkProgram(@program)
|
38
|
+
|
39
|
+
linked_buf = ' ' * 4
|
40
|
+
GL.GetProgramiv(@program, GL::LINK_STATUS, linked_buf)
|
41
|
+
linked = linked_buf.unpack('L')[0]
|
42
|
+
if linked == 0
|
43
|
+
compile_log = ' ' * 1024
|
44
|
+
GL.GetProgramInfoLog(@program, 1023, nil, compile_log)
|
45
|
+
vertex_log = ' ' * 1024
|
46
|
+
GL.GetShaderInfoLog(@vertex_shader, 1023, nil, vertex_log)
|
47
|
+
fragment_log = ' ' * 1024
|
48
|
+
GL.GetShaderInfoLog(@fragment_shader, 1023, nil, fragment_log)
|
49
|
+
puts "Shader program failed to link"
|
50
|
+
puts compile_log.strip
|
51
|
+
puts vertex_log.strip
|
52
|
+
puts fragment_log.strip
|
53
|
+
end
|
54
|
+
@uniform_cache = {}
|
55
|
+
@uniform_locations = {}
|
56
|
+
end
|
57
|
+
|
58
|
+
def compile_shader(shader, type)
|
59
|
+
handle = GL.CreateShader(type)
|
60
|
+
path = File.join(File.dirname(__FILE__), shader)
|
61
|
+
s_srcs = [File.read(path)].pack('p')
|
62
|
+
s_lens = [File.size(path)].pack('I')
|
63
|
+
GL.ShaderSource(handle, 1, s_srcs, s_lens)
|
64
|
+
GL.CompileShader(handle)
|
65
|
+
handle
|
66
|
+
end
|
67
|
+
|
68
|
+
def use
|
69
|
+
GL.UseProgram(@program)
|
70
|
+
end
|
71
|
+
|
72
|
+
def set_vec3(name, vec)
|
73
|
+
return if @uniform_cache[name] == vec
|
74
|
+
@uniform_cache[name] = vec
|
75
|
+
vector = if vec.is_a?(Vector)
|
76
|
+
vec
|
77
|
+
else
|
78
|
+
Vector[vec[:r], vec[:g], vec[:b]]
|
79
|
+
end
|
80
|
+
GL.Uniform3f(uniform_location(name), vector[0], vector[1], vector[2])
|
81
|
+
end
|
82
|
+
|
83
|
+
def set_vec4(name, vec)
|
84
|
+
return if @uniform_cache[name] == vec
|
85
|
+
@uniform_cache[name] = vec
|
86
|
+
GL.Uniform4f(uniform_location(name), vec[0], vec[1], vec[2], vec[3])
|
87
|
+
end
|
88
|
+
|
89
|
+
def set_mat4(name, mat)
|
90
|
+
return if @uniform_cache[name] == mat
|
91
|
+
@uniform_cache[name] = mat
|
92
|
+
mat_array = [
|
93
|
+
mat[0, 0], mat[0, 1], mat[0, 2], mat[0, 3],
|
94
|
+
mat[1, 0], mat[1, 1], mat[1, 2], mat[1, 3],
|
95
|
+
mat[2, 0], mat[2, 1], mat[2, 2], mat[2, 3],
|
96
|
+
mat[3, 0], mat[3, 1], mat[3, 2], mat[3, 3]
|
97
|
+
]
|
98
|
+
GL.UniformMatrix4fv(uniform_location(name), 1, GL::FALSE, mat_array.pack('F*'))
|
99
|
+
end
|
100
|
+
|
101
|
+
def set_int(name, int)
|
102
|
+
return if @uniform_cache[name] == int
|
103
|
+
@uniform_cache[name] = int
|
104
|
+
GL.Uniform1i(uniform_location(name), int)
|
105
|
+
end
|
106
|
+
|
107
|
+
def set_float(name, float)
|
108
|
+
return if @uniform_cache[name] == float
|
109
|
+
@uniform_cache[name] = float
|
110
|
+
GL.Uniform1f(uniform_location(name), float)
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def uniform_location(name)
|
116
|
+
@uniform_locations[name] ||= GL.GetUniformLocation(@program, name)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|