mittsu 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/mittsu.rb +8 -0
  4. data/lib/mittsu/core/face3.rb +1 -1
  5. data/lib/mittsu/core/geometry.rb +0 -4
  6. data/lib/mittsu/core/object_3d.rb +7 -6
  7. data/lib/mittsu/extras/helpers/camera_helper.rb +2 -2
  8. data/lib/mittsu/materials/material.rb +0 -4
  9. data/lib/mittsu/renderers/opengl/core/{opengl_geometry.rb → geometry.rb} +23 -32
  10. data/lib/mittsu/renderers/opengl/core/{opengl_object_3d.rb → object_3d.rb} +20 -24
  11. data/lib/mittsu/renderers/opengl/lights/{opengl_ambient_light.rb → ambient_light.rb} +4 -4
  12. data/lib/mittsu/renderers/opengl/lights/{opengl_directional_light.rb → directional_light.rb} +4 -4
  13. data/lib/mittsu/renderers/opengl/lights/{opengl_hemisphere_light.rb → hemisphere_light.rb} +4 -7
  14. data/lib/mittsu/renderers/opengl/lights/light.rb +55 -0
  15. data/lib/mittsu/renderers/opengl/lights/{opengl_point_light.rb → point_light.rb} +5 -5
  16. data/lib/mittsu/renderers/opengl/lights/{opengl_spot_light.rb → spot_light.rb} +8 -8
  17. data/lib/mittsu/renderers/opengl/materials/{opengl_line_basic_material.rb → line_basic_material.rb} +3 -3
  18. data/lib/mittsu/renderers/opengl/materials/{opengl_material.rb → material.rb} +55 -56
  19. data/lib/mittsu/renderers/opengl/materials/mesh_basic_material.rb +21 -0
  20. data/lib/mittsu/renderers/opengl/materials/{opengl_mesh_lambert_material.rb → mesh_lambert_material.rb} +9 -5
  21. data/lib/mittsu/renderers/opengl/materials/{opengl_mesh_phong_material.rb → mesh_phong_material.rb} +11 -7
  22. data/lib/mittsu/renderers/opengl/materials/opengl_material_basics.rb +57 -0
  23. data/lib/mittsu/renderers/opengl/materials/{opengl_shader_material.rb → shader_material.rb} +1 -1
  24. data/lib/mittsu/renderers/opengl/objects/group.rb +9 -0
  25. data/lib/mittsu/renderers/opengl/objects/line.rb +45 -0
  26. data/lib/mittsu/renderers/opengl/objects/{opengl_mesh.rb → mesh.rb} +15 -22
  27. data/lib/mittsu/renderers/opengl/opengl_buffer.rb +4 -0
  28. data/lib/mittsu/renderers/opengl/opengl_debug.rb +5 -4
  29. data/lib/mittsu/renderers/opengl/opengl_default_target.rb +0 -4
  30. data/lib/mittsu/renderers/opengl/opengl_geometry_group.rb +2 -8
  31. data/lib/mittsu/renderers/opengl/opengl_geometry_like.rb +2 -1
  32. data/lib/mittsu/renderers/opengl/opengl_implementations.rb +24 -53
  33. data/lib/mittsu/renderers/opengl/opengl_lib.rb +62 -0
  34. data/lib/mittsu/renderers/opengl/opengl_light_renderer.rb +6 -6
  35. data/lib/mittsu/renderers/opengl/opengl_program.rb +3 -5
  36. data/lib/mittsu/renderers/opengl/plugins/shadow_map_plugin.rb +11 -12
  37. data/lib/mittsu/renderers/opengl/scenes/scene.rb +9 -0
  38. data/lib/mittsu/renderers/opengl/textures/{opengl_compressed_texture.rb → compressed_texture.rb} +5 -6
  39. data/lib/mittsu/renderers/opengl/textures/{opengl_cube_texture.rb → cube_texture.rb} +23 -21
  40. data/lib/mittsu/renderers/opengl/textures/{opengl_data_texture.rb → data_texture.rb} +3 -5
  41. data/lib/mittsu/renderers/opengl/textures/{opengl_texture.rb → texture.rb} +29 -33
  42. data/lib/mittsu/renderers/opengl_render_target.rb +5 -6
  43. data/lib/mittsu/renderers/opengl_renderer.rb +35 -44
  44. data/lib/mittsu/textures/texture.rb +0 -4
  45. data/lib/mittsu/version.rb +1 -1
  46. metadata +26 -24
  47. data/lib/mittsu/renderers/opengl/lights/opengl_light.rb +0 -52
  48. data/lib/mittsu/renderers/opengl/materials/opengl_mesh_basic_material.rb +0 -69
  49. data/lib/mittsu/renderers/opengl/objects/opengl_group.rb +0 -8
  50. data/lib/mittsu/renderers/opengl/objects/opengl_line.rb +0 -54
  51. data/lib/mittsu/renderers/opengl/scenes/opengl_scene.rb +0 -8
@@ -1,5 +1,5 @@
1
1
  module Mittsu
2
- class OpenGLPointLight < OpenGLLight
2
+ class PointLight
3
3
  TYPE = :point
4
4
 
5
5
  class Cache < Struct.new(:length, :count, :colors, :distances, :positions, :decays)
@@ -15,9 +15,9 @@ module Mittsu
15
15
  def setup_specific(index)
16
16
  offset = index * 3;
17
17
 
18
- OpenGLHelper.set_color_linear(@cache.colors, offset, @light.color, @light.intensity)
18
+ OpenGLHelper.set_color_linear(@cache.colors, offset, color, intensity)
19
19
 
20
- @_vector3.set_from_matrix_position(@light.matrix_world)
20
+ @_vector3.set_from_matrix_position(matrix_world)
21
21
 
22
22
  positions = @cache.positions
23
23
  positions[offset] = @_vector3.x
@@ -25,8 +25,8 @@ module Mittsu
25
25
  positions[offset + 2] = @_vector3.z
26
26
 
27
27
  # distance is 0 if decay is 0, because there is no attenuation at all.
28
- @cache.distances[index] = @light.distance
29
- @cache.decays[index] = @light.distance.zero? ? 0.0 : @light.decay
28
+ @cache.distances[index] = distance
29
+ @cache.decays[index] = distance.zero? ? 0.0 : decay
30
30
  end
31
31
 
32
32
  def to_sym
@@ -1,5 +1,5 @@
1
1
  module Mittsu
2
- class OpenGLSpotLight < OpenGLLight
2
+ class SpotLight
3
3
  TYPE = :spot
4
4
 
5
5
  class Cache < Struct.new(:length, :count, :colors, :directions, :distances, :positions, :exponents, :angles_cos, :decays)
@@ -15,18 +15,18 @@ module Mittsu
15
15
  def setup_specific(index)
16
16
  offset = index * 3
17
17
 
18
- OpenGLHelper.set_color_linear(@cache.colors, offset, @light.color, @light.intensity)
18
+ OpenGLHelper.set_color_linear(@cache.colors, offset, color, intensity)
19
19
 
20
- @_direction.set_from_matrix_position(@light.matrix_world)
20
+ @_direction.set_from_matrix_position(matrix_world)
21
21
 
22
22
  positions = @cache.positions
23
23
  positions[offset] = @_direction.x
24
24
  positions[offset + 1] = @_direction.y
25
25
  positions[offset + 2] = @_direction.z
26
26
 
27
- @cache.distances[index] = @light.distance
27
+ @cache.distances[index] = distance
28
28
 
29
- @_vector3.set_from_matrix_position(@light.target.matrix_world)
29
+ @_vector3.set_from_matrix_position(target.matrix_world)
30
30
  @_direction.sub(@_vector3)
