CCDrawnode not respecting alpha?

I seem to be having an issue with the alpha being ignored when doing a drawpolygon on a CCDrawNode. What I expect to happen is, if I draw with 50 alpha (.5), I will be able to see through the drawn shapes to other Sprites behind the drawnode. But instead, the fills are always at 100 opacity. Is this a bug, or am I confused?

Example code:

    CCDrawNode *draw = CCDrawNode::create();
    CCPoint vertices2[] = { ccp(left, top), ccp(right, top), ccp(right, bottom), ccp(left, bottom) };
    draw->drawPolygon(vertices2, 4, ccc4f(1,0,0,.5), 1, ccc4f(1,1,1,1));
    layer->addChild(draw);

From this, I get a red square at 100% opacity. Everything behind it is completely hidden.

Any ideas would be greatly appreciated!

Well, I don’t know what the CCDrawNode does, but here is what mine does:

class DrawNode : public CCNode {
public:
        struct rect {
            float left, top, right, bottom;
            ccColor4F color;
        };
        vector rects;
        DrawNode() {
        }
        virtual void draw() {
            for(auto it = rects.begin(); it != rects.end(); ++it) {
                rect r = *it;
                CCPoint filledVertices[] = { ccp(r.left,r.top), ccp(r.right,r.top), ccp(r.right,r.bottom), ccp(r.left,r.bottom) };
                ccDrawSolidPoly(filledVertices, 4, r.color );
            }           
        }
        void addRect(rect r) {
            rects.push_back(r);
        }
        static DrawNode* create() {
            DrawNode *dr = new DrawNode();
            if (dr) {
                dr->autorelease();
                return dr;
            }
            CC_SAFE_DELETE(dr);
            return nullptr;
        }
};

And mine has transparency :slight_smile:

It only allows you to add rectangles at the moment, but could easily be extended.

Example use:

    auto dr = DrawNode::create();
    DrawNode::rect r = { left, top, right, bottom, p->color };
    dr->addRect(r);
    dr->setPosition(ccp(0, 0));
    layer->addChild(dr);

I have the same issue with CCDrawNode not respecting alpha and always drawing with 100% opacity.

@Adipose Dan

Yours solution work fine, but it’s a lot (and I mean a lot) slower than CCDrawNode.

Is any hope for update for CCDrawNode?

Adam Pach wrote:

I have the same issue with CCDrawNode not respecting alpha and always drawing with 100% opacity.
>
`Adipose Dan

Yours solution work fine, but it’s a lot (and I mean a lot) slower than CCDrawNode.

Is any hope for update for CCDrawNode?

Adam,

I seem to remember that dynamically allocating an array like I did above can be expensive. Maybe if you made it a `CCPOint[4]@ and moved it into the class definition, it would speed things up:

class DrawNode : public CCNode {
public:
        CCPoint filledVertices[4];
        struct rect {
            float left, top, right, bottom;
            ccColor4F color;
        };
        vector rects;
        DrawNode() {
        }
        virtual void draw() {
            for(auto it = rects.begin(); it != rects.end(); ++it) {
                rect r = *it;
                filledVertices[0]=ccp(r.left,r.top);
                filledVertices[1]=ccp(r.right,r.top);
                filledVertices[2]=ccp(r.right,r.bottom);
                filledVertices[3]=ccp(r.left,r.bottom);
                ccDrawSolidPoly(filledVertices, 4, r.color);
            }            
        }
        void addRect(rect r) {
            rects.push_back(r);
        }
        static DrawNode* create() {
            DrawNode *dr = new DrawNode();
            if (dr)    {
                dr->autorelease();
                return dr;
            }
            CC_SAFE_DELETE(dr);
            return nullptr;
        }
};

I haven’t tested this, but let me know if it’s any faster.

Supposely alpha from CCDrawNode works on iOS, but on Android and Windows it does not work.

By the way, I found an issue with my class. When fading out, it wasn’t working properly. I had to change the draw function to have GL_SRC_ALPHA:

void DrawNode::draw() {
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    for(auto it = polys.begin(); it != polys.end(); ++it) {
        poly r = *it;
        CCPoint* filledVertices = &r.verts[0];
        ccDrawSolidPoly(filledVertices, r.verts.size(), r.color );
    }           
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}

I set it back to GL_ONE afterwards as that appears to be what cocos2d-x expects.

Not sure if this is what you’re seeing, but I had a similar issue with CCDrawNode not respecting alpha on android. I was able to fix what I was seeing by changing a line in CCDrawNode::render()

this line:
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(ccV2F_C4B_T2F), (GLvoid *)offsetof(ccV2F_C4B_T2F, colors));

I change GL_FALSE -> GL_TRUE