简体   繁体   English

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

[英]How can I buffer model matrix data into texture using OpenGL ES2, GLSL, C++?

According to this answer , OpenGL allows you to store arbitrary data within a texture buffer that can be accessed in the vertex shader. 根据此答案 ,OpenGL允许您将任意数据存储在可以在顶点着色器中访问的纹理缓冲区中。

I created a Float32Array of size 4096 * 256 * 4 which contains the world matrix for every model (enough for ~256k models). 我创建了一个Float32Array,大小为4096 * 256 * 4,其中包含每个模型的世界矩阵(足以容纳约256k个模型)。 Each model has a modelIndex attribute which is used to read its matrix from the texture. 每个模型都有一个modelIndex属性,该属性用于从纹理读取其矩阵。 Then at each frame, gl.texSubImage2D the entire texture and draw as many as possible in each draw call. 然后在每一帧,gl.texSubImage2D整个纹理,并在每个绘制调用中绘制尽可能多的。

An example scenario: 一个示例场景:
I have a bunch of unique models who share a common shader, their vertex positions packed into a single VBO. 我有一堆独特的模型,它们共享一个公共着色器,它们的顶点位置打包在一个VBO中。 I'd like to update each model with a unique model matrix fetched from the texture buffer, then draw these in a single glDrawElements() call. 我想用从纹理缓冲区获取的唯一模型矩阵更新每个模型,然后在单个glDrawElements()调用中绘制它们。

Some very rough pseudo code: 一些非常粗糙的伪代码:

// 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(...);

Could someone please share an example of how to implement this? 有人可以分享一个实现此示例的例子吗? I'm struggling to find relevant information online. 我正努力在网上找到相关信息。

OpenGL allows you to store arbitrary data within a texture buffer that can be accessed in the vertex shader. OpenGL允许您将任意数据存储在可以在顶点着色器中访问的纹理缓冲区中。

It's possible in some implementations of OpenGL ES 2.0, but it's not required in the specification (max vertex texturing units is allowed to be 0), and so many OpenGL ES 2.0 only GPUs don't support it. 在OpenGL ES 2.0的某些实现中是可能的 ,但在规范中并不需要(最大顶点纹理单位允许为0),因此许多OpenGL ES 2.0仅GPU不支持它。

Similarly, OpenGL ES 2.0 doesn't support floating point texturing (except as an extension) so that isn't guaranteed to work either. 同样,OpenGL ES 2.0不支持浮点纹理化(作为扩展除外),因此也不保证它们也可以工作。

Finally, even if you could get it working, loading a 16 element FP32 matrix from a texture for every vertex is going to be insanely slow, so just don't do it unless this is just a toy project for a desktop GPU ... (ie if your target is shipping commercial mobile content using OpenGL ES 2.x then this is probably not a good idea). 最后,即使您可以使用它,从纹理为每个顶点加载一个16元素的FP32矩阵的速度也会异常缓慢,因此不要这样做,除非这只是桌面GPU的玩具项目... (即,如果您的目标是使用OpenGL ES 2.x发行商业移动内容,那么这可能不是一个好主意)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM