Kevin H ExtendedSpriteBatchNode for cocos2d-x
Posts 70
Added by Kevin H over 1 year ago

Over at Cocos2d-iPhone an extension was made to allow a single CCSpriteBatchNode render all sprite nodes at once and maintain proper z-ordering.
http://www.cocos2d-iphone.org/forum/topic/14747

Attached is the ExtendedSpriteBatchNode that is converted to C++ for Cocos2d-x.

Adding particles has not been converted (yet) and the sorting algorithm has been changed from insertion sort (n^2 worst case) to smooth sort (nlogn worst case).

This class has helped me tremendously with isometric perspectives. Thanks again to the original author!

Zhe Wang RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 1642
Location Amoy, China
Added by Zhe Wang over 1 year ago

Thanks to your contribution. I added a link to this topic in the sticky FAQ, http://www.cocos2d-x.org/boards/6/topics/567

Enjoy Coding, Enjoy Life.

Dae-yung Hwang RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 3
Added by Dae-yung Hwang about 1 year ago

Thanks a lot!

Kevin H RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 70
Added by Kevin H about 1 year ago

There were a few bugs and performance issues with this class.
Line 254 should read: CCSpriteBatchNode::removeChild(sprite, cleanup);

I've ended up using vertexz instead, which is must faster. I'll contribute the class in a day or two.

Kevin H RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 70
Added by Kevin H about 1 year ago

Just to follow up..

I've found that it is very easy to sort objects across CCSpriteBatchNodes using the follow method:

In the function AppDelegate::applicationDidFinishLaunching() add the following

pDirector->setDepthTest(true);

then, at the beginning of the draw() function for CCSpriteBatchNode, etc, add the following

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0);

and at the end of the same function add

glDisable(GL_ALPHA_TEST);

After those modifications are done the setVertexZ for sprites should work instead of using setZOrder. I usually scale my range values to within 1000, because much higher and stange things start to occur. Also, sprites sometimes have thin alpha "halos" because of the alpha testing.

D L RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 22
Added by D L 10 months ago

HI,

We're trying to do the same time, but I'm running into compile errors: it can't find GL_ALPHA_TEST or glAlphaFunc(). I'm using cocos2d-x. 2.0rc1 on iPhone, so I'm thinking I might have some missing header files?

D

EDIT:
Hmm, ok these things are OpenGL ES1 only, so that seems why I can't get access to it, does anyone have an example of the replacement code I could use for cocos2d-x 2?

D L RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 22
Added by D L 10 months ago

I found this for OpenGL ES 2.0 but it doesn't seem to work. any one have an idea?

void GameMob::draw()
{
    // OpenGL ES 1.0 only??
//    glEnable(GL_ALPHA_TEST);
//    glAlphaFunc( GL_GREATER, 0 );
//
//    glDisable(GL_ALPHA_TEST);

     CCGLProgram* s_pShader = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest);

     int alphaValue = glGetUniformLocation( s_pShader->getProgram(), "u_alpha_value");

     s_pShader->setUniformLocationWith1f(alphaValue, 0.0f);

    CCSpriteBatchNode::draw();

}
Orson Bushnell RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 1
Added by Orson Bushnell 7 months ago

I'm having the same problem. But I noticed in your sample that you setup the shader but don't run it.

setShaderProgram(s_pShader);

I tried changing the default shader and setting the alpha value like you did but did not get any farther.
What happens when you set the shader?

Kevin H RE: ExtendedSpriteBatchNode for cocos2d-x
Posts 70
Added by Kevin H 7 months ago

I'm using the shader method now, but with batch nodes (not individual sprites).

In my game init I'm doing the following:

CCGLProgram* alphashader = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColorAlphaTest);
unsigned loc = glGetUniformLocation(alphashader->getProgram(), kCCUniformAlphaTestValue);
alphashader->setUniformLocationWith1f(loc,0.0f);

After creating my batch nodes I call the following:

batch->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey( kCCShader_PositionTextureColorAlphaTest ));


(1-8/8)