Revision af4cc5db
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/DistortedOutputSurface.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.library; |
21 | 21 |
|
22 |
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 |
|
|
22 | 51 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
23 | 52 |
|
53 |
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 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
24 | 89 |
/** |
25 |
* A Surface that we can set as Output, i.e. render to it. |
|
90 |
* 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. |
|
26 | 98 |
*/ |
99 |
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! |
|
27 | 103 |
|
28 |
public interface DistortedOutputSurface extends DistortedSurface |
|
29 |
{ |
|
104 |
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 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
30 | 128 |
/** |
31 | 129 |
* Bind this Surface as a Framebuffer we can render to. |
32 | 130 |
*/ |
33 |
void setAsOutput(); |
|
131 |
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 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
34 | 151 |
/** |
35 |
* Return the Projection. |
|
152 |
* Resize the underlying Framebuffer. |
|
153 |
* |
|
154 |
* @param width The new width. |
|
155 |
* @param height The new height. |
|
36 | 156 |
*/ |
37 |
DistortedProjection getProjection(); |
|
38 |
} |
|
157 |
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 |
} |
Also available in: Unified diff
Simplify yesterday's refactoring.