Project

General

Profile

« Previous | Next » 

Revision ed13a5de

Added by Leszek Koltunski about 8 years ago

Cleanup

View differences:

src/main/java/org/distorted/library/Distorted.java
91 91
  static int mTextureCoordH;    // pass in model texture coordinate information.
92 92
  static int mProgramH;         // This is a handle to our shading program.
93 93

  
94
  static DistortedRenderTarget mRenderTarget = new DistortedRenderTarget(0);
94
  static DistortedFramebuffer mFramebuffer = new DistortedFramebuffer(0);
95 95

  
96 96
///////////////////////////////////////////////////////////////////////////////////////////////////
97 97

  
......
292 292
 */
293 293
  public static void setFov(float fov)
294 294
    {
295
    mRenderTarget.setProjection(fov,0.0f,0.0f);
295
    mFramebuffer.setProjection(fov,0.0f,0.0f);
296 296
    }
297 297
  
298 298
///////////////////////////////////////////////////////////////////////////////////////////////////
......
362 362
 */
363 363
  public static void onSurfaceChanged(int surfaceWidth, int surfaceHeight)
364 364
    {
365
    mRenderTarget.resize(surfaceWidth,surfaceHeight);
365
    mFramebuffer.resize(surfaceWidth,surfaceHeight);
366 366
    }
