Convert glm::vec4 to glm::vec3

2020-07-02 07:42发布

How do I convert a glm::vec4 to a glm::vec3?

Only x, y, z is required- the w component can be dropped.

In GLSL this can be done with .xyz[1], but in glm this results in a compile error:

error: 'glm::vec4' has no member named 'xyz'

[1] http://en.wikibooks.org/wiki/GLSL_Programming/Vector_and_Matrix_Operations#Components

标签: glm-math
2条回答
混吃等死
2楼-- · 2020-07-02 08:09

Swizzling is not enabled by default in glm as it uses macros which might cause naming conflicts. To enable it:

#define GLM_SWIZZLE
#include <glm/glm.hpp>

In glm, swizzling is done using functions:

vec3 v3 = v4.xyz();

See: http://glm.g-truc.net/0.9.2/api/a00002.html

查看更多
beautiful°
3楼-- · 2020-07-02 08:18

Just use vec3 constructor. Here, on 0.9.5 branch:

glm::vec4 v4(1, 2, 3, 4);
glm::vec3 v(v4);
printf("%s\n", glm::to_string(v).c_str());

and gives this output

fvec3(1.000000, 2.000000, 3.000000)

查看更多
登录 后发表回答