1 /*
2 Copyright (c) 2019 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.game.hudrenderer;
29 
30 import dlib.core.memory;
31 import dlib.core.ownership;
32 import dlib.image.color;
33 
34 import dagon.core.bindings;
35 import dagon.core.event;
36 import dagon.core.time;
37 import dagon.graphics.entity;
38 import dagon.resource.scene;
39 import dagon.render.pipeline;
40 import dagon.render.pass;
41 import dagon.render.deferred;
42 import dagon.game.renderer;
43 
44 class HUDPass: RenderPass
45 {
46     this(RenderPipeline pipeline, EntityGroup group = null)
47     {
48         super(pipeline, group);
49     }
50 
51     override void render()
52     {
53         if (view && group)
54         {
55             glScissor(view.x, view.y, view.width, view.height);
56             glViewport(view.x, view.y, view.width, view.height);
57 
58             if (clear)
59             {
60                 Color4f backgroundColor = Color4f(0.0f, 0.0f, 0.0f, 1.0f);
61                 if (state.environment)
62                     backgroundColor = state.environment.backgroundColor;
63 
64                 glClearColor(
65                     backgroundColor.r,
66                     backgroundColor.g,
67                     backgroundColor.b,
68                     backgroundColor.a);
69                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70             }
71 
72             foreach(entity; group)
73             if (entity.visible && entity.drawable)
74             {
75                 state.layer = entity.layer;
76 
77                 state.modelViewMatrix = state.viewMatrix * entity.absoluteTransformation;
78                 state.normalMatrix = state.modelViewMatrix.inverse.transposed;
79 
80                 //TODO:
81 
82                 if (entity.material)
83                 {
84                     entity.material.bind(&state);
85                     if (entity.material.shader)
86                     {
87                         state.shader = entity.material.shader;
88                         entity.material.shader.bind();
89                         entity.material.shader.bindParameters(&state);
90                     }
91                 }
92 
93                 entity.drawable.render(&state);
94 
95                 if (entity.material)
96                 {
97                     if (entity.material.shader)
98                     {
99                         entity.material.shader.unbindParameters(&state);
100                         entity.material.shader.unbind();
101                     }
102                     entity.material.unbind(&state);
103                 }
104             }
105         }
106     }
107 }
108 
109 class HUDRenderer: Renderer
110 {
111     HUDPass passHUD;
112 
113     this(EventManager eventManager, Owner owner)
114     {
115         super(eventManager, owner);
116 
117         setViewport(0, 0, eventManager.windowWidth, eventManager.windowHeight);
118         view.ortho = true;
119 
120         passHUD = New!HUDPass(pipeline);
121         passHUD.clear = false;
122         passHUD.defaultMaterial.depthWrite = false;
123         passHUD.defaultMaterial.culling = false;
124         passHUD.view = view;
125     }
126 
127     override void scene(Scene s)
128     {
129         passHUD.group = s.foreground;
130     }
131 }