367 367

  
368 368
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedFramebuffer.java
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
package org.distorted.library;
21

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

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
public class DistortedFramebuffer
28
  {
29
  private static final int TEXTURE_FAILED_TO_CREATE = -1;
30
  private static final int TEXTURE_NOT_CREATED_YET  = -2;
31
  private static final int TEXTURE_DONT_CREATE      = -3;
32

  
33
  private int mFramebufferID, mTextureID;
34
  private float mX, mY;
35
  private float mFOV;
36

  
37
  int mWidth,mHeight,mDepth,mDistance;
38
  float[] mProjectionMatrix;
39
  boolean mMarked;
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
  private void createProjection()
44
    {
45
    float ratio  = (float) mWidth / mHeight;
46
    float left   =-ratio;          //
47
    float right  = ratio;          // Create a new perspective projection matrix.
48
    float bottom = -1.0f;          //
49
    float top    =  1.0f;          // any change to those values will have serious consequences!
50
    float near, far;
51

  
52
    if( mFOV>0.0f )  // perspective projection
53
      {
54
      near= (float)(top / Math.tan(mFOV*Math.PI/360));
55
      mDistance = (int)(mHeight*near/(top-bottom));
56
      far = 2*mDistance-near;
57

  
58
      Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
59
      }
60
    else                      // parallel projection
61
      {
62
      near= (float)(top / Math.tan(Math.PI/360));
63
      mDistance = (int)(mHeight*near/(top-bottom));
64
      far = 2*mDistance-near;
65

  
66
      Matrix.orthoM(mProjectionMatrix, 0, -mWidth/2, mWidth/2,-mHeight/2, mHeight/2, near, far);
67
      }
68

  
69
    mDepth = (int)((far-near)/2);
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
// must be called form a thread holding OpenGL Context
74

  
75
  private 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, mWidth, mHeight, 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("RenderTarget", "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
// must be called form a thread holding OpenGL Context
112

  
113
  private void deleteFBO()
114
    {
115
    int[] textureIds = new int[1];
116
    int[] mFBORenderToTexture = new int[1];
117

  
118
    textureIds[0] = mTextureID;
119
    mFBORenderToTexture[0] = mFramebufferID;
120

  
121
    GLES20.glDeleteTextures(1, textureIds, 0);
122
    GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
123

  
124
    mFramebufferID = 0;
125
    mTextureID = TEXTURE_NOT_CREATED_YET;
126
    }
127

  
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// must be called form a thread holding OpenGL Context
130

  
131
  void release()
132
    {
133
    if( mTextureID>=0 ) deleteFBO();
134

  
135
    mProjectionMatrix = null;
136
    mMarked = false;
137
    }
138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
  void reset()
142
    {
143
    mTextureID = TEXTURE_NOT_CREATED_YET;
144
    }
145

  
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
// PUBLIC API
148

  
149
  public DistortedFramebuffer(int width, int height)
150
    {
151
    mProjectionMatrix = new float[16];
152

  
153
    mHeight        = height;
154
    mWidth         = width;
155
    mFramebufferID = 0;
156
    mTextureID     = TEXTURE_NOT_CREATED_YET;
157
    mFOV           = 60.0f;
158
    mX             = 0.0f;
159
    mY             = 0.0f;
160

  
161
    mMarked = false;
162

  
163
    createProjection();
164
    }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
  public DistortedFramebuffer(int fbo)
169
    {
170
    mProjectionMatrix = new float[16];
171

  
172
    mFramebufferID = fbo;
173
    mTextureID     = TEXTURE_DONT_CREATE;
174
    mFOV           = 60.0f;
175
    mX             = 0.0f;
176
    mY             = 0.0f;
177

  
178
    mMarked = false;
179
    }
180

  
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

  
183
  public void markForDeletion()
184
    {
185
    DistortedFramebufferList.markForDeletion();
186
    mMarked = true;
187
    }
188

  
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

  
191
  public void setProjection(float FOV, float x, float y)
192
    {
193
    mFOV = FOV;
194
    mX   = x;
195
    mY   = y;
196

  
197
    createProjection();
198
    }
199

  
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

  
202
  public void resize(int width, int height)
203
    {
204
    if( mWidth!=width || mHeight!=height )
205
      {
206
      mWidth = width;
207
      mHeight= height;
208

  
209
      createProjection();
210

  
211
      if( mTextureID>0 ) markForDeletion();
212
      }
213
    }
214

  
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

  
217
  public int getFBO()
218
    {
219
    return mFramebufferID;
220
    }
221

  
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
// set this as Render Target to draw to. Must be called from a thread holding OpenGL context.
224

  
225
  public void setOutput()
226
    {
227
    if( mTextureID==TEXTURE_NOT_CREATED_YET ) createFBO();
228

  
229
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFramebufferID);
230
    }
231

  
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
// set this as Render Target to draw from. Must be called from a thread holding OpenGL context.
234

  
235
  public void setInput()
236
    {
237
    if( mTextureID==TEXTURE_NOT_CREATED_YET ) createFBO();
238

  
239
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
240
    }
241

  
242
  }
src/main/java/org/distorted/library/DistortedFramebufferList.java
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
package org.distorted.library;
21

  
22
import java.util.LinkedList;
23
import java.util.Iterator;
24

  
25
/**
26
 * List of all DistortedRenderTarget objects currently created by the application.
27
 *
28
 * The point: we need to be able ot mark RenderTargets for deletion, and delete all marked
29
 * objects later at a convenient time. Thus we keep all of them in a LinkedList here.
30
 */
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
final class DistortedFramebufferList
35
  {
36
  private static boolean mMarked = false;
37
  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
38

  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

  
41
  static synchronized void add(DistortedFramebuffer drt)
42
    {
43
    mList.add(drt);
44
    }
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  static synchronized void markForDeletion()
49
    {
50
    mMarked = true;
51
    }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// must be called form a thread holding OpenGL Context
55

  
56
  static synchronized void deleteAllMarked()
57
    {
58
    if( mMarked )
59
      {
60
      DistortedFramebuffer tmp;
61
      Iterator<DistortedFramebuffer> iterator = mList.iterator();
62

  
63
      while(iterator.hasNext())
64
        {
65
        tmp = iterator.next();
66

  
67
        if( tmp.mMarked )
68
          {
69
          tmp.release();
70
          iterator.remove();
71
          }
72
        }
73

  
74
      mMarked = false;
75
      }
76
    }
77
  }
src/main/java/org/distorted/library/DistortedNode.java
48 48
    {
49 49
    long ID;
50 50
    int numPointingNodes;
51
    DistortedRenderTarget mDRT;
51
    DistortedFramebuffer mDF;
52 52
    boolean mRendered;
53 53

  
54 54
    NodeData(long id)
55 55
      {
56 56
      ID              = id;
57 57
      numPointingNodes= 1;
58
      mDRT            = null;
58
      mDF             = null;
59 59
      mRendered       = false;
60 60
      }
61 61
    }
......
82 82
  
83 83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 84
  
85
  private void drawRecursive(long currTime, DistortedRenderTarget drt)
85
  private void drawRecursive(long currTime, DistortedFramebuffer df)
86 86
    {
87 87
    if( mNumChildren[0]<=0 )
88 88
      {
......
93 93
      if( mData.mRendered==false )
94 94
        {
95 95
        mData.mRendered = true;
96
        mData.mDRT.setOutput();
96
        mData.mDF.setOutput();
97 97

  
98 98
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
99 99
        GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
......
101 101
        if( mObject.mBitmapSet[0] )
102 102
          {
103 103
          GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mObject.mTextureDataH[0]);
104
          mObject.drawNoEffectsPriv(mData.mDRT);
104
          mObject.drawNoEffectsPriv(mData.mDF);
105 105
          }
106 106
      
107 107
        synchronized(this)
108 108
          {
109 109
          for(int i=0; i<mNumChildren[0]; i++)
110 110
            {
111
            mChildren.get(i).drawRecursive(currTime, mData.mDRT);
111
            mChildren.get(i).drawRecursive(currTime, mData.mDF);
112 112
            }
113 113
          }
114 114
        }
115 115

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

  
176
      if( newList.size()>1 && mData.mDRT==null )
177
        mData.mDRT = new DistortedRenderTarget(mObject.getWidth(), mObject.getHeight());
176
      if( newList.size()>1 && mData.mDF==null )
177
        mData.mDF = new DistortedFramebuffer(mObject.getWidth(), mObject.getHeight());
178 178

  
179 179
      mMapNodeID.put(newList, mData);
180 180
      }
......
197 197
      {
198 198
      tmp = mMapNodeID.get(key);
199 199
          
200
      if( tmp.mDRT != null )
200
      if( tmp.mDF != null )
201 201
        {
202
    	  tmp.mDRT.reset();
202
    	  tmp.mDF.reset();
203 203
        tmp.mRendered  = false;
204 204
        }
205 205
      }
......
445 445
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
446 446
    GLES20.glUniform1i(Distorted.mTextureUniformH, 0);  
447 447

  
448
    DistortedRenderTargetList.deleteAllMarked();
449

  
450 448
    markRecursive();
451
    drawRecursive(currTime,Distorted.mRenderTarget);
449
    drawRecursive(currTime,Distorted.mFramebuffer);
452 450
    }
