Rotate sprite to face 3D camera

Posted by omikun on Game Development See other posts from Game Development or by omikun
Published on 2013-06-28T03:23:59Z Indexed on 2013/06/28 4:29 UTC
Read the original article Hit count: 365

Filed under:
|
|

I am trying to rotate a sprite so it is always facing a 3D camera.

shaders->setUniform("camera", gCamera.matrix());
glm::mat4 scale = glm::scale(glm::mat4(), glm::vec3(5e5, 5e5, 5e5));
glm::vec3 look =  gCamera.position();
glm::vec3 right = glm::cross(gCamera.up(), look);
glm::vec3 up = glm::cross(look, right);
glm::mat4 newTransform = glm::lookAt(glm::vec3(0), gCamera.position(), up) * scale;
shaders->setUniform("model", newTransform);

In the vertex shader:

gl_Position = camera * model * vec4(vert, 1);

The object will track the camera if I move the camera up or down, but if I rotate the camera around it, it will rotate in the other direction so I end up seeing its front twice and its back twice as I rotate around it 360. What am I doing wrong?

© Game Development or respective owner

Related posts about opengl

Related posts about camera