1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Various material implementations
18 intrude import depth_core
21 # Simple material with static colors used for debugging or display abstract objects
25 # Get the default blueish material
31 # Ambient color, always visible
32 var ambient_color
: Array[Float] is writable
34 # Diffuse color when covered by a light source
35 var diffuse_color
: Array[Float] is writable
37 # Specular color affecting reflections
38 var specular_color
: Array[Float] is writable
40 redef fun draw
(actor
, model
)
42 var program
= app
.versatile_program
48 program
.translation
.uniform
(actor
.center
.x
, actor
.center
.y
, actor
.center
.z
, 0.0)
49 program
.scale
.uniform actor
.scale
50 program
.rotation
.uniform
new Matrix.rotation
(actor
.rotation
, 0.0, 1.0, 0.0)
53 program
.coord
.array_enabled
= true
54 program
.coord
.array
(mesh
.vertices
, 3)
56 program
.normal
.array_enabled
= true
57 program
.normal
.array
(mesh
.normals
, 3)
60 program
.use_map_ambient
.uniform
false
61 program
.use_map_diffuse
.uniform
false
62 program
.use_map_specular
.uniform
false
63 program
.tex_coord
.array_enabled
= false
66 program
.light_center
.uniform
(app
.light
.position
.x
, app
.light
.position
.y
, app
.light
.position
.z
)
69 program
.camera
.uniform
(app
.world_camera
.position
.x
, app
.world_camera
.position
.y
, app
.world_camera
.position
.z
)
71 # Colors from the material
72 program
.ambient_color
.uniform
(ambient_color
[0], ambient_color
[1], ambient_color
[2], ambient_color
[3]*actor
.alpha
)
73 program
.diffuse_color
.uniform
(diffuse_color
[0], diffuse_color
[1], diffuse_color
[2], diffuse_color
[3]*actor
.alpha
)
74 program
.specular_color
.uniform
(specular_color
[0], specular_color
[1], specular_color
[2], specular_color
[3]*actor
.alpha
)
77 if mesh
.indices
.is_empty
then
78 glDrawArrays
(gl_TRIANGLES
, 0, mesh
.vertices
.length
/3)
80 glDrawElements
(gl_TRIANGLES
, mesh
.indices
.length
, gl_UNSIGNED_SHORT
, mesh
.indices_c
.native_array
)
85 # Material with potential `diffuse_texture` and `specular_texture`
86 class TexturedMaterial
89 # Texture applied to the ambient_color
90 var ambient_texture
: nullable Texture = null is writable
92 # Texture applied to the diffuse color
93 var diffuse_texture
: nullable Texture = null is writable
95 # Texture applied to the specular color
96 var specular_texture
: nullable Texture = null is writable
98 redef fun draw
(actor
, model
)
100 var mesh
= model
.mesh
102 var program
= app
.versatile_program
105 # One of the textures used, if any
106 var sample_used_texture
= null
108 var texture
= ambient_texture
109 if texture
!= null then
110 glActiveTexture gl_TEXTURE0
111 glBindTexture
(gl_TEXTURE_2D
, texture
.gl_texture
)
112 program
.use_map_ambient
.uniform
true
113 program
.map_ambient
.uniform
0
114 sample_used_texture
= texture
116 program
.use_map_ambient
.uniform
false
119 texture
= diffuse_texture
120 if texture
!= null then
121 glActiveTexture gl_TEXTURE1
122 glBindTexture
(gl_TEXTURE_2D
, texture
.gl_texture
)
123 program
.use_map_diffuse
.uniform
true
124 program
.map_diffuse
.uniform
1
125 sample_used_texture
= texture
127 program
.use_map_diffuse
.uniform
false
130 texture
= specular_texture
131 if texture
!= null then
132 glActiveTexture gl_TEXTURE2
133 glBindTexture
(gl_TEXTURE_2D
, texture
.gl_texture
)
134 program
.use_map_specular
.uniform
true
135 program
.map_specular
.uniform
2
136 sample_used_texture
= texture
138 program
.use_map_specular
.uniform
false
141 program
.translation
.uniform
(actor
.center
.x
, actor
.center
.y
, actor
.center
.z
, 0.0)
142 program
.scale
.uniform actor
.scale
144 # If using a texture, set `texture_coords`
145 program
.tex_coord
.array_enabled
= sample_used_texture
!= null
146 if sample_used_texture
!= null then
147 if sample_used_texture
isa GamnitRootTexture then
148 # Coordinates are directly valid
149 program
.tex_coord
.array
(mesh
.texture_coords
, 2)
151 # Correlate texture coordinates from the substexture and the mesh.
152 # This is slow, but should be cached on the GPU.
153 var xa
= sample_used_texture
.offset_left
154 var xd
= sample_used_texture
.offset_right
- xa
155 var ya
= sample_used_texture
.offset_top
156 var yd
= sample_used_texture
.offset_bottom
- ya
158 var tex_coords
= new Array[Float].with_capacity
(mesh
.texture_coords
.length
)
159 for i
in [0..mesh
.texture_coords
.length
/2[ do
160 tex_coords
[i
*2] = xa
+ xd
* mesh
.texture_coords
[i
*2]
161 tex_coords
[i
*2+1] = ya
+ yd
* mesh
.texture_coords
[i
*2+1]
164 program
.tex_coord
.array
(tex_coords
, 2)
168 program
.coord
.array_enabled
= true
169 program
.coord
.array
(mesh
.vertices
, 3)
170 program
.rotation
.uniform
new Matrix.rotation
(actor
.rotation
, 0.0, 1.0, 0.0)
172 program
.ambient_color
.uniform
(ambient_color
[0], ambient_color
[1], ambient_color
[2], ambient_color
[3]*actor
.alpha
)
173 program
.diffuse_color
.uniform
(diffuse_color
[0], diffuse_color
[1], diffuse_color
[2], diffuse_color
[3]*actor
.alpha
)
174 program
.specular_color
.uniform
(specular_color
[0], specular_color
[1], specular_color
[2], specular_color
[3]*actor
.alpha
)
176 program
.normal
.array_enabled
= true
177 program
.normal
.array
(mesh
.normals
, 3)
179 program
.light_center
.uniform
(app
.light
.position
.x
, app
.light
.position
.y
, app
.light
.position
.z
)
180 program
.camera
.uniform
(app
.world_camera
.position
.x
, app
.world_camera
.position
.y
, app
.world_camera
.position
.z
)
182 if mesh
.indices
.is_empty
then
183 glDrawArrays
(gl_TRIANGLES
, 0, mesh
.vertices
.length
/3)
185 glDrawElements
(gl_TRIANGLES
, mesh
.indices
.length
, gl_UNSIGNED_SHORT
, mesh
.indices_c
.native_array
)
190 # Simple material using the normals of the surface as color
192 # Each axis composing the normals are translated to color values.
193 # This material is useful for debugging normals or display models in a colorful way.
194 class NormalsMaterial
197 redef fun draw
(actor
, model
)
199 var program
= app
.normals_program
201 program
.mvp
.uniform app
.world_camera
.mvp_matrix
203 var mesh
= model
.mesh
205 # TODO apply normal map
207 program
.translation
.uniform
(actor
.center
.x
, actor
.center
.y
, actor
.center
.z
, 0.0)
208 program
.scale
.uniform actor
.scale
210 program
.tex_coord
.array_enabled
= true
211 program
.tex_coord
.array
(mesh
.texture_coords
, 2)
213 program
.coord
.array_enabled
= true
214 program
.coord
.array
(mesh
.vertices
, 3)
215 program
.rotation
.uniform
new Matrix.rotation
(actor
.rotation
, 0.0, 1.0, 0.0)
217 program
.normal
.array_enabled
= true
218 program
.normal
.array
(mesh
.normals
, 3)
220 if mesh
.indices
.is_empty
then
221 glDrawArrays
(gl_TRIANGLES
, 0, mesh
.vertices
.length
/3)
223 glDrawElements
(gl_TRIANGLES
, mesh
.indices
.length
, gl_UNSIGNED_SHORT
, mesh
.indices_c
.native_array
)
228 # Graphic program to display 3D models with Lamber diffuse lighting
230 super GamnitProgramFromSource
232 redef var vertex_shader_source
= """
233 // Vertex coordinates
234 attribute vec4 coord;
236 // Vertex translation
237 uniform vec4 translation;
242 // Vertex coordinates on textures
243 attribute vec2 tex_coord;
246 attribute vec3 normal;
248 // Model view projection matrix
251 uniform mat4 rotation;
254 uniform vec3 light_center;
256 // Coordinates of the camera
259 // Output for the fragment shader
260 varying vec2 v_tex_coord;
261 varying vec3 v_normal;
262 varying vec4 v_light_center;
263 varying vec4 v_camera;
267 // Pass varyings to the fragment shader
268 v_tex_coord = vec2(tex_coord.x, 1.0 - tex_coord.y);
269 v_normal = normalize(vec4(normal, 0.0) * rotation * mvp).xyz;
271 gl_Position = (vec4(coord.xyz * scale, 1.0) * rotation + translation) * mvp;
273 // TODO compute v_light_center and v_camera on the CPU side and pass as uniforms
274 v_light_center = vec4(light_center, 0.0) * mvp;
275 v_camera = vec4(camera, 0.0) * mvp;
277 """ @ glsl_vertex_shader
279 redef var fragment_shader_source
= """
280 precision mediump float;
282 // Input from the vertex shader
283 varying vec2 v_tex_coord;
284 varying vec3 v_normal;
285 varying vec4 v_light_center;
286 varying vec4 v_camera;
289 uniform vec4 ambient_color;
290 uniform vec4 diffuse_color;
291 uniform vec4 specular_color;
294 uniform bool use_map_ambient;
295 uniform sampler2D map_ambient;
298 uniform bool use_map_diffuse;
299 uniform sampler2D map_diffuse;
302 uniform bool use_map_specular;
303 uniform sampler2D map_specular;
306 uniform bool use_map_bump;
307 uniform sampler2D map_bump;
310 uniform bool use_map_normal;
311 uniform sampler2D map_normal;
316 vec3 light_dir = normalize(v_light_center.xyz);
317 float lambert = max(dot(light_dir, v_normal), 0.0);
320 gl_FragColor = ambient_color * texture2D(map_ambient, v_tex_coord);
322 gl_FragColor = ambient_color;
325 gl_FragColor += lambert * diffuse_color * texture2D(map_diffuse, v_tex_coord);
327 gl_FragColor += lambert * diffuse_color;
329 """ @ glsl_fragment_shader
331 # Vertices coordinates
332 var coord
= attributes
["coord"].as(AttributeVec4) is lazy
334 # Should this program use the texture `map_ambient`?
335 var use_map_ambient
= uniforms
["use_map_ambient"].as(UniformBool) is lazy
337 # Ambient texture unit
338 var map_ambient
= uniforms
["map_ambient"].as(UniformSampler2D) is lazy
340 # Should this program use the texture `map_diffuse`?
341 var use_map_diffuse
= uniforms
["use_map_diffuse"].as(UniformBool) is lazy
343 # Diffuser texture unit
344 var map_diffuse
= uniforms
["map_diffuse"].as(UniformSampler2D) is lazy
346 # Should this program use the texture `map_specular`?
347 var use_map_specular
= uniforms
["use_map_specular"].as(UniformBool) is lazy
349 # Specularity texture unit
350 var map_specular
= uniforms
["map_specular"].as(UniformSampler2D) is lazy
353 var normal
= attributes
["normal"].as(AttributeVec3) is lazy
355 # Coordinates on the textures, per vertex
356 var tex_coord
= attributes
["tex_coord"].as(AttributeVec2) is lazy
359 var ambient_color
= uniforms
["ambient_color"].as(UniformVec4) is lazy
362 var diffuse_color
= uniforms
["diffuse_color"].as(UniformVec4) is lazy
365 var specular_color
= uniforms
["specular_color"].as(UniformVec4) is lazy
367 # Center position of the light
368 var light_center
= uniforms
["light_center"].as(UniformVec3) is lazy
371 var camera
= uniforms
["camera"].as(UniformVec3) is lazy
373 # Translation applied to each vertex
374 var translation
= uniforms
["translation"].as(UniformVec4) is lazy
377 var rotation
= uniforms
["rotation"].as(UniformMat4) is lazy
380 var scale
= uniforms
["scale"].as(UniformFloat) is lazy
382 # Model view projection matrix
383 var mvp
= uniforms
["mvp"].as(UniformMat4) is lazy
386 # Program to color objects from their normal vectors
390 redef var fragment_shader_source
= """
391 precision mediump float;
393 // Input from the vertex shader
394 varying vec3 v_normal;
398 gl_FragColor = vec4(v_normal*0.5 + 0.5, 1.0);
400 """ @ glsl_fragment_shader
404 private var versatile_program
= new LambertProgram is lazy
406 private var normals_program
= new NormalProgram is lazy