了解OpenGL ES崩溃原因以及调试方法

时间:2015-10-08 14:56:06

标签: c++ ios opengl-es cocos2d-x

了解如何在cocos2d-x下使用OpenGL ES。 帮助我理解,虽然以下功能在噪音上与EXC_BAD_ACCESS崩溃 - > visit();这可能意味着我的OpenGL状态在命令执行时被搞砸了。

我的方法:

Sprite* GameLevelLayer::spriteWithColor(Color4F bgColor, float textureWidth, float textureHeight)
{

    // 1: Create new RenderTexture
    auto rt = RenderTexture::create(textureWidth, textureHeight);

    // 2: Call RenderTexture:begin
    rt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);

    // 3: Draw into the texture
    //// gradient
    gradient_command.init(rt->getGlobalZOrder());
    gradient_command.func = std::bind(&GameLevelLayer::onDrawGradient, this, textureWidth, textureHeight);
    auto renderer = Director::getInstance()->getRenderer();
    renderer->addCommand(&gradient_command);

    //// noise cloud
    BlendFunc blendFunc;
    blendFunc.src = GL_DST_COLOR;
    blendFunc.dst = GL_ZERO;

    auto noise = Sprite::create("Noise.png");
    noise->setBlendFunc(blendFunc);
    noise->setPosition(Vec2(textureWidth / 2, textureHeight / 2));
    // XXX
    noise->visit();

    // 4: Call RenderTexture:end
    rt->end();

    // 5: Create a new Sprite from the texture
    return Sprite::createWithTexture(rt->getSprite()->getTexture());
}

和渐变:

void GameLevelLayer::onDrawGradient(float textureWidth, float textureHeight)
{
    setGLProgram(ShaderCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_COLOR));
    getGLProgram()->use();
    getGLProgram()->setUniformsForBuiltins();

    float gradientAlpha = 0.8f;

    std::vector<Vec2> vertices;
    std::vector<Color4F> colors;

    vertices.push_back(Vec2(0, 0));
    colors.push_back(Color4F(0.0f, 0.0f, 0.0f, 0.0f ));

    vertices.push_back(Vec2(textureWidth, 0));
    colors.push_back(Color4F(0.0f, 0.0f, 0.0f, 0.0f ));

    vertices.push_back(Vec2(0, textureHeight));
    colors.push_back(Color4F(0.0f, 0.0f, 0.0f, gradientAlpha ));

    vertices.push_back(Vec2(textureWidth, textureHeight));
    colors.push_back(Color4F(0.0f, 0.0f, 0.0f, gradientAlpha ));

    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices.data());
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, colors.data());
    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)vertices.size());
}

这是因为我在某处使用了错误大小的参数吗?怎么去调试这样的东西?

0 个答案:

没有答案