Cocos2d-x 3.8 - RenderTexture ,OpenGL not rendering

Cocos2d-x 3.8 - RenderTexture ,OpenGL not rendering

本文关键字:OpenGL not rendering RenderTexture Cocos2d-x      更新时间:2023-10-16

我试图在RenderTexture创建的纹理上画一条bezier线,但是线没有显示,只有纹理

这是我遵循的教程

Bezier.h:

class Bezier : public Node
{
private:
    Color4F genRandomBrightColor();
public:
    Sprite* create(float width, float height);
};

Bezier.cpp:

Sprite* Bezier::create(float width, float height){
    auto rt = RenderTexture::create(width, height);
    auto randomColor = genRandomBrightColor(); 
    //rt->begin();
    rt->beginWithClear(randomColor.r, randomColor.g, randomColor.b, randomColor.a);
    //draw bezier line
    int segments = 50;
    //tried with Vec2 too
    Vertex2F vertices[51];
    Color4F colors[51];
    float t = 0;
    Point startPoint = Point(0, CCRANDOM_0_1()*height);
    Point anchor1 = Point(CCRANDOM_0_1()*width / 2, CCRANDOM_0_1()*height);
    Point anchor2 = Point((CCRANDOM_0_1()*width / 2) + (width / 2), CCRANDOM_0_1()*height);
    Point endPoint = Point(width, CCRANDOM_0_1()*height);
    //this i copied from DrawNode so it should be good
    for (int i = 0; i < segments; i++){
        colors[i] = Color4F::WHITE;
        vertices[i] = Vertex2F(powf(1 - t, 3) * startPoint.x + 3.0f * powf(1 - t, 2) * t * anchor1.x + 3.0f * (1 - t) * t * t * anchor2.x + t * t * t * endPoint.x,
                        powf(1 - t, 3) * startPoint.y + 3.0f * powf(1 - t, 2) * t * anchor1.y + 3.0f * (1 - t) * t * t * anchor2.y + t * t * t * endPoint.y);
        t += 1.0f / segments;
    }
    vertices[segments] = Vertex2F(endPoint.x, endPoint.y);
    //////////////////////////////////////////////////////////////////////////
    auto shaderProgram = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_COLOR);
    setShaderProgram(shaderProgram);
    CC_NODE_DRAW_SETUP();
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, segments);
    rt->end();
    auto sprite = Sprite::createWithTexture(rt->getSprite()->getTexture());
    return sprite;
}
Color4F Bezier::genRandomBrightColor(){
    while (true){
        float r = CCRANDOM_0_1();
        float g = CCRANDOM_0_1();
        float b = CCRANDOM_0_1();
        if ((r < 0.25) && (g > 0.5) && (b > 0.75) || (r > 0.75) && (g > 0.5) && (b<0.25)){
            return Color4F(r,g,b,1);
        }
    }
}

GameScene.cpp:

#include <Bezier.h>
....
auto dl = new Bezier();
auto sprite = dl->create(visibleSize.width, visibleSize.height/2);
sprite->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 4 + origin.y));
this->addChild(sprite);

下面是截图:http://postimg.org/image/cvy46wtwv/

任何帮助将不胜感激!PS:我不使用DrawNode的功能,因为我想更多地了解这个

编辑:明白了!我需要使用自定义命令OpenGl代码不能在cocos2dx- 3.0与VS2013 CPP下工作

在create函数的最后,像这样添加addchild(精灵):

auto sprite = Sprite::createWithTexture(rt->getSprite()->getTexture());
addChild(sprite);
    return sprite;