Project

General

Profile

« Previous | Next » 

Revision 55c14a19

Added by Leszek Koltunski almost 8 years ago

Minor reorganization.

View differences:

src/main/java/org/distorted/library/Distorted.java
77 77
   */
78 78
  public static final int CLONE_CHILDREN= 0x20;
79 79

  
80
  static DistortedProgram mMainProgram, mPostProgram;
80
  private static boolean mInitialized=false;
81 81

  
82 82
///////////////////////////////////////////////////////////////////////////////////////////////////
83 83
// private: hide this from Javadoc
......
91 91

  
92 92
  static boolean isInitialized()
93 93
    {
94
    return (mMainProgram!=null);
94
    return mInitialized;
95 95
    }
96 96

  
97 97
///////////////////////////////////////////////////////////////////////////////////////////////////
......
132 132
        mainFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
133 133
      }
134 134

  
135
    mMainProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
136
    int mainProgramH = mMainProgram.getProgramHandle();
135
    DistortedProgram mainProgram = new DistortedProgram(mainVertexStream,mainFragmentStream, mainVertexHeader, mainFragmentHeader);
136
    int mainProgramH = mainProgram.getProgramHandle();
137 137

  
138 138
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
139 139
    GLES20.glEnable(GLES20.GL_BLEND);
......
158 158
        postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
159 159
      }
160 160

  
161
    mPostProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
162
    int postProgramH = mPostProgram.getProgramHandle();
161
    DistortedProgram postProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
162
    int postProgramH = postProgram.getProgramHandle();
163 163

  
164 164
    EffectQueuePostprocess.getUniforms(postProgramH);
165 165

  
166
    DistortedEffects.setPrograms(mainProgram,postProgram);
166 167
    DistortedTree.reset();
167 168
    EffectMessageSender.startSending();
168 169
    }
......
181 182
    DistortedEffects.onDestroy();
182 183
    EffectMessageSender.stopSending();
183 184

  
184
    mMainProgram = null;
185
    mPostProgram = null;
185
    mInitialized = false;
186 186
    }
187 187
  }
src/main/java/org/distorted/library/DistortedEffects.java
22 22
import android.opengl.GLES20;
23 23

  
24 24
import org.distorted.library.message.EffectListener;
25
import org.distorted.library.program.DistortedProgram;
25 26
import org.distorted.library.type.Data1D;
26 27
import org.distorted.library.type.Data2D;
27 28
import org.distorted.library.type.Data3D;
......
42 43
public class DistortedEffects
43 44
  {
44 45
  private static final int BYTES_PER_FLOAT   = 4; //
45
  private static final int POSITION_DATA_SIZE= 2; // Size of the position data in elements
46
  private static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
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.
51

  
47 52
  private static final FloatBuffer mQuadPositions, mQuadTexture;
53
  private static DistortedProgram mMainProgram, mPostProgram;
48 54

  
49 55
  private static long mNextID =0;
50 56
  private long mID;
......
65 71
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
66 72
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
67 73

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

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

  
76 82
///////////////////////////////////////////////////////////////////////////////////////////////////
77
    
83

  
84
  static void setPrograms(DistortedProgram main, DistortedProgram post)
85
    {
86
    mMainProgram = main;
87
    mPostProgram = post;
88
    }
89

  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

  
78 92
  private void initializeEffectLists(DistortedEffects d, int flags)
79 93
    {
80 94
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
......
132 146
    mP.compute(currTime);
133 147

  
134 148
    float halfZ = halfInputW*mesh.zFactor;
149
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
135 150

  
136 151
    if( mP.mNumEffects==0 )
137 152
      {
138
      Distorted.mMainProgram.useProgram();
139
      GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
140

  
153
      mMainProgram.useProgram();
141 154
      mM.send(df,halfInputW,halfInputH,halfZ);
142 155
      mV.send(halfInputW,halfInputH,halfZ);
143 156
      mF.send(halfInputW,halfInputH);
144

  
145
      mesh.draw();
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);
160
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
146 161
      }
147 162
    else
148 163
      {
149 164
      if( mV.mNumEffects>0 || mF.mNumEffects>0 )
150 165
        {
151
        Distorted.mMainProgram.useProgram();
152
        GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
153

  
166
        mMainProgram.useProgram();
154 167
        mBufferFBO.resizeFast(df.mWidth, df.mHeight);
155 168
        mBufferFBO.setAsOutput();
156

  
157 169
        mM.send(mBufferFBO,halfInputW,halfInputH,halfZ);
158 170
        mV.send(halfInputW,halfInputH,halfZ);
159 171
        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);
175
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
160 176

  
161
        mesh.draw();
162

  
163
        Distorted.mPostProgram.useProgram();
177
        mPostProgram.useProgram();
164 178
        mBufferFBO.setAsInput();
165 179
        df.setAsOutput();
166 180
        mP.send(df);
167

  
168
        GLES20.glVertexAttribPointer(Distorted.mPostProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
169
        GLES20.glVertexAttribPointer(Distorted.mPostProgram.mAttribute[1], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
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);
170 183
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
171 184
        }
172 185
      else
173 186
        {
174
        Distorted.mPostProgram.useProgram();
175
        GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
176

  
187
        mPostProgram.useProgram();
177 188
        mM.constructMatrices(df,halfInputW,halfInputH);
178 189
        mP.send(2*halfInputW,2*halfInputH, mM.getMVP() );
179

  
180
        GLES20.glVertexAttribPointer(Distorted.mPostProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
181
        GLES20.glVertexAttribPointer(Distorted.mPostProgram.mAttribute[1], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
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);
182 192
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
183 193
        }
184 194
      }
......
195 205
    EffectQueueFragment.sendZero();
196 206
    EffectQueuePostprocess.sendZero(df);
197 207

  
198
    mesh.draw();
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);
211
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
199 212
    }
200 213
    
201 214
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/MeshObject.java
21 21

  
22 22
import java.nio.FloatBuffer;
23 23

  
24
import android.opengl.GLES20;
25

  
26 24
///////////////////////////////////////////////////////////////////////////////////////////////////
27 25
/**
28 26
 * Abstract class which represents a Mesh, ie 3 arrays of Vertex attributes: 1) positions
......
33 31
 */
34 32
public abstract class MeshObject
35 33
   {
36
   protected static final int BYTES_PER_FLOAT   = 4; //
37
   protected static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
38
   protected static final int NORMAL_DATA_SIZE  = 3; // Size of the normal data in elements.
39
   protected static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements. 
34
   static final int BYTES_PER_FLOAT   = 4; //
35
   static final int POSITION_DATA_SIZE= 3; // Size of the position data in elements
36
   static final int NORMAL_DATA_SIZE  = 3; // Size of the normal data in elements.
37
   static final int TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
40 38

  
41
   protected int dataLength;
42
   protected FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture;
39
   int dataLength;
40
   FloatBuffer mMeshPositions, mMeshNormals, mMeshTexture;
43 41

  
44 42
   final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
45 43
                        // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
......
50 48
     {
51 49
     zFactor = factor;
52 50
     }
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

  
56
   void draw()
57
     { 
58
     GLES20.glVertexAttribPointer(Distorted.mMainProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mMeshPositions);
59
     GLES20.glVertexAttribPointer(Distorted.mMainProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mMeshNormals);
60
     GLES20.glVertexAttribPointer(Distorted.mMainProgram.mAttribute[2], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mMeshTexture);
61

  
62
     GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, dataLength); 
63
     }
64 51
   }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff