rendering only to depth buffer

Started by raft, February 18, 2014, 11:53:32 AM

Previous topic - Next topic

raft

is it possible to render an object only to depth buffer but not to frame buffer? i want to use this for a kind of masking.

here is my tunnel rendered on top of camera view:


same tunnel with a different camara orientation. obviously looks wrong since the deeper parts of the tunnel should not be actually visible.


if I understand the depth mask thing in unity correct, one way to resolve this issue is, place a plane on top of the tunnel with a hole in it (hole is the entry of the tunnel), render it to only depth buffer and then render the scene (tunnel).

like this, the yellow area will be masked:


is this possible?

raft

solved :) i've attached an IRenderHook to the mask plane:

public void beforeRendering(int polyID) {
    GLES20.glColorMask(false, false, false, true);
    GLES20.glDepthMask(true);
}

public void afterRendering(int polyID) {
    GLES20.glColorMask(true, true, true, true);
}


i was expecting this to work without GLES20.glDepthMask(true) since depth buffer is enabled by default, but it didnt work that way. i put the mask plane to another world and render to same frame buffer before the main world (similar to sky box but just the opposite). i couldnt make it work when all are in the same world. i'm not sure why? i've played with sortOffset of the mask plane but didnt helped.

here is a screenshot:

EgonOlsen

Another option would be to render the excluder world, clear only the color buffer and render the rest. That should work without the additional gl calls, but i see no need to change what you have. I just wanted to mention it... ;)

raft

thx, i wasnt aware of that method.

btw, why is not my method working when mask and the rest are in the same world?

EgonOlsen

Maybe because of the ordering. It's state based for opaque objects, so you can't tell which comes first.

raft

i see. btw, I couldnt make the masking by clearing color buffer. i've made a test case by modifying the hello world sample and it works, however in my real application it doesnt. i've even omited the call to clearColorBufferOnly and still real world is not masked but rendered on top of mask plane. obviously something is clearing the depth buffer in between, but I can't find it.

any ideas?


raft

i'm correcting myself here. i just created another cube in the helloworld sample, translated it a bit to right and used it as a mask. this worked. however now i tried different geometries as mask, some work some not. this also depends on the location of the mask.

i dont know what's hapenning.

this is is what I do, anything wrong with this?

fb.clear(back);

maskWorld.setCameraTo(world.getCamera());
maskWorld.renderScene(fb);
maskWorld.draw(fb);

fb.clearColorBufferOnly(back);

world.renderScene(fb);
world.draw(fb);

fb.display();

EgonOlsen

Maybe different clipping planes in both Worlds?

raft

they are default for both. coming from Config.farPlane

EgonOlsen

I can't spot anything wrong in what you did. I advised the same approach in some other thread a few days ago and it seemed to work fine. You might want to set the Logger to debug and enable glDebug in the Config to see if something in the engine clears something where it shouldn't...that won't detect any direct calls in some render hook, of course. If nothing else helps, feel free to send me a test case.

raft

i couldn't create a test case. but while debugging i've noticed that FrameBuffer.clearColorBufferOnly results in a call to glClear, may this be relavant?

below are the logs of a whole cycle. it starts with FrameBuffer.clear which calls glClearColor and glClear. in the middle, there is a call to FrameBuffer.clearColorBufferOnly which also calls glClearColor and glClear.

FrameBuffer.clear(color)
02-20 11:36:20.374: I/jPCT-AE(14432): glClearColor(0.0, 0.0, 0.0, 1.0) took 86041ns
02-20 11:36:20.374: I/jPCT-AE(14432): glClear(16640) took 462666ns


