着色器位置 vec4 或 vec3 [英] Shader position vec4 or vec3

查看:50
本文介绍了着色器位置 vec4 或 vec3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于 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会为你填入W1.0.我总是走这条路线,它避免了在对位置进行矩阵乘法时显式编写 vec4 (...) 并且还避免浪费存储第四个组件的内存.

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 属性的二维坐标变为 vec4 (x, y, 0.0, 1.0).一般规则是这样的:除了W,所有缺失的组件都替换为0.0,替换为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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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