How to create billboard matrix in glm

前端 未结 2 551
北荒
北荒 2021-02-10 03:35

How to create a billboard translation matrix from a point in space using glm?

2条回答
  •  后悔当初
    2021-02-10 03:59

    Just set the upper left 3×3 submatrix of the transformation to identity.


    Update: Fixed function OpenGL variant:

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

提交回复
热议问题