aa9600cd4332869186004c71983e93c711be2efa
[nit.git] / lib / gamnit / depth / more_materials.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Various material implementations
16 module more_materials
17
18 intrude import depth_core
19 intrude import flat
20
21 # Simple material with static colors used for debugging or display abstract objects
22 class SmoothMaterial
23 super Material
24
25 # Get the default blueish material
26 init default do init(
27 [0.0, 0.0, 0.3, 1.0],
28 [0.0, 0.0, 0.6, 1.0],
29 [1.0, 1.0, 1.0, 1.0])
30
31 # Ambient color, always visible
32 var ambient_color: Array[Float] is writable
33
34 # Diffuse color when covered by a light source
35 var diffuse_color: Array[Float] is writable
36
37 # Specular color affecting reflections
38 var specular_color: Array[Float] is writable
39
40 redef fun draw(actor, model)
41 do
42 var program = app.versatile_program
43 program.use
44
45 var mesh = model.mesh
46
47 # Actor specs
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)
51
52 # From mesh
53 program.coord.array_enabled = true
54 program.coord.array(mesh.vertices, 3)
55
56 program.normal.array_enabled = true
57 program.normal.array(mesh.normals, 3)
58
59 # No textures
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
64
65 # Lights
66 program.light_center.uniform(app.light.position.x, app.light.position.y, app.light.position.z)
67
68 # Camera
69 program.camera.uniform(app.world_camera.position.x, app.world_camera.position.y, app.world_camera.position.z)
70
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)
75
76 # Execute draw
77 if mesh.indices.is_empty then
78 glDrawArrays(gl_TRIANGLES, 0, mesh.vertices.length/3)
79 else
80 glDrawElements(gl_TRIANGLES, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
81 end
82 end
83 end
84
85 # Material with potential `diffuse_texture` and `specular_texture`
86 class TexturedMaterial
87 super SmoothMaterial
88
89 # Texture applied to the ambient_color
90 var ambient_texture: nullable Texture = null is writable
91
92 # Texture applied to the diffuse color
93 var diffuse_texture: nullable Texture = null is writable
94
95 # Texture applied to the specular color
96 var specular_texture: nullable Texture = null is writable
97
98 redef fun draw(actor, model)
99 do
100 var mesh = model.mesh
101
102 var program = app.versatile_program
103 program.use
104
105 # One of the textures used, if any
106 var sample_used_texture = null
107
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
115 else
116 program.use_map_ambient.uniform false
117 end
118
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
126 else
127 program.use_map_diffuse.uniform false
128 end
129
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
137 else
138 program.use_map_specular.uniform false
139 end
140
141 program.translation.uniform(actor.center.x, actor.center.y, actor.center.z, 0.0)
142 program.scale.uniform actor.scale
143
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)
150 else
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
157
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]
162 end
163
164 program.tex_coord.array(tex_coords, 2)
165 end
166 end
167
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)
171
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)
175
176 program.normal.array_enabled = true
177 program.normal.array(mesh.normals, 3)
178
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)
181
182 if mesh.indices.is_empty then
183 glDrawArrays(gl_TRIANGLES, 0, mesh.vertices.length/3)
184 else
185 glDrawElements(gl_TRIANGLES, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
186 end
187 end
188 end
189
190 # Simple material using the normals of the surface as color
191 #
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
195 super Material
196
197 redef fun draw(actor, model)
198 do
199 var program = app.normals_program
200 program.use
201 program.mvp.uniform app.world_camera.mvp_matrix
202
203 var mesh = model.mesh
204
205 # TODO apply normal map
206
207 program.translation.uniform(actor.center.x, actor.center.y, actor.center.z, 0.0)
208 program.scale.uniform actor.scale
209
210 program.tex_coord.array_enabled = true
211 program.tex_coord.array(mesh.texture_coords, 2)
212
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)
216
217 program.normal.array_enabled = true
218 program.normal.array(mesh.normals, 3)
219
220 if mesh.indices.is_empty then
221 glDrawArrays(gl_TRIANGLES, 0, mesh.vertices.length/3)
222 else
223 glDrawElements(gl_TRIANGLES, mesh.indices.length, gl_UNSIGNED_SHORT, mesh.indices_c.native_array)
224 end
225 end
226 end
227
228 # Graphic program to display 3D models with Lamber diffuse lighting
229 class LambertProgram
230 super GamnitProgramFromSource
231
232 redef var vertex_shader_source = """
233 // Vertex coordinates
234 attribute vec4 coord;
235
236 // Vertex translation
237 uniform vec4 translation;
238
239 // Vertex scaling
240 uniform float scale;
241
242 // Vertex coordinates on textures
243 attribute vec2 tex_coord;
244
245 // Vertex normal
246 attribute vec3 normal;
247
248 // Model view projection matrix
249 uniform mat4 mvp;
250
251 uniform mat4 rotation;
252
253 // Lights config
254 uniform vec3 light_center;
255
256 // Coordinates of the camera
257 uniform vec3 camera;
258
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;
264
265 void main()
266 {
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;
270
271 gl_Position = (vec4(coord.xyz * scale, 1.0) * rotation + translation) * mvp;
272
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;
276 }
277 """ @ glsl_vertex_shader
278
279 redef var fragment_shader_source = """
280 precision mediump float;
281
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;
287
288 // Colors
289 uniform vec4 ambient_color;
290 uniform vec4 diffuse_color;
291 uniform vec4 specular_color;
292
293 // Ambient map
294 uniform bool use_map_ambient;
295 uniform sampler2D map_ambient;
296
297 // Diffuse map
298 uniform bool use_map_diffuse;
299 uniform sampler2D map_diffuse;
300
301 // Specular map
302 uniform bool use_map_specular;
303 uniform sampler2D map_specular;
304
305 // Bump map
306 uniform bool use_map_bump;
307 uniform sampler2D map_bump;
308
309 // Normal map
310 uniform bool use_map_normal;
311 uniform sampler2D map_normal;
312
313 void main()
314 {
315 // Lambert diffusion
316 vec3 light_dir = normalize(v_light_center.xyz);
317 float lambert = max(dot(light_dir, v_normal), 0.0);
318
319 if (use_map_ambient)
320 gl_FragColor = ambient_color * texture2D(map_ambient, v_tex_coord);
321 else
322 gl_FragColor = ambient_color;
323
324 if (use_map_diffuse)
325 gl_FragColor += lambert * diffuse_color * texture2D(map_diffuse, v_tex_coord);
326 else
327 gl_FragColor += lambert * diffuse_color;
328 }
329 """ @ glsl_fragment_shader
330
331 # Vertices coordinates
332 var coord = attributes["coord"].as(AttributeVec4) is lazy
333
334 # Should this program use the texture `map_ambient`?
335 var use_map_ambient = uniforms["use_map_ambient"].as(UniformBool) is lazy
336
337 # Ambient texture unit
338 var map_ambient = uniforms["map_ambient"].as(UniformSampler2D) is lazy
339
340 # Should this program use the texture `map_diffuse`?
341 var use_map_diffuse = uniforms["use_map_diffuse"].as(UniformBool) is lazy
342
343 # Diffuser texture unit
344 var map_diffuse = uniforms["map_diffuse"].as(UniformSampler2D) is lazy
345
346 # Should this program use the texture `map_specular`?
347 var use_map_specular = uniforms["use_map_specular"].as(UniformBool) is lazy
348
349 # Specularity texture unit
350 var map_specular = uniforms["map_specular"].as(UniformSampler2D) is lazy
351
352 # Normal per vertex
353 var normal = attributes["normal"].as(AttributeVec3) is lazy
354
355 # Coordinates on the textures, per vertex
356 var tex_coord = attributes["tex_coord"].as(AttributeVec2) is lazy
357
358 # Ambient color
359 var ambient_color = uniforms["ambient_color"].as(UniformVec4) is lazy
360
361 # Diffuse color
362 var diffuse_color = uniforms["diffuse_color"].as(UniformVec4) is lazy
363
364 # Specular color
365 var specular_color = uniforms["specular_color"].as(UniformVec4) is lazy
366
367 # Center position of the light
368 var light_center = uniforms["light_center"].as(UniformVec3) is lazy
369
370 # Camera position
371 var camera = uniforms["camera"].as(UniformVec3) is lazy
372
373 # Translation applied to each vertex
374 var translation = uniforms["translation"].as(UniformVec4) is lazy
375
376 # Rotation matrix
377 var rotation = uniforms["rotation"].as(UniformMat4) is lazy
378
379 # Scaling per vertex
380 var scale = uniforms["scale"].as(UniformFloat) is lazy
381
382 # Model view projection matrix
383 var mvp = uniforms["mvp"].as(UniformMat4) is lazy
384 end
385
386 # Program to color objects from their normal vectors
387 class NormalProgram
388 super LambertProgram
389
390 redef var fragment_shader_source = """
391 precision mediump float;
392
393 // Input from the vertex shader
394 varying vec3 v_normal;
395
396 void main()
397 {
398 gl_FragColor = vec4(v_normal*0.5 + 0.5, 1.0);
399 }
400 """ @ glsl_fragment_shader
401 end
402
403 redef class App
404 private var versatile_program = new LambertProgram is lazy
405
406 private var normals_program = new NormalProgram is lazy
407 end