1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library;
|
21
|
|
22
|
import android.opengl.Matrix;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
|
26
|
class DistortedProjection
|
27
|
{
|
28
|
private float mX, mY, mFOV;
|
29
|
int mWidth,mHeight,mDepth;
|
30
|
float mDistance;
|
31
|
float[] mProjectionMatrix;
|
32
|
|
33
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34
|
|
35
|
DistortedProjection()
|
36
|
{
|
37
|
mProjectionMatrix = new float[16];
|
38
|
|
39
|
mWidth = 0;
|
40
|
mHeight= 0;
|
41
|
|
42
|
mFOV = 60.0f;
|
43
|
mX = 0.0f;
|
44
|
mY = 0.0f;
|
45
|
}
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
DistortedProjection(int width, int height)
|
50
|
{
|
51
|
mProjectionMatrix = new float[16];
|
52
|
|
53
|
mWidth = width;
|
54
|
mHeight= height;
|
55
|
|
56
|
mFOV = 60.0f;
|
57
|
mX = 0.0f;
|
58
|
mY = 0.0f;
|
59
|
|
60
|
createProjection();
|
61
|
}
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
boolean resize(int width, int height)
|
66
|
{
|
67
|
if( mWidth!=width || mHeight!=height )
|
68
|
{
|
69
|
mWidth = width;
|
70
|
mHeight= height;
|
71
|
createProjection();
|
72
|
return true;
|
73
|
}
|
74
|
|
75
|
return false;
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
void set(float fov, float x, float y)
|
81
|
{
|
82
|
mFOV = fov;
|
83
|
mX = x;
|
84
|
mY = y;
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
void createProjection()
|
90
|
{
|
91
|
if( mWidth>0 && mHeight>1 )
|
92
|
{
|
93
|
if( mFOV>0.0f ) // perspective projection
|
94
|
{
|
95
|
float left = (-mX-mWidth /2.0f)/mHeight;
|
96
|
float right = (-mX+mWidth /2.0f)/mHeight;
|
97
|
float bottom = (-mY-mHeight/2.0f)/mHeight;
|
98
|
float top = (-mY+mHeight/2.0f)/mHeight;
|
99
|
float near = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
|
100
|
mDistance = mHeight*near/(top-bottom);
|
101
|
float far = 2*mDistance-near;
|
102
|
mDepth = (int)((far-near)/2);
|
103
|
|
104
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
105
|
}
|
106
|
else // parallel projection
|
107
|
{
|
108
|
float left = -mX-mWidth /2.0f;
|
109
|
float right = -mX+mWidth /2.0f;
|
110
|
float bottom = -mY-mHeight/2.0f;
|
111
|
float top = -mY+mHeight/2.0f;
|
112
|
float near = (mWidth+mHeight)/2;
|
113
|
mDistance = 2*near;
|
114
|
float far = 3*near;
|
115
|
mDepth = (int)near;
|
116
|
|
117
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
|
122
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123
|
|
124
|
}
|