gpt4 book ai didi

c++ - 如何使用 OpenGL ES2、GLSL、C++ 将模型矩阵数据缓冲到纹理中?

转载 作者:行者123 更新时间:2023-11-28 05:26:13 28 4
gpt4 key购买 nike

根据 this answer , OpenGL 允许您将任意数据存储在可以在顶点着色器中访问的纹理缓冲区中。

I created a Float32Array of size 4096 * 256 * 4 which contains the world matrix for every model (enough for ~256k models). Each model has a modelIndex attribute which is used to read its matrix from the texture. Then at each frame, gl.texSubImage2D the entire texture and draw as many as possible in each draw call.

示例场景:
我有一堆独特的模型,它们共享一个公共(public)着色器,它们的顶点位置打包到一个 VBO 中。我想用从纹理缓冲区获取的唯一模型矩阵更新每个模型,然后在单个 glDrawElements() 调用中绘制它们。

一些非常粗糙的伪代码:

// In C++
struct Vertex {
GLFloat x, y;
GLuint matrixID;
}

// Create an array of floats that represent a unique model matrix
// Assume pre-calculation of values
GLfloat data[32] { /* 32 floats, two 4x4 matrices */ }
glTexImage2D(..., data);

// Then in the vertex shader
attribute vec4 in_Position; // x, y, 1.0, matrixID;
uniform mat4 uf_Projection;
void main() {
// I need help implementing this magical function...
mat4 model = getMatrixFromTextureBuffer(in_Position.w);

// Apply unique model matrix
gl_Position = uf_Projection * model * vec4(in_Position.xy, 1.0, 1.0);
}

// Draw everything!
glDrawElements(...);

有人可以分享一个如何实现这个的例子吗?我很难在网上找到相关信息。

最佳答案

OpenGL allows you to store arbitrary data within a texture buffer that can be accessed in the vertex shader.

在 OpenGL ES 2.0 的某些实现中可能,但规范中没有要求(最大顶点纹理单元允许为 0),而且很多 OpenGL ES 2.0 only GPU 不需要支持一下。

同样,OpenGL ES 2.0 不支持浮点纹理(作为扩展除外),因此不能保证它也能正常工作。

最后,即使你能让它工作,从纹理为每个顶点加载一个 16 元素的 FP32 矩阵也会非常慢,所以不要这样做,除非这只是桌面 GPU 的玩具项目...(即,如果您的目标是使用 OpenGL ES 2.x 传送商业移动内容,那么这可能不是一个好主意)。

关于c++ - 如何使用 OpenGL ES2、GLSL、C++ 将模型矩阵数据缓冲到纹理中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40500107/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com