453 451
 
454 452
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedObject.java
159 159
  
160 160
///////////////////////////////////////////////////////////////////////////////////////////////////
161 161
   
162
  void drawPriv(long currTime, DistortedRenderTarget drt)
162
  void drawPriv(long currTime, DistortedFramebuffer df)
163 163
    {
164
    GLES20.glViewport(0, 0, drt.mWidth, drt.mHeight);
164
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
165 165
      
166 166
    mM.compute(currTime);
167
    mM.send(drt);
167
    mM.send(df);
168 168
      
169 169
    mV.compute(currTime);
170 170
    mV.send();
......
177 177

  
178 178
///////////////////////////////////////////////////////////////////////////////////////////////////
179 179
   
180
  void drawNoEffectsPriv(DistortedRenderTarget drt)
180
  void drawNoEffectsPriv(DistortedFramebuffer df)
181 181
    {
182
    GLES20.glViewport(0, 0, drt.mWidth, drt.mHeight);
183
    mM.sendZero(drt);
182
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
183
    mM.sendZero(df);
184 184
    mV.sendZero();
185 185
    mF.sendZero();
186 186
    mGrid.draw();
......
274 274
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
275 275
    GLES20.glUniform1i(Distorted.mTextureUniformH, 0);
276 276
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
277
      
278
    drawPriv(currTime, Distorted.mRenderTarget);
277

  
278
    DistortedFramebufferList.deleteAllMarked();
279

  
280
    drawPriv(currTime, Distorted.mFramebuffer);
279 281
    }
280 282
 
281 283
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedRenderTarget.java
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
package org.distorted.library;
21

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

  
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
public class DistortedRenderTarget
28
  {
29
  private static final int TEXTURE_FAILED_TO_CREATE = -1;
30
  private static final int TEXTURE_NOT_CREATED_YET  = -2;
31
  private static final int TEXTURE_DONT_CREATE      = -3;
32

  
33
  private int mFramebufferID, mTextureID;
34
  private float mX, mY;
35
  private float mFOV;
36

  
37
  int mWidth,mHeight,mDepth,mDistance;
38
  float[] mProjectionMatrix;
39

  
40
  boolean mMarked;
41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

  
44
  private void createProjection()
45
    {
46
    float ratio  = (float) mWidth / mHeight;
47
    float left   =-ratio;          //
48
    float right  = ratio;          // Create a new perspective projection matrix.
49
    float bottom = -1.0f;          //
50
    float top    =  1.0f;          // any change to those values will have serious consequences!
51
    float near, far;
52

  
53
    if( mFOV>0.0f )  // perspective projection
54
      {
55
      near= (float)(top / Math.tan(mFOV*Math.PI/360));
56
      mDistance = (int)(mHeight*near/(top-bottom));
57
      far = 2*mDistance-near;
58

  
59
      Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
60
      }
61
    else                      // parallel projection
62
      {
63
      near= (float)(top / Math.tan(Math.PI/360));
64
      mDistance = (int)(mHeight*near/(top-bottom));
65
      far = 2*mDistance-near;
66

  
67
      Matrix.orthoM(mProjectionMatrix, 0, -mWidth/2, mWidth/2,-mHeight/2, mHeight/2, near, far);
68
      }
69

  
70
    mDepth = (int)((far-near)/2);
71
    }
72

  
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// must be called form a thread holding OpenGL Context
75

  
76
  private boolean createFBO()
77
    {
78
    int[] textureIds = new int[1];
79
    GLES20.glGenTextures(1, textureIds, 0);
80
    mTextureID = textureIds[0];
81
    int[] mFBORenderToTexture = new int[1];
82

  
83
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
84
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
85
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
86
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
87
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
88
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
89

  
90
    GLES20.glGenFramebuffers(1, mFBORenderToTexture, 0);
91
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFBORenderToTexture[0]);
92
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, mTextureID, 0);
93
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
94

  
95
    if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
96
      {
97
      android.util.Log.e("RenderTarget", "failed to create framebuffer, error="+status);
98

  
99
      GLES20.glDeleteTextures(1, textureIds, 0);
100
      GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
101
      mFramebufferID = 0;
102
      mTextureID = TEXTURE_FAILED_TO_CREATE;
103
      return false;
104
      }
105

  
106
    mFramebufferID = mFBORenderToTexture[0];
107

  
108
    return true;
109
    }
