Search code examples
c++openglcocos2d-xcocos2d-x-3.0

OpenGl rotating triangles have some bad effects


I wrote a code to draw 10 yellow triangles on the screen and rotate them to animate like this I will add 2 frames below:frame 1 frame 2

All this is very nice, but when I add number of rectangles to be about 800 then I see this:

open gl cannot handle 800 triangles 1.

In the case when I have 500 triangles I see the following problems:

500 triangles problem 1 500 triangles problem 2 500 triangles problem 3 500 triangles problem 4

As you can see I don't even have those colors. (I have only black and the yellow one.) Actually, you can see there any color, depending on the number of triangles. I think this is some OpenGL problem. I an new to OpenGL and I guess I misused something in OpenGL. Here is the code. It is in cocos2d-x (C++) but surely you can understand what is being done here:

Color4F color1e(239.0f / 255, 255.0f / 255, 138.0f / 255, 1);

auto sunNode = DrawNode::create();
sunNode->setContentSize(_size);

int bansCount = 1000;
Vec2 center(_size / 2);

float angleStep = (2 * 3.1415926) / bansCount;

auto rotated = [angleStep, center](float mult) {
Vec2 vc(0, 0);
vc.rotate(center, angleStep * mult);
return vc - center;
};

for(int i = 0; i < bansCount; i+=2) {
   sunNode->drawTriangle(
   Vec2(0,0), // vertex 1
    rotated(i), // vertex 2
    rotated(i + 1), // vertex 3
    color1e // color of vertices
);
}

addChild(sunNode, 2); // add to sceen
sunNode->setPosition(center); // position on center

// infinitely rotate 
sunNode->runAction(RepeatForever::create(RotateBy::create(0.05, 3.1415926 / 10)));

Code full is here, in case someone want to look at or experiment: https://github.com/jonyrock/Cocos2dxDrawingArtifacts


Solution

  • The interference pattern is normal, and it happens because edges are quantized (to a pixel or to some sub-pixel fixed value) and steps create those interference patterns with a much lower frequency than the step frequency itself.

    The color is not normal, and I can think to two possibilities:

    • Using non-saturated RGB colors and "cleartype"-like technology that tries to use separate subpixel led intesity values to improve resolution (and this could interact with the interference patterns)

    • A bug

    If it's the first case then using pure red, pure green or pure blue like

    Color4F color1e(255.0f / 255, 0.0f / 255, 0.0f / 255, 1); // Pure red
    

    shouldn't introduce extra colors.