mittsu 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +20 -0
  3. data/circle.yml +5 -5
  4. data/lib/mittsu/core/raycaster.rb +5 -5
  5. data/lib/mittsu/materials.rb +2 -0
  6. data/lib/mittsu/materials/point_cloud_material.rb +53 -0
  7. data/lib/mittsu/materials/sprite_material.rb +53 -0
  8. data/lib/mittsu/math/euler.rb +1 -1
  9. data/lib/mittsu/math/ray.rb +0 -1
  10. data/lib/mittsu/objects.rb +2 -0
  11. data/lib/mittsu/objects/mesh.rb +54 -28
  12. data/lib/mittsu/objects/point_cloud.rb +100 -0
  13. data/lib/mittsu/objects/sprite.rb +53 -0
  14. data/lib/mittsu/renderers/glfw_window.rb +8 -0
  15. data/lib/mittsu/renderers/opengl/core/buffer_geometry.rb +11 -0
  16. data/lib/mittsu/renderers/opengl/core/geometry.rb +119 -17
  17. data/lib/mittsu/renderers/opengl/materials/point_cloud_material.rb +27 -0
  18. data/lib/mittsu/renderers/opengl/objects/line.rb +1 -1
  19. data/lib/mittsu/renderers/opengl/objects/point_cloud.rb +39 -0
  20. data/lib/mittsu/renderers/opengl/objects/sprite.rb +12 -0
  21. data/lib/mittsu/renderers/opengl/opengl_buffer.rb +4 -0
  22. data/lib/mittsu/renderers/opengl/opengl_debug.rb +6 -6
  23. data/lib/mittsu/renderers/opengl/opengl_geometry_like.rb +1 -0
  24. data/lib/mittsu/renderers/opengl/opengl_helper.rb +7 -7
  25. data/lib/mittsu/renderers/opengl/opengl_implementations.rb +5 -0
  26. data/lib/mittsu/renderers/opengl/plugins/sprite_fragment.glsl +38 -0
  27. data/lib/mittsu/renderers/opengl/plugins/sprite_plugin.rb +250 -0
  28. data/lib/mittsu/renderers/opengl/plugins/sprite_vertex.glsl +31 -0
  29. data/lib/mittsu/renderers/opengl_renderer.rb +8 -28
  30. data/lib/mittsu/renderers/shaders/shader_lib/particle_basic/particle_basic_fragment.rbsl +27 -0
  31. data/lib/mittsu/renderers/shaders/shader_lib/particle_basic/particle_basic_uniforms.rbslu +2 -0
  32. data/lib/mittsu/renderers/shaders/shader_lib/particle_basic/particle_basic_vertex.rbsl +25 -0
  33. data/lib/mittsu/version.rb +1 -1
  34. metadata +18 -3
@@ -1,5 +1,6 @@
1
1
  require 'mittsu/core/geometry'
2
2
  require 'mittsu/core/object_3d'
3
+ require 'mittsu/core/buffer_geometry'
3
4
  require 'mittsu/objects'
4
5
  require 'mittsu/materials'
5
6
  require 'mittsu/textures'
@@ -7,9 +8,12 @@ require 'mittsu/scenes'
7
8
 
8
9
  require 'mittsu/renderers/opengl/core/geometry'
9
10
  require 'mittsu/renderers/opengl/core/object_3d'
11
+ require 'mittsu/renderers/opengl/core/buffer_geometry'
10
12
  require 'mittsu/renderers/opengl/objects/mesh'
11
13
  require 'mittsu/renderers/opengl/objects/line'
12
14
  require 'mittsu/renderers/opengl/objects/group'
15
+ require 'mittsu/renderers/opengl/objects/sprite'
16
+ require 'mittsu/renderers/opengl/objects/point_cloud'
13
17
  require 'mittsu/renderers/opengl/scenes/scene'
14
18
 
15
19
  require 'mittsu/renderers/opengl/lights/light'
@@ -25,6 +29,7 @@ require 'mittsu/renderers/opengl/materials/mesh_lambert_material'
25
29
  require 'mittsu/renderers/opengl/materials/mesh_phong_material'
26
30
  require 'mittsu/renderers/opengl/materials/line_basic_material'
27
31
  require 'mittsu/renderers/opengl/materials/shader_material'
32
+ require 'mittsu/renderers/opengl/materials/point_cloud_material'
28
33
 
29
34
  require 'mittsu/renderers/opengl/textures/texture'
30
35
  require 'mittsu/renderers/opengl/textures/cube_texture'
