Project

General

Profile

« Previous | Next » 

Revision 09d4f4b1

Added by Leszek Koltunski about 8 years ago

Change of API in DistortedFramebuffer.

View differences:

src/main/java/org/distorted/library/DistortedFramebuffer.java
59 59
  float mDistance;
60 60
  float[] mProjectionMatrix;
61 61

  
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

  
64
  private void createProjection()
65
    {
66
    if( mWidth>0 && mHeight>1 )
67
      {
68
      if( mFOV>0.0f )  // perspective projection
69
        {
70
        float left   = (-mX-mWidth /2.0f)/mHeight;
71
        float right  = (-mX+mWidth /2.0f)/mHeight;
72
        float bottom = (-mY-mHeight/2.0f)/mHeight;
73
        float top    = (-mY+mHeight/2.0f)/mHeight;
74
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
75
        mDistance    = mHeight*near/(top-bottom);
76
        float far    = 2*mDistance-near;
77
        mDepth       = (int)((far-near)/2);
78

  
79
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
80
        }
81
      else             // parallel projection
82
        {
83
        float left   = -mX-mWidth /2.0f;
84
        float right  = -mX+mWidth /2.0f;
85
        float bottom = -mY-mHeight/2.0f;
86
        float top    = -mY+mHeight/2.0f;
87
        float near   = (mWidth+mHeight)/2;
88
        mDistance    = 2*near;
89
        float far    = 3*near;
90
        mDepth       = (int)near;
91

  
92
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
93
        }
94
      }
95
    }
96

  
97 62
///////////////////////////////////////////////////////////////////////////////////////////////////
98 63
// Must be called from a thread holding OpenGL Context
99 64

  
......
148 113
    mMarked = false;
149 114
    }
150 115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// Must be called from a thread holding OpenGL Context
118

  
119
  void setAsOutput()
120
    {
121
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
122

  
123
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
124
    }
125

  
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
// Must be called from a thread holding OpenGL Context
128

  
129
  void setAsInput()
130
    {
131
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
132

  
133
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
134
    }
135

  
151 136
///////////////////////////////////////////////////////////////////////////////////////////////////
152 137

  
153 138
  void reset()
......
156 141
      texIds[0] = TEXTURE_NOT_CREATED_YET;
157 142
    }
158 143

  
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

  
146
  private void createProjection()
147
    {
148
    if( mWidth>0 && mHeight>1 )
149
      {
150
      if( mFOV>0.0f )  // perspective projection
151
        {
152
        float left   = (-mX-mWidth /2.0f)/mHeight;
153
        float right  = (-mX+mWidth /2.0f)/mHeight;
154
        float bottom = (-mY-mHeight/2.0f)/mHeight;
155
        float top    = (-mY+mHeight/2.0f)/mHeight;
156
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
157
        mDistance    = mHeight*near/(top-bottom);
158
        float far    = 2*mDistance-near;
159
        mDepth       = (int)((far-near)/2);
160

  
161
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
162
        }
163
      else             // parallel projection
164
        {
165
        float left   = -mX-mWidth /2.0f;
166
        float right  = -mX+mWidth /2.0f;
167
        float bottom = -mY-mHeight/2.0f;
168
        float top    = -mY+mHeight/2.0f;
169
        float near   = (mWidth+mHeight)/2;
170
        mDistance    = 2*near;
171
        float far    = 3*near;
172
        mDepth       = (int)near;
173

  
174
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
175
        }
176
      }
177
    }
178

  
159 179
///////////////////////////////////////////////////////////////////////////////////////////////////
160 180

  
161 181
  static synchronized void release()
......
293 313

  
294 314
///////////////////////////////////////////////////////////////////////////////////////////////////
295 315
/**
296
 *  Set this as the Framebuffer to write to.
297
 *  <p>
298
 *  Must be called from a thread holding OpenGL Context
299
 */
300
  public void setAsOutput()
301
    {
302
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
303

  
304
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
305
    }
306

  
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308
/**
309
 *  Set this as the Framebuffer to read from.
310
 *  <p>
311
 *  Must be called from a thread holding OpenGL Context
316
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
317
 * <p>
318
 * Catch: this will only work if the library has had time to actually create the texture. Remember
319
 * that the texture gets created only on first render, thus creating a Texture object and immediately
320
 * calling this method will return an invalid (negative) result.
321
 *
322
 * @returns If there was not a single render between creation of the Object and calling this method on
323
 *          it, return a negative value. Otherwise, return ID of COLOR attachment 0.
312 324
 */
313
  public void setAsInput()
325
  public int getTextureID()
314 326
    {
315
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
316

  
317
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
327
    return texIds[0];
318 328
    }
319

  
320 329
  }

Also available in: Unified diff