Question

Why this throws an compilation error: no matching function for call to ‘cross(glm::vec4&, glm::vec4&)’

glm::vec4 a;
glm::vec4 b;
glm::vec4 c = glm::cross(a, b);

but it works fine for vec3?

Was it helpful?

Solution

There is no such thing as a 4D vector cross-product; the operation is only defined for 3D vectors. Well, technically, there is a seven-dimensional vector cross-product, but somehow I don't think you're looking for that.

Since 4D vector cross-products aren't mathematically reasonable, GLM doesn't offer a function to compute it.

OTHER TIPS

What do your vec4's represent? Like Nicol said, cross products are only for 3D vectors. The cross product operation is used to find a vector that is orthogonal to the two input vectors. So if your vec4's represent 3D homogeneous vectors in the form {x, y, z, w}, then the w-component doesn't matter to you; You could simply ignore it.

A workaround could go as follows:

vec4 crossVec4(vec4 _v1, vec4 _v2){
    vec3 vec1 = vec3(_v1[0], _v1[1], _v1[2]);
    vec3 vec2 = vec3(_v2[0], _v2[1], _v2[2]);
    vec3 res = cross(vec1, vec2);
    return vec4(res[0], res[1], res[2], 1);
}

Simply turn your vec4's into vec3's, perform the cross product, then add a w-component of 1 back into it.

The generalization of the cross product is the wedge product, and the wedge product of two vectors is a 2-form, also known as a bivector.

In 3-space, the 2-form kinda looks like a vector, but it behaves quite differently. Suppose we have two non-collinear vectors tangent to a surface (aka tangent vectors). By taking the cross product of these vectors, we have a 2-form that represents the tangent plane. We can also represent that tangent plane by the vector normal to that plane (aka the normal vector). But the tangent and normal vectors are transformed differently, i.e. the normal vector is transformed by the inverse transpose of the matrix used to transform the tangent vectors.

In 4-space, the 2-form resulting from the wedge product of two vectors also represents the plane that contains the two vectors (this is also true in N-space). Similarly to the case in 3-space, we can have an alternate interpretation of that plane, but in 4-space, the complement to a plane is not a 4-vector, but another plane, both of which are represented with 6 components, not 4.

c1 * e1^e2 + c2 * e1^e3 + c3 * e1^e4 + c4 * e2^e3 + c5 * e2^e4 + c6 * e3^e4

Since glm doesn't provide the API for wedge products, you will have to roll your own. You can easily work out the algebra for the wedge product with two simple rules:

(1) ei ^ ei = 0
(2) ei ^ ej = -ej ^ ei

where the ei and ej are the component vectors (bases) of the vector space, e.g.

[a b c d] --> a * e1 + b * e2 + c * e3 + d * e4

The 7-dimensional vector referred to in a previous post is the geometric product of two vectors, which uses ei^ei=1 instead of rule (1) above, and is like a meld of the dot and cross products (or complex multiplication), which is more than what you want. For more information, https://en.wikipedia.org/wiki/Exterior_algebra or https://en.wikipedia.org/wiki/Geometric_algebra .

There is more shortcut way to calculate cross product using glm's GLM_SWIZZLE.

Just do #define GLM_SWIZZLE before inclusion of any glm file. It's also helpful for lots of other tricks.

glm::vec4 a;
glm::vec4 b;
glm::vec4 c = glm::vec4( glm::cross( glm::vec3( a.xyz ), glm::vec3( b.xyz ) ), 0 );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top