Revision 133cbb2b
Added by Leszek Koltunski almost 8 years ago
src/main/java/org/distorted/library/DistortedFramebuffer.java | ||
---|---|---|
39 | 39 |
* framework where one is able to mark for deletion at any time and actual deletion takes place |
40 | 40 |
* on the next render). |
41 | 41 |
*/ |
42 |
public class DistortedFramebuffer |
|
42 |
public class DistortedFramebuffer extends DistortedRenderable
|
|
43 | 43 |
{ |
44 |
private static final int FAILED_TO_CREATE = -1; |
|
45 |
private static final int NOT_CREATED_YET = -2; |
|
46 |
private static final int DONT_CREATE = -3; |
|
47 |
|
|
48 | 44 |
private static boolean mListMarked = false; |
49 | 45 |
private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>(); |
50 | 46 |
|
51 |
private int[] colorIds = new int[1]; |
|
52 |
private int[] depthIds = new int[1]; |
|
53 |
private int[] fboIds = new int[1]; |
|
47 |
private int[] mDepthH = new int[1]; |
|
48 |
private int[] mFBOH = new int[1]; |
|
54 | 49 |
|
55 | 50 |
private boolean mMarked; |
56 | 51 |
private boolean mDepthEnabled; |
57 | 52 |
|
58 |
private int mTexWidth, mTexHeight; |
|
59 |
|
|
60 | 53 |
// Projection stuff |
61 | 54 |
private float mX, mY, mFOV; |
62 | 55 |
int mWidth,mHeight,mDepth; |
... | ... | |
66 | 59 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
67 | 60 |
// Must be called from a thread holding OpenGL Context |
68 | 61 |
|
69 |
void createFBO()
|
|
62 |
void create() |
|
70 | 63 |
{ |
71 |
if( colorIds[0]==NOT_CREATED_YET )
|
|
64 |
if( mColorH[0]==NOT_CREATED_YET )
|
|
72 | 65 |
{ |
73 |
GLES30.glGenTextures(1, colorIds, 0);
|
|
74 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, colorIds[0]);
|
|
66 |
GLES30.glGenTextures(1, mColorH, 0);
|
|
67 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
|
75 | 68 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT); |
76 | 69 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT); |
77 | 70 |
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST); |
78 | 71 |
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR); |
79 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mTexWidth, mTexHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
|
|
72 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
|
|
80 | 73 |
|
81 |
GLES30.glGenFramebuffers(1, fboIds, 0);
|
|
82 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, fboIds[0]);
|
|
83 |
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, colorIds[0], 0);
|
|
74 |
GLES30.glGenFramebuffers(1, mFBOH, 0);
|
|
75 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
|
76 |
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
|
|
84 | 77 |
|
85 | 78 |
checkStatus("color"); |
86 | 79 |
} |
87 |
if( mDepthEnabled && depthIds[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
|
|
80 |
if( mDepthEnabled && mDepthH[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
|
|
88 | 81 |
{ |
89 |
GLES30.glGenTextures(1, depthIds, 0);
|
|
90 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, depthIds[0]);
|
|
82 |
GLES30.glGenTextures(1, mDepthH, 0);
|
|
83 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
|
|
91 | 84 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT); |
92 | 85 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT); |
93 | 86 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST); |
94 | 87 |
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST); |
95 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mTexWidth, mTexHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
|
|
88 |
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
|
|
96 | 89 |
|
97 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, fboIds[0]);
|
|
98 |
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, depthIds[0], 0);
|
|
90 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
|
91 |
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
|
|
99 | 92 |
|
100 | 93 |
checkStatus("depth"); |
101 | 94 |
} |
102 |
if( !mDepthEnabled && depthIds[0]!=NOT_CREATED_YET ) // we need to detach and destroy the DEPTH attachment.
|
|
95 |
if( !mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET ) // we need to detach and destroy the DEPTH attachment.
|
|
103 | 96 |
{ |
104 |
GLES30.glDeleteTextures(1, depthIds, 0);
|
|
105 |
depthIds[0]=NOT_CREATED_YET;
|
|
97 |
GLES30.glDeleteTextures(1, mDepthH, 0);
|
|
98 |
mDepthH[0]=NOT_CREATED_YET;
|
|
106 | 99 |
} |
107 | 100 |
} |
108 | 101 |
|
... | ... | |
116 | 109 |
{ |
117 | 110 |
android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status); |
118 | 111 |
|
119 |
GLES30.glDeleteTextures(1, colorIds, 0);
|
|
120 |
GLES30.glDeleteTextures(1, depthIds, 0);
|
|
121 |
GLES30.glDeleteFramebuffers(1, fboIds, 0);
|
|
122 |
fboIds[0] = 0;
|
|
123 |
colorIds[0] = FAILED_TO_CREATE;
|
|
124 |
depthIds[0] = FAILED_TO_CREATE;
|
|
112 |
GLES30.glDeleteTextures(1, mColorH, 0);
|
|
113 |
GLES30.glDeleteTextures(1, mDepthH, 0);
|
|
114 |
GLES30.glDeleteFramebuffers(1, mFBOH, 0);
|
|
115 |
mFBOH[0] = 0;
|
|
116 |
mColorH[0] = FAILED_TO_CREATE;
|
|
117 |
mDepthH[0] = FAILED_TO_CREATE;
|
|
125 | 118 |
|
126 | 119 |
return false; |
127 | 120 |
} |
... | ... | |
132 | 125 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
133 | 126 |
// Must be called from a thread holding OpenGL Context |
134 | 127 |
|
135 |
private void deleteFBO()
|
|
128 |
private void delete() |
|
136 | 129 |
{ |
137 |
if( colorIds[0]>=0 )
|
|
130 |
if( mColorH[0]>=0 )
|
|
138 | 131 |
{ |
139 |
//android.util.Log.e("FBO", "deleting ("+mWidth+","+mHeight+") "+fboIds[0]); |
|
140 |
|
|
141 |
if( depthIds[0]>=0 ) |
|
132 |
if( mDepthH[0]>=0 ) |
|
142 | 133 |
{ |
143 |
GLES30.glDeleteTextures(1, depthIds, 0);
|
|
144 |
depthIds[0]=NOT_CREATED_YET;
|
|
134 |
GLES30.glDeleteTextures(1, mDepthH, 0);
|
|
135 |
mDepthH[0]=NOT_CREATED_YET;
|
|
145 | 136 |
} |
146 | 137 |
|
147 |
GLES30.glDeleteTextures(1, colorIds, 0);
|
|
148 |
colorIds[0] = NOT_CREATED_YET;
|
|
138 |
GLES30.glDeleteTextures(1, mColorH, 0);
|
|
139 |
mColorH[0] = NOT_CREATED_YET;
|
|
149 | 140 |
|
150 |
GLES30.glDeleteFramebuffers(1, fboIds, 0);
|
|
151 |
fboIds[0] = 0;
|
|
141 |
GLES30.glDeleteFramebuffers(1, mFBOH, 0);
|
|
142 |
mFBOH[0] = 0;
|
|
152 | 143 |
} |
153 | 144 |
|
154 | 145 |
mMarked = false; |
... | ... | |
158 | 149 |
|
159 | 150 |
void setAsOutput() |
160 | 151 |
{ |
161 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, fboIds[0]);
|
|
152 |
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
|
162 | 153 |
|
163 |
if( depthIds[0]!=NOT_CREATED_YET )
|
|
154 |
if( mDepthH[0]!=NOT_CREATED_YET )
|
|
164 | 155 |
{ |
165 | 156 |
GLES30.glEnable(GLES30.GL_DEPTH_TEST); |
166 | 157 |
GLES30.glDepthMask(true); |
... | ... | |
172 | 163 |
} |
173 | 164 |
} |
174 | 165 |
|
175 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
176 |
|
|
177 |
void setAsInput() |
|
178 |
{ |
|
179 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, colorIds[0]); |
|
180 |
} |
|
181 |
|
|
182 | 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
183 | 167 |
|
184 | 168 |
private void createProjection() |
... | ... | |
220 | 204 |
{ |
221 | 205 |
for( DistortedFramebuffer fbo : mList) |
222 | 206 |
{ |
223 |
if( fbo.colorIds[0]!=DONT_CREATE ) fbo.colorIds[0] = NOT_CREATED_YET;
|
|
224 |
if( fbo.mDepthEnabled) fbo.depthIds[0] = NOT_CREATED_YET;
|
|
207 |
if( fbo.mColorH[0]!=DONT_CREATE ) fbo.mColorH[0] = NOT_CREATED_YET;
|
|
208 |
if( fbo.mDepthEnabled ) fbo.mDepthH[0] = NOT_CREATED_YET;
|
|
225 | 209 |
} |
226 | 210 |
} |
227 | 211 |
|
... | ... | |
241 | 225 |
|
242 | 226 |
if( tmp.mMarked ) |
243 | 227 |
{ |
244 |
tmp.deleteFBO();
|
|
228 |
tmp.delete(); |
|
245 | 229 |
iterator.remove(); |
246 | 230 |
} |
247 | 231 |
} |
... | ... | |
265 | 249 |
|
266 | 250 |
createProjection(); |
267 | 251 |
|
268 |
if( mWidth>mTexWidth || mHeight>mTexHeight )
|
|
252 |
if( mWidth> mSizeX || mHeight> mSizeY)
|
|
269 | 253 |
{ |
270 |
mTexWidth = mWidth;
|
|
271 |
mTexHeight= mHeight;
|
|
272 |
deleteFBO();
|
|
254 |
mSizeX = mWidth;
|
|
255 |
mSizeY = mHeight;
|
|
256 |
delete(); |
|
273 | 257 |
} |
274 | 258 |
} |
275 | 259 |
|
276 |
createFBO();
|
|
260 |
create(); |
|
277 | 261 |
} |
278 | 262 |
|
279 | 263 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
293 | 277 |
|
294 | 278 |
mHeight = height; |
295 | 279 |
mWidth = width; |
296 |
mTexHeight = height;
|
|
297 |
mTexWidth = width;
|
|
280 |
mSizeY = height;
|
|
281 |
mSizeX = width;
|
|
298 | 282 |
mFOV = 60.0f; |
299 | 283 |
mX = 0.0f; |
300 | 284 |
mY = 0.0f; |
301 | 285 |
mMarked = false; |
302 | 286 |
mDepthEnabled= depthEnabled; |
303 | 287 |
|
304 |
fboIds[0] =-1;
|
|
305 |
colorIds[0]= NOT_CREATED_YET;
|
|
306 |
depthIds[0]= NOT_CREATED_YET;
|
|
288 |
mFBOH[0] =-1;
|
|
289 |
mColorH[0]= NOT_CREATED_YET;
|
|
290 |
mDepthH[0]= NOT_CREATED_YET;
|
|
307 | 291 |
|
308 | 292 |
createProjection(); |
309 | 293 |
|
310 | 294 |
mList.add(this); |
311 | 295 |
} |
312 | 296 |
|
297 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
298 |
|
|
313 | 299 |
/** |
314 | 300 |
* Create a new offscreen Framebuffer. |
315 | 301 |
* |
... | ... | |
323 | 309 |
|
324 | 310 |
mHeight = height; |
325 | 311 |
mWidth = width; |
326 |
mTexHeight = height;
|
|
327 |
mTexWidth = width;
|
|
312 |
mSizeY = height;
|
|
313 |
mSizeX = width;
|
|
328 | 314 |
mFOV = 60.0f; |
329 | 315 |
mX = 0.0f; |
330 | 316 |
mY = 0.0f; |
331 | 317 |
mMarked = false; |
332 | 318 |
mDepthEnabled= false; |
333 | 319 |
|
334 |
fboIds[0] =-1;
|
|
335 |
colorIds[0]= NOT_CREATED_YET;
|
|
336 |
depthIds[0]= NOT_CREATED_YET;
|
|
320 |
mFBOH[0] =-1;
|
|
321 |
mColorH[0]= NOT_CREATED_YET;
|
|
322 |
mDepthH[0]= NOT_CREATED_YET;
|
|
337 | 323 |
|
338 | 324 |
createProjection(); |
339 | 325 |
|
... | ... | |
358 | 344 |
mMarked = false; |
359 | 345 |
mDepthEnabled= true; |
360 | 346 |
|
361 |
fboIds[0] = fbo;
|
|
362 |
colorIds[0]= DONT_CREATE;
|
|
363 |
depthIds[0]= DONT_CREATE;
|
|
347 |
mFBOH[0] = fbo;
|
|
348 |
mColorH[0]= DONT_CREATE;
|
|
349 |
mDepthH[0]= DONT_CREATE;
|
|
364 | 350 |
} |
365 | 351 |
|
366 | 352 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
389 | 375 |
*/ |
390 | 376 |
public void renderTo(DistortedTexture tex, MeshObject mesh, DistortedEffects effects, long time) |
391 | 377 |
{ |
392 |
tex.createTexture(); |
|
393 |
DistortedFramebuffer.deleteAllMarked(); |
|
394 |
DistortedTexture.deleteAllMarked(); |
|
395 |
createFBO(); |
|
396 |
tex.setAsInput(); |
|
397 |
effects.drawPriv(tex.mHalfX, tex.mHalfY, mesh, this, time); |
|
378 |
tex.create(); |
|
379 |
|
|
380 |
if( tex.setAsInput() ) |
|
381 |
{ |
|
382 |
DistortedFramebuffer.deleteAllMarked(); |
|
383 |
DistortedTexture.deleteAllMarked(); |
|
384 |
create(); |
|
385 |
effects.drawPriv(tex.getWidth()/2.0f, tex.getHeight()/2.0f, mesh, this, time); |
|
386 |
} |
|
398 | 387 |
} |
399 | 388 |
|
400 | 389 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
412 | 401 |
*/ |
413 | 402 |
public void renderTo(DistortedFramebuffer fbo, MeshObject mesh, DistortedEffects effects, long time) |
414 | 403 |
{ |
415 |
fbo.createFBO();
|
|
404 |
fbo.create(); |
|
416 | 405 |
|
417 |
if( fbo.colorIds[0]>0 ) // fbo created with the first constructor
|
|
406 |
if( fbo.setAsInput() )
|
|
418 | 407 |
{ |
419 | 408 |
DistortedFramebuffer.deleteAllMarked(); |
420 | 409 |
DistortedTexture.deleteAllMarked(); |
421 |
createFBO(); |
|
422 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, fbo.colorIds[0]); |
|
423 |
effects.drawPriv(fbo.mWidth/2, fbo.mHeight/2, mesh, this, time); |
|
410 |
create(); |
|
411 |
effects.drawPriv(fbo.getWidth()/2.0f, fbo.getHeight()/2.0f, mesh, this, time); |
|
424 | 412 |
} |
425 | 413 |
} |
426 | 414 |
|
... | ... | |
437 | 425 |
{ |
438 | 426 |
DistortedFramebuffer.deleteAllMarked(); |
439 | 427 |
DistortedTexture.deleteAllMarked(); |
440 |
createFBO();
|
|
428 |
create(); |
|
441 | 429 |
dt.drawRecursive(time,this); |
442 | 430 |
} |
443 | 431 |
|
... | ... | |
447 | 435 |
*/ |
448 | 436 |
public void markForDeletion() |
449 | 437 |
{ |
450 |
//android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
|
|
438 |
//android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+mFBOH[0]);
|
|
451 | 439 |
|
452 | 440 |
mListMarked = true; |
453 | 441 |
mMarked = true; |
... | ... | |
490 | 478 |
{ |
491 | 479 |
mWidth = width; |
492 | 480 |
mHeight = height; |
493 |
mTexWidth = width;
|
|
494 |
mTexHeight= height;
|
|
481 |
mSizeX = width;
|
|
482 |
mSizeY = height;
|
|
495 | 483 |
|
496 | 484 |
createProjection(); |
497 | 485 |
|
498 |
if( colorIds[0]>0 ) markForDeletion();
|
|
486 |
if( mColorH[0]>0 ) markForDeletion();
|
|
499 | 487 |
} |
500 | 488 |
} |
501 | 489 |
|
... | ... | |
512 | 500 |
*/ |
513 | 501 |
public int getTextureID() |
514 | 502 |
{ |
515 |
return colorIds[0];
|
|
503 |
return mColorH[0];
|
|
516 | 504 |
} |
517 | 505 |
|
518 | 506 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/DistortedRenderable.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.GLES30; |
|
23 |
|
|
24 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
25 |
|
|
26 |
abstract class DistortedRenderable |
|
27 |
{ |
|
28 |
static final int FAILED_TO_CREATE = -1; |
|
29 |
static final int NOT_CREATED_YET = -2; |
|
30 |
static final int DONT_CREATE = -3; |
|
31 |
|
|
32 |
int[] mColorH = new int[1]; |
|
33 |
int mSizeX, mSizeY; // in screen space |
|
34 |
|
|
35 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
36 |
|
|
37 |
long getID() |
|
38 |
{ |
|
39 |
return mColorH[0]; |
|
40 |
} |
|
41 |
|
|
42 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
43 |
|
|
44 |
boolean setAsInput() |
|
45 |
{ |
|
46 |
if( mColorH[0]>0 ) |
|
47 |
{ |
|
48 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]); |
|
49 |
return true; |
|
50 |
} |
|
51 |
|
|
52 |
return false; |
|
53 |
} |
|
54 |
|
|
55 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
56 |
// PUBLIC API |
|
57 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
58 |
/** |
|
59 |
* Returns the height of the Renderable. |
|
60 |
* |
|
61 |
* @return height of the object, in pixels. |
|
62 |
*/ |
|
63 |
public int getWidth() |
|
64 |
{ |
|
65 |
return mSizeX; |
|
66 |
} |
|
67 |
|
|
68 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
69 |
/** |
|
70 |
* Returns the width of the Renderable. |
|
71 |
* |
|
72 |
* @return width of the Object, in pixels. |
|
73 |
*/ |
|
74 |
public int getHeight() |
|
75 |
{ |
|
76 |
return mSizeY; |
|
77 |
} |
|
78 |
|
|
79 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
80 |
/** |
|
81 |
* Returns the depth of the Renderable. |
|
82 |
* <p> |
|
83 |
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine |
|
84 |
* 'depth' of a Renderable (bitmap really!) when rendered based only on the texture itself, that depends |
|
85 |
* on the Mesh it is rendered with. |
|
86 |
* |
|
87 |
* @return depth of the Object, in pixels. |
|
88 |
*/ |
|
89 |
public int getDepth(MeshObject mesh) |
|
90 |
{ |
|
91 |
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor); |
|
92 |
} |
|
93 |
} |
src/main/java/org/distorted/library/DistortedTexture.java | ||
---|---|---|
39 | 39 |
* framework where one is able to mark for deletion at any time and actual deletion takes place |
40 | 40 |
* on the next render). |
41 | 41 |
*/ |
42 |
public class DistortedTexture |
|
42 |
public class DistortedTexture extends DistortedRenderable
|
|
43 | 43 |
{ |
44 | 44 |
private static boolean mListMarked = false; |
45 | 45 |
private static LinkedList<DistortedTexture> mList = new LinkedList<>(); |
46 | 46 |
|
47 | 47 |
private static int mTextureH; |
48 | 48 |
|
49 |
private int mSizeX, mSizeY; // in screen space |
|
50 |
float mHalfX, mHalfY; // halves of the above |
|
51 |
private long mID; |
|
52 | 49 |
private boolean mMarked; |
53 |
|
|
54 | 50 |
private Bitmap mBmp= null; |
55 |
private int[] mTextureDataH; |
|
56 | 51 |
|
57 | 52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
58 | 53 |
// We have to flip vertically every single Bitmap that we get fed with. |
... | ... | |
75 | 70 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
76 | 71 |
// must be called from a thread holding OpenGL Context |
77 | 72 |
|
78 |
void createTexture()
|
|
73 |
void create() |
|
79 | 74 |
{ |
80 |
if( mBmp!=null && mTextureDataH!=null )
|
|
75 |
if( mBmp!=null && mColorH !=null )
|
|
81 | 76 |
{ |
82 |
//android.util.Log.e("Texture", "creating "+mID); |
|
83 |
|
|
84 |
if( mTextureDataH[0]==0 ) |
|
77 |
if( mColorH[0]==0 ) |
|
85 | 78 |
{ |
86 |
GLES30.glGenTextures(1, mTextureDataH, 0);
|
|
87 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mTextureDataH[0]);
|
|
79 |
GLES30.glGenTextures(1, mColorH, 0);
|
|
80 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
|
88 | 81 |
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR ); |
89 | 82 |
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR ); |
90 | 83 |
GLES30.glTexParameteri ( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE ); |
... | ... | |
93 | 86 |
} |
94 | 87 |
else |
95 | 88 |
{ |
96 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mTextureDataH[0]);
|
|
89 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
|
97 | 90 |
GLUtils.texSubImage2D(GLES30.GL_TEXTURE_2D, 0,0,0,flipBitmap(mBmp)); |
98 | 91 |
} |
99 | 92 |
|
... | ... | |
104 | 97 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
105 | 98 |
// must be called from a thread holding OpenGL Context |
106 | 99 |
|
107 |
private void deleteTexture()
|
|
100 |
private void delete() |
|
108 | 101 |
{ |
109 |
if( mTextureDataH!=null && mTextureDataH[0]>0 )
|
|
102 |
if( mColorH !=null && mColorH[0]>0 )
|
|
110 | 103 |
{ |
111 |
//android.util.Log.e("Texture", "deleting "+mID); |
|
112 |
|
|
113 |
GLES30.glDeleteTextures(1, mTextureDataH, 0); |
|
114 |
|
|
115 |
mTextureDataH[0] = 0; |
|
116 |
mID = 0; |
|
104 |
GLES30.glDeleteTextures(1, mColorH, 0); |
|
105 |
mColorH[0] = 0; |
|
117 | 106 |
} |
118 | 107 |
|
119 | 108 |
mMarked = false; |
120 | 109 |
} |
121 | 110 |
|
122 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
123 |
|
|
124 |
long getID() |
|
125 |
{ |
|
126 |
return mID; |
|
127 |
} |
|
128 |
|
|
129 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
130 |
|
|
131 |
boolean setAsInput() |
|
132 |
{ |
|
133 |
if( mID!=0 ) |
|
134 |
{ |
|
135 |
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mTextureDataH[0]); |
|
136 |
return true; |
|
137 |
} |
|
138 |
|
|
139 |
return false; |
|
140 |
} |
|
141 |
|
|
142 | 111 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
143 | 112 |
|
144 | 113 |
static void getUniforms(int mProgramH) |
... | ... | |
173 | 142 |
|
174 | 143 |
if( tmp.mMarked ) |
175 | 144 |
{ |
176 |
tmp.deleteTexture();
|
|
145 |
tmp.delete(); |
|
177 | 146 |
iterator.remove(); |
178 | 147 |
} |
179 | 148 |
} |
... | ... | |
190 | 159 |
*/ |
191 | 160 |
public DistortedTexture(int width, int height) |
192 | 161 |
{ |
193 |
mSizeX= width ; mHalfX = mSizeX/2.0f; |
|
194 |
mSizeY= height; mHalfY = mSizeY/2.0f; |
|
195 |
|
|
196 |
mTextureDataH = new int[1]; |
|
197 |
mTextureDataH[0]= 0; |
|
198 |
mBmp = null; |
|
199 |
mID = 0; |
|
200 |
mMarked = false; |
|
162 |
mSizeX = width ; |
|
163 |
mSizeY = height; |
|
164 |
mColorH[0]= 0; |
|
165 |
mBmp = null; |
|
166 |
mMarked = false; |
|
201 | 167 |
|
202 | 168 |
mList.add(this); |
203 | 169 |
} |
... | ... | |
208 | 174 |
*/ |
209 | 175 |
public void markForDeletion() |
210 | 176 |
{ |
211 |
//android.util.Log.e("Texture", "marking for deletion "+mID); |
|
212 |
|
|
213 | 177 |
mListMarked = true; |
214 | 178 |
mMarked = true; |
215 | 179 |
} |
... | ... | |
227 | 191 |
public void setTexture(Bitmap bmp) |
228 | 192 |
{ |
229 | 193 |
mBmp= bmp; |
230 |
mID = bmp.hashCode(); |
|
231 |
|
|
232 |
//android.util.Log.e("Texture", "setting new bitmap "+mID); |
|
233 |
} |
|
234 |
|
|
235 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
236 |
/** |
|
237 |
* Returns the height of the Texture. |
|
238 |
* |
|
239 |
* @return height of the object, in pixels. |
|
240 |
*/ |
|
241 |
public int getWidth() |
|
242 |
{ |
|
243 |
return mSizeX; |
|
244 |
} |
|
245 |
|
|
246 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
247 |
/** |
|
248 |
* Returns the width of the Texture. |
|
249 |
* |
|
250 |
* @return width of the Object, in pixels. |
|
251 |
*/ |
|
252 |
public int getHeight() |
|
253 |
{ |
|
254 |
return mSizeY; |
|
255 |
} |
|
256 |
|
|
257 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
258 |
/** |
|
259 |
* Returns the depth of the Texture. |
|
260 |
* <p> |
|
261 |
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine |
|
262 |
* 'depth' of a texture when rendered based only on the texture itself, that depends on the Mesh it is |
|
263 |
* rendered with. |
|
264 |
* |
|
265 |
* @return depth of the Object, in pixels. |
|
266 |
*/ |
|
267 |
public int getDepth(MeshObject mesh) |
|
268 |
{ |
|
269 |
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor); |
|
270 | 194 |
} |
271 | 195 |
} |
src/main/java/org/distorted/library/DistortedTree.java | ||
---|---|---|
200 | 200 |
|
201 | 201 |
void drawRecursive(long currTime, DistortedFramebuffer df) |
202 | 202 |
{ |
203 |
mTexture.createTexture(); |
|
203 |
mTexture.create(); |
|
204 |
float halfX = mTexture.getWidth()/2.0f; |
|
205 |
float halfY = mTexture.getHeight()/2.0f; |
|
204 | 206 |
|
205 | 207 |
if( mNumChildren[0]<=0 ) |
206 | 208 |
{ |
... | ... | |
208 | 210 |
} |
209 | 211 |
else |
210 | 212 |
{ |
211 |
mData.mFBO.createFBO();
|
|
213 |
mData.mFBO.create(); |
|
212 | 214 |
|
213 | 215 |
if( mData.numRendered==0 ) |
214 | 216 |
{ |
... | ... | |
218 | 220 |
GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT); |
219 | 221 |
|
220 | 222 |
if( mTexture.setAsInput() ) |
221 |
DistortedEffects.drawNoEffectsPriv(mTexture.mHalfX, mTexture.mHalfY, mMesh, mData.mFBO);
|
|
223 |
DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO);
|
|
222 | 224 |
|
223 | 225 |
synchronized(this) |
224 | 226 |
{ |
... | ... | |
234 | 236 |
mData.mFBO.setAsInput(); |
235 | 237 |
} |
236 | 238 |
|
237 |
mEffects.drawPriv(mTexture.mHalfX, mTexture.mHalfY, mMesh, df, currTime);
|
|
239 |
mEffects.drawPriv(halfX, halfY, mMesh, df, currTime);
|
|
238 | 240 |
} |
239 | 241 |
|
240 | 242 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Introduce DistortedRenderable, a base class for Texture and Framebuffer