Monday, December 26, 2016

opengl - Rotate billboard towards camera


I'm trying to create a particle system in OpenGL, and as such I need to use billboards.



I need these billboards to face the camera at all times.


The tutorials I've been following only rotated the billboard on 1 axis, such as billboards being used as grass or plants, but I need a method of rotating the quad to always face the camera, even when looking down on it.




The Original Scene: Prior to rotation


Desired Scene: Desired Scene


While searching for an answer, I only really found the technique of turning the mat3 component of the camera's view/model matrix back to an identity matrix. This isn't quite what I need, as the point's location in space never change then.


Incorrect Scene: Incorrect Rotation




How can I calculate a new rotation matrix that will successfully rotate a quad always towards the camera, or modify an existing matrix?


In the geometry shader I have access to the camera's position, the perspective matrix, and the view/model matrix.



I didn't include any shader code because it is really trivial; I create vertices that are an offset of the desired point's position based on the desired size. So if the quad should be 50 x 50, the vertex is the position offset by 25 on X and Y, multiplied by the Camera's Perspective x View matrix.




Solution:


mat4 VP = pMatrix * vMatrix;       
vec3 CameraRight = vec3(vMatrix[0][0], vMatrix[1][0], vMatrix[2][0]);
vec3 CameraUp = vec3(vMatrix[0][1], vMatrix[1][1], vMatrix[2][1]);

vec3 Pos = gl_in[0].gl_Position.xyz; // The desired point for the billboard

vec4 v1 = VP * vec4(Pos + CameraRight * 0.5 * Size + CameraUp * -0.5 * Size, 1.0);

vec4 v2 = VP * vec4(Pos + CameraRight * 0.5 * Size + CameraUp * 0.5 * Size, 1.0);
vec4 v3 = VP * vec4(Pos + CameraRight * -0.5 * Size + CameraUp * -0.5 * Size, 1.0);
vec4 v4 = VP * vec4(Pos + CameraRight * -0.5 * Size + CameraUp * 0.5 * Size, 1.0);

Finished



Answer



I think this article is what you are looking for.


Article from opengl tutorials


Basically, you'll have to find the matrix camera rotation and apply a derived form to your mesh.


Hope it helps!



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...