31
31
  @_direction.normalize
32
32
 
@@ -35,9 +35,9 @@ module Mittsu
35
35
  directions[offset + 1] = @_direction.y
36
36
  directions[offset + 2] = @_direction.z
37
37
 
38
- @cache.angles_cos[index] = Math.cos(@light.angle)
39
- @cache.exponents[index] = @light.exponent;
40
- @cache.decays[index] = @light.distance.zero? ? 0.0 : @light.decay
38
+ @cache.angles_cos[index] = Math.cos(angle)
39
+ @cache.exponents[index] = exponent;
40
+ @cache.decays[index] = distance.zero? ? 0.0 : decay
41
41
  end
42
42
 
43
43
  def to_sym
@@ -1,8 +1,8 @@
1
1
  module Mittsu
2
- class OpenGLLineBasicMaterial < OpenGLMaterial
2
+ class LineBasicMaterial
3
3
  def refresh_uniforms(uniforms)
4
- uniforms['diffuse'].value = @material.color
5
- uniforms['opacity'].value = @material.opacity
4
+ uniforms['diffuse'].value = color
5
+ uniforms['opacity'].value = opacity
6
6
  end
7
7
 
8
8
  def init_shader
@@ -1,5 +1,5 @@
1
1
  module Mittsu
2
- class OpenGLMaterial
2
+ class Material
3
3
  # TODO: init_shader for these material-types
4
4
  # MeshDepthMaterial => :depth, # TODO...
5
5
  # MeshNormalMaterial => :normal, # TODO...
@@ -9,48 +9,47 @@ module Mittsu
9
9
  attr_accessor :shadow_pass
10
10
  attr_reader :shader, :uniforms_list
11
11
 
12
- def initialize(material, renderer)
13
- @material = material
12
+ def init(lights, fog, object, renderer)
14
13
  @renderer = renderer
15
- end
16
14
 
17
- def init(lights, fog, object)
18
- @material.add_event_listener(:dispose, @renderer.method(:on_material_dispose))
15
+ add_event_listener(:dispose, @renderer.method(:on_material_dispose))
19
16
 
20
17
  init_shader
21
18
 
22
- @material.program = find_or_create_program(lights, fog, object)
19
+ self.program = find_or_create_program(lights, fog, object)
23
20
 
24
- count_supported_morph_attributes(@material.program.attributes)
21
+ count_supported_morph_attributes(program.attributes)
25
22
 
26
23
  @uniforms_list = get_uniforms_list
27
24
  end
28
25
 
29
- def set
30
- if @material.transparent
31
- @renderer.state.set_blending(@material.blending, @material.blend_equation, @material.blend_src, @material.blend_dst, @material.blend_equation_alpha, @material.blend_src_alpha, @material.blend_dst_alpha)
26
+ def set(renderer)
27
+ @renderer = renderer
28
+
29
+ if transparent
30
+ @renderer.state.set_blending(blending, blend_equation, blend_src, blend_dst, blend_equation_alpha, blend_src_alpha, blend_dst_alpha)
32
31
  else
33
32
  @renderer.state.set_blending(NoBlending)
34
33
  end
35
34
 
36
- @renderer.state.set_depth_test(@material.depth_test)
37
- @renderer.state.set_depth_write(@material.depth_write)
38
- @renderer.state.set_color_write(@material.color_write)
39
- @renderer.state.set_polygon_offset(@material.polygon_offset, @material.polygon_offset_factor, @material.polygon_offset_units)
35
+ @renderer.state.set_depth_test(depth_test)
36
+ @renderer.state.set_depth_write(depth_write)
37
+ @renderer.state.set_color_write(color_write)
38
+ @renderer.state.set_polygon_offset(polygon_offset, polygon_offset_factor, polygon_offset_units)
40
39
  end
41
40
 
42
41
  def needs_face_normals?
43
- @material.shading == FlatShading
42
+ shading == FlatShading
44
43
  end
