Merge: gamnit: new services and a lot of bug fixes and performance improvements
[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 import shadow
25
26 redef class App
27
28 redef fun create_scene
29 do
30 # Move the camera back a bit
31 world_camera.reset_height(10.0)
32 world_camera.near = 0.1
33
34 super
35 end
36
37 redef fun create_gamnit
38 do
39 super
40
41 # Cull the invisible triangles in the back of the geometries
42 glCullFace gl_BACK
43
44 # Prepare programs
45 var programs = [blinn_phong_program, normals_program, explosion_program, smoke_program, static_program, selection_program: GamnitProgram]
46 for program in programs do
47 program.compile_and_link
48 var gamnit_error = program.error
49 assert gamnit_error == null else print_error gamnit_error
50 end
51 end
52
53 redef fun frame_core_draw(display) do frame_core_depth display
54
55 # Draw all elements of `actors` and then call `frame_core_flat`
56 protected fun frame_core_depth(display: GamnitDisplay)
57 do
58 frame_core_depth_clock.lapse
59
60 # Compute shadows
61 if light isa LightCastingShadows then
62 frame_core_shadow_prep display
63 perfs["gamnit depth shadows"].add frame_core_depth_clock.lapse
64 end
65
66 glViewport(0, 0, display.width, display.height)
67 frame_core_dynamic_resolution_before display
68 perfs["gamnit depth dynres"].add frame_core_depth_clock.lapse
69
70 for actor in actors do
71 for leaf in actor.model.leaves do
72 leaf.material.draw(actor, leaf, app.world_camera)
73 assert glGetError == gl_NO_ERROR else print_error "Gamnit error on material {leaf.material.class_name}"
74 end
75 end
76 perfs["gamnit depth actors"].add frame_core_depth_clock.lapse
77
78 frame_core_world_sprites display
79 perfs["gamnit depth sprites"].add frame_core_depth_clock.lapse
80
81 # Toggle writing to the depth buffer for particles effects
82 glDepthMask false
83 for system in particle_systems do
84 system.draw
85 assert glGetError == gl_NO_ERROR else print_error "OpenGL error in {system}"
86 end
87 glDepthMask true
88 perfs["gamnit depth particles"].add frame_core_depth_clock.lapse
89
90 # Stop using the dynamic resolution before drawing UI sprites
91 frame_core_dynamic_resolution_after display
92
93 frame_core_ui_sprites display
94 perfs["gamnit depth ui_sprites"].add frame_core_depth_clock.lapse
95
96 # Debug, show the light point of view
97 #frame_core_shadow_debug display
98 end
99
100 private var frame_core_depth_clock = new Clock
101 end