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

时间:2013-03-10 18:14:07

标签: opengl glsl glm-math

如何使用glm从空间中创建广告牌平移矩阵?

2 个答案:

答案 0 :(得分:3)

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;
}

答案 1 :(得分:2)

只需将转换的左上角3×3子矩阵设置为标识。


更新:固定功能OpenGL变体:

void makebillboard_mat4x4(double *BM, double const * const MV)
{
    for(size_t i = 0; i < 3; i++) {

        for(size_t j = 0; j < 3; j++) {
            BM[4*i + j] = i==j ? 1 : 0;
        }
        BM[4*i + 3] = MV[4*i + 3];
    }

    for(size_t i = 0; i < 4; i++) {
        BM[12 + i] = MV[12 + i];
    }
}

void mygltoolMakeMVBillboard(void)
{
    GLenum active_matrix;
    double MV[16];

    glGetIntegerv(GL_MATRIX_MODE, &active_matrix);

    glMatrixMode(GL_MODELVIEW);
    glGetDoublev(GL_MODELVIEW_MATRIX, MV);
    makebillboard_mat4x4(MV, MV);
    glLoadMatrixd(MV);
    glMatrixMode(active_matrix);
}