VBO not working with OpenGL ES 2.0
I'm trying to render a simple triangle with vbos (not indexed!). But for some reason it doesn't work. Nothing shows up on my display. This is code for OpenGL ES 2.0 on the raspberry pi. Here is part of my code:
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);
}
and
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);
}
My shaders (which compile and link without error):
attribute vec4 vert_position;
void main() {
gl_Position = vert_position;
}
and
void main() {
gl_FragColor = vec4(1.0, 0, 0, 1.0);
}
And my shader setup code:
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);
}
and
void ShaderProgramGL::bindGL() {
glUseProgram(programHandle);
glBindAttribLocation(programHandle, 0, "vert_position");
}
void ShaderProgramGL::unbindGL() {
glUseProgram(0);
}
EDIT: Fixed not-releavant mistake EDIT: Solved! Problem was a the display mapping (?) of the raspberry pi firmware: github.com/raspberrypi/firmware/issues/142 Rendering was fine after all. Thanks for your help!
At the moment you're drawing a single triangle with all 3 vertices being the same (-0.5, -0.5, 0.0)
and thus degenerating to an empty triangle (or a point, to be precise). So your drawing works perfectly, it just doesn't generate any fragments at all.
上一篇: opengl es着色器程序标识和vbo缓冲区标识相同
下一篇: VBO不能使用OpenGL ES 2.0