mittsu-opengl 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +18 -0
  3. data/.github/workflows/build-workflow.yml +67 -0
  4. data/.gitignore +12 -0
  5. data/.rubocop.yml +1158 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +195 -0
  10. data/Rakefile +8 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +7 -0
  13. data/install-glfw.ps1 +13 -0
  14. data/lib/mittsu/generic_lib.rb +116 -0
  15. data/lib/mittsu/glfw/lib.rb +58 -0
  16. data/lib/mittsu/glfw/window.rb +231 -0
  17. data/lib/mittsu/opengl/buffer.rb +13 -0
  18. data/lib/mittsu/opengl/default_target.rb +50 -0
  19. data/lib/mittsu/opengl/geometry_group.rb +758 -0
  20. data/lib/mittsu/opengl/geometry_like.rb +132 -0
  21. data/lib/mittsu/opengl/gl_debug.rb +85 -0
  22. data/lib/mittsu/opengl/gl_extensions.rb +42 -0
  23. data/lib/mittsu/opengl/gl_mittsu_params.rb +53 -0
  24. data/lib/mittsu/opengl/helper.rb +120 -0
  25. data/lib/mittsu/opengl/implementation.rb +31 -0
  26. data/lib/mittsu/opengl/lib.rb +19 -0
  27. data/lib/mittsu/opengl/light_renderer.rb +43 -0
  28. data/lib/mittsu/opengl/material_basics.rb +57 -0
  29. data/lib/mittsu/opengl/plugins/shadow_map_plugin.rb +416 -0
  30. data/lib/mittsu/opengl/plugins/sprite_fragment.glsl +38 -0
  31. data/lib/mittsu/opengl/plugins/sprite_plugin.rb +250 -0
  32. data/lib/mittsu/opengl/plugins/sprite_vertex.glsl +31 -0
  33. data/lib/mittsu/opengl/program.rb +250 -0
  34. data/lib/mittsu/opengl/renderer.rb +1028 -0
  35. data/lib/mittsu/opengl/shader/chunk.rb +11 -0
  36. data/lib/mittsu/opengl/shader/chunks/alphamap_fragment.glsl +5 -0
  37. data/lib/mittsu/opengl/shader/chunks/alphamap_pars_fragment.glsl +5 -0
  38. data/lib/mittsu/opengl/shader/chunks/alphatest_fragment.glsl +5 -0
  39. data/lib/mittsu/opengl/shader/chunks/bumpmap_pars_fragment.glsl +40 -0
  40. data/lib/mittsu/opengl/shader/chunks/color_fragment.glsl +5 -0
  41. data/lib/mittsu/opengl/shader/chunks/color_pars_fragment.glsl +5 -0
  42. data/lib/mittsu/opengl/shader/chunks/color_pars_vertex.glsl +5 -0
  43. data/lib/mittsu/opengl/shader/chunks/color_vertex.glsl +5 -0
  44. data/lib/mittsu/opengl/shader/chunks/common.glsl +60 -0
  45. data/lib/mittsu/opengl/shader/chunks/default_vertex.glsl +15 -0
  46. data/lib/mittsu/opengl/shader/chunks/defaultnormal_vertex.glsl +21 -0
  47. data/lib/mittsu/opengl/shader/chunks/envmap_fragment.glsl +62 -0
  48. data/lib/mittsu/opengl/shader/chunks/envmap_pars_fragment.glsl +21 -0
  49. data/lib/mittsu/opengl/shader/chunks/envmap_pars_vertex.glsl +7 -0
  50. data/lib/mittsu/opengl/shader/chunks/envmap_vertex.glsl +17 -0
  51. data/lib/mittsu/opengl/shader/chunks/fog_fragment.glsl +26 -0
  52. data/lib/mittsu/opengl/shader/chunks/fog_pars_fragment.glsl +15 -0
  53. data/lib/mittsu/opengl/shader/chunks/lightmap_fragment.glsl +5 -0
  54. data/lib/mittsu/opengl/shader/chunks/lightmap_pars_fragment.glsl +6 -0
  55. data/lib/mittsu/opengl/shader/chunks/lightmap_pars_vertex.glsl +5 -0
  56. data/lib/mittsu/opengl/shader/chunks/lightmap_vertex.glsl +5 -0
  57. data/lib/mittsu/opengl/shader/chunks/lights_lambert_pars_vertex.glsl +43 -0
  58. data/lib/mittsu/opengl/shader/chunks/lights_lambert_vertex.glsl +196 -0
  59. data/lib/mittsu/opengl/shader/chunks/lights_phong_fragment.glsl +243 -0
  60. data/lib/mittsu/opengl/shader/chunks/lights_phong_pars_fragment.glsl +58 -0
  61. data/lib/mittsu/opengl/shader/chunks/lights_phong_pars_vertex.glsl +5 -0
  62. data/lib/mittsu/opengl/shader/chunks/lights_phong_vertex.glsl +5 -0
  63. data/lib/mittsu/opengl/shader/chunks/linear_to_gamma_fragment.glsl +2 -0
  64. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_fragment.glsl +5 -0
  65. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_pars_fragment.glsl +12 -0
  66. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_pars_vertex.glsl +11 -0
  67. data/lib/mittsu/opengl/shader/chunks/logdepthbuf_vertex.glsl +15 -0
  68. data/lib/mittsu/opengl/shader/chunks/map_fragment.glsl +9 -0
  69. data/lib/mittsu/opengl/shader/chunks/map_pars_fragment.glsl +11 -0
  70. data/lib/mittsu/opengl/shader/chunks/map_pars_vertex.glsl +6 -0
  71. data/lib/mittsu/opengl/shader/chunks/map_particle_fragment.glsl +5 -0
  72. data/lib/mittsu/opengl/shader/chunks/map_particle_pars_fragment.glsl +6 -0
  73. data/lib/mittsu/opengl/shader/chunks/map_vertex.glsl +5 -0
  74. data/lib/mittsu/opengl/shader/chunks/morphnormal_vertex.glsl +12 -0
  75. data/lib/mittsu/opengl/shader/chunks/morphtarget_pars_vertex.glsl +13 -0
  76. data/lib/mittsu/opengl/shader/chunks/morphtarget_vertex.glsl +20 -0
  77. data/lib/mittsu/opengl/shader/chunks/normalmap_pars_fragment.glsl +27 -0
  78. data/lib/mittsu/opengl/shader/chunks/shadowmap_fragment.glsl +216 -0
  79. data/lib/mittsu/opengl/shader/chunks/shadowmap_pars_fragment.glsl +19 -0
  80. data/lib/mittsu/opengl/shader/chunks/shadowmap_pars_vertex.glsl +6 -0
  81. data/lib/mittsu/opengl/shader/chunks/shadowmap_vertex.glsl +9 -0
  82. data/lib/mittsu/opengl/shader/chunks/skinbase_vertex.glsl +8 -0
  83. data/lib/mittsu/opengl/shader/chunks/skinning_pars_vertex.glsl +47 -0
  84. data/lib/mittsu/opengl/shader/chunks/skinning_vertex.glsl +20 -0
  85. data/lib/mittsu/opengl/shader/chunks/skinnormal_vertex.glsl +20 -0
  86. data/lib/mittsu/opengl/shader/chunks/specularmap_fragment.glsl +12 -0
  87. data/lib/mittsu/opengl/shader/chunks/specularmap_pars_fragment.glsl +5 -0
  88. data/lib/mittsu/opengl/shader/chunks/worldpos_vertex.glsl +17 -0
  89. data/lib/mittsu/opengl/shader/lib/basic/basic_fragment.rbsl +37 -0
  90. data/lib/mittsu/opengl/shader/lib/basic/basic_uniforms.rbslu +3 -0
  91. data/lib/mittsu/opengl/shader/lib/basic/basic_vertex.rbsl +33 -0
  92. data/lib/mittsu/opengl/shader/lib/cube/cube_fragment.rbsl +12 -0
  93. data/lib/mittsu/opengl/shader/lib/cube/cube_uniforms.rbslu +2 -0
  94. data/lib/mittsu/opengl/shader/lib/cube/cube_vertex.rbsl +12 -0
  95. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_fragment.rbsl +26 -0
  96. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_uniforms.rbslu +0 -0
  97. data/lib/mittsu/opengl/shader/lib/depth_rgba/depth_rgba_vertex.rbsl +12 -0
  98. data/lib/mittsu/opengl/shader/lib/lambert/lambert_fragment.rbsl +56 -0
  99. data/lib/mittsu/opengl/shader/lib/lambert/lambert_uniforms.rbslu +7 -0
  100. data/lib/mittsu/opengl/shader/lib/lambert/lambert_vertex.rbsl +37 -0
  101. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_fragment.rbsl +27 -0
  102. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_uniforms.rbslu +2 -0
  103. data/lib/mittsu/opengl/shader/lib/particle_basic/particle_basic_vertex.rbsl +25 -0
  104. data/lib/mittsu/opengl/shader/lib/phong/phong_fragment.rbsl +45 -0
  105. data/lib/mittsu/opengl/shader/lib/phong/phong_uniforms.rbslu +11 -0
  106. data/lib/mittsu/opengl/shader/lib/phong/phong_vertex.rbsl +43 -0
  107. data/lib/mittsu/opengl/shader/lib.rb +45 -0
  108. data/lib/mittsu/opengl/shader/rbsl_loader.rb +168 -0
  109. data/lib/mittsu/opengl/shader/templates/fragment.glsl.erb +105 -0
  110. data/lib/mittsu/opengl/shader/templates/vertex.glsl.erb +143 -0
  111. data/lib/mittsu/opengl/shader/uniforms_lib.rb +86 -0
  112. data/lib/mittsu/opengl/shader/uniforms_utils.rb +31 -0
  113. data/lib/mittsu/opengl/shader.rb +56 -0
  114. data/lib/mittsu/opengl/state.rb +205 -0
  115. data/lib/mittsu/opengl/version.rb +5 -0
  116. data/lib/mittsu/opengl.rb +2 -0
  117. data/lib/mittsu/opengl_implementation/core/buffer_geometry.rb +11 -0
  118. data/lib/mittsu/opengl_implementation/core/geometry.rb +346 -0
  119. data/lib/mittsu/opengl_implementation/core/object_3d.rb +134 -0
  120. data/lib/mittsu/opengl_implementation/lights/ambient_light.rb +26 -0
  121. data/lib/mittsu/opengl_implementation/lights/directional_light.rb +35 -0
  122. data/lib/mittsu/opengl_implementation/lights/hemisphere_light.rb +39 -0
  123. data/lib/mittsu/opengl_implementation/lights/light.rb +55 -0
  124. data/lib/mittsu/opengl_implementation/lights/point_light.rb +36 -0
  125. data/lib/mittsu/opengl_implementation/lights/spot_light.rb +47 -0
  126. data/lib/mittsu/opengl_implementation/materials/line_basic_material.rb +16 -0
  127. data/lib/mittsu/opengl_implementation/materials/material.rb +274 -0
  128. data/lib/mittsu/opengl_implementation/materials/mesh_basic_material.rb +21 -0
  129. data/lib/mittsu/opengl_implementation/materials/mesh_lambert_material.rb +33 -0
  130. data/lib/mittsu/opengl_implementation/materials/mesh_phong_material.rb +44 -0
  131. data/lib/mittsu/opengl_implementation/materials/point_cloud_material.rb +27 -0
  132. data/lib/mittsu/opengl_implementation/materials/shader_material.rb +11 -0
  133. data/lib/mittsu/opengl_implementation/objects/group.rb +9 -0
  134. data/lib/mittsu/opengl_implementation/objects/line.rb +45 -0
  135. data/lib/mittsu/opengl_implementation/objects/mesh.rb +70 -0
  136. data/lib/mittsu/opengl_implementation/objects/point_cloud.rb +39 -0
  137. data/lib/mittsu/opengl_implementation/objects/sprite.rb +12 -0
  138. data/lib/mittsu/opengl_implementation/scenes/scene.rb +9 -0
  139. data/lib/mittsu/opengl_implementation/textures/compressed_texture.rb +20 -0
  140. data/lib/mittsu/opengl_implementation/textures/cube_texture.rb +77 -0
  141. data/lib/mittsu/opengl_implementation/textures/data_texture.rb +21 -0
  142. data/lib/mittsu/opengl_implementation/textures/render_target.rb +124 -0
  143. data/lib/mittsu/opengl_implementation/textures/texture.rb +107 -0
  144. data/mittsu-opengl.gemspec +36 -0
  145. metadata +314 -0
