Update the matrices in our uniform buffer every frame
| 2383 | |
| 2384 | // Update the matrices in our uniform buffer every frame |
| 2385 | void updateUniformBuffer(float elapsed) |
| 2386 | { |
| 2387 | // Construct the model matrix |
| 2388 | // clang-format off |
| 2389 | Matrix model = {{ |
| 2390 | {1.0f, 0.0f, 0.0f, 0.0f}, |
| 2391 | {0.0f, 1.0f, 0.0f, 0.0f}, |
| 2392 | {0.0f, 0.0f, 1.0f, 0.0f}, |
| 2393 | {0.0f, 0.0f, 0.0f, 1.0f} |
| 2394 | }}; |
| 2395 | // clang-format on |
| 2396 | |
| 2397 | matrixRotateX(model, sf::degrees(elapsed * 59.0f)); |
| 2398 | matrixRotateY(model, sf::degrees(elapsed * 83.0f)); |
| 2399 | matrixRotateZ(model, sf::degrees(elapsed * 109.0f)); |
| 2400 | |
| 2401 | // Translate the model based on the mouse position |
| 2402 | const sf::Vector2f mousePosition = sf::Vector2f(sf::Mouse::getPosition(window)); |
| 2403 | const sf::Vector2f windowSize = sf::Vector2f(window.getSize()); |
| 2404 | const float x = std::clamp(mousePosition.x * 2.f / windowSize.x - 1.f, -1.0f, 1.0f) * 2.0f; |
| 2405 | const float y = std::clamp(-mousePosition.y * 2.f / windowSize.y + 1.f, -1.0f, 1.0f) * 1.5f; |
| 2406 | |
| 2407 | model[3][0] -= x; |
| 2408 | model[3][2] += y; |
| 2409 | |
| 2410 | // Construct the view matrix |
| 2411 | const sf::Vector3f eye(0.0f, 4.0f, 0.0f); |
| 2412 | const sf::Vector3f center(0.0f, 0.0f, 0.0f); |
| 2413 | const sf::Vector3f up(0.0f, 0.0f, 1.0f); |
| 2414 | |
| 2415 | Matrix view; |
| 2416 | matrixLookAt(view, eye, center, up); |
| 2417 | |
| 2418 | // Construct the projection matrix |
| 2419 | const sf::Angle fov = sf::degrees(45); |
| 2420 | const float aspect = static_cast<float>(swapchainExtent.width) / static_cast<float>(swapchainExtent.height); |
| 2421 | const float nearPlane = 0.1f; |
| 2422 | const float farPlane = 10.0f; |
| 2423 | |
| 2424 | Matrix projection; |
| 2425 | |
| 2426 | matrixPerspective(projection, fov, aspect, nearPlane, farPlane); |
| 2427 | |
| 2428 | char* ptr = nullptr; |
| 2429 | |
| 2430 | // Map the current frame's uniform buffer into our address space |
| 2431 | if (vkMapMemory(device, uniformBuffersMemory[currentFrame], 0, sizeof(Matrix) * 3, 0, reinterpret_cast<void**>(&ptr)) != |
| 2432 | VK_SUCCESS) |
| 2433 | { |
| 2434 | vulkanAvailable = false; |
| 2435 | return; |
| 2436 | } |
| 2437 | |
| 2438 | // Copy the matrix data into the current frame's uniform buffer |
| 2439 | std::memcpy(ptr + sizeof(Matrix) * 0, model.data(), sizeof(Matrix)); |
| 2440 | std::memcpy(ptr + sizeof(Matrix) * 1, view.data(), sizeof(Matrix)); |
| 2441 | std::memcpy(ptr + sizeof(Matrix) * 2, projection.data(), sizeof(Matrix)); |
| 2442 |
nothing calls this directly
no test coverage detected