Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create billboard matrix in glm

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

like image 636
SystemParadox Avatar asked Mar 10 '13 18:03

SystemParadox


People also ask

What is view matrix and projection matrix?

The model, view and projection matrices are three separate matrices. Model maps from an object's local coordinate space into world space, view from world space to camera space, projection from camera to screen.

How does OpenGL matrix work?

Each coordinate in OpenGL actually has four components, X, Y, Z, and W. The projection matrix sets things up so that after multiplying with the projection matrix, each coordinate's W will increase the further away the object is. OpenGL will then divide by w: X, Y, Z will be divided by W.

What is view matrix?

The View Matrix. Like the model matrix, the view matrix can contain translation and rotation components - but instead of transforming an individual object, it transforms everything! It moves your geometry from world space to view space - that is, a coordinate system with your desired viewpoint at the origin.


2 Answers

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;
}
like image 122
SystemParadox Avatar answered Sep 19 '22 13:09

SystemParadox


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);
}
like image 37
datenwolf Avatar answered Sep 18 '22 13:09

datenwolf