12d0f15fc439c349176ae17df51cf4f7d334a45a
[nit.git] / lib / gamnit / depth / depth.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 # Framework for 3D games in Nit
16 module depth
17
18 intrude import more_materials
19 import more_models
20 import particles
21
22 redef class App
23
24 redef fun on_create
25 do
26 super
27
28 # Move the camera back a bit
29 world_camera.reset_height(10.0)
30 world_camera.near = 0.1
31
32 # Prepare programs
33 var programs = [versatile_program, normals_program, explosion_program, smoke_program, static_program: GamnitProgram]
34 for program in programs do
35 program.compile_and_link
36 var gamnit_error = program.error
37 assert gamnit_error == null else print_error gamnit_error
38 end
39 end
40
41 redef fun frame_core_draw(display) do frame_core_depth display
42
43 # Draw all elements of `actors` and then call `frame_core_flat`
44 protected fun frame_core_depth(display: GamnitDisplay)
45 do
46 # Update cameras on both our programs
47 versatile_program.use
48 versatile_program.mvp.uniform world_camera.mvp_matrix
49
50 normals_program.use
51 normals_program.mvp.uniform app.world_camera.mvp_matrix
52
53 for actor in actors do
54 for leaf in actor.model.leaves do
55 leaf.material.draw(actor, leaf)
56 end
57 end
58
59 frame_core_world_sprites display
60
61 # Toggle writing to the depth buffer for particles effects
62 glDepthMask false
63 for system in particle_systems do system.draw
64 glDepthMask true
65
66 frame_core_ui_sprites display
67 end
68 end