mittsu 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -1
  3. data/README.md +1 -1
  4. data/lib/mittsu/cameras/orthographic_camera.rb +13 -0
  5. data/lib/mittsu/cameras/perspective_camera.rb +11 -0
  6. data/lib/mittsu/core/geometry.rb +12 -9
  7. data/lib/mittsu/core/object_3d.rb +33 -66
  8. data/lib/mittsu/lights/ambient_light.rb +8 -0
  9. data/lib/mittsu/lights/directional_light.rb +9 -0
  10. data/lib/mittsu/lights/hemisphere_light.rb +9 -0
  11. data/lib/mittsu/lights/point_light.rb +11 -0
  12. data/lib/mittsu/lights/spot_light.rb +13 -0
  13. data/lib/mittsu/loaders.rb +1 -0
  14. data/lib/mittsu/loaders/mtl_loader.rb +5 -12
  15. data/lib/mittsu/loaders/obj_loader.rb +212 -0
  16. data/lib/mittsu/loaders/obj_mtl_loader.rb +11 -207
  17. data/lib/mittsu/materials/material.rb +5 -2
  18. data/lib/mittsu/materials/mesh_basic_material.rb +0 -9
  19. data/lib/mittsu/math/color.rb +44 -104
  20. data/lib/mittsu/math/matrix3.rb +8 -1
  21. data/lib/mittsu/math/matrix4.rb +6 -0
  22. data/lib/mittsu/math/vector.rb +251 -0
  23. data/lib/mittsu/math/vector2.rb +14 -213
  24. data/lib/mittsu/math/vector3.rb +61 -351
  25. data/lib/mittsu/math/vector4.rb +45 -295
  26. data/lib/mittsu/objects/line.rb +12 -2
  27. data/lib/mittsu/objects/mesh.rb +18 -9
  28. data/lib/mittsu/renderers/glfw_window.rb +15 -13
  29. data/lib/mittsu/renderers/opengl/core/opengl_geometry.rb +253 -0
  30. data/lib/mittsu/renderers/opengl/core/opengl_object_3d.rb +131 -0
  31. data/lib/mittsu/renderers/opengl/lights/opengl_ambient_light.rb +26 -0
  32. data/lib/mittsu/renderers/opengl/lights/opengl_directional_light.rb +35 -0
  33. data/lib/mittsu/renderers/opengl/lights/opengl_hemisphere_light.rb +42 -0
  34. data/lib/mittsu/renderers/opengl/lights/opengl_light.rb +52 -0
  35. data/lib/mittsu/renderers/opengl/lights/opengl_point_light.rb +36 -0
  36. data/lib/mittsu/renderers/opengl/lights/opengl_spot_light.rb +47 -0
  37. data/lib/mittsu/renderers/opengl/materials/opengl_line_basic_material.rb +16 -0
  38. data/lib/mittsu/renderers/opengl/materials/opengl_material.rb +275 -0
  39. data/lib/mittsu/renderers/opengl/materials/opengl_mesh_basic_material.rb +69 -0
  40. data/lib/mittsu/renderers/opengl/materials/opengl_mesh_lambert_material.rb +29 -0
  41. data/lib/mittsu/renderers/opengl/materials/opengl_mesh_phong_material.rb +40 -0
  42. data/lib/mittsu/renderers/opengl/materials/opengl_shader_material.rb +11 -0
  43. data/lib/mittsu/renderers/opengl/objects/opengl_group.rb +8 -0
  44. data/lib/mittsu/renderers/opengl/objects/opengl_line.rb +54 -0
  45. data/lib/mittsu/renderers/opengl/objects/opengl_mesh.rb +77 -0
  46. data/lib/mittsu/renderers/opengl/opengl_buffer.rb +5 -0
  47. data/lib/mittsu/renderers/opengl/opengl_debug.rb +49 -7
  48. data/lib/mittsu/renderers/opengl/opengl_default_target.rb +54 -0
  49. data/lib/mittsu/renderers/opengl/opengl_geometry_group.rb +763 -0
  50. data/lib/mittsu/renderers/opengl/opengl_geometry_like.rb +130 -0
  51. data/lib/mittsu/renderers/opengl/opengl_helper.rb +161 -0
  52. data/lib/mittsu/renderers/opengl/opengl_implementations.rb +61 -0
  53. data/lib/mittsu/renderers/opengl/opengl_light_renderer.rb +43 -0
  54. data/lib/mittsu/renderers/opengl/opengl_mittsu_params.rb +53 -0
  55. data/lib/mittsu/renderers/opengl/opengl_program.rb +147 -296
  56. data/lib/mittsu/renderers/opengl/opengl_state.rb +3 -5
  57. data/lib/mittsu/renderers/opengl/plugins/shadow_map_plugin.rb +12 -10
  58. data/lib/mittsu/renderers/opengl/scenes/opengl_scene.rb +8 -0
  59. data/lib/mittsu/renderers/opengl/textures/opengl_compressed_texture.rb +21 -0
  60. data/lib/mittsu/renderers/opengl/textures/opengl_cube_texture.rb +75 -0
  61. data/lib/mittsu/renderers/opengl/textures/opengl_data_texture.rb +23 -0
  62. data/lib/mittsu/renderers/opengl/textures/opengl_texture.rb +111 -0
  63. data/lib/mittsu/renderers/opengl_render_target.rb +117 -2
  64. data/lib/mittsu/renderers/opengl_renderer.rb +653 -2978
  65. data/lib/mittsu/renderers/shaders/rbsl_loader.rb +166 -0
  66. data/lib/mittsu/renderers/shaders/shader_chunk.rb +6 -9
  67. data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_fragment.glsl +36 -37
  68. data/lib/mittsu/renderers/shaders/shader_lib.rb +26 -403
  69. data/lib/mittsu/renderers/shaders/shader_lib/basic/basic_fragment.rbsl +37 -0
  70. data/lib/mittsu/renderers/shaders/shader_lib/basic/basic_uniforms.rbslu +3 -0
  71. data/lib/mittsu/renderers/shaders/shader_lib/basic/basic_vertex.rbsl +33 -0
  72. data/lib/mittsu/renderers/shaders/shader_lib/cube/cube_fragment.rbsl +12 -0
  73. data/lib/mittsu/renderers/shaders/shader_lib/cube/cube_uniforms.rbslu +2 -0
  74. data/lib/mittsu/renderers/shaders/shader_lib/cube/cube_vertex.rbsl +12 -0
  75. data/lib/mittsu/renderers/shaders/shader_lib/depth_rgba/depth_rgba_fragment.rbsl +26 -0
  76. data/lib/mittsu/renderers/shaders/shader_lib/depth_rgba/depth_rgba_uniforms.rbslu +0 -0
  77. data/lib/mittsu/renderers/shaders/shader_lib/depth_rgba/depth_rgba_vertex.rbsl +12 -0
  78. data/lib/mittsu/renderers/shaders/shader_lib/lambert/lambert_fragment.rbsl +56 -0
  79. data/lib/mittsu/renderers/shaders/shader_lib/lambert/lambert_uniforms.rbslu +7 -0
  80. data/lib/mittsu/renderers/shaders/shader_lib/lambert/lambert_vertex.rbsl +37 -0
  81. data/lib/mittsu/renderers/shaders/shader_lib/phong/phong_fragment.rbsl +45 -0
  82. data/lib/mittsu/renderers/shaders/shader_lib/phong/phong_uniforms.rbslu +11 -0
  83. data/lib/mittsu/renderers/shaders/shader_lib/phong/phong_vertex.rbsl +43 -0
  84. data/lib/mittsu/renderers/shaders/shader_templates/fragment.glsl.erb +105 -0
  85. data/lib/mittsu/renderers/shaders/shader_templates/vertex.glsl.erb +143 -0
  86. data/lib/mittsu/renderers/shaders/uniforms_lib.rb +54 -55
  87. data/lib/mittsu/textures/texture.rb +5 -2
  88. data/lib/mittsu/version.rb +1 -1
  89. data/run_all_examples.sh +7 -0
  90. metadata +77 -28
  91. data/.ruby-version +0 -1
  92. data/lib/mittsu/core/hash_object.rb +0 -19
