Revision aedd9013
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/examples/stencil/StencilRenderer.java | ||
---|---|---|
27 | 27 |
import org.distorted.examples.R; |
28 | 28 |
import org.distorted.library.Distorted; |
29 | 29 |
import org.distorted.library.DistortedEffects; |
30 |
import org.distorted.library.DistortedFramebuffer; |
|
30 | 31 |
import org.distorted.library.DistortedNode; |
31 | 32 |
import org.distorted.library.DistortedScreen; |
32 | 33 |
import org.distorted.library.DistortedTexture; |
... | ... | |
51 | 52 |
{ |
52 | 53 |
private GLSurfaceView mView; |
53 | 54 |
private DistortedScreen mScreen; |
54 |
private DistortedTexture mCubeTex, mFloorTex; |
|
55 |
private DistortedEffects mCube1Effects, mCube2Effects, mFloorEffects; |
|
56 |
private MeshObject mCubeMesh, mFloorMesh; |
|
55 |
private DistortedTexture mCubeTex, mFloorTex, mFBOTex; |
|
56 |
private DistortedEffects mCube1Effects, mCube2Effects, mFloorEffects, mFBOEffects; |
|
57 |
private DistortedNode mCube1Node, mCube2Node, mFloorNode, mFBONode; |
|
58 |
private MeshObject mCubeMesh, mQuad; |
|
59 |
|
|
60 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
61 |
|
|
62 |
void setScreen(boolean screen) |
|
63 |
{ |
|
64 |
if( screen ) |
|
65 |
{ |
|
66 |
mScreen.detachAll(); |
|
67 |
mScreen.attach(mCube1Node); |
|
68 |
mScreen.attach(mFloorNode); |
|
69 |
mScreen.attach(mCube2Node); |
|
70 |
} |
|
71 |
else |
|
72 |
{ |
|
73 |
if( mFBONode==null ) mFBONode = new DistortedNode(mFBOTex,mFBOEffects,mQuad); |
|
74 |
|
|
75 |
mScreen.detachAll(); |
|
76 |
mScreen.attach(mFBONode); |
|
77 |
mFBONode.attach(mCube1Node); |
|
78 |
mFBONode.attach(mCube2Node); |
|
79 |
mFBONode.attach(mFloorNode); |
|
80 |
|
|
81 |
DistortedFramebuffer fbo = mFBONode.getFramebuffer(); |
|
82 |
if( fbo!=null ) fbo.enableDepthStencil(DistortedFramebuffer.BOTH_DEPTH_STENCIL); |
|
83 |
else |
|
84 |
{ |
|
85 |
android.util.Log.e("stencil", "failed to enable STENCIL!"); |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
57 | 89 |
|
58 | 90 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
59 | 91 |
|
... | ... | |
61 | 93 |
{ |
62 | 94 |
mView = v; |
63 | 95 |
|
64 |
mCubeMesh = new MeshCubes(1,1,false);
|
|
65 |
mFloorMesh = new MeshFlat(1,1);
|
|
96 |
mCubeMesh = new MeshCubes(1,1,false); |
|
97 |
mQuad = new MeshFlat(1,1);
|
|
66 | 98 |
|
67 | 99 |
mCubeTex = new DistortedTexture(1,1); |
68 | 100 |
mFloorTex = new DistortedTexture(1,1); |
101 |
mFBOTex = new DistortedTexture(1,1); |
|
69 | 102 |
|
70 | 103 |
mCube1Effects = new DistortedEffects(); |
71 | 104 |
mCube2Effects = new DistortedEffects(); |
72 | 105 |
mFloorEffects = new DistortedEffects(); |
106 |
mFBOEffects = new DistortedEffects(); |
|
73 | 107 |
|
74 | 108 |
mCube2Effects.brightness(new Static1D(0.5f)); |
75 | 109 |
|
76 |
DistortedNode cube1Node = new DistortedNode(mCubeTex ,mCube1Effects,mCubeMesh );
|
|
77 |
DistortedNode cube2Node = new DistortedNode(mCubeTex ,mCube2Effects,mCubeMesh );
|
|
78 |
DistortedNode floorNode = new DistortedNode(mFloorTex,mFloorEffects,mFloorMesh);
|
|
110 |
mCube1Node = new DistortedNode(mCubeTex ,mCube1Effects,mCubeMesh );
|
|
111 |
mCube2Node = new DistortedNode(mCubeTex ,mCube2Effects,mCubeMesh );
|
|
112 |
mFloorNode = new DistortedNode(mFloorTex,mFloorEffects,mQuad );
|
|
79 | 113 |
|
80 |
floorNode.glEnable(GLES30.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
|
81 |
floorNode.glStencilFunc(GLES30.GL_ALWAYS, 1, 0xFF); // Set any stencil to 1
|
|
82 |
floorNode.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_REPLACE); // replace with 1 when we fail Depth test
|
|
83 |
floorNode.glStencilMask(0xFF); // Write to stencil buffer
|
|
84 |
floorNode.glDepthMask(false); // Don't write to depth buffer
|
|
85 |
floorNode.glClear(GLES30.GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (0 by default)
|
|
114 |
mFloorNode.glEnable(GLES30.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
|
115 |
mFloorNode.glStencilFunc(GLES30.GL_ALWAYS, 1, 0xFF); // Set any stencil to 1
|
|
116 |
mFloorNode.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_REPLACE); // replace with 1 when we fail Depth test
|
|
117 |
mFloorNode.glStencilMask(0xFF); // Write to stencil buffer
|
|
118 |
mFloorNode.glDepthMask(false); // Don't write to depth buffer
|
|
119 |
mFloorNode.glClear(GLES30.GL_STENCIL_BUFFER_BIT); // Clear stencil buffer (0 by default)
|
|
86 | 120 |
|
87 |
cube2Node.glEnable(GLES30.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
|
88 |
cube2Node.glStencilFunc(GLES30.GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
|
|
89 |
cube2Node.glStencilMask(0x00); // Don't write anything to stencil buffer
|
|
90 |
cube2Node.glDepthMask(true); // Write to depth buffer
|
|
121 |
mCube2Node.glEnable(GLES30.GL_STENCIL_TEST); // Enable Stencil when rendering this Node
|
|
122 |
mCube2Node.glStencilFunc(GLES30.GL_EQUAL, 1, 0xFF); // Pass test if stencil value is 1
|
|
123 |
mCube2Node.glStencilMask(0x00); // Don't write anything to stencil buffer
|
|
124 |
mCube2Node.glDepthMask(true); // Write to depth buffer
|
|
91 | 125 |
|
92 | 126 |
mScreen = new DistortedScreen(mView); |
93 |
mScreen.attach(cube1Node); |
|
94 |
mScreen.attach(floorNode); |
|
95 |
mScreen.attach(cube2Node); |
|
96 | 127 |
mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f); |
97 |
|
|
98 | 128 |
mView.setEGLConfigChooser(5,6,5,0,16,8); // Screen: 16 bit depth, 8 bit STENCIL |
129 |
|
|
130 |
setScreen(true); |
|
99 | 131 |
} |
100 | 132 |
|
101 | 133 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
102 |
|
|
134 |
|
|
103 | 135 |
public void onDrawFrame(GL10 glUnused) |
104 | 136 |
{ |
105 | 137 |
mScreen.render(System.currentTimeMillis()); |
... | ... | |
115 | 147 |
|
116 | 148 |
float fw = mFloorTex.getWidth(); |
117 | 149 |
float fh = mFloorTex.getHeight(); |
118 |
float fd = mFloorTex.getDepth(mFloorMesh);
|
|
150 |
float fd = mFloorTex.getDepth(mQuad);
|
|
119 | 151 |
|
120 | 152 |
float cubeScale = 0.4f*(width>height ? height/ch:width/cw); |
121 | 153 |
float floorScale= 0.8f*(width>height ? height/fh:width/fw); |
... | ... | |
157 | 189 |
mFloorEffects.rotate(rotSt2, axisX, floorCenter); |
158 | 190 |
mFloorEffects.rotate(rotDyn, axisZ, floorCenter); |
159 | 191 |
|
192 |
if( mFBONode==null ) mFBONode = new DistortedNode(mFBOTex,mFBOEffects,mQuad); |
|
193 |
|
|
194 |
DistortedFramebuffer fbo = mFBONode.getFramebuffer(); |
|
195 |
if( fbo!=null ) fbo.resize(width,height); |
|
196 |
else |
|
197 |
{ |
|
198 |
android.util.Log.e("stencil", "failed to resize FBO!"); |
|
199 |
} |
|
200 |
|
|
160 | 201 |
mScreen.resize(width, height); |
161 | 202 |
} |
162 | 203 |
|
Also available in: Unified diff
First try to convert the Stencil app to a dual (directly to Screen / through intermediate Framebuffer) mode.
Doesn't work yet ( API is inconvenient / plain wrong )