mittsu-opengl 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +18 -0
  3. data/.github/workflows/build-workflow.yml +67 -0
  4. data/.gitignore +12 -0
  5. data/.rubocop.yml +1158 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +195 -0
  10. data/Rakefile +8 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +7 -0
  13. data/install-glfw.ps1 +13 -0
  14. data/lib/mittsu/generic_lib.rb +116 -0
  15. data/lib/mittsu/glfw/lib.rb +58 -0
  16. data/lib/mittsu/glfw/window.rb +231 -0
  17. data/lib/mittsu/opengl/buffer.rb +13 -0
  18. data/lib/mittsu/opengl/default_target.rb +50 -0
  19. data/lib/mittsu/opengl/geometry_group.rb +758 -0
  20. data/lib/mittsu/opengl/geometry_like.rb +132 -0
  21. data/lib/mittsu/opengl/gl_debug.rb +85 -0
  22. data/lib/mittsu/opengl/gl_extensions.rb +42 -0
  23. data/lib/mittsu/opengl/gl_mittsu_params.rb +53 -0
  24. data/lib/mittsu/opengl/helper.rb +120 -0
  25. data/lib/mittsu/opengl/implementation.rb +31 -0
  26. data/lib/mittsu/opengl/lib.rb +19 -0
  27. data/lib/mittsu/opengl/light_renderer.rb +43 -0
  28. data/lib/mittsu/opengl/material_basics.rb +57 -0
  29. data/lib/mittsu/opengl/plugins/shadow_map_plugin.rb +416 -0
  30. data/lib/mittsu/opengl/plugins/sprite_fragment.glsl +38 -0
  31. data/lib/mittsu/opengl/plugins/sprite_plugin.rb +250 -0
  32. data/lib/mittsu/opengl/plugins/sprite_vertex.glsl +31 -0
  33. data/lib/mittsu/opengl/program.rb +250 -0
  34. data/lib/mittsu/opengl/renderer.rb +1028 -0
  35. data/lib/mittsu/opengl/shader/chunk.rb +11 -0
  36. data/lib/mittsu/opengl/shader/chunks/alphamap_fragment.glsl +5 -0
  37. data/lib/mittsu/opengl/shader/chunks/alphamap_pars_fragment.glsl +5 -0
  38. data/lib/mittsu/opengl/shader/chunks/alphatest_fragment.glsl +5 -0
  39. data/lib/mittsu/opengl/shader/chunks/bumpmap_pars_fragment.glsl +40 -0
  40. data/lib/mittsu/opengl/shader/chunks/color_fragment.glsl +5 -0
  41. data/lib/mittsu/opengl/shader/chunks/color_pars_fragment.glsl +5 -0
  42. data/lib/mittsu/opengl/shader/chunks/color_pars_vertex.glsl +5 -0
  43. data/lib/mittsu/opengl/shader/chunks/color_vertex.glsl +5 -0
  44. data/lib/mittsu/opengl/shader/chunks/common.glsl +60 -0
  45. data/lib/mittsu/opengl/shader/chunks/default_vertex.glsl +15 -0
  46. data/lib/mittsu/opengl/shader/chunks/defaultnormal_vertex.glsl +21 -0
  47. data/lib/mittsu/opengl/shader/chunks/envmap_fragment.glsl +62 -0
  48. data/lib/mittsu/opengl/shader/chunks/envmap_pars_fragment.glsl +21 -0
  49. data/lib/mittsu/opengl/shader/chunks/envmap_pars_vertex.glsl +7 -0
  50. data/lib/mittsu/opengl/shader/chunks/envmap_vertex.glsl +17 -0
  51. data/lib/mittsu/opengl/shader/chunks/fog_fragment.glsl +26 -0
  52. data/lib/mittsu/opengl/shader/chunks/fog_pars_fragment.glsl +15 -0
  53. data/lib/mittsu/opengl/shader/chunks/lightmap_fragment.glsl +5 -0
  54. data/lib/mittsu/opengl/shader/chunks/lightmap_pars_fragment.glsl +6 -0
  55. data/lib/mittsu/opengl/shader/chunks/lightmap_pars_vertex.glsl +5 -0
  56. data/lib/mittsu/opengl/shader/chunks/lightmap_vertex.glsl +5 -0
  57. data/lib/mittsu/opengl/shader/chunks/lights_lambert_pars_vertex.glsl +43 -0
  58. data/lib/mittsu/opengl/shader/chunks/lights_lambert_vertex.glsl +196 -0
  59. data/lib/mittsu/opengl/shader/chunks/lights_phong_fragment.glsl +243 -0
  60. data/lib/mittsu/opengl/shader/chunks/lights_phong_pars_fragment.glsl +58 -0
  61. data/lib/mittsu/opengl/shader/chunks/lights_phong_pars_vertex.glsl +5 -0
  62. data/lib/mittsu/opengl/shader/chunks/lights_phong_vertex.glsl +5 -0
  63. data/lib/mittsu/opengl/shader/chunks/linear_to_gamma_fragment.glsl +2 -0
  64. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_fragment.glsl +5 -0
  65. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_pars_fragment.glsl +12 -0
  66. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_pars_vertex.glsl +11 -0
  67. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_vertex.glsl +15 -0
  68. data/lib/mittsu/opengl/shader/chunks/map_fragment.glsl +9 -0
  69. data/lib/mittsu/opengl/shader/chunks/map_pars_fragment.glsl +11 -0
  70. data/lib/mittsu/opengl/shader/chunks/map_pars_vertex.glsl +6 -0
  71. data/lib/mittsu/opengl/shader/chunks/map_particle_fragment.glsl +5 -0
  72. data/lib/mittsu/opengl/shader/chunks/map_particle_pars_fragment.glsl +6 -0
  73. data/lib/mittsu/opengl/shader/chunks/map_vertex.glsl +5 -0
  74. data/lib/mittsu/opengl/shader/chunks/morphnormal_vertex.glsl +12 -0
  75. data/lib/mittsu/opengl/shader/chunks/morphtarget_pars_vertex.glsl +13 -0
  76. data/lib/mittsu/opengl/shader/chunks/morphtarget_vertex.glsl +20 -0
  77. data/lib/mittsu/opengl/shader/chunks/normalmap_pars_fragment.glsl +27 -0
  78. data/lib/mittsu/opengl/shader/chunks/shadowmap_fragment.glsl +216 -0
  79. data/lib/mittsu/opengl/shader/chunks/shadowmap_pars_fragment.glsl +19 -0
  80. data/lib/mittsu/opengl/shader/chunks/shadowmap_pars_vertex.glsl +6 -0
  81. data/lib/mittsu/opengl/shader/chunks/shadowmap_vertex.glsl +9 -0
  82. data/lib/mittsu/opengl/shader/chunks/skinbase_vertex.glsl +8 -0
  83. data/lib/mittsu/opengl/shader/chunks/skinning_pars_vertex.glsl +47 -0
  84. data/lib/mittsu/opengl/shader/chunks/skinning_vertex.glsl +20 -0
  85. data/lib/mittsu/opengl/shader/chunks/skinnormal_vertex.glsl +20 -0
  86. data/lib/mittsu/opengl/shader/chunks/specularmap_fragment.glsl +12 -0
  87. data/lib/mittsu/opengl/shader/chunks/specularmap_pars_fragment.glsl +5 -0
  88. data/lib/mittsu/opengl/shader/chunks/worldpos_vertex.glsl +17 -0
  89. data/lib/mittsu/opengl/shader/lib/basic/basic_fragment.rbsl +37 -0
  90. data/lib/mittsu/opengl/shader/lib/basic/basic_uniforms.rbslu +3 -0
  91. data/lib/mittsu/opengl/shader/lib/basic/basic_vertex.rbsl +33 -0
  92. data/lib/mittsu/opengl/shader/lib/cube/cube_fragment.rbsl +12 -0
  93. data/lib/mittsu/opengl/shader/lib/cube/cube_uniforms.rbslu +2 -0
  94. data/lib/mittsu/opengl/shader/lib/cube/cube_vertex.rbsl +12 -0
  95. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_fragment.rbsl +26 -0
  96. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_uniforms.rbslu +0 -0
  97. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_vertex.rbsl +12 -0
  98. data/lib/mittsu/opengl/shader/lib/lambert/lambert_fragment.rbsl +56 -0
  99. data/lib/mittsu/opengl/shader/lib/lambert/lambert_uniforms.rbslu +7 -0
  100. data/lib/mittsu/opengl/shader/lib/lambert/lambert_vertex.rbsl +37 -0
  101. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_fragment.rbsl +27 -0
  102. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_uniforms.rbslu +2 -0
  103. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_vertex.rbsl +25 -0
  104. data/lib/mittsu/opengl/shader/lib/phong/phong_fragment.rbsl +45 -0
  105. data/lib/mittsu/opengl/shader/lib/phong/phong_uniforms.rbslu +11 -0
  106. data/lib/mittsu/opengl/shader/lib/phong/phong_vertex.rbsl +43 -0
  107. data/lib/mittsu/opengl/shader/lib.rb +45 -0
  108. data/lib/mittsu/opengl/shader/rbsl_loader.rb +168 -0
  109. data/lib/mittsu/opengl/shader/templates/fragment.glsl.erb +105 -0
  110. data/lib/mittsu/opengl/shader/templates/vertex.glsl.erb +143 -0
  111. data/lib/mittsu/opengl/shader/uniforms_lib.rb +86 -0
  112. data/lib/mittsu/opengl/shader/uniforms_utils.rb +31 -0
  113. data/lib/mittsu/opengl/shader.rb +56 -0
  114. data/lib/mittsu/opengl/state.rb +205 -0
  115. data/lib/mittsu/opengl/version.rb +5 -0
  116. data/lib/mittsu/opengl.rb +2 -0
  117. data/lib/mittsu/opengl_implementation/core/buffer_geometry.rb +11 -0
  118. data/lib/mittsu/opengl_implementation/core/geometry.rb +346 -0
  119. data/lib/mittsu/opengl_implementation/core/object_3d.rb +134 -0
  120. data/lib/mittsu/opengl_implementation/lights/ambient_light.rb +26 -0
  121. data/lib/mittsu/opengl_implementation/lights/directional_light.rb +35 -0
  122. data/lib/mittsu/opengl_implementation/lights/hemisphere_light.rb +39 -0
  123. data/lib/mittsu/opengl_implementation/lights/light.rb +55 -0
  124. data/lib/mittsu/opengl_implementation/lights/point_light.rb +36 -0
  125. data/lib/mittsu/opengl_implementation/lights/spot_light.rb +47 -0
  126. data/lib/mittsu/opengl_implementation/materials/line_basic_material.rb +16 -0
  127. data/lib/mittsu/opengl_implementation/materials/material.rb +274 -0
  128. data/lib/mittsu/opengl_implementation/materials/mesh_basic_material.rb +21 -0
  129. data/lib/mittsu/opengl_implementation/materials/mesh_lambert_material.rb +33 -0
  130. data/lib/mittsu/opengl_implementation/materials/mesh_phong_material.rb +44 -0
  131. data/lib/mittsu/opengl_implementation/materials/point_cloud_material.rb +27 -0
  132. data/lib/mittsu/opengl_implementation/materials/shader_material.rb +11 -0
  133. data/lib/mittsu/opengl_implementation/objects/group.rb +9 -0
  134. data/lib/mittsu/opengl_implementation/objects/line.rb +45 -0
  135. data/lib/mittsu/opengl_implementation/objects/mesh.rb +70 -0
  136. data/lib/mittsu/opengl_implementation/objects/point_cloud.rb +39 -0
  137. data/lib/mittsu/opengl_implementation/objects/sprite.rb +12 -0
  138. data/lib/mittsu/opengl_implementation/scenes/scene.rb +9 -0
  139. data/lib/mittsu/opengl_implementation/textures/compressed_texture.rb +20 -0
  140. data/lib/mittsu/opengl_implementation/textures/cube_texture.rb +77 -0
  141. data/lib/mittsu/opengl_implementation/textures/data_texture.rb +21 -0
  142. data/lib/mittsu/opengl_implementation/textures/render_target.rb +124 -0
  143. data/lib/mittsu/opengl_implementation/textures/texture.rb +107 -0
  144. data/mittsu-opengl.gemspec +36 -0
  145. metadata +314 -0