@@ -0,0 +1,38 @@
1
+ #version 330
2
+
3
+ uniform vec3 color;
4
+ uniform sampler2D map;
5
+ uniform float opacity;
6
+
7
+ uniform int fogType;
8
+ uniform vec3 fogColor;
9
+ uniform float fogDensity;
10
+ uniform float fogNear;
11
+ uniform float fogFar;
12
+ uniform float alphaTest;
13
+
14
+ in vec2 vUV;
15
+ layout(location = 0) out vec4 fragColor;
16
+
17
+ void main() {
18
+ vec4 texture = texture( map, vUV );
19
+
20
+ if ( texture.a < alphaTest ) discard;
21
+
22
+ fragColor = vec4( color * texture.xyz, texture.a * opacity );
23
+
24
+ if ( fogType > 0 ) {
25
+ float depth = gl_FragCoord.z / gl_FragCoord.w;
26
+ float fogFactor = 0.0;
27
+
28
+ if ( fogType == 1 ) {
29
+ fogFactor = smoothstep( fogNear, fogFar, depth );
30
+ } else {
31
+ const float LOG2 = 1.442695;
32
+ float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );
33
+ fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );
34
+ }
35
+
36
+ fragColor = mix( fragColor, vec4( fogColor, fragColor.w ), fogFactor );
37
+ }
38
+ }
@@ -0,0 +1,250 @@
1
+ module Mittsu
2
+ class SpritePlugin
3
+ include OpenGLHelper
4
+
5
+ VERTICES = [
6
+ -0.5, -0.5, 0.0, 0.0,
7
+ 0.5, -0.5, 1.0, 0.0,
8
+ 0.5, 0.5, 1.0, 1.0,
9
+ -0.5, 0.5, 0.0, 1.0
10
+ ] # Float32Array
11
+
12
+ FACES = [
13
+ 0, 1, 2,
14
+ 0, 2, 3
15
+ ] # Uint16Array
16
+
17
+ def initialize(renderer, sprites)
18
+ @renderer = renderer
19
+ @sprites = sprites
20
+ @program = nil
21
+
22
+ # for decomposing matrixWorld
23
+ @sprite_position = Vector3.new
24
+ @sprite_rotation = Quaternion.new
25
+ @sprite_scale = Vector3.new
26
+ end
27
+
28
+ def render(scene, camera)
29
+ return if @sprites.empty?
30
+
31
+ init if @program.nil?
32
+ setup_gl_for_render(camera)
33
+ setup_fog(scene)
34
+
35
+ update_positions_and_sort(camera)
36
+
37
+ render_all_sprites(scene)
38
+
39
+ glEnable(GL_CULL_FACE)
40
+ @renderer.reset_gl_state
41
+ end
42
+
43
+ private
44
+
45
+ def init
46
+ create_vertex_array_object
47
+ create_program
48
+
49
+ init_attributes
50
+ init_uniforms
51
+
52
+ # TODO: canvas texture??
53
+ end
54
+
55
+ def create_vertex_array_object
56
+ @vertex_array_object = glCreateVertexArray
57
+ glBindVertexArray(@vertex_array_object)
58
+
59
+ @vertex_buffer = glCreateBuffer
60
+ @element_buffer = glCreateBuffer
61
+
62
+ glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
63
+ glBufferData_easy(GL_ARRAY_BUFFER, VERTICES, GL_STATIC_DRAW)
64
+
65
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, @element_buffer)
66
+ glBufferData_easy(GL_ELEMENT_ARRAY_BUFFER, FACES, GL_STATIC_DRAW)
67
+ end
68
+
69
+ def create_program
70
+ @program = glCreateProgram
71
+
72
+ vertex_shader = OpenGLShader.new(GL_VERTEX_SHADER, File.read(File.join(__dir__, 'sprite_vertex.glsl')))
73
+ fragment_shader = OpenGLShader.new(GL_FRAGMENT_SHADER, File.read(File.join(__dir__, 'sprite_fragment.glsl')))
74
+
75
+ glAttachShader(@program, vertex_shader.shader)
76
+ glAttachShader(@program, fragment_shader.shader)
77
+
78
+ glLinkProgram(@program)
79
+ end
80
+
81
+ def init_attributes
82
+ @attributes = {
83
+ position: glGetAttribLocation(@program, 'position'),
84
+ uv: glGetAttribLocation(@program, 'uv')
85
+ }
86
+ end
87
+
88
+ def init_uniforms
89
+ @uniforms = {
90
+ uvOffset: glGetUniformLocation(@program, 'uvOffset'),
91
+ uvScale: glGetUniformLocation(@program, 'uvScale'),
92
+
93
+ rotation: glGetUniformLocation(@program, 'rotation'),
94
+ scale: glGetUniformLocation(@program, 'scale'),
95
+
96
+ color: glGetUniformLocation(@program, 'color'),
97
+ map: glGetUniformLocation(@program, 'map'),
98
+ opacity: glGetUniformLocation(@program, 'opacity'),
99
+
100
+ modelViewMatrix: glGetUniformLocation(@program, 'modelViewMatrix'),
101
+ projectionMatrix: glGetUniformLocation(@program, 'projectionMatrix'),
102
+
103
+ fogType: glGetUniformLocation(@program, 'fogType'),
104
+ fogDensity: glGetUniformLocation(@program, 'fogDensity'),
105
+ fogNear: glGetUniformLocation(@program, 'fogNear'),
106
+ fogFar: glGetUniformLocation(@program, 'fogFar'),
107
+ fogColor: glGetUniformLocation(@program, 'fogColor'),
108
+
109
+ alphaTest: glGetUniformLocation(@program, 'alphaTest')
110
+ }
111
+ end
112
+
113
+ def painter_sort_stable(a, b)
114
+ if a.z != b.z
115
+ b.z - a.z
116
+ else
117
+ b.id - a.id
118
+ end
119
+ end
120
+
121
+ def setup_gl_for_render(camera)
122
+ glUseProgram(@program)
123
+
124
+ glDisable(GL_CULL_FACE)
125
+ glEnable(GL_BLEND)
126
+
127
+ glBindVertexArray(@vertex_array_object)
128
+
129
+ glEnableVertexAttribArray(@attributes[:position])
130
+ glEnableVertexAttribArray(@attributes[:uv])
131
+
132
+ glBindBuffer(GL_ARRAY_BUFFER, @vertex_buffer)
133
+
134
+ glVertexAttribPointer(@attributes[:position], 2, GL_FLOAT, GL_FALSE, 2 * 8, 0)
135
+ glVertexAttribPointer(@attributes[:uv], 2, GL_FLOAT, GL_FALSE, 2 * 8, 8)
136
+
137
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, @element_buffer)
138
+
139
+ glUniformMatrix4fv(@uniforms[:projectionMatrix], 1, GL_FALSE, array_to_ptr_easy(camera.projection_matrix.elements))
140
+
141
+ glActiveTexture(GL_TEXTURE0)
142
+ glUniform1i(@uniforms[:map], 0)
143
+ end
144
+
145
+ def setup_fog(scene)
146
+ @old_fog_type = 0
147
+ @scene_fog_type = 0
148
+ fog = scene.fog
149
+
150
+ if fog
151
+ glUniform3f(@uniforms[:fogColor], fog.color.r, fog.color.g, fog.color.b)
152
+
153
+ if fog.is_a?(Fog)
154
+ glUniform1f(@uniforms[:fogNear], fog.near)
155
+ glUniform1f(@uniforms[:fogFar], fog.far)
156
+
157
+ glUniform1i(@uniforms[:fogType], 1)
158
+ @old_fog_type = 1
159
+ @scene_fog_type = 1
160
+ elsif fog.is_a?(FogExp2)
161
+ glUniform1f(@uniforms[:fogDensity], fog.density)
162
+
163
+ glUniform1i(@uniforms[:fogType], 2)
164
+ @old_fog_type = 2
165
+ @scene_fog_type = 2
166
+ end
167
+ else
168
+ glUniform1i(@uniforms[:fogType], 0)
169
+ @old_fog_type = 0
170
+ @scene_fog_type = 0
171
+ end
172
+ end
173
+
174
+ def update_positions_and_sort(camera)
175
+ @sprites.each do |sprite|
176
+ sprite.model_view_matrix.multiply_matrices(camera.matrix_world_inverse, sprite.matrix_world)
177
+ sprite.z = -sprite.model_view_matrix.elements[14]
178
+ end
179
+
180
+ @sprites.sort!(&self.method(:painter_sort_stable))
181
+ end
182
+
183
+ def render_all_sprites(scene)
184
+ @sprites.each do |sprite|
185
+ material = sprite.material
186
+
187
+ set_fog_uniforms(material, scene)
188
+ set_uv_uniforms(material)
189
+ set_color_uniforms(material)
190
+ set_transform_uniforms(sprite)
191
+ set_blend_mode(material)
192
+
193
+ # set texture
194
+ if material.map && material.map.image && material.map.image.width
195
+ material.map.set(0, @renderer)
196
+ else
197
+ # TODO: canvas texture?
198
+ # texture.set(0, @renderer)
199
+ end
200
+
201
+ # draw elements
202
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0) # GL_UNSIGNED_SHORT
203
+ end
204
+ end
205
+
206
+ def set_fog_uniforms(material, scene)
207
+ fog_type = 0
208
+
209
+ if scene.fog && material.fog
210
+ fog_type = @scene_fog_type
211
+ end
212
+
213
+ if @old_fog_type != fog_type
214
+ glUniform1(@uniforms[:fogType], fog_type)
215
+ @old_fog_type = fog_type
216
+ end
217
+ end
218
+
219
+ def set_uv_uniforms(material)
220
+ if !material.map.nil?
221
+ glUniform2f(@uniforms[:uvOffset], material.map.offset.x, material.map.offset.y)
222
+ glUniform2f(@uniforms[:uvScale], material.map.repeat.x, material.map.repeat.y)
223
+ else
224
+ glUniform2f(@uniforms[:uvOffset], 0.0, 0.0)
225
+ glUniform2f(@uniforms[:uvScale], 1.0, 1.0)
226
+ end
227
+ end
228
+
229
+ def set_color_uniforms(material)
230
+ glUniform1f(@uniforms[:opacity], material.opacity)
231
+ glUniform3f(@uniforms[:color], material.color.r, material.color.g, material.color.b)
232
+ glUniform1f(@uniforms[:alphaTest], material.alpha_test)
233
+ end
234
+
235
+ def set_transform_uniforms(sprite)
236
+ glUniformMatrix4fv(@uniforms[:modelViewMatrix], 1, GL_FALSE, array_to_ptr_easy(sprite.model_view_matrix.elements))
237
+
238
+ sprite.matrix_world.decompose(@sprite_position, @sprite_rotation, @sprite_scale)
239
+
240
+ glUniform1f(@uniforms[:rotation], sprite.material.rotation)
241
+ glUniform2fv(@uniforms[:scale], 1, array_to_ptr_easy([@sprite_scale.x, @sprite_scale.y]))
242
+ end
243
+
244
+ def set_blend_mode(material)
245
+ @renderer.state.set_blending(material.blending, material.blend_equation, material.blend_src, material.blend_dst)
246
+ @renderer.state.set_depth_test(material.depth_test)
247
+ @renderer.state.set_depth_write(material.depth_write)
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,31 @@
1
+ #version 330
2
+
3
+ uniform mat4 modelViewMatrix;
4
+ uniform mat4 projectionMatrix;
5
+ uniform float rotation;
6
+ uniform vec2 scale;
7
+ uniform vec2 uvOffset;
8
+ uniform vec2 uvScale;
9
+
10
+ in vec2 position;
11
+ in vec2 uv;
12
+
13
+ out vec2 vUV;
14
+
15
+ void main() {
16
+ vUV = uvOffset + uv * uvScale;
17
+
18
+ vec2 alignedPosition = position * scale;
19
+
20
+ vec2 rotatedPosition;
21
+ rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;
22
+ rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;
23
+
24
+ vec4 finalPosition;
25
+
26
+ finalPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );
27
+ finalPosition.xy += rotatedPosition;
28
+ finalPosition = projectionMatrix * finalPosition;
29
+
30
+ gl_Position = finalPosition;
31
+ }
@@ -19,6 +19,7 @@ require 'mittsu/renderers/opengl/opengl_light_renderer'
19
19
  require 'mittsu/renderers/opengl/opengl_default_target'
