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 struct RenderingContext
42 {
43     Matrix4x4f modelViewMatrix;
44 
45     Matrix4x4f modelMatrix;
46     Matrix4x4f invModelMatrix;
47 
48     Vector3f cameraPosition;
49     Vector3f prevCameraPosition;
50 
51     Matrix4x4f viewMatrix;
52     Matrix4x4f invViewMatrix;
53 
54     Matrix4x4f viewRotationMatrix;
55     Matrix4x4f invViewRotationMatrix;
56 
57     Matrix4x4f projectionMatrix;
58     Matrix4x4f normalMatrix;
59 
60     Matrix4x4f prevViewMatrix;
61     Matrix4x4f prevModelViewProjMatrix;
62     Matrix4x4f blurModelViewProjMatrix;
63 
64     Frustum frustum;
65 
66     EventManager eventManager;
67     Environment environment;
68 
69     Material material;
70 
71     Shader overrideShader;
72     Material overrideMaterial;
73 
74     float time;
75     float blurMask;
76 
77     bool depthPass;
78     bool colorPass;
79     bool shadowPass;
80 
81     int layer;
82 
83     bool ignoreTransparentEntities;
84     bool ignoreOpaqueEntities;
85 
86     void init(EventManager emngr, Environment env)
87     {
88         modelViewMatrix = Matrix4x4f.identity;
89         modelMatrix = Matrix4x4f.identity;
90         invModelMatrix = Matrix4x4f.identity;
91         cameraPosition = Vector3f(0.0f, 0.0f, 0.0f);
92         prevCameraPosition = Vector3f(0.0f, 0.0f, 0.0f);
93         viewMatrix = Matrix4x4f.identity;
94         invViewMatrix = Matrix4x4f.identity;
95         viewRotationMatrix = Matrix4x4f.identity;
96         invViewRotationMatrix = Matrix4x4f.identity;
97         projectionMatrix = Matrix4x4f.identity;
98         normalMatrix = Matrix4x4f.identity;
99         prevViewMatrix = Matrix4x4f.identity;
100         prevModelViewProjMatrix = Matrix4x4f.identity;
101         blurModelViewProjMatrix = Matrix4x4f.identity;
102         eventManager = emngr;
103         environment = env;
104         overrideMaterial = null;
105         time = 0.0f;
106         depthPass = true;
107         colorPass = true;
108         blurMask = 1.0f;
109         layer = 1;
110         ignoreTransparentEntities = false;
111         ignoreOpaqueEntities = false;
112         shadowPass = false;
113     }
114 
115     void initPerspective(EventManager emngr, Environment env, float fov, float znear, float zfar)
116     {
117         init(emngr, env);
118         projectionMatrix = perspectiveMatrix(fov, emngr.aspectRatio, znear, zfar);
119     }
120 
121     void initPerspective(EventManager emngr, Environment env, float fov, float aspect, float znear, float zfar)
122     {
123         init(emngr, env);
124         projectionMatrix = perspectiveMatrix(fov, aspect, znear, zfar);
125     }
126 
127     void initOrtho(EventManager emngr, Environment env, float znear, float zfar)
128     {
129         init(emngr, env);
130         projectionMatrix = orthoMatrix(0.0f, emngr.windowWidth, emngr.windowHeight, 0.0f, znear, zfar);
131     }
132 
133     void initOrtho(EventManager emngr, Environment env, float w, float h, float znear, float zfar)
134     {
135         init(emngr, env);
136         projectionMatrix = orthoMatrix(0.0f, w, h, 0.0f, znear, zfar);
137     }
138 }