<COCOS2D-X>

5. DrawNode 사용하기

CodeGrimie 2021. 2. 8. 11:42

Cocos2D-X에는 프로그래머가 임의로 Mesh를 생성해서 사용할 수 있는 DrawNode를 지원한다.

마침 OpenGL을 틈틈히 공부하고 있었던 덕에 그리 어렵지 않게 손에 익힐 수 있었다.

 

▼ 삼각형, 사각형, 8각형, 16각형을 그리는 코드

    /* VISIBLE SCREEN SIZE */
    Size screenSize = Director::getInstance()->getVisibleSize();
    /* VISIBLE ORIGIN POSITION */
    Vec2 originVec2 = Director::getInstance()->getVisibleOrigin();

    /* COLOR */
    Color4F colorWhite(1.0f, 1.0f, 1.0f, 1.0f);
    Color4F colorRed(1.0f, 0.0f, 0.0f, 1.0f);

    /* COCOS2D DRAWNODE TRIANGLE */
    DrawNode* triNode = DrawNode::create();
    Size triSize = Size(20.0f, 20.0f);
    Vec2 triArray[3]; // CCW
    triArray[0] = Vec2(-triSize.width, -triSize.height);
    triArray[1] = Vec2(+triSize.width, -triSize.height);
    triArray[2] = Vec2(0.0f          , +triSize.height);
    triNode->drawPolygon(triArray, 3, colorWhite, 1, colorRed);
    triNode->setPosition(Vec2(originVec2.x + screenSize.width * 0.5f, originVec2.y + screenSize.height * 0.75f));
    this->addChild(triNode);

    /* COCOS2D DRAWNODE RECT */
    DrawNode* rectNode = DrawNode::create();
    Size rectSize = Size(20.0f, 20.0f);
    Vec2 rectArray[4]; // CCW
    rectArray[0] = Vec2(-rectSize.width, -rectSize.height);
    rectArray[1] = Vec2(+rectSize.width, -rectSize.height);
    rectArray[2] = Vec2(+rectSize.width, +rectSize.height);
    rectArray[3] = Vec2(-rectSize.width, +rectSize.height);
    rectNode->drawPolygon(rectArray, 4, colorWhite, 1, colorRed);
    rectNode->setPosition(Vec2(originVec2.x + screenSize.width * 0.5f, originVec2.y + screenSize.height * 0.5f));
    this->addChild(rectNode);

    /* COCOS2D DRAWNODE POLYGON(8) */
    DrawNode* polyNode = DrawNode::create();
    Size polySize = Size(20.0f, 20.0f);
    Vec2 polyArray[8 + 2]; // CCW
    polyArray[0] = Vec2(0.0f,  0.0f);
    polyArray[1] = Vec2(-polySize.width, 0.0f);
    polyArray[2] = Vec2(-polySize.width + polySize.width * 0.3f, -polySize.height + polySize.height * 0.3f);
    polyArray[3] = Vec2(0.0f, -polySize.height);
    polyArray[4] = Vec2(+polySize.width - polySize.width * 0.3f, -polySize.height + polySize.height * 0.3f);
    polyArray[5] = Vec2(+polySize.width, 0.0f);
    polyArray[6] = Vec2(+polySize.width - polySize.width * 0.3f, +polySize.height - polySize.height * 0.3f);
    polyArray[7] = Vec2(0.0f, +polySize.height);
    polyArray[8] = Vec2(-polySize.width + polySize.width * 0.3f, +polySize.height - polySize.height * 0.3f);
    polyArray[9] = Vec2(-polySize.width, 0.0f);
    polyNode->drawPolygon(polyArray, 10, colorWhite, 1, colorRed);
    polyNode->setPosition(Vec2(originVec2.x + screenSize.width * 0.5f, originVec2.y + screenSize.height * 0.25f));
    this->addChild(polyNode);

    /* COCOS2D DRAWNODE ROUND */
    DrawNode* roundNode = DrawNode::create();
    Size roundSize = Size(40.0f, 40.0f);
    Vec2 roundArray[16 + 2]; // CCW
    roundArray[0] = Vec2(0.0f, 0.0f);
    for (int i = 0; i < 16; ++i)
    {
        roundArray[1 + i] = Vec2(roundSize.width * cos((360.0f / 16.0f) * i * (3.141592f / 180.0f)), roundSize.height * sin((360.0f / 16.0f) * i * (3.141592f / 180.0f)));
    }
    roundArray[17] = Vec2(roundSize.width, 0.0f);
    roundNode->drawPolygon(roundArray, 18, colorWhite, 1, colorRed);
    roundNode->setPosition(Vec2(originVec2.x + screenSize.width * 0.25f, originVec2.y + screenSize.height * 0.75f));
    this->addChild(roundNode);

원래 이 기능을 사용해서 간단한 팝업창을 만들 생각이었다.

그런데 다 좋은데 GL_CALL 수가 DrawNode 객체 수만큼 호출된다는 치명적인 문제점을 가지고 있었다.

 

가뜩이나 드로우 콜을 줄일려고 하고 있는데 오히려 드로우 콜이 늘어나버리니..

분명 드로우 콜을 줄이는 방법이 있을텐데 찾아보고 적용시켜보고 글을 업데이트 해야겠다.