45
44
 
46
45
  def clear_custom_attributes
47
- @material.attributes.each do |attribute|
46
+ attributes.each do |attribute|
48
47
  attribute.needs_update = false
49
48
  end
50
49
  end
51
50
 
52
51
  def custom_attributes_dirty?
53
- @material.attributes.each do |attribute|
52
+ attributes.each do |attribute|
54
53
  return true if attribute.needs_update
55
54
  end
56
55
  false
@@ -61,24 +60,24 @@ module Mittsu
61
60
  end
62
61
 
63
62
  def needs_camera_position_uniform?
64
- @material.env_map
63
+ env_map
65
64
  end
66
65
 
67
66
  def needs_view_matrix_uniform?
68
- @material.skinning
67
+ skinning
69
68
  end
70
69
 
71
70
  def needs_lights?
72
- @material.lights
71
+ lights
73
72
  end
74
73
 
75
74
  protected
76
75
 
77
76
  def init_shader
78
77
  @shader = {
79
- uniforms: @material.uniforms,
80
- vertex_shader: @material.vertex_shader,
81
- fragment_shader: @material.fragment_shader
78
+ uniforms: uniforms,
79
+ vertex_shader: vertex_shader,
80
+ fragment_shader: fragment_shader
82
81
  }
83
82
  end
84
83
 
@@ -94,7 +93,7 @@ module Mittsu
94
93
  }.each_with_object({
95
94
  directional: 0, point: 0, spot: 0, hemi: 0, other: 0
96
95
  }) { |light, counts|
97
- counts[light.implementation(@renderer).to_sym] += 1
96
+ counts[light.to_sym] += 1
98
97
  }
99
98
  end
100
99
 
@@ -142,11 +141,11 @@ module Mittsu
142
141
  end
143
142
 
144
143
  def count_supported_morph_attributes(attributes)
145
- if @material.morph_targets
146
- @material.num_supported_morph_normals = count_supported_morph_attribute(attributes, 'morphTarget', @renderer.max_morph_normals)
144
+ if morph_targets
145
+ self.num_supported_morph_normals = count_supported_morph_attribute(attributes, 'morphTarget', @renderer.max_morph_normals)
147
146
  end
148
- if @material.morph_normals
149
- @material.num_supported_morph_normals = count_supported_morph_attribute(attributes, 'morphNormal', @renderer.max_morph_normals)
147
+ if morph_normals
148
+ self.num_supported_morph_normals = count_supported_morph_attribute(attributes, 'morphNormal', @renderer.max_morph_normals)
150
149
  end
151
150
  end
152
151
 
@@ -159,7 +158,7 @@ module Mittsu
159
158
 
160
159
  def get_uniforms_list