20
20
  require 'mittsu/renderers/opengl/opengl_buffer'
21
21
  require 'mittsu/renderers/opengl/plugins/shadow_map_plugin'
22
+ require 'mittsu/renderers/opengl/plugins/sprite_plugin'
22
23
  require 'mittsu/renderers/shaders/shader_lib'
23
24
  require 'mittsu/renderers/shaders/uniforms_utils'
24
25
 
@@ -260,13 +261,6 @@ module Mittsu
260
261
  @state.disable_unused_attributes
261
262
 
262
263
  object.render_buffer(camera, lights, fog, material, geometry_group, buffers_need_update)
263
-
264
- # TODO: render particles
265
- # when PointCloud
266
- # glDrawArrays(GL_POINTS, 0, geometry_group.particle_count)
267
- #
268
- # @info[:render][:calls] += 1
269
- # @info[:render][:points] += geometry_group.particle_count
270
264
  end
271
265
 
272
266
  def compressed_texture_formats
@@ -488,21 +482,6 @@ module Mittsu
488
482
  else
489
483
  object.update
490
484
  end
491
- # TODO: when PointCloud exists
492
- # elsif object.is_A? PointCloud
493
- # # TODO: glBindVertexArray ???
494
- # material = object.buffer_material(geometry)
495
- # custom_attributes_dirty = material.attributes && are_custom_attributes_dirty(material)
496
- #
497
- # if geometry.vertices_need_update || geometry.colors_need_update || custom_attributes_dirty
498
- # set_particle_buffers(geometry, GL_DYNAMIC_DRAW, object)
499
- # end
500
- #
501
- # geometry.vertices_need_update = false
502
- # geometry.colors_need_update = false
503
- #
504
- # material.attributes && clear_custom_attributes(material)
505
- # end
506
485
  end
