1 /*
2 Copyright (c) 2017-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.materials.hud;
29 
30 import std.stdio;
31 import std.math;
32 import std.conv;
33 
34 import dlib.core.memory;
35 import dlib.math.vector;
36 import dlib.math.matrix;
37 import dlib.image.color;
38 import dlib.image.unmanaged;
39 
40 import derelict.opengl;
41 
42 import dagon.core.ownership;
43 import dagon.graphics.rc;
44 import dagon.graphics.material;
45 import dagon.graphics.materials.generic;
46 
47 class HUDMaterialBackend: GLSLMaterialBackend
48 {    
49     private string vsText = 
50     q{
51         #version 330 core
52         
53         uniform mat4 modelViewMatrix;
54         uniform mat4 projectionMatrix;
55         
56         layout (location = 0) in vec2 va_Vertex;
57         layout (location = 1) in vec2 va_Texcoord;
58 
59         out vec2 texCoord;
60         
61         void main()
62         {
63             texCoord = va_Texcoord;
64             gl_Position = projectionMatrix * modelViewMatrix * vec4(va_Vertex, 0.0, 1.0);
65         }
66     };
67     
68     private string fsText = q{
69         #version 330 core
70         
71         uniform sampler2D diffuseTexture;
72         
73         in vec2 texCoord;
74         
75         out vec4 frag_color;
76 
77         void main()
78         {
79             frag_color = texture(diffuseTexture, texCoord);
80         }
81     };
82     
83     override string vertexShaderSrc() {return vsText;}
84     override string fragmentShaderSrc() {return fsText;}
85 
86     GLint modelViewMatrixLoc;
87     GLint projectionMatrixLoc;
88     GLint diffuseTextureLoc;
89     
90     this(Owner o)
91     {
92         super(o);
93         
94         modelViewMatrixLoc = glGetUniformLocation(shaderProgram, "modelViewMatrix");
95         projectionMatrixLoc = glGetUniformLocation(shaderProgram, "projectionMatrix");
96         diffuseTextureLoc = glGetUniformLocation(shaderProgram, "diffuseTexture");
97     }
98     
99     override void bind(GenericMaterial mat, RenderingContext* rc)
100     {
101         auto idiffuse = "diffuse" in mat.inputs;
102     
103         glUseProgram(shaderProgram);
104         
105         // Matrices
106         glUniformMatrix4fv(modelViewMatrixLoc, 1, GL_FALSE, rc.modelViewMatrix.arrayof.ptr);
107         glUniformMatrix4fv(projectionMatrixLoc, 1, GL_FALSE, rc.projectionMatrix.arrayof.ptr);
108 
109         // Texture 0 - diffuse texture
110         if (idiffuse.texture is null)
111         {
112             Color4f color = Color4f(idiffuse.asVector4f);
113             idiffuse.texture = makeOnePixelTexture(mat, color);
114         }
115         glActiveTexture(GL_TEXTURE0);
116         idiffuse.texture.bind();
117         glUniform1i(diffuseTextureLoc, 0);
118     }
119     
120     override void unbind(GenericMaterial mat, RenderingContext* rc)
121     {
122         glUseProgram(0);
123     }
124 }