110

  
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
// must be called form a thread holding OpenGL Context
113

  
114
  private void deleteFBO()
115
    {
116
    int[] textureIds = new int[1];
117
    int[] mFBORenderToTexture = new int[1];
118

  
119
    textureIds[0] = mTextureID;
120
    mFBORenderToTexture[0] = mFramebufferID;
121

  
122
    GLES20.glDeleteTextures(1, textureIds, 0);
123
    GLES20.glDeleteFramebuffers(1, mFBORenderToTexture, 0);
124

  
125
    mFramebufferID = 0;
126
    mTextureID = TEXTURE_NOT_CREATED_YET;
127
    }
128

  
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// must be called form a thread holding OpenGL Context
131

  
132
  void release()
133
    {
134
    if( mTextureID>=0 ) deleteFBO();
135

  
136
    mProjectionMatrix = null;
137
    mMarked = false;
138
    }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

  
142
  void reset()
143
    {
144
    mTextureID = TEXTURE_NOT_CREATED_YET;
145
    }
146

  
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
// PUBLIC API
149

  
150
  public DistortedRenderTarget(int width, int height)
151
    {
152
    mProjectionMatrix = new float[16];
153

  
154
    mHeight        = height;
155
    mWidth         = width;
156
    mFramebufferID = 0;
157
    mTextureID     = TEXTURE_NOT_CREATED_YET;
158
    mFOV           = 60.0f;
159
    mX             = 0.0f;
160
    mY             = 0.0f;
161

  
162
    mMarked = false;
163

  
164
    createProjection();
165
    }
166

  
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

  
169
  public DistortedRenderTarget(int fbo)
170
    {
171
    mProjectionMatrix = new float[16];
172

  
173
    mFramebufferID = fbo;
174
    mTextureID     = TEXTURE_DONT_CREATE;
175
    mFOV           = 60.0f;
176
    mX             = 0.0f;
177
    mY             = 0.0f;
178

  
179
    mMarked = false;
180
    }
181

  
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

  
184
  public void markForDeletion()
185
    {
186
    DistortedRenderTargetList.markForDeletion();
187
    mMarked = true;
188
    }
189

  
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

  
192
  public void setProjection(float FOV, float x, float y)
193
    {
194
    mFOV = FOV;
195
    mX   = x;
196
    mY   = y;
197

  
198
    createProjection();
199
    }
200

  
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

  
203
  public void resize(int width, int height)
204
    {
205
    if( mWidth!=width || mHeight!=height )
206
      {
207
      mWidth = width;
208
      mHeight= height;
209

  
210
      createProjection();
211

  
212
      if( mTextureID>0 ) markForDeletion();
213
      }
214
    }
215

  
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

  
218
  public int getFBO()
219
    {
220
    return mFramebufferID;
221
    }
222

  
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
// set this as Render Target to draw to. Must be called from a thread holding OpenGL context.
225

  
226
  public void setOutput()
227
    {
228
    if( mTextureID==TEXTURE_NOT_CREATED_YET ) createFBO();
229

  
230
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFramebufferID);
231
    }
232

  
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// set this as Render Target to draw from. Must be called from a thread holding OpenGL context.
235

  
236
  public void setInput()
