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,33 @@
1
+ require 'mittsu/opengl/material_basics'
2
+
3
+ module Mittsu
4
+ class MeshLambertMaterial
5
+ include OpenGL::MaterialBasics
6
+
7
+ def refresh_uniforms(uniforms)
8
+ refresh_uniforms_basic(uniforms)
9
+
10
+ uniforms['emissive'].value = emissive
11
+
12
+ if wrap_around
13
+ uniforms['wrapRGB'].value.copy(wrap_rgb)
14
+ end
15
+ end
16
+
17
+ def needs_view_matrix_uniform?
18
+ true
19
+ end
20
+
21
+ def needs_lights?
22
+ true
23
+ end
24
+
25
+ def init_shader
26
+ @shader = OpenGL::Shader::Lib.create_shader(shader_id)
27
+ end
28
+
29
+ def shader_id
30
+ :lambert
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,44 @@
1
+ require 'mittsu/opengl/material_basics'
2
+
3
+ module Mittsu
4
+ class MeshPhongMaterial
5
+ include OpenGL::MaterialBasics
6
+
7
+ def needs_face_normals?
8
+ false
9
+ end
10
+
11
+ def refresh_uniforms(uniforms)
12
+ refresh_uniforms_basic(uniforms)
13
+
14
+ uniforms['shininess'].value = shininess
15
+
16
+ uniforms['emissive'].value = emissive
17
+ uniforms['specular'].value = specular
18
+
19
+ if wrap_around
20
+ uniforms['wrapRGB'].value.copy(wrap_rgb)
21
+ end
22
+ end
23
+
24
+ def needs_camera_position_uniform?
25
+ true
26
+ end
27
+
28
+ def needs_view_matrix_uniform?
29
+ true
30
+ end
31
+
32
+ def needs_lights?
33
+ true
34
+ end
35
+
36
+ def init_shader
37
+ @shader = OpenGL::Shader::Lib.create_shader(shader_id)
38
+ end
39
+
40
+ def shader_id
41
+ :phong
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,27 @@
1
+ module Mittsu
2
+ class PointCloudMaterial
3
+ def refresh_uniforms(uniforms)
4
+ uniforms['psColor'].value = color
5
+ uniforms['opacity'].value = opacity
6
+ uniforms['size'].value = size
7
+ # uniforms['scale'].value = _canvas.height / 2.0 # TODO get window height?
8
+
9
+ uniforms['map'].value = map
10
+
11
+ if !map.nil?
12
+ offset = material.map.offset
13
+ repeat = material.map.repeat
14
+
15
+ uniforms['offsetRepeat'].value.set(offset.x, offset.y, repeat.x, repeat.y)
16
+ end
17
+ end
18
+
19
+ def init_shader
20
+ @shader = OpenGL::Shader::Lib.create_shader(shader_id)
21
+ end
22
+
23
+ def shader_id
24
+ :particle_basic
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module Mittsu
2
+ class ShaderMaterial
3
+ def needs_camera_position_uniform?
4
+ true
5
+ end
6
+
7
+ def needs_view_matrix_uniform?
8
+ true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Mittsu
2
+ class Group
3
+ def project(renderer)
4
+ @renderer = renderer
5
+ return unless visible
6
+ project_children
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,45 @@
1
+ module Mittsu
2
+ class Line
3
+ def render_buffer(camera, lights, fog, material, geometry_group, update_buffers)
4
+ opengl_mode = mode == LineStrip ? GL::LINE_STRIP : GL::LINES
5
+
6
+ @renderer.state.set_line_width(material.line_width * @renderer.pixel_ratio)
7
+
8
+ GL.DrawArrays(opengl_mode, 0, geometry_group.line_count)
9
+
10
+ @renderer.info[:render][:calls] += 1
11
+ end
12
+
13
+ def update
14
+ # TODO: GL.BindVertexArray ???
15
+ material = buffer_material(geometry)
16
+ custom_attributes_dirty = material.attributes && material.custom_attributes_dirty?
17
+
18
+ if geometry.vertices_need_update || geometry.colors_need_update || geometry.line_distances_need_update || custom_attributes_dirty
19
+ geometry.set_line_buffers(GL::DYNAMIC_DRAW)
20
+ end
21
+
22
+ geometry.vertices_need_update = false
23
+ geometry.colors_need_update = false
24
+ geometry.line_distances_need_update = false
25
+
26
+ material.attributes && material.clear_custom_attributes
27
+ end
28
+
29
+ def init_geometry
30
+ geometry.renderer = @renderer
31
+ if geometry.vertex_buffer.nil?
32
+ geometry.create_line_buffers
33
+ geometry.init_line_buffers(self)
34
+
35
+ geometry.vertices_need_update = true
36
+ geometry.colors_need_update = true
37
+ geometry.line_distances_need_update = true
38
+ end
39
+ end
40
+
41
+ def add_opengl_object
42
+ @renderer.add_opengl_object(geometry, self)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,70 @@
1
+ module Mittsu
2
+ class Mesh
3
+ attr_accessor :renderer
4
+
5
+ def render_buffer(camera, lights, fog, material, geometry_group, update_buffers)
6
+ type = GL::UNSIGNED_INT # geometry_group.type_array == Uint32Array ? GL::UNSIGNED_INT : GL::UNSIGNED_SHORT
7
+
8
+ # wireframe
9
+ if material.wireframe
10
+ @renderer.state.set_line_width(material.wireframe_linewidth * @renderer.pixel_ratio)
11
+
12
+ GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, geometry_group.line_buffer) if update_buffers
13
+ GL.DrawElements(GL::LINES, geometry_group.line_count, type, 0)
14
+
15
+ # triangles
16
+ else
17
+ GL.BindBuffer(GL::ELEMENT_ARRAY_BUFFER, geometry_group.face_buffer) if update_buffers
18
+ GL.DrawElements(GL::TRIANGLES, geometry_group.face_count, type, 0)
19
+ end
20
+
21
+ @renderer.info[:render][:calls] += 1
22
+ @renderer.info[:render][:vertices] += geometry_group.face_count
23
+ @renderer.info[:render][:faces] += geometry_group.face_count / 3
24
+ end
25
+
26
+ def update
27
+ # check all geometry groubs
28
+ mat = nil
29
+ geometry.groups.each do |geometry_group|
30
+ # TODO: place to put this???
31
+ # GL.BindVertexArray(geometry_group.vertex_array_object)
32
+ mat = buffer_material(geometry_group)
33
+
34
+ custom_attributes_dirty = mat.attributes && mat.custom_attributes_dirty?
35
+
36
+ if geometry.vertices_need_update || geometry.morph_targets_need_update || geometry.elements_need_update || geometry.uvs_need_update || geometry.normals_need_update || geometry.colors_need_update || geometry.tangents_need_update || custom_attributes_dirty
37
+ geometry_group.set_mesh_buffers(self, GL::DYNAMIC_DRAW, !geometry.dynamic, mat)
38
+ end
39
+ end
40
+
41
+ geometry.vertices_need_update = false
42
+ geometry.morph_targets_need_update = false
43
+ geometry.elements_need_update = false
44
+ geometry.uvs_need_update = false
45
+ geometry.normals_need_update = false
46
+ geometry.colors_need_update = false
47
+ geometry.tangents_need_update = false
48
+
49
+ mat.attributes && mat.clear_custom_attributes
50
+ end
51
+
52
+ def init_geometry
53
+ geometry.renderer = @renderer
54
+ geometry.init_geometry_groups(self)
55
+ end
56
+
57
+ def add_opengl_object
58
+ case geometry
59
+ when BufferGeometry
60
+ @renderer.add_opengl_object(geometry, self)
61
+ when Geometry
62
+ geometry.groups.each do |group|
63
+ @renderer.add_opengl_object(group, self)
64
+ end
65
+ else
66
+ raise "GEOMETRY IS NULL"
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,39 @@
1
+ module Mittsu
2
+ class PointCloud
3
+ def render_buffer(camera, lights, fog, material, geometry_group, update_buffers)
4
+ GL.DrawArrays(GL::POINTS, 0, geometry_group.particle_count)
5
+
6
+ @renderer.info[:render][:calls] += 1
7
+ @renderer.info[:render][:points] += geometry_group.particle_count
8
+ end
9
+
10
+ def update
11
+ material = buffer_material(geometry)
12
+ custom_attributes_dirty = material.attributes && material.custom_attributes_dirty?
13
+
14
+ if geometry.vertices_need_update || geometry.colors_need_update || custom_attributes_dirty
15
+ geometry.set_particle_buffers(GL::DYNAMIC_DRAW)
16
+ end
17
+
18
+ geometry.vertices_need_update = false
19
+ geometry.colors_need_update = false
20
+
21
+ material.attributes && material.clear_custom_attributes
22
+ end
23
+
24
+ def init_geometry
25
+ geometry.renderer = @renderer
26
+ if geometry.vertex_buffer.nil?
27
+ geometry.create_particle_buffers
28
+ geometry.init_particle_buffers(self)
29
+
30
+ geometry.vertices_need_update = true
31
+ geometry.colors_need_update = true
32
+ end
33
+ end
34
+
35
+ def add_opengl_object
36
+ @renderer.add_opengl_object(geometry, self)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,12 @@
1
+ module Mittsu
2
+ class Sprite
3
+ def project(renderer)
4
+ @renderer = renderer
5
+ return unless visible
6
+ init
7
+ # TODO!!! FIXME!!!
8
+ @renderer.instance_variable_get(:@sprites) << self
9
+ project_children
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Mittsu
2
+ class Scene
3
+ def project(renderer)
4
+ @renderer = renderer
5
+ return unless visible
6
+ project_children
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module Mittsu
2
+ class CompressedTexture
3
+ def update_specific
4
+ gl_format = GL::MITTSU_PARAMS[format]
5
+ gl_type = GL::MITTSU_PARAMS[type]
6
+
7
+ mipmaps.each_with_index do |mipmap, i|
8
+ if format != RGBAFormat && format != RGBFormat
9
+ if @renderer.compressed_texture_formats.include?(gl_format)
10
+ GL.CompressedTexImage2D(GL::TEXTURE_2D, i, gl_format, mipmap.width, mipmap.height, 0, mipmap.data)
11
+ else
12
+ puts 'WARNING: Mittsu::Texture: Attempt to load unsupported compressed texture format in #update_texture'
13
+ end
14
+ else
15
+ GL.TexImage2D(GL::TEXTURE_2D, i, gl_format, mipmap.width, mipmap.height, 0, gl_format, gl_type, mipmap.data)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,77 @@
1
+ module Mittsu
2
+ class CubeTexture
3
+ def set(slot, renderer)
4
+ @renderer = renderer
5
+
6
+ if image.length == 6
7
+ if needs_update?
8
+ if !image[:_opengl_texture_cube]
9
+ add_event_listener(:dispose, @renderer.method(:on_texture_dispose))
10
+ image[:_opengl_texture_cube] = GL.CreateTexture
11
+ @renderer.info[:memory][:textures] += 1
12
+ end
13
+
14
+ GL.ActiveTexture(GL::TEXTURE0 + slot)
15
+ GL.BindTexture(GL::TEXTURE_CUBE_MAP, image[:_opengl_texture_cube])
16
+
17
+ # GL.PixelStorei(GL::UNPACK_FLIP_Y_WEBGL, texture.flip_y)
18
+
19
+ is_compressed = is_a?(CompressedTexture)
20
+ is_data_texture = image[0].is_a?(DataTexture)
21
+
22
+ cube_image = [];
23
+
24
+ 6.times do |i|
25
+ if @auto_scale_cubemaps && !is_compressed && !is_data_texture
26
+ cube_image[i] = clamp_to_max_size(image[i], @_max_cubemap_size)
27
+ else
28
+ cube_image[i] = is_data_texture ? image[i].image : image[i];
29
+ end
30
+ end
31
+
32
+ img = cube_image[0]
33
+ is_image_power_of_two = Math.power_of_two?(img.width) && Math.power_of_two?(img.height)
34
+ gl_format = GL::MITTSU_PARAMS[format]
35
+ gl_type = GL::MITTSU_PARAMS[type]
36
+
37
+ set_parameters(GL::TEXTURE_CUBE_MAP, is_image_power_of_two)
38
+
39
+ 6.times do |i|
40
+ if !is_compressed
41
+ if is_data_texture
42
+ GL.TexImage2D(GL::TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl_format, cube_image[i].width, cube_image[i].height, 0, gl_format, gl_type, cube_image[i].data)
43
+ else
44
+ GL.TexImage2D(GL::TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl_format, cube_image[i].width, cube_image[i].height, 0, gl_format, gl_type, cube_image[i].data)
45
+ end
46
+ else
47
+ mipmaps = cube_image[i].mipmaps
48
+
49
+ mipmaps.each_with_index do |mipmap, j|
50
+ if format != RGBAFormat && format != RGBFormat
51
+ if @renderer.compressed_texture_formats.include?(gl_format)
52
+ GL.CompressedTexImage2D(GL::TEXTURE_CUBE_MAP_POSITIVE_X + i, j, gl_format, mipmap.width, mipmap.height, 0, mipmap.data)
53
+ else
54
+ puts "WARNING: Mittsu::OpenGLCubeTexture: Attempt to load unsupported compressed texture format in #set"
55
+ end
56
+ else
57
+ GL.TexImage2D(GL::TEXTURE_CUBE_MAP_POSITIVE_X + i, j, gl_format, mipmap.width, mipmap.height, 0, gl_format, gl_type, mipmap.data)
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ if generate_mipmaps && is_image_power_of_two
64
+ GL.GenerateMipmap(GL::TEXTURE_CUBE_MAP)
65
+ end
66
+
67
+ self.needs_update = false
68
+
69
+ on_update.call if on_update
70
+ else
71
+ GL.ActiveTexture(GL::TEXTURE0 + slot)
72
+ GL.BindTexture(GL::TEXTURE_CUBE_MAP, image[:_opengl_texture_cube])
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,21 @@
1
+ module Mittsu
2
+ class DataTexture
3
+ def update_specific
4
+ gl_format = GL::MITTSU_PARAMS[format]
5
+ gl_type = GL::MITTSU_PARAMS[type]
6
+ is_image_power_of_two = Math.power_of_two?(image.width) && Math.power_of_two?(image.height)
7
+
8
+ # use manually created mipmaps if available
9
+ # if there are no manual mipmaps
10
+ # set 0 level mipmap and then use GL to generate other mipmap levels
11
+
12
+ if !mipmaps.empty? && is_image_power_of_two
13
+ mipmaps.each_with_index do |mipmap, i|
14
+ GL.TexImage2D(GL::TEXTURE_2D, i, gl_format, mipmap.width, mipmap.height, 0, gl_format, gl_type, mipmap.data)
15
+ end
16
+ else
17
+ GL.TexImage2D(GL::TEXTURE_2D, 0, gl_format, image.width, image.height, 0, gl_format, gl_type, image.data)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,124 @@
1
+ module Mittsu
2
+ class RenderTarget < Texture
3
+ attr_writer :renderer
4
+
5
+ alias :original_clone :clone
6
+ def clone
7
+ original_clone.tap do |tmp|
8
+ tmp.renderer = @renderer
9
+ end
10
+ end
11
+
12
+ def setup_buffers
13
+ return unless @framebuffer.nil?
14
+
15
+ # TODO: when OpenGLRenderTargetCube exists
16
+ is_cube = false # render_target.is_a? OpenGLRenderTargetCube
17
+
18
+ @depth_buffer = true if @depth_buffer.nil?
19
+ @stencil_buffer = true if @stencil_buffer.nil?
20
+
21
+ add_event_listener(:dispose, @renderer.method(:on_render_target_dispose))
22
+
23
+ @opengl_texture = GL.CreateTexture
24
+
25
+ @renderer.info[:memory][:textures] += 1
26
+
27
+ # Setup texture, create render and frame buffers
28
+
29
+ is_target_power_of_two = Math.power_of_two?(@width) && Math.power_of_two?(@height)
30
+ gl_format = GL::MITTSU_PARAMS[@format]
31
+ gl_type = GL::MITTSU_PARAMS[@type]
32
+
33
+ if is_cube
34
+ # TODO
35
+ else
36
+ @framebuffer = GL.CreateFramebuffer
37
+
38
+ if @share_depth_from
39
+ @renderbuffer = share_depth_from.renderbuffer
40
+ else
41
+ @renderbuffer = GL.CreateRenderbuffer
42
+ end
43
+
44
+ GL.BindTexture(GL::TEXTURE_2D, @opengl_texture)
45
+ set_parameters(GL::TEXTURE_2D, is_target_power_of_two)
46
+
47
+ GL.TexImage2D(GL::TEXTURE_2D, 0, gl_format, @width, @height, 0, gl_format, gl_type, nil)
48
+
49
+ setup_framebuffer(GL::TEXTURE_2D)
50
+
51
+ if @share_depth_from
52
+ if @depth_buffer && !@stencil_buffer
53
+ GL.FramebufferRenderbuffer(GL::FRAMEBUFFER, GL::DEPTH_ATTACHMENT, GL::RENDERBUFFER, @renderbuffer)
54
+ elsif @depth_buffer && @stencil_buffer
55
+ GL.FramebufferRenderbuffer(GL::FRAMEBUFFER, GL::DEPTH_STENCIL_ATTACHMENT, GL::RENDERBUFFER, @renderbuffer)
56
+ end
57
+ else
58
+ setup_renderbuffer
59
+ end
60
+
61
+ GL.GenerateMipmap(GL::TEXTURE_2D) if is_target_power_of_two
62
+ end
63
+
64
+ # Release everything
65
+
66
+ if is_cube
67
+ # TODO
68
+ else
69
+ GL.BindTexture(GL::TEXTURE_2D, 0)
70
+ end
71
+
72
+ GL.BindRenderbuffer(GL::RENDERBUFFER, 0)
73
+ GL.BindFramebuffer(GL::FRAMEBUFFER, 0)
74
+ end
75
+
76
+ def use
77
+ GL.BindFramebuffer(GL::FRAMEBUFFER, @framebuffer)
78
+ GL.Viewport(0, 0, @width, @height)
79
+ end
80
+
81
+ def dispose
82
+ dispatch_event(type: :dispose)
83
+ end
84
+
85
+ def update_mipmap
86
+ return if !@generate_mipmaps || @min_filter == NearestFilter || @min_filter == LinearFilter
87
+ # TODO: when OpenGLRenderTargetCube exists
88
+ # GL.BindTexture(GL::TEXTURE_CUBE_MAP, @opengl_texture)
89
+ # GL.GenerateMipmap(GL::TEXTURE_CUBE_MAP)
90
+ # GL.BindTexture(GL::TEXTURE_CUBE_MAP, nil)
91
+ GL.BindTexture(GL::TEXTURE_2D, @opengl_texture)
92
+ GL.GenerateMipmap(GL::TEXTURE_2D)
93
+ GL.BindTexture(GL::TEXTURE_2D, nil)
94
+ end
95
+
96
+ private
97
+
98
+ def setup_framebuffer(texture_target)
99
+ GL.BindFramebuffer(GL::FRAMEBUFFER, @framebuffer)
100
+ GL.FramebufferTexture2D(GL::FRAMEBUFFER, GL::COLOR_ATTACHMENT0, texture_target, @opengl_texture, 0)
101
+ end
102
+
103
+ def setup_renderbuffer
104
+ GL.BindRenderbuffer(GL::RENDERBUFFER, @renderbuffer)
105
+
106
+ if @depth_buffer && !@stencil_buffer
107
+ GL.RenderbufferStorage(GL::RENDERBUFFER, GL::DEPTH_COMPONENT16, @width, @height)
108
+ GL.FramebufferRenderbuffer(GL::FRAMEBUFFER, GL::DEPTH_ATTACHMENT, GL::RENDERBUFFER, @renderbuffer)
109
+
110
+ # TODO: investigate this (?):
111
+ # THREE.js - For some reason this is not working. Defaulting to RGBA4.
112
+ # } else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
113
+ #
114
+ # _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
115
+ # _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
116
+ elsif @depth_buffer && @stencil_buffer
117
+ GL.RenderbufferStorage(GL::RENDERBUFFER, GL::DEPTH_STENCIL, @width, @height)
118
+ GL.FramebufferRenderbuffer(GL::FRAMEBUFFER, GL::DEPTH_STENCIL_ATTACHMENT, GL::RENDERBUFFER, @renderbuffer)
119
+ else
120
+ GL.RenderbufferStorage(GL::RENDERBUFFER, GL::RGBA4, @width, @height)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,107 @@
1
+ module Mittsu
2
+ class Texture
3
+ attr_reader :opengl_texture
4
+
5
+ def set(slot, renderer)
6
+ @renderer = renderer
7
+
8
+ GL.ActiveTexture(GL::TEXTURE0 + slot)
9
+
10
+ if needs_update?
11
+ update_opengl(@renderer)
12
+ else
13
+ GL.BindTexture(GL::TEXTURE_2D, @opengl_texture)
14
+ end
15
+ end
16
+
17
+ def update_opengl(renderer)
18
+ @renderer = renderer
19
+
20
+ if !@initted
21
+ @initted = true
22
+ add_event_listener(:dispose, @renderer.method(:on_texture_dispose))
23
+ @opengl_texture = GL.CreateTexture
24
+ @renderer.info[:memory][:textures] += 1
25
+ end
26
+
27
+ GL.BindTexture(GL::TEXTURE_2D, @opengl_texture)
28
+
29
+ # GL.PixelStorei(GL::UNPACK_FLIP_Y_WEBGL, flip_y) ???
30
+ # GL.PixelStorei(GL::UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiply_alpha) ???
31
+ GL.PixelStorei(GL::UNPACK_ALIGNMENT, unpack_alignment)
32
+
33
+ self.image = @renderer.clamp_to_max_size(image)
34
+
35
+ is_image_power_of_two = Math.power_of_two?(image.width) && Math.power_of_two?(image.height)
36
+
37
+ set_parameters(GL::TEXTURE_2D, is_image_power_of_two)
38
+
39
+ update_specific
40
+
41
+ if generate_mipmaps && is_image_power_of_two
42
+ GL.GenerateMipmap(GL::TEXTURE_2D)
43
+ end
44
+
45
+ self.needs_update = false
46
+
47
+ on_update.call if on_update
48
+ end
49
+
50
+ protected
51
+
52
+ def set_parameters(texture_type, is_image_power_of_two)
53
+ if is_image_power_of_two
54
+ GL.TexParameteri(texture_type, GL::TEXTURE_WRAP_S, GL::MITTSU_PARAMS[wrap_s])
55
+ GL.TexParameteri(texture_type, GL::TEXTURE_WRAP_T, GL::MITTSU_PARAMS[wrap_t])
56
+
57
+ GL.TexParameteri(texture_type, GL::TEXTURE_MAG_FILTER, GL::MITTSU_PARAMS[mag_filter])
58
+ GL.TexParameteri(texture_type, GL::TEXTURE_MIN_FILTER, GL::MITTSU_PARAMS[min_filter])
59
+ else
60
+ GL.TexParameteri(texture_type, GL::TEXTURE_WRAP_S, GL::CLAMP_TO_EDGE)
61
+ GL.TexParameteri(texture_type, GL::TEXTURE_WRAP_T, GL::CLAMP_TO_EDGE)
62
+
63
+ if wrap_s != ClampToEdgeWrapping || wrap_t != ClampToEdgeWrapping
64
+ puts "WARNING: Mittsu::Texture: Texture is not power of two. Texture.wrap_s and Texture.wrap_t should be set to Mittsu::ClampToEdgeWrapping. (#{source_file})"
65
+ end
66
+
67
+ GL.TexParameteri(texture_type, GL::TEXTURE_MAG_FILTER, filter_fallback(mag_filter))
68
+ GL.TexParameteri(texture_type, GL::TEXTURE_MIN_FILTER, filter_fallback(min_filter))
69
+
70
+ if min_filter != NearestFilter && min_filter != LinearFilter
71
+ puts "WARNING: Mittsu::Texture: Texture is not a power of two. Texture.min_filter should be set to Mittsu::NearestFilter or Mittsu::LinearFilter. (#{source_file})"
72
+ end
73
+
74
+ # TODO: anisotropic extension ???
75
+ end
76
+ end
77
+
78
+ # Fallback filters for non-power-of-2 textures
79
+ def filter_fallback(filter)
80
+ if filter == NearestFilter || filter == NearestMipMapNearestFilter || filter == NearestMipMapLinearFilter
81
+ GL::NEAREST
82
+ end
83
+
84
+ GL::LINEAR
85
+ end
86
+
87
+ def update_specific
88
+ gl_format = GL::MITTSU_PARAMS[format]
89
+ gl_type = GL::MITTSU_PARAMS[type]
90
+ is_image_power_of_two = Math.power_of_two?(image.width) && Math.power_of_two?(image.height)
91
+
92
+ # use manually created mipmaps if available
93
+ # if there are no manual mipmaps
94
+ # set 0 level mipmap and then use GL to generate other mipmap levels
95
+
96
+ if !mipmaps.empty? && is_image_power_of_two
97
+ mipmaps.each_with_index do |mipmap, i|
98
+ GL.TexImage2D(GL::TEXTURE_2D, i, gl_format, mipmap.width, mipmap.height, 0, gl_format, gl_type, mipmap.data)
99
+ end
100
+
101
+ self.generate_mipmaps = false
102
+ else
103
+ GL.TexImage2D(GL::TEXTURE_2D, 0, gl_format, image.width, image.height, 0, gl_format, gl_type, image.data)
104
+ end
105
+ end
106
+ end
107
+ end