Repository navigation
Expand file tree
/
Copy pathscreenport.cpp
More file actions
189 lines (151 loc) · 4.62 KB
/
Copy pathscreenport.cpp
File metadata and controls
189 lines (151 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "screenport.h"
#include "gpustatemanager.h"
#include "enginelimits.h"
#include "../grinliz/glrectangle.h"
#include "../grinliz/glutil.h"
#include "../grinliz/glmatrix.h"
#include "../gamui/gamui.h"
using namespace grinliz;
Screenport::Screenport(int w, int h)
{
this->near = EL_NEAR;
this->far = EL_FAR;
physicalWidth = float(w);
physicalHeight = float(h);
uiMode = false;
orthoCamera = false;
}
void Screenport::Resize(int w, int h, GPUDevice* device)
{
if (w > 0 && h > 0) {
physicalWidth = (float)w;
physicalHeight = (float)h;
}
else {
w = (int)physicalWidth;
h = (int)physicalHeight;
}
device->SetViewport(w, h);
}
void Screenport::SetUI(GPUDevice* device)
{
projection2D.SetIdentity();
projection2D.SetOrtho(0, physicalWidth, physicalHeight, 0, -1, 1);
device->SetOrthoTransform((int)physicalWidth, (int)physicalHeight);
uiMode = true;
}
void Screenport::SetView(const Matrix4& _view, GPUDevice* device)
{
GLASSERT(uiMode == false);
view3D = _view;
device->SetCameraTransform(view3D);
}
void Screenport::SetPerspective(GPUDevice* device)
{
uiMode = false;
GLASSERT( uiMode == false );
GLASSERT( near > 0.0f );
GLASSERT( far > near );
frustum.zNear = near;
frustum.zFar = far;
// Convert from the FOV to the half angle.
float theta = ToRadian( EL_FOV * 0.5f );
float tanTheta = tanf( theta );
float halfLongSide = tanTheta * frustum.zNear;
// left, right, top, & bottom are on the near clipping
// plane. (Not an obvious point to my mind.)
// Also, the 3D camera applies the rotation.
// Since FOV is specified as the 1/2 width, the ratio
// is the height/width (different than gluPerspective)
float ratio = physicalHeight / physicalWidth;
frustum.top = ratio * halfLongSide;
frustum.bottom = -frustum.top;
frustum.left = -halfLongSide;
frustum.right = halfLongSide;
if ( orthoCamera ) {
float w = 0;
float h = 0;
if ( orthoWidth > 0 ) {
w = orthoWidth;
h = orthoWidth * ratio;
}
else {
w = orthoHeight / ratio;
h = orthoHeight;
}
projection3D.SetOrtho( -w/2, w/2, -h/2, h/2, frustum.zNear, frustum.zFar );
device->SetPerspectiveTransform( projection3D );
}
else {
projection3D.SetFrustum( frustum.left, frustum.right, frustum.bottom, frustum.top, frustum.zNear, frustum.zFar );
device->SetPerspectiveTransform( projection3D );
}
}
grinliz::Vector2F Screenport::WorldToUI(const grinliz::Vector3F& world, const gamui::Gamui& g) const
{
Vector2F view = WorldToView(world);
Vector2F window = ViewToWindow(view);
Vector2F ui = { 0, 0 };
ui.x = g.TransformPhysicalToVirtual(window.x);
ui.y = g.TransformPhysicalToVirtual(window.y);
return ui;
}
bool Screenport::ViewToWorld( const grinliz::Vector2F& v, const grinliz::Matrix4* _mvpi, grinliz::Ray* ray ) const
{
Matrix4 mvpi;
if ( _mvpi ) {
mvpi = *_mvpi;
}
else {
Matrix4 mvp;
ViewProjection3D( &mvp );
mvp.Invert( &mvpi );
}
// View normalized:
Vector4F in = { 2.0f * v.x / physicalWidth - 1.0f,
2.0f * v.y / physicalHeight - 1.0f,
0.f, //v.z*2.0f-1.f,
1.0f };
Vector4F out0, out1;
MultMatrix4( mvpi, in, &out0 );
in.z = 1.0f;
MultMatrix4( mvpi, in, &out1 );
if ( out0.w == 0.0f ) {
return false;
}
if ( ray ) {
ray->origin.x = out0.x / out0.w;
ray->origin.y = out0.y / out0.w;
ray->origin.z = out0.z / out0.w;
ray->direction.x = out1.x / out1.w - ray->origin.x;
ray->direction.y = out1.y / out1.w - ray->origin.y;
ray->direction.z = out1.z / out1.w - ray->origin.z;
}
return true;
}
void Screenport::WorldToView( const grinliz::Vector3F& world, grinliz::Vector2F* v ) const
{
Matrix4 mvp;
ViewProjection3D( &mvp );
Vector4F p, r;
p.Set( world, 1 );
r = mvp * p;
v->x = Interpolate( -1.0f, 0.0f,
1.0f, (float)physicalWidth,
r.x/r.w );
v->y = Interpolate( -1.0f, 0.0f,
1.0f, (float)physicalHeight,
r.y/r.w );
}