Revision 6e7c8721
Added by Leszek Koltunski over 7 years ago
src/main/java/org/distorted/library/DistortedFramebuffer.java | ||
---|---|---|
45 | 45 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT); |
46 | 46 |
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST); |
47 | 47 |
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR); |
48 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
|
|
48 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
|
|
49 | 49 |
|
50 | 50 |
GLES30.glGenFramebuffers(1, mFBOH, 0); |
51 | 51 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]); |
... | ... | |
61 | 61 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT); |
62 | 62 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST); |
63 | 63 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST); |
64 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
|
|
64 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
|
|
65 | 65 |
|
66 | 66 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]); |
67 | 67 |
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0); |
src/main/java/org/distorted/library/DistortedOutputSurface.java | ||
---|---|---|
55 | 55 |
|
56 | 56 |
private long mTime; |
57 | 57 |
private float mFOV; |
58 |
int mWidth,mHeight,mDepth; |
|
59 | 58 |
float mDistance, mNear; |
60 | 59 |
float[] mProjectionMatrix; |
61 | 60 |
|
... | ... | |
118 | 117 |
|
119 | 118 |
mDistance = mHeight/a; |
120 | 119 |
float far = 2*mDistance-near; |
121 |
mDepth = (int)((far-near)/2); |
|
122 | 120 |
|
123 | 121 |
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); |
124 | 122 |
} |
... | ... | |
131 | 129 |
float near = mWidth+mHeight-mHeight*(1.0f-mNear); |
132 | 130 |
mDistance = mWidth+mHeight; |
133 | 131 |
float far = mWidth+mHeight+mHeight*(1.0f-mNear); |
134 |
mDepth = (int)((far-near)/2); |
|
135 | 132 |
|
136 | 133 |
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far); |
137 | 134 |
} |
... | ... | |
365 | 362 |
{ |
366 | 363 |
mWidth = width; |
367 | 364 |
mHeight= height; |
368 |
mSizeX = width; |
|
369 |
mSizeY = height; |
|
370 | 365 |
|
371 | 366 |
createProjection(); |
372 | 367 |
|
src/main/java/org/distorted/library/DistortedSurface.java | ||
---|---|---|
20 | 20 |
package org.distorted.library; |
21 | 21 |
|
22 | 22 |
import java.util.HashMap; |
23 |
import java.util.Iterator; |
|
24 | 23 |
import java.util.LinkedList; |
25 |
import java.util.Map; |
|
26 | 24 |
|
27 | 25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
28 | 26 |
/** |
... | ... | |
68 | 66 |
private int mType; |
69 | 67 |
int mColorCreated; |
70 | 68 |
int[] mColorH = new int[1]; |
71 |
int mSizeX, mSizeY; // in screen space
|
|
69 |
int mWidth, mHeight;
|
|
72 | 70 |
|
73 | 71 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
74 | 72 |
|
... | ... | |
193 | 191 |
|
194 | 192 |
DistortedSurface(int width, int height, int create, int type) |
195 | 193 |
{ |
196 |
mSizeX = width ;
|
|
197 |
mSizeY = height;
|
|
194 |
mWidth = width ;
|
|
195 |
mHeight = height;
|
|
198 | 196 |
mColorCreated = create; |
199 | 197 |
mColorH[0] = 0; |
200 | 198 |
mID = type==TYPE_SYST ? --mNextSystemID : ++mNextClientID; |
... | ... | |
247 | 245 |
*/ |
248 | 246 |
public int getWidth() |
249 | 247 |
{ |
250 |
return mSizeX;
|
|
248 |
return mWidth;
|
|
251 | 249 |
} |
252 | 250 |
|
253 | 251 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
258 | 256 |
*/ |
259 | 257 |
public int getHeight() |
260 | 258 |
{ |
261 |
return mSizeY;
|
|
259 |
return mHeight;
|
|
262 | 260 |
} |
263 | 261 |
|
264 | 262 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
273 | 271 |
*/ |
274 | 272 |
public int getDepth(MeshObject mesh) |
275 | 273 |
{ |
276 |
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
|
|
274 |
return mesh==null ? 0 : (int)(mWidth*mesh.zFactor);
|
|
277 | 275 |
} |
278 | 276 |
} |
src/main/java/org/distorted/library/EffectQueueMatrix.java | ||
---|---|---|
47 | 47 |
private static float[] mViewMatrix= new float[16]; |
48 | 48 |
|
49 | 49 |
private static int mObjDH; // This is a handle to half a Object dimensions |
50 |
private static int mDepthH; // Handle to the max Depth, i.e (farplane-nearplane)/2 |
|
51 | 50 |
private static int mMVPMatrixH; // pass in the transformation matrix |
52 | 51 |
private static int mMVMatrixH; // pass in the modelview matrix. |
53 | 52 |
|
... | ... | |
192 | 191 |
static void getUniforms(int mProgramH) |
193 | 192 |
{ |
194 | 193 |
mObjDH = GLES30.glGetUniformLocation(mProgramH, "u_objD"); |
195 |
mDepthH = GLES30.glGetUniformLocation(mProgramH, "u_Depth"); |
|
196 | 194 |
mMVPMatrixH= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix"); |
197 | 195 |
mMVMatrixH = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix"); |
198 | 196 |
} |
... | ... | |
256 | 254 |
constructMatrices(projection,halfX,halfY); |
257 | 255 |
|
258 | 256 |
GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ); |
259 |
GLES30.glUniform1f( mDepthH, projection.mDepth); |
|
260 | 257 |
GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0); |
261 | 258 |
GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0); |
262 | 259 |
} |
src/main/res/raw/main_vertex_shader.glsl | ||
---|---|---|
38 | 38 |
uniform vec3 u_objD; // half of object width x half of object height X half the depth; |
39 | 39 |
// point (0,0,0) is the center of the object |
40 | 40 |
|
41 |
uniform float u_Depth; // max absolute value of v.z ; beyond that the vertex would be culled by the near or far planes. |
|
42 |
// I read OpenGL ES has a built-in uniform variable gl_DepthRange.near = n, |
|
43 |
// .far = f, .diff = f-n so maybe u_Depth is redundant |
|
44 |
// Update: this struct is only available in fragment shaders |
|
45 |
|
|
46 | 41 |
uniform mat4 u_MVPMatrix; // the combined model/view/projection matrix. |
47 | 42 |
uniform mat4 u_MVMatrix; // the combined model/view matrix. |
48 | 43 |
|
... | ... | |
147 | 142 |
return min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT)),E); |
148 | 143 |
} |
149 | 144 |
|
150 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
151 |
// Clamp v.z to (-u_Depth,u_Depth) with the following function: |
|
152 |
// define h to be, say, 0.7; let H=u_Depth |
|
153 |
// if v.z < -hH then v.z = (-(1-h)^2 * H^2)/(v.z+(2h-1)H) -H (function satisfying f(-hH)=-hH, f'(-hH)=1, lim f(x) = -H) |
|
154 |
// else if v.z > hH then v.z = (-(1-h)^2 * H^2)/(v.z-(2h-1)H) +H (function satisfying f(+hH)=+hH, f'(+hH)=1, lim f(x) = +H) |
|
155 |
// else v.z = v.z |
|
156 |
|
|
157 |
void restrictZ(inout float v) |
|
158 |
{ |
|
159 |
const float h = 0.7; |
|
160 |
float signV = 2.0*max(0.0,sign(v))-1.0; |
|
161 |
float c = ((1.0-h)*(h-1.0)*u_Depth*u_Depth)/(v-signV*(2.0*h-1.0)*u_Depth) +signV*u_Depth; |
|
162 |
float b = max(0.0,sign(abs(v)-h*u_Depth)); |
|
163 |
|
|
164 |
v = b*c+(1.0-b)*v; // Avoid branching: if abs(v)>h*u_Depth, then v=c; otherwise v=v. |
|
165 |
} |
|
166 |
|
|
167 | 145 |
////////////////////////////////////////////////////////////////////////////////////////////// |
168 | 146 |
// DEFORM EFFECT |
169 | 147 |
// |
... | ... | |
575 | 553 |
|
576 | 554 |
j+=3; |
577 | 555 |
} |
578 |
|
|
579 |
restrictZ(v.z); |
|
580 | 556 |
#endif |
581 | 557 |
|
582 | 558 |
v_Position = v; |
Also available in: Unified diff
Simplifications.