I want to use a RenderTexture to render some color gradient triangles (before you recommend DrawNode, no, DrawNode is unfortunately not good enough, because I will want to manipulate the RenderTexture by splitting it up into sub triangles, and maybe use some textures). But the below code in my HelloWorldScene that I wrote doesn’t work as expected:
bool HelloWorld::init()
{
if ( !Scene::init() )
{
return false;
}
// creating rendertexture
Size size = Size(100, 100);
auto renderTexture = RenderTexture::create(size.width, size.height, Texture2D::PixelFormat::RGBA8888);
// clearing rendertexture with uniform white color
renderTexture->beginWithClear(Color4F::WHITE.r, Color4F::WHITE.g, Color4F::WHITE.b, Color4F::WHITE.a);
renderTexture->end();
// creating triangles to render
auto triangles = new TrianglesCommand::Triangles();
triangles->indexCount = 3;
triangles->indices = new unsigned short[3]{ 1, 2, 3 };
triangles->vertCount = 3;
auto v1 = V3F_C4B_T2F();
auto v2 = V3F_C4B_T2F();
auto v3 = V3F_C4B_T2F();
v1.vertices = Vec3(0, 0, 0);
v2.vertices = Vec3(100, 0, 0);
v3.vertices = Vec3(100, 100, 0);
v1.colors = Color4B::RED;
v2.colors = Color4B::GREEN;
v3.colors = Color4B::BLUE;
v1.texCoords = Tex2F(0, 0);
v2.texCoords = Tex2F(1, 0);
v3.texCoords = Tex2F(1, 1);
triangles->verts = new V3F_C4B_T2F[3]{ v1, v2, v3 };
auto polygonInfo = renderTexture->getSprite()->getPolygonInfo();
polygonInfo.setTriangles(*triangles);
renderTexture->getSprite()->setPolygonInfo(polygonInfo);
this->addChild(renderTexture);
renderTexture->setPosition(Vec2(200, 200));
return true;
}
This gives me the following output:
The problems are:
- There is no red in it anywhere, however I explicitely specified it. Instead, the bottom left vertex - which should be red - fades into black. Why?
- The triangle is boxed in a rectangle that is much bigger than 100×100, however I explicitely specified that it should be 100×100.
- The triangle should have a right angle and the other sides should be of equal length - see the coordinates. But instead, it’s stretched in a weird way when I set the position of the RenderTexture. It should just translate the whole RenderTexture to the specified point. Why is it stretching it, while leaving the bottom left vertex in the origin?
How can I fix all these issues?