507
486
 
508
487
  # FIXME: refactor
@@ -605,8 +584,6 @@ module Mittsu
605
584
  # when LineDashedMaterial
606
585
  # refresh_uniforms_line(material_uniforms, material)
607
586
  # refresh_uniforms_dash(material_uniforms, material)
608
- # when PointCloudMaterial
609
- # refresh_uniforms_particle(material_uniforms, material)
610
587
  # when MeshDepthMaterial
611
588
  # material_uniforms.m_near.value = camera.near
612
589
  # material_uniforms.m_far.value = camera.far
@@ -806,8 +783,8 @@ module Mittsu
806
783
  end
807
784
 
808
785
  def sort_objects_for_render
809
- @opaque_objects.sort { |a,b| OpenGLHelper.painter_sort_stable(a,b) }
810
- @transparent_objects.sort { |a,b| OpenGLHelper.reverse_painter_sort_stable(a,b) }
786
+ @opaque_objects.sort! { |a,b| OpenGLHelper.painter_sort_stable(a,b) }
787
+ @transparent_objects.sort! { |a,b| OpenGLHelper.reverse_painter_sort_stable(a,b) }
811
788
  end
812
789
 
813
790
  def set_matrices_for_immediate_objects(camera)
@@ -863,8 +840,9 @@ module Mittsu
863
840
  end
