1 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
af4cc5db
|
Leszek Koltunski
|
import android.opengl.Matrix;
|
23 |
|
|
|
24 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
|
26 |
|
|
abstract class DistortedOutputSurface extends DistortedSurface
|
27 |
|
|
{
|
28 |
|
|
private float mX, mY, mFOV;
|
29 |
|
|
int mWidth,mHeight,mDepth;
|
30 |
|
|
float mDistance;
|
31 |
|
|
float[] mProjectionMatrix;
|
32 |
|
|
|
33 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
34 |
|
|
|
35 |
|
|
DistortedOutputSurface(int width, int height, int color)
|
36 |
|
|
{
|
37 |
|
|
super(width,height,color);
|
38 |
|
|
|
39 |
|
|
mProjectionMatrix = new float[16];
|
40 |
|
|
|
41 |
|
|
mWidth = width;
|
42 |
|
|
mHeight= height;
|
43 |
|
|
|
44 |
|
|
mFOV = 60.0f;
|
45 |
|
|
mX = 0.0f;
|
46 |
|
|
mY = 0.0f;
|
47 |
|
|
|
48 |
|
|
createProjection();
|
49 |
|
|
}
|
50 |
|
|
|
51 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
52 |
|
|
|
53 |
af4cc5db
|
Leszek Koltunski
|
void createProjection()
|
54 |
|
|
{
|
55 |
|
|
if( mWidth>0 && mHeight>1 )
|
56 |
|
|
{
|
57 |
|
|
if( mFOV>0.0f ) // perspective projection
|
58 |
|
|
{
|
59 |
|
|
float left = (-mX-mWidth /2.0f)/mHeight;
|
60 |
|
|
float right = (-mX+mWidth /2.0f)/mHeight;
|
61 |
|
|
float bottom = (-mY-mHeight/2.0f)/mHeight;
|
62 |
|
|
float top = (-mY+mHeight/2.0f)/mHeight;
|
63 |
|
|
float near = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
|
64 |
|
|
mDistance = mHeight*near/(top-bottom);
|
65 |
|
|
float far = 2*mDistance-near;
|
66 |
|
|
mDepth = (int)((far-near)/2);
|
67 |
|
|
|
68 |
|
|
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
69 |
|
|
}
|
70 |
|
|
else // parallel projection
|
71 |
|
|
{
|
72 |
|
|
float left = -mX-mWidth /2.0f;
|
73 |
|
|
float right = -mX+mWidth /2.0f;
|
74 |
|
|
float bottom = -mY-mHeight/2.0f;
|
75 |
|
|
float top = -mY+mHeight/2.0f;
|
76 |
|
|
float near = (mWidth+mHeight)/2;
|
77 |
|
|
mDistance = 2*near;
|
78 |
|
|
float far = 3*near;
|
79 |
|
|
mDepth = (int)near;
|
80 |
|
|
|
81 |
|
|
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
|
82 |
|
|
}
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87 |
|
|
// PUBLIC API
|
88 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
c5369f1b
|
leszek
|
/**
|
90 |
af4cc5db
|
Leszek Koltunski
|
* Draw the (texture,mesh,effects) object to the Framebuffer.
|
91 |
|
|
* <p>
|
92 |
|
|
* Must be called from a thread holding OpenGL Context.
|
93 |
|
|
*
|
94 |
|
|
* @param surface InputSurface to skin our Mesh with.
|
95 |
|
|
* @param mesh Class descendant from MeshObject
|
96 |
|
|
* @param effects The DistortedEffects to use when rendering
|
97 |
|
|
* @param time Current time, in milliseconds.
|
98 |
c5369f1b
|
leszek
|
*/
|
99 |
af4cc5db
|
Leszek Koltunski
|
public void renderTo(DistortedInputSurface surface, MeshObject mesh, DistortedEffects effects, long time)
|
100 |
|
|
{
|
101 |
|
|
surface.create(); // Watch out - this needs to be before
|
102 |
|
|
create(); // the 'setAsInput' because this has side-effects!
|
103 |
c5369f1b
|
leszek
|
|
104 |
af4cc5db
|
Leszek Koltunski
|
if( surface.setAsInput() )
|
105 |
|
|
{
|
106 |
|
|
DistortedSurface.deleteAllMarked();
|
107 |
|
|
effects.drawPriv(surface.getWidth()/2.0f, surface.getHeight()/2.0f, mesh, this, time);
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112 |
|
|
/**
|
113 |
|
|
* Draws the Tree, and all its children, to the Framebuffer.
|
114 |
|
|
* <p>
|
115 |
|
|
* Must be called from a thread holding OpenGL Context.
|
116 |
|
|
*
|
117 |
|
|
* @param dt DistortedTree to render.
|
118 |
|
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the Tree.
|
119 |
|
|
*/
|
120 |
|
|
public void renderTo(DistortedTree dt, long time)
|
121 |
|
|
{
|
122 |
|
|
DistortedSurface.deleteAllMarked();
|
123 |
|
|
create();
|
124 |
|
|
dt.drawRecursive(time,this);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128 |
c5369f1b
|
leszek
|
/**
|
129 |
|
|
* Bind this Surface as a Framebuffer we can render to.
|
130 |
|
|
*/
|
131 |
af4cc5db
|
Leszek Koltunski
|
public abstract void setAsOutput();
|
132 |
|
|
|
133 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
/**
|
135 |
|
|
* Create new Projection matrix.
|
136 |
|
|
*
|
137 |
|
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
138 |
|
|
* @param x X-coordinate of the point at which our camera looks at. 0 is the center.
|
139 |
|
|
* @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
|
140 |
|
|
*/
|
141 |
|
|
public void setProjection(float fov, float x, float y)
|
142 |
|
|
{
|
143 |
|
|
mFOV = fov;
|
144 |
|
|
mX = x;
|
145 |
|
|
mY = y;
|
146 |
|
|
|
147 |
|
|
createProjection();
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151 |
c5369f1b
|
leszek
|
/**
|
152 |
af4cc5db
|
Leszek Koltunski
|
* Resize the underlying Framebuffer.
|
153 |
|
|
*
|
154 |
|
|
* @param width The new width.
|
155 |
|
|
* @param height The new height.
|
156 |
c5369f1b
|
leszek
|
*/
|
157 |
af4cc5db
|
Leszek Koltunski
|
public void resize(int width, int height)
|
158 |
|
|
{
|
159 |
|
|
if( mWidth!=width || mHeight!=height )
|
160 |
|
|
{
|
161 |
|
|
mWidth = width;
|
162 |
|
|
mHeight= height;
|
163 |
|
|
createProjection();
|
164 |
|
|
mSizeX = width;
|
165 |
|
|
mSizeY = height;
|
166 |
|
|
if( mColorH[0]>0 ) markForDeletion();
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
}
|