@@ -1,3 +1,5 @@
1
+ require 'erb'
2
+
1
3
  require 'mittsu/renderers/opengl/opengl_shader'
2
4
 
3
5
  module Mittsu
@@ -6,12 +8,23 @@ module Mittsu
6
8
  attr_accessor :code, :used_times, :attributes, :vertex_shader, :fragment_shader
7
9
 
8
10
  def initialize(renderer, code, material, parameters)
9
- defines = material.defines || {} # TODO: setting to default object... ?
10
- uniforms = material[:_opengl_shader][:uniforms]
11
- attributes = material.attributes || [] # TODO: setting to default array... ?
11
+ @id = (@@id ||= 1).tap { @@id += 1 }
12
+ @renderer = renderer
13
+
14
+ compile_and_link_program(material, parameters)
15
+
16
+ material_impl = material.implementation(@renderer)
17
+ cache_uniform_locations(material_impl.shader[:uniforms] || {}, parameters)
18
+ cache_attribute_locations(material.attributes || {}, parameters)
19
+
20
+ @code = code
21
+ @used_times = 2
22
+ end
23
+
24
+ private
12
25
 
13
- vertex_shader = material[:_opengl_shader][:vertex_shader]
14
- fragment_shader = material[:_opengl_shader][:fragment_shader]
26
+ def compile_and_link_program(material, parameters)
27
+ @program = glCreateProgram
15
28
 
