Reputation: 67
I'm trying to get an object to always face the camera. I looked up a way to do this, but the problem is when I put this part into the view matrix nothing is affected by the model matrix. How can I get it to translate it using the model matrix? code:
GLuint transformLocation=glGetUniformLocation(textureShaders,"transform");
glm::mat4 transform;
glm::mat4 model;
glm::vec3 playerPosition=user.getPosition();
model=glm::translate(model,glm::vec3(xpos,0.0f,zpos));
glm::mat4 view;
view=glm::lookAt(cam.getPositionVector(),cam.getPositionVector()+cam.getFrontVector(),cam.getUpVector());
glm::mat4 rotationMatrix=glm::transpose(glm::lookAt(glm::vec3(xpos,0.0f,zpos),playerPosition,glm::vec3(0.0f,1.0f,0.0f)));
view*=rotationMatrix;
glm::mat4 projection;
projection=glm::perspective(45.0f,(float)900/(float)600,0.1f,100.0f);
transform=projection*view*model;
Upvotes: 0
Views: 493
Reputation: 45322
Your "rotation" matrix doesn't really make sense:
rotationMatrix=glm::transpose(glm::lookAt(glm::vec3(xpos,0.0f,zpos),playerPosition,glm::vec3(0.0f,1.0f,0.0f)));
This will not result in a rotation matrix (except when both xpos
and zpos
happen to be zero). lookAt
will create a transform matrix which can be decomposed to r * t(-pos)
(for whatever pos
you call it with). Building the transpose of this matrix will result in the translation column beeing transposed to the fourth row, which completely will screw the final w
coordinate.
Upvotes: 1