RenderTexture ordering PositionZ backwards

I’ve been dealing with this for quite a while and came across something very odd so I hope someone here can help me out with this.

I’m using and following this tutorial for post processing effects for Cocos2dx: http://www.babaei.net/blog/2016/01/08/post-processing-effects-in-cocos2d-x/

For simplicity, I’m only using one Post-Processing layer at this point in time (the tutorial uses 2).

I’ve followed the tutorial (it’s essentially copy and pasting code) and have gotten the effect. However, for whatever reason the vertexZ buffering I’m using for the Tiled Maps isn’t being applied. (The depth buffering thing here: http://www.cocos2d-x.org/wiki/TileMap)

The vertex shader:

[code]attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;

varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform vec2 strokeWidth;

void main()
{
gl_Position = CC_PMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}

[/code]

If the fragment shader:

[code]varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

void main()
{
vec4 c = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);
gl_FragColor = c;
}[/code]

How it should look:

How it looks:

Does anyone have any suggestions?

EDIT:: Edited the title because of RenderTexture ordering PositionZ backwards

Minor update because it’s very late here. The reason it works fine without gl_FragColor being set to anything is because it’s transparent so it shows the original scene underneath.

Problem still persists otherwise with when it IS set to something it doesn’t work.

Edited the original post to clarify issue.

Some small updates.

I removed all of the shader nonsense and tried to simply have a RenderTexture render the scene as mentioned here: http://www.cocos2d-x.org/wiki/Render_To_Texture The problem still persists.

If I initialize the RenderTexture differently, like in the following, it only shows the item with the smallest vertexZ.

I’m going to test out if it’s drawing the item with the smallest vertexZ last (and covering up everything else because it takes up the fullscreen) or not a bit later.

What are you using the RenderTexture for? I’m not sure how RT’s work with depth and vertexz.
Have you tried the experimental::FastTMXTiledMap?

I’m using the RenderTexture for PostProcessing as described in this link: http://www.babaei.net/blog/2016/01/08/post-processing-effects-in-cocos2d-x/

Also if you try to look for any sort of postprocessing for Cocos2dx as far as I’ve seen they all refer to the same method.

Also, no, I’m not using the FastTMX map because it’s inefficient for the maps I’m using (3200x3200px large).

Ah so you’re rendering into a RenderTexture. If so, I’m surprised it doesn’t work the same with depth since internally the default camera is essentially using one (well an FBO).

So is the first screenshot rendering normally w/o post-processing and the second is rendering into a RenderTexture with it?

Yep. I’m pretty confused. I figured maybe initiallizing the render texture as follows would help, but then it’s only rendering items with the lowest vertexZ order. That or it’s rendering it backwards (lowest ontop). I haven’t tested the backwards thing just yet.

Bumping this in case anyone else has any ideas

Try using GL_DEPTH24_STENCIL8_OES instead, I doubt you’ll be needing 32bits of floating point depth, and GL_DEPTH32F_STENCIL8 generates a 64bit pixel buffer, 24bits of which are used for alignment - i.e. wasted.

I’m not even sure if many if any mobile devices support GL_DEPTH32F_STENCIL8 :stuck_out_tongue:

1 Like

That particular version? isn’t available (I got a compile error) so I just tried GL_DEPTH24_STENCIL8. No dice. Same error as I mentioned before, it’s not drawing everything in its proper order anymore.

I did some testing and this is what I found:

  • My Background layer is set to cc_vertexz of -500 normally; it was drawn on top of everything. I changed it to 0 and it was finally drawn behind the characters.
  • My Overlay/Foreground is set to cc_vertexz of 0 normally. It was always drawn behind the character. Changing it to 500 put it always on top of the character.
  • My other TMX layers are using cc_vertexz set to automatic. When the player is in front of a tile, the tile is drawn on top of the character, instead of the other way around. When the player moves behind the tile, the character is drawn on top.

So essentially, the issue here is that the RenderTexture is ordering the vertexZ (Position Z) backwards from how it is normally. I can only think that it must be related to the reason why you need to set “setFlippedY” to true. So perhaps whatever it is that’s going on in there needs to be applied to the PositionZ as well?