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

标签 c++ opengl cocos2d-x

想创建一个纹理贴图,但似乎这段代码不起作用。我认为 openGL 部分不起作用。可能我错过了什么,我正在尝试制作像 map 一样的 Tiny wing 并完成教程: http://www.raywenderlich.com/33266/how-to-create-dynamic-textures-with-ccrendertexture-in-cocos2d-2-x . 以下代码假定提供条纹背景的输出,请检查链接。

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

最佳答案

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

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

}

关于c++ - OpenGl 代码不适用于带有 VS2013 CPP 的 cocos2dx-3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23950633/

相关文章:

c++ - C++11 中的 std::basic_string::npos 声明

c++ - 使用声明包含未扩展的参数包

c++ - 无法在 Windows XP 上编译 rsa.h

objective-c - 如何在Objective-C OpenGL中正确使用InitWindow?

c++ - 在 xcode 中将 boost 库添加到 cocos2d-x 项目中?

c++ - 从 QNX 移植到开源 RTOS 所需的步骤

c++ - 对同一图形应用多个同时旋转

c++ - gluLookAt 不断旋转相机

cocos2d-android - Cocos2d-x 与 cocos2d-android 的 Android 游戏

c++ - 从 C++ 中的 switch case 返回二维数组