将纹理应用于顶点缓冲区对象基元

发布于 2024-12-07 10:12:41 字数 1623 浏览 0 评论 0原文

如何将纹理应用到 Android 中的顶点缓冲区对象?

答案:


代码工作正常,只是缺少对

glEnable(GL_TEXTURE_2D);

This 的调用,并且

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

为了顶点缓冲区对象绘制纹理都需要调用 of 。


问题:

据我所知,首先必须创建一个 NIO 缓冲区:

ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = tbb.asFloatBuffer();
textureBuffer.put(texCoords);
textureBuffer.position(0);

在此代码示例中,数组 texCoords 包含 2 分量 (s, t) 纹理数据。

创建 NIO 缓冲区后,您需要将其传递给 opengl 并创建顶点缓冲区对象:

int[] id = new int[1];//stores the generated ID.
gl11.glGenBuffers(1, id, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0]);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoords.length * 4, textureBuffer, GL11.GL_STATIC_DRAW);

这样就可以完成所有初始化工作。接下来我们需要绘制它,我们这样做:

gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);//enable for textures
gl11.glActiveTexture(GL11.GL_TEXTURE0);
//lets pretend we created our texture elsewheres and we have an ID to represent it.
gl11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);

//Now we bind the VBO and point to the buffer.
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0])//the id generated earlier.
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);//this points to the bound buffer

//Lets also pretend we have our Vertex and Index buffers specified.
//and they are bound/drawn correctly.

因此,尽管这是我认为 OpenGL 绘制纹理所需要的,但我有一个错误,并且只有一个红色三角形(没有我的调制石头)纹理)渲染。

How do you apply textures to a vertex buffer object in Android?

ANSWER:


The code works fine, except it is missing a call to

glEnable(GL_TEXTURE_2D);

This and the call of

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

are both required for in order for vertex buffer object to draw texture.


QUESTION:

From what I know, first you must create a NIO Buffer:

ByteBuffer tbb = ByteBuffer.allocateDirect(texCoords.length * 4);
tbb.order(ByteOrder.nativeOrder());
FloatBuffer textureBuffer = tbb.asFloatBuffer();
textureBuffer.put(texCoords);
textureBuffer.position(0);

In this code sample, the array texCoords contains the 2-component (s, t) texture data.

After creating the NIO Buffer, you need to pass it to opengl and create Vertex Buffer Object:

int[] id = new int[1];//stores the generated ID.
gl11.glGenBuffers(1, id, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0]);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, texCoords.length * 4, textureBuffer, GL11.GL_STATIC_DRAW);

So that takes care of all the initalization. Next we need to draw it, and we do so like this:

gl11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);//enable for textures
gl11.glActiveTexture(GL11.GL_TEXTURE0);
//lets pretend we created our texture elsewheres and we have an ID to represent it.
gl11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);

//Now we bind the VBO and point to the buffer.
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, id[0])//the id generated earlier.
gl11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);//this points to the bound buffer

//Lets also pretend we have our Vertex and Index buffers specified.
//and they are bound/drawn correctly.

So even though this is what I would think would be needed in order for OpenGL to draw the texture, I have an error, and only a red triangle (without my modulated stone texture) renders.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

把回忆走一遍 2024-12-14 10:12:41

需要调用 VBO 的两个函数才能启用纹理。

gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

Both functions for VBO's need to be called to enable textures.

gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);

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