OpenGL (ES)——在 VBO 中存储和使用颜色数组,并将其应用于 VBO 三角形

发布于 2024-11-05 16:46:21 字数 1506 浏览 0 评论 0原文

这怎么能做到呢?此代码不起作用:


//////////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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮茶煮酒煮时光 2024-11-12 16:46:21

您正在绑定顶点缓冲区,然后立即绑定颜色缓冲区。当您现在调用 glColor/VertexPointer 时,颜色和顶点都来自颜色缓冲区。您需要编写:

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);

由于 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:

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);

As the gl...Pointer command uses the buffer that is currently bound.

澜川若宁 2024-11-12 16:46:21

如果我错了,请纠正我,我 15 分钟前刚开始使用 VBO。

不管怎样,看来你必须“一分为二”使用 VBO。首先你绑定你的数据/数组,然后告诉 GL 上下文你刚刚给它的内容。我花了很长时间才读完这个源文件 来弄清楚发生了什么,并真心希望我没有完全误解它。该文件摘录;

if (useTexture) {
    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex);
    gl11.glTexCoordPointer(2, mCoordinateType, 0, 0);
}            
if (useColor) {
    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mColorBufferIndex);
    gl11.glColorPointer(4, mCoordinateType, 0, 0);
}

在您的代码中,您绑定了 3 次,并且在开始切换绑定的内容后立即进行。我不知道那里到底发生了什么,但我会尝试一下,例如

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerTri);
gl11.glDrawElements(GL10.GL_TRIANGLES, 3, GL10.GL_UNSIGNED_SHORT, 0);

,从我所看到的来看,如果您在渲染后解除数组的绑定,它不会造成任何伤害。也许它在您的代码中,没有复制/粘贴到此处,但以防万一。

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);

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;

if (useTexture) {
    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mTextureCoordBufferIndex);
    gl11.glTexCoordPointer(2, mCoordinateType, 0, 0);
}            
if (useColor) {
    gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, mColorBufferIndex);
    gl11.glColorPointer(4, mCoordinateType, 0, 0);
}

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.

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, vertexPointerTri);
gl11.glVertexPointer(3, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, colorPointerTri);
gl11.glColorPointer(4, GL10.GL_FLOAT, 0, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexPointerTri);
gl11.glDrawElements(GL10.GL_TRIANGLES, 3, GL10.GL_UNSIGNED_SHORT, 0);

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.

gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文