开发者

Using camera rotation to make quad always appear in front of the camera (c++/opengl)

I'm trying to make a quad appear always in front of the camera, I'm trying to start by aligning it with the camera on the x-z plane and making sure it always faces the camera. I used this code...

float ry = cameraRY+PI_2;
float dis = 12;
float sz = 4;

float x = cameraX-dis*cosf(ry);
float y = cameraY;
float z = cameraZ-dis*sinf(ry)+cosf(ry)*sz;

float x2 = x + sinf(ry)*sz;
float y2 = y + sz;
float z2 = z - cosf(ry)*sz;

glVertex3f(x,y,z);
glVertex3f(x2,y,z2);
glVertex3f(x2,y2,z2);
glVertex3f(x,y2,z);

But it didn't quite look right, it seemed that the quad was rotating around a invisible point that 开发者_JAVA百科was rotating correctly around the camera. I don't really know how to change it or how to go about doing this, any help appreciated!

Edit: Forgot to mention, cameraX,cameraY,cameraZ are the camera's x,y,z positions

cameraRX and cameraRY are the camera's x and y rotations (Z rotation is always zero)


Check out this old tutorial on Lighthouse3D. It describes several "billboarding" techniques, which I believe are what you want.


Let P be your model view projection matrix, and c be the center of the quad you are trying to draw. You want to find a pair of vectors u, v that determine the edges of your quad,

Q = [ c-u-v, c-u+v, c-u-v, c+u-v ]

Such that u is pointing directly down in clip coordinates, while v is pointing to the right:

P(u) = (0, s, 0, 0)
P(v) = (s, 0, 0, 0)

Where s is the desired scale of your quad. Suppose that P is written in block diagonal form,

    [   M   | t ]
P = [-----------]
    [ 0 0 1 | 0 ]

Then let m0, m1 be the first two rows of M. Now consider the equation we got for P(u), substituting and simplifying, we get:

              [ 0 ]
P(u) ~> M u = [ s ]
              [ 0 ]

Which leads to the following solution for u, v:

u = s * m1 / |m1|^2
v = s * m0 / |m0|^2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