864
841
 
865
842
  def render_custom_plugins_post_pass(scene, camera)
843
+ @sprite_plugin.render(scene, camera)
844
+
866
845
  # TODO: when these custom plugins are implemented
867
- # @sprite_plugin.render(scene, camera)
868
846
  # lens_flare_plugin.render(scene, camera, @_current_render_target.width, @_current_render_target.height)
869
847
  end
870
848
 
@@ -912,6 +890,7 @@ module Mittsu
912
890
 
913
891
  def init_collections
914
892
  @lights = []
893
+ @sprites = []
915
894
 
916
895
  @_opengl_objects = {}
917
896
  @_opengl_objects_immediate = []
@@ -984,8 +963,9 @@ module Mittsu
984
963
  def init_plugins
985
964
  @shadow_map_plugin = ShadowMapPlugin.new(self, @lights, @_opengl_objects, @_opengl_objects_immediate)
986
965
 
966
+ @sprite_plugin = SpritePlugin.new(self, @sprites)
967
+
987
968
  # TODO: when these custom plugins are implemented
988
- # @sprite_plugin = SpritePlugin.new(self, @sprites)
989
969
  # @lens_flare_plugin = LensFlarePlugin.new(self, @lens_flares)
990
970
  end
991
971
 
@@ -0,0 +1,27 @@
1
+ uniform vec3 psColor;
2
+ uniform float opacity;
3
+
4
+ #include common
5
+ #include color_pars_fragment
6
+ #include map_particle_pars_fragment
7
+ #include fog_pars_fragment
8
+ #include shadowmap_pars_fragment
9
+ #include logdepthbuf_pars_fragment
10
+
11
+ void main() {
12
+ vec3 outgoingLight = vec3( 0.0 ); // outgoing light does not have an alpha, the surface does
13
+ vec4 diffuseColor = vec4( psColor, opacity );
14
+
15
+ #include logdepthbuf_fragment
16
+ #include map_particle_fragment
17
+ #include color_fragment
18
+ #include alphatest_fragment
19
+
20
+ outgoingLight = diffuseColor.rgb; // simple shader
21
+
22
+ #include shadowmap_fragment
23
+ #include fog_fragment
24
+
25
+
26
+ fragColor = vec4( outgoingLight, diffuseColor.a ); // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
27
+ }