@@ -0,0 +1,31 @@
1
+ #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
+ }
@@ -0,0 +1,250 @@
1
+ require 'erb'
2
+ require 'mittsu/opengl/shader'
3
+
4
+ module Mittsu
5
+ class OpenGL::Program
6
+ attr_reader :id, :program, :uniforms
7
+ attr_accessor :code, :used_times, :attributes, :vertex_shader, :fragment_shader
8
+
9
+ def initialize(renderer, code, material, parameters)
10
+ @id = (@@id ||= 1).tap { @@id += 1 }
11
+ @renderer = renderer
12
+
13
+ compile_and_link_program(material, parameters)
14
+
15
+ cache_uniform_locations(material.shader[:uniforms] || {}, parameters)
16
+ cache_attribute_locations(material.attributes || {}, parameters)
17
+
18
+ @code = code
19
+ @used_times = 2
20
+ end
21
+
22
+ private
23
+
24
+ def compile_and_link_program(material, parameters)
25
+ @program = GL.CreateProgram
26
+
27
+ # TODO: necessary for OpenGL?
28
+ # index0_attribute_name = material.index0_attribute_name
29
+ #
30
+ # if index0_attribute_name.nil? && parameters[:morph_targets]
31
+ # # programs with morph_targets displace position of attribute 0
32
+ #
33
+ # index0_attribute_name = 'position'
34
+ # end
35
+
36
+ compile_shaders(material, parameters)
37
+
38
+ # if !index0_attribute_name.nil?
39
+ # TODO: is this necessary in OpenGL ???
40
+ # Force a particular attribute to index 0.
41
+ # because potentially expensive emulation is done by __browser__ if attribute 0 is disabled. (no browser here!)
42
+ # And, color, for example is often automatically bound to index 0 so disabling it
43
+
44
+ # GL.BindAttributeLocation(program, 0, index0_attribute_name)
45
+ # end
46
+
47
+ GL.LinkProgram(@program)
48
+ check_for_link_errors
49
+ post_link_clean_up
50
+ end
51
+
52
+ def generate_defines(defines)
53
+ chunks = []
54
+
55
+ defines.each do |d, value|
56
+ next if value == false
57
+
58
+ chunk = "#define #{d} #{value}"
59
+ chunks << chunk
60
+ end
61
+
62
+ chunks.join("\n")
63
+ end
64
+
65
+ def link_status
66
+ ptr = ' '*8
67
+ GL.GetProgramiv @program, GL::LINK_STATUS, ptr
68
+ ptr.unpack('L')[0]
69
+ end
70
+
71
+ def program_info_log
72
+ ptr = ' '*8
73
+ GL.GetProgramiv @program, GL::INFO_LOG_LENGTH, ptr
74
+ length = ptr.unpack('L')[0]
75
+
76
+ if length > 0
77
+ log = ' '*length
78
+ GL.GetProgramInfoLog @program, length, ptr, log
79
+ log.unpack("A#{length}")[0]
80
+ else
81
+ ''
82
+ end
83
+ end
84
+
85
+ def check_for_link_errors
86
+ log_info = program_info_log
87
+
88
+ if !link_status
89
+ puts "ERROR: Mittsu::OpenGL::Program: shader error: #{GL.GetError}, GL::INVALID_STATUS, #{GL.GetProgramParameter(program, GL::VALIDATE_STATUS)}, GL.GetProgramParameterInfoLog, #{log_info}"
90
+ end
91
+
92
+ if !log_info.empty?
93
+ puts "WARNING: Mittsu::OpenGL::Program: GL.GetProgramInfoLog, #{log_info}"
94
+ # TODO: useless in OpenGL ???
95
+ # puts "WARNING: #{GL.GetExtension( 'OPENGL_debug_shaders' ).getTranslatedShaderSource( GL.VertexShader )}"
96
+ # puts "WARNING: #{GL.GetExtension( 'OPENGL_debug_shaders' ).getTranslatedShaderSource( GL.FragmentShader )}"
97
+ end
98
+ end
99
+
100
+ def get_shadow_map_define(shadow_map_param)
101
+ case shadow_map_param
102
+ when PCFShadowMap
103
+ 'SHADOWMAP_TYPE_PCF'
104
+ when PCFSoftShadowMap
105
+ 'SHADOWMAP_TYPE_PCF_SOFT'
106
+ else
107
+ 'SHADOWMAP_TYPE_BASIC'
108
+ end
109
+ end
110
+
111
+ def get_env_map_type_define(env_map_param, material)
112
+ return 'ENVMAP_TYPE_CUBE' unless env_map_param
113
+ case material.env_map.mapping
114
+ when CubeReflectionMapping, CubeRefractionMapping
115
+ 'ENVMAP_TYPE_CUBE'
116
+ when EquirectangularReflectionMapping, EquirectangularRefractionMapping
117
+ 'ENVMAP_TYPE_EQUIREC'
118
+ when SphericalReflectionMapping
119
+ 'ENVMAP_TYPE_SPHERE'
120
+ else
121
+ 'ENVMAP_TYPE_CUBE'
122
+ end
123
+ end
124
+
125
+ def get_env_map_mode_define(env_map_param, material)
126
+ return 'ENVMAP_MODE_REFLECTION' unless env_map_param
127
+ case material.env_map.mapping
128
+ when CubeRefractionMapping, EquirectangularRefractionMapping
129
+ 'ENVMAP_MODE_REFRACTION'
130
+ else
131
+ 'ENVMAP_MODE_REFLECTION'
132
+ end
133
+ end
134
+
135
+ def get_env_map_blending_define(env_map_param, material)
136
+ return 'ENVMAP_BLENDING_MULTIPLY' unless env_map_param
137
+ case material.combine
138
+ when MultiplyOperation
139
+ 'ENVMAP_BLENDING_MULTIPLY'
140
+ when MixOperation
141
+ 'ENVMAP_BLENDING_MIX'
142
+ when AddOperation
143
+ 'ENVMAP_BLENDING_ADD'
144
+ else
145
+ 'ENVMAP_BLENDING_MULTIPLY'
146
+ end
147
+ end
148
+
149
+ def compile_shaders(material, parameters)
150
+ shadow_map_type_define = get_shadow_map_define(parameters[:shadow_map_type])
151
+
152
+ env_map_type_define = get_env_map_type_define(parameters[:env_map], material)
153
+ env_map_mode_define = get_env_map_mode_define(parameters[:env_map], material)
154
+ env_map_blending_define = get_env_map_blending_define(parameters[:env_map], material)
155
+
156
+ gamma_factor_define = (@renderer.gamma_factor > 0) ? @renderer.gamma_factor : 1.0
157
+
158
+ custom_defines = generate_defines(material.defines || {})
159
+
160
+ if false # material.is_a?(RawShaderMaterial) # TODO: when RawShaderMaterial exists
161
+ prefix_vertex = ''
162
+ prefix_fragment = ''
163
+ else
164
+ prefix_vertex = File.read(File.expand_path('../shader/templates/vertex.glsl.erb', __FILE__))
165
+ prefix_fragment = File.read(File.expand_path('../shader/templates/fragment.glsl.erb', __FILE__))
166
+ end
167
+
168
+ @vertex_shader = OpenGL::Shader.new(GL::VERTEX_SHADER, compile_shader_template(prefix_vertex + material.shader[:vertex_shader], binding))
169
+ @fragment_shader = OpenGL::Shader.new(GL::FRAGMENT_SHADER, compile_shader_template(prefix_fragment + material.shader[:fragment_shader], binding))
170
+
171
+ GL.AttachShader(@program, @vertex_shader.shader)
172
+ GL.AttachShader(@program, @fragment_shader.shader)
173
+ end
174
+
175
+ def compile_shader_template(template, b)
176
+ ERB.new(template).result(b)
177
+ end
178
+
179
+ def post_link_clean_up
180
+ GL.DeleteShader(@vertex_shader.shader)
181
+ GL.DeleteShader(@fragment_shader.shader)
182
+ end
183
+
184
+ def cache_uniform_locations(uniforms, parameters)
185
+ identifiers = [
186
+ 'viewMatrix',
187
+ 'modelViewMatrix',
188
+ 'projectionMatrix',
189
+ 'normalMatrix',
190
+ 'modelMatrix',
191
+ 'cameraPosition',
192
+ 'morphTargetInfluences',
193
+ 'bindMatrix',
194
+ 'bindMatrixInverse'
195
+ ]
196
+
197
+ if parameters[:use_vertex_texture]
198
+ identifiers << 'boneTexture'
199
+ identifiers << 'boneTextureWidth'
200
+ identifiers << 'boneTextureHeight'
201
+ else
202
+ identifiers << 'boneGlobalMatrices'
203
+ end
204
+
205
+ if parameters[:logarithmic_depth_buffer]
206
+ identifiers << 'logDepthBufFC'
207
+ end
208
+
209
+ uniforms.each do |k, v|
210
+ identifiers << k.to_s
211
+ end
212
+
213
+ @uniforms = {}
214
+ identifiers.each do |id|
215
+ @uniforms[id] = GL.GetUniformLocation(program, id)
216
+ end
217
+ end
218
+
219
+ def cache_attribute_locations(attributes, parameters)
220
+ identifiers = [
221
+ 'position',
222
+ 'normal',
223
+ 'uv',
224
+ 'uv2',
225
+ 'tangent',
226
+ 'color',
227
+ 'skinIndex',
228
+ 'skinWeight',
229
+ 'lineDistance'
230
+ ]
231
+
232
+ parameters[:max_morph_targets].times do |i|
233
+ identifiers << "morphTarget#{i}"
234
+ end
235
+
236
+ parameters[:max_morph_normals].times do |i|
237
+ identifiers << "morphNormal#{i}"
238
+ end
239
+
240
+ attributes.each do |k, v|
241
+ identifiers << k
242
+ end
243
+
244
+ @attributes = {}
245
+ identifiers.each do |id|
246
+ @attributes[id] = GL.GetAttribLocation(program, id)
247
+ end
248
+ end
249
+ end
250
+ end