View Full Version : Billboarding???

I was wondering how to use the billboarding effect (where a certain poly always faces the viewer), i am using the LookAt function. So i dont really know the roation values...so does anybody know how to make a certain polygon always face the viewer, while using the LookAt function?

P.S. any code would be greatly appreciated http://www.opengl.org/discussion_boards/ubb/biggrin.gif

I snapped this some messages back. I found this very helpful information. The answer to your problem lies within. If there is a function to resolve the camera pitch, bank and while using gluLookAt(), I would like to know.

Alternate option - you could use the details below to write your ownLookAT().

-----------------------------------

sine, cosine and tangent are 3 trigonometry functions which relate angles and lengths of sides of a right triangle and are defined this way

T = the angle of interest

A = side of triangle Adjacent to T

O = side of triangle Opposide T

H = the hypotenuse (sp?) (angle opposite the right angle).

```````````b`````

``````````/|`````

`````````/`|`````

````````/``|`````

````H``/```|`````

``````/````|`O```

`````/`````|`````

````/``````|`````

```/`)T````|`````

`a---------+`c```

``````A``````````

cos(T) = A/H

sin(T) = O/H

tan(T) = O/A

These functions are useful for all sorts of things. For example if you want to break an arbitrary vector up into x and y components, you find the angle the vector makes with the x-axis of your coordinate system and H = length of the vector and you get:

x_component_of_H = A = H*cos(T)

y_component_of_H = O = H*sin(T)

---------------------------------

I had to modify the drawing from the original post. Sorry who posted this, I cannot remember.

a is your camera

b is your object

c.x = object.x; //(a)

c.y = camera.y; //(b)

Return T to get your angle. This is in 2D only you can fix for 3D in the same fashion. Once you have created the right angle use pythagoras theorm to solve the whole triangle, assuming you have the x,y coords for a & b.

2 create the billboard effect (*nearly forgot http://www.opengl.org/discussion_boards/ubb/smile.gif send the inverted angle of your camera to glRotated(-angle , 0.0f, 1.0f, 0.0f);

this rotation is across the X,Z axis.

Cheers,

DanS

[This message has been edited by dans (edited 03-09-2000).]

Here's some linear Algerbra for you! whee http://www.opengl.org/discussion_boards/ubb/smile.gif

(in code so its actually useful!!)

Vector Vo, Vn, Vr;

double VoMag, VrMag;

// this will get the vector between camera and object you want to billboard

Vo[0] = Obj->GetX() - Camera->GetX();

Vo[1] = Obj->GetY() - Camera->GetY();

Vo[2] = Obj->GetZ() - Camera->GetZ();

VoMag = sqrt(Vo[0]*Vo[0] + Vo[1]*Vo[1] + Vo[2]*Vo[2]);

// distance between object and camera (magnitude of the vector)

Vn[0] = 0;

Vn[1] = 0;

Vn[2] = 1;

// this is a Normal vector about the vector I want to be billboarded (Z-Axis in this example)

Vr = Vn ^ Vo;

// Cross Product give you the perpendicular axis about which you want to rotate to always face the camera coords you just used

// dont forget to Normalize the size! so you can use it later!

VrMag = sqrt(Vr[0]*Vr[0] + Vr[1]*Vr[1] + Vr[2]*Vr[2]);

if(VrMag != 0)

{

Vr[0] = Vr[0]/VrMag;

Vr[1] = Vr[1]/VrMag;

Vr[2] = Vr[2]/VrMag;

}

// ok we have the vector to rotate about, now what degree do we rotate by?

// this is can be found in any linear algerbra book

theta = acos((Vn * Vo)/(VoMag)) * 180 /PI;

// of course the Mag of the Z-Axis is 1, so I left it out of this equation (who wants to multiply by 1?)

there you go!

just use

glRotated(theta, Vr[0], Vr[1], Vr[2]);

and you will always face the camera!

(the object billboarded will rotate about the Z on its own... but i am still fixing that.. http://www.opengl.org/discussion_boards/ubb/smile.gif

Powered by vBulletin® Version 4.2.5 Copyright © 2019 vBulletin Solutions Inc. All rights reserved.