Apply the camera viewpoint to the view matrix
| 217 | |
| 218 | // Apply the camera viewpoint to the view matrix |
| 219 | void Camera::applyViewpoint() { |
| 220 | Online* online = Online::Instance(); |
| 221 | Graphics* gi = Graphics::Instance(); |
| 222 | mat4 projection = GetPerspectiveMatrix(); |
| 223 | |
| 224 | CalcFacing(); // Find the forward vector |
| 225 | calcUp(); // Find the up vector |
| 226 | |
| 227 | float interp_weight = GetInterpWeight(); |
| 228 | interp_pos = mix(old_pos, pos, interp_weight); |
| 229 | interp_chase_distance = mix(old_chase_distance, chase_distance, interp_weight); |
| 230 | |
| 231 | // Apply camera transformation |
| 232 | vec3 final_cam_pos; |
| 233 | |
| 234 | final_cam_pos = interp_pos - facing * interp_chase_distance; |
| 235 | |
| 236 | cameraViewMatrix.SetLookAt(final_cam_pos, final_cam_pos + facing, up); |
| 237 | calcFrustumPlanes(projection, cameraViewMatrix); |
| 238 | inverseCameraViewMatrix = invert(cameraViewMatrix); |
| 239 | |
| 240 | Input* userInput = Input::Instance(); |
| 241 | |
| 242 | // Find mouse direction in world space |
| 243 | vec3 mouse; |
| 244 | mouse.x() = (float)userInput->getMouse().pos_[0]; |
| 245 | mouse.y() = (float)(userInput->getMouse().pos_[1] * -1 + gi->window_dims[1]); |
| 246 | mouse.z() = 0.5f; |
| 247 | |
| 248 | for (unsigned i = 0; i < 16; ++i) { |
| 249 | modelview_matrix[i] = cameraViewMatrix.entries[i]; |
| 250 | projection_matrix[i] = projection.entries[i]; |
| 251 | } |
| 252 | |
| 253 | viewport[0] = 0; |
| 254 | viewport[1] = 0; |
| 255 | viewport[2] = gi->window_dims[0]; |
| 256 | viewport[3] = gi->window_dims[1]; |
| 257 | |
| 258 | glm::vec3 ray = glm::unProject(glm::vec3(mouse.x(), mouse.y(), mouse.z()), |
| 259 | glm::make_mat4(modelview_matrix), |
| 260 | glm::make_mat4(projection_matrix), |
| 261 | glm::make_vec4(viewport)); |
| 262 | |
| 263 | mouseray.x() = (float)ray.x; |
| 264 | mouseray.y() = (float)ray.y; |
| 265 | mouseray.z() = (float)ray.z; |
| 266 | |
| 267 | mouse.x() = (float)(gi->window_dims[0]) / 2.0f; |
| 268 | mouse.y() = (float)(gi->window_dims[1]) / 2.0f; |
| 269 | |
| 270 | ray = glm::unProject(glm::vec3(mouse.x(), mouse.y(), mouse.z()), |
| 271 | glm::make_mat4(modelview_matrix), |
| 272 | glm::make_mat4(projection_matrix), |
| 273 | glm::make_vec4(viewport)); |
| 274 | |
| 275 | vec3 center = vec3((float)ray.x, (float)ray.y, (float)ray.z); |
| 276 | center -= interp_pos; |