OpenGL (ES) Android -- 为什么这个 glcolorpointer 只显示黑色?
我正在尝试绘制渐变,但首先我只想让 glcolorpointer 正常工作。如果我使用 glColor4f(...) 它会正确绘制颜色,但 glcolorpointer 只是绘制黑色。请帮忙
gl11.glPushMatrix();
//gl11.glColor4f(RGBBorder[0], RGBBorder[1], RGBBorder[2], alpha);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerCube);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerCube);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
float colors[] = {.7f,.7f,.7f,.5f};
ByteBuffer vbb = ByteBuffer.allocateDirect(colors.length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer buff = vbb.asFloatBuffer();
buff.put(colors);
buff.position(0);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, buff);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glDrawElements(GL11.GL_TRIANGLES, indicesCube, GL11.GL_UNSIGNED_SHORT, 0);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glPopMatrix();
I'm trying to draw a gradient, but first I just want to get the glcolorpointer working. If I use glColor4f(...) it draw colors correctly, but the glcolorpointer just draws black. Help please
gl11.glPushMatrix();
//gl11.glColor4f(RGBBorder[0], RGBBorder[1], RGBBorder[2], alpha);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerCube);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerCube);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
float colors[] = {.7f,.7f,.7f,.5f};
ByteBuffer vbb = ByteBuffer.allocateDirect(colors.length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer buff = vbb.asFloatBuffer();
buff.put(colors);
buff.position(0);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, buff);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glDrawElements(GL11.GL_TRIANGLES, indicesCube, GL11.GL_UNSIGNED_SHORT, 0);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glPopMatrix();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您只为一个顶点添加颜色,并且将
buff
留得太小而无法处理您拥有的所有顶点。使用颜色缓冲区,在本例中,每个顶点应该有 4 个浮点乘以 8(假设您正在绘制一个立方体)。它应该与引入顶点坐标非常相似,即使我从未使用过 GL11.glBindBuffer 。这次的不同之处在于您为每个顶点分配的颜色值。本教程如果您还没有读过,那么这本书非常值得一读。
I think the problem is that you add color for one vertex only, and leave
buff
too small to handle all vertices you have. Using color buffer you're supposed to have, in this case, 4 floats per vertex, times 8 (assuming it's a cube you're drawing). It should be very much the same as introducing your vertex coordinates, even though I've never played around withGL11.glBindBuffer
. This time with the difference it's a color value you're assigning for each vertex.This tutorial is very good read if you haven't done it already.