Revision 57578636
Added by Leszek Koltunski about 8 years ago
src/main/java/org/distorted/library/Distorted.java | ||
---|---|---|
85 | 85 |
private static final String TAG = Distorted.class.getSimpleName(); |
86 | 86 |
private static boolean mInitialized = false; |
87 | 87 |
|
88 |
static int mPositionH; // pass in model position information |
|
89 |
static int mTextureUniformH; // pass in the texture. |
|
90 |
static int mNormalH; // pass in model normal information. |
|
91 |
static int mTextureCoordH; // pass in model texture coordinate information. |
|
92 |
static int mProgramH; // This is a handle to our shading program. |
|
88 |
private static int mProgramH; // handle to our shading program. |
|
89 |
private static int mTextureUniformH; // the texture. |
|
90 |
|
|
91 |
static int mPositionH; // model position information |
|
92 |
static int mNormalH; // model normal information. |
|
93 |
static int mTextureCoordH; // model texture coordinate information. |
|
93 | 94 |
|
94 | 95 |
static DistortedFramebuffer mFramebuffer = new DistortedFramebuffer(0); // output to the screen |
95 | 96 |
|
... | ... | |
325 | 326 |
final int fragmentShaderHandle = compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader); |
326 | 327 |
|
327 | 328 |
mProgramH = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, new String[] {"a_Position", "a_Color", "a_Normal", "a_TexCoordinate"}); |
328 |
|
|
329 |
|
|
330 |
mFramebuffer.setProjection(60.0f,0.0f,0.0f); |
|
331 |
|
|
329 | 332 |
GLES20.glUseProgram(mProgramH); |
333 |
|
|
334 |
mTextureUniformH = GLES20.glGetUniformLocation(mProgramH, "u_Texture"); |
|
335 |
mPositionH = GLES20.glGetAttribLocation( mProgramH, "a_Position"); |
|
336 |
mNormalH = GLES20.glGetAttribLocation( mProgramH, "a_Normal"); |
|
337 |
mTextureCoordH = GLES20.glGetAttribLocation( mProgramH, "a_TexCoordinate"); |
|
338 |
|
|
330 | 339 |
GLES20.glEnable (GLES20.GL_DEPTH_TEST); |
331 | 340 |
GLES20.glDepthFunc(GLES20.GL_LEQUAL); |
332 | 341 |
GLES20.glEnable(GLES20.GL_BLEND); |
... | ... | |
334 | 343 |
GLES20.glEnable(GLES20.GL_CULL_FACE); |
335 | 344 |
GLES20.glCullFace(GLES20.GL_BACK); |
336 | 345 |
GLES20.glFrontFace(GLES20.GL_CW); |
346 |
GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
|
347 |
GLES20.glUniform1i(mTextureUniformH, 0); |
|
337 | 348 |
|
338 |
mTextureUniformH = GLES20.glGetUniformLocation(mProgramH, "u_Texture"); |
|
339 |
|
|
340 |
mPositionH = GLES20.glGetAttribLocation( mProgramH, "a_Position"); |
|
341 |
mNormalH = GLES20.glGetAttribLocation( mProgramH, "a_Normal"); |
|
342 |
mTextureCoordH = GLES20.glGetAttribLocation( mProgramH, "a_TexCoordinate"); |
|
343 |
|
|
344 | 349 |
EffectQueueFragment.getUniforms(mProgramH); |
345 | 350 |
EffectQueueVertex.getUniforms(mProgramH); |
346 | 351 |
EffectQueueMatrix.getUniforms(mProgramH); |
src/main/java/org/distorted/library/DistortedFramebuffer.java | ||
---|---|---|
67 | 67 |
{ |
68 | 68 |
if( mFOV>0.0f ) // perspective projection |
69 | 69 |
{ |
70 |
float left =(-mX-mWidth/2 )/mHeight; |
|
71 |
float right =(-mX+mWidth/2 )/mHeight; |
|
72 |
float bottom =(-mY-mHeight/2)/mHeight; |
|
73 |
float top =(-mY+mHeight/2)/mHeight; |
|
74 |
float near= (float)( (top-bottom) / (2*Math.tan(mFOV*Math.PI/360)) ); |
|
75 |
mDistance = (int)(mHeight*near/(top-bottom)); |
|
76 |
float far = 2*mDistance-near; |
|
77 |
mDepth = (int)((far-near)/2); |
|
70 |
float left = (-mX-mWidth/2 )/mHeight;
|
|
71 |
float right = (-mX+mWidth/2 )/mHeight;
|
|
72 |
float bottom = (-mY-mHeight/2)/mHeight;
|
|
73 |
float top = (-mY+mHeight/2)/mHeight;
|
|
74 |
float near = (float)( (top-bottom) / (2*Math.tan(mFOV*Math.PI/360)) );
|
|
75 |
mDistance = (int)(mHeight*near/(top-bottom));
|
|
76 |
float far = 2*mDistance-near;
|
|
77 |
mDepth = (int)((far-near)/2);
|
|
78 | 78 |
|
79 | 79 |
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); |
80 | 80 |
} |
81 | 81 |
else // parallel projection |
82 | 82 |
{ |
83 |
float left =-mX-mWidth/2; |
|
84 |
float right =-mX+mWidth/2; |
|
85 |
float bottom =-mY-mHeight/2; |
|
86 |
float top =-mY+mHeight/2; |
|
87 |
float near= (float)( (top-bottom) / (2*Math.tan(Math.PI/360)) ); |
|
88 |
mDistance = (int)(mHeight*near/(top-bottom)); |
|
89 |
float far = 2*mDistance-near; |
|
90 |
mDepth = (int)((far-near)/2); |
|
83 |
float left = -mX-mWidth/2;
|
|
84 |
float right = -mX+mWidth/2;
|
|
85 |
float bottom = -mY-mHeight/2;
|
|
86 |
float top = -mY+mHeight/2;
|
|
87 |
float near = (float)( (top-bottom) / (2*Math.tan(Math.PI/360)) );
|
|
88 |
mDistance = (int)(mHeight*near/(top-bottom));
|
|
89 |
float far = 2*mDistance-near;
|
|
90 |
mDepth = (int)((far-near)/2);
|
|
91 | 91 |
|
92 | 92 |
Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far); |
93 | 93 |
} |
... | ... | |
193 | 193 |
mListMarked = false; |
194 | 194 |
} |
195 | 195 |
} |
196 |
|
|
197 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
198 |
// Set this as the Framebuffer to write to. |
|
199 |
|
|
200 |
void setAsOutput() |
|
201 |
{ |
|
202 |
if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO(); |
|
203 |
|
|
204 |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]); |
|
205 |
} |
|
206 |
|
|
207 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
208 |
// Set this as the Framebuffer to read from. |
|
209 |
|
|
210 |
void setAsInput() |
|
211 |
{ |
|
212 |
if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO(); |
|
213 |
|
|
214 |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]); |
|
215 |
} |
|
216 |
|
|
196 | 217 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
197 | 218 |
// PUBLIC API |
198 | 219 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
199 | 220 |
/** |
200 |
* Create a new offscreen FBO.
|
|
221 |
* Create a new offscreen Framebuffer.
|
|
201 | 222 |
* |
202 | 223 |
* @param width Width of the COLOR attachment. |
203 | 224 |
* @param height Height of the COLOR attachment. |
... | ... | |
206 | 227 |
{ |
207 | 228 |
mProjectionMatrix = new float[16]; |
208 | 229 |
|
209 |
mHeight = height; |
|
210 |
mWidth = width; |
|
211 |
fboIds[0] = -1; |
|
212 |
texIds[0] = TEXTURE_NOT_CREATED_YET; |
|
213 |
mFOV = 60.0f; |
|
214 |
mX = 0.0f; |
|
215 |
mY = 0.0f; |
|
216 |
|
|
217 |
mMarked = false; |
|
230 |
mHeight = height; |
|
231 |
mWidth = width; |
|
232 |
fboIds[0]= -1; |
|
233 |
texIds[0]= TEXTURE_NOT_CREATED_YET; |
|
234 |
mFOV = 60.0f; |
|
235 |
mX = 0.0f; |
|
236 |
mY = 0.0f; |
|
237 |
mMarked = false; |
|
218 | 238 |
|
219 | 239 |
createProjection(); |
220 | 240 |
} |
221 | 241 |
|
222 | 242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
223 | 243 |
/** |
224 |
* Create a new DistortedFramebuffer from an already created OpenGL Framebuffer. |
|
244 |
* Create a new Framebuffer from an already created OpenGL Framebuffer. |
|
245 |
* <p> |
|
246 |
* Has to be followed by a 'resize()' to set the size. |
|
225 | 247 |
* |
226 | 248 |
* @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen) |
227 | 249 |
*/ |
... | ... | |
229 | 251 |
{ |
230 | 252 |
mProjectionMatrix = new float[16]; |
231 | 253 |
|
232 |
fboIds[0] = fbo; |
|
233 |
texIds[0] = TEXTURE_DONT_CREATE; |
|
234 |
mFOV = 60.0f; |
|
235 |
mX = 0.0f; |
|
236 |
mY = 0.0f; |
|
237 |
|
|
238 |
mMarked = false; |
|
254 |
fboIds[0]= fbo; |
|
255 |
texIds[0]= TEXTURE_DONT_CREATE; |
|
256 |
mFOV = 60.0f; |
|
257 |
mX = 0.0f; |
|
258 |
mY = 0.0f; |
|
259 |
mMarked = false; |
|
239 | 260 |
} |
240 | 261 |
|
241 | 262 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
292 | 313 |
if( texIds[0]>0 ) markForDeletion(); |
293 | 314 |
} |
294 | 315 |
} |
295 |
|
|
296 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
297 |
/** |
|
298 |
* Set this as the Framebuffer to write to. |
|
299 |
*/ |
|
300 |
|
|
301 |
public void setAsOutput() |
|
302 |
{ |
|
303 |
if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO(); |
|
304 |
|
|
305 |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]); |
|
306 |
} |
|
307 |
|
|
308 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
309 |
/** |
|
310 |
* Set this as the Framebuffer to read from. |
|
311 |
*/ |
|
312 |
|
|
313 |
public void setAsInput() |
|
314 |
{ |
|
315 |
if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO(); |
|
316 |
|
|
317 |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]); |
|
318 |
} |
|
319 |
|
|
320 | 316 |
} |
src/main/java/org/distorted/library/DistortedNode.java | ||
---|---|---|
458 | 458 |
{ |
459 | 459 |
GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
460 | 460 |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
461 |
GLES20.glUniform1i(Distorted.mTextureUniformH, 0); |
|
462 | 461 |
|
463 | 462 |
markRecursive(); |
464 | 463 |
drawRecursive(currTime,Distorted.mFramebuffer); |
src/main/java/org/distorted/library/DistortedObject.java | ||
---|---|---|
326 | 326 |
*/ |
327 | 327 |
public void draw(long currTime) |
328 | 328 |
{ |
329 |
GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
|
330 | 329 |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0); |
331 |
GLES20.glUniform1i(Distorted.mTextureUniformH, 0); |
|
330 |
|
|
332 | 331 |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]); |
333 | 332 |
|
334 | 333 |
drawPriv(currTime, Distorted.mFramebuffer); |
Also available in: Unified diff
Improvements for DFramebuffer.