PDA

View Full Version : Having quads always face the camera



Structural
12-02-2002, 06:10 AM
At the moment I'm working on a neat little particle engine. I only have one little problem: the Quads don't alwas face the camera.

For example, when I start the engine the quads nicely face the camera with their entire surface, but when I move around the engine they become thinner, and thinner, up untill they are no more than a thin line (or invisible).

How would I make the quads face the camera no matter the rotation? Any example code is welcome.

Thank you!

Kent767
12-02-2002, 06:17 AM
assuming you know your camera's rotation.. all you have to do is do a negative rotation for the particles... (this makes them face the camera if it were a vector, not really right to the camera... but for most gaming purposes you can't tell the difference)

Kent

M/\dm/\n
12-02-2002, 06:32 AM
You should look for billboarding tutorials, although they work best with vertexshaders http://www.opengl.org/discussion_boards/ubb/smile.gif

Structural
12-02-2002, 06:39 AM
Originally posted by Kent767:
assuming you know your camera's rotation.. all you have to do is do a negative rotation for the particles... (this makes them face the camera if it were a vector, not really right to the camera... but for most gaming purposes you can't tell the difference)

Kent

http://www.opengl.org/discussion_boards/ubb/smile.gif
OK, How would I do this? I don't know my cams rotation since I use GLULOOKAT.
I know how I can get the current view matrix (glGetFloatv(GL_MODELVIEW_MATRIX, array)) but which values in that matrix are the rotation (or how would I get them)?


MadMan, I read something about billboarding in the gamedev book, but it seems to lack a decent snippet (or anything that I can understand)


[This message has been edited by Structural (edited 12-02-2002).]

Kent767
12-02-2002, 06:48 AM
gluLookAt is kinda limiting, but i've been able to fudge results, (causes some wierd z axis rotations.. i think its something w/ how gluLookAt works, but for most particles that shouldn't matter toooo much, as long as it faces the camera,
im just cutting code right out of an engine i wrote trying to do it this way...





void GFX::SetCamera(GLfloat ox, GLfloat oy, GLfloat oz, GLfloat tx,GLfloat ty, GLfloat tz)
{
GLfloat OrientedX, OrientedY,OrientedZ;
if ((oz==0))
oz=0.000000001f;
camXpos=ox;camYpos=oy;camZpos=oz;
OrientedX=tx-ox;
OrientedY=ty-oy;
OrientedZ=tz-oz;
camXrot=0;camYrot=0;
camXrot=(atan(OrientedY/OrientedZ))*180/3.14159;
camYrot=-(atan(OrientedX/OrientedZ))*180/3.14159;
gluLookAt(ox,oy,oz,tx,ty,tz,0,1,0);
}



basically its inverse trig stuff... whenever you render the particles just do the rotation in reverse

Kent767
12-02-2002, 06:50 AM
oh had wierd issues with crossing the x axis w/ the camera did a quick fix in the particle routine... so basically this is a very innefective way of doing it, but you can make it work.... its probably better to just write your own camera class to do the matrix rotations...
Kent

nexusone
12-02-2002, 07:04 AM
Hum....giving that you are suppling gluLookAt with the points in which to look at.
With a little vector math you should be able to get the camera rotation.

Using the first two points in the gluLookAt, you should be able to come up with angle of rotation.

the other option would be to not use gluLookAt, since it is equal to doing something like the following:

glTranslatef(x2,y2,z2); // look at
glRotate( angle_x, 1.0, 0.0, 0.0);
glRotate( angle_y, 0.0, 1.0, 0.0);
glRotate( angle_z, 0.0, 0.0, 1.0);
glTranslatef(x1,y1,z1); // eye point
// Note bound to be a error here, doing it all in my head.. http://www.opengl.org/discussion_boards/ubb/smile.gif but it should give you an idea.

This way you know the angles of rotation supplied vs. maybe having to convert the gluLookAt points to angles.
Or if you want to keep using glLookAt, it maybe easier to conver angles to the points needed in the gluLookAt, I did a post on this some time back using sin/cos to give rotated points for gluLookAt. I don't have my notes handy on it.



Originally posted by Structural:
http://www.opengl.org/discussion_boards/ubb/smile.gif
OK, How would I do this? I don't know my cams rotation since I use GLULOOKAT.
I know how I can get the current view matrix (glGetFloatv(GL_MODELVIEW_MATRIX, array)) but which values in that matrix are the rotation (or how would I get them)?


MadMan, I read something about billboarding in the gamedev book, but it seems to lack a decent snippet (or anything that I can understand)


[This message has been edited by Structural (edited 12-02-2002).]



[This message has been edited by nexusone (edited 12-02-2002).]