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.rc; 29 30 import dlib.math.vector; 31 import dlib.math.matrix; 32 import dlib.math.transformation; 33 import dlib.geometry.frustum; 34 35 import dagon.core.libs; 36 import dagon.core.event; 37 import dagon.graphics.environment; 38 import dagon.graphics.material; 39 import dagon.graphics.shader; 40 41 // TODO: don't pass RenderingContext via program stack, use special state object 42 43 struct RenderingContext 44 { 45 Matrix4x4f modelViewMatrix; 46 47 Matrix4x4f modelMatrix; 48 Matrix4x4f invModelMatrix; 49 50 Vector3f cameraPosition; 51 Vector3f prevCameraPosition; 52 53 Matrix4x4f viewMatrix; 54 Matrix4x4f invViewMatrix; 55 56 Matrix4x4f viewRotationMatrix; 57 Matrix4x4f invViewRotationMatrix; 58 59 Matrix4x4f projectionMatrix; 60 Matrix4x4f normalMatrix; 61 62 Matrix4x4f prevViewMatrix; 63 Matrix4x4f prevModelViewProjMatrix; 64 Matrix4x4f blurModelViewProjMatrix; 65 66 Frustum frustum; 67 68 EventManager eventManager; 69 Environment environment; 70 71 Material material; 72 73 Shader overrideShader; 74 Material overrideMaterial; 75 76 double time; 77 float blurMask; 78 79 bool depthPass; 80 bool colorPass; 81 bool shadowPass; 82 83 int layer; 84 85 bool ignoreTransparentEntities; 86 bool ignoreOpaqueEntities; 87 88 bool rebindShaderProgram; 89 90 void init(EventManager emngr, Environment env) 91 { 92 modelViewMatrix = Matrix4x4f.identity; 93 modelMatrix = Matrix4x4f.identity; 94 invModelMatrix = Matrix4x4f.identity; 95 cameraPosition = Vector3f(0.0f, 0.0f, 0.0f); 96 prevCameraPosition = Vector3f(0.0f, 0.0f, 0.0f); 97 viewMatrix = Matrix4x4f.identity; 98 invViewMatrix = Matrix4x4f.identity; 99 viewRotationMatrix = Matrix4x4f.identity; 100 invViewRotationMatrix = Matrix4x4f.identity; 101 projectionMatrix = Matrix4x4f.identity; 102 normalMatrix = Matrix4x4f.identity; 103 prevViewMatrix = Matrix4x4f.identity; 104 prevModelViewProjMatrix = Matrix4x4f.identity; 105 blurModelViewProjMatrix = Matrix4x4f.identity; 106 eventManager = emngr; 107 environment = env; 108 overrideMaterial = null; 109 time = 0.0; 110 depthPass = true; 111 colorPass = true; 112 shadowPass = false; 113 blurMask = 1.0f; 114 layer = 1; 115 ignoreTransparentEntities = false; 116 ignoreOpaqueEntities = false; 117 rebindShaderProgram = true; 118 } 119 120 void initPerspective(EventManager emngr, Environment env, float fov, float znear, float zfar) 121 { 122 init(emngr, env); 123 projectionMatrix = perspectiveMatrix(fov, emngr.aspectRatio, znear, zfar); 124 } 125 126 void initPerspective(EventManager emngr, Environment env, float fov, float aspect, float znear, float zfar) 127 { 128 init(emngr, env); 129 projectionMatrix = perspectiveMatrix(fov, aspect, znear, zfar); 130 } 131 132 void initOrtho(EventManager emngr, Environment env, float znear, float zfar) 133 { 134 init(emngr, env); 135 projectionMatrix = orthoMatrix(0.0f, emngr.windowWidth, emngr.windowHeight, 0.0f, znear, zfar); 136 } 137 138 void initOrtho(EventManager emngr, Environment env, float w, float h, float znear, float zfar) 139 { 140 init(emngr, env); 141 projectionMatrix = orthoMatrix(0.0f, w, h, 0.0f, znear, zfar); 142 } 143 }