161
160
  @shader[:uniforms].map { |(key, uniform)|
162
- location = @material.program.uniforms[key]
161
+ location = program.uniforms[key]
163
162
  if location
164
163
  [uniform, location]
165
164
  end
@@ -177,34 +176,34 @@ module Mittsu
177
176
  {
178
177
  supports_vertex_textures: @renderer.supports_vertex_textures?,
179
178
 
180
- map: !!@material.map,
181
- env_map: !!@material.env_map,
182
- env_map_mode: @material.env_map && @material.env_map.mapping,
183
- light_map: !!@material.light_map,
184
- bump_map: !!@material.light_map,
185
- normal_map: !!@material.normal_map,
186
- specular_map: !!@material.specular_map,
187
- alpha_map: !!@material.alpha_map,
179
+ map: !!map,
180
+ env_map: !!env_map,
181
+ env_map_mode: env_map && env_map.mapping,
182
+ light_map: !!light_map,
183
+ bump_map: !!light_map,
184
+ normal_map: !!normal_map,
185
+ specular_map: !!specular_map,
186
+ alpha_map: !!alpha_map,
188
187
 
189
- combine: @material.combine,
188
+ combine: combine,
190
189
 
191
- vertex_colors: @material.vertex_colors,
190
+ vertex_colors: vertex_colors,
192
191
 
193
192
  fog: fog,
194
- use_fog: @material.fog,
193
+ use_fog: fog,
195
194
  # fog_exp: fog.is_a?(FogExp2), # TODO: when FogExp2 exists
196
195
 
197
- flat_shading: @material.shading == FlatShading,
196
+ flat_shading: shading == FlatShading,
198
197
 
199
- size_attenuation: @material.size_attenuation,
198
+ size_attenuation: size_attenuation,
200
199
  logarithmic_depth_buffer: @renderer.logarithmic_depth_buffer,
201
200
 
202
- skinning: @material.skinning,
201
+ skinning: skinning,
203
202
  max_bones: max_bones,
204
203
  use_vertex_texture: @renderer.supports_bone_textures?,
205
204
 
206
- morph_targets: @material.morph_targets,
207
- morph_normals: @material.morph_normals,
205
+ morph_targets: morph_targets,
206
+ morph_normals: morph_normals,
208
207
  max_morph_targets: @renderer.max_morph_targets,
209
208
  max_morph_normals: @renderer.max_morph_normals,
210
209
 
@@ -219,11 +218,11 @@ module Mittsu
219
218
  shadow_map_debug: @renderer.shadow_map_debug,
220
219
  shadow_map_cascade: @renderer.shadow_map_cascade,
221
220
 
222
- alpha_test: @material.alpha_test,
223
- metal: @material.metal,
224
- wrap_around: @material.wrap_around,
225
- double_sided: @material.side == DoubleSide,
226
- flip_sided: @material.side == BackSide
221
+ alpha_test: alpha_test,
222
+ metal: metal,
223
+ wrap_around: wrap_around,
224
+ double_sided: side == DoubleSide,
225
+ flip_sided: side == BackSide
227
226
  }
228
227
  end
229
228
 
@@ -233,12 +232,12 @@ module Mittsu
233
232
  if shader_id
234
233
  chunks << shader_id
235
234
  else
236
- chunks << @material.fragment_shader
237
- chunks << @material.vertex_shader
235
+ chunks << fragment_shader
236
+ chunks << vertex_shader
238
237
  end
239
238
 
240
- if !@material.defines.nil?
241
- @material.defines.each do |(name, define)|
239
+ if !defines.nil?
240
+ defines.each do |(name, define)|
242
241
  chunks << name
243
242
  chunks << define
244
243
  end
@@ -261,7 +260,7 @@ module Mittsu
261
260
  end
262
261
 
263
262
  if program.nil?
264
- program = OpenGLProgram.new(@renderer, code, @material, parameters)
263
+ program = OpenGLProgram.new(@renderer, code, self, parameters)
265
264
  @renderer.programs.push(program)
266
265
 
267
266
  @renderer.info[:memory][:programs] = @renderer.programs.length
@@ -0,0 +1,21 @@
1
+ require 'mittsu/renderers/opengl/materials/opengl_material_basics'
2
+
3
+ module Mittsu
4
+ class MeshBasicMaterial
5
+ include OpenGLMaterialBasics
6
+
7
+ def refresh_uniforms(uniforms)
8
+ refresh_uniforms_basic(uniforms)
9
+ end
10
+
11
+ protected
12
+
13
+ def init_shader
14
+ @shader = ShaderLib.create_shader(shader_id)
15
+ end
16
+
17
+ def shader_id
18
+ :basic
19
+ end
20
+ end
21
+ end
@@ -1,12 +1,16 @@
1
+ require 'mittsu/renderers/opengl/materials/opengl_material_basics'
2
+
1
3
  module Mittsu
2
- class OpenGLMeshLambertMaterial < OpenGLMeshBasicMaterial
4
+ class MeshLambertMaterial
5
+ include OpenGLMaterialBasics
6
+
3
7
  def refresh_uniforms(uniforms)
4
- super
8
+ refresh_uniforms_basic(uniforms)
5
9
 
6
- uniforms['emissive'].value = @material.emissive
10
+ uniforms['emissive'].value = emissive
7
11
 
8
- if @material.wrap_around
9
- uniforms['wrapRGB'].value.copy(@material.wrap_rgb)
12
+ if wrap_around
13
+ uniforms['wrapRGB'].value.copy(wrap_rgb)
10
14
  end
11
15
  end
12
16
 
@@ -1,19 +1,23 @@
1
+ require 'mittsu/renderers/opengl/materials/opengl_material_basics'
2
+
1
3
  module Mittsu
2
- class OpenGLMeshPhongMaterial < OpenGLMeshBasicMaterial
4
+ class MeshPhongMaterial
5
+ include OpenGLMaterialBasics
6
+
3
7
  def needs_face_normals?
4
8
  false
5
9
  end
6
10
 
7
11
  def refresh_uniforms(uniforms)
8
- super
12
+ refresh_uniforms_basic(uniforms)
9
13
 
10
- uniforms['shininess'].value = @material.shininess
14
+ uniforms['shininess'].value = shininess
11
15
 
12
- uniforms['emissive'].value = @material.emissive
13
- uniforms['specular'].value = @material.specular
16
+ uniforms['emissive'].value = emissive
17
+ uniforms['specular'].value = specular
14
18
 
15
- if @material.wrap_around
16
- uniforms['wrapRGB'].value.copy(@material.wrap_rgb)
19
+ if wrap_around
20
+ uniforms['wrapRGB'].value.copy(wrap_rgb)
17
21
  end
18
22
  end
19
23
 
@@ -0,0 +1,57 @@
1
+ module Mittsu
2
+ module OpenGLMaterialBasics
3
+ def refresh_uniforms_basic(uniforms)
4
+ refresh_map_uniforms(uniforms)
5
+ refresh_env_map_uniforms(uniforms)
6
+ refresh_other_uniforms(uniforms)
7
+ end
8
+
9
+ def refresh_map_uniforms(uniforms)
10
+ uniforms['map'].value = map
11
+ uniforms['lightMap'].value = light_map
12
+ uniforms['specularMap'].value = specular_map
13
+ uniforms['alphaMap'].value = alpha_map
14
+
15
+ if bump_map
16
+ uniforms['bumpMap'].value = bump_map
17
+ uniforms['bumpScale'].value = bump_scale
18
+ end
19
+
20
+ if normal_map
21
+ uniforms['normalMap'].value = normal_map
22
+ uniforms['normalScale'].value.copy(normal_scale)
23
+ end
24
+ end
25
+
26
+ def refresh_env_map_uniforms(uniforms)
27
+ uv_scale_map = get_uv_scale_map
28
+
29
+ if uv_scale_map
30
+ offset = uv_scale_map.offset
31
+ repeat = uv_scale_map.repeat
32
+
33
+ uniforms['offsetRepeat'].value.set(offset.x, offset.y, repeat.x, repeat.y)
34
+ end
35
+
36
+ uniforms['envMap'].value = env_map
37
+ # TODO: when OpenGLRenderTargetCube exists
38
+ # uniforms['flipEnvMap'].value = envMap.is_a?(OpenGLRenderTargetCube) ? 1 : - 1
39
+ end
40
+
41
+ def refresh_other_uniforms(uniforms)
42
+ uniforms['opacity'].value = opacity
43
+ uniforms['diffuse'].value = color
44
+
45
+ uniforms['reflectivity'].value = reflectivity
46
+ uniforms['refractionRatio'].value = refraction_ratio
47
+ end
48
+
49
+ def get_uv_scale_map
50
+ map ||
51
+ specular_map ||
52
+ normal_map ||
53
+ bump_map ||
54
+ alpha_map
55
+ end
56
+ end
57
+ end