16
29
  # TODO: necessary for OpenGL?
17
30
  # index0_attribute_name = material.index0_attribute_name
@@ -22,263 +35,156 @@ module Mittsu
22
35
  # index0_attribute_name = 'position'
23
36
  # end
24
37
 
25
- shadow_map_type_define = 'SHADOWMAP_TYPE_BASIC'
26
-
27
- if parameters[:shadow_map_type] == PCFShadowMap
28
- shadow_map_type_define = 'SHADOWMAP_TYPE_PCF_SOFT'
29
- elsif parameters[:shadow_map_type] == PCFSoftShadowMap
30
- shadow_map_type_define = 'SHADOWMAP_TYPE_PCF_SOFT'
31
- end
32
-
33
- env_map_type_define = 'ENVMAP_TYPE_CUBE'
34
- env_map_mode_define = 'ENVMAP_MODE_REFLECTION'
35
- env_map_blending_define = 'ENVMAP_BLENDING_MULTIPLY'
36
-
37
- if parameters[:env_map]
38
- case material.env_map.mapping
39
- when CubeReflectionMapping, CubeRefractionMapping
40
- env_map_type_define = 'ENVMAP_TYPE_CUBE'
41
- when EquirectangularReflectionMapping, EquirectangularRefractionMapping
42
- env_map_type_define = 'ENVMAP_TYPE_EQUIREC'
43
- when SphericalReflectionMapping
44
- env_map_type_define = 'ENVMAP_TYPE_SPHERE'
45
- end
46
-
47
- case material.env_map.mapping
48
- when CubeRefractionMapping, EquirectangularRefractionMapping
49
- env_map_mode_define 'ENVMAP_MODE_REFRACTION'
50
- end
51
-
52
- case material.combine
53
- when MultiplyOperation
54
- env_map_blending_define = 'ENVMAP_BLENDING_MULTIPLY'
55
- when MixOperation
56
- env_map_blending_define = 'ENVMAP_BLENDING_MIX'
57
- when AddOperation
58
- env_map_blending_define = 'ENVMAP_BLENDING_ADD'
59
- end
60
- end
61
-
62
- gamma_factor_define = (renderer.gamma_factor > 0) ? renderer.gamma_factor : 1.0
63
-
64
- # puts 'building new program'
65
-
66
- #
67
-
68
- custom_defines = generate_defines(defines)
69
-
70
- #
71
-
72
- @program = glCreateProgram
73
-
74
- if false # material.is_a?(RawShaderMaterial) # TODO: when RawShaderMaterial exists
75
- prefix_vertex = ''
76
- prefix_fragment = ''
77
- else
78
- prefix_vertex = [
79
- '#version 330',
80
- # TODO: do we need precision for an OpenGL program?
81
- # "precision #{parameters[:precision]} float;",
82
- # "precision #{parameters[:precision]} int;",
83
-
84
- custom_defines,
85
-
86
- parameters[:supports_vertex_textures] ? '#define VERTEX_TEXTURES' : '',
87
-
88
- renderer.gamma_input ? '#define GAMMA_INPUT' : '',
89
- renderer.gamma_output ? '#define GAMMA_OUTPUT' : '',
90
- "#define GAMMA_FACTOR #{gamma_factor_define}",
91
-
92
- "#define MAX_DIR_LIGHTS #{parameters[:max_dir_lights]}",
93
- "#define MAX_POINT_LIGHTS #{parameters[:max_point_lights]}",
94
- "#define MAX_SPOT_LIGHTS #{parameters[:max_spot_lights]}",
95
- "#define MAX_HEMI_LIGHTS #{parameters[:max_hemi_lights]}",
96
-
97
- "#define MAX_SHADOWS #{parameters[:max_shadows]}",
98
-
99
- "#define MAX_BONES #{parameters[:max_bones]}",
100
-
101
- parameters[:map] ? '#define USE_MAP' : '',
102
- parameters[:env_map] ? '#define USE_ENVMAP' : '',
103
- parameters[:env_map] ? "#define #{env_map_mode_define}" : '',
104
- parameters[:light_map] ? '#define USE_LIGHTMAP' : '',
105
- parameters[:bump_map] ? '#define USE_BUMPMAP' : '',
106
- parameters[:normal_map] ? '#define USE_NORMALMAP' : '',
107
- parameters[:specular_map] ? '#define USE_SPECULARMAP' : '',
108
- parameters[:alpha_map] ? '#define USE_ALPHAMAP' : '',
109
- parameters[:vertex_colors] ? '#define USE_COLOR' : '',
110
-
111
- parameters[:flat_shading] ? '#define FLAT_SHADED': '',
112
-
113
- parameters[:skinning] ? '#define USE_SKINNING' : '',
114
- parameters[:use_vertex_texture] ? '#define BONE_TEXTURE' : '',
115
-
116
- parameters[:morph_targets] ? '#define USE_MORPHTARGETS' : '',
117
- parameters[:morph_normals] ? '#define USE_MORPHNORMALS' : '',
118
- parameters[:wrap_around] ? '#define WRAP_AROUND' : '',
119
- parameters[:double_sided] ? '#define DOUBLE_SIDED' : '',
120
- parameters[:flip_sided] ? '#define FLIP_SIDED' : '',
121
-
122
- parameters[:shadow_map_enabled] ? '#define USE_SHADOWMAP' : '',
123
- parameters[:shadow_map_enabled] ? "#define #{shadow_map_type_define}" : '',
124
- parameters[:shadow_map_debug] ? '#define SHADOWMAP_DEBUG' : '',
125
- parameters[:shadow_map_cascade] ? '#define SHADOWMAP_CASCADE' : '',
126
-
127
- parameters[:size_attenuation] ? '#define USE_SIZEATTENUATION' : '',
128
-
129
- parameters[:logarithmic_depth_buffer] ? '#define USE_LOGDEPTHBUF' : '',
130
- #renderer._glExtensionFragDepth ? '#define USE_LOGDEPTHBUF_EXT' : '',
131
-
132
-
133
- 'uniform mat4 modelMatrix;',
134
- 'uniform mat4 modelViewMatrix;',
135
- 'uniform mat4 projectionMatrix;',
136
- 'uniform mat4 viewMatrix;',
137
- 'uniform mat3 normalMatrix;',
138
- 'uniform vec3 cameraPosition;',
139
-
140
- 'in vec3 position;',
141
- 'in vec3 normal;',
142
- 'in vec2 uv;',
143
- 'in vec2 uv2;',
144
-
145
- '#ifdef USE_COLOR',
146
-
147
- ' in vec3 color;',
148
-
149
- '#endif',
38
+ compile_shaders(material, parameters)
150
39
 
151
- '#ifdef USE_MORPHTARGETS',
152
-
153
- ' in vec3 morphTarget0;',
154
- ' in vec3 morphTarget1;',
155
- ' in vec3 morphTarget2;',
156
- ' in vec3 morphTarget3;',
157
-
158
- ' #ifdef USE_MORPHNORMALS',
159
-
160
- ' in vec3 morphNormal0;',
161
- ' in vec3 morphNormal1;',
162
- ' in vec3 morphNormal2;',
163
- ' in vec3 morphNormal3;',
164
-
165
- ' #else',
166
-
167
- ' in vec3 morphTarget4;',
168
- ' in vec3 morphTarget5;',
169
- ' in vec3 morphTarget6;',
170
- ' in vec3 morphTarget7;',
171
-
172
- ' #endif',
173
-
174
- '#endif',
175
-
176
- '#ifdef USE_SKINNING',
177
-
178
- ' in vec4 skinIndex;',
179
- ' in vec4 skinWeight;',
180
-
181
- '#endif',
182
- ].reject(&:empty?).join("\n") + "\n"
183
-
184
- prefix_fragment = [
185
- '#version 330',
186
- # TODO: do we need precision for an OpenGL program?
187
- # "precison #{parameters[:precision]} float;",
188
- # "precison #{parameters[:precision]} int;",
189
-
190
- # (parameters[:bump_map] || parameters[:normal_map] || parameters[:flat_shading]) ? '#extension GL_OES_standard_derivatives : enable' : '', # TODO: oes extension in OpenGL?
40
+ # if !index0_attribute_name.nil?
41
+ # TODO: is this necessary in OpenGL ???
42
+ # Force a particular attribute to index 0.
43
+ # because potentially expensive emulation is done by __browser__ if attribute 0 is disabled. (no browser here!)
44
+ # And, color, for example is often automatically bound to index 0 so disabling it
191
45
 
192
- custom_defines,
46
+ # glBindAttributeLocation(program, 0, index0_attribute_name)
47
+ # end
193
48
 
194
- "#define MAX_DIR_LIGHTS #{parameters[:max_dir_lights]}",
195
- "#define MAX_POINT_LIGHTS #{parameters[:max_point_lights]}",
196
- "#define MAX_SPOT_LIGHTS #{parameters[:max_spot_lights]}",
197
- "#define MAX_HEMI_LIGHTS #{parameters[:max_hemi_lights]}",
49
+ glLinkProgram(@program)
50
+ check_for_link_errors
51
+ post_link_clean_up
52
+ end
198
53
 
199
- "#define MAX_SHADOWS #{parameters[:max_shadows]}",
54
+ def generate_defines(defines)
55
+ chunks = []
200
56
 
201
- parameters[:alpha_test] ? "#define ALPHATEST #{parameters[:alpha_test].to_f}" : '',
57
+ defines.each do |d, value|
58
+ next if value == false
202
59
 
203
- renderer.gamma_input ? '#define GAMMA_INPUT' : '',
204
- renderer.gamma_output ? '#define GAMMA_OUTPUT' : '',
205
- "#define GAMMA_FACTOR #{gamma_factor_define}",
60
+ chunk = "#define #{d} #{value}"
61
+ chunks << chunk
62
+ end
206
63
 
207
- (parameters[:use_fog] && parameters[:fog]) ? '#define USE_FOG' : '',
208
- (parameters[:use_fog] && parameters[:fog_exp]) ? '#define FOG_EXP2' : '',
64
+ chunks.join("\n")
65
+ end
209
66
 
210
- parameters[:map] ? '#define USE_MAP' : '',
211
- parameters[:env_map] ? '#define USE_ENVMAP' : '',
212
- parameters[:env_map] ? "#define #{env_map_type_define}" : '',
213
- parameters[:env_map] ? "#define #{env_map_mode_define}" : '',
214
- parameters[:env_map] ? "#define #{env_map_blending_define}" : '',
215
- parameters[:light_map] ? '#define USE_LIGHTMAP' : '',
216
- parameters[:bump_map] ? '#define USE_BUMPMAP' : '',
217
- parameters[:normal_map] ? '#define USE_NORMALMAP' : '',
218
- parameters[:specular_map] ? '#define USE_SPECULARMAP' : '',
219
- parameters[:alpha_map] ? '#define USE_ALPHAMAP' : '',
220
- parameters[:vertex_colors] ? '#define USE_COLOR' : '',
67
+ def link_status
68
+ ptr = ' '*8
69
+ glGetProgramiv @program, GL_LINK_STATUS, ptr
70
+ ptr.unpack('L')[0]
71
+ end
221
72
 
222
- parameters[:flat_shading] ? '#define FLAT_SHADED' : '',
73
+ def program_info_log
74
+ ptr = ' '*8
75
+ glGetProgramiv @program, GL_INFO_LOG_LENGTH, ptr
76
+ length = ptr.unpack('L')[0]
223
77
 
224
- parameters[:metal] ? '#define METAL' : '',
225
- parameters[:wrap_around] ? '#define WRAP_AROUND' : '',
226
- parameters[:double_sided] ? '#define DOUBLE_SIDED' : '',
227
- parameters[:flip_sided] ? '#define FLIP_SIDED' : '',
78
+ if length > 0
79
+ log = ' '*length
80
+ glGetProgramInfoLog @program, length, ptr, log
81
+ log.unpack("A#{length}")[0]
82
+ else
83
+ ''
84
+ end
85
+ end
228
86
 
87
+ def check_for_link_errors
88
+ log_info = program_info_log
229
89
 
230
- parameters[:shadow_map_enabled] ? '#define USE_SHADOWMAP' : '',
231
- parameters[:shadow_map_enabled] ? "#define #{shadow_map_type_define}" : '',
232
- parameters[:shadow_map_debug] ? '#define SHADOWMAP_DEBUG' : '',
233
- parameters[:'shadow_map_cascade'] ? '#define SHADOWMAP_CASCADE' : '',
90
+ if !link_status
91
+ puts "ERROR: Mittsu::OpenGLProgram: shader error: #{glGetError}, GL_INVALID_STATUS, #{glGetProgramParameter(program, GL_VALIDATE_STATUS)}, glGetProgramParameterInfoLog, #{log_info}"
92
+ end
234
93
 
235
- parameters[:logarithmic_depth_buffer] ? '#define USE_LOGDEPTHBUF' : '',
236
- #renderer._glExtensionFragDepth ? '#define USE_LOGDEPTHBUF_EXT' : '',
94
+ if !log_info.empty?
95
+ puts "WARNING: Mittsu::OpenGLProgram: glGetProgramInfoLog, #{log_info}"
96
+ # TODO: useless in OpenGL ???
97
+ # puts "WARNING: #{glGetExtension( 'OPENGL_debug_shaders' ).getTranslatedShaderSource( glVertexShader )}"
98
+ # puts "WARNING: #{glGetExtension( 'OPENGL_debug_shaders' ).getTranslatedShaderSource( glFragmentShader )}"
99
+ end
100
+ end
237
101
 
238
- 'uniform mat4 viewMatrix;',
239
- 'uniform vec3 cameraPosition;',
102
+ def get_shadow_map_define(shadow_map_param)
103
+ case shadow_map_param
104
+ when PCFShadowMap
105
+ 'SHADOWMAP_TYPE_PCF'
106
+ when PCFSoftShadowMap
107
+ 'SHADOWMAP_TYPE_PCF_SOFT'
108
+ else
109
+ 'SHADOWMAP_TYPE_BASIC'
110
+ end
111
+ end
240
112
 
241
- 'layout(location = 0) out vec4 fragColor;'
242
- ].reject(&:empty?).join("\n") + "\n"
113
+ def get_env_map_type_define(env_map_param, material)
114
+ return 'ENVMAP_TYPE_CUBE' unless env_map_param
115
+ case material.env_map.mapping
116
+ when CubeReflectionMapping, CubeRefractionMapping
117
+ 'ENVMAP_TYPE_CUBE'
118
+ when EquirectangularReflectionMapping, EquirectangularRefractionMapping
119
+ 'ENVMAP_TYPE_EQUIREC'
120
+ when SphericalReflectionMapping
121
+ 'ENVMAP_TYPE_SPHERE'
122
+ else
123
+ 'ENVMAP_TYPE_CUBE'
243
124
  end