02-20 11:36:35.979: I/jPCT-AE(14432): glMatrixMode(5889) took 272125ns
02-20 11:36:35.984: I/jPCT-AE(14432): glLoadIdentity() took 309584ns
02-20 11:36:35.994: I/jPCT-AE(14432): glFrustumf(-0.625, 0.625, -0.3515625, 0.3515625, 1.0, 15000.0) took 368125ns
02-20 11:36:35.999: I/jPCT-AE(14432): glDisable(2912) took 294541ns
02-20 11:36:36.004: I/jPCT-AE(14432): glEnable(2977) took 290709ns
02-20 11:36:36.004: I/jPCT-AE(14432): glEnable(2896) took 289958ns
02-20 11:36:36.009: I/jPCT-AE(14432): glEnable(2884) took 494709ns
02-20 11:36:36.009: I/jPCT-AE(14432): glEnable(3042) took 333292ns
02-20 11:36:36.014: I/jPCT-AE(14432): glBlendFunc(770, 771) took 310209ns
02-20 11:36:36.014: I/jPCT-AE(14432): glDepthMask(false) took 287791ns
02-20 11:36:36.019: I/jPCT-AE(14432): glActiveTexture(33984) took 297959ns
02-20 11:36:36.024: I/jPCT-AE(14432): glBindTexture(3553, 4) took 303916ns
02-20 11:36:36.024: I/jPCT-AE(14432): glMatrixMode(5888) took 256666ns
02-20 11:36:36.029: I/jPCT-AE(14432): glPushMatrix() took 323542ns
02-20 11:36:36.029: I/jPCT-AE(14432): glLoadIdentity() took 287708ns
02-20 11:36:36.034: I/jPCT-AE(14432): glLightModelfv(2899, [F@424d4f40, 0) took 270166ns
02-20 11:36:36.039: I/jPCT-AE(14432): glMaterialfv(1032, 5632, [F@424d4f68, 0) took 277041ns
02-20 11:36:36.039: I/jPCT-AE(14432): glLoadMatrixf([F@4248e6b8, 0) took 287666ns
02-20 11:36:36.044: I/jPCT-AE(14432): glEnableClientState(32885) took 292875ns
02-20 11:36:36.049: I/jPCT-AE(14432): glBindBuffer(34962, 1) took 633375ns
02-20 11:36:36.049: I/jPCT-AE(14432): glNormalPointer(5132, 12, 0) took 74458ns
02-20 11:36:36.054: I/jPCT-AE(14432): glEnableClientState(32884) took 181459ns
02-20 11:36:36.054: I/jPCT-AE(14432): glBindBuffer(34962, 2) took 84708ns
02-20 11:36:36.054: I/jPCT-AE(14432): glVertexPointer(3, 5132, 12, 0) took 80708ns
02-20 11:36:36.054: I/jPCT-AE(14432): glDisableClientState(32886) took 97875ns
02-20 11:36:36.054: I/jPCT-AE(14432): glClientActiveTexture(33984) took 74625ns
02-20 11:36:36.054: I/jPCT-AE(14432): glEnableClientState(32888) took 84666ns
02-20 11:36:36.054: I/jPCT-AE(14432): glBindBuffer(34962, 3) took 75875ns
02-20 11:36:36.059: I/jPCT-AE(14432): glTexCoordPointer(2, 5132, 8, 0) took 86292ns
02-20 11:36:36.059: I/jPCT-AE(14432): glBindBuffer(34962, 0) took 73792ns
02-20 11:36:36.059: I/jPCT-AE(14432): glBindBuffer(34963, 4) took 74333ns
02-20 11:36:36.059: I/jPCT-AE(14432): glDrawElements(4, 48, 5123, 0) took 127750ns
02-20 11:36:36.059: I/jPCT-AE(14432): glBindBuffer(34963, 0) took 75625ns
02-20 11:36:36.059: I/jPCT-AE(14432): glMatrixMode(5888) took 64958ns
02-20 11:36:36.064: I/jPCT-AE(14432): glPopMatrix() took 74791ns
02-20 11:36:36.064: I/jPCT-AE(14432): glDisable(3042) took 85083ns
02-20 11:36:36.064: I/jPCT-AE(14432): glDepthMask(true) took 72542ns
02-20 11:36:36.064: I/jPCT-AE(14432): glDisable(2884) took 81458ns
02-20 11:36:36.064: I/jPCT-AE(14432): glDisable(2896) took 71917ns
02-20 11:36:36.064: I/jPCT-AE(14432): glDisable(2977) took 73959ns


FrameBuffer.clearColorBufferOnly(color)
02-20 11:36:36.064: I/jPCT-AE(14432): glClearColor(0.0, 0.0, 0.0, 1.0) took 76292ns
02-20 11:36:36.069: I/jPCT-AE(14432): glClear(16384) took 93917ns


and the rest:
02-20 11:36:44.644: I/jPCT-AE(14432): glDisable(2929) took 365833ns
02-20 11:36:44.649: I/jPCT-AE(14432): glDeleteTextures(1, java.nio.IntToByteBufferAdapter, status: capacity=1 position=0 limit=1) took 443000ns
02-20 11:36:44.654: I/jPCT-AE(14432): glGenTextures(1, java.nio.IntToByteBufferAdapter, status: capacity=1 position=0 limit=1) took 445417ns
02-20 11:36:44.654: I/jPCT-AE(14432): glActiveTexture(33984) took 302959ns
02-20 11:36:44.659: I/jPCT-AE(14432): glBindTexture(3553, 2) took 326500ns
02-20 11:36:44.664: I/jPCT-AE(14432): glTexParameterx(3553, 10241, 9729) took 694001ns
02-20 11:36:44.669: I/jPCT-AE(14432): glTexParameterx(3553, 10240, 9729) took 361792ns
02-20 11:36:44.669: I/jPCT-AE(14432): glTexParameterx(3553, 10242, 10497) took 372167ns
02-20 11:36:44.674: I/jPCT-AE(14432): glTexParameterx(3553, 10243, 10497) took 80792ns
02-20 11:36:44.679: I/jPCT-AE(14432): glTexImage2D(3553, 0, 6408, 512, 512, 0, 6408, 5121, java.nio.ReadWriteHeapByteBuffer, status: capacity=1048576 position=0 limit=1048576) took 1882959ns
02-20 11:36:44.679: I/jPCT-AE(14432): glBindTexture(3553, -1) took 83875ns
02-20 11:36:44.679: I/jPCT-AE(14432): glBindTexture(3553, 2) took 77334ns
02-20 11:36:50.324: I/jPCT-AE(14432): glVertexPointer(3, 5132, 12, java.nio.IntToByteBufferAdapter, status: capacity=1800 position=0 limit=1800) took 397583ns
02-20 11:36:50.324: I/jPCT-AE(14432): glEnableClientState(32884) took 86833ns
02-20 11:36:50.324: I/jPCT-AE(14432): glDisableClientState(32885) took 74458ns
02-20 11:36:50.324: I/jPCT-AE(14432): glClientActiveTexture(33984) took 77291ns
02-20 11:36:50.324: I/jPCT-AE(14432): glEnableClientState(32888) took 256542ns
02-20 11:36:50.329: I/jPCT-AE(14432): glTexCoordPointer(2, 5132, 8, java.nio.IntToByteBufferAdapter, status: capacity=1200 position=0 limit=1200) took 106333ns
02-20 11:36:50.329: I/jPCT-AE(14432): glColorPointer(4, 5132, 16, java.nio.IntToByteBufferAdapter, status: capacity=2400 position=0 limit=2400) took 97000ns
02-20 11:36:50.329: I/jPCT-AE(14432): glEnableClientState(32886) took 103041ns
02-20 11:36:50.329: I/jPCT-AE(14432): glDrawElements(4, 6, 5123, java.nio.ShortToByteBufferAdapter, status: capacity=1200 position=0 limit=1200) took 137083ns
02-20 11:36:50.329: I/jPCT-AE(14432): glEnable(2929) took 92708ns
02-20 11:36:50.334: I/jPCT-AE(14432): glMatrixMode(5889) took 73083ns
02-20 11:36:50.334: I/jPCT-AE(14432): glLoadIdentity() took 85333ns
02-20 11:36:50.334: I/jPCT-AE(14432): glFrustumf(-0.625, 0.625, -0.3515625, 0.3515625, 1.0, 15000.0) took 86708ns
02-20 11:36:50.334: I/jPCT-AE(14432): glDisable(2912) took 73500ns
02-20 11:36:50.339: I/jPCT-AE(14432): glEnable(2977) took 73583ns
02-20 11:36:50.339: I/jPCT-AE(14432): glEnable(2896) took 890208ns
02-20 11:36:50.339: I/jPCT-AE(14432): glEnable(2884) took 91125ns
02-20 11:36:50.339: I/jPCT-AE(14432): glActiveTexture(33984) took 76584ns
02-20 11:36:50.344: I/jPCT-AE(14432): glBindTexture(3553, 5) took 78875ns
02-20 11:36:50.344: I/jPCT-AE(14432): glMatrixMode(5888) took 65291ns
02-20 11:36:50.344: I/jPCT-AE(14432): glPushMatrix() took 59666ns
02-20 11:36:50.344: I/jPCT-AE(14432): glLoadIdentity() took 72958ns
02-20 11:36:50.344: I/jPCT-AE(14432): glLightModelfv(2899, [F@424d4f40, 0) took 67042ns
02-20 11:36:50.344: I/jPCT-AE(14432): glMaterialfv(1032, 5632, [F@424d4f68, 0) took 67708ns
02-20 11:36:50.349: I/jPCT-AE(14432): glLoadMatrixf([F@4248e6b8, 0) took 72083ns
02-20 11:36:50.349: I/jPCT-AE(14432): glEnableClientState(32885) took 73500ns
02-20 11:36:50.349: I/jPCT-AE(14432): glBindBuffer(34962, 5) took 77625ns
02-20 11:36:50.349: I/jPCT-AE(14432): glNormalPointer(5132, 12, 0) took 65292ns
02-20 11:36:50.349: I/jPCT-AE(14432): glEnableClientState(32884) took 84958ns
02-20 11:36:50.349: I/jPCT-AE(14432): glBindBuffer(34962, 6) took 76000ns
02-20 11:36:50.354: I/jPCT-AE(14432): glVertexPointer(3, 5132, 12, 0) took 81458ns
02-20 11:36:50.354: I/jPCT-AE(14432): glDisableClientState(32886) took 94874ns
02-20 11:36:50.354: I/jPCT-AE(14432): glClientActiveTexture(33984) took 74333ns
02-20 11:36:50.354: I/jPCT-AE(14432): glEnableClientState(32888) took 83583ns
02-20 11:36:50.354: I/jPCT-AE(14432): glBindBuffer(34962, 7) took 74875ns
02-20 11:36:50.354: I/jPCT-AE(14432): glTexCoordPointer(2, 5132, 8, 0) took 86167ns
02-20 11:36:50.354: I/jPCT-AE(14432): glBindBuffer(34962, 0) took 74250ns
02-20 11:36:50.359: I/jPCT-AE(14432): glBindBuffer(34963, 8) took 74959ns
02-20 11:36:50.359: I/jPCT-AE(14432): glDrawElements(4, 36, 5123, 0) took 113833ns
02-20 11:36:50.359: I/jPCT-AE(14432): glBindBuffer(34963, 0) took 75250ns
02-20 11:36:50.359: I/jPCT-AE(14432): glMatrixMode(5888) took 63834ns
02-20 11:36:50.359: I/jPCT-AE(14432): glPopMatrix() took 73084ns
02-20 11:36:50.359: I/jPCT-AE(14432): glEnable(3042) took 83750ns
02-20 11:36:50.359: I/jPCT-AE(14432): glBlendFunc(770, 771) took 76458ns
02-20 11:36:50.364: I/jPCT-AE(14432): glDepthMask(false) took 73500ns
02-20 11:36:50.364: I/jPCT-AE(14432): glActiveTexture(33984) took 72417ns
02-20 11:36:50.364: I/jPCT-AE(14432): glBindTexture(3553, 4) took 75958ns
02-20 11:36:50.364: I/jPCT-AE(14432): glMatrixMode(5888) took 65208ns
02-20 11:36:50.364: I/jPCT-AE(14432): glPushMatrix() took 58709ns
02-20 11:36:50.364: I/jPCT-AE(14432): glLoadIdentity() took 72916ns
02-20 11:36:50.364: I/jPCT-AE(14432): glLightModelfv(2899, [F@424d4f40, 0) took 66625ns
02-20 11:36:50.369: I/jPCT-AE(14432): glMaterialfv(1032, 5632, [F@424d4f68, 0) took 67750ns
02-20 11:36:50.369: I/jPCT-AE(14432): glLoadMatrixf([F@4248e6b8, 0) took 72208ns
02-20 11:36:50.369: I/jPCT-AE(14432): glEnableClientState(32885) took 72791ns
02-20 11:36:50.369: I/jPCT-AE(14432): glBindBuffer(34962, 9) took 75083ns
02-20 11:36:50.369: I/jPCT-AE(14432): glNormalPointer(5132, 12, 0) took 65000ns
02-20 11:36:50.369: I/jPCT-AE(14432): glEnableClientState(32884) took 83459ns
02-20 11:36:50.374: I/jPCT-AE(14432): glBindBuffer(34962, 10) took 76166ns
02-20 11:36:50.374: I/jPCT-AE(14432): glVertexPointer(3, 5132, 12, 0) took 78750ns
02-20 11:36:50.374: I/jPCT-AE(14432): glDisableClientState(32886) took 92417ns
02-20 11:36:50.374: I/jPCT-AE(14432): glClientActiveTexture(33984) took 73083ns
02-20 11:36:50.374: I/jPCT-AE(14432): glEnableClientState(32888) took 83625ns
02-20 11:36:50.374: I/jPCT-AE(14432): glBindBuffer(34962, 11) took 77667ns
02-20 11:36:50.379: I/jPCT-AE(14432): glTexCoordPointer(2, 5132, 8, 0) took 87709ns
02-20 11:36:50.379: I/jPCT-AE(14432): glBindBuffer(34962, 0) took 75541ns
02-20 11:36:50.379: I/jPCT-AE(14432): glBindBuffer(34963, 12) took 74834ns
02-20 11:36:50.379: I/jPCT-AE(14432): glDrawElements(4, 486, 5123, 0) took 103875ns
02-20 11:36:50.379: I/jPCT-AE(14432): glBindBuffer(34963, 0) took 74625ns
02-20 11:36:50.379: I/jPCT-AE(14432): glMatrixMode(5888) took 64334ns
02-20 11:36:50.384: I/jPCT-AE(14432): glPopMatrix() took 72417ns
02-20 11:36:50.384: I/jPCT-AE(14432): glDisable(3042) took 84875ns
02-20 11:36:50.384: I/jPCT-AE(14432): glDepthMask(true) took 71916ns
02-20 11:36:50.384: I/jPCT-AE(14432): glDisable(2884) took 81417ns
02-20 11:36:50.384: I/jPCT-AE(14432): glDisable(2896) took 72792ns
02-20 11:36:50.384: I/jPCT-AE(14432): glDisable(2977) took 72917ns
02-20 11:36:52.699: I/jPCT-AE(14432): glEnable(3042) took 361959ns
02-20 11:36:52.704: I/jPCT-AE(14432): glBlendFunc(770, 771) took 307500ns
02-20 11:36:52.704: I/jPCT-AE(14432): glDisable(2929) took 333209ns
02-20 11:36:52.709: I/jPCT-AE(14432): glActiveTexture(33984) took 296708ns
02-20 11:36:52.714: I/jPCT-AE(14432): glBindTexture(3553, 3) took 303500ns
02-20 11:36:55.839: I/jPCT-AE(14432): glVertexPointer(3, 5132, 12, java.nio.IntToByteBufferAdapter, status: capacity=1800 position=0 limit=1800) took 396500ns
02-20 11:36:55.844: I/jPCT-AE(14432): glEnableClientState(32884) took 339208ns
02-20 11:36:55.844: I/jPCT-AE(14432): glDisableClientState(32885) took 291333ns
02-20 11:36:55.849: I/jPCT-AE(14432): glClientActiveTexture(33984) took 301166ns
02-20 11:36:55.849: I/jPCT-AE(14432): glEnableClientState(32888) took 331292ns
02-20 11:36:55.859: I/jPCT-AE(14432): glTexCoordPointer(2, 5132, 8, java.nio.IntToByteBufferAdapter, status: capacity=1200 position=0 limit=1200) took 405750ns
02-20 11:36:55.864: I/jPCT-AE(14432): glColorPointer(4, 5132, 16, java.nio.IntToByteBufferAdapter, status: capacity=2400 position=0 limit=2400) took 380500ns
02-20 11:36:55.864: I/jPCT-AE(14432): glEnableClientState(32886) took 372917ns
02-20 11:36:55.869: I/jPCT-AE(14432): glDrawElements(4, 6, 5123, java.nio.ShortToByteBufferAdapter, status: capacity=1200 position=0 limit=1200) took 457542ns
02-20 11:36:55.874: I/jPCT-AE(14432): glDisable(3042) took 332375ns
02-20 11:36:55.879: I/jPCT-AE(14432): glEnable(2929) took 327458ns

EgonOlsen

No, the glClear is fine. It clears 0x4000, which is color buffer only. The depth buffer should remain unaffected by this.

EgonOlsen

Thanks for the test case. This one was hard to find, because i was looking at the wrong places. Actually, all is fine...it's just that, by definition, transparent objects don't write into the depth buffer. And the mask object that you are using contains some transparency in the file. If you do a tunnelMask.setTransparency(-1) after loading the file, my solution works fine. Your IRenderHook implemention works too, because it overrides the behaviour of transparent object not writing into the depth buffer.

raft

argh, thanks. i prepared that mask in 3ds max myself and god knows where that transparency came from. it was a simple plane with 9 quads where center quad is removed, other vertices shifted a bit and exported as 3ds, nothing else ???

EgonOlsen

I don't know. We had this issue before with your sky dome if you remember.