質問

In a OpenGL fragment-shader, I need to transform a vec2 that represents a xy pair I need to another coordinate space.

I got the mat4 transformation-matrix for this, but can simply transform by:

vec2 transformed = (tansformMat4 * vec4(xy, 0.0f)).xy ?

I guess the result would not be correct, since the the 0s would get claculated into the x and y part.

役に立ちましたか?

解決

4D vectors and Matrices are used for 3D Affine Transformations.

You can use the equivalent 2D Affine Transformation which is a 3D vector with the z-value to 1 (and then discarded):

vec3 transformed3d = transformMat3 * vec3(x, y, 1.0);
vec2 transformed2d(transformed3d.x, transformed3d.y);

Or alternatively, you can use can use the aforementioned 3D Affine Transformation with a 4D vector and Matrix by setting the z-value to zero and the w-value to 1 for homogeneous coordinates and then using the x and y values directly in a parallel projection:

vec4 transformed4d = transformMat4 * vec3(x, y, 0.0, 1.0);
vec2 transformed2d(transformed4d.x, transformed4d.y);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top