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()

Examples

W/ ripple shader on

https://www.dropbox.com/s/7o2jf2yxklfzd9b/Video%20Feb%2025%2C%205%2034%2054%20PM.mp4

W/ shader off

https://www.dropbox.com/s/hmb93vyb0dqmnez/Video%20Feb%2025%2C%205%2051%2039%20PM.mp4

Images

https://www.dropbox.com/s/65y6gcbik0er617/Photo%20Feb%2025%2C%205%2051%2047%20PM.png

https://www.dropbox.com/s/521xhxxd9chdi7z/Photo%20Feb%2025%2C%205%2052%2043%20PM.png

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.

http://twolivesleft.com/Codea/Talk/discussion/2373/shadow-mapping/p1

@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
Else
Discard

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: