Project

General

Profile

« Previous | Next » 

Revision 8fa96e69

Added by Leszek Koltunski almost 8 years ago

Move more intelligence to EffectQueuePostprocess.

View differences:

src/main/java/org/distorted/library/Distorted.java
163 163

  
164 164
    EffectQueuePostprocess.getUniforms(postProgramH);
165 165

  
166
    DistortedEffects.setPrograms(mainProgram,postProgram);
166
    DistortedEffects.setProgram(mainProgram);
167
    EffectQueuePostprocess.setProgram(postProgram);
167 168
    DistortedTree.reset();
168 169
    EffectMessageSender.startSending();
169 170
    }
src/main/java/org/distorted/library/DistortedEffects.java
20 20
package org.distorted.library;
21 21

  
22 22
import android.opengl.GLES20;
23
import android.opengl.Matrix;
23 24

  
24 25
import org.distorted.library.message.EffectListener;
25 26
import org.distorted.library.program.DistortedProgram;
......
42 43
 */
43 44
public class DistortedEffects
44 45
  {
45
  private static final int BYTES_PER_FLOAT   = 4; //
46
  private static final int MAIN_POSITION_DATA_SIZE= 3; // Size of the position data in elements
47
  private static final int MAIN_NORMAL_DATA_SIZE  = 3; // Size of the position data in elements
48
  private static final int MAIN_TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
49
  private static final int POST_POSITION_DATA_SIZE= 2; // Size of the position data in elements
50
  private static final int POST_TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
46
  private static final int POSITION_DATA_SIZE= 3; // Main Program: size of the position data in elements
47
  private static final int NORMAL_DATA_SIZE  = 3; // Main Program: size of the normal data in elements
48
  private static final int TEX_DATA_SIZE     = 2; // Main Program: size of the texture coordinate data in elements.
51 49

  
52
  private static final FloatBuffer mQuadPositions, mQuadTexture;
53
  private static DistortedProgram mMainProgram, mPostProgram;
50
  private static DistortedProgram mProgram;
51

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

  
55 55
  private static long mNextID =0;
56 56
  private long mID;
57 57

  
58
  private static DistortedFramebuffer mBufferFBO;
58
  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(1,1);
59 59

  
60 60
  private EffectQueueMatrix      mM;
61 61
  private EffectQueueFragment    mF;
......
64 64

  
65 65
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
66 66

  
67
  static
68
    {
69
    int dataLength = 4;
70

  
71
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
72
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
73

  
74
    mQuadPositions = ByteBuffer.allocateDirect(POST_POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
75
    mQuadPositions.put(positionData).position(0);
76
    mQuadTexture   = ByteBuffer.allocateDirect(POST_TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
77
    mQuadTexture.put(textureData).position(0);
78

  
79
    mBufferFBO = new DistortedFramebuffer(1,1);
80
    }
81

  
82 67
///////////////////////////////////////////////////////////////////////////////////////////////////
83 68

  
84
  static void setPrograms(DistortedProgram main, DistortedProgram post)
69
  static void setProgram(DistortedProgram p)
85 70
    {
86
    mMainProgram = main;
87
    mPostProgram = post;
71
    mProgram = p;
88 72
    }
89 73

  
90 74
///////////////////////////////////////////////////////////////////////////////////////////////////
......
150 134

  
151 135
    if( mP.mNumEffects==0 )
152 136
      {
153
      mMainProgram.useProgram();
137
      mProgram.useProgram();
138
      df.setAsOutput();
154 139
      mM.send(df,halfInputW,halfInputH,halfZ);
155 140
      mV.send(halfInputW,halfInputH,halfZ);
156 141
      mF.send(halfInputW,halfInputH);
157
      GLES20.glVertexAttribPointer(mMainProgram.mAttribute[0], MAIN_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
158
      GLES20.glVertexAttribPointer(mMainProgram.mAttribute[1], MAIN_NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
159
      GLES20.glVertexAttribPointer(mMainProgram.mAttribute[2], MAIN_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
142
      GLES20.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
143
      GLES20.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
144
      GLES20.glVertexAttribPointer(mProgram.mAttribute[2], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
160 145
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
161 146
      }
162 147
    else
163 148
      {
164 149
      if( mV.mNumEffects>0 || mF.mNumEffects>0 )
165 150
        {
166
        mMainProgram.useProgram();
151
        mProgram.useProgram();
167 152
        mBufferFBO.resizeFast(df.mWidth, df.mHeight);
168 153
        mBufferFBO.setAsOutput();
154
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
155
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
169 156
        mM.send(mBufferFBO,halfInputW,halfInputH,halfZ);
170 157
        mV.send(halfInputW,halfInputH,halfZ);
171 158
        mF.send(halfInputW,halfInputH);
172
        GLES20.glVertexAttribPointer(mMainProgram.mAttribute[0], MAIN_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
173
        GLES20.glVertexAttribPointer(mMainProgram.mAttribute[1], MAIN_NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
174
        GLES20.glVertexAttribPointer(mMainProgram.mAttribute[2], MAIN_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
159
        GLES20.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
160
        GLES20.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
161
        GLES20.glVertexAttribPointer(mProgram.mAttribute[2], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
175 162
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
176 163

  
177
        mPostProgram.useProgram();
164
        Matrix.setIdentityM(mTmpMatrix, 0);
165
        Matrix.translateM(mTmpMatrix, 0, 0, 0, -df.mDistance);
166
        Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
167

  
178 168
        mBufferFBO.setAsInput();
179
        df.setAsOutput();
180
        mP.send(df);
181
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[0], POST_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
182
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[1], POST_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
183
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
169
        mP.render(df.mWidth, df.mHeight, mMVPMatrix, df);
184 170
        }
185 171
      else
186 172
        {
187
        mPostProgram.useProgram();
188 173
        mM.constructMatrices(df,halfInputW,halfInputH);
189
        mP.send(2*halfInputW,2*halfInputH, mM.getMVP() );
190
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[0], POST_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
191
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[1], POST_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
192
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
174
        mP.render(2*halfInputW, 2*halfInputH, mM.getMVP(), df);
193 175
        }
194 176
      }
195 177
    }
......
200 182
    {
201 183
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
202 184

  
185
    Matrix.setIdentityM(mTmpMatrix, 0);
186
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -df.mDistance);
187
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
188

  
203 189
    EffectQueueMatrix.sendZero(df,halfInputW,halfInputH,halfInputW*mesh.zFactor);
204 190
    EffectQueueVertex.sendZero();
205 191
    EffectQueueFragment.sendZero();
206
    EffectQueuePostprocess.sendZero(df);
192
    EffectQueuePostprocess.sendZero(df.mWidth, df.mHeight, mMVPMatrix);
207 193

  
208
    GLES20.glVertexAttribPointer(mMainProgram.mAttribute[0], MAIN_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
209
    GLES20.glVertexAttribPointer(mMainProgram.mAttribute[1], MAIN_NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
210
    GLES20.glVertexAttribPointer(mMainProgram.mAttribute[2], MAIN_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
194
    GLES20.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
195
    GLES20.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
196
    GLES20.glVertexAttribPointer(mProgram.mAttribute[2], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
211 197
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
212 198
    }
213 199
    
src/main/java/org/distorted/library/DistortedFramebuffer.java
363 363
    DistortedTexture.deleteAllMarked();
364 364
    createFBO();
365 365
    tex.setAsInput();
366
    setAsOutput();
367 366
    effects.drawPriv(tex.mHalfX, tex.mHalfY, mesh, this, time);
368 367
    }
369 368

  
......
389 388
      DistortedFramebuffer.deleteAllMarked();
390 389
      DistortedTexture.deleteAllMarked();
391 390
      createFBO();
392
      setAsOutput();
393 391
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fbo.colorIds[0]);
394 392
      effects.drawPriv(fbo.mWidth/2, fbo.mHeight/2, mesh, this, time);
395 393
      }
......
409 407
    DistortedFramebuffer.deleteAllMarked();
410 408
    DistortedTexture.deleteAllMarked();
411 409
    createFBO();
412
    setAsOutput();
413 410
    dt.drawRecursive(time,this);
414 411
    }
415 412

  
src/main/java/org/distorted/library/DistortedTree.java
231 231

  
232 232
      mData.numRendered++;
233 233
      mData.numRendered %= mData.numPointingNodes;
234

  
235
      df.setAsOutput();
236 234
      mData.mFBO.setAsInput();
237 235
      }
238 236

  
src/main/java/org/distorted/library/EffectQueuePostprocess.java
20 20
package org.distorted.library;
21 21

  
22 22
import android.opengl.GLES20;
23
import android.opengl.Matrix;
24 23

  
25 24
import org.distorted.library.message.EffectMessage;
25
import org.distorted.library.program.DistortedProgram;
26 26
import org.distorted.library.type.Data1D;
27 27
import org.distorted.library.type.Data2D;
28 28
import org.distorted.library.type.Dynamic1D;
......
30 30
import org.distorted.library.type.Static1D;
31 31
import org.distorted.library.type.Static2D;
32 32

  
33
import java.nio.ByteBuffer;
34
import java.nio.ByteOrder;
35
import java.nio.FloatBuffer;
36

  
33 37
///////////////////////////////////////////////////////////////////////////////////////////////////
34 38

  
35 39
class EffectQueuePostprocess extends EffectQueue
36 40
  {
41
  private static final int BYTES_PER_FLOAT   = 4;
42
  private static final int POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
43
  private static final int TEX_DATA_SIZE     = 2; // Post Program: size of the texture coordinate data in elements.
44

  
37 45
  private static final int NUM_UNIFORMS = 3;
38 46
  private static final int NUM_CACHE    = 0;
39 47
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
......
44 52
  private static int mObjDH;
45 53
  private static int mMVPMatrixH;
46 54

  
47
  private static float[] mMVPMatrix = new float[16];
48
  private static float[] mTmpMatrix = new float[16];
55
  private static final FloatBuffer mQuadPositions, mQuadTexture;
56
  private static DistortedProgram mProgram;
57

  
58
  static
59
    {
60
    int dataLength = 4;
61

  
62
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
63
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
64

  
65
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
66
    mQuadPositions.put(positionData).position(0);
67
    mQuadTexture   = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadTexture.put(textureData).position(0);
69
    }
49 70

  
50 71
///////////////////////////////////////////////////////////////////////////////////////////////////
51 72

  
......
65 86
    mMVPMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
66 87
    }
67 88

  
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

  
91
  static void setProgram(DistortedProgram p)
92
    {
93
    mProgram = p;
94
    }
95

  
68 96
///////////////////////////////////////////////////////////////////////////////////////////////////
69 97
  
70 98
  synchronized void compute(long currTime) 
......
115 143

  
116 144
///////////////////////////////////////////////////////////////////////////////////////////////////
117 145

  
118
  synchronized void send(float w, float h, float[] mvp)
146
  synchronized static void sendZero(float w, float h, float[] mvp)
119 147
    {
120
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
121
    GLES20.glUniform2f( mObjDH , w, h );
148
    GLES20.glUniform1i( mNumEffectsH, 0);
149
    GLES20.glUniform2f( mObjDH , w, h);
122 150
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
123

  
124
    if( mNumEffects>0 )
125
      {
126
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
127
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
128
      }
129 151
    }
130 152

  
131 153
///////////////////////////////////////////////////////////////////////////////////////////////////
132 154

  
133
  synchronized void send(DistortedFramebuffer df)
155
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
134 156
    {
135
    Matrix.setIdentityM(mTmpMatrix, 0);
136
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -df.mDistance);
137
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
157
    mProgram.useProgram();
158
    df.setAsOutput();
138 159

  
139 160
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
140
    GLES20.glUniform2f( mObjDH , df.mWidth, df.mHeight);
141
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
161
    GLES20.glUniform2f( mObjDH , w, h );
162
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
142 163

  
143 164
    if( mNumEffects>0 )
144 165
      {
145 166
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
146 167
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
147 168
      }
148
    }
149

  
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151 169

  
152
  synchronized static void sendZero(DistortedFramebuffer df)
153
    {
154
    Matrix.setIdentityM(mTmpMatrix, 0);
155
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -df.mDistance);
156
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
157

  
158
    GLES20.glUniform1i( mNumEffectsH, 0);
159
    GLES20.glUniform2f( mObjDH , df.mWidth, df.mHeight);
160
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
170
    GLES20.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
171
    GLES20.glVertexAttribPointer(mProgram.mAttribute[1], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
172
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
161 173
    }
162 174

  
163 175
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff