Project

General

Profile

« Previous | Next » 

Revision 3fc9327a

Added by Leszek Koltunski about 7 years ago

Use Transform Feedback to (optionally) display the Normal vectors.

View differences:

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
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectQueueMatrix.java
250 250
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
251 251
    }
252 252

  
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

  
255
  float[] getMVP()
256
    {
257
    return mMVPMatrix;
258
    }
259

  
253 260
///////////////////////////////////////////////////////////////////////////////////////////////////
254 261

  
255 262
  synchronized void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
src/main/java/org/distorted/library/MeshObject.java
38 38
   static final int POS_DATA_SIZE= 3;
39 39
   static final int NOR_DATA_SIZE= 3;
40 40
   static final int TEX_DATA_SIZE= 2;
41
   static final int NDC_DATA_SIZE= 2;
42 41

  
43 42
   static final int OFFSET0 =                                                           0;
44 43
   static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
45 44
   static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
46 45

  
47
   static final int TFSIZE  = (POS_DATA_SIZE+NOR_DATA_SIZE+NDC_DATA_SIZE)*BYTES_PER_FLOAT;
46
   static final int TFSIZE  = (POS_DATA_SIZE+POS_DATA_SIZE              )*BYTES_PER_FLOAT;
48 47
   static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
49 48

  
49
   boolean mShowNormals;
50

  
50 51
   int numVertices;
51 52
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX, NorY,NorZ, TexS, TexT
52 53
   int[] mAttVBO = new int[1]; // server-side packed vertex attributes
......
62 63
     super(DistortedObject.NOT_CREATED_YET,DistortedObject.TYPE_USER);
63 64

  
64 65
     zFactor = factor;
66
     mShowNormals = false;
65 67
     recreate();
66 68
     }
67 69

  
......
132 134
 * This is used to be able to quickly compute, in window coordinates, the Mesh'es bounding rectangle.
133 135
 */
134 136
   abstract float[] getBoundingVertices();
137

  
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
/**
140
 * When rendering this Mesh, do we want to render the Normal vectors as well?
141
 *
142
 * @param show Controls if we render the Normal vectors or not.
143
 */
144
   public void setShowNormals(boolean show)
145
     {
146
     mShowNormals = show;
147
     }
135 148
   }
136 149

  
150

  
151

  
src/main/res/raw/main_vertex_shader.glsl
24 24
in vec3 a_Normal;                    // Per-vertex normal vector.
25 25
in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
26 26
out vec3 v_Position;                 //
27
out vec2 v_ndcPosition;              // for Transform Feedback only
27
out vec3 v_endPosition;              // for Transform Feedback only
28 28
out vec3 v_Normal;                   //
29 29
out vec2 v_TexCoordinate;            //
30 30
#else
......
32 32
attribute vec3 a_Normal;             // Per-vertex normal vector.
33 33
attribute vec2 a_TexCoordinate;      // Per-vertex texture coordinate.
34 34
varying vec3 v_Position;             //
35
varying vec2 v_ndcPosition;          // for Transform Feedback only
35
varying vec3 v_endPosition;          // for Transform Feedback only
36 36
varying vec3 v_Normal;               //
37 37
varying vec2 v_TexCoordinate;        //
38 38
#endif
......
527 527
  {
528 528
  vec3 v = 2.0*u_objD*a_Position;
529 529
  vec3 n = a_Normal;
530
  vec4 mvpPos;
531 530

  
532 531
#if NUM_VERTEX>0
533 532
  int j=0;
......
559 558
#endif
560 559
   
561 560
  v_Position      = v;
561
  v_endPosition   = v + (0.3*u_objD.x)*n;
562 562
  v_TexCoordinate = a_TexCoordinate;
563 563
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
564
  mvpPos          = u_MVPMatrix*vec4(v,1.0);
565
  v_ndcPosition   = vec2(mvpPos.x/mvpPos.w,mvpPos.y/mvpPos.w);
566
  gl_Position     = mvpPos;
564
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
567 565
  }                               
src/main/res/raw/normal_fragment_shader.glsl
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
precision lowp float;
21

  
22
#if __VERSION__ != 100
23
out vec4 fragColor;
24
#define FRAG_COLOR fragColor
25
#else
26
#define FRAG_COLOR gl_FragColor
27
#endif
28

  
29
//////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
void main()
32
  {
33
  FRAG_COLOR = vec4(1.0,0.0,0.0,1.0);
34
  }
src/main/res/raw/normal_vertex_shader.glsl
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
precision lowp float;
21

  
22
#if __VERSION__ != 100
23
in vec3 a_Position;
24
#else
25
attribute vec3 a_Position;
26
#endif
27

  
28
uniform mat4 u_MVPMatrix;
29

  
30
//////////////////////////////////////////////////////////////////////////////////////////////
31

  
32
void main()
33
  {
34
  gl_Position = u_MVPMatrix*vec4( a_Position, 1.0);
35
  }
src/main/res/raw/test_fragment_shader.glsl
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
precision lowp float;
21

  
22
#if __VERSION__ != 100
23
out vec4 fragColor;
24
#define FRAG_COLOR fragColor
25
#else
26
#define FRAG_COLOR gl_FragColor
27
#endif
28

  
29
//////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
void main()
32
  {
33
  FRAG_COLOR = vec4(1.0,0.0,0.0,0.2);
34
  }
src/main/res/raw/test_vertex_shader.glsl
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
precision lowp float;
21

  
22
#if __VERSION__ != 100
23
in vec2 a_Position;        // Per-vertex position information we will pass in.
24
#else
25
attribute vec2 a_Position; // Per-vertex position information we will pass in.
26
#endif
27

  
28
uniform vec2 u_objD;       // object width X object height.
29
uniform mat4 u_MVPMatrix;  // the combined model/view/projection matrix.
30

  
31
//////////////////////////////////////////////////////////////////////////////////////////////
32

  
33
void main()
34
  {
35
  gl_Position = u_MVPMatrix*vec4( u_objD*a_Position, 0.0, 1.0);
36
  }

Also available in: Unified diff