Skip to content

Commit 86c396a

Browse files
committed
The bistro scene now loads. Still have some issues with PBR but I think that's due to material only looking at the vespa. This includes camera controls.
1 parent 9513597 commit 86c396a

26 files changed

+1873
-263
lines changed

attachments/simple_engine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ add_custom_target(shaders DEPENDS ${SHADER_SPVS})
8989
set(SOURCES
9090
main.cpp
9191
engine.cpp
92+
scene_loading.cpp
9293
platform.cpp
9394
renderer_core.cpp
9495
renderer_rendering.cpp

attachments/simple_engine/audio_system.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
#include <condition_variable>
1010
#include <atomic>
1111
#include <queue>
12-
#ifdef __INTELLISENSE__
1312
#include <vulkan/vulkan_raii.hpp>
14-
#else
15-
import vulkan_hpp;
16-
#endif
1713
#include <vulkan/vk_platform.h>
1814

1915
/**

attachments/simple_engine/camera_component.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,5 @@ void CameraComponent::UpdateProjectionMatrix() {
5656
float halfHeight = orthoHeight * 0.5f;
5757
projectionMatrix = glm::ortho(-halfWidth, halfWidth, -halfHeight, halfHeight, nearPlane, farPlane);
5858
}
59-
// Flip Y-axis for Vulkan coordinate system
60-
// @see en/Building_a_Simple_Engine/Camera_Transformations/05_vulkan_integration.adoc#coordinate-system-differences
61-
projectionMatrix[1][1] *= -1;
6259
projectionMatrixDirty = false;
6360
}

attachments/simple_engine/camera_component.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ class CameraComponent : public Component {
150150
viewMatrixDirty = true;
151151
}
152152

153+
/**
154+
* @brief Make the camera look at a specific target position.
155+
* @param targetPosition The position to look at.
156+
* @param upVector The up vector (optional, defaults to current up vector).
157+
*/
158+
void LookAt(const glm::vec3& targetPosition, const glm::vec3& upVector = glm::vec3(0.0f, 1.0f, 0.0f)) {
159+
target = targetPosition;
160+
up = upVector;
161+
viewMatrixDirty = true;
162+
}
163+
153164
/**
154165
* @brief Get the view matrix.
155166
* @return The view matrix.

attachments/simple_engine/descriptor_manager.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#pragma once
22

3-
#ifdef __INTELLISENSE__
43
#include <vulkan/vulkan_raii.hpp>
5-
#else
6-
import vulkan_hpp;
7-
#endif
84
#include <vector>
95
#include <unordered_map>
106
#define GLM_FORCE_RADIANS

attachments/simple_engine/engine.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "engine.h"
2+
#include "scene_loading.h"
23

34
#include <chrono>
45
#include <algorithm>
56
#include <stdexcept>
7+
#include <iostream>
68

79
// This implementation corresponds to the Engine_Architecture chapter in the tutorial:
810
// @see en/Building_a_Simple_Engine/Engine_Architecture/02_architectural_patterns.adoc
@@ -38,13 +40,72 @@ bool Engine::Initialize(const std::string& appName, int width, int height, bool
3840

3941
// Set mouse callback
4042
platform->SetMouseCallback([this](float x, float y, uint32_t buttons) {
43+
// Handle camera rotation when left mouse button is pressed
44+
if (buttons & 1) { // Left mouse button (bit 0)
45+
if (!cameraControl.mouseLeftPressed) {
46+
cameraControl.mouseLeftPressed = true;
47+
cameraControl.firstMouse = true;
48+
}
49+
50+
if (cameraControl.firstMouse) {
51+
cameraControl.lastMouseX = x;
52+
cameraControl.lastMouseY = y;
53+
cameraControl.firstMouse = false;
54+
}
55+
56+
float xOffset = x - cameraControl.lastMouseX;
57+
float yOffset = cameraControl.lastMouseY - y; // Reversed since y-coordinates go from bottom to top
58+
cameraControl.lastMouseX = x;
59+
cameraControl.lastMouseY = y;
60+
61+
xOffset *= cameraControl.mouseSensitivity;
62+
yOffset *= cameraControl.mouseSensitivity;
63+
64+
cameraControl.yaw += xOffset;
65+
cameraControl.pitch += yOffset;
66+
67+
// Constrain pitch to avoid gimbal lock
68+
if (cameraControl.pitch > 89.0f) cameraControl.pitch = 89.0f;
69+
if (cameraControl.pitch < -89.0f) cameraControl.pitch = -89.0f;
70+
} else {
71+
cameraControl.mouseLeftPressed = false;
72+
}
73+
4174
if (imguiSystem) {
4275
imguiSystem->HandleMouse(x, y, buttons);
4376
}
4477
});
4578

4679
// Set keyboard callback
4780
platform->SetKeyboardCallback([this](uint32_t key, bool pressed) {
81+
// Handle camera movement keys (WASD + Arrow keys)
82+
switch (key) {
83+
case GLFW_KEY_W:
84+
case GLFW_KEY_UP:
85+
cameraControl.moveForward = pressed;
86+
break;
87+
case GLFW_KEY_S:
88+
case GLFW_KEY_DOWN:
89+
cameraControl.moveBackward = pressed;
90+
break;
91+
case GLFW_KEY_A:
92+
case GLFW_KEY_LEFT:
93+
cameraControl.moveLeft = pressed;
94+
break;
95+
case GLFW_KEY_D:
96+
case GLFW_KEY_RIGHT:
97+
cameraControl.moveRight = pressed;
98+
break;
99+
case GLFW_KEY_Q:
100+
case GLFW_KEY_PAGE_UP:
101+
cameraControl.moveUp = pressed;
102+
break;
103+
case GLFW_KEY_E:
104+
case GLFW_KEY_PAGE_DOWN:
105+
cameraControl.moveDown = pressed;
106+
break;
107+
}
108+
48109
if (imguiSystem) {
49110
imguiSystem->HandleKeyboard(key, pressed);
50111
}
@@ -68,6 +129,9 @@ bool Engine::Initialize(const std::string& appName, int width, int height, bool
68129
return false;
69130
}
70131

132+
// Connect model loader to renderer for light extraction
133+
renderer->SetModelLoader(modelLoader.get());
134+
71135
// Initialize audio system
72136
if (!audioSystem->Initialize(renderer.get())) {
73137
return false;
@@ -246,6 +310,9 @@ ImGuiSystem* Engine::GetImGuiSystem() const {
246310
}
247311

248312
void Engine::Update(float deltaTime) {
313+
// Check for completed background loading and create entities if ready
314+
CheckAndCreateLoadedEntities();
315+
249316
// Update physics system
250317
physicsSystem->Update(deltaTime);
251318

@@ -255,6 +322,11 @@ void Engine::Update(float deltaTime) {
255322
// Update ImGui system
256323
imguiSystem->NewFrame();
257324

325+
// Update camera controls
326+
if (activeCamera) {
327+
UpdateCameraControls(deltaTime);
328+
}
329+
258330
// Update all entities
259331
for (auto& entity : entities) {
260332
if (entity->IsActive()) {
@@ -325,6 +397,75 @@ void Engine::HandleResize(int width, int height) {
325397
}
326398
}
327399

400+
void Engine::UpdateCameraControls(float deltaTime) {
401+
if (!activeCamera) return;
402+
403+
// Get camera transform component
404+
TransformComponent* cameraTransform = activeCamera->GetOwner()->GetComponent<TransformComponent>();
405+
if (!cameraTransform) return;
406+
407+
// Calculate movement speed
408+
float velocity = cameraControl.cameraSpeed * deltaTime;
409+
410+
// Calculate camera direction vectors based on yaw and pitch
411+
glm::vec3 front;
412+
front.x = cos(glm::radians(cameraControl.yaw)) * cos(glm::radians(cameraControl.pitch));
413+
front.y = sin(glm::radians(cameraControl.pitch));
414+
front.z = sin(glm::radians(cameraControl.yaw)) * cos(glm::radians(cameraControl.pitch));
415+
front = glm::normalize(front);
416+
417+
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
418+
glm::vec3 right = glm::normalize(glm::cross(front, up));
419+
up = glm::normalize(glm::cross(right, front));
420+
421+
// Get current camera position
422+
glm::vec3 position = cameraTransform->GetPosition();
423+
424+
// Apply movement based on input
425+
if (cameraControl.moveForward) {
426+
position += front * velocity;
427+
}
428+
if (cameraControl.moveBackward) {
429+
position -= front * velocity;
430+
}
431+
if (cameraControl.moveLeft) {
432+
position -= right * velocity;
433+
}
434+
if (cameraControl.moveRight) {
435+
position += right * velocity;
436+
}
437+
if (cameraControl.moveUp) {
438+
position += up * velocity;
439+
}
440+
if (cameraControl.moveDown) {
441+
position -= up * velocity;
442+
}
443+
444+
// Update camera position
445+
cameraTransform->SetPosition(position);
446+
447+
// Update camera target based on direction
448+
glm::vec3 target = position + front;
449+
activeCamera->SetTarget(target);
450+
}
451+
452+
void Engine::CheckAndCreateLoadedEntities() {
453+
// Check if background loading is complete
454+
if (g_loadingState.loadingComplete && !g_loadingState.loadedMaterials.empty()) {
455+
// Create entities from loaded materials on the main thread
456+
CreateEntitiesFromLoadedMaterials(this);
457+
458+
// Reset the loading complete flag
459+
g_loadingState.loadingComplete = false;
460+
}
461+
462+
// Check for loading errors
463+
if (g_loadingState.loadingFailed) {
464+
std::cerr << "Background loading failed: " << g_loadingState.errorMessage << std::endl;
465+
g_loadingState.loadingFailed = false; // Reset the flag
466+
}
467+
}
468+
328469
#if PLATFORM_ANDROID
329470
// Android-specific implementation
330471
bool Engine::InitializeAndroid(android_app* app, const std::string& appName, bool enableValidationLayers) {
@@ -371,6 +512,9 @@ bool Engine::InitializeAndroid(android_app* app, const std::string& appName, boo
371512
return false;
372513
}
373514

515+
// Connect model loader to renderer for light extraction
516+
renderer->SetModelLoader(modelLoader.get());
517+
374518
// Initialize audio system
375519
if (!audioSystem->Initialize(renderer.get())) {
376520
return false;

attachments/simple_engine/engine.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ class Engine {
182182
float deltaTime = 0.0f;
183183
uint64_t lastFrameTime = 0;
184184

185+
// Camera control state
186+
struct CameraControlState {
187+
bool moveForward = false;
188+
bool moveBackward = false;
189+
bool moveLeft = false;
190+
bool moveRight = false;
191+
bool moveUp = false;
192+
bool moveDown = false;
193+
bool mouseLeftPressed = false;
194+
float lastMouseX = 0.0f;
195+
float lastMouseY = 0.0f;
196+
float yaw = 0.0f; // Horizontal rotation
197+
float pitch = 0.0f; // Vertical rotation
198+
bool firstMouse = true;
199+
float cameraSpeed = 5.0f;
200+
float mouseSensitivity = 0.1f;
201+
} cameraControl;
202+
185203
/**
186204
* @brief Update the engine state.
187205
* @param deltaTime The time elapsed since the last update.
@@ -205,4 +223,15 @@ class Engine {
205223
* @param height The new height of the window.
206224
*/
207225
void HandleResize(int width, int height);
226+
227+
/**
228+
* @brief Update camera controls based on input state.
229+
* @param deltaTime The time elapsed since the last update.
230+
*/
231+
void UpdateCameraControls(float deltaTime);
232+
233+
/**
234+
* @brief Check for completed background loading and create entities if ready.
235+
*/
236+
void CheckAndCreateLoadedEntities();
208237
};

