RenderTexture used with PolygonInfo renders incorrectly

RenderTexture used with PolygonInfo renders incorrectly

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

// 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();
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?

It is not the RenderTexture that’s the problem, as it is in fact 100x100 in size. You can test this out by commenting out the code related to the TriangleCommand, and even a debugger will show you that the size is 100x100.

The problem is this line:

triangles->indices = new unsigned short[3]{ 1, 2, 3 };

Which should be this:

triangles->indices = new unsigned short[3]{ 0, 1, 2 };

Index starts at 0, not 1.

1 Like

I was bashing my face into the monitor when I read your answer. How could I be so stupid… Such a noob mistake. Thank you!

It happens to us all.

Just a tip: Always assume the bug is in your own code, and if you can’t see it, take a break, or move onto something else.

If in the end you feel confident that the bug is not in your code, then avoid jumping to conclusions as to where it may be, but come up with theories as to why it would be happening, and then try to prove those theories correct. The added benefit is that you’ll learn a lot in the process.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.