Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vec3.transformMat4 performs w-division; vec2.transformMat4 does not #415

Open
ghost opened this issue Jan 22, 2021 · 0 comments
Open

vec3.transformMat4 performs w-division; vec2.transformMat4 does not #415

ghost opened this issue Jan 22, 2021 · 0 comments

Comments

@ghost
Copy link

ghost commented Jan 22, 2021

As pointed out here #279 (comment), vec4.transformMat4 doesn't do w-division, and the user is responsible for doing it:

gl-matrix/src/vec4.js

Lines 478 to 488 in 21b745f

export function transformMat4(out, a, m) {
let x = a[0],
y = a[1],
z = a[2],
w = a[3];
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
return out;
}

For vec3.transformMat4 (assumes w=1), the W-division is done, presumably because the w-component isn't returned, so it's done because the user couldn't do it:

gl-matrix/src/vec3.js

Lines 505 to 515 in 21b745f

export function transformMat4(out, a, m) {
let x = a[0],
y = a[1],
z = a[2];
let w = m[3] * x + m[7] * y + m[11] * z + m[15];
w = w || 1.0;
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
return out;
}

For vec2.transformMat4 (assumes z=0 and w=1), however, the w-division is not done. Unfortunately, the user can't do it either, because the w-component isn't returned (so the user must compute w themselves, which defeats the purpose):

gl-matrix/src/vec2.js

Lines 442 to 448 in 21b745f

export function transformMat4(out, a, m) {
let x = a[0];
let y = a[1];
out[0] = m[0] * x + m[4] * y + m[12];
out[1] = m[1] * x + m[5] * y + m[13];
return out;
}

I'd like to see this fixed or a new function added for this purpose, but it's obviously a breaking change if this function was modified. It could also help to have a function to compute w (maybe a homogenous-dot-product already exists for this?), then the w division can be avoided.
A potential use-case for this can be found in maplibre (which would already benefit from the vec3 variant, but could be optimized further).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants