Sign in to follow this  
Monkan

3D Billboarding Woes

Recommended Posts

Hi,

I'm making a 3d particle system and am getting a bit confused by my billboarding math.
So far I've got this:

[code]

Vector3f vec = cGraphicsManager::Instance().GetCamera()->GetPosition() - mParticles[i].mPosition;
vec .Normalise();
float angle = atan2(vec .z, vec .x);

angle = cMathLookUps::Instance().RadiansToDegrees(angle);

glRotatef(-angle + 90, 0, 1, 0);

[/code]


This works for a 2D rotation towards the camera but then I get confused after that. I've read various websites which have confused me further. For example lighthouse3d's tutorial - [url="http://www.lighthouse3d.com/opengl/billboarding/index.php?billSphe"]http://www.lighthouse3d.com/opengl/billboarding/index.php?billSphe[/url]

Is there not a simple way of just adding to my existing code which makes sense to me to tilt the image as well as rotate it. It seems like I should just be able to do the calculation I've already done again somehow to calculate the rotation around the other axis.

Please help.

Cheers.

Share this post


Link to post
Share on other sites
it's just a matter of taking the distance on the X/Z plane that you've generated your Y-axis angle from and doing an atan2 with that and the vec.y

[code]

Vector3f vec = cGraphicsManager::Instance().GetCamera()->GetPosition() - mParticles[i].mPosition;
float angle_x, angle_y, xzdist;

vec .Normalise();

xzdist = sqrt(vec.x * vec.x + vec.z * vec.z);
angle_x = cMathLookUps::Instance().RadiansToDegrees(atan2(vec.y, xzdist));
angle_y = cMathLookUps::Instance().RadiansToDegrees(atan2(vec.z, vec.x));


glRotatef(-angle_y + 90, 0, 1, 0);
glRotatef(angle_x, 1, 0, 0);

[/code]

there might be a better way that doesn't use sqrt, somehow extracting out the vec.y from the vec itself to get the length of XZ, especially since you're already calling a sqrt from inside the Normalize, and having many particles will definitely slow it down after a point.. You might also need to invert and add/sub 90 degrees from angle_x.. and I can't remember if you rotate X first or last, you might need to mess with that too.

but that's basically the jist of it.

Share this post


Link to post
Share on other sites
This would be much more efficiently done in your vertex shader rather than on the CPU. Stick "billboarding vertex shader" into Google for lots of useful hints and techniques.

Share this post


Link to post
Share on other sites
Thanks for the replies,

Thats worked radioteeth, thanks. I know its not the most optimal method but since it works for now I'm happy.

And mhagain I know, thats the next step, I'm in the process at the moment of teaching myself shader programming but I'm trying not to just copy and paste things off websites and trying to really learn the theory behind it so its quite a slow process.

Cheers
x

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this