如何在 glm 中创建广告牌矩阵

Sys*_*dox 6 opengl glsl glm-math

如何使用 glm 从空间中的一点创建广告牌翻译矩阵?

dat*_*olf 6

只需将变换的左上角 3\xc3\x973 子矩阵设置为恒等即可。

\n
\n

更新:修复函数 OpenGL 变体:

\n
void makebillboard_mat4x4(double *BM, double const * const MV)\n{\n    for(size_t i = 0; i < 3; i++) {\n    \n        for(size_t j = 0; j < 3; j++) {\n            BM[4*i + j] = i==j ? 1 : 0;\n        }\n        BM[4*i + 3] = MV[4*i + 3];\n    }\n\n    for(size_t i = 0; i < 4; i++) {\n        BM[12 + i] = MV[12 + i];\n    }\n}\n\nvoid mygltoolMakeMVBillboard(void)\n{\n    GLenum active_matrix;\n    double MV[16];\n\n    glGetIntegerv(GL_MATRIX_MODE, &active_matrix);\n\n    glMatrixMode(GL_MODELVIEW);\n    glGetDoublev(GL_MODELVIEW_MATRIX, MV);\n    makebillboard_mat4x4(MV, MV);\n    glLoadMatrixd(MV);\n    glMatrixMode(active_matrix);\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Sys*_*dox 5

mat4 billboard(vec3 position, vec3 cameraPos, vec3 cameraUp) {
    vec3 look = normalize(cameraPos - position);
    vec3 right = cross(cameraUp, look);
    vec3 up2 = cross(look, right);
    mat4 transform;
    transform[0] = vec4(right, 0);
    transform[1] = vec4(up2, 0);
    transform[2] = vec4(look, 0);
    // Uncomment this line to translate the position as well
    // (without it, it's just a rotation)
    //transform[3] = vec4(position, 0);
    return transform;
}
Run Code Online (Sandbox Code Playgroud)