mittsu 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build-workflow.yml +3 -38
  3. data/Gemfile +0 -4
  4. data/README.md +16 -130
  5. data/bin/console +0 -4
  6. data/lib/mittsu/cameras/cube_camera.rb +1 -1
  7. data/lib/mittsu/core/uniform.rb +23 -0
  8. data/lib/mittsu/core.rb +1 -0
  9. data/lib/mittsu/loaders/stl_loader.rb +186 -0
  10. data/lib/mittsu/loaders.rb +1 -0
  11. data/lib/mittsu/textures/render_target.rb +93 -0
  12. data/lib/mittsu/textures/render_target_cube.rb +8 -0
  13. data/lib/mittsu/textures/texture.rb +1 -1
  14. data/lib/mittsu/textures.rb +2 -0
  15. data/lib/mittsu/version.rb +1 -1
  16. data/lib/mittsu.rb +0 -1
  17. data/mittsu.gemspec +8 -12
  18. metadata +28 -199
  19. data/install-glfw.ps1 +0 -13
  20. data/lib/mittsu/renderers/generic_lib.rb +0 -125
  21. data/lib/mittsu/renderers/glfw_lib.rb +0 -64
  22. data/lib/mittsu/renderers/glfw_window.rb +0 -233
  23. data/lib/mittsu/renderers/opengl/core/buffer_geometry.rb +0 -11
  24. data/lib/mittsu/renderers/opengl/core/geometry.rb +0 -346
  25. data/lib/mittsu/renderers/opengl/core/object_3d.rb +0 -134
  26. data/lib/mittsu/renderers/opengl/lights/ambient_light.rb +0 -26
  27. data/lib/mittsu/renderers/opengl/lights/directional_light.rb +0 -35
  28. data/lib/mittsu/renderers/opengl/lights/hemisphere_light.rb +0 -39
  29. data/lib/mittsu/renderers/opengl/lights/light.rb +0 -55
  30. data/lib/mittsu/renderers/opengl/lights/point_light.rb +0 -36
  31. data/lib/mittsu/renderers/opengl/lights/spot_light.rb +0 -47
  32. data/lib/mittsu/renderers/opengl/materials/line_basic_material.rb +0 -16
  33. data/lib/mittsu/renderers/opengl/materials/material.rb +0 -274
  34. data/lib/mittsu/renderers/opengl/materials/mesh_basic_material.rb +0 -21
  35. data/lib/mittsu/renderers/opengl/materials/mesh_lambert_material.rb +0 -33
  36. data/lib/mittsu/renderers/opengl/materials/mesh_phong_material.rb +0 -44
  37. data/lib/mittsu/renderers/opengl/materials/opengl_material_basics.rb +0 -57
  38. data/lib/mittsu/renderers/opengl/materials/point_cloud_material.rb +0 -27
  39. data/lib/mittsu/renderers/opengl/materials/shader_material.rb +0 -11
  40. data/lib/mittsu/renderers/opengl/objects/group.rb +0 -9
  41. data/lib/mittsu/renderers/opengl/objects/line.rb +0 -45
  42. data/lib/mittsu/renderers/opengl/objects/mesh.rb +0 -70
  43. data/lib/mittsu/renderers/opengl/objects/point_cloud.rb +0 -39
  44. data/lib/mittsu/renderers/opengl/objects/sprite.rb +0 -12
  45. data/lib/mittsu/renderers/opengl/opengl_buffer.rb +0 -13
  46. data/lib/mittsu/renderers/opengl/opengl_debug.rb +0 -81
  47. data/lib/mittsu/renderers/opengl/opengl_default_target.rb +0 -50
  48. data/lib/mittsu/renderers/opengl/opengl_geometry_group.rb +0 -758
  49. data/lib/mittsu/renderers/opengl/opengl_geometry_like.rb +0 -132
  50. data/lib/mittsu/renderers/opengl/opengl_helper.rb +0 -161
  51. data/lib/mittsu/renderers/opengl/opengl_implementations.rb +0 -37
  52. data/lib/mittsu/renderers/opengl/opengl_lib.rb +0 -19
  53. data/lib/mittsu/renderers/opengl/opengl_light_renderer.rb +0 -43
  54. data/lib/mittsu/renderers/opengl/opengl_mittsu_params.rb +0 -53
  55. data/lib/mittsu/renderers/opengl/opengl_program.rb +0 -250
  56. data/lib/mittsu/renderers/opengl/opengl_shader.rb +0 -58
  57. data/lib/mittsu/renderers/opengl/opengl_state.rb +0 -205
  58. data/lib/mittsu/renderers/opengl/plugins/shadow_map_plugin.rb +0 -417
  59. data/lib/mittsu/renderers/opengl/plugins/sprite_fragment.glsl +0 -38
  60. data/lib/mittsu/renderers/opengl/plugins/sprite_plugin.rb +0 -250
  61. data/lib/mittsu/renderers/opengl/plugins/sprite_vertex.glsl +0 -31
  62. data/lib/mittsu/renderers/opengl/scenes/scene.rb +0 -9
  63. data/lib/mittsu/renderers/opengl/textures/compressed_texture.rb +0 -20
  64. data/lib/mittsu/renderers/opengl/textures/cube_texture.rb +0 -77
  65. data/lib/mittsu/renderers/opengl/textures/data_texture.rb +0 -21
  66. data/lib/mittsu/renderers/opengl/textures/texture.rb +0 -107
  67. data/lib/mittsu/renderers/opengl_render_target.rb +0 -201
  68. data/lib/mittsu/renderers/opengl_renderer.rb +0 -1028
  69. data/lib/mittsu/renderers/shaders/rbsl_loader.rb +0 -166
  70. data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_fragment.glsl +0 -5
  71. data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_pars_fragment.glsl +0 -5
  72. data/lib/mittsu/renderers/shaders/shader_chunk/alphatest_fragment.glsl +0 -5
  73. data/lib/mittsu/renderers/shaders/shader_chunk/bumpmap_pars_fragment.glsl +0 -40
  74. data/lib/mittsu/renderers/shaders/shader_chunk/color_fragment.glsl +0 -5
  75. data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_fragment.glsl +0 -5
  76. data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_vertex.glsl +0 -5
  77. data/lib/mittsu/renderers/shaders/shader_chunk/color_vertex.glsl +0 -5
  78. data/lib/mittsu/renderers/shaders/shader_chunk/common.glsl +0 -60
  79. data/lib/mittsu/renderers/shaders/shader_chunk/default_vertex.glsl +0 -15
  80. data/lib/mittsu/renderers/shaders/shader_chunk/defaultnormal_vertex.glsl +0 -21
  81. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_fragment.glsl +0 -62
  82. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_fragment.glsl +0 -21
  83. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_vertex.glsl +0 -7
  84. data/lib/mittsu/renderers/shaders/shader_chunk/envmap_vertex.glsl +0 -17
  85. data/lib/mittsu/renderers/shaders/shader_chunk/fog_fragment.glsl +0 -26
  86. data/lib/mittsu/renderers/shaders/shader_chunk/fog_pars_fragment.glsl +0 -15
  87. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_fragment.glsl +0 -5
  88. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_fragment.glsl +0 -6
  89. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_vertex.glsl +0 -5
  90. data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_vertex.glsl +0 -5
  91. data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_pars_vertex.glsl +0 -43
  92. data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_vertex.glsl +0 -196
  93. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_fragment.glsl +0 -243
  94. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_fragment.glsl +0 -58
  95. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_vertex.glsl +0 -5
  96. data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_vertex.glsl +0 -5
  97. data/lib/mittsu/renderers/shaders/shader_chunk/linear_to_gamma_fragment.glsl +0 -2
  98. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_fragment.glsl +0 -5
  99. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_fragment.glsl +0 -12
  100. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_vertex.glsl +0 -11
  101. data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_vertex.glsl +0 -15
  102. data/lib/mittsu/renderers/shaders/shader_chunk/map_fragment.glsl +0 -9
  103. data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_fragment.glsl +0 -11
  104. data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_vertex.glsl +0 -6
  105. data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_fragment.glsl +0 -5
  106. data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_pars_fragment.glsl +0 -6
  107. data/lib/mittsu/renderers/shaders/shader_chunk/map_vertex.glsl +0 -5
  108. data/lib/mittsu/renderers/shaders/shader_chunk/morphnormal_vertex.glsl +0 -12
  109. data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_pars_vertex.glsl +0 -13
  110. data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_vertex.glsl +0 -20
  111. data/lib/mittsu/renderers/shaders/shader_chunk/normalmap_pars_fragment.glsl +0 -27
  112. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_fragment.glsl +0 -216
  113. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_fragment.glsl +0 -19
  114. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_vertex.glsl +0 -6
  115. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_vertex.glsl +0 -9
  116. data/lib/mittsu/renderers/shaders/shader_chunk/skinbase_vertex.glsl +0 -8
  117. data/lib/mittsu/renderers/shaders/shader_chunk/skinning_pars_vertex.glsl +0 -47
  118. data/lib/mittsu/renderers/shaders/shader_chunk/skinning_vertex.glsl +0 -20
  119. data/lib/mittsu/renderers/shaders/shader_chunk/skinnormal_vertex.glsl +0 -20
  120. data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_fragment.glsl +0 -12
  121. data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_pars_fragment.glsl +0 -5
  122. data/lib/mittsu/renderers/shaders/shader_chunk/worldpos_vertex.glsl +0 -17
  123. data/lib/mittsu/renderers/shaders/shader_chunk.rb +0 -9
  124. data/lib/mittsu/renderers/shaders/shader_lib/basic/basic_fragment.rbsl +0 -37
  125. data/lib/mittsu/renderers/shaders/shader_lib/basic/basic_uniforms.rbslu +0 -3
  126. data/lib/mittsu/renderers/shaders/shader_lib/basic/basic_vertex.rbsl +0 -33
  127. data/lib/mittsu/renderers/shaders/shader_lib/cube/cube_fragment.rbsl +0 -12
  128. data/lib/mittsu/renderers/shaders/shader_lib/cube/cube_uniforms.rbslu +0 -2
  129. data/lib/mittsu/renderers/shaders/shader_lib/cube/cube_vertex.rbsl +0 -12
  130. data/lib/mittsu/renderers/shaders/shader_lib/depth_rgba/depth_rgba_fragment.rbsl +0 -26
  131. data/lib/mittsu/renderers/shaders/shader_lib/depth_rgba/depth_rgba_uniforms.rbslu +0 -0
  132. data/lib/mittsu/renderers/shaders/shader_lib/depth_rgba/depth_rgba_vertex.rbsl +0 -12
  133. data/lib/mittsu/renderers/shaders/shader_lib/lambert/lambert_fragment.rbsl +0 -56
  134. data/lib/mittsu/renderers/shaders/shader_lib/lambert/lambert_uniforms.rbslu +0 -7
  135. data/lib/mittsu/renderers/shaders/shader_lib/lambert/lambert_vertex.rbsl +0 -37
  136. data/lib/mittsu/renderers/shaders/shader_lib/particle_basic/particle_basic_fragment.rbsl +0 -27
  137. data/lib/mittsu/renderers/shaders/shader_lib/particle_basic/particle_basic_uniforms.rbslu +0 -2
  138. data/lib/mittsu/renderers/shaders/shader_lib/particle_basic/particle_basic_vertex.rbsl +0 -25
  139. data/lib/mittsu/renderers/shaders/shader_lib/phong/phong_fragment.rbsl +0 -45
  140. data/lib/mittsu/renderers/shaders/shader_lib/phong/phong_uniforms.rbslu +0 -11
  141. data/lib/mittsu/renderers/shaders/shader_lib/phong/phong_vertex.rbsl +0 -43
  142. data/lib/mittsu/renderers/shaders/shader_lib.rb +0 -43
  143. data/lib/mittsu/renderers/shaders/shader_templates/fragment.glsl.erb +0 -105
  144. data/lib/mittsu/renderers/shaders/shader_templates/vertex.glsl.erb +0 -143
  145. data/lib/mittsu/renderers/shaders/uniforms_lib.rb +0 -106
  146. data/lib/mittsu/renderers/shaders/uniforms_utils.rb +0 -31
  147. data/lib/mittsu/renderers.rb +0 -1