attachments/simple_engine/imgui_system.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ void ImGuiSystem::NewFrame() {
109109
// Create HRTF Audio Control UI
110110
ImGui::Begin("HRTF Audio Controls");
111111
ImGui::Text("Hello, Vulkan!");
112+
// PBR Rendering Controls
113+
ImGui::Separator();
114+
ImGui::Text("PBR Rendering Controls:");
115+
116+
if (ImGui::Checkbox("Enable PBR Rendering", &pbrEnabled)) {
117+
std::cout << "PBR rendering " << (pbrEnabled ? "enabled" : "disabled") << std::endl;
118+
}
119+
120+
if (pbrEnabled) {
121+
ImGui::Text("Status: PBR pipeline active for supported models");
122+
ImGui::Text("Models using PBR: Bistro scene");
123+
} else {
124+
ImGui::Text("Status: Using basic rendering pipeline");
125+
ImGui::Text("All models rendered with basic shading");
126+
}
127+
112128
ImGui::Text("3D Audio Position Control");
113129

114130
// Audio source selection

attachments/simple_engine/imgui_system.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
#include <string>
44
#include <vector>
55
#include <memory>
6-
#ifdef __INTELLISENSE__
76
#include <vulkan/vulkan_raii.hpp>
8-
#else
9-
import vulkan_hpp;
10-
#endif
117
#include <vulkan/vk_platform.h>
128

139
// Forward declarations
@@ -105,6 +101,12 @@ class ImGuiSystem {
105101
*/
106102
void SetAudioSystem(AudioSystem* audioSystem);
107103

104+
/**
105+
* @brief Get the current PBR rendering state.
106+
* @return True if PBR rendering is enabled, false otherwise.
107+
*/
108+
bool IsPBREnabled() const { return pbrEnabled; }
109+
108110
private:
109111
// ImGui context
110112
ImGuiContext* context = nullptr;
@@ -151,6 +153,9 @@ class ImGuiSystem {
151153
// Initialization flag
152154
bool initialized = false;
153155

156+
// PBR rendering state
157+
bool pbrEnabled = false;
158+
154159
/**
155160
* @brief Create Vulkan resources for ImGui.
156161
* @return True if creation was successful, false otherwise.

0 commit comments

Comments
 (0)