1 /*
2 Copyright (c) 2018 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.graphics.shaders.hud;
29 
30 import std.stdio;
31 import std.math;
32 import std.conv;
33 
34 import dlib.core.memory;
35 import dlib.core.stream;
36 import dlib.math.vector;
37 import dlib.math.matrix;
38 import dlib.math.transformation;
39 import dlib.image.color;
40 
41 import dagon.core.libs;
42 import dagon.core.ownership;
43 import dagon.graphics.rc;
44 import dagon.graphics.shader;
45 import dagon.graphics.texture;
46 
47 class HUDShader: Shader
48 {
49     string vs = import("HUD.vs");
50     string fs = import("HUD.fs");
51 
52     this(Owner o)
53     {
54         auto myProgram = New!ShaderProgram(vs, fs, this);
55         super(myProgram, o);
56     }
57 
58     override void bind(RenderingContext* rc)
59     {
60         auto idiffuse = "diffuse" in rc.material.inputs;
61 
62         // Matrices
63         setParameter("modelViewMatrix", rc.modelViewMatrix);
64         setParameter("projectionMatrix", rc.projectionMatrix);
65 
66         // diffuse
67         if (idiffuse.texture)
68         {
69             glActiveTexture(GL_TEXTURE0);
70             idiffuse.texture.bind();
71             setParameter("diffuseTexture", 0);
72             setParameterSubroutine("diffuse", ShaderType.Fragment, "diffuseColorTexture");
73         }
74         else
75         {
76             setParameter("diffuseVector", rc.material.diffuse.asVector4f);
77             setParameterSubroutine("diffuse", ShaderType.Fragment, "diffuseColorValue");
78         }
79 
80         super.bind(rc);
81     }
82 
83     override void unbind(RenderingContext* rc)
84     {
85         super.unbind(rc);
86 
87         glActiveTexture(GL_TEXTURE0);
88         glBindTexture(GL_TEXTURE_2D, 0);
89     }
90 }