Revision 3fc9327a
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/DistortedEffects.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import android.content.res.Resources; |
23 | 23 |
import android.opengl.GLES30; |
24 |
import android.opengl.Matrix; |
|
25 | 24 |
|
26 | 25 |
import org.distorted.library.message.EffectListener; |
27 | 26 |
import org.distorted.library.program.DistortedProgram; |
... | ... | |
38 | 37 |
import org.distorted.library.type.Static3D; |
39 | 38 |
|
40 | 39 |
import java.io.InputStream; |
41 |
import java.nio.Buffer; |
|
42 | 40 |
import java.nio.ByteBuffer; |
43 | 41 |
import java.nio.ByteOrder; |
44 | 42 |
import java.nio.FloatBuffer; |
... | ... | |
80 | 78 |
mQuadPositions.put(positionData).position(0); |
81 | 79 |
} |
82 | 80 |
|
83 |
/// DEBUG ONLY ///// |
|
84 |
private static DistortedProgram mDebugProgram; |
|
85 |
|
|
86 |
private static int mDebugObjDH; |
|
87 |
private static int mDebugMVPMatrixH; |
|
88 |
/// END DEBUG ////// |
|
89 |
|
|
90 |
private static float[] mMVPMatrix = new float[16]; |
|
91 |
private static float[] mTmpMatrix = new float[16]; |
|
81 |
/// NORMAL PROGRAM ///// |
|
82 |
private static DistortedProgram mNormalProgram; |
|
83 |
private static int mNormalMVPMatrixH; |
|
84 |
/// END PROGRAMS ////// |
|
92 | 85 |
|
93 | 86 |
private static long mNextID =0; |
94 | 87 |
private long mID; |
95 | 88 |
|
96 |
private EffectQueueMatrix mM;
|
|
97 |
private EffectQueueFragment mF;
|
|
98 |
private EffectQueueVertex mV;
|
|
89 |
private EffectQueueMatrix mM; |
|
90 |
private EffectQueueFragment mF; |
|
91 |
private EffectQueueVertex mV; |
|
99 | 92 |
|
100 | 93 |
private boolean matrixCloned, vertexCloned, fragmentCloned; |
101 | 94 |
|
... | ... | |
142 | 135 |
//android.util.Log.e("Effects", "vertHeader= "+mainVertHeader); |
143 | 136 |
//android.util.Log.e("Effects", "fragHeader= "+mainFragHeader); |
144 | 137 |
|
145 |
String[] feedback = { "v_Position", "v_Normal", "v_ndcPosition" };
|
|
138 |
String[] feedback = { "v_Position", "v_endPosition" };
|
|
146 | 139 |
|
147 | 140 |
mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL, feedback); |
148 | 141 |
|
... | ... | |
173 | 166 |
mBlitTextureH = GLES30.glGetUniformLocation( blitProgramH, "u_Texture"); |
174 | 167 |
mBlitDepthH = GLES30.glGetUniformLocation( blitProgramH, "u_Depth"); |
175 | 168 |
|
176 |
// DEBUG PROGRAM //////////////////////////////////////
|
|
177 |
final InputStream debugVertexStream = resources.openRawResource(R.raw.test_vertex_shader);
|
|
178 |
final InputStream debugFragmentStream = resources.openRawResource(R.raw.test_fragment_shader);
|
|
169 |
// NORMAL PROGRAM //////////////////////////////////////
|
|
170 |
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader);
|
|
171 |
final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
|
|
179 | 172 |
|
180 | 173 |
try |
181 | 174 |
{ |
182 |
mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
|
|
175 |
mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
|
|
183 | 176 |
} |
184 | 177 |
catch(Exception e) |
185 | 178 |
{ |
186 |
android.util.Log.e("EFFECTS", "exception trying to compile DEBUG program: "+e.getMessage());
|
|
179 |
android.util.Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
|
|
187 | 180 |
throw new RuntimeException(e.getMessage()); |
188 | 181 |
} |
189 | 182 |
|
190 |
int debugProgramH = mDebugProgram.getProgramHandle(); |
|
191 |
mDebugObjDH = GLES30.glGetUniformLocation( debugProgramH, "u_objD"); |
|
192 |
mDebugMVPMatrixH = GLES30.glGetUniformLocation( debugProgramH, "u_MVPMatrix"); |
|
183 |
int normalProgramH = mNormalProgram.getProgramHandle(); |
|
184 |
mNormalMVPMatrixH = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix"); |
|
193 | 185 |
} |
194 | 186 |
|
195 | 187 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
232 | 224 |
|
233 | 225 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
234 | 226 |
|
235 |
@SuppressWarnings("unused") |
|
236 |
private void displayBoundingRect(float halfW, float halfH, DistortedOutputSurface surface, MeshObject mesh) |
|
227 |
private void displayNormals(MeshObject mesh) |
|
237 | 228 |
{ |
238 | 229 |
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]); |
239 | 230 |
GLES30.glBeginTransformFeedback( GLES30.GL_POINTS); |
240 |
|
|
241 | 231 |
DistortedRenderState.switchOffDrawing(); |
242 | 232 |
GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices); |
243 | 233 |
DistortedRenderState.restoreDrawing(); |
244 |
|
|
245 |
int error = GLES30.glGetError(); |
|
246 |
|
|
247 |
if( error != GLES30.GL_NO_ERROR ) |
|
248 |
{ |
|
249 |
throw new RuntimeException("DrawArrays: glError 0x" + Integer.toHexString(error)); |
|
250 |
} |
|
251 |
|
|
252 |
// mapping the buffer range slows down rendering ///////////////////////// |
|
253 |
int size = (MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE+MeshObject.NDC_DATA_SIZE)*mesh.numVertices; |
|
254 |
|
|
255 |
Buffer mappedBuffer = GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 4*size, GLES30.GL_MAP_READ_BIT); |
|
256 |
FloatBuffer fb = ((ByteBuffer) mappedBuffer).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
257 |
GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER); |
|
258 |
// end of slowdown ////////////////////////////////////////////////////// |
|
259 |
|
|
260 | 234 |
GLES30.glEndTransformFeedback(); |
261 | 235 |
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0); |
262 |
/* |
|
263 |
String msg = ""; |
|
264 |
for(int i=0; i<size; i++) msg += (" "+fb.get(i)); |
|
265 |
android.util.Log.d( "Feedback", msg); |
|
266 |
*/ |
|
267 |
float minx = Integer.MAX_VALUE; |
|
268 |
float maxx = Integer.MIN_VALUE; |
|
269 |
float miny = Integer.MAX_VALUE; |
|
270 |
float maxy = Integer.MIN_VALUE; |
|
271 |
float ndcX,ndcY; |
|
272 |
|
|
273 |
for(int i=0; i<mesh.numVertices; i++) |
|
274 |
{ |
|
275 |
ndcX = fb.get(8*i+6); |
|
276 |
ndcY = fb.get(8*i+7); |
|
277 |
|
|
278 |
if( ndcX<minx ) minx = ndcX; |
|
279 |
if( ndcX>maxx ) maxx = ndcX; |
|
280 |
if( ndcY<miny ) miny = ndcY; |
|
281 |
if( ndcY>maxy ) maxy = ndcY; |
|
282 |
} |
|
283 |
|
|
284 |
minx = surface.mWidth *(minx+1)/2; // |
|
285 |
miny = surface.mHeight*(miny+1)/2; // transform from NDC to |
|
286 |
maxx = surface.mWidth *(maxx+1)/2; // window coordinates |
|
287 |
maxy = surface.mHeight*(maxy+1)/2; // |
|
288 | 236 |
|
289 |
mDebugProgram.useProgram(); |
|
290 |
|
|
291 |
Matrix.setIdentityM( mTmpMatrix, 0); |
|
292 |
Matrix.translateM ( mTmpMatrix, 0, minx-surface.mWidth/2, maxy-surface.mHeight/2, -surface.mDistance); |
|
293 |
Matrix.scaleM ( mTmpMatrix, 0, (maxx-minx)/(2*halfW), (maxy-miny)/(2*halfH), 1.0f); |
|
294 |
Matrix.translateM ( mTmpMatrix, 0, halfW,-halfH, 0); |
|
295 |
Matrix.multiplyMM ( mMVPMatrix, 0, surface.mProjectionMatrix, 0, mTmpMatrix, 0); |
|
296 |
|
|
297 |
GLES30.glUniform2f(mDebugObjDH, 2*halfW, 2*halfH); |
|
298 |
GLES30.glUniformMatrix4fv(mDebugMVPMatrixH, 1, false, mMVPMatrix , 0); |
|
299 |
|
|
300 |
GLES30.glVertexAttribPointer(mDebugProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
|
301 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
|
237 |
mNormalProgram.useProgram(); |
|
238 |
GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0); |
|
239 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttTFO[0]); |
|
240 |
GLES30.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0); |
|
241 |
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0); |
|
242 |
GLES30.glLineWidth(8.0f); |
|
243 |
GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*mesh.numVertices); |
|
302 | 244 |
} |
303 | 245 |
|
304 | 246 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
329 | 271 |
|
330 | 272 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices); |
331 | 273 |
|
332 |
displayBoundingRect(halfW,halfH,surface,mesh);
|
|
274 |
if( mesh.mShowNormals ) displayNormals(mesh);
|
|
333 | 275 |
} |
334 | 276 |
|
335 | 277 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Use Transform Feedback to (optionally) display the Normal vectors.