OpenGL (ES)——在 VBO 中存储和使用颜色数组,并将其应用于 VBO 三角形
这怎么能做到呢?此代码不起作用:
//////////INIT COLOR VBO FOR USE WITH A TRIANGLE
triColorBuffer = ByteBuffer.allocateDirect(3 * 4
* 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
float[] colors = {
1f, 0f, 0f, 1f,
0f, 1f, 0f, 1f,
0f, 0f, 1f, 1f,};
triColorBuffer.put(colors);
triColorBuffer.flip();
int[] buffer = new int[1];
gl11.glGenBuffers(1, buffer, 0);
colorPointerTri = buffer[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, triColorBuffer.capacity()
* 4, triColorBuffer, GL11.GL_STATIC_DRAW);
////////////DRAW USING COLOR VBO
gl11.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glPushMatrix();
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerTri);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glDrawElements(GL10.GL_TRIANGLES, 3, GL10.GL_UNSIGNED_SHORT, 0);
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glPopMatrix();
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
三角形绘制VBO'd或nor,并且颜色渐变(glcolorpointer)工作非VBO'd,但我无法使用颜色指针(VBO或不使用VBO)将VBO'd三角形着色)。帮助表示赞赏。
How can this be done? This code isn't working:
//////////INIT COLOR VBO FOR USE WITH A TRIANGLE
triColorBuffer = ByteBuffer.allocateDirect(3 * 4
* 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
float[] colors = {
1f, 0f, 0f, 1f,
0f, 1f, 0f, 1f,
0f, 0f, 1f, 1f,};
triColorBuffer.put(colors);
triColorBuffer.flip();
int[] buffer = new int[1];
gl11.glGenBuffers(1, buffer, 0);
colorPointerTri = buffer[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, triColorBuffer.capacity()
* 4, triColorBuffer, GL11.GL_STATIC_DRAW);
////////////DRAW USING COLOR VBO
gl11.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glPushMatrix();
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerTri);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glDrawElements(GL10.GL_TRIANGLES, 3, GL10.GL_UNSIGNED_SHORT, 0);
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glPopMatrix();
gl11.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl11.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
The triangle draws VBO'd or nor, and the color gradient (glcolorpointer) works non VBO'd, but I can't get the VBO'd triangle to color using a color pointer (VBO or without). Help Appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在绑定顶点缓冲区,然后立即绑定颜色缓冲区。当您现在调用 glColor/VertexPointer 时,颜色和顶点都来自颜色缓冲区。您需要编写:
由于 gl...Pointer 命令使用当前绑定的缓冲区。
You are binding the vertex buffer and right after that you bind the color buffer. When you now call glColor/VertexPointer, both colors and vertices are sourced from the color buffer. You need to write:
As the gl...Pointer command uses the buffer that is currently bound.
如果我错了,请纠正我,我 15 分钟前刚开始使用 VBO。
不管怎样,看来你必须“一分为二”使用 VBO。首先你绑定你的数据/数组,然后告诉 GL 上下文你刚刚给它的内容。我花了很长时间才读完这个源文件 来弄清楚发生了什么,并真心希望我没有完全误解它。该文件摘录;
在您的代码中,您绑定了 3 次,并且在开始切换绑定的内容后立即进行。我不知道那里到底发生了什么,但我会尝试一下,例如
,从我所看到的来看,如果您在渲染后解除数组的绑定,它不会造成任何伤害。也许它在您的代码中,没有复制/粘贴到此处,但以防万一。
Please do correct me if I'm wrong, I started out with VBOs just 15mins ago.
Anyway, it seems so you have to use VBOs 'in two'. First you bind your data/array, and right after that tell GL context what you just gave it. Took me a good a while to read through this source file to figure out what was going on and truly hope I didn't misunderstood it totally. Excerpt from that file;
In your code, you're binding 3 times and right after start switching what was binded. I don't know what exactly happens there but I would give it a go to try e.g.
Also from what I've seen it shouldn't do any harm if you unbind your arrays after rendering. Maybe it's in your code not copy/pasted here, but just in case.