Game Engineer at Filament Games

Image RSS Feed Latest Screens
InvertY Random 6 Random Ice
Blog RSS Feed Report abuse Latest Blog: Billboarding with OpenGL

0 comments by onionman on Dec 12th, 2011

Standard Billboards

So billboarding in games development was something that always used to frighten me. I understood you were supposed to make an object always face the screen, but I didn't know an easy way to do that. I mean, we're you supposed to somehow calculate all the rotations needed by glRotatef() to make a picture always face you?

Anyway, eventually I found a solution with some online help that made the whole thing really easy, and I couldn't find it anywhere else, so I just thought I'd share.

Basically, you just take the current rotation matrix and invert it. Pretty simple! So here's some sample code to do that.

cpp code:
  GLfloat modelview[16];
  GLfloat tempModelView[16];
  glGetFloatv( GL_MODELVIEW_MATRIX, modelview );
  glGetFloatv( GL_MODELVIEW_MATRIX, tempModelView );
 
  modelview[0] = tempModelView[0];
  modelview[1] = tempModelView[4];
  modelview[2] = tempModelView[8];
  modelview[3] = 0.f;

  modelview[4] = tempModelView[1];
  modelview[5] = tempModelView[5];
  modelview[6] = tempModelView[9];
  modelview[7] = 0.f;

  modelview[8] = tempModelView[2];
  modelview[9] = tempModelView[6];
  modelview[10] = tempModelView[10];
  modelview[11] = 0.f;

//if you'd like, plug the position of the object into the transformation matrix, here I'm drawing a sun
  modelview[12] = sunPosition.position[0];
  modelview[13] = sunPosition.position[1];
  modelview[14] = sunPosition.position[2];
  modelview[15] = 1.f;

  glPushMatrix();
  glMultMatrixf(modelview);

  renderFlare(0.8f,0.8f,0.6f,1.f,5.f); //render the billboard

  glPopMatrix();
 

Semi-Billboards: Lasers

So a friend also asked me how I drew the lasers in our upcoming game Ensign 1. These really aren't anything special, and still need some work. Still, the basic idea is that these objects should be drawn facing the direction they are traveling, but their up vector should be pointing into the line of site of the viewer. Iunno if I explained that right, but anyway, here's the code I used to do it.
Basically you generate the forward, up, and right vectors for this object. And you need to ensure that the up vector is completely orthogonal to the forward vector.

cpp code:
glDepthMask(GL_FALSE);
Vector up,forward,right;
 
forward = lasers[index].velocity;
forward = forward*6.f;

up = lasers[index].position - parentModel->position;
up -= forward * forward.dot(forward,up); //ensure up vector is orthogonal to forward vector
up.normalize();

right = up.cross(forward,up);
right.normalize();
right = right*1.f;

GLfloat modelView[16];

modelView[0] = right[0];
modelView[1] = right[1];
modelView[2] = right[2];
modelView[3] = 0.f;

modelView[4] = up[0];
modelView[5] = up[1];
modelView[6] = up[2];
modelView[7] = 0.f;

modelView[8] = forward[0];
modelView[9] = forward[1];
modelView[10] = forward[2];
modelView[11] = 0.f;

modelView[12] = lasers[index].position[0];
modelView[13] = lasers[index].position[1];
modelView[14] = lasers[index].position[2];
modelView[15] = 1.f;

glPushMatrix();
  glMultMatrixf(modelView);

  glEnable(GL_BLEND);
  lasers[index].Draw(viewerPosition);
  glDisable(GL_BLEND);
glPopMatrix();

glDepthMask(GL_TRUE);
 

Some more tricks would might be that, when the up vector here is very small, the laser might appear flat, you might actually purposely add some forward vector to it to give it some depth. I haven't tried this myself yet though.

Anyway, enjoy!

Groups
Co-op Games & Mods DEVELOPER GROUP

Co-op Games & Mods DEVELOPER GROUP

Fans & Clans group with 23 members, must apply to join

This group is dedicated to promote and help Co-operative Games & Mods Developers. A place to co-operate and share, and also to get the attention our...

Only Human Studios

Only Human Studios

Developer with 9 members, must apply to join

We are a small indie developer of a team scattered between the states and the UK. Our focus is on building 3D action games that we've always wanted to...

Post comment Comments
OnlySolus
OnlySolus Dec 2 2010, 3:03pm says:

If you want a proper Zombie(ish) game you might want to try out Killing Floor, I was hoping RR to be more of a Survival (scavange/barricade/survive) game then a coop game and I think thats more what they are going for...
Afterall they are making a freeroam open world zombie survival :'D

+4 votes     reply to comment
onionman
onionman Jun 7 2011, 7:55pm replied:

Hey just curious, was this zombie game comment meant to be for me? How'd you know I love zombie games!!?

+4 votes     reply to comment
onionman
onionman Jun 25 2011, 1:36pm replied:

For zombie games I have project zomboid now :)

+3 votes     reply to comment
cork279
cork279 Dec 8 2010, 5:28pm replied:

Yep, that's exactly how were planning RR to be :D
And there'll be a large map!

+3 votes     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Level
Avatar
Avatar
Offline Since
Jul 10, 2014
Country
United States United States
Gender
Male
Member Watch
Track this member
Statistics
Activity Points
2,165
Rank
689 of 467,608
Watchers
4 members
Time Online
1 second
Comments
611
Site Visits
13,017
Profile Visitors
6,537 (2 today)
Contact
Private Message
Send Now
Email
Members Only
Madara_Killer
Madara_Killer friends since Sep 20, 2011
Bahlof
Bahlof friends since Apr 23, 2012
LiquidusWolf
LiquidusWolf friends since Jun 8, 2011
LandsHeer
LandsHeer friends since Jun 8, 2011
Venn
Venn friends since Oct 27, 2011
Rustybolts
Rustybolts friends since Jun 3, 2011
SolCommand
SolCommand friends since Sep 7, 2013
Slushy_
Slushy_ friends since Jun 5, 2012
DarkstarMatryx
DarkstarMatryx friends since Jun 25, 2012
RaineMaker1
RaineMaker1 friends since Jun 4, 2012