PDA

View Full Version : How to access local coordinates?



eratic-magician
03-22-2017, 03:45 PM
Hi, I'm trying to draw an object in the view space, and then get its world space coordinates (I want my bullet model to first render at the point my camera is looking at, which is why it's "attached" to the camera, but then I want its position in the world).

This is what I'm trying now, as suggested online:


// Rotate bullet based on camera angles
glm::quat bulletRotation = glm::normalize(glm::quat(glm::vec3(-1.5708, 0.03054326, 0)));
glm::mat4 BulletRotationMatrix = glm::toMat4(bulletRotation);
// Offset so that it appears after the barrel of the gun
glm::mat4 BulletTranslationMatrix = translate(mat4(), glm::vec3(0.35, 0, -3));
glm::mat4 BulletScaleMatrix = scale(mat4(), glm::vec3(0.1f, 0.1f, 0.1f));
glm::mat4 BulletInCameraMatrix = BulletTranslationMatrix * BulletRotationMatrix * BulletScaleMatrix;
glm::mat4 CameraAndProj = ProjectionMatrix * BulletInCameraMatrix;

glm::mat4 BulletToWorld = inverse(CameraAndProj * ViewMatrix);
//bullet coordinates in world space
glm::vec3 bulletWorldCoords = glm::vec3(BulletToWorld * glm::vec4(localBulletCoordinate.x, localBulletCoordinate.y, localBulletCoordinate.z, 1.0));


However, I don't understand what my "localBulletCoordinate" vector would be. I load in my model like this:

std::vector<unsigned short> bulletIndices;
std::vector<glm::vec3> bulletVertices;
std::vector<glm::vec2> bulletUvs;
std::vector<glm::vec3> bulletNormals;


So I'm unsure where local coordinates are defined/found. This also may not even be a correct way of doing it. Can anyone offer any help? Cheers.