Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?
share|improve this question
    
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! –  Ali.S Dec 11 '11 at 22:35

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer
    
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)... –  lukeluke Dec 10 '11 at 23:10
    
For standard i mean the functions you get including "gl/gl.h". –  lukeluke Dec 10 '11 at 23:23
    
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. –  Ali.S Dec 11 '11 at 12:26
2  
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(). –  lukeluke Dec 11 '11 at 15:33
2  
@lukeluke can you maybe post this solution in a separate answer and accept it? That way, readers will find the solution immediately. –  Sam Hocevar Dec 13 '11 at 16:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.