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,132 @@
1
+ module Mittsu
2
+ module OpenGL::GeometryLike
3
+ CONST_BUFFER_NAMES = [
4
+ :vertex,
5
+ :color,
6
+ :normal,
7
+ :tangent,
8
+ :uv,
9
+ :uv2,
10
+ :skin_indices,
11
+ :skin_weight,
12
+ :line_distance,
13
+ :face,
14
+ :line
15
+ ]
16
+ attr_accessor(*CONST_BUFFER_NAMES.map(&:to_s).map { |name| "#{name}_buffer" }.map(&:to_sym))
17
+ attr_accessor(*CONST_BUFFER_NAMES.map(&:to_s).map { |name| "#{name}_array" }.map(&:to_sym))
18
+
19
+ attr_accessor :vertex_array_object,
20
+ :num_morph_targets,
21
+ :num_morph_normals,
22
+ :morph_targets_buffers,
23
+ :morph_normals_buffers,
24
+ :morph_targets_arrays,
25
+ :morph_normals_arrays,
26
+ :faces3,
27
+ :type_array,
28
+ :face_count,
29
+ :line_count,
30
+ :particle_count,
31
+ :initted_arrays,
32
+ :custom_attributes_list,
33
+ :num_vertices,
34
+ :renderer
35
+
36
+ def bind_vertex_array_object
37
+ GL.BindVertexArray(@vertex_array_object) if @vertex_array_object
38
+ end
39
+
40
+ def update_vertex_buffer(attribute)
41
+ GL.BindBuffer(GL::ARRAY_BUFFER, @vertex_buffer)
42
+ @renderer.state.enable_attribute(attribute)
43
+ GL.VertexAttribPointer(attribute, 3, GL::FLOAT, GL::FALSE, 0, 0)
44
+ end
45
+
46
+ def update_other_buffers(object, material, attributes)
47
+ update_custom_attributes(attributes)
48
+
49
+ update_color_buffer(attributes['color'], object, material)
50
+ update_normal_buffer(attributes['normal'])
51
+ update_tangent_buffer(attributes['tangent'])
52
+ update_uv_buffers(attributes['uv'], attributes['uv2'], object, material)
53
+
54
+ if material.skinning
55
+ update_skin_buffers(attributes['skin_index'], attributes['skin_weight'])
56
+ end
57
+
58
+ update_line_distances_buffer(attributes['line_distances'])
59
+ end
60
+
61
+ private
62
+
63
+ def attribute_exists?(attribute)
64
+ attribute && attribute >= 0
65
+ end
66
+
67
+ def update_color_buffer(attribute, object, material)
68
+ return unless attribute_exists?(attribute)
69
+ if object.geometry.colors.length > 0 || object.geometry.faces.length > 0
70
+ update_attribute(attribute, @color_buffer, 3)
71
+ elsif material.default_attribute_values
72
+ GL.VertexAttrib3fv(attribute, material.default_attribute_values.color)
73
+ end
74
+ end
75
+
76
+ def update_normal_buffer(attribute)
77
+ return unless attribute_exists?(attribute)
78
+ update_attribute(attribute, @normal_buffer, 3)
79
+ end
80
+
81
+ def update_tangent_buffer(attribute)
82
+ return unless attribute_exists?(attribute)
83
+ update_attribute(attribute, @tangent_buffer, 4)
84
+ end
85
+
86
+ def update_uv_buffers(uv_attribute, uv2_attribute, object, material)
87
+ update_uv_buffer(uv_attribute, @uv_buffer, object, 0)
88
+ update_uv_buffer(uv2_attribute, @uv2_buffer, object, 1)
89
+ end
90
+
91
+ def update_uv_buffer(attribute, buffer, object, index)
92
+ return unless attribute_exists?(attribute)
93
+ if object.geometry.face_vertex_uvs[index]
94
+ update_attribute(attribute, buffer, 2)
95
+ else
96
+ # TODO default_attribute_value ???
97
+ # GL.VertexAttrib2fv(attribute, default_attribute_value)
98
+ end
99
+ end
100
+
101
+ def update_skin_buffers(index_attribute, weight_attribute)
102
+ return unless attribute_exists?(index_attribute) && attribute_exists?(weight_attribute)
103
+ update_attribute(attribute, @skin_indices_buffer, 4)
104
+ update_attribute(attribute, @skin_weight_buffer, 4)
105
+ end
106
+
107
+ def update_line_distances_buffer(attribute)
108
+ return unless attribute_exists?(attribute)
109
+ update_attribute(attribute, @line_distance_buffer, 1)
110
+ end
111
+
112
+ def update_custom_attributes(attributes)
113
+ if @custom_attributes_list
114
+ @custom_attributes_list.each do |custom_attribute|
115
+ belongs_to_attribute = attributes[custom_attribute.buffer.belongs_to_attribute]
116
+ update_custom_attribute(custom_attribute, belongs_to_attribute)
117
+ end
118
+ end
119
+ end
120
+
121
+ def update_custom_attribute(custom_attribute, belongs_to_attribute)
122
+ return unless attribute_exists?(belongs_to_attribute)
123
+ update_attribute(attribute, custom_attribute.buffer, custom_attribute.size)
124
+ end
125
+
126
+ def update_attribute(attribute, buffer, size)
127
+ GL.BindBuffer(GL::ARRAY_BUFFER, buffer)
128
+ @renderer.state.enable_attribute(attribute)
129
+ GL.VertexAttribPointer(attribute, size, GL::FLOAT, GL::FALSE, 0, 0)
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,85 @@
1
+ require 'opengl'
2
+
3
+ ERROR_STRINGS = {
4
+ GL::NO_ERROR => 'NO_ERROR',
5
+ GL::INVALID_ENUM => 'INVALID_ENUM',
6
+ GL::INVALID_VALUE => 'INVALID_VALUE',
7
+ GL::INVALID_OPERATION => 'INVALID_OPERATION',
8
+ GL::STACK_OVERFLOW => 'STACK_OVERFLOW',
9
+ GL::STACK_UNDERFLOW => 'STACK_UNDERFLOW',
10
+ GL::OUT_OF_MEMORY => 'OUT_OF_MEMORY',
11
+ # GL::TABLE_TOO_LARGE => 'TABLE_TOO_LARGE'
12
+ }
13
+
14
+ OriginalGL = GL
15
+
16
+ module OpenGLDebug
17
+ class DebugShader
18
+ def initialize(handle)
19
+ @handle = handle
20
+ @uniforms = {}
21
+ end
22
+
23
+ def set_uniform(handle, name)
24
+ @uniforms[handle] = name
25
+ end
26
+
27
+ def get_uniform_name(handle)
28
+ @uniforms[handle]
29
+ end
30
+ end
31
+
32
+ def self.load_lib(*args)
33
+ OriginalGL.load_lib(*args)
34
+ end
35
+
36
+ OriginalGL.constants.each do |c|
37
+ const_set c, OriginalGL.const_get(c)
38
+ end
39
+
40
+ class << self
41
+ OriginalGL::GL_FUNCTION_SYMBOLS
42
+ .map { |m| m.to_s.gsub(/^gl/, '').to_sym }
43
+ .each do |m|
44
+ define_method(m) do |*args|
45
+ call_debug_method(m, caller[0], *args)
46
+ end
47
+ end
48
+
49
+ def call_debug_method m, called_from = caller[0], *args
50
+ if m.to_s.start_with?('Uniform')
51
+ uniform_name = @@current_shader.get_uniform_name(args.first)
52
+ call = "#{m}('#{uniform_name}',#{args[1..-1].map { |s| s.to_s[0..20] }.join(', ')})"
53
+ else
54
+ call = "#{m}(#{args.map { |s| s.to_s[0..20] }.join(', ')})"
55
+ end
56
+ print call
57
+ r = OriginalGL.send(m, *args)
58
+ ret = r.nil? ? '' : " => #{r}"
59
+ puts "#{ret} (#{called_from})"
60
+ e = OriginalGL.GetError
61
+ raise "ERROR: #{m} => #{ERROR_STRINGS[e]}" unless e == GL::NO_ERROR
62
+ r
63
+ end
64
+
65
+ def CreateProgram
66
+ call_debug_method(:CreateProgram, caller[0]).tap do |handle|
67
+ @@shaders ||= {}
68
+ @@shaders[handle] = DebugShader.new(handle)
69
+ end
70
+ end
71
+
72
+ def UseProgram(handle)
73
+ @@current_shader = @@shaders[handle]
74
+ call_debug_method(:UseProgram, caller[0], handle)
75
+ end
76
+
77
+ def GetUniformLocation(program, name)
78
+ call_debug_method(:GetUniformLocation, caller[0], program, name).tap do |handle|
79
+ @@shaders[program].set_uniform(handle, name)
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ GL = OpenGLDebug
@@ -0,0 +1,42 @@
1
+ module GL
2
+ def self.CreateBuffer
3
+ @_b ||= ' '*8
4
+ ::GL.GenBuffers(1, @_b)
5
+ @_b.unpack('L')[0]
6
+ end
7
+
8
+ def self.CreateTexture
9
+ @_b ||= ' '*8
10
+ ::GL.GenTextures(1, @_b)
11
+ @_b.unpack('L')[0]
12
+ end
13
+
14
+ def self.CreateVertexArray
15
+ @_b ||= ' '*8
16
+ ::GL.GenVertexArrays(1, @_b)
17
+ @_b.unpack('L')[0]
18
+ end
19
+
20
+ def self.CreateFramebuffer
21
+ @_b ||= ' '*8
22
+ ::GL.GenFramebuffers(1, @_b)
23
+ @_b.unpack('L')[0]
24
+ end
25
+
26
+ def self.CreateRenderbuffer
27
+ @_b ||= ' '*8
28
+ ::GL.GenRenderbuffers(1, @_b)
29
+ @_b.unpack('L')[0]
30
+ end
31
+
32
+ def self.BufferData_easy(target, data, usage)
33
+ ptr = array_to_ptr_easy(data)
34
+ ::GL.BufferData(target, ptr.size, ptr, usage)
35
+ end
36
+
37
+ def self.GetParameter(pname)
38
+ @_b ||= ' '*8
39
+ ::GL.GetIntegerv(pname, @_b)
40
+ @_b.unpack('L')[0]
41
+ end
42
+ end
@@ -0,0 +1,53 @@
1
+ module Mittsu
2
+ GL::MITTSU_PARAMS = {
3
+ RepeatWrapping => GL::REPEAT,
4
+ ClampToEdgeWrapping => GL::CLAMP_TO_EDGE,
5
+ MirroredRepeatWrapping => GL::MIRRORED_REPEAT,
6
+
7
+ NearestFilter => GL::NEAREST,
8
+ NearestMipMapNearestFilter => GL::NEAREST_MIPMAP_NEAREST,
9
+ NearestMipMapLinearFilter => GL::NEAREST_MIPMAP_LINEAR,
10
+
11
+ LinearFilter => GL::LINEAR,
12
+ LinearMipMapNearestFilter => GL::LINEAR_MIPMAP_NEAREST,
13
+ LinearMipMapLinearFilter => GL::LINEAR_MIPMAP_LINEAR,
14
+
15
+ UnsignedByteType => GL::UNSIGNED_BYTE,
16
+ UnsignedShort4444Type => GL::UNSIGNED_SHORT_4_4_4_4,
17
+ UnsignedShort5551Type => GL::UNSIGNED_SHORT_5_5_5_1,
18
+ UnsignedShort565Type => GL::UNSIGNED_SHORT_5_6_5,
19
+
20
+ ByteType => GL::BYTE,
21
+ ShortType => GL::SHORT,
22
+ UnsignedShortType => GL::UNSIGNED_SHORT,
23
+ IntType => GL::INT,
24
+ UnsignedIntType => GL::UNSIGNED_INT,
25
+ FloatType => GL::FLOAT,
26
+
27
+ AlphaFormat => GL::ALPHA,
28
+ RGBFormat => GL::RGB,
29
+ RGBAFormat => GL::RGBA,
30
+ LuminanceFormat => GL::LUMINANCE,
31
+ LuminanceAlphaFormat => GL::LUMINANCE_ALPHA,
32
+
33
+ AddEquation => GL::FUNC_ADD,
34
+ SubtractEquation => GL::FUNC_SUBTRACT,
35
+ ReverseSubtractEquation => GL::FUNC_REVERSE_SUBTRACT,
36
+
37
+ ZeroFactor => GL::ZERO,
38
+ OneFactor => GL::ONE,
39
+ SrcColorFactor => GL::SRC_COLOR,
40
+ OneMinusSrcColorFactor => GL::ONE_MINUS_SRC_COLOR,
41
+ SrcAlphaFactor => GL::SRC_ALPHA,
42
+ OneMinusSrcAlphaFactor => GL::ONE_MINUS_SRC_ALPHA,
43
+ DstAlphaFactor => GL::DST_ALPHA,
44
+ OneMinusDstAlphaFactor => GL::ONE_MINUS_DST_ALPHA,
45
+
46
+ DstColorFactor => GL::DST_COLOR,
47
+ OneMinusDstColorFactor => GL::ONE_MINUS_DST_COLOR,
48
+ SrcAlphaSaturateFactor => GL::SRC_ALPHA_SATURATE
49
+
50
+ # TODO: populate with extension parameters?
51
+ }
52
+ GL::MITTSU_PARAMS.default = 0
53
+ end
@@ -0,0 +1,120 @@
1
+ module Mittsu
2
+ module OpenGL::Helper
3
+ def array_to_ptr_easy(data)
4
+ if data.first.is_a?(Float)
5
+ size_of_element = Fiddle::SIZEOF_FLOAT
6
+ format_of_element = 'F'
7
+ # data.map!{ |d| d.nil? ? 0.0 : d }
8
+ else
9
+ size_of_element = Fiddle::SIZEOF_INT
10
+ format_of_element = 'L'
11
+ # data.map!{ |d| d.nil? ? 0 : d }
12
+ end
13
+ size = data.length * size_of_element
14
+ array_to_ptr(data, size, format_of_element)
15
+ end
16
+
17
+ def array_to_ptr(data, size, format)
18
+ ptr = Fiddle::Pointer.malloc(size)
19
+ ptr[0,size] = data.pack(format * data.length)
20
+ ptr
21
+ end
22
+
23
+ class << self
24
+ def mark_uniforms_lights_needs_update(uniforms, value)
25
+ uniforms['ambientLightColor'].needs_update = value
26
+
27
+ uniforms['directionalLightColor'].needs_update = value
28
+ uniforms['directionalLightDirection'].needs_update = value
29
+
30
+ uniforms['pointLightColor'].needs_update = value
31
+ uniforms['pointLightPosition'].needs_update = value
32
+ uniforms['pointLightDistance'].needs_update = value
33
+ uniforms['pointLightDecay'].needs_update = value
34
+
35
+ uniforms['spotLightColor'].needs_update = value
36
+ uniforms['spotLightPosition'].needs_update = value
37
+ uniforms['spotLightDistance'].needs_update = value
38
+ uniforms['spotLightDirection'].needs_update = value
39
+ uniforms['spotLightAngleCos'].needs_update = value
40
+ uniforms['spotLightExponent'].needs_update = value
41
+ uniforms['spotLightDecay'].needs_update = value
42
+
43
+ uniforms['hemisphereLightSkyColor'].needs_update = value
44
+ uniforms['hemisphereLightGroundColor'].needs_update = value
45
+ uniforms['hemisphereLightDirection'].needs_update = value
46
+ end
47
+
48
+ def refresh_uniforms_shadow(uniforms, lights)
49
+ if uniforms['shadowMatrix']
50
+ lights.select(&:cast_shadow).select { |light|
51
+ light.is_a?(SpotLight) || (light.is_a?(DirectionalLight) && !light.shadow_cascade)
52
+ }.each_with_index { |light, i|
53
+ uniforms['shadowMap'].value[i] = light.shadow_map
54
+ uniforms['shadowMapSize'].value[i] = light.shadow_map_size
55
+
56
+ uniforms['shadowMatrix'].value[i] = light.shadow_matrix
57
+
58
+ uniforms['shadowDarkness'].value[i] = light.shadow_darkness
59
+ uniforms['shadowBias'].value[i] = light.shadow_bias
60
+ }
61
+ end
62
+ end
63
+
64
+ def refresh_uniforms_lights(uniforms, lights)
65
+ uniforms['ambientLightColor'].value = lights[:ambient].value
66
+
67
+ uniforms['directionalLightColor'].value = lights[:directional].colors
68
+ uniforms['directionalLightDirection'].value = lights[:directional].positions
69
+
70
+ uniforms['pointLightColor'].value = lights[:point].colors
71
+ uniforms['pointLightPosition'].value = lights[:point].positions
72
+ uniforms['pointLightDistance'].value = lights[:point].distances
73
+ uniforms['pointLightDecay'].value = lights[:point].decays
74
+
75
+ uniforms['spotLightColor'].value = lights[:spot].colors
76
+ uniforms['spotLightPosition'].value = lights[:spot].positions
77
+ uniforms['spotLightDistance'].value = lights[:spot].distances
78
+ uniforms['spotLightDirection'].value = lights[:spot].directions
79
+ uniforms['spotLightAngleCos'].value = lights[:spot].angles_cos
80
+ uniforms['spotLightExponent'].value = lights[:spot].exponents
81
+ uniforms['spotLightDecay'].value = lights[:spot].decays
82
+
83
+ uniforms['hemisphereLightSkyColor'].value = lights[:hemi].sky_colors
84
+ uniforms['hemisphereLightGroundColor'].value = lights[:hemi].ground_colors
85
+ uniforms['hemisphereLightDirection'].value = lights[:hemi].positions
86
+ end
87
+
88
+ def refresh_uniforms_lambert(uniforms, material)
89
+ end
90
+
91
+ def set_color_linear(array, offset, color, intensity)
92
+ array[offset] = color.r * intensity
93
+ array[offset + 1] = color.g * intensity
94
+ array[offset + 2] = color.b * intensity
95
+ end
96
+
97
+ def painter_sort_stable(a, b)
98
+ if a.object.render_order != b.object.render_order
99
+ a.object.render_order <=> b.object.render_order
100
+ elsif a.material.id != b.material.id
101
+ a.material.id <=> b.material.id
102
+ elsif a.z != b.z
103
+ a.z <=> b.z
104
+ else
105
+ a.object.id <=> b.object.id
106
+ end
107
+ end
108
+
109
+ def reverse_painter_sort_stable(a, b)
110
+ if a.object.render_order != b.object.render_order
111
+ a.object.render_order <=> b.object.render_order
112
+ elsif a.z != b.z
113
+ b.z <=> a.z
114
+ else
115
+ b.id <=> a.id
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,31 @@
1
+ require 'mittsu/opengl_implementation/core/geometry'
2
+ require 'mittsu/opengl_implementation/core/object_3d'
3
+ require 'mittsu/opengl_implementation/core/buffer_geometry'
4
+ require 'mittsu/opengl_implementation/objects/mesh'
5
+ require 'mittsu/opengl_implementation/objects/line'
6
+ require 'mittsu/opengl_implementation/objects/group'
7
+ require 'mittsu/opengl_implementation/objects/sprite'
8
+ require 'mittsu/opengl_implementation/objects/point_cloud'
9
+ require 'mittsu/opengl_implementation/scenes/scene'
10
+
11
+ require 'mittsu/opengl_implementation/lights/light'
12
+ require 'mittsu/opengl_implementation/lights/spot_light'
13
+ require 'mittsu/opengl_implementation/lights/point_light'
14
+ require 'mittsu/opengl_implementation/lights/hemisphere_light'
15
+ require 'mittsu/opengl_implementation/lights/ambient_light'
16
+ require 'mittsu/opengl_implementation/lights/directional_light'
17
+
18
+ require 'mittsu/opengl_implementation/materials/material'
19
+ require 'mittsu/opengl_implementation/materials/mesh_basic_material'
20
+ require 'mittsu/opengl_implementation/materials/mesh_lambert_material'
21
+ require 'mittsu/opengl_implementation/materials/mesh_phong_material'
22
+ require 'mittsu/opengl_implementation/materials/line_basic_material'
23
+ require 'mittsu/opengl_implementation/materials/shader_material'
24
+ require 'mittsu/opengl_implementation/materials/point_cloud_material'
25
+
26
+ require 'mittsu/opengl_implementation/textures/texture'
27
+ require 'mittsu/opengl_implementation/textures/cube_texture'
28
+ require 'mittsu/opengl_implementation/textures/data_texture'
29
+ require 'mittsu/opengl_implementation/textures/compressed_texture'
30
+ require 'mittsu/opengl_implementation/textures/render_target'
31
+ # require 'mittsu/opengl_implementation/textures/render_target_cube' # TODO
@@ -0,0 +1,19 @@
1
+ require 'mittsu/generic_lib'
2
+
3
+ module Mittsu
4
+ module OpenGL::Lib
5
+ extend GenericLib
6
+
7
+ class Linux < GenericLib::Linux
8
+ def initialize(loader = Linux)
9
+ @loader = loader
10
+ end
11
+ end
12
+
13
+ class Windows < GenericLib::Base
14
+ end
15
+
16
+ class MacOS < GenericLib::Base
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ module Mittsu
2
+ class OpenGL::LightRenderer
3
+ attr_accessor :lights_need_update, :cache
4
+
5
+ LIGHT_CLASSES = [
6
+ AmbientLight,
7
+ DirectionalLight,
8
+ PointLight,
9
+ SpotLight,
10
+ HemisphereLight
11
+ ]
12
+ LIGHT_TYPES = LIGHT_CLASSES.map { |klass| klass::TYPE }
13
+
14
+ def initialize(renderer)
15
+ @renderer = renderer
16
+ @lights_need_update = true
17
+ @cache = {}
18
+ LIGHT_CLASSES.each { |klass|
19
+ @cache[klass::TYPE] = klass::Cache.new
20
+ }
21
+ end
22
+
23
+ def setup(lights)
24
+ @cache.values.each(&:reset)
25
+
26
+ lights.each do |light|
27
+ next if light.only_shadow
28
+ light.setup(self)
29
+ end
30
+
31
+ LIGHT_CLASSES.each do |klass|
32
+ cache = @cache[klass::TYPE]
33
+ klass.null_remaining_lights(cache)
34
+ end
35
+
36
+ @lights_need_update = false
37
+ end
38
+
39
+ def reset
40
+ @lights_need_update = true
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,57 @@
1
+ module Mittsu
2
+ module OpenGL::MaterialBasics
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