125
+ end
244
126
 
245
- gl_vertex_shader = OpenGLShader.new(GL_VERTEX_SHADER, prefix_vertex + vertex_shader)
246
- gl_fragment_shader = OpenGLShader.new(GL_FRAGMENT_SHADER, prefix_fragment + fragment_shader)
127
+ def get_env_map_mode_define(env_map_param, material)
128
+ return 'ENVMAP_MODE_REFLECTION' unless env_map_param
129
+ case material.env_map.mapping
130
+ when CubeRefractionMapping, EquirectangularRefractionMapping
131
+ 'ENVMAP_MODE_REFRACTION'
132
+ else
133
+ 'ENVMAP_MODE_REFLECTION'
134
+ end
135
+ end
247
136
 
248
- glAttachShader(@program, gl_vertex_shader.shader)
249
- glAttachShader(@program, gl_fragment_shader.shader)
137
+ def get_env_map_blending_define(env_map_param, material)
138
+ return 'ENVMAP_BLENDING_MULTIPLY' unless env_map_param
139
+ case material.combine
140
+ when MultiplyOperation
141
+ 'ENVMAP_BLENDING_MULTIPLY'
142
+ when MixOperation
143
+ 'ENVMAP_BLENDING_MIX'
144
+ when AddOperation
145
+ 'ENVMAP_BLENDING_ADD'
146
+ else
147
+ 'ENVMAP_BLENDING_MULTIPLY'
148
+ end
149
+ end
250
150
 