237
    {
238
    if( mTextureID==TEXTURE_NOT_CREATED_YET ) createFBO();
239

  
240
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);
241
    }
242

  
243
  }
src/main/java/org/distorted/library/DistortedRenderTargetList.java
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
package org.distorted.library;
21

  
22
import java.util.LinkedList;
23
import java.util.Iterator;
24

  
25
/**
26
 * List of all DistortedRenderTarget objects currently created by the application.
27
 *
28
 * The point: we need to be able ot mark RenderTargets for deletion, and delete all marked
29
 * objects later at a convenient time. Thus we keep all of them in a LinkedList here.
30
 */
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
final class DistortedRenderTargetList
35
  {
36
  private static boolean mMarked = false;
37
  private static LinkedList<DistortedRenderTarget> mList = new LinkedList<>();
38

  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

  
41
  static synchronized void add(DistortedRenderTarget drt)
42
    {
43
    mList.add(drt);
44
    }
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
48
  static synchronized void markForDeletion()
49
    {
50
    mMarked = true;
51
    }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// must be called form a thread holding OpenGL Context
55

  
56
  static synchronized void deleteAllMarked()
57
    {
58
    if( mMarked )
59
      {
60
      DistortedRenderTarget tmp;
61
      Iterator<DistortedRenderTarget> iterator = mList.iterator();
62

  
63
      while(iterator.hasNext())
64
        {
65
        tmp = iterator.next();
66

  
67
        if( tmp.mMarked )
68
          {
69
          tmp.release();
70
          iterator.remove();
71
          }
72
        }
73

  
74
      mMarked = false;
75
      }
76
    }
77
  }
src/main/java/org/distorted/library/EffectQueueMatrix.java
153 153
///////////////////////////////////////////////////////////////////////////////////////////////////
154 154
// here construct the ModelView Matrix
155 155

  
156
  synchronized void send(DistortedRenderTarget drt)
156
  synchronized void send(DistortedFramebuffer df)
157 157
    {
158 158
    Matrix.setIdentityM(mViewMatrix, 0);
159
    Matrix.translateM(mViewMatrix, 0, -drt.mWidth/2, drt.mHeight/2, -drt.mDistance);
159
    Matrix.translateM(mViewMatrix, 0, -df.mWidth/2, df.mHeight/2, -df.mDistance);
160 160
    
161 161
    float x,y,z, sx,sy,sz;
162 162
   
......
230 230
      }
231 231
   
232 232
    Matrix.translateM(mViewMatrix, 0, mObjHalfX,-mObjHalfY, 0);
233
    Matrix.multiplyMM(mMVPMatrix, 0, drt.mProjectionMatrix, 0, mViewMatrix, 0);
233
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mViewMatrix, 0);
234 234
    
235 235
    GLES20.glUniform3f( mObjDH , mObjHalfX, mObjHalfY, mObjHalfZ);
236
    GLES20.glUniform1f( mDepthH, drt.mDepth);
236
    GLES20.glUniform1f( mDepthH, df.mDepth);
237 237
    GLES20.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
238 238
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
239 239
    }
......
241 241
///////////////////////////////////////////////////////////////////////////////////////////////////
242 242
// here construct the ModelView Matrix, but without any effects
243 243

  
244
  synchronized void sendZero(DistortedRenderTarget drt)
244
  synchronized void sendZero(DistortedFramebuffer df)
245 245
    {
246 246
    Matrix.setIdentityM(mTmpMatrix, 0);
247
    Matrix.translateM(mTmpMatrix, 0, mObjHalfX-drt.mWidth/2, drt.mHeight/2-mObjHalfY, -drt.mDistance);
248
    Matrix.multiplyMM(mMVPMatrix, 0, drt.mProjectionMatrix, 0, mTmpMatrix, 0);
247
    Matrix.translateM(mTmpMatrix, 0, mObjHalfX-df.mWidth/2, df.mHeight/2-mObjHalfY, -df.mDistance);
248
    Matrix.multiplyMM(mMVPMatrix, 0, df.mProjectionMatrix, 0, mTmpMatrix, 0);
249 249
    
250 250
    GLES20.glUniform3f( mObjDH , mObjHalfX, mObjHalfY, mObjHalfZ);
251
    GLES20.glUniform1f( mDepthH, drt.mDepth);
251
    GLES20.glUniform1f( mDepthH, df.mDepth);
252 252
    GLES20.glUniformMatrix4fv(mMVMatrixH , 1, false, mTmpMatrix, 0);
253 253
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
254 254
    }

Also available in: Unified diff