@@ -1,233 +0,0 @@
1
- require 'opengl'
2
- require 'glfw'
3
-
4
- require 'mittsu/utils'
5
- require 'mittsu/renderers/glfw_lib'
6
- glfw_lib = Mittsu::GLFWLib.discover
7
- GLFW.load_lib(ENV["MITTSU_LIBGLFW_FILE"] || glfw_lib.file, ENV["MITTSU_LIBGLFW_PATH"] || glfw_lib.path) unless Mittsu.test?
8
-
9
- include GLFW
10
-
11
- module Mittsu
12
- module GLFW
13
- class Window
14
- attr_accessor :key_press_handler, :key_release_handler, :key_repeat_handler, :char_input_handler, :cursor_pos_handler, :mouse_button_press_handler, :mouse_button_release_handler, :scroll_handler, :framebuffer_size_handler
15
-
16
- def initialize(width, height, title, antialias: 0)
17
- glfwInit
18
-
19
- glfwWindowHint GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE
20
- glfwWindowHint GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE
21
- glfwWindowHint GLFW_CONTEXT_VERSION_MAJOR, 3
22
- glfwWindowHint GLFW_CONTEXT_VERSION_MINOR, 3
23
- glfwWindowHint GLFW_CONTEXT_REVISION, 0
24
-
25
- if antialias > 0
26
- glfwWindowHint GLFW_SAMPLES, antialias
27
- end
28
-
29
- @width, @height, @title = width, height, title
30
- @handle = glfwCreateWindow(@width, @height, @title, nil, nil)
31
- if @handle.null?
32
- raise "Unable to create window."
33
- end
34
- glfwMakeContextCurrent @handle
35
- glfwSwapInterval 1
36
-
37
- this = self
38
- @key_callback = ::GLFW::create_callback(:GLFWkeyfun) do |window_handle, key, scancode, action, mods|
39
- if action == GLFW_PRESS
40
- this.key_press_handler.call(key) unless this.key_press_handler.nil?
41
- this.key_repeat_handler.call(key) unless this.key_repeat_handler.nil?
42
- elsif action == GLFW_RELEASE
43
- this.key_release_handler.call(key) unless this.key_release_handler.nil?
44
- elsif action == GLFW_REPEAT
45
- this.key_repeat_handler.call(key) unless this.key_repeat_handler.nil?
46
- end
47
- end
48
- glfwSetKeyCallback(@handle, @key_callback)
49
-
50
- @char_callback = ::GLFW::create_callback(:GLFWcharfun) do |window_handle, codepoint|
51
- char = [codepoint].pack('U')
52
- this.char_input_handler.call(char) unless this.char_input_handler.nil?
53
- end
54
- glfwSetCharCallback(@handle, @char_callback)
55
-
56
- @cursor_pos_callback = ::GLFW::create_callback(:GLFWcursorposfun) do |window_handle, xpos, ypos|
57
- this.cursor_pos_handler.call(Vector2.new(xpos, ypos)) unless this.cursor_pos_handler.nil?
58
- end
59
- glfwSetCursorPosCallback(@handle, @cursor_pos_callback)
60
-
61
- @mouse_button_callback = ::GLFW::create_callback(:GLFWmousebuttonfun) do |window_handle, button, action, mods|
62
- mpos = this.mouse_position
63
- if action == GLFW_PRESS
64
- this.mouse_button_press_handler.call(button, mpos) unless this.mouse_button_press_handler.nil?
65
- elsif action == GLFW_RELEASE
66
- this.mouse_button_release_handler.call(button, mpos) unless this.mouse_button_release_handler.nil?
67
- end
68
- end
69
- glfwSetMouseButtonCallback(@handle, @mouse_button_callback)
70
-
71
- @scroll_callback = ::GLFW::create_callback(:GLFWscrollfun) do |window_handle, xoffset, yoffset|
72
- this.scroll_handler.call(Vector2.new(xoffset, yoffset)) unless this.scroll_handler.nil?
73
- end
74
- glfwSetScrollCallback(@handle, @scroll_callback)
75
-
76
- @frabuffer_size_callback = ::GLFW::create_callback(:GLFWframebuffersizefun) do |window_handle, new_width, new_height|
77
- this.framebuffer_size_handler.call(new_width, new_height) unless this.framebuffer_size_handler.nil?
78
- end
79
- glfwSetFramebufferSizeCallback(@handle, @frabuffer_size_callback)
80
-
81
- @joystick_buttons = poll_all_joysticks_buttons
82
- end
83
-
84
- def run
85
- while glfwWindowShouldClose(@handle) == 0
86
- yield
87
-
88
- glfwSwapBuffers @handle
89
- glfwPollEvents
90
- poll_joystick_events
91
- end
92
- glfwDestroyWindow @handle
93
- glfwTerminate
94
- end
95
-
96
- def framebuffer_size
97
- width, height = ' '*8, ' '*8
98
- glfwGetFramebufferSize(@handle, width, height)
99
- [width.unpack('L')[0], height.unpack('L')[0]]
100
- end
101
-
102
- def on_key_pressed &block
103
- @key_press_handler = block
104
- end
105
-
106
- def on_key_released &block
107
- @key_release_handler = block
108
- end
109
-
110
- def on_key_typed &block
111
- @key_repeat_handler = block
112
- end
113
-
114
- def key_down?(key)
115
- glfwGetKey(@handle, key) == GLFW_PRESS
116
- end
117
-
118
- def on_character_input &block
119
- @char_input_handler = block
120
- end
121
-
122
- def on_mouse_move &block
123
- @cursor_pos_handler = block
124
- end
125
-
126
- def on_mouse_button_pressed &block
127
- @mouse_button_press_handler = block
128
- end
129
-
130
- def on_mouse_button_released &block
131
- @mouse_button_release_handler = block
132
- end
133
-
134
- def mouse_position
135
- xpos, ypos = ' '*8, ' '*8
136
- glfwGetCursorPos(@handle, xpos, ypos);
137
- Vector2.new(xpos.unpack('D')[0], ypos.unpack('D')[0])
138
- end
139
-
140
- def mouse_button_down?(button)
141
- glfwGetMouseButton(@handle, button) == GLFW_PRESS
142
- end
143
-
144
- def on_scroll &block
145
- @scroll_handler = block
146
- end
147
-
148
- def on_resize &block
149
- @framebuffer_size_handler = block
150
- end
151
-
152
- def joystick_buttons(joystick = GLFW_JOYSTICK_1)
153
- @joystick_buttons = poll_all_joysticks_buttons
154
- @joystick_buttons[joystick]
155
- end
156
-
157
- def joystick_axes(joystick = GLFW_JOYSTICK_1)
158
- return [] unless joystick_present?(joystick)
159
- count = ' ' * 4
160
- array = glfwGetJoystickAxes(joystick, count)
161
- count = count.unpack('l')[0]
162
- array[0, count * 4].unpack('f' * count)
163
- end
164
-
165
- def on_joystick_button_pressed &block
166
- @joystick_button_press_handler = block
167
- end
168
-
169
- def on_joystick_button_released &block
170
- @joystick_button_release_handler = block
171
- end
172
-
173
- def joystick_present?(joystick = GLFW_JOYSTICK_1)
174
- glfwJoystickPresent(joystick).nonzero?
175
- end
176
-
177
- def joystick_button_down?(button, joystick = GLFW_JOYSTICK_1)
178
- @joystick_buttons[joystick][button]
179
- end
180
-
181
- def joystick_name(joystick = GLFW_JOYSTICK_1)
182
- glfwGetJoystickName(joystick)
183
- end
184
-
185
- def set_mouselock(value)
186
- if value
187
- glfwSetInputMode(@handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED)
188
- else
189
- glfwSetInputMode(@handle, GLFW_CURSOR, GLFW_CURSOR_NORMAL)
190
- end
191
- end
192
-
193
- private
194
-
195
- def poll_all_joysticks_buttons
196
- (GLFW_JOYSTICK_1..GLFW_JOYSTICK_LAST).map do |joystick|
197
- poll_joystick_buttons(joystick)
198
- end
199
- end
200
-
201
- def poll_joystick_buttons(joystick)
202
- return nil unless joystick_present?(joystick)
203
- count = ' ' * 4
204
- array = glfwGetJoystickButtons(joystick, count)
205
- count = count.unpack('l')[0]
206
- array[0, count].unpack('c' * count).map(&:nonzero?)
207
- end
208
-
209
- def poll_joystick_events
210
- new_joystick_buttons = poll_all_joysticks_buttons
211
- new_joystick_buttons.each_with_index do |buttons, joystick|
212
- poll_single_joystick_events(joystick, buttons)
213
- end
214
- @joystick_buttons = new_joystick_buttons
215
- end
216
-
217
- def poll_single_joystick_events(joystick, buttons)
218
- return if buttons.nil?
219
- buttons.each_with_index do |pressed, button|
220
- fire_joystick_button_event(joystick, button, pressed)
221
- end
222
- end
223
-
224
- def fire_joystick_button_event(joystick, button, pressed)
225
- if !@joystick_buttons[joystick][button] && pressed
226
- @joystick_button_press_handler.call(joystick, button) unless @joystick_button_press_handler.nil?
227
- elsif @joystick_buttons[joystick][button] && !pressed
228
- @joystick_button_release_handler.call(joystick, button) unless @joystick_button_release_handler.nil?
229
- end
230
- end
231
- end
232
- end
233
- end
@@ -1,11 +0,0 @@
1
- module Mittsu
2
- class BufferGeometry
3
- include OpenGLGeometryLike
4
-
5
- attr_accessor :initted
6
-
7
- def init
8
- @initted = true
9
- end
10
- end
11
- end
@@ -1,346 +0,0 @@
1
- require 'mittsu/renderers/opengl/opengl_geometry_like'
2
-
3
- module Mittsu
4
- class Geometry
5
- include OpenGLGeometryLike
6
-
7
- attr_accessor :groups, :initted
8
-
9
- def init_geometry_groups(object)
10
- material = object.material
11
- add_buffers = false
12
-
13
- if @groups.nil? || @groups_need_update
14
- @renderer.remove_opengl_object(object)
15
-
16
- @groups = make_groups(material.is_a?(MeshFaceMaterial))
17
-
18
- @groups_need_update = false
19
- end
20
-
21
- # create separate VBOs per geometry chunk
22
-
23
- @groups.each do |geometry_group|
24
- # initialize VBO on the first access
25
- if geometry_group.vertex_buffer.nil?
26
- geometry_group.create_mesh_buffers
27
- geometry_group.init_mesh_buffers(object)
28
-
29
- @vertices_need_update = true
30
- @morph_targets_need_update = true
31
- @elements_need_update = true
32
- @uvs_need_update = true
33
- @normals_need_update = true
34
- @tangents_need_update = true
35
- @colors_need_update = true
36
-
37
- add_buffers = true
38
- else
39
- add_buffers = false
40
- end
41
-
42
- if add_buffers || !object.active?
43
- @renderer.add_opengl_object(geometry_group, object)
44
- end
45
- end
46
-
47
- object.active = true
48
- end
49
-
50
- def init_line_buffers(object)
51
- nvertices = @vertices.length
52
-
53
- @vertex_array = Array.new(nvertices * 3, 0.0) # Float32Array
54
- @color_array = Array.new(nvertices * 3, 0.0) # Float32Array
55
- @line_distance_array = Array.new(nvertices, 0.0) # Float32Array
56
-
57
- @line_count = nvertices
58
-
59
- init_custom_attributes(object)
60
- end
61
-
62
- def init_particle_buffers(object)
63
- nvertices = @vertices.length
64
-
65
- @vertex_array = Array.new(nvertices * 3, 0.0) # Float32Array
66
- @color_array = Array.new(nvertices * 3, 0.0) # Float32Array
67
-
68
- @particle_count = nvertices
69
-
70
- init_custom_attributes(object)
71
- end
72
-
73
- def create_line_buffers
74
- @vertex_array_object = glCreateVertexArray
75
-
76
- @vertex_buffer = glCreateBuffer
77
- @color_buffer = glCreateBuffer
78
- @line_distance_buffer = glCreateBuffer
79
-
80
- @renderer.info[:memory][:geometries] += 1
81
- end
82
-
83
- def create_particle_buffers
84
- @vertex_array_object = glCreateVertexArray
85
-
86
- @vertex_buffer = glCreateBuffer
87
- @color_buffer = glCreateBuffer
88
-
89
- @renderer.info[:memory][:geometries] += 1
90
- end
91
-
92
- def set_line_buffers(hint)
93
- if @vertices_need_update
94
- @vertices.each_with_index do |vertex, v|
95
- offset = v * 3
96
-
97
- @vertex_array[offset] = vertex.x
98
- @vertex_array[offset + 1] = vertex.y
99
- @vertex_array[offset + 2] = vertex.z
100
- end
101
-
102
- glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
103
- glBufferData_easy(GL_ARRAY_BUFFER, @vertex_array, hint)
104
- end
105
-
106
- if @colors_need_update
107
- @colors.each_with_index do |color, c|
108
- offset = c * 3
109
-
110
- @color_array[offset] = color.r
111
- @color_array[offset + 1] = color.g
112
- @color_array[offset + 2] = color.b
113
- end
114
-
115
- glBindBuffer(GL_ARRAY_BUFFER, @color_buffer)
116
- glBufferData_easy(GL_ARRAY_BUFFER, @color_array, hint)
117
- end
118
-
119
- if @line_distances_need_update
120
- @line_distances.each_with_index do |l, d|
121
- @line_distance_array[d] = l
122
- end
123
-
124
- glBindBuffer(GL_ARRAY_BUFFER, @line_distance_buffer)
125
- glBufferData_easy(GL_ARRAY_BUFFER, @line_distance_array, hint)
126
- end
127
-
128
- if @custom_attributes
129
- @custom_attributes.each do |custom_attribute|
130
- offset = 0
131
-
132
- values = custom_attribute.value
133
-
134
- case custom_attribute.size
135
- when 1
136
- value.each_with_index do |value, ca|
137
- custom_attribute.array[ca] = value
138
- end
139
- when 2
140
- values.each do |value|
141
- custom_attribute.array[offset ] = value.x
142
- custom_attribute.array[offset + 1] = value.y
143
-
144
- offset += 2
145
- end
146
- when 3
147
- if custom_attribute.type === :c
148
- values.each do |value|
149
- custom_attribute.array[offset ] = value.r
150
- custom_attribute.array[offset + 1] = value.g
151
- custom_attribute.array[offset + 2] = value.b
152
-
153
- offset += 3
154
- end
155
- else
156
- values.each do |value|
157
- custom_attribute.array[offset ] = value.x
158
- custom_attribute.array[offset + 1] = value.y
159
- custom_attribute.array[offset + 2] = value.z
160
-
161
- offset += 3
162
- end
163
- end
164
- when 4
165
- values.each do |value|
166
- custom_attribute.array[offset ] = value.x
167
- custom_attribute.array[offset + 1] = value.y
168
- custom_attribute.array[offset + 2] = value.z
169
- custom_attribute.array[offset + 3] = value.w
170
-
171
- offset += 4
172
- end
173
- end
174
-
175
- glBindBuffer(GL_ARRAY_BUFFER, custom_attribute.buffer)
176
- glBufferData_easy(GL_ARRAY_BUFFER, custom_attribute.array, hint)
177
-
178
- custom_attribute.needs_update = false
179
- end
180
- end
181
- end
182
-
183
- def set_particle_buffers(hint)
184
- if @vertices_need_update
185
- @vertices.each_with_index do |vertex, v|
186
- offset = v * 3
187
-
188
- @vertex_array[offset] = vertex.x
189
- @vertex_array[offset + 1] = vertex.y
190
- @vertex_array[offset + 2] = vertex.z
191
- end
192
-
193
-
194
- glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
195
- glBufferData_easy(GL_ARRAY_BUFFER, @vertex_array, hint)
196
- end
197
-
198
- if @colors_need_update
199
- @colors.each_with_index do |color, c|
200
- offset = c * 3;
201
-
202
- @color_array[offset] = color.r
203
- @color_array[offset + 1] = color.g
204
- @color_array[offset + 2] = color.b
205
- end
206
-
207
- glBindBuffer(GL_ARRAY_BUFFER, @color_buffer)
208
- glBufferData_easy(GL_ARRAY_BUFFER, @color_array, hint)
209
- end
210
-
211
- if @custom_attribute
212
- @custom_attributes.each do |custom_attribute|
213
- if custom_attribute.needs_update? && (custom_attribute.bount_to.nil? || custom_attribute.bount_to == 'vertices')
214
- offset = 0
215
-
216
- if custom_attribute.size == 1
217
- custom_attribute.value.each_with_index do |value, ca|
218
- custom_attribute.array[ca] = value
219
- end
220
- elsif custom_attribute.size == 2
221
- custom_attribute.value.each do |value|
222
- custom_attribute.array[offset] = value.x
223
- custom_attribute.array[offset + 1] = value.y
224
-
225
- offset += 2
226
- end
227
- elsif custom_attribute.size == 3
228
- if custom_attribute.type == :c
229
- custom_attribute.value.each do |value|
230
- custom_attribute.array[offset] = value.r
231
- custom_attribute.array[offset + 1] = value.g
232
- custom_attribute.array[offset + 2] = value.b
233
-
234
- offset += 3
235
- end
236
- else
237
- custom_attribute.value.each do |value|
238
- custom_attribute.array[offset] = value.x
239
- custom_attribute.array[offset + 1] = value.y
240
- custom_attribute.array[offset + 2] = value.z
241
-
242
- offset += 3
243
- end
244
- end
245
- elsif custom_attribute.size == 4
246
- custom_attribute.value.each do |value|
247
- custom_attribute.array[offset] = value.x
248
- custom_attribute.array[offset + 1] = value.y
249
- custom_attribute.array[offset + 2] = value.z
250
- custom_attribute.array[offset + 3] = value.w
251
-
252
- offset += 3
253
- end
254
- end
255
- end
256
-
257
- glBindBuffer(GL_ARRAY_BUFFER, customAttribute.buffer)
258
- glBufferData(GL_ARRAY_BUFFER, customAttribute.array, hint)
259
-
260
- custom_attribute.needs_update = false
261
- end
262
- end
263
- end
264
-
265
- private
266
-
267
- def make_groups(uses_face_material = false)
268
- max_vertices_in_group = 65535 # TODO: OES_element_index_uint ???
269
-
270
- hash_map = {}
271
-
272
- num_morph_targets = @morph_targets.length
273
- num_morph_normals = @morph_normals.length
274
-
275
- groups = {}
276
- groups_list = []
277
-
278
- @faces.each_with_index do |face, f|
279
- material_index = uses_face_material ? face.material_index : 0
280
-
281
- if !hash_map.include? material_index
282
- hash_map[material_index] = { hash: material_index, counter: 0 }
283
- end
284
-
285
- group_hash = "#{hash_map[material_index][:hash]}_#{hash_map[material_index][:counter]}"
286
-
287
- if !groups.include? group_hash
288
- group = OpenGLGeometryGroup.new(material_index, num_morph_targets, num_morph_normals, @renderer)
289
-
290
- groups[group_hash] = group
291
- groups_list << group
292
- end
293
-
294
- if groups[group_hash].num_vertices + 3 > max_vertices_in_group
295
- hash_map[material_index][:counter] += 1
296
- group_hash = "#{hash_map[material_index][:hash]}_#{hash_map[material_index][:counter]}"
297
-
298
- if !groups.include? group_hash
299
- group = OpenGLGeometryGroup.new(material_index, num_morph_targets, num_morph_normals, @renderer)
300
-
301
- groups[group_hash] = group
302
- groups_list << group
303
- end
304
- end
305
- groups[group_hash].faces3 << f
306
- groups[group_hash].num_vertices += 3
307
- end
308
- groups_list
309
- end
310
-
311
- def init_custom_attributes(object)
312
- material = object.material
313
-
314
- nvertices = @vertices.length
315
-
316
- if material.attributes
317
- @custom_attributes_list ||= []
318
-
319
- material.attributes.each do |(name, attribute)|
320
- if !attribute[:_opengl_initialized] || attribute.create_unique_buffers
321
- attribute[:_opengl_initialized] = true
322
-
323
- size = case attribute.type
324
- when :v2 then 2
325
- when :v3 then 3
326
- when :v4 then 4
327
- when :c then 3
328
- else 1
329
- end
330
-
331
- attribute.size = size
332
-
333
- attribute.array = Array.new(nvertices * size) # Float32Array
334
-
335
- attribute.buffer = glCreateBuffer
336
- attribute.buffer.belongs_to_attribute = name
337
-
338
- attribute.needs_update = true
339
- end
340
-
341
- @custom_attributes_list << attribute
342
- end
343
- end
344
- end
345
- end
346
- end