I have implemented a spherical billboard
via the vertex shader by applying additional rotations to the object being rendered. Basically, that means that I first compute the horizontal and vertical angles an object must be rotated to "face the camera", generating rotation matrices for them and then just updating gl_Position
accordingly. For simplicity, assume it code looks like this:
gl_Position = projectionMatrix * viewMatrix * translationMatrix * rotationLeftRight * rotationUpDown * vec4(inVertexPosition, 1.0);
Low, let's since we are doing billboards, the object is a textued quad for now.
Consider for example this well-know face of evil which can be seen below. Let's say its located at (0,0,0)
while our camera is at (0,0,1)
, looking towards it, aka into (0,0,-1)
. We can see that it is being rendered correctly (never mind the texture filter
Now, moving the camera to the left and updwards, from (0,0,-1)
to something like (1,1,0)
for example. We rotate the camera back towards more or less the origin and we arrive at the next image:
We see that the object remains facing towards us, while the terrain, rendered with a standard, non-billboarding shader, obviously does not (never mind that the object appears a little larger, I was not too precise when taking these example shots).
moving the camera but only rotating it to the right to face the edge of the map, the perspective matrix kicks in, "destroying" our billboarding sprite's effect, as seen in below:
Illustrating the problem in an easier to see manner, I have created this handy image, in which I have set transparent fragments to red, thus making the quad visible
Mathematically speaking, everything going on here is obviously correct, both the spherical billboard as well as the perspective matrix is doing its job. From a sprite's perspective however, said matrix is introducing a unwanted rotation once the object is no longer the center. Basically, it has tilted to the left as the camera went looking to the right, as that is the "job" of the perspective matrix, creating perspective. However, rendering the billboards utterly separate with a different perspective matrix "detaches" them from the rest of the scene and creates a whole new set of challenges, for example in regards to the depth buffer.
One possiblity to fix this would be to apply an additional rolling-rotation based on the horizontal viewing angle difference but when that thought came to my mind, I decided should stop and first ask around.My question: I am approaching this in an entirely incorrect way?