Skip to content

Commit

Permalink
Updated the UI to display some camera settings
Browse files Browse the repository at this point in the history
  • Loading branch information
tstullich committed Oct 21, 2020
1 parent 296dc02 commit 2c013d1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/camera.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#pragma once

#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

struct Camera {
glm::mat4 model;
glm::mat4 view;
glm::mat4 proj;
Camera() = default;

explicit Camera(float aspectRatio) {
model = glm::mat4(1.0f);
view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
proj = glm::perspective(glm::radians(60.0f), aspectRatio, 0.1f, 10.0f);
proj[1][1] *= -1;
}

glm::mat4 model = {};
glm::mat4 view = {};
glm::mat4 proj = {};
};
42 changes: 39 additions & 3 deletions src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,12 @@ UserInterface::UIContext Renderer::createUIContext() {
context.imageCount = swapchain.getImageCount();
context.graphicsFamilyIndex = queueIndices.graphicsFamilyIndex;
context.swapchain = swapchain;

return context;
}

void Renderer::createUniformBuffers() {
Camera cam = {};
// Create a uniform buffer for our camera parameters
uniformBuffers.resize(swapchain.getImagesSize());
for (size_t i = 0; i < swapchain.getImagesSize(); ++i) {
uniformBuffers[i].create(createBufferContext(), cam);
Expand Down Expand Up @@ -1187,6 +1188,31 @@ void Renderer::keyCallback(GLFWwindow *window, int key, int scancode, int action
}
}

void Renderer::loadCamera(tinygltf::Model &model, tinygltf::Node &node) {
// TODO Figure out why this does not work!
auto translation = glm::mat4(1.0f);
if (!node.translation.empty()) {
auto translationVec = glm::vec3(node.translation[0], node.translation[1], node.translation[2]);
translation = glm::translate(translation, translationVec);
}

auto rotation = glm::mat4(1.0f);
if (!node.rotation.empty()) {
auto rotationQuat = glm::quat(node.rotation[0], node.rotation[1], node.rotation[2], node.rotation[3]);
rotation = glm::mat4_cast(rotationQuat);
}

auto scale = glm::mat4(1.0f);
if (!node.scale.empty()) {
// Unlikely that the camera node will have a scale vector
// but keeping it just in case
auto scalingVec = glm::vec3(node.scale[0], node.scale[1], node.scale[2]);
scale = glm::scale(scale, scalingVec);
}

cam.model = translation * rotation * scale;
}

void Renderer::loadMesh(tinygltf::Model &model, tinygltf::Mesh &mesh) {
for (auto primitive : mesh.primitives) {
if (primitive.indices >= 0) {
Expand Down Expand Up @@ -1300,15 +1326,21 @@ void Renderer::loadNode(tinygltf::Model &model, tinygltf::Node &node) {
loadMesh(model, model.meshes[node.mesh]);
}

// TODO Check if this is the best way to setup the camera
//if (node.name == "Camera") {
// loadCamera(model, node);
//}

// Parse child nodes
for (int i : node.children) {
loadNode(model, model.nodes[i]);
}
}

void Renderer::loadScene() {
auto model = GLTFLoader::load("models/box-textured/BoxTextured.gltf");
auto model = GLTFLoader::load("models/helmet/SciFiHelmet.gltf");
auto scene = model.scenes[model.defaultScene];
cam = Camera(static_cast<float>(windowWidth) / windowHeight);
for (int nodeId : scene.nodes) {
loadNode(model, model.nodes[nodeId]);
}
Expand Down Expand Up @@ -1531,9 +1563,13 @@ void Renderer::updateUniformBuffer(size_t bufferIdx) {
cam.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));
}
cam.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
cam.proj = glm::perspective(glm::radians(45.0f), extent.width / (float) extent.height, 0.1f, 10.0f);
cam.proj = glm::perspective(glm::radians(60.0f), extent.width / (float) extent.height, 0.1f, 10.0f);
cam.proj[1][1] *= -1;

// Update UI elements
options.cam = cam;
ui.setUIOptions(options);

void* data;
vkMapMemory(logicalDevice, uniformBuffers[bufferIdx].getDeviceMemory(), 0, sizeof(cam), 0, &data);
memcpy(data, &cam, sizeof(cam));
Expand Down
5 changes: 4 additions & 1 deletion src/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>

#include "buffer.h"
#include "gltfloader.h"
Expand Down Expand Up @@ -172,6 +173,8 @@ class Renderer {

bool isDeviceSuitable(VkPhysicalDevice device);

void loadCamera(tinygltf::Model &model, tinygltf::Node &node);

void loadMesh(tinygltf::Model &model, tinygltf::Mesh &mesh);

void loadMeshMaterial(tinygltf::Model &model, tinygltf::Primitive &primitive);
Expand Down Expand Up @@ -255,7 +258,7 @@ class Renderer {

UserInterface ui;

Camera cam = {};
Camera cam;

bool framebufferResized = false;

Expand Down
11 changes: 7 additions & 4 deletions src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ void UserInterface::draw() {
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();

if (ImGui::CollapsingHeader("Camera")) {
ImGui::Checkbox("Rotate Object", &options.rotate);
}
ImGui::Text("Camera Position - X: %.4f Y: %.4f Z: %.4f", options.cam.model[3][0], options.cam.model[3][1], options.cam.model[3][2]);

//const std::array<float, 5> values = { 0.2, 1.0, 0.4, 0.4, 0.3 };
//ImGui::PlotLines("Frame Times", values.data(), values.size());

ImGui::Checkbox("Rotate Object", &options.rotate);

ImGui::ShowDemoWindow();

Expand Down Expand Up @@ -238,7 +241,7 @@ VkCommandBuffer UserInterface::recordCommands(uint32_t bufferIdx, VkExtent2D swa
throw std::runtime_error("Unable to start recording UI command buffer!");
}

VkClearValue clearColor = {0.0f, 0.0f, 0.0f, 1.0f};
VkClearValue clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
VkRenderPassBeginInfo renderPassBeginInfo = {};
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.renderPass = renderPass;
Expand Down
6 changes: 5 additions & 1 deletion src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_vulkan.h"

#include "camera.h"
#include "swapchain.h"

class UserInterface {
Expand All @@ -29,6 +30,7 @@ class UserInterface {

struct UIOptions {
alignas(16) bool rotate;
Camera cam;
};

UserInterface() = default;
Expand All @@ -45,6 +47,8 @@ class UserInterface {

void recreate(const UIContext &ctx);

inline void setUIOptions(const UIOptions &uiOptions) { options = uiOptions; }

private:
VkCommandBuffer beginSingleTimeCommands(VkCommandPool cmdPool) const;

Expand All @@ -63,7 +67,7 @@ class UserInterface {
void endSingleTimeCommands(VkCommandBuffer commandBuffer, VkCommandPool cmdPool) const;

UIContext context = {};
UIOptions options = { .rotate = true };
UIOptions options = { .rotate = false, .cam = Camera() };

VkRenderPass renderPass = {};
VkDescriptorPool descriptorPool = {};
Expand Down

0 comments on commit 2c013d1

Please sign in to comment.