Cocos2d-x Gaussian Blur

Source: Internet
Author: User
Tags addchild

The recording uses Gaussian blur under COCOS2DX, and can be used in areas such as the UI Bullet box.

The approximate idea is to get the current screen data (screenshot), data processing (Gaussian blur), generate a image-> texture--wizard 1. Screenshots can be used with COCOS2DX Ccutils::o Ncapturescreen Get Flippedbuffer

void Oncapturescreen (const std::function<void (bool, const std::string&) >& aftercaptured, const std::
    string& filename) {Auto Glview = director::getinstance ()->getopenglview ();
Auto framesize = Glview->getframesize (); #if (Cc_target_platform = = Cc_platform_mac) | | (Cc_target_platform = = Cc_platform_win32) | | (Cc_target_platform = = cc_platform_linux) framesize = framesize * Glview->getframezoomfactor () * GlView->getReti
Nafactor ();
    #endif int width = static_cast<int> (framesize.width);
    
    int height = static_cast<int> (framesize.height);
    BOOL succeed = FALSE;
    
    std::string outputFile = ""; Do {std::shared_ptr<glubyte> buffer (new glubyte[width * height * 4], [] (glubyte* p) {Cc_safe_delete_arr AY (P);
        });
        if (!buffer) {break;

} glpixelstorei (Gl_pack_alignment, 1); #if (Cc_target_platform = = CC_PLATFORM_WP8)//The frame buffer is aLways created with portrait orientation on WP8.  
        So if the current device orientation are landscape, we need to rotate the frame buffer.
        Auto rendertargetsize = Glview->getrenertargetsize (); Ccassert (Width * height = = static_cast<int> (Rendertargetsize.width * rendertargetsize.height), "The frame size is n
        OT matched "); Glreadpixels (0, 0, (int) rendertargetsize.width, (int) rendertargetsize.height, Gl_rgba, Gl_unsigned_byte, Buffer.get (
));
#else glreadpixels (0, 0, width, height, Gl_rgba, Gl_unsigned_byte, Buffer.get ()); #endif std::shared_ptr<glubyte> Flippedbuffer (New glubyte[width * height * 4], [] (glubyte* p) {CC_ Safe_delete_array (P);
        });
        if (!flippedbuffer) {break;
        } #if (Cc_target_platform = = CC_PLATFORM_WP8) if (width = = static_cast<int> (rendertargetsize.width))
            {//The current device, orientation is portrait. for (int row =0; Row < height; ++row) {memcpy (Flippedbuffer.get () + (height-row-1) * Width * 4, Buffer.get () + row * wid
            TH * 4, Width * 4);
            }} else {//The current device orientation is landscape.
                for (int row = 0, row < width; ++row) {for (int col = 0; col < height; ++col) {* (int*) (Flippedbuffer.get () + (HEIGHT-COL-1) * Width * 4 + row * 4) = * (int*) (Buffer.get (
                ) + row * Height * 4 + col * 4); }}} #else for (int row = 0; row < height; ++row) {memcpy (Flippe
        Dbuffer.get () + (height-row-1) * Width * 4, Buffer.get () + row * Width * 4, Width * 4); } #endif
2. Gaussian BlurGaussian algorithm, you can see the source code
Gaussian blur int _datalen = height * Width * 4;
        unsigned char* _data = static_cast<unsigned char*> (malloc (_datalen * sizeof (unsigned char)));
                     Stackblurjob (Flippedbuffer.get (),///< input image data width,///< image width Height,///< image height 8,///< blur intensity (should is in 2..254 range ) 1,///< total number of working threads 0,///< current Threa
        D Number 1,///< step of processing _data);
                     Stackblurjob (Flippedbuffer.get (),///< input image data width,///< image width Height,///< image height 8,///< blur intensity (should is in 2..254 range ) 1,///< total number of working threads 0,///< CurrenT thread number 2,///< step of Processing (_data); 

3. Spawn Wizard3.1 Generating an image
<span style= "White-space:pre" >	</span>image* image = new Image;
        if (image)
        {
            image->initwithrawdata (Flippedbuffer.get (), Width * Height * 4, width, height, 8);
            Notification End
            aftercaptured (image);
            Image->release ();
        }
3.2 Textures to sprites, aftercaptured specific as follows
<span style= "White-space:pre" >	</span>texture2d* t = new texture2d;
        T->initwithimage (IMG);
        sprite* SPT = sprite::createwithtexture (t);
        Spt->setzorder ( -1);
        This->addchild (SPT);
                                              
        Spt->setposition (Getcontentsize (). WIDTH/2, Getcontentsize (). HEIGHT/2);
        Spt->setscale (Getcontentsize (). Width/spt->getcontentsize (). Width, getcontentsize () .height/spt-> Getcontentsize (). height);                                     
        T->release ();
4. UseCreate a Customcommand and add it to the render renderer queue
    This->retain ();
    Static Customcommand Capturescreencommand;
    Capturescreencommand.init (Std::numeric_limits<float>::max ());
                                              Capturescreencommand.func = Std::bind (Oncapturescreen, [=] (image* img) {
                                              This->setvisible (TRUE);
                                              
                                              This->removechildbytag (123456);
                                              Gaussian Blur is saved to local texture2d* t = new texture2d;
                                              T->initwithimage (IMG);
                                              sprite* SPT = sprite::createwithtexture (t);
                                              Spt->setzorder (-1);
                                              This->addchild (SPT);
                                 Spt->settag (123456);             
                                              Spt->setposition (Getcontentsize (). WIDTH/2, Getcontentsize ()
                                              . HEIGHT/2); Spt->setscale (Getcontentsize (). Width/spt->getcontentsize (). Width, getcontentsize () .height/spt->
                                              
                                              Getcontentsize (). height);
                                              T->release ();
                                          This->release ();
    }, filename); Director::getinstance ()->getrenderer ()->addcommand (&capturescreencommand);

Above is all the steps, write good source code, welcome correction.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.