setContext, shaders, draw order advice/question (bug perhaps?)

In my ongoing quest to learn the ins and outs of glsl shaders stepping from a single image to a 2D scene took a little learning about frame buffers and setContext(). Drawing the scene off camera to apply the shader to the entire scene works fine for simple 2D but provides strange effects in 3D space. It seems that it has to do with the draw order of the objects. When drawing a 2D game, the order of the object change what will go behind what, but in 3D the draw order is handled by the device with things like depth buffers and math way over my head. This seams to be ignored when using setContext()


W/ ripple shader on

W/ shader off


This might be a question that can be quickly explained with a “no” but I thought I might try:
Is there a way to solve this draw order problem?

@1980geeksquad - the 3D depth setting does not work with setContext. The next version will hopefuly solve that.

There’s a workaround for that though…

Use this extension in the fragment shader

#extension GL_EXT_shader_framebuffer_fetch : require

And then use “gl_LastFragData[0].x” to get the depth of previous fragment.
Found it useful while creating depth maps for shadowing.

Here’s the link to the post by space monkey who uses it for depth maps.

@Ignats - I sure hope so. I don’t plan on using this idea right away, but in the future, it would be nice to have the option for some neat effects.

@Saurabh - I’m not sure that is really helps my problem. A depthmap would be useful, but how would I pass that into the draw order and get rid of the oddities that I showed?

Thanks for the help @Ignatz and @Saurabh. ;:wink:

Double post

@1980geeksquad It should probably work this way…

In the fragment shader,

If (last depth > current depth) 
Gl_FragColor = vColor

I haven’t tried it, but it should work.

I just saw this on codea commits Runtime - JohnTLL - Added depth attachment to setContext. :slight_smile: