1 /*
2 Copyright (c) 2019-2020 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.render.view;
29 
30 import dlib.core.memory;
31 import dlib.core.ownership;
32 import dlib.math.vector;
33 import dlib.math.matrix;
34 import dlib.math.transformation;
35 import dlib.math.utils;
36 
37 import dagon.core.bindings;
38 import dagon.graphics.camera;
39 
40 import dagon.render.pipeline;
41 
42 class RenderView: Owner
43 {    
44     Camera camera;
45     uint x;
46     uint y;
47     uint width;
48     uint height;
49     float aspectRatio;
50     bool ortho = false;
51     RenderPipeline pipeline;
52     
53     this(uint x, uint y, uint width, uint height, Owner owner)
54     {
55         super(owner);
56         this.x = x;
57         this.y = y;
58         this.width = width;
59         this.height = height;
60         aspectRatio = cast(float)width / cast(float)height;
61     }
62     
63     ~this()
64     {
65     }
66     
67     Matrix4x4f viewMatrix()
68     {
69         if (camera)
70             return camera.viewMatrix();
71         else
72             return Matrix4x4f.identity;
73     }
74     
75     Matrix4x4f invViewMatrix()
76     {
77         if (camera)
78             return camera.invViewMatrix();
79         else
80             return Matrix4x4f.identity;
81     }
82     
83     Matrix4x4f projectionMatrix()
84     {
85         float fov = 60.0f;
86         float zNear = 0.01f;
87         float zFar = 1000.0f;
88         if (camera)
89         {
90             fov = camera.fov;
91             zNear = camera.zNear;
92             zFar = camera.zFar;
93         }
94         if (ortho)
95             return orthoMatrix(0.0f, width, height, 0.0f, 0.0f, zFar);
96         else
97             return perspectiveMatrix(fov, aspectRatio, zNear, zFar);
98     }
99     
100     float zNear()
101     {
102         if (camera)
103             return camera.zNear;
104         else
105             return 0.01f;
106     }
107     
108     float zFar()
109     {
110         if (camera)
111             return camera.zFar;
112         else
113             return 1000.0f;
114     }
115     
116     Vector3f cameraPosition()
117     {
118         if (camera)
119             return camera.positionAbsolute;
120         else
121             return Vector3f(0.0f, 0.0f, 0.0f);
122     }
123     
124     void setPosition(uint x, uint y)
125     {
126         this.x = x;
127         this.y = y;
128     }
129     
130     void resize(uint width, uint height)
131     {
132         this.width = width;
133         this.height = height;
134         aspectRatio = cast(float)width / cast(float)height;
135     }
136 
137     // TODO: pixel to ray
138     // TODO: point to pixel
139     // TODO: pixel visible
140 }