GLSL每顶点固定大小数组 [英] GLSL per vertex fixed size array

查看:222
本文介绍了GLSL每顶点固定大小数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在桌面GLSL中是否可以将固定大小的浮点数作为属性传递给顶点着色器?如果是,如何?

Is it possible in desktop GLSL to pass a fixed size array of floats to the vertex shader as an attribute? If yes, how?

我想为每个角色动画的顶点权重,所以我想在顶点着色器中有如下的东西:

I want to have per vertex weights for character animation so I would like to have something like the following in my vertex shader:

attribute float weights[25];

如何填充我的C ++& OpenGL程序?我在另一个问题,我可以得到数组属性的属性位置,然后只是添加到该位置的索引。有人可以给我一个例子,对我的漂亮的大阵列?

How would I fill the attribute array from my C++ & OpenGL program? I have seen in another question that I could get the attribute location of the array attribute and then just add the index to that location. Could someone give an example on that for my pretty large array?

感谢。

推荐答案

现在几乎没有任何硬件会存在属性float weights [25]; 编译。虽然着色器可以具有属性数组,但每个数组索引都表示一个新的属性索引。并且在所有硬件当前存在,属性索引的最大数量是... 16.你需要25,这是的权重。

On pretty much no hardware that exists currently will attribute float weights[25]; compile. While shaders can have arrays of attributes, each array index represents a new attribute index. And on all hardware the currently exists, the maximum number of attribute indices is... 16. You'd need 25, and that's just for the weights.

现在,你可以轻松地通过记住你可以使用 vec4 属性来缓解这个问题。因此,将每四个数组元素存储在一个属性中。你的数组将是属性vec4 weights [7]; 这是可行的。你的体重获取逻辑必须改变当然。

Now, you can mitigate this easily enough by remembering that you can use vec4 attributes. Thus, you store every four array elements in a single attribute. Your array would be attribute vec4 weights[7]; which is doable. Your weight-fetching logic will have to change of course.

即使如此,你似乎没有采取这个实际的意义用于顶点数据。每个属性表示顶点数据的组件。渲染调用的每个顶点将具有相同的数据量;

Even so, you don't seem to be taking in the ramifications of what this would actually mean for your vertex data. Each attribute represents a component of a vertex's data. Each vertex for a rendering call will have the same amount of data; the contents of that data will differ, but not how much data.

为了执行你的建议,你的每个顶点网格将需要25个浮标描述重量。即使这是作为规范化的无符号字节存储,仍然至少25个额外字节的数据。好多啊。特别是考虑到对于大多数顶点,大多数这些值将是0.即使在最坏的情况下,你可能会看到影响单个顶点的6-7个骨骼。

In order to do what you're suggesting, every vertex in your mesh would need 25 floats describing the weight. Even if this was stored as normalized unsigned bytes, that's still 25 extra bytes worth of data at a minimum. That's a lot. Especially considering that for the vast majority of vertices, most of these values will be 0. Even in the worst case, you'd be looking at maybe 6-7 bones affecting an single vertex.

在顶点着色器中通常执行换肤的方式是将影响单个顶点的骨骼数限制为四个。这样,你不使用属性数组;你只需使用一个 vec4 属性的权重。当然,你现在还需要说明哪个骨骼与哪个重量相关联。因此,您有第二个 vec4 属性,用于指定该体重的骨骼索引。

The way skinning is generally done in vertex shaders is to limit the number of bones that affects a single vertex to four. This way, you don't use an array of attributes; you just use a vec4 attribute for the weights. Of course, you also now need to say which bone is associated with which weight. So you have a second vec4 attribute that specifies the bone index for that weight.

您只需要占用2个额外的属性(在大小方面可以是无符号字节)。对于绝大多数的顶点,你永远不会注意到,因为大多数顶点只受1-3骨骼的影响。少数使用4,少使用5+。在这些情况下,您只是切掉最低权重,并按比例重新计算其他权重。

This strikes a good balance. You only take up 2 extra attributes (which can be unsigned bytes in terms of size). And for the vast majority of vertices, you'll never even notice, because most vertices are only influenced by 1-3 bones. A few uses 4, and fewer still use 5+. In those cases, you just cut off the lowest weights and recompute the weights of the others proportionately.

这篇关于GLSL每顶点固定大小数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