Forum > C++ > getPixel setPixel from CCTexture2D

getPixel setPixel from CCTexture2D

By ljhjason Posted 2011-08-31 13:20 Comments 6
ljhjason
  • Posts: 1

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?

walzer
  • Posts: 1735
  • Location: Xiamen, China

#1 RE: 2011-08-31 18:37

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.

Less is More. Keep Focus.

huangrh
  • Posts: 171

#2 RE: 2011-08-31 19:14

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.

dmitry
  • Posts: 40

#3 RE: 2011-12-12 10:54

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();
dmitry
  • Posts: 40

#4 RE: 2011-12-12 10:55

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 :)

chenium
  • Posts: 7

#5 RE: 2012-02-06 20:06

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 :)

asymptote
  • Posts: 3

#6 RE: 2012-05-14 15:32

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?

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps