I want to cast a ray into my model based on a mouse click and be able to select the closest intersecting point.
The difference I have, is that when “rotating”, I rotate the model, not the camera. My application is a model viewer, not a game, so I want the user to see the other sides of the model, not look around the world.
The code approach I’ve been using is from this tutorial http://antongerdelan.net/opengl/raycasting.html and is based on the following:
private Vector3f calculateMouseRay()
{
float mouseX = Mouse.getX();
float mouseY = Mouse.getY();
Vector2f normalizedCoords = getNormalisedDeviceCoordinates(mouseX, mouseY);
Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
Vector4f eyeCoords = toEyeCoords(clipCoords);
Vector3f worldRay = toWorldCoords(eyeCoords);
return worldRay;
}
Full ray cast code is here: https://www.dropbox.com/s/qkslys3p3xzh8av/MousePicker%20Code.txt?dl=0
My model rotation code is:
QMatrix4x4 modelMatrix;
modelMatrix.translate(xCenter, yCenter, zCenter);
modelMatrix.rotate(m_zRot, QVector3D(0, 0, 1));
modelMatrix.rotate(m_yRot, QVector3D(0, 1, 0));
modelMatrix.rotate(m_xRot, QVector3D(1, 0, 0));
modelMatrix.translate(-xCenter, -yCenter, -zCenter);
My camera is set at:
QVector3D cameraPos(m_xMax*1.25, m_yMax*0.75, m_zMax);
QVector3D cameraTarget(xCenter, yCenter, zCenter);
QVector3D cameraUp(0,1,0);
m_camera->lookAt(cameraPos, cameraTarget, cameraUp);
What I want to do is draw a line from the mouse click through the scene.
I know I need to de-embed the model rotation, by multiplying by the inverse matrix somewhere along the chain, but thus far I haven’t been successful.
Where amongst the first steps listed above should I de-embed the model rotation to get the ray cast vector?