Project

General

Profile

« Previous | Next » 

Revision bd3da5b2

Added by Leszek Koltunski over 7 years ago

Move DistortedNode and Distorted to use the new DistortedRenderTarget.

View differences:

src/main/java/org/distorted/library/DistortedNode.java
34 34
 */
35 35
public class DistortedNode 
36 36
  {
37
  private static final int TEXTURE_FAILED_TO_CREATE = -1;   
38
  private static final int TEXTURE_NOT_CREATED_YET  = -2;   
39
   
37
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
38
  private static long mNextNodeID =0;
39

  
40 40
  private DistortedObject mObject;
41 41
  private NodeData mData;
42
  
42

  
43 43
  private DistortedNode mParent;
44 44
  private ArrayList<DistortedNode> mChildren;
45 45
  private int[] mNumChildren;  // ==mChildren.length(), but we only create mChildren if the first one gets added
46
  
47
  private static HashMap<ArrayList<Long>,NodeData> mMapNodeID = new HashMap<>();
48
  private static long mNextNodeID =0;
49 46

  
50
  //////////////////////////////////////////////////////////////////
51
  
52 47
  private class NodeData
53 48
    {
54
    long ID;  
49
    long ID;
55 50
    int numPointingNodes;
56
    int mFramebufferID;
57
    int mTextureID;
58
    DistortedProjection mProjection;
51
    DistortedRenderTarget mDRT;
59 52
    boolean mRendered;
60
    
61
  //////////////////////////////////////////////////////////////////
62
 
63
   NodeData(long id)
64
     {
65
     ID = id;
66
     numPointingNodes= 1;
67
     mFramebufferID = 0;
68
     mTextureID = TEXTURE_NOT_CREATED_YET;
69
     mProjection = null;
70
     mRendered = false;
71
     }
72
   
73
  //////////////////////////////////////////////////////////////////
74

  
75
    boolean createFBO()
76
      {  
77
      int[] textureIds = new int[1];
78
      GLES20.glGenTextures(1, textureIds, 0);
79
      mTextureID = textureIds[0];
80
      int[] mFBORenderToTexture = new int[1];
81
      
82
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
83
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
84
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
85
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
86
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
87
      GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mProjection.width, mProjection.height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
88

  
89
      GLES20.glGenFramebuffers(1, mFBORenderToTexture, 0);
90
      GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBORenderToTexture[0]);
91
      GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mTextureID, 0);
92
      int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
93
    
94
      if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
95
        {
96
        android.util.Log.e("Node", "failed to create framebuffer, error="+status);  
97
        
98
        GLES20.glDeleteTextures(1, textureIds, 0);
99
        GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
100
        mFramebufferID = 0;
101
        mTextureID = TEXTURE_FAILED_TO_CREATE;
102
        return false;
103
        }
104
      
105
      mFramebufferID = mFBORenderToTexture[0];
106
      
107
      return true;
108
      }
109
   
110
  //////////////////////////////////////////////////////////////////
111 53

  
112
    void deleteFBO()
54
    NodeData(long id)
113 55
      {
114
      int[] textureIds = new int[1];
115
      int[] mFBORenderToTexture = new int[1];
116
     
117
      textureIds[0] = mTextureID;
118
      mFBORenderToTexture[0] = mFramebufferID;
119
      
120
      GLES20.glDeleteTextures(1, textureIds, 0);
121
      GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
122
      
123
      mFramebufferID = 0;
124
      mTextureID = TEXTURE_NOT_CREATED_YET;
56
      ID              = id;
57
      numPointingNodes= 1;
58
      mDRT            = null;
59
      mRendered       = false;
125 60
      }
126
   }
61
    }
127 62
 
128 63
///////////////////////////////////////////////////////////////////////////////////////////////////
129 64

  
......
147 82
  
148 83
///////////////////////////////////////////////////////////////////////////////////////////////////
149 84
  
150
  private void drawRecursive(long currTime, DistortedProjection dp, int mFBO)
85
  private void drawRecursive(long currTime, DistortedRenderTarget drt)
151 86
    {
152 87
    if( mNumChildren[0]<=0 )
153 88
      {
154 89
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
155
      
156
      if( mData.mProjection!=null )
157
        {
158
        mData.deleteFBO();
159
        mData.mProjection = null;
160
        }
161 90
      }
162 91
    else
163 92
      {
164 93
      if( mData.mRendered==false )
165 94
        {
166 95
        mData.mRendered = true;
167
       
168
        if( mData.mTextureID==TEXTURE_NOT_CREATED_YET ) mData.createFBO();
169
       
170
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mData.mFramebufferID);
96
        mData.mDRT.setOutput();
97

  
171 98
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
172 99
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
173 100
      
174 101
        if( mObject.mBitmapSet[0] )
175 102
          {
176 103
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
177
          mObject.drawNoEffectsPriv(mData.mProjection);
104
          mObject.drawNoEffectsPriv(mData.mDRT);
178 105
          }
179 106
      
180 107
        synchronized(this)
181 108
          {
182 109
          for(int i=0; i<mNumChildren[0]; i++)
183 110
            {
184
            mChildren.get(i).drawRecursive(currTime, mData.mProjection, mData.mFramebufferID);
111
            mChildren.get(i).drawRecursive(currTime, mData.mDRT);
185 112
            }
186 113
          }
187 114
        }
188
      
189
      GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBO);
190
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mData.mTextureID);   // this is safe because we must have called createFBO() above before.     
115

  
116
      drt.setOutput();
117
      mData.mDRT.setInput();   // this is safe because we must have called createFBO() above before.
191 118
      }
192 119
    
193
    mObject.drawPriv(currTime, dp);
120
    mObject.drawPriv(currTime, drt);
194 121
    }
195 122
  
196 123
///////////////////////////////////////////////////////////////////////////////////////////////////
......
246 173
      if( otherNodesPoint )  mData = new NodeData(++mNextNodeID);
247 174
      else                   mData.ID = ++mNextNodeID;  // numPointingNodes must be 1 already
248 175

  
249
      if( newList.size()>1 && mData.mProjection==null )
250
        {     
251
        mData.mProjection = new DistortedProjection();
252
        mData.mProjection.onSurfaceChanged(mObject.getWidth(), mObject.getHeight());
253
        mData.mFramebufferID = 0;
254
        mData.mTextureID = TEXTURE_NOT_CREATED_YET;
255
        }
256
       
176
      if( newList.size()>1 && mData.mDRT==null )
177
        mData.mDRT = new DistortedRenderTarget(mObject.getWidth(), mObject.getHeight());
178

  
257 179
      mMapNodeID.put(newList, mData);
258 180
      }
259 181
    else
......
275 197
      {
276 198
      tmp = mMapNodeID.get(key);
277 199
          
278
      if( tmp.mProjection!=null )
200
      if( tmp.mDRT != null )
279 201
        {
280
    	  tmp.mTextureID = TEXTURE_NOT_CREATED_YET;
202
    	  tmp.mDRT.reset();
281 203
        tmp.mRendered  = false;
282 204
        }
283 205
      }
......
522 444
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
523 445
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
524 446
    GLES20.glUniform1i(Distorted.mTextureUniformH, 0);  
525
    
447

  
448
    DistortedRenderTargetList.deleteAllMarked();
449

  
526 450
    markRecursive();
527
    drawRecursive(currTime,Distorted.mProjection,0);
451
    drawRecursive(currTime,Distorted.mRenderTarget);
528 452
    }
529 453
 
530 454
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff