Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"queue": "cpp"
"queue": "cpp",
"stdfloat": "cpp"
},
"C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 80, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false, PointerAlignment: Left, Cpp11BracedListStyle: true, AlignAfterOpenBracket: AlwaysBreak}"
}
51 changes: 48 additions & 3 deletions Application.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
#define GLM_SWIZZLE
#include "Application.hpp"
#include "imgui/imgui_impl_sdl2.h"
#include "imgui/imgui_impl_vulkan.h"
#include <SDL2/SDL.h>
#include <glm/gtx/perpendicular.hpp>

Application::Application() {}
Application::Application()
: m_LastTimePoint(std::chrono::steady_clock::now())
{
ImGui::CreateContext();
ImGui::StyleColorsDark();
m_Video.InitImGui();
vk::Extent2D extent = m_Video.GetScreenSize();
m_Camera.setAspect(extent.width, extent.height);
}

Application::~Application() {}

void Application::Run()
{
m_Running = true;
while (m_Running)
{
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
// ImGui::ShowDemoWindow();
ImGui::Begin("Hello, world!");
ImGui::InputFloat3("position", &m_Camera.position.x);
ImGui::InputFloat3("lookdir", &m_Camera.lookdir.x);
ImGui::Text(
"Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
Update();
ImGui::End();

m_Video.Render();
}
}
void Application::Update()
{
auto deltaT = std::chrono::steady_clock::now() - m_LastTimePoint;
m_LastTimePoint = std::chrono::steady_clock::now();
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
m_Camera.processEvent(event);
switch (event.type)
{
case SDL_WINDOWEVENT:
Expand All @@ -26,8 +56,23 @@ void Application::Update()
m_Running = false;
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
SDL_SetRelativeMouseMode(SDL_TRUE);
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
SDL_SetRelativeMouseMode(SDL_FALSE);
break;
}
break;
}
}
m_Video.UpdateUnformBuffers(m_Theta);
m_Theta += 0.1f;
m_Camera.move(deltaT);
m_Video.UpdateUniformBuffers(m_Camera.GetMVP());
}
8 changes: 7 additions & 1 deletion Application.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
#pragma once

#include "Video.hpp"
#include "Camera.hpp"
#include <glm/vec2.hpp>
#include <chrono>

class Application
{
public:
Application();
~Application();
void Run();
void Update();

private:
Video m_Video;
bool m_Running;
float m_Theta;
bool m_CaptureMouse;
Camera m_Camera;
std::chrono::steady_clock::time_point m_LastTimePoint;
};
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include_directories(/usr/local/include)
endif()

# add_subdirectory(fmt)
add_subdirectory(imgui)

file(GLOB source_files CONFIGURE_DEPENDS "${PROJECT_SOURCE_DIR}/*.cpp" )

Expand All @@ -44,6 +45,7 @@ target_link_libraries(UntitledGame SDL2::SDL2main)
target_link_libraries(UntitledGame SDL2::SDL2)
target_link_libraries(UntitledGame SDL2_image::SDL2_image)
target_link_libraries(UntitledGame Vulkan::Vulkan)
target_link_libraries(UntitledGame imgui)
# message(STATUS ${Vulkan_LIBRARY})
if (WIN32)
target_link_libraries(UntitledGame glm)
Expand Down
100 changes: 100 additions & 0 deletions Camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "Camera.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include <SDL2/SDL_keycode.h>
#include <algorithm>

constexpr const float sensitivity = 0.2f;

glm::mat4x4 Camera::GetMVP()
{
float yaw_radians = glm::radians(yaw);
float pitch_radians = glm::radians(pitch);
lookdir = glm::vec3(
-sin(yaw_radians) * cos(pitch_radians),
-cos(yaw_radians) * cos(pitch_radians), sin(pitch_radians));

glm::mat4x4 model = glm::mat4(1.0f);
glm::mat4x4 view =
glm::lookAt(position, position + lookdir, {0.0f, 0.0f, 1.0f});
glm::mat4x4 projection =
glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
glm::mat4x4 clip = glm::mat4x4(
1.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f,
0.0f, 0.0f, 0.5f,
1.0f); // vulkan clip space has inverted y and half z !
return clip * projection * view * model;
}

void Camera::move(std::chrono::nanoseconds deltaT)
{
float seconds =
std::chrono::duration_cast<std::chrono::duration<float>>(deltaT)
.count();

velocity = glm::vec3(0.0f);

if (movementBits & Directions::eForward)
velocity += lookdir;
if (movementBits & Directions::eBack)
velocity -= lookdir;
if (movementBits & Directions::eRight)
velocity += glm::normalize(glm::cross(lookdir, upVector));
if (movementBits & Directions::eLeft)
velocity -= glm::normalize(glm::cross(lookdir, upVector));
if (movementBits & Directions::eUp)
velocity += upVector;
if (movementBits & Directions::eDown)
velocity -= upVector;

position += velocity * seconds;
}

void Camera::processEvent(SDL_Event& event)
{
switch (event.type)
{
case SDL_MOUSEMOTION:
yaw += (event.motion.xrel * sensitivity);
pitch = std::clamp(
pitch - (event.motion.yrel * sensitivity), -90.0f, 90.0f);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
vk::Flags<Directions> mask;
switch (event.key.keysym.sym)
{
case SDLK_w:
mask = Directions::eForward;
break;
case SDLK_s:
mask = Directions::eBack;
break;
case SDLK_d:
mask = Directions::eRight;
break;
case SDLK_a:
mask = Directions::eLeft;
break;
case SDLK_SPACE:
mask = Directions::eUp;
break;
case SDLK_c:
mask = Directions::eDown;
break;
}
if (event.type == SDL_KEYDOWN)
{
movementBits |= mask;
}
else if (event.type == SDL_KEYUP)
{
movementBits &= ~mask;
}
break;
}
}

void Camera::setAspect(uint32_t width, uint32_t height)
{
aspect = static_cast<float>(width) / static_cast<float>(height);
}
40 changes: 40 additions & 0 deletions Camera.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <SDL2/SDL_events.h>
#include <chrono>
#include <glm/common.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>
#include <vulkan/vulkan.hpp>

constexpr const glm::vec3 upVector{0.0f, 0.0f, 1.0f};

enum class Directions
{
eForward = 0x00000001,
eBack = 0x00000002,
eRight = 0x00000004,
eLeft = 0x00000008,
eUp = 0x00000010,
eDown = 0x00000020
};

template <> struct vk::FlagTraits<Directions>
{
static constexpr bool isBitmask = true;
static constexpr vk::Flags<Directions> allFlags =
Directions::eForward | Directions::eBack | Directions::eRight |
Directions::eLeft | Directions::eUp | Directions::eDown;
};

struct Camera
{
glm::mat4x4 GetMVP();
void move(std::chrono::nanoseconds deltaT);
void processEvent(SDL_Event& event);
void setAspect(uint32_t width, uint32_t height);
glm::vec3 position = {0.0f, 2.0f, 0.0f}, lookdir, velocity;
float yaw, pitch;
float aspect = 1.0f;
vk::Flags<Directions> movementBits;
};
5 changes: 5 additions & 0 deletions CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ CommandBuffer::CommandBuffer(
{
}

vk::raii::CommandBuffers& CommandBuffer::Get()
{
return m_CommandBuffers;
}

vk::raii::CommandBuffer& CommandBuffer::operator[](size_t index)
{
return m_CommandBuffers.at(index);
Expand Down
1 change: 1 addition & 0 deletions CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CommandBuffer
public:
CommandBuffer(Device& device, uint32_t queueIndex, size_t bufferCount = 1);

vk::raii::CommandBuffers& Get();
vk::raii::CommandBuffer& operator[](size_t index);

private:
Expand Down
15 changes: 9 additions & 6 deletions Descriptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ Descriptors::Descriptors(
device.Get().updateDescriptorSets(descriptorWriteSets, nullptr);
}

vk::raii::DescriptorPool
Descriptors::CreateDescriptorPool(Device& device, size_t imageCount)
vk::raii::DescriptorPool Descriptors::CreateDescriptorPool(
Device& device, size_t imageCount) // maxSets needs to be revisited later
{
vk::DescriptorPoolSize discriptorPoolSize(
vk::DescriptorType::eUniformBuffer, imageCount);
std::array<vk::DescriptorPoolSize, 2> discriptorPoolSizes{
{{vk::DescriptorType::eUniformBuffer,
static_cast<uint32_t>(imageCount)},
{vk::DescriptorType::eCombinedImageSampler, /*For ImGui*/
static_cast<uint32_t>(imageCount)}}};
vk::DescriptorPoolCreateInfo createInfo(
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, imageCount,
discriptorPoolSize);
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet,
discriptorPoolSizes.size() * imageCount, discriptorPoolSizes);
return device.Get().createDescriptorPool(createInfo);
}

Expand Down
1 change: 0 additions & 1 deletion Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <fmt/ostream.h>
#include <iostream>
#include <stdexcept>
#include <vulkan/vulkan.h>

void LogError(const std::string& message);

Expand Down
10 changes: 5 additions & 5 deletions Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ vk::raii::Pipeline GraphicsPipeline::CreatePipeline(
0, sizeof(Vertex), vk::VertexInputRate::eVertex);

std::array<vk::VertexInputAttributeDescription, 2> attributeDescription = {
{{0, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos)},
{{0, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, pos)},
{1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color)}}};

vk::PipelineVertexInputStateCreateInfo vertexInputState(
Expand All @@ -42,9 +42,9 @@ vk::raii::Pipeline GraphicsPipeline::CreatePipeline(

vk::PipelineViewportStateCreateInfo viewportState({}, viewport, scissor);

vk::PipelineRasterizationStateCreateInfo rasteriztionState{};
rasteriztionState.setCullMode(vk::CullModeFlagBits::eBack);
rasteriztionState.setLineWidth(1.0f);
vk::PipelineRasterizationStateCreateInfo rasterizationState{};
rasterizationState.setCullMode(vk::CullModeFlagBits::eBack);
rasterizationState.setLineWidth(1.0f);

vk::PipelineMultisampleStateCreateInfo multisampleState{};

Expand All @@ -64,7 +64,7 @@ vk::raii::Pipeline GraphicsPipeline::CreatePipeline(

vk::GraphicsPipelineCreateInfo graphicsPipelineCreateInfo(
{}, shaderStages, &vertexInputState, &inputAssemblyState, {},
&viewportState, &rasteriztionState, &multisampleState,
&viewportState, &rasterizationState, &multisampleState,
&depthStencilState, &colorBlendState, {}, *m_PipelineLayout,
*renderPass.Get());

Expand Down
4 changes: 2 additions & 2 deletions Swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Swapchain
return m_SwapchainImageViews;
}
constexpr vk::raii::SwapchainKHR& Get() { return m_Swapchain; }
constexpr vk::Extent2D GetExtent() { return m_Extent; }
constexpr size_t GetImageCount() { return m_ImageCount; }
constexpr vk::Extent2D GetExtent() const { return m_Extent; }
constexpr size_t GetImageCount() const { return m_ImageCount; }

private:
std::vector<vk::raii::ImageView> m_SwapchainImageViews;
Expand Down
6 changes: 2 additions & 4 deletions UniformBuffer.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once
#include "glm/mat2x4.hpp"
#include "glm/mat3x4.hpp"
#include "glm/mat4x4.hpp"

struct UniformBufferObject
{
glm::mat2x4 rotation;
float colorRotation;
glm::mat4x4 MVP;
};
Loading