【发布时间】:2016-07-23 20:08:20
【问题描述】:
我正在查看raytracer 的源代码。在第 145 行名为 algebra3.cpp 的文件中,我们可以看到这个函数:
inline vec2 operator * (const mat3& a, const vec2& v) {
vec3 av;
av.n[VX] = a.v[0].n[VX]*v.n[VX] + a.v[0].n[VY]*v.n[VY] + a.v[0].n[VZ];
av.n[VY] = a.v[1].n[VX]*v.n[VX] + a.v[1].n[VY]*v.n[VY] + a.v[1].n[VZ];
av.n[VZ] = a.v[2].n[VX]*v.n[VX] + a.v[2].n[VY]*v.n[VY] + a.v[2].n[VZ];
return av;
}
它应该返回一个vec2 对象而不是它返回一个vec3。这是为什么呢?
【问题讨论】:
-
顶部的源代码状态:
A vector of one dimension (2d, 3d, or 4d) can be cast to a vector of a higher or lower dimension. If casting to a higher dimension, the new component is set by default to 1.0, unless a value is specified: vec3 a(1.0, 2.0, 3.0 ); vec4 b( a, 4.0 ); // now b == {1.0, 2.0, 3.0, 4.0}; When casting to a lower dimension, the vector is homogenized in the lower dimension. E.g., if a 4d {X,Y,Z,W} is cast to 3d, the resulting vector is {X/W, Y/W, Z/W}. It is up to the user to insure the fourth component is not zero before casting. -
它指出向量可以转换为不同类型的更高/更低维度
-
可能是一个错误,可能是故意的。似乎编译器从 vec3 转换为 vec2。
-
或者它们可能是某种类层次结构,而 vec2 将是 vec3 的父类,这将允许这种转换。
-
我看到了,我正在阅读的版本没有任何 cmets,因为我是从另一个我不记得的页面下载的,但功能几乎相同,所以我链接了那个。
标签: c++ vector raytracing