为什么glDrawArrays会导致EXC_BAD_ACCESS(代码= 2,地址= 0x0)?

时间:2014-06-21 05:31:46

标签: opengl cocos2d-x

为什么会出现以下行:

glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)numberOfVertices);

结果为:EXC_BAD_ACCESS (code=2, address=0x0)

void Earth::onDraw(const cocos2d::Mat4 &transform, bool transformUpdated){
    Sprite *stripes = Sprite::create("stripes.png");
    this->setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::GLProgram::SHADER_NAME_POSITION_TEXTURE));
    CC_NODE_DRAW_SETUP();
    GL::bindTexture2D(stripes->getTexture()->getName());
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD);
    DrawPrimitives::setDrawColor4F(1.0f, 1.0f, 1.0f, 1.0f);
    glVertexAttribPointer(GL::VERTEX_ATTRIB_FLAG_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(GL::VERTEX_ATTRIB_FLAG_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)numberOfVertices);
}

Earth是一个cocos2d :: Node。

1 个答案:

答案 0 :(得分:2)

基于cocos2d-x文档,我发现(http://www.cocos2d-x.org/reference/native-cpp/V3.0beta2/d4/d83/group__shaders.html),GL::VERTEX_ATTRIB_FLAG_POSITIONGL::VERTEX_ATTRIB_FLAG_TEX_COORD是位掩码值,值为1和4.但是您将它们用作第一个参数glVertexAttribPointer()

glVertexAttribPointer(GL::VERTEX_ATTRIB_FLAG_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GL::VERTEX_ATTRIB_FLAG_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);

这里的第一个参数需要是顶点属性的位置,它不太可能是1和4.我解释文档的方式,你需要使用这些值:

glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);