251
- # if !index0_attribute_name.nil?
252
- # TODO: is this necessary in OpenGL ???
253
- # Force a particular attribute to index 0.
254
- # because potentially expensive emulation is done by __browser__ if attribute 0 is disabled. (no browser here!)
255
- # And, color, for example is often automatically bound to index 0 so disabling it
151
+ def compile_shaders(material, parameters)
152
+ material_impl = material.implementation(@renderer)
153
+ shadow_map_type_define = get_shadow_map_define(parameters[:shadow_map_type])
256
154
 
257
- # glBindAttributeLocation(program, 0, index0_attribute_name)
258
- # end
155
+ env_map_type_define = get_env_map_type_define(parameters[:env_map], material)
156
+ env_map_mode_define = get_env_map_mode_define(parameters[:env_map], material)
157
+ env_map_blending_define = get_env_map_blending_define(parameters[:env_map], material)
259
158
 
260
- glLinkProgram(@program)
159
+ gamma_factor_define = (@renderer.gamma_factor > 0) ? @renderer.gamma_factor : 1.0
261
160
 
262
- log_info = program_info_log
161
+ custom_defines = generate_defines(material.defines || {})
263
162
 
264
- if !link_status
265
- puts "ERROR: Mittsu::OpenGLProgram: shader error: #{glGetError}, GL_INVALID_STATUS, #{glGetProgramParameter(program, GL_VALIDATE_STATUS)}, glGetProgramParameterInfoLog, #{program_log_info}"
163
+ if false # material.is_a?(RawShaderMaterial) # TODO: when RawShaderMaterial exists
164
+ prefix_vertex = ''
165
+ prefix_fragment = ''
166
+ else
167
+ prefix_vertex = File.read(File.expand_path('../../shaders/shader_templates/vertex.glsl.erb', __FILE__))
168
+ prefix_fragment = File.read(File.expand_path('../../shaders/shader_templates/fragment.glsl.erb', __FILE__))
266
169
  end
