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