mittsu 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/examples/01_-_Default1noCulling.png +0 -0
- data/examples/01_scene_example.rb +14 -0
- data/examples/02_box_mesh_example.rb +30 -0
- data/examples/02_sphere_mesh_example.rb +30 -0
- data/examples/03_complex_object_example.rb +52 -0
- data/examples/04_ambient_light_example.rb +33 -0
- data/examples/04_dir_light_example.rb +36 -0
- data/examples/04_hemi_light_example.rb +30 -0
- data/examples/04_point_light_example.rb +50 -0
- data/examples/04_spot_light_example.rb +44 -0
- data/examples/05_earth_example.rb +42 -0
- data/examples/05_earth_moon_example.rb +46 -0
- data/examples/05_texture_example.rb +32 -0
- data/examples/06_cube_texture_example.rb +36 -0
- data/examples/06_skybox_example.rb +60 -0
- data/examples/07_earth_normal_example.rb +36 -0
- data/examples/08_shadow_example.rb +87 -0
- data/examples/09_line_example.rb +52 -0
- data/examples/10_obj_loader_example.rb +68 -0
- data/examples/11_character_input_example.rb +18 -0
- data/examples/11_continuous_keyboard_input_example.rb +35 -0
- data/examples/11_keyboard_input_example.rb +43 -0
- data/examples/12_mouse_click_example.rb +38 -0
- data/examples/12_mouse_motion_example.rb +35 -0
- data/examples/12_mouse_scroll_example.rb +36 -0
- data/examples/12_orbit_zoom_example.rb +68 -0
- data/examples/13_joystick_example.rb +80 -0
- data/examples/cubemap/tron_bk.png +0 -0
- data/examples/cubemap/tron_dn.png +0 -0
- data/examples/cubemap/tron_ft.png +0 -0
- data/examples/cubemap/tron_lf.png +0 -0
- data/examples/cubemap/tron_rt.png +0 -0
- data/examples/cubemap/tron_up.png +0 -0
- data/examples/earth.png +0 -0
- data/examples/earth_normal.png +0 -0
- data/examples/example_helper.rb +2 -0
- data/examples/male-02-1noCulling.png +0 -0
- data/examples/male02.mtl +54 -0
- data/examples/male02.obj +13888 -0
- data/examples/moon.png +0 -0
- data/examples/orig_02_-_Defaul1noCulling.png +0 -0
- data/examples/texture.png +0 -0
- data/lib/mittsu.rb +15 -0
- data/lib/mittsu/cameras.rb +4 -0
- data/lib/mittsu/cameras/camera.rb +34 -0
- data/lib/mittsu/cameras/cube_camera.rb +74 -0
- data/lib/mittsu/cameras/orthographic_camera.rb +53 -0
- data/lib/mittsu/cameras/perspective_camera.rb +115 -0
- data/lib/mittsu/constants.rb +160 -0
- data/lib/mittsu/core.rb +10 -0
- data/lib/mittsu/core/buffer_attribute.rb +87 -0
- data/lib/mittsu/core/buffer_geometry.rb +694 -0
- data/lib/mittsu/core/clock.rb +44 -0
- data/lib/mittsu/core/dynamic_buffer_attribute.rb +16 -0
- data/lib/mittsu/core/event_dispatcher.rb +39 -0
- data/lib/mittsu/core/face3.rb +30 -0
- data/lib/mittsu/core/geometry.rb +596 -0
- data/lib/mittsu/core/hash_array.rb +36 -0
- data/lib/mittsu/core/hash_object.rb +19 -0
- data/lib/mittsu/core/object_3d.rb +421 -0
- data/lib/mittsu/core/raycaster.rb +78 -0
- data/lib/mittsu/extras.rb +3 -0
- data/lib/mittsu/extras/geometries.rb +2 -0
- data/lib/mittsu/extras/geometries/box_geometry.rb +108 -0
- data/lib/mittsu/extras/geometries/sphere_geometry.rb +88 -0
- data/lib/mittsu/extras/helpers.rb +1 -0
- data/lib/mittsu/extras/helpers/camera_helper.rb +155 -0
- data/lib/mittsu/extras/image.rb +3 -0
- data/lib/mittsu/extras/image_utils.rb +80 -0
- data/lib/mittsu/lights.rb +7 -0
- data/lib/mittsu/lights/ambient_light.rb +16 -0
- data/lib/mittsu/lights/area_light.rb +24 -0
- data/lib/mittsu/lights/directional_light.rb +131 -0
- data/lib/mittsu/lights/hemisphere_light.rb +29 -0
- data/lib/mittsu/lights/light.rb +21 -0
- data/lib/mittsu/lights/point_light.rb +27 -0
- data/lib/mittsu/lights/spot_light.rb +104 -0
- data/lib/mittsu/loaders.rb +7 -0
- data/lib/mittsu/loaders/cache.rb +53 -0
- data/lib/mittsu/loaders/file_loader.rb +22 -0
- data/lib/mittsu/loaders/image_loader.rb +32 -0
- data/lib/mittsu/loaders/loader.rb +212 -0
- data/lib/mittsu/loaders/loading_manager.rb +17 -0
- data/lib/mittsu/loaders/mtl_loader.rb +242 -0
- data/lib/mittsu/loaders/obj_mtl_loader.rb +225 -0
- data/lib/mittsu/materials.rb +7 -0
- data/lib/mittsu/materials/line_basic_material.rb +39 -0
- data/lib/mittsu/materials/material.rb +156 -0
- data/lib/mittsu/materials/mesh_basic_material.rb +122 -0
- data/lib/mittsu/materials/mesh_face_material.rb +30 -0
- data/lib/mittsu/materials/mesh_lambert_material.rb +126 -0
- data/lib/mittsu/materials/mesh_phong_material.rb +152 -0
- data/lib/mittsu/materials/shader_material.rb +108 -0
- data/lib/mittsu/math.rb +105 -0
- data/lib/mittsu/math/box2.rb +135 -0
- data/lib/mittsu/math/box3.rb +194 -0
- data/lib/mittsu/math/color.rb +252 -0
- data/lib/mittsu/math/color_keywords.rb +151 -0
- data/lib/mittsu/math/euler.rb +182 -0
- data/lib/mittsu/math/frustum.rb +106 -0
- data/lib/mittsu/math/line3.rb +76 -0
- data/lib/mittsu/math/matrix3.rb +163 -0
- data/lib/mittsu/math/matrix4.rb +581 -0
- data/lib/mittsu/math/plane.rb +128 -0
- data/lib/mittsu/math/quaternion.rb +309 -0
- data/lib/mittsu/math/ray.rb +292 -0
- data/lib/mittsu/math/sphere.rb +91 -0
- data/lib/mittsu/math/spline.rb +128 -0
- data/lib/mittsu/math/triangle.rb +121 -0
- data/lib/mittsu/math/vector2.rb +238 -0
- data/lib/mittsu/math/vector3.rb +491 -0
- data/lib/mittsu/math/vector4.rb +414 -0
- data/lib/mittsu/objects.rb +3 -0
- data/lib/mittsu/objects/group.rb +8 -0
- data/lib/mittsu/objects/line.rb +143 -0
- data/lib/mittsu/objects/mesh.rb +243 -0
- data/lib/mittsu/renderers.rb +1 -0
- data/lib/mittsu/renderers/glfw_window.rb +216 -0
- data/lib/mittsu/renderers/opengl/opengl_debug.rb +38 -0
- data/lib/mittsu/renderers/opengl/opengl_program.rb +402 -0
- data/lib/mittsu/renderers/opengl/opengl_shader.rb +58 -0
- data/lib/mittsu/renderers/opengl/opengl_state.rb +207 -0
- data/lib/mittsu/renderers/opengl/plugins/shadow_map_plugin.rb +416 -0
- data/lib/mittsu/renderers/opengl_render_target.rb +87 -0
- data/lib/mittsu/renderers/opengl_renderer.rb +3376 -0
- data/lib/mittsu/renderers/shaders/shader_chunk.rb +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/alphamap_pars_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/alphatest_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/bumpmap_pars_fragment.glsl +40 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_pars_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/color_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/common.glsl +60 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/default_vertex.glsl +15 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/defaultnormal_vertex.glsl +21 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_fragment.glsl +62 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_fragment.glsl +21 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_pars_vertex.glsl +7 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/envmap_vertex.glsl +17 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/fog_fragment.glsl +26 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/fog_pars_fragment.glsl +15 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_fragment.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_pars_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lightmap_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_pars_vertex.glsl +43 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_lambert_vertex.glsl +196 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_fragment.glsl +243 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_fragment.glsl +58 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_pars_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/lights_phong_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/linear_to_gamma_fragment.glsl +2 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_fragment.glsl +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_pars_vertex.glsl +11 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/logdepthbuf_vertex.glsl +15 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_fragment.glsl +9 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_fragment.glsl +11 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_pars_vertex.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_particle_pars_fragment.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/map_vertex.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/morphnormal_vertex.glsl +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_pars_vertex.glsl +13 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/morphtarget_vertex.glsl +20 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/normalmap_pars_fragment.glsl +27 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_fragment.glsl +217 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_fragment.glsl +19 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_pars_vertex.glsl +6 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/shadowmap_vertex.glsl +9 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinbase_vertex.glsl +8 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinning_pars_vertex.glsl +47 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinning_vertex.glsl +20 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/skinnormal_vertex.glsl +20 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_fragment.glsl +12 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/specularmap_pars_fragment.glsl +5 -0
- data/lib/mittsu/renderers/shaders/shader_chunk/worldpos_vertex.glsl +17 -0
- data/lib/mittsu/renderers/shaders/shader_lib.rb +420 -0
- data/lib/mittsu/renderers/shaders/uniforms_lib.rb +107 -0
- data/lib/mittsu/renderers/shaders/uniforms_utils.rb +31 -0
- data/lib/mittsu/scenes.rb +1 -0
- data/lib/mittsu/scenes/scene.rb +27 -0
- data/lib/mittsu/textures.rb +5 -0
- data/lib/mittsu/textures/compressed_texture.rb +30 -0
- data/lib/mittsu/textures/cube_texture.rb +19 -0
- data/lib/mittsu/textures/data_texture.rb +17 -0
- data/lib/mittsu/textures/texture.rb +92 -0
- data/lib/mittsu/textures/video_texture.rb +17 -0
- data/lib/mittsu/version.rb +4 -0
- data/mittsu.gemspec +31 -0
- metadata +357 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'opengl'
|
2
|
+
|
3
|
+
ERROR_STRINGS = {
|
4
|
+
OpenGL::GL_NO_ERROR => 'NO_ERROR',
|
5
|
+
OpenGL::GL_INVALID_ENUM => 'INVALID_ENUM',
|
6
|
+
OpenGL::GL_INVALID_VALUE => 'INVALID_VALUE',
|
7
|
+
OpenGL::GL_INVALID_OPERATION => 'INVALID_OPERATION',
|
8
|
+
OpenGL::GL_STACK_OVERFLOW => 'STACK_OVERFLOW',
|
9
|
+
OpenGL::GL_STACK_UNDERFLOW => 'STACK_UNDERFLOW',
|
10
|
+
OpenGL::GL_OUT_OF_MEMORY => 'OUT_OF_MEMORY',
|
11
|
+
# OpenGL::GL_TABLE_TOO_LARGE => 'TABLE_TOO_LARGE'
|
12
|
+
}
|
13
|
+
|
14
|
+
module OpenGLDebug
|
15
|
+
module OpenGLProxy
|
16
|
+
extend OpenGL
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.load_lib
|
20
|
+
OpenGL.load_lib
|
21
|
+
end
|
22
|
+
|
23
|
+
OpenGL.constants.each do |c|
|
24
|
+
const_set c, OpenGL.const_get(c)
|
25
|
+
end
|
26
|
+
|
27
|
+
OpenGL.instance_methods.each do |m|
|
28
|
+
define_method m do |*args|
|
29
|
+
r = OpenGLProxy.send(m, *args)
|
30
|
+
call = "#{m}(#{args.map { |s| s.to_s[0..20] }.join(', ')})"
|
31
|
+
ret = r.nil? ? '' : " => #{r}"
|
32
|
+
puts "#{call}#{ret}"
|
33
|
+
e = OpenGLProxy.glGetError
|
34
|
+
raise "ERROR: #{m} => #{ERROR_STRINGS[e]}" unless e == OpenGL::GL_NO_ERROR
|
35
|
+
r
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,402 @@
|
|
1
|
+
require 'mittsu/renderers/opengl/opengl_shader'
|
2
|
+
|
3
|
+
module Mittsu
|
4
|
+
class OpenGLProgram
|
5
|
+
attr_reader :id, :program, :uniforms, :attributes
|
6
|
+
attr_accessor :code, :used_times, :attributes, :vertex_shader, :fragment_shader
|
7
|
+
|
8
|
+
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... ?
|
12
|
+
|
13
|
+
vertex_shader = material[:_opengl_shader][:vertex_shader]
|
14
|
+
fragment_shader = material[:_opengl_shader][:fragment_shader]
|
15
|
+
|
16
|
+
# TODO: necessary for OpenGL?
|
17
|
+
# index0_attribute_name = material.index0_attribute_name
|
18
|
+
#
|
19
|
+
# if index0_attribute_name.nil? && parameters[:morph_targets]
|
20
|
+
# # programs with morph_targets displace position of attribute 0
|
21
|
+
#
|
22
|
+
# index0_attribute_name = 'position'
|
23
|
+
# end
|
24
|
+
|
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',
|
150
|
+
|
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?
|
191
|
+
|
192
|
+
custom_defines,
|
193
|
+
|
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]}",
|
198
|
+
|
199
|
+
"#define MAX_SHADOWS #{parameters[:max_shadows]}",
|
200
|
+
|
201
|
+
parameters[:alpha_test] ? "#define ALPHATEST #{parameters[:alpha_test].to_f}" : '',
|
202
|
+
|
203
|
+
renderer.gamma_input ? '#define GAMMA_INPUT' : '',
|
204
|
+
renderer.gamma_output ? '#define GAMMA_OUTPUT' : '',
|
205
|
+
"#define GAMMA_FACTOR #{gamma_factor_define}",
|
206
|
+
|
207
|
+
(parameters[:use_fog] && parameters[:fog]) ? '#define USE_FOG' : '',
|
208
|
+
(parameters[:use_fog] && parameters[:fog_exp]) ? '#define FOG_EXP2' : '',
|
209
|
+
|
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' : '',
|
221
|
+
|
222
|
+
parameters[:flat_shading] ? '#define FLAT_SHADED' : '',
|
223
|
+
|
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' : '',
|
228
|
+
|
229
|
+
|
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' : '',
|
234
|
+
|
235
|
+
parameters[:logarithmic_depth_buffer] ? '#define USE_LOGDEPTHBUF' : '',
|
236
|
+
#renderer._glExtensionFragDepth ? '#define USE_LOGDEPTHBUF_EXT' : '',
|
237
|
+
|
238
|
+
'uniform mat4 viewMatrix;',
|
239
|
+
'uniform vec3 cameraPosition;',
|
240
|
+
|
241
|
+
'layout(location = 0) out vec4 fragColor;'
|
242
|
+
].reject(&:empty?).join("\n") + "\n"
|
243
|
+
end
|
244
|
+
|
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)
|
247
|
+
|
248
|
+
glAttachShader(@program, gl_vertex_shader.shader)
|
249
|
+
glAttachShader(@program, gl_fragment_shader.shader)
|
250
|
+
|
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
|
256
|
+
|
257
|
+
# glBindAttributeLocation(program, 0, index0_attribute_name)
|
258
|
+
# end
|
259
|
+
|
260
|
+
glLinkProgram(@program)
|
261
|
+
|
262
|
+
log_info = program_info_log
|
263
|
+
|
264
|
+
if !link_status
|
265
|
+
puts "ERROR: Mittsu::OpenGLProgram: shader error: #{glGetError}, GL_INVALID_STATUS, #{glGetProgramParameter(program, GL_VALIDATE_STATUS)}, glGetProgramParameterInfoLog, #{program_log_info}"
|
266
|
+
end
|
267
|
+
|
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
|
274
|
+
|
275
|
+
# clean up
|
276
|
+
|
277
|
+
glDeleteShader(gl_vertex_shader.shader)
|
278
|
+
glDeleteShader(gl_fragment_shader.shader)
|
279
|
+
|
280
|
+
# cache uniform locations
|
281
|
+
|
282
|
+
identifiers = [
|
283
|
+
'viewMatrix',
|
284
|
+
'modelViewMatrix',
|
285
|
+
'projectionMatrix',
|
286
|
+
'normalMatrix',
|
287
|
+
'modelMatrix',
|
288
|
+
'cameraPosition',
|
289
|
+
'morphTargetInfluences',
|
290
|
+
'bindMatrix',
|
291
|
+
'bindMatrixInverse'
|
292
|
+
]
|
293
|
+
|
294
|
+
if parameters[:use_vertex_texture]
|
295
|
+
identifiers << 'boneTexture'
|
296
|
+
identifiers << 'boneTextureWidth'
|
297
|
+
identifiers << 'boneTextureHeight'
|
298
|
+
else
|
299
|
+
identifiers << 'boneGlobalMatrices'
|
300
|
+
end
|
301
|
+
|
302
|
+
if parameters[:logarithmic_depth_buffer]
|
303
|
+
identifiers << 'logDepthBufFC'
|
304
|
+
end
|
305
|
+
|
306
|
+
uniforms.each do |k, v|
|
307
|
+
identifiers << k.to_s
|
308
|
+
end
|
309
|
+
|
310
|
+
@uniforms = cache_uniform_locations(program, identifiers)
|
311
|
+
|
312
|
+
# cache attributes locations
|
313
|
+
|
314
|
+
identifiers = [
|
315
|
+
'position',
|
316
|
+
'normal',
|
317
|
+
'uv',
|
318
|
+
'uv2',
|
319
|
+
'tangent',
|
320
|
+
'color',
|
321
|
+
'skinIndex',
|
322
|
+
'skinWeight',
|
323
|
+
'lineDistance'
|
324
|
+
]
|
325
|
+
|
326
|
+
parameters[:max_morph_targets].times do |i|
|
327
|
+
identifiers << "morphTarget#{i}"
|
328
|
+
end
|
329
|
+
|
330
|
+
parameters[:max_morph_normals].times do |i|
|
331
|
+
identifiers << "morphNormal#{i}"
|
332
|
+
end
|
333
|
+
|
334
|
+
attributes.each do |k, v|
|
335
|
+
identifiers << k
|
336
|
+
end
|
337
|
+
|
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
|
+
|
375
|
+
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
|
+
''
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'fiddle'
|
2
|
+
|
3
|
+
module Mittsu
|
4
|
+
class OpenGLShader
|
5
|
+
attr_reader :shader
|
6
|
+
|
7
|
+
def initialize(type, string)
|
8
|
+
@shader = glCreateShader(type)
|
9
|
+
# filename = type == GL_VERTEX_SHADER ? 'vertex.glsl' : 'fragment.glsl'
|
10
|
+
# File.write filename, string
|
11
|
+
|
12
|
+
string_pointer = Fiddle::Pointer[string]
|
13
|
+
string_length = Fiddle::Pointer[string.length]
|
14
|
+
|
15
|
+
glShaderSource(@shader, 1, string_pointer.ref, string_length.ref)
|
16
|
+
glCompileShader(@shader)
|
17
|
+
|
18
|
+
if !compile_status
|
19
|
+
puts "ERROR: Mittsu::OpenGLShader: Shader couldn't compile"
|
20
|
+
end
|
21
|
+
|
22
|
+
log_info = shader_info_log
|
23
|
+
if !log_info.empty?
|
24
|
+
puts "WARNING: Mittsu::OpenGLShader: glGetShaderInfoLog, #{log_info}"
|
25
|
+
puts add_line_numbers(string)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def compile_status
|
32
|
+
ptr = ' '*8
|
33
|
+
glGetShaderiv @shader, GL_COMPILE_STATUS, ptr
|
34
|
+
ptr.unpack('L')[0]
|
35
|
+
end
|
36
|
+
|
37
|
+
def shader_info_log
|
38
|
+
ptr = ' '*8
|
39
|
+
glGetShaderiv @shader, GL_INFO_LOG_LENGTH, ptr
|
40
|
+
length = ptr.unpack('L')[0]
|
41
|
+
|
42
|
+
if length > 0
|
43
|
+
log = ' '*length
|
44
|
+
glGetShaderInfoLog @shader, length, ptr, log
|
45
|
+
log.unpack("A#{length}")[0]
|
46
|
+
else
|
47
|
+
''
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def add_line_numbers(string)
|
52
|
+
string.split("\n").each_with_index.map { |line, i|
|
53
|
+
line_number = "#{i + 1}".rjust(4, ' ')
|
54
|
+
"#{line_number}: #{line}"
|
55
|
+
}.join("\n")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|