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 #version 330 core 51 52 uniform mat4 modelViewMatrix; 53 uniform mat4 projectionMatrix; 54 55 layout (location = 0) in vec2 va_Vertex; 56 layout (location = 1) in vec2 va_Texcoord; 57 58 out vec2 texCoord; 59 60 void main() 61 { 62 texCoord = va_Texcoord; 63 gl_Position = projectionMatrix * modelViewMatrix * vec4(va_Vertex, 0.0, 1.0); 64 } 65 "; 66 67 private string fsText = " 68 #version 330 core 69 70 uniform sampler2D diffuseTexture; 71 72 in vec2 texCoord; 73 74 out vec4 frag_color; 75 76 void main() 77 { 78 frag_color = texture(diffuseTexture, texCoord); 79 } 80 "; 81 82 override string vertexShaderSrc() {return vsText;} 83 override string fragmentShaderSrc() {return fsText;} 84 85 GLint modelViewMatrixLoc; 86 GLint projectionMatrixLoc; 87 GLint diffuseTextureLoc; 88 89 this(Owner o) 90 { 91 super(o); 92 93 modelViewMatrixLoc = glGetUniformLocation(shaderProgram, "modelViewMatrix"); 94 projectionMatrixLoc = glGetUniformLocation(shaderProgram, "projectionMatrix"); 95 diffuseTextureLoc = glGetUniformLocation(shaderProgram, "diffuseTexture"); 96 } 97 98 override void bind(GenericMaterial mat, RenderingContext* rc) 99 { 100 auto idiffuse = "diffuse" in mat.inputs; 101 102 glUseProgram(shaderProgram); 103 104 // Matrices 105 glUniformMatrix4fv(modelViewMatrixLoc, 1, GL_FALSE, rc.modelViewMatrix.arrayof.ptr); 106 glUniformMatrix4fv(projectionMatrixLoc, 1, GL_FALSE, rc.projectionMatrix.arrayof.ptr); 107 108 // Texture 0 - diffuse texture 109 if (idiffuse.texture is null) 110 { 111 Color4f color = Color4f(idiffuse.asVector4f); 112 idiffuse.texture = makeOnePixelTexture(mat, color); 113 } 114 glActiveTexture(GL_TEXTURE0); 115 idiffuse.texture.bind(); 116 glUniform1i(diffuseTextureLoc, 0); 117 } 118 119 override void unbind(GenericMaterial mat, RenderingContext* rc) 120 { 121 glUseProgram(0); 122 } 123 }