267
170
 
268
- if !log_info.empty?
269
- puts "WARNING: Mittsu::OpenGLProgram: glGetProgramInfoLog, #{log_info}"
270
- # TODO: useless in OpenGL ???
271
- # puts "WARNING: #{glGetExtension( 'OPENGL_debug_shaders' ).getTranslatedShaderSource( glVertexShader )}"
272
- # puts "WARNING: #{glGetExtension( 'OPENGL_debug_shaders' ).getTranslatedShaderSource( glFragmentShader )}"
273
- end
171
+ @vertex_shader = OpenGLShader.new(GL_VERTEX_SHADER, compile_shader_template(prefix_vertex + material_impl.shader[:vertex_shader], binding))
172
+ @fragment_shader = OpenGLShader.new(GL_FRAGMENT_SHADER, compile_shader_template(prefix_fragment + material_impl.shader[:fragment_shader], binding))
274
173
 
275
- # clean up
174
+ glAttachShader(@program, @vertex_shader.shader)
175
+ glAttachShader(@program, @fragment_shader.shader)
176
+ end
276
177
 
277
- glDeleteShader(gl_vertex_shader.shader)
278
- glDeleteShader(gl_fragment_shader.shader)
178
+ def compile_shader_template(template, b)
179
+ ERB.new(template).result(b)
180
+ end
279
181
 
