问题描述
我已经阅读了一些有关GLSL的教程.在某些位置属性是某些vec3中的vec4.我知道矩阵运算需要vec4,但是是否值得发送其他元素?发送vec3并随后在着色器vec4(position,1.0)中投射是否更好?内存中的数据更少-会更快吗?还是我们应该打包一个额外的元素以避免转换?
I have read some tutorials about GLSL.In certain position attribute is a vec4 in some vec3.I know that the matrix operations need a vec4, but is it worth to send an additional element?Isn't it better to send vec3 and later cast in the shader vec4(position, 1.0)?Less data in memory - it will be faster? Or we should pack an extra element to avoid casting?
任何提示都应该更好吗?
Any tips what should be better?
layout(location = 0) in vec4 position;
MVP*position;
或
layout(location = 0) in vec3 position;
MVP*vec4(position,1.0);
推荐答案
对于顶点属性,这无关紧要.缺少第4个组件时,它会自动扩展为1.0
.
For vertex attributes, this will not matter. The 4th component is automatically expanded to 1.0
when it is absent.
也就是说,如果将3维顶点属性指针传递给4维向量,则GL将为您用1.0
填充W
.我一直沿用这条路线,这样可以避免在位置上进行矩阵乘法时显式地编写vec4 (...)
,并且还可以避免浪费存储第4个分量的内存.
That is to say, if you pass a 3-dimensional vertex attribute pointer to a 4-dimensional vector, GL will fill-in W
with 1.0
for you. I always go with this route, it avoids having to explicitly write vec4 (...)
when doing matrix multiplication on the position and it also avoids wasting memory storing the 4th component.
顺便说一下,这也适用于2D坐标.传递给vec4
属性的2D坐标变为vec4 (x, y, 0.0, 1.0)
.一般规则是:将所有缺少的组件替换为0.0
,除了W
替换为1.0
.
This works for 2D coordinates too, by the way. A 2D coordinate passed to a vec4
attribute becomes vec4 (x, y, 0.0, 1.0)
. The general rule is this: all missing components are replaced with 0.0
except for W
, which is replaced with 1.0
.
但是,对于在这种情况下不了解GLSL行为的人来说,这可能会造成混淆.我想这就是为什么大多数教程从不涉及这个主题的原因.
However, to people who are unaware of the behavior of GLSL in this circumstance, it can be confusing. I suppose this is why most tutorials never touch on this topic.
这篇关于着色器位置vec4或vec3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!