cocos2d-x v3 中获取屏幕的像素点

https://github.com/c0i/cocos2dx-lite/issues/15

void pickPixelAt(Node* node, float world_posx, float world_posy)
{
    auto winSize = Director::getInstance()->getWinSize();
    static RenderTexture *rt = 0;
    if (!rt) {
        rt = RenderTexture::create(winSize.width , winSize.height );
        rt->retain();
    }
    
    auto pixelPos = CC_POINT_POINTS_TO_PIXELS(Vec2(x, y));
    
    static CustomCommand *cmd = 0;
    if (!cmd) {
        cmd = new CustomCommand();
    }
    cmd->func = [=]() {
        
        GLubyte pixel[4] = {0};
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glReadPixels(pixelPos.x, pixelPos.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, (void*)&pixel);
            CCLOG("r=%d,g=%d,b=%d, a=%d", pixel[0], pixel[1], pixel[2], pixel[3]);
    };

    
    rt->beginWithClear(0, 0, 0, 0);
    node->visit();
    Director::getInstance()->getRenderer()->addCommand(cmd);
    rt->end();
}

你可能感兴趣的:(cocos2d-x v3 中获取屏幕的像素点)