getPixel setPixel from CCTexture2D

i want to know is there a way to getPixel/setPixel from CCTexture2D
i look into a class CCMutableTexture2D which can do that
and try to convert it to cocos2dx.
but there is a lot of class cocos2dx doesnt support.
like CGContextRef,CGColorSpaceRef …

any other way to do this?

In cocos2d-iphone, CCTexture2D have no interfaces of getPixel & setPixel, because glReadPixels & glWritePixels have very low performance.
But you can add glReadPixels & glWritePixels into CCTexture2D as you wish, and export interface for your usage.

You talked about CGContextRef,CGColorSpaceRef, they work with CGImage/UIImage, not CCTexture2D. So another approach is to modify CCImage in cocos2d-x, it’ll be more close to your original code. But you had to implement the new interfaces in all your target platforms.

1 Like

In my knowledge, i don’t know there is any way to read or write pixels directly from a texture.

if you do want to do that, ccrendertexture can realize the idea, but glreadpixels is rather slow, a texture of 1024*512 would cost 0.2-0.3s.

I’ve just ported CCTexture2DMutable to cocos2d-x and it seems working. A very quick test:

    CCSize texSize = CCSizeMake(128, 128);
    int bytes = texSize.width * texSize.height * 4;
    void* textureData = malloc(bytes);
    memset(textureData, INT32_MAX, bytes);

    CCTexture2DMutable* texture = new CCTexture2DMutable();
    if (!texture->initWithData(textureData, kCCTexture2DPixelFormat_RGBA8888, texSize.width, texSize.height, texSize)) {
        CCLOG("Could not create texture");
        delete texture;
        texture = NULL;
    }

    texture->setAliasTexParameters();

    for (int x = 0; x < 128; x++) {
        for (int y = 0; y < 128; y++) {
            ccColor4B color = ccc4(CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255,
                                   CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255);
            texture->setPixelAt(CCPointMake(x, y), color);
        }
    }
    texture->apply();

    CCSprite* sprite = CCSprite::spriteWithTexture(texture);
    sprite->setScale(3.0);
    this->addChild(sprite);
    texture->autorelease();

However, I don’t know why, but another simpler test doesn’t work. It should draw a single diagonal line with random pixels.

    for (int x = 0; x < 128; x++) {
            ccColor4B color = ccc4(CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255,
                                   CCRANDOM_0_1() * 255, CCRANDOM_0_1() * 255);
            texture->setPixelAt(CCPointMake(x, x), color);
    }

Anyway, I think, it would be good to get CCRenderTexture and CCTexture2DMutable married together :slight_smile:

this actually does work, i think the issue is when you apply the texture to the sprite, it anchors the center of the texture to the origin of the sprite. if you translate the sprite to the upper right, you will see the result.

Dmitry Matyukhin wrote:

However, I don’t know why, but another simpler test doesn’t work. It should draw a single diagonal line with random pixels.
>
[…]
>
Anyway, I think, it would be good to get CCRenderTexture and CCTexture2DMutable married together :slight_smile:

Hi

I just tested it with cocos2d-1.0.1-x-0.13.0-beta. In Android if you change the texSize to NPOT (Not Power Of Two), it just draws white. On iOS it works fine.
Any clue why it does this?