C++ OpenGl代码不适用于带有VS2013 CPP的cocos2dx-3.0

C++ OpenGl代码不适用于带有VS2013 CPP的cocos2dx-3.0,c++,opengl,cocos2d-x,C++,Opengl,Cocos2d X,想要创建一个文本映射,但此代码似乎不起作用。我认为openGL部分不起作用。可能我错过了一些东西,我正在尝试制作一张像翅膀一样的小地图,并完成教程: . 下面的代码假设给条带背景的输出请检查链接 RenderTexture* rt = RenderTexture::create(textureWidth, textureHeight); rt->beginWithClear(color1.r, color1.g, color1.b, color1.a); Point* vertices =

想要创建一个文本映射,但此代码似乎不起作用。我认为openGL部分不起作用。可能我错过了一些东西,我正在尝试制作一张像翅膀一样的小地图,并完成教程: . 下面的代码假设给条带背景的输出请检查链接

RenderTexture* rt = RenderTexture::create(textureWidth, textureHeight);
rt->beginWithClear(color1.r, color1.g, color1.b, color1.a);
Point* vertices = (Point*)calloc(nStrips * 6, sizeof(Point));;
Color4F* colors = (Color4F*)calloc(nStrips * 6, sizeof(Color4F));
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
CC_NODE_DRAW_SETUP();

int nVertices = 0;
float x1 = -textureHeight;
float x2;
float y1 = textureHeight;
float y2 = 0;
float dx = textureWidth / nStrips;
float stripeWidth = dx / 2;
for (int i = 0; i<nStrips; i++) {
    x2 = x1 + textureHeight;

    vertices[nVertices] = Point(x1, y1);
    colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);

    vertices[nVertices] = Point(x1 + stripeWidth, y1);
    colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);

    vertices[nVertices] = Point(x2, y2);
    colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);

    vertices[nVertices] = vertices[nVertices - 2];
    colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);

    vertices[nVertices] = vertices[nVertices - 2];
    colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);

    vertices[nVertices] = Point(x2 + stripeWidth, y2);
    colors[nVertices++] = Color4F(color2.r, color2.g, color2.b, color2.a);
    x1 += dx;
}
Sprite* noise = Sprite::create("Noise.png");
BlendFunc blendFunc;
blendFunc.src = GL_DST_COLOR;
blendFunc.dst = GL_ZERO;
noise->setBlendFunc(blendFunc);
noise->setPosition(Point(textureWidth / 2, textureHeight / 2));
noise->visit();

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_TRUE, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices);
rt->end();
return  Sprite::createWithTexture(rt->getSprite()->getTexture());
RenderTexture*rt=RenderTexture::create(textureWidth,textureHeight);
rt->beginWithClear(color1.r、color1.g、color1.b、color1.a);
点*顶点=(点*)calloc(nStrips*6,sizeof(点));;
Color4F*colors=(Color4F*)calloc(nStrips*6,sizeof(Color4F));
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionColor));
CC_节点_绘图_设置();
int-nVertices=0;
浮点x1=-纹理高度;
浮动x2;
浮动y1=纹理高度;
浮动y2=0;
float dx=纹理宽度/nStrips;
浮带宽度=dx/2;
for(int i=0;isetBlendFunc(blendFunc);
噪声->设置位置(点(纹理宽度/2,纹理高度/2));
噪音->访问();
glVertexAttribPointer(kCCVertexAttrib_位置,2,GL_浮动,GL_假,0,顶点);
glVertexAttribPointer(kCCVertexAttrib_颜色,4,GL_浮点,GL_真,0,颜色);
gldrawArray(GL_三角形,0,(GLsizei)n实体);
rt->end();
返回Sprite::createWithTexture(rt->getSprite()->getTexture());

此代码片段应该可以解决您的问题:

Sprite* HelloWorld::spriteWithColor(cocos2d::Color4F bgColor, float textureWidth, float textureHeight)
{
    RenderTexture *rt = RenderTexture::create(textureWidth, textureHeight);

    rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);


    // 3: Draw into the texture
    //Caution!!!  if you want to use your own opengl draw call ,you must wrap them with a custom command
    _customCommand.init(rt->getGlobalZOrder());
    _customCommand.func = CC_CALLBACK_0(HelloWorld::onDraw,this);
    auto renderer = Director::getInstance()->getRenderer();
    renderer->addCommand(&_customCommand);

    //draw into texture
    _noiseSprite->setBlendFunc({GL_DST_COLOR, GL_ZERO});
    _noiseSprite->setPosition(Vec2(textureWidth/2, textureHeight/2));
    _noiseSprite->visit();

    rt->end();


    return Sprite::createWithTexture(rt->getSprite()->getTexture());
}


void HelloWorld::onDraw()
{
    this->setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
    this->getGLProgram()->use();
    this->getGLProgram()->setUniformsForBuiltins();


    Size winSize = Director::getInstance()->getVisibleSize();


    float gradientAlpha =  0.7;
    Vec2 vertices[4];
    Color4F colors[4];
    int nVertices = 0;

    vertices[nVertices] = Vec2(0, 0);
    colors[nVertices++] = (Color4F){0, 0, 0, 0 };
    vertices[nVertices] = Vec2(winSize.width, 0);
    colors[nVertices++] = (Color4F){0, 0, 0, 0};
    vertices[nVertices] = Vec2(0, winSize.height);
    colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};
    vertices[nVertices] = Vec2(winSize.width, winSize.height);
    colors[nVertices++] = (Color4F){0, 0, 0, gradientAlpha};

    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_COLOR | GL::VERTEX_ATTRIB_FLAG_POSITION);

    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors);
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);

}

嗯,你不需要创建新的精灵就可以返回。你可以只做
rt->getSprite()
你找到答案了吗?没有人回答它还在等待!!!!请回答或给出任何提示,这样我就可以处理它了。。。