@@ -0,0 +1,31 @@
1
+ module Mittsu
2
+ module OpenGL::Shader::UniformsUtils
3
+ def self.merge(uniforms)
4
+ merged = {}
5
+
6
+ uniforms.each do |uniform|
7
+ tmp = UniformsUtils.clone(uniform)
8
+
9
+ next if tmp.nil?
10
+
11
+ tmp.each do |(p, tmp_p)|
12
+ merged[p] = tmp_p
13
+ end
14
+ end
15
+
16
+ merged
17
+ end
18
+
19
+ def self.clone(uniforms_src)
20
+ return if uniforms_src.nil?
21
+
22
+ uniforms_dst = {}
23
+
24
+ uniforms_src.each do |(u, uniform_src)|
25
+ uniforms_dst[u] = uniform_src.clone
26
+ end
27
+
28
+ uniforms_dst
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ module Mittsu
2
+ class OpenGL::Shader
3
+ attr_reader :shader
4
+
5
+ def initialize(type, string)
6
+ @shader = GL.CreateShader(type)
7
+ # filename = type == GL::VERTEX_SHADER ? 'vertex.glsl' : 'fragment.glsl'
8
+ # File.write filename, string
9
+
10
+ string_pointer = Fiddle::Pointer[string]
11
+ string_length = Fiddle::Pointer[string.length]
12
+
13
+ GL.ShaderSource(@shader, 1, string_pointer.ref, string_length.ref)
14
+ GL.CompileShader(@shader)
15
+
16
+ if !compile_status
17
+ puts "ERROR: Mittsu::OpenGL::Shader: Shader couldn't compile"
18
+ end
19
+
20
+ log_info = shader_info_log
21
+ if !log_info.empty?
22
+ puts "WARNING: Mittsu::OpenGL::Shader: GL.GetShaderInfoLog, #{log_info}"
23
+ puts add_line_numbers(string)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def compile_status
30
+ ptr = ' '*8
31
+ GL.GetShaderiv @shader, GL::COMPILE_STATUS, ptr
32
+ ptr.unpack('L')[0]
33
+ end
34
+
35
+ def shader_info_log
36
+ ptr = ' '*8
37
+ GL.GetShaderiv @shader, GL::INFO_LOG_LENGTH, ptr
38
+ length = ptr.unpack('L')[0]
39
+
40
+ if length > 0
41
+ log = ' '*length
42
+ GL.GetShaderInfoLog @shader, length, ptr, log
43
+ log.unpack("A#{length}")[0]
44
+ else
45
+ ''
46
+ end
47
+ end
48
+
49
+ def add_line_numbers(string)
50
+ string.split("\n").each_with_index.map { |line, i|
51
+ line_number = "#{i + 1}".rjust(4, ' ')
52
+ "#{line_number}: #{line}"
53
+ }.join("\n")
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,205 @@
1
+ module Mittsu
2
+ class OpenGL::State
3
+ def initialize
4
+ @new_attributes = Array.new(16) # Uint8Array
5
+ @enabled_attributes = Array.new(16) # Uint8Array
6
+
7
+ @current_blending = nil
8
+ @current_blend_equation = nil
9
+ @current_blend_src = nil
10
+ @current_blend_dst = nil
11
+ @current_blend_equation_alpha = nil
12
+ @current_blend_src_alpha = nil
13
+ @current_blend_dst_alpha = nil
14
+
15
+ @current_depth_test = nil
16
+ @current_depth_write = nil
17
+
18
+ @current_color_write = nil
19
+
20
+ @current_double_sided = nil
21
+ @current_flip_sided = nil
22
+
23
+ @current_line_width = nil
24
+
25
+ @current_polygon_offset = nil
26
+ @current_polygon_offset_factor = nil
27
+ @current_polygon_offset_units = nil
28
+ end
29
+
30
+ def init_attributes
31
+ @new_attributes.length.times do |i|
32
+ @new_attributes[i] = false
33
+ end
34
+ end
35
+
36
+ def enable_attribute(attribute)
37
+ GL.EnableVertexAttribArray(attribute)
38
+ @new_attributes[attribute] = true
39
+
40
+ if !@enabled_attributes[attribute]
41
+ # GL.EnableVertexAttribArray(attribute)
42
+ @enabled_attributes[attribute] = true
43
+ end
44
+ end
45
+
46
+ def disable_unused_attributes
47
+ @enabled_attributes.length.times do |i|
48
+ if @enabled_attributes[i] && !@new_attributes[i]
49
+ GL.DisableVertexAttribArray(i)
50
+ @enabled_attributes[i] = false
51
+ end
52
+ end
53
+ end
54
+
55
+ def set_blending(blending, blend_equation = nil, blend_src = nil, blend_dst = nil, blend_equation_alpha = nil, blend_src_alpha = nil, blend_dst_alpha = nil)
56
+ if blending != @current_blending
57
+ case blending
58
+ when NoBlending
59
+ GL.Disable(GL::BLEND)
60
+ when AdditiveBlending
61
+ GL.Enable(GL::BLEND)
62
+ GL.BlendEquation(GL::FUNC_ADD)
63
+ GL.BlendFunc(GL::SRC_ALPHA, GL::ONE)
64
+ when SubtractiveBlending
65
+ # TODO: Find blendFuncSeparate() combination ???
66
+ GL.Enable(GL::BLEND)
67
+ GL.BlendEquation(GL::FUNC_ADD)
68
+ GL.BlendFunc(GL::ZERO, GL::ONE_MINUS_SRC_COLOR)
69
+ when MultiplyBlending
70
+ # TODO: Find blendFuncSeparate() combination ???
71
+ GL.Enable(GL::BLEND)
72
+ GL.BlendEquation(GL::FUNC_ADD)
73
+ GL.BlendFunc(GL::ZERO, GL::ONE_MINUS_SRC_COLOR)
74
+ when CustomBlending
75
+ GL.Enable(GL::BLEND)
76
+ else
77
+ GL.Enable(GL::BLEND)
78
+ GL.BlendEquationSeparate(GL::FUNC_ADD, GL::FUNC_ADD)
79
+ GL.BlendFuncSeparate(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA, GL::ONE, GL::ONE_MINUS_SRC_ALPHA)
80
+ end
81
+
82
+ @current_blending = blending
83
+ end
84
+
85
+ if blending == CustomBlending
86
+ blend_equation_alpha ||= blend_equation
87
+ blend_src_alpha ||= blend_src
88
+ blend_dst_alpha ||= blend_dst
89
+
90
+ if blend_equation != @current_blend_equation || blend_equation_alpha != @current_blend_equation_alpha
91
+ GL.BlendEquationSeparate(GL::MITTSU_PARAMS[blend_equation], GL::MITTSU_PARAMS[blend_equation_alpha])
92
+
93
+ @current_blend_equation = blend_equation
94
+ @current_blend_equation_alpha = blend_equation_alpha
95
+ end
96
+
97
+ if blend_src != @current_blend_src || blend_dst != @current_blend_dst || blend_src_alpha != @current_blend_src_alpha || blend_dst_alpha != @current_blend_dst_alpha
98
+ GL.BlendFuncSeparate(GL::MITTSU_PARAMS[blend_src], GL::MITTSU_PARAMS[blend_dst], GL::MITTSU_PARAMS[blend_src_alpha], GL::MITTSU_PARAMS[blend_dst_alpha])
99
+
100
+ @current_blend_src = nil
101
+ @current_blend_dst = nil
102
+ @current_blend_src_alpha = nil
103
+ @current_blend_dst_alpha = nil
104
+ end
105
+ else
106
+ @current_blend_equation = nil
107
+ @current_blend_src = nil
108
+ @current_blend_dst = nil
109
+ @current_blend_equation_alpha = nil
110
+ @current_blend_src_alpha = nil
111
+ @current_blend_dst_alpha = nil
112
+ end
113
+ end
114
+
115
+ def set_depth_test(depth_test)
116
+ if @current_depth_test != depth_test
117
+ if depth_test
118
+ GL.Enable(GL::DEPTH_TEST)
119
+ else
120
+ GL.Disable(GL::DEPTH_TEST)
121
+ end
122
+
123
+ @current_depth_test = depth_test
124
+ end
125
+ end
126
+
127
+ def set_depth_write(depth_write)
128
+ if @current_depth_write != depth_write
129
+ GL.DepthMask(depth_write ? GL::TRUE : GL::FALSE)
130
+ @current_depth_write = depth_write
131
+ end
132
+ end
133
+
134
+ def set_color_write(color_write)
135
+ if @current_color_write != color_write
136
+ gl_color_write = color_write ? GL::TRUE : GL::FALSE
137
+ GL.ColorMask(gl_color_write, gl_color_write, gl_color_write, gl_color_write)
138
+ @current_color_write = color_write
139
+ end
140
+ end
141
+
142
+ def set_double_sided(double_sided)
143
+ if @current_double_sided != double_sided
144
+ if double_sided
145
+ GL.Disable(GL::CULL_FACE)
146
+ else
147
+ GL.Enable(GL::CULL_FACE)
148
+ end
149
+
150
+ @current_double_sided = double_sided
151
+ end
152
+ end
153
+
154
+ def set_flip_sided(flip_sided)
155
+ if @current_flip_sided != flip_sided
156
+ if flip_sided
157
+ GL.FrontFace(GL::CW)
158
+ else
159
+ GL.FrontFace(GL::CCW)
160
+ end
161
+
162
+ @current_flip_sided = flip_sided
163
+ end
164
+ end
165
+
166
+ def set_line_width(width)
167
+ if width != @current_line_width
168
+ GL.LineWidth(width)
169
+ @current_line_width = width
170
+ end
171
+ end
172
+
173
+ def set_polygon_offset(polygon_offset, factor, units)
174
+ if @current_polygon_offset != polygon_offset
175
+ if polygon_offset
176
+ GL.Enable(GL::POLYGON_OFFSET_FILL)
177
+ else
178
+ GL.Disable(GL::POLYGON_OFFSET_FILL)
179
+ end
180
+
181
+ @current_polygon_offset = polygon_offset
182
+ end
183
+
184
+ if polygon_offset && (@current_polygon_offset_factor != factor || @current_polygon_offset_units != units)
185
+ GL.PolygonOffset(factor, units)
186
+
187
+ @current_polygon_offset_factor = factor
188
+ @current_polygon_offset_units = units
189
+ end
190
+ end
191
+
192
+ def reset
193
+ @enabled_attributes.length.times do |i|
194
+ @enabled_attributes[i] = false
195
+ end
196
+
197
+ @current_blending = nil
198
+ @current_depth_test = nil
199
+ @current_depth_write = nil
200
+ @current_color_write = nil
201
+ @current_double_sided = nil
202
+ @current_flip_sided = nil
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,5 @@
1
+ module Mittsu
2
+ module OpenGL
3
+ VERSION = '0.5.0'
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'mittsu'
2
+ require 'mittsu/opengl/renderer'
@@ -0,0 +1,11 @@
1
+ module Mittsu
2
+ class BufferGeometry
3
+ include OpenGL::GeometryLike
4
+
5
+ attr_accessor :initted
6
+
7
+ def init
8
+ @initted = true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,346 @@
1
+ require 'mittsu/opengl/geometry_like'
2
+
3
+ module Mittsu
4
+ class Geometry
5
+ include OpenGL::GeometryLike
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 = GL.CreateVertexArray
75
+
76
+ @vertex_buffer = GL.CreateBuffer
77
+ @color_buffer = GL.CreateBuffer
78
+ @line_distance_buffer = GL.CreateBuffer
79
+
80
+ @renderer.info[:memory][:geometries] += 1
81
+ end
82
+
83
+ def create_particle_buffers
84
+ @vertex_array_object = GL.CreateVertexArray
85
+
86
+ @vertex_buffer = GL.CreateBuffer
87
+ @color_buffer = GL.CreateBuffer
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
+ GL.BindBuffer(GL::ARRAY_BUFFER, @vertex_buffer)
103
+ GL.BufferData_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
+ GL.BindBuffer(GL::ARRAY_BUFFER, @color_buffer)
116
+ GL.BufferData_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
+ GL.BindBuffer(GL::ARRAY_BUFFER, @line_distance_buffer)
125
+ GL.BufferData_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
+ GL.BindBuffer(GL::ARRAY_BUFFER, custom_attribute.buffer)
176
+ GL.BufferData_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
+ GL.BindBuffer(GL::ARRAY_BUFFER, @vertex_buffer)
195
+ GL.BufferData_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
+ GL.BindBuffer(GL::ARRAY_BUFFER, @color_buffer)
208
+ GL.BufferData_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
+ GL.BindBuffer(GL::ARRAY_BUFFER, customAttribute.buffer)
258
+ GL.BufferData(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 = OpenGL::GeometryGroup.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 = GL.CreateBuffer
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