Project

General

Profile

« Previous | Next » 

Revision c90b9e01

Added by Leszek Koltunski over 7 years ago

Progress with getting Mesh'es bounding rectangle. Still doesn't quite work yet (visible : 'Cubes' app with a 0111 Mesh)

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
19 19

  
20 20
package org.distorted.library;
21 21

  
22
import android.content.res.Resources;
22 23
import android.opengl.GLES20;
23 24
import android.opengl.Matrix;
24 25

  
25 26
import org.distorted.library.message.EffectListener;
26 27
import org.distorted.library.program.DistortedProgram;
28
import org.distorted.library.program.FragmentCompilationException;
29
import org.distorted.library.program.FragmentUniformsException;
30
import org.distorted.library.program.LinkingException;
31
import org.distorted.library.program.VertexCompilationException;
32
import org.distorted.library.program.VertexUniformsException;
27 33
import org.distorted.library.type.Data1D;
28 34
import org.distorted.library.type.Data2D;
29 35
import org.distorted.library.type.Data3D;
......
31 37
import org.distorted.library.type.Data5D;
32 38
import org.distorted.library.type.Static3D;
33 39

  
40
import java.io.InputStream;
34 41
import java.nio.ByteBuffer;
35 42
import java.nio.ByteOrder;
36 43
import java.nio.FloatBuffer;
......
49 56

  
50 57
  private static DistortedProgram mProgram;
51 58

  
59
  /// DEBUG ONLY /////
60
  private static DistortedProgram mDebugProgram;
61
  private static final FloatBuffer mQuadPositions;
62

  
63
  static
64
    {
65
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
66
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
67
    mQuadPositions.put(positionData).position(0);
68
    }
69

  
70
  private static int mObjDH;
71
  private static int mMVPMatrixH;
72
  /// END DEBUG //////
73

  
52 74
  private static float[] mMVPMatrix = new float[16];
53 75
  private static float[] mTmpMatrix = new float[16];
54 76

  
......
66 88

  
67 89
///////////////////////////////////////////////////////////////////////////////////////////////////
68 90

  
69
  static void setProgram(DistortedProgram p)
91
  static void createProgram(Resources resources)
92
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
70 93
    {
71
    mProgram = p;
94
    final InputStream mainVertexStream   = resources.openRawResource(R.raw.main_vertex_shader);
95
    final InputStream mainFragmentStream = resources.openRawResource(R.raw.main_fragment_shader);
96

  
97
    String mainVertexHeader= ("#version 100\n#define NUM_VERTEX "  + getMaxVertex()+"\n");
98

  
99
    for(EffectNames name: EffectNames.values() )
100
      {
101
      if( name.getType()== EffectTypes.VERTEX)
102
        mainVertexHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
103
      }
104

  
105
    String mainFragmentHeader= ("#version 100\n#define NUM_FRAGMENT "  + getMaxFragment()+"\n");
106

  
107
    for(EffectNames name: EffectNames.values() )
108
      {
109
      if( name.getType()== EffectTypes.FRAGMENT)
110
        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
111
      }
112

  
113
    mProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
114

  
115
    int mainProgramH = mProgram.getProgramHandle();
116
    EffectQueueFragment.getUniforms(mainProgramH);
117
    EffectQueueVertex.getUniforms(mainProgramH);
118
    EffectQueueMatrix.getUniforms(mainProgramH);
119
    DistortedTexture.getUniforms(mainProgramH);
120

  
121
    // DEBUG ONLY //////////////////////////////////////
122
    final InputStream debugVertexStream   = resources.openRawResource(R.raw.test_vertex_shader);
123
    final InputStream debugFragmentStream = resources.openRawResource(R.raw.test_fragment_shader);
124

  
125
    mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, "#version 100\n", "#version 100\n");
126

  
127
    int debugProgramH = mDebugProgram.getProgramHandle();
128
    mObjDH      = GLES20.glGetUniformLocation( debugProgramH, "u_objD");
129
    mMVPMatrixH = GLES20.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
130
    // END DEBUG  //////////////////////////////////////
72 131
    }
73 132

  
74 133
///////////////////////////////////////////////////////////////////////////////////////////////////
......
121 180
    }
122 181

  
123 182
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
183
// DEBUG ONLY
184

  
185
  private void displayBoundingRect(float halfX, float halfY, float halfZ, DistortedFramebuffer df, float[] mvp, float[] vertices)
186
    {
187
    int len  = vertices.length/3;
188
    int minx = Integer.MAX_VALUE;
189
    int maxx = Integer.MIN_VALUE;
190
    int miny = Integer.MAX_VALUE;
191
    int maxy = Integer.MIN_VALUE;
192
    int wx,wy;
193

  
194
    float x,y,z, X,Y,W, ndcX,ndcY;
195

  
196
    for(int i=0; i<len; i++)
197
      {
198
      x = 2*halfX*vertices[3*i  ];
199
      y = 2*halfY*vertices[3*i+1];
200
      z = 2*halfZ*vertices[3*i+2];
201

  
202
      X = mvp[ 0]*x + mvp[ 4]*y + mvp[ 8]*z + mvp[12];
203
      Y = mvp[ 1]*x + mvp[ 5]*y + mvp[ 9]*z + mvp[13];
204
      W = mvp[ 3]*x + mvp[ 7]*y + mvp[11]*z + mvp[15];
205

  
206
      ndcX = X/W;
207
      ndcY = Y/W;
208

  
209
      wx = (int)(df.mWidth *(ndcX+1)/2);
210
      wy = (int)(df.mHeight*(ndcY+1)/2);
211

  
212
      if( wx<minx ) minx = wx;
213
      if( wx>maxx ) maxx = wx;
214
      if( wy<miny ) miny = wy;
215
      if( wy>maxy ) maxy = wy;
216
      }
217

  
218
    mDebugProgram.useProgram();
219
    df.setAsOutput();
220

  
221
    Matrix.setIdentityM(mTmpMatrix, 0);
222
    Matrix.translateM(mTmpMatrix, 0, -df.mWidth/2, df.mHeight/2, -df.mDistance);
223

  
224
    Matrix.translateM(mTmpMatrix, 0, minx, -df.mHeight+maxy, 0.0f);
225
    Matrix.scaleM(mTmpMatrix, 0, (float)(maxx-minx)/(2*halfX), (float)(maxy-miny)/(2*halfY), 1.0f);
226

  
227
    Matrix.translateM(mTmpMatrix, 0, halfX,-halfY, 0);
228
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
229

  
230
    GLES20.glUniform2f( mObjDH , 2*halfX, 2*halfY);
231
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
232

  
233
    GLES20.glVertexAttribPointer(mDebugProgram.mAttribute[0], 2, GLES20.GL_FLOAT, false, 0, mQuadPositions);
234
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
235
    }
236

  
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

  
125 239
  void drawPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df, long currTime)
126 240
    {
127 241
    mM.compute(currTime);
......
174 288
        mP.render(df.mWidth, df.mHeight, mMVPMatrix, df);
175 289
        }
176 290
      }
291

  
292
    /// DEBUG ONLY //////
293
    displayBoundingRect(halfInputH, halfInputW, halfZ, df, mM.getMVP(), mesh.getBoundingVertices() );
294
    /// END DEBUG ///////
177 295
    }
178 296

  
179 297
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff