# Billboards

This code calculates the offsets to add to a center point to draw a billboard, by calculating the orientation of the user's screen ("eye space" in OpenGL terms) but returning the results in the OpenGL local coordinates that you have to use to draw.

# The Code

```// This routine returns up to 3 camera directions: which way is "right", "up" and which way is the camera pointing ("look")
// in OpenGL coordinates.  In other words, this is which way the user's SCREEN is pointing in OpenGL "local" coordinates.
// (If the user is facing true north at the origin and is not rolled, these functiosn would be trivially easy because
// right would be 1,0,0, up would be 0,1,0 and look would be 0,0,1.  (NOTE: the look vector points TO the user, not
// FROM the user.)
//
// To draw a billboard centered at C, you would use these coordinates:
//
// c-rgt+up---c+rgt+up
// |                 |
// |        C        |
// c-rgt-up---c+rgt-up
//
static void camera_directions(
float * out_rgt,		// Any can be NULL
float * out_up ,
float * out_look)
{
float m;
glGetFloatv(GL_MODELVIEW_MATRIX, m);

// Roughly speaking, a modelview matrix is made up more or less like this:
// [ EyeX_x EyeX_y EyeX_z    a
//   EyeY_x EyeY_y EyeY_z    b
//   EyeZ_x EyeZ_y EyeZ_z    c
//   um don't look down here ]
// where a, b, c are translations in _eye_ space.  (For this reason, a,b,c is not
// the camera location - sorry!)

if(out_rgt) {
out_rgt = m;
out_rgt = m;
out_rgt = m;
}
if(out_up) {
out_up = m;
out_up = m;
out_up = m;
}
if(out_look) {
out_up = m;
out_up = m;
out_up = m;
}
}
```

# Sample Usage

How to draw the billboard? Something like this would work:

```void draw_billboard(float x, float y, float z)
{

float r, u;
camera_directions(r,u,NULL);
glBegin(GL_LINE_LOOP);
glVertex3f(x-r-u,y-r-u,z-r-u);
glVertex3f(x+r-u,y+r-u,z+r-u);
glVertex3f(x+r+u,y+r+u,z+r+u);
glVertex3f(x-r+u,y-r+u,z-r+u);
glEnd();
}```

Note that this is just an example; since camera_directions reads the GL matrices, you would want to call it only once per draw callback, not once per billboard!!!

# Usage For "Look" Vectors

One possible usage of the look vector is to measure alignment between the camera and drawing and then fade. For example, to fade a prop disc you could take the dot product of the look vector and the normal vector of the prop disc - as the dot product becomes zero, fade the prop disc.