VBO不使用OpenGL ES 2.0

时间:2013-11-13 11:08:00

标签: opengl-es opengl-es-2.0

我正在尝试使用vbos渲染一个简单的三角形(未编入索引!)。但由于某种原因,它不起作用。我的显示器上没有显示任何内容。这是覆盆子pi上OpenGL ES 2.0的代码。 这是我的代码的一部分:

void RenderGL::drawGL() {
    glViewport(0, 0, 480, 320);

    glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
    shaderProgram.bindGL();

    glDrawArrays(GL_TRIANGLES, 0, 3);

    shaderProgram.unbindGL();
    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void RenderGL::initGL(std::string shaderPath) {
    quadVertices.push_back(-0.5f);
    quadVertices.push_back(-0.5f);
    quadVertices.push_back(0.0f);
    quadVertices.push_back(1.0f);

    quadVertices.push_back(-0.5f);
    quadVertices.push_back(0.5f);
    quadVertices.push_back(0.0f);
    quadVertices.push_back(1.0f);

    quadVertices.push_back(0.5f);
    quadVertices.push_back(0.5f);
    quadVertices.push_back(0.0f);
    quadVertices.push_back(1.0f);

    glGenBuffers(1, &vboHandle);
    glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
    glBufferData(GL_ARRAY_BUFFER, quadVertices.size() * sizeof(GLfloat), 
        &quadVertices[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    shaderProgram.initGL(shaderPath);
}

我的着色器(编译和链接没有错误):

attribute vec4 vert_position;
void main() {
    gl_Position = vert_position;
}

void main() {
    gl_FragColor = vec4(1.0, 0, 0, 1.0);
}

我的着色器设置代码:

void ShaderProgramGL::compileShader() {
    vertShaderHandle = glCreateShader(GL_VERTEX_SHADER);
    const char* vertS = vertSrc.c_str();
    glShaderSource(vertShaderHandle, 1, &vertS, 0);
    glCompileShader(vertShaderHandle);
    checkCompilationGL(vertShaderHandle);

    fragShaderHandle = glCreateShader(GL_FRAGMENT_SHADER);
    const char* fragS = fragSrc.c_str();
    glShaderSource(fragShaderHandle, 1, &fragS, 0);
    glCompileShader(fragShaderHandle);
    checkCompilationGL(fragShaderHandle);
}

void ShaderProgramGL::linkShader() {
    programHandle = glCreateProgram();
    glAttachShader(programHandle, vertShaderHandle);
    glAttachShader(programHandle, fragShaderHandle);
    glBindAttribLocation(programHandle, 0, "vert_position");
    glLinkProgram(programHandle);
    checkLinkageGL();
    glValidateProgram(programHandle);
}

void ShaderProgramGL::bindGL() {
    glUseProgram(programHandle);
    glBindAttribLocation(programHandle, 0, "vert_position");
}

void ShaderProgramGL::unbindGL() {
    glUseProgram(0);
}

编辑:修正了不重要的错误 编辑:解决了!问题是树莓派固件的显示映射(?):github.com/raspberrypi/firmware/issues/142毕竟渲染很好。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

此刻,您正在绘制一个三角形,其中所有3个顶点都是相同的(-0.5, -0.5, 0.0),因此退化为空三角形(或精确的点)。因此,您的绘图完美无缺,它根本不会产生任何碎片。