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.shadeless;
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 /*
48  * Backend for shadeless material (e.g., only textured or filled with solid color)
49  */
50 
51 class ShadelessBackend: GLSLMaterialBackend
52 {    
53     private string vsText = q{
54         #version 330 core
55         
56         layout (location = 0) in vec3 va_Vertex;
57         layout (location = 2) in vec2 va_Texcoord;
58         
59         out vec3 eyePosition;
60         out vec2 texCoord;
61         
62         uniform mat4 modelViewMatrix;
63         uniform mat4 projectionMatrix;
64         
65         uniform mat4 invViewMatrix;
66     
67         void main()
68         {
69             vec4 pos = modelViewMatrix * vec4(va_Vertex, 1.0);
70             eyePosition = pos.xyz;
71         
72             texCoord = va_Texcoord;
73             gl_Position = projectionMatrix * pos;
74         }
75     };
76     
77     private string fsText = q{
78         #version 330 core
79         
80         uniform sampler2D diffuseTexture;
81         uniform float alpha;
82         uniform float energy;
83         
84         in vec3 eyePosition;
85         in vec2 texCoord;
86         
87         layout(location = 0) out vec4 frag_color;
88         layout(location = 1) out vec4 frag_velocity;
89         layout(location = 2) out vec4 frag_luma;
90         layout(location = 3) out vec4 frag_position;
91         
92         float luminance(vec3 color)
93         {
94             return (
95                 color.x * 0.27 +
96                 color.y * 0.67 +
97                 color.z * 0.06
98             );
99         }
100         
101         vec3 toLinear(vec3 v)
102         {
103             return pow(v, vec3(2.2));
104         }
105 
106         void main()
107         {
108             vec4 col = texture(diffuseTexture, texCoord);
109             frag_color = vec4(toLinear(col.rgb) * energy, col.a * alpha);
110             frag_luma = vec4(energy, 0.0, 0.0, 1.0);
111             frag_velocity = vec4(0.0, 0.0, 0.0, 1.0);
112             frag_position = vec4(eyePosition, 0.0);
113         }
114     };
115     
116     override string vertexShaderSrc() {return vsText;}
117     override string fragmentShaderSrc() {return fsText;}
118 
119     GLint modelViewMatrixLoc;
120     GLint projectionMatrixLoc;
121     
122     GLint diffuseTextureLoc;
123     GLint alphaLoc;
124     GLint energyLoc;
125     
126     this(Owner o)
127     {
128         super(o);
129         
130         modelViewMatrixLoc = glGetUniformLocation(shaderProgram, "modelViewMatrix");
131         projectionMatrixLoc = glGetUniformLocation(shaderProgram, "projectionMatrix");
132             
133         diffuseTextureLoc = glGetUniformLocation(shaderProgram, "diffuseTexture");
134         alphaLoc = glGetUniformLocation(shaderProgram, "alpha");
135         energyLoc = glGetUniformLocation(shaderProgram, "energy");
136     }
137     
138     override void bind(GenericMaterial mat, RenderingContext* rc)
139     {
140         auto idiffuse = "diffuse" in mat.inputs;
141         auto ienergy = "energy" in mat.inputs;
142         auto itransparency = "transparency" in mat.inputs;
143         
144         float energy = ienergy.asFloat;
145 
146         glUseProgram(shaderProgram);
147         
148         // Matrices
149         glUniformMatrix4fv(modelViewMatrixLoc, 1, GL_FALSE, rc.modelViewMatrix.arrayof.ptr);
150         glUniformMatrix4fv(projectionMatrixLoc, 1, GL_FALSE, rc.projectionMatrix.arrayof.ptr);
151 
152         // Texture 0 - diffuse texture
153         Color4f color = Color4f(idiffuse.asVector4f);
154         float alpha = 1.0f;
155         if (idiffuse.texture is null)
156         {
157             idiffuse.texture = makeOnePixelTexture(mat, color);
158         }
159         if (itransparency)
160         {
161             alpha = itransparency.asFloat;
162         }
163         glActiveTexture(GL_TEXTURE0);
164         idiffuse.texture.bind();
165         glUniform1i(diffuseTextureLoc, 0);
166         glUniform1f(alphaLoc, alpha);
167         glUniform1f(energyLoc, energy);
168     }
169     
170     override void unbind(GenericMaterial mat, RenderingContext* rc)
171     {
172         auto idiffuse = "diffuse" in mat.inputs;
173         
174         glActiveTexture(GL_TEXTURE0);
175         idiffuse.texture.unbind();
176     
177         glUseProgram(0);
178     }
179 }