280
- # cache uniform locations
182
+ def post_link_clean_up
183
+ glDeleteShader(@vertex_shader.shader)
184
+ glDeleteShader(@fragment_shader.shader)
185
+ end
281
186
 
187
+ def cache_uniform_locations(uniforms, parameters)
282
188
  identifiers = [
283
189
  'viewMatrix',
284
190
  'modelViewMatrix',
@@ -307,10 +213,13 @@ module Mittsu
307
213
  identifiers << k.to_s
308
214
  end
309
215
 
310
- @uniforms = cache_uniform_locations(program, identifiers)
311
-
312
- # cache attributes locations
216
+ @uniforms = {}
217
+ identifiers.each do |id|
218
+ @uniforms[id] = glGetUniformLocation(program, id)
219
+ end
220
+ end
313
221
 
222
+ def cache_attribute_locations(attributes, parameters)
314
223
  identifiers = [
315
224
  'position',
316
225
  'normal',
@@ -335,67 +244,9 @@ module Mittsu
335
244
  identifiers << k
336
245
  end
337
246
 
338
- @attributes = cache_attribute_locations(program, identifiers)
339
-
340
- @id = (@@id ||= 1).tap { @@id += 1 }
341
- @code = code
342
- @used_times = 2
343
- @vertex_shader = gl_vertex_shader
344
- @fragment_shader = gl_fragment_shader
345
- end
346
-
347
- private
348
-
349
- def generate_defines(defines)
350
- chunks = []
351
-
352
- defines.each do |d, value|
353
- next if value == false
354
-
355
- chunk = "#define #{d} #{value}"
356
- chunks << chunk
357
- end
358
-
359
- chunks.join("\n")
360
- end
361
-
362
- def cache_uniform_locations(program, identifiers)
363
- uniforms = {}
364
-
365
- identifiers.each do |id|
366
- uniforms[id] = glGetUniformLocation(program, id)
367
- end
368
-
369
- uniforms
370
- end
371
-
372
- def cache_attribute_locations(program, identifiers)
373
- attributes = {}
374
-
247
+ @attributes = {}
375
248
  identifiers.each do |id|
376
- attributes[id] = glGetAttribLocation(program, id)
377
- end
378
-
379
- attributes
380
- end
381
-
382
- def link_status
383
- ptr = ' '*8
384
- glGetProgramiv @program, GL_LINK_STATUS, ptr
385
- ptr.unpack('L')[0]
386
- end
387
-
388
- def program_info_log
389
- ptr = ' '*8
390
- glGetProgramiv @program, GL_INFO_LOG_LENGTH, ptr
391
- length = ptr.unpack('L')[0]
392
-
393
- if length > 0
394
- log = ' '*length
395
- glGetProgramInfoLog @program, length, ptr, log
396
- log.unpack("A#{length}")[0]
397
- else
398
- ''
249
+ @attributes[id] = glGetAttribLocation(program, id)
399
250
  end
400
251
  end
401
252
  end