只需将变换的左上角 3\xc3\x973 子矩阵设置为恒等即可。
\nvoid 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}\nRun Code Online (Sandbox Code Playgroud)\n
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)