glocrawArrays与cocos2d完全没有绘图

时间:2018-02-06 13:01:52

标签: c++ cocos2d-x opengl-3

我有很多要点,我需要在一个批次中绘制,我已经尝试了两天,我似乎无法获得glDrawArrays的任何进展。我已经尝试了DrawNode并绘制每个单独的点进行测试并且它正常工作......但我似乎无法让glDrawArray给出任何视觉效果。

这是我的绘图代码(更改了一些变量名称):

auto glProgram = getGLProgram();
if (glProgram == nullptr) {
    setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(
            GLProgram::SHADER_NAME_POSITION_COLOR));
    glProgram = getGLProgram();
    if (glProgram == nullptr) {
        return;
    }
}
glProgram->use();
glProgram->setUniformsForBuiltins();

GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
GLfloat *vertices = new GLfloat[myStruct->data.size()*2];
GLfloat *colors   = new GLfloat[myStruct->data.size()*4];
int vIndex = 0;
int cIndex = 0;
for (std::vector<myPointStruct*>::iterator it = myStruct->data.begin(); it != myStruct->data.end(); ++it) {
    vertices[vIndex++] = (*it)->pos.x;
    vertices[vIndex++] = (*it)->pos.y;
    colors[cIndex++] = (*it)->color.r;
    colors[cIndex++] = (*it)->color.g;
    colors[cIndex++] = (*it)->color.b;
    colors[cIndex++] = (*it)->color.a;
glLineWidth(10);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat), &vertices[0]);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat), &colors[0]);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_POINTS, 0, (GLsizei) myStruct->data.size());
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,  (GLsizei) myStruct->data.size());

以下是我如何称呼该方法:

_renderTexture->begin();
myMethodForDrawing();
_renderTexture->end();
Director::getInstance()->getRenderer()->render();

我也尝试过:

_renderTexture->begin();
_customCommand.init(_renderTexture->getGlobalZOrder());
_customCommand.func = CC_CALLBACK_0(MyClass:: myMethodForDrawing,this);
auto renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_customCommand);
_renderTexture->end();

1 个答案:

答案 0 :(得分:1)

glVertexAttribPointer的第5个参数指定连续通用顶点属性之间的字节偏移量。如果stride为0,则通用顶点属性被理解为紧密打包在数组中。

由于顶点和颜色紧密排列,因此无需设置stride参数。请注意,sizeof(GLfloat)无论如何都是错误的。在您的情况下,2 * sizeof(GLfloat)vertices4 * sizeof(GLfloat)colors

像这样更改代码(专注于第5个参数的0):

glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0]);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, &colors[0]);