I am writing a 2D game with cocos2d-x. I want to put a "shadow" sprite on a background sprite using the equation:

MAX(0, Cd * 1 - Cs * S)

where Cd is the destination color (that is, a background pixel), Cs is the source color (the shadow pixel), and S is the scale factor (between 0 and 1). The MAX() function is used to avoid negative results. This is a lighting effect: when the shadow sprite pixel is 0, there is no effect on the background pixel, otherwise, the background pixel becomes darker.

Now, the only way that comes to my mind is to change the blending equation to GL_FUNC_SUBTRACT, but it doesn't compile with cocos2d-x (can't found it).

I would subclass the CCSprite class in order to implement the draw() method in order to change, when needed, the blending equation, call the original draw() method and restore the blending equation to its previous state at the end of the method.

So my questions are two:

  1. How to use glBlendEquation() with cocos2d-x? Keep in mind that I am writing a game for iPhone/Android/Windows.
  2. Are shadows handled this way in 2D games?
  • \$\begingroup\$ as a side note: as stated in cocos2d-x site, it's not recommended to develop a game for windows using it, unless you are rewriting all opengles functions used in cocos2d-x to use OpenGL/DirectX! \$\endgroup\$
    – Ali1S232
    Commented Dec 11, 2011 at 22:35

2 Answers 2


cocos2d is not much different than a normal opengl app. there are some basic states that is set while you are running a cocos2d app, but other than that every thing is just the same as a normal opengl application. I guess the normal way of using glBlendEquation or glBlendFunc is to inherit some class from CCSprite class (or any other class that you want it to drop shadow) and then override draw method. it will look something like this :

-(void) draw
    // create a backup from glBlendEquation
    // do somthing with glBlendEquation
    [super draw];
    // restore glBlendEquation options

I'm not familiar with either obj-c nor opengl (I'm using cocos2d-x in c++ and occasionally needed to add some basic opengl function), but from what I know the code above should work.

  • \$\begingroup\$ Gajet, i am using cocos2d-x, exactly like you. Your solution is exactly equal to the one i had tought about using. It should work, but the glBlendEquation() function does not belong to standard opengl (it seems to me)... \$\endgroup\$
    – lukeluke
    Commented Dec 10, 2011 at 23:10
  • \$\begingroup\$ For standard i mean the functions you get including "gl/gl.h". \$\endgroup\$
    – lukeluke
    Commented Dec 10, 2011 at 23:23
  • \$\begingroup\$ cocos2d-x is using OpenGL-es, which shares many features with OpenGL. after a search it seems OpenGLes2 has a function named glBlendEquation (khronos.org/opengles/sdk/docs/man/xhtml/glBlendEquation.xml) but I'm not sure how to use it. \$\endgroup\$
    – Ali1S232
    Commented Dec 11, 2011 at 12:26
  • 2
    \$\begingroup\$ Finally i made it work: i had to change the blending factors (DST=GL_ONE, SRC=GL_SRC_ALPHA)!!! You can esily control the opacity in the usual way (setOpacity(). \$\endgroup\$
    – lukeluke
    Commented Dec 11, 2011 at 15:33
  • 3
    \$\begingroup\$ @lukeluke can you maybe post this solution in a separate answer and accept it? That way, readers will find the solution immediately. \$\endgroup\$ Commented Dec 13, 2011 at 16:23

Answer 1:

You can use shaders if you want to go for glBlendEquation. Have a look here;

Please note that, using more amount of shaders can effect the performance of some devices.

Answer 2:

And if light direction is constant in your game, than you can attach shadow with graphics only (i.e in PNG only). Or you can use separate dark shadow image with some transparency. There are the lightweight solutions.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .