【发布时间】:2015-08-22 21:51:10
【问题描述】:
我在 opengles 2.0 应用程序上工作得很好,直到我在不支持 VAO 的旧手机上对其进行了测试,现在我似乎陷入了困境。
在 VAO 成为某种标准并且无处不在之后,我开始使用 opengl,所以我从来不用不使用它来渲染。现在我必须编写支持它的代码,我遇到了一些麻烦。
顶点着色器
attribute vec3 position;
attribute vec4 icolor;
varying vec4 fcolor;
void main()
{
gl_Position = vec4(position, 1.0);
fcolor = icolor;
}
片段着色器
precision mediump float;
varying vec4 fcolor;
void main (void)
{
gl_FragColor = fcolor;
}
应用方面
初始化代码:
glGenBuffers(1, &verticesBuffer);
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(rend2d->vertices), rend2d->vertices, GL_STATIC_DRAW);
glGenBuffers(1, &indicesBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(rend2d->indices), rend2d->indices, GL_STATIC_DRAW);
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(rend2d->colors), rend2d->colors, GL_STATIC_DRAW);
渲染代码: glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(sp);
GLint posLoc = glGetAttribLocation(sp, "position");
GLint colLoc = glGetAttribLocation(sp, "icolor");
glBindBuffer(GL_ARRAY_BUFFER, verticesBuffer);
glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glVertexAttribPointer(colLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer);
glDrawElements(GL_TRIANGLES, rend2d->vertexCount, GL_UNSIGNED_INT, 0);
我的错误可能非常明显,但我只是看不到我目前没有正确执行的部分,并希望获得有关半现代 opengl 的帮助。这主要是为opengles 2.0但不支持的app提供支持 GL_OES_vertex_array_object 扩展。
【问题讨论】:
-
我在您的代码中没有看到任何
glEnableVertexAttribArray()调用。每个属性的启用状态也存储在 VAO 中。