f18f2eb16d2e7d4127a519b281f805f4ab66a708
[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 model_dimensions
21 import particles
22 import selection
23
24 redef class App
25
26 redef fun on_create
27 do
28 super
29
30 # Move the camera back a bit
31 world_camera.reset_height(10.0)
32 world_camera.near = 0.1
33
34 # Prepare programs
35 var programs = [versatile_program, normals_program, explosion_program, smoke_program, static_program, selection_program: GamnitProgram]
36 for program in programs do
37 program.compile_and_link
38 var gamnit_error = program.error
39 assert gamnit_error == null else print_error gamnit_error
40 end
41 end
42
43 redef fun frame_core_draw(display) do frame_core_depth display
44
45 # Draw all elements of `actors` and then call `frame_core_flat`
46 protected fun frame_core_depth(display: GamnitDisplay)
47 do
48 frame_core_dynamic_resolution_before display
49
50 # Update cameras on both our programs
51 versatile_program.use
52 versatile_program.mvp.uniform world_camera.mvp_matrix
53
54 normals_program.use
55 normals_program.mvp.uniform app.world_camera.mvp_matrix
56
57 frame_core_depth_clock.lapse
58 for actor in actors do
59 for leaf in actor.model.leaves do
60 leaf.material.draw(actor, leaf)
61 end
62 end
63 perfs["gamnit depth actors"].add frame_core_depth_clock.lapse
64
65 frame_core_world_sprites display
66 perfs["gamnit depth sprites"].add frame_core_depth_clock.lapse
67
68 # Toggle writing to the depth buffer for particles effects
69 glDepthMask false
70 for system in particle_systems do system.draw
71 glDepthMask true
72 perfs["gamnit depth particles"].add frame_core_depth_clock.lapse
73
74 frame_core_ui_sprites display
75 perfs["gamnit depth ui_sprites"].add frame_core_depth_clock.lapse
76
77 frame_core_dynamic_resolution_after display
78 end
79
80 private var frame_core_depth_clock = new Clock
81 end