gpt4 book ai didi

c++ - RenderTexture 创建的图像不可见

转载 作者:行者123 更新时间:2023-11-28 05:23:33 26 4
gpt4 key购买 nike

我正在尝试基于另一个 Sprite 创建一个黑白 Sprite。由于在创建和缓存 Sprite 像素数据后我无法访问它,因此我通过基于 Image 创建一个新的 Image 来解决它,然后更改图像数据,然后将 Image 变成 Texture 然后最后再次变成 Sprite,现在是黑白的。

问题是,当我使用从 RenderTexture 生成的 Image 时,它实际上并没有绘制它,但是当我使用 生成的 png 图像时>RenderTexture::saveToFile 之后,它工作正常。

似乎我应该能够在 scene 上看到 output_sprite 就好了,因为它的构建方式几乎相同,唯一的区别是 Image 是从磁盘上的现有文件构建的,而不是直接从 RenderTexture 构建的。

cocos2d::Sprite* anvil_sprite = cocos2d::Sprite::createWithSpriteFrameName("anvil.png");
anvil_sprite->setAnchorPoint(Vec2::ZERO);
anvil_sprite->setPosition(0,0);

RenderTexture* rt = RenderTexture::create(anvil_sprite->getContentSize().width, anvil_sprite->getContentSize().height);
rt->begin();
anvil_sprite->visit();
rt->end();

//generates a file so I can confirm it's generating correctly in the OS's
//image viewer
rt->saveToFile("test_FILE.png");

Image* output_image = new Image;

// Either use the png generated from a previous run's RenderTexture (which works)
// or use a new Image generated on this run.
// If I use the existing png, this works as intended, but I'd like to be
// able to use the texture I generate right away instead of needing to save
// it to disk first

/* OPTION A */
output_image->initWithImageFile("test_FILE.png"); // works
/* OPTION B */
output_image = rt->newImage(); //doesn't render anything

auto output_texture = new Texture2D;
output_texture->initWithImage(output_image);

Sprite* output_sprite = cocos2d::Sprite::createWithTexture(output_texture);
scene->addChild(output_sprite);

除了在 Sprite 中使用纹理之前 GPU 没有机会渲染纹理之外,我不太确定会发生什么。我正在寻找选项 A 和 B 都以相同的方式呈现,但现在只有将预先存在的文件作为 Image 源的那个正在工作。

最佳答案

试试这个:

auto anvil_sprite = cocos2d::Sprite::create("player.png");
anvil_sprite->setAnchorPoint(Vec2::ZERO);

auto rt = RenderTexture::create(anvil_sprite->getTexture()->getPixelsWide(), anvil_sprite->getTexture()->getPixelsHigh());
rt->begin();
//Draw
anvil_sprite->visit();
rt->end();

cocos2d::Director::getInstance()->getRenderer()->render();

auto img = rt->newImage(); //Generate Image
auto texture = new cocos2d::Texture2D();
texture->initWithImage(img); //Texture sprite

auto sp = cocos2d::Sprite::createWithTexture(texture); //New Sprite
sp->setPosition(cocos2d::Director::getInstance()->getWinSize().width, cocos2d::Director::getInstance()->getWinSize().height);
this->addChild(sp);

可以使用“getData”访问同一个 sprite 的像素,并根据需要使用循环或 memset 编辑此 vector ...

auto anvil_sprite = cocos2d::Sprite::create("player.png");
anvil_sprite->setAnchorPoint(Vec2::ZERO);

auto rt = RenderTexture::create(anvil_sprite->getTexture()->getPixelsWide(), anvil_sprite->getTexture()->getPixelsHigh());
rt->begin();
anvil_sprite->visit();
rt->end();

cocos2d::Director::getInstance()->getRenderer()->render();

auto img = rt->newImage(); //Generate Image

auto data = img->getData();
for (size_t i = 0; i < img->getDataLen(); i++)
{
data[i] = 400;
}
//memset(data, 400, img->getDataLen());

我不太了解像素“颜色”,但您可以在 colors 中对自己进行更多记录。如果你想要;0

关于c++ - RenderTexture 创建的图像不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40990646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com