Browse Source

Some refactoring.

master
Draklaw 4 years ago
parent
commit
f03a0bd98b
  1. 6
      CMakeLists.txt
  2. 3
      src/Planet.cpp
  3. 3
      src/Planet.h
  4. 3
      src/Simplex.h
  5. 4
      src/VkExpe.cpp
  6. 3
      src/VkExpe.h
  7. 5
      src/VulkanTutorial.cpp
  8. 7
      src/VulkanTutorial.h
  9. 46
      src/core/ArrayView.h
  10. 2
      src/core/Logger.cpp
  11. 0
      src/core/Logger.h
  12. 25
      src/core/math.cpp
  13. 54
      src/core/math.h
  14. 10
      src/core/types.h
  15. 26
      src/core/utils.cpp
  16. 12
      src/core/utils.h
  17. 3
      src/main.cpp
  18. 3
      src/vk/Buffer.cpp
  19. 4
      src/vk/Buffer.h
  20. 4
      src/vk/Context.cpp
  21. 6
      src/vk/Context.h
  22. 2
      src/vk/Swapchain.cpp
  23. 4
      src/vk/Swapchain.h

6
CMakeLists.txt

@ -30,14 +30,16 @@ function(add_shaders TARGET)
endfunction()
add_executable(vk_expe
src/core/math.cpp
src/core/utils.cpp
src/core/Logger.cpp
src/vk/Context.cpp
src/vk/Swapchain.cpp
src/vk/Memory.cpp
src/vk/Buffer.cpp
src/main.cpp
src/utils.cpp
src/Simplex.cpp
src/Logger.cpp
src/Planet.cpp
src/VkExpe.cpp
src/VulkanTutorial.cpp

3
src/Planet.cpp

@ -1,6 +1,7 @@
#include <Planet.h>
#include <Logger.h>
#include <core/Logger.h>
#include <cassert>
#include <vector>

3
src/Planet.h

@ -1,6 +1,7 @@
#pragma once
#include <core.h>
#include <core/math.h>
#include <core/ArrayView.h>
#include <memory>
#include <cassert>

3
src/Simplex.h

@ -1,6 +1,7 @@
#pragma once
#include <core.h>
#include <core/math.h>
#include <core/ArrayView.h>
class Simplex {

4
src/VkExpe.cpp

@ -1,7 +1,7 @@
#include <VkExpe.h>
#include <utils.h>
#include <Logger.h>
#include <core/utils.h>
#include <core/Logger.h>
#include <stdexcept>
#include <iostream>

3
src/VkExpe.h

@ -1,8 +1,9 @@
#pragma once
#include <core.h>
#include <VulkanTutorial.h>
#include <core/math.h>
#include <SDL2/SDL.h>
#include <memory>

5
src/VulkanTutorial.cpp

@ -1,9 +1,10 @@
#include <VulkanTutorial.h>
#include <utils.h>
#include <Logger.h>
#include <Planet.h>
#include <core/utils.h>
#include <core/Logger.h>
#include <SDL2/SDL_vulkan.h>
#include <Eigen/Geometry>

7
src/VulkanTutorial.h

@ -1,14 +1,17 @@
#pragma once
#include <core.h>
#include <vk/Context.h>
#include <vk/Swapchain.h>
#include <vk/Buffer.h>
#include <core/math.h>
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <Eigen/Dense>
#include <vector>
#include <chrono>

46
src/core.h → src/core/ArrayView.h

@ -1,51 +1,7 @@
#pragma once
#include <Eigen/Dense>
#include <type_traits>
using Byte = unsigned char;
using Index = uint32_t;
using Real = float;
using Vector2 = Eigen::Matrix<Real, 2, 1>;
using Vector3 = Eigen::Matrix<Real, 3, 1>;
using Vector4 = Eigen::Matrix<Real, 4, 1>;
using Matrix2 = Eigen::Matrix<Real, 2, 2>;
using Matrix3 = Eigen::Matrix<Real, 3, 3>;
using Matrix4 = Eigen::Matrix<Real, 4, 4>;
using Transform = Eigen::Transform<Real, 3, Eigen::Affine>;
using AngleAxis = Eigen::AngleAxis<Real>;
using Quaternion = Eigen::Quaternion<Real>;
using Triangle = Eigen::Array<Index, 3, 1>;
template<typename Scalar, typename Derived0, typename Derived1>
auto lerp(
Scalar factor,
const Eigen::MatrixBase<Derived0>& m0,
const Eigen::MatrixBase<Derived1>& m1
) -> std::enable_if_t<
Derived0::RowsAtCompileTime == Derived1::RowsAtCompileTime &&
Derived0::ColsAtCompileTime == Derived1::ColsAtCompileTime,
Eigen::Matrix<
std::common_type_t<
Scalar,
typename Derived0::Scalar,
typename Derived1::Scalar
>,
Derived0::RowsAtCompileTime,
Derived0::ColsAtCompileTime
>
> {
return (Scalar(1) - factor) * m0 + factor * m1;
}
#include <core/types.h>
template<typename T>

2
src/Logger.cpp → src/core/Logger.cpp

@ -1,4 +1,4 @@
#include <Logger.h>
#include <core/Logger.h>
#include <iostream>

0
src/Logger.h → src/core/Logger.h

25
src/utils.cpp → src/core/math.cpp

@ -1,28 +1,5 @@
#include <utils.h>
#include <core/math.h>
#include <cstring>
#include <fstream>
Uuid make_uuid(uint8_t const* bytes) {
Uuid uuid;
std::memcpy(uuid.data(), bytes, UUID_SIZE);
return uuid;
}
std::vector<char> read_binary_file(const char* path) {
std::ifstream istream(path, std::ios_base::ate | std::ios_base::binary);
if(!istream.is_open())
throw std::runtime_error(cat("failed to open '", path, "'"));
std::vector<char> buffer(size_t(istream.tellg()));
istream.seekg(0);
istream.read(buffer.data(), buffer.size());
istream.close();
return buffer;
}
Eigen::Matrix4f projection_matrix(float x_min, float x_max, float y_min, float y_max, float z_min, float z_max) {
auto const cx = x_max + x_min;

54
src/core/math.h

@ -0,0 +1,54 @@
#pragma once
#include <core/types.h>
#include <Eigen/Dense>
#include <type_traits>
using Real = float;
using Vector2 = Eigen::Matrix<Real, 2, 1>;
using Vector3 = Eigen::Matrix<Real, 3, 1>;
using Vector4 = Eigen::Matrix<Real, 4, 1>;
using Matrix2 = Eigen::Matrix<Real, 2, 2>;
using Matrix3 = Eigen::Matrix<Real, 3, 3>;
using Matrix4 = Eigen::Matrix<Real, 4, 4>;
using Transform = Eigen::Transform<Real, 3, Eigen::Affine>;
using AngleAxis = Eigen::AngleAxis<Real>;
using Quaternion = Eigen::Quaternion<Real>;
using Triangle = Eigen::Array<Index, 3, 1>;
template<typename Scalar, typename Derived0, typename Derived1>
auto lerp(
Scalar factor,
const Eigen::MatrixBase<Derived0>& m0,
const Eigen::MatrixBase<Derived1>& m1
) -> std::enable_if_t<
Derived0::RowsAtCompileTime == Derived1::RowsAtCompileTime &&
Derived0::ColsAtCompileTime == Derived1::ColsAtCompileTime,
Eigen::Matrix<
std::common_type_t<
Scalar,
typename Derived0::Scalar,
typename Derived1::Scalar
>,
Derived0::RowsAtCompileTime,
Derived0::ColsAtCompileTime
>
> {
return (Scalar(1) - factor) * m0 + factor * m1;
}
Eigen::Matrix4f projection_matrix(float x_min, float x_max, float y_min, float y_max, float z_min, float z_max);
Eigen::Matrix4f look_at_matrix(
const Eigen::Vector3f& cam_pos,
const Eigen::Vector3f& look_at,
const Eigen::Vector3f& up
);

10
src/core/types.h

@ -0,0 +1,10 @@
#pragma once
#include <Eigen/Dense>
#include <type_traits>
using Byte = unsigned char;
using Index = uint32_t;

26
src/core/utils.cpp

@ -0,0 +1,26 @@
#include <core/utils.h>
#include <cstring>
#include <fstream>
Uuid make_uuid(uint8_t const* bytes) {
Uuid uuid;
std::memcpy(uuid.data(), bytes, UUID_SIZE);
return uuid;
}
std::vector<char> read_binary_file(const char* path) {
std::ifstream istream(path, std::ios_base::ate | std::ios_base::binary);
if(!istream.is_open())
throw std::runtime_error(cat("failed to open '", path, "'"));
std::vector<char> buffer(size_t(istream.tellg()));
istream.seekg(0);
istream.read(buffer.data(), buffer.size());
istream.close();
return buffer;
}

12
src/utils.h → src/core/utils.h

@ -1,7 +1,5 @@
#pragma once
#include <Eigen/Dense>
#include <cassert>
#include <vector>
#include <string>
@ -33,6 +31,7 @@ Guard<T> make_guard(T&& teardown) {
return Guard<T>(std::move(teardown));
}
template<typename... Args>
std::string cat(Args&&... args) {
std::ostringstream ostream;
@ -40,8 +39,10 @@ std::string cat(Args&&... args) {
return ostream.str();
}
std::vector<char> read_binary_file(const char* path);
template<typename T>
class Array {
public:
@ -76,10 +77,3 @@ private:
size_t m_size = 0;
T* m_data = nullptr;
};
Eigen::Matrix4f projection_matrix(float x_min, float x_max, float y_min, float y_max, float z_min, float z_max);
Eigen::Matrix4f look_at_matrix(
const Eigen::Vector3f& cam_pos,
const Eigen::Vector3f& look_at,
const Eigen::Vector3f& up
);

3
src/main.cpp

@ -1,6 +1,7 @@
#include <Logger.h>
#include <VkExpe.h>
#include <core/Logger.h>
int main(int argc, char** argv) {
VkExpe vk_expe(argc, argv);

3
src/vk/Buffer.cpp

@ -1,6 +1,7 @@
#include <vk/Buffer.h>
#include <vk/Context.h>
#include <Logger.h>
#include <core/Logger.h>
#include <stdexcept>
#include <algorithm>

4
src/vk/Buffer.h

@ -1,10 +1,10 @@
#pragma once
#include <utils.h>
#include <vk/forward.h>
#include <vk/Memory.h>
#include <core/utils.h>
#include <vulkan/vulkan.h>
#include <memory>

4
src/vk/Context.cpp

@ -1,8 +1,8 @@
#include <vk/Context.h>
#include <vk/Memory.h>
#include <utils.h>
#include <Logger.h>
#include <core/utils.h>
#include <core/Logger.h>
#include <SDL2/SDL_vulkan.h>

6
src/vk/Context.h

@ -1,10 +1,12 @@
#pragma once
#include <utils.h>
#include <Logger.h>
#include <vk/forward.h>
#include <core/utils.h>
#include <core/Logger.h>
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <optional>

2
src/vk/Swapchain.cpp

@ -1,6 +1,6 @@
#include <vk/Swapchain.h>
#include <Logger.h>
#include <core/Logger.h>
#include <SDL2/SDL_vulkan.h>

4
src/vk/Swapchain.h

@ -1,9 +1,11 @@
#pragma once
#include <vk/Context.h>
#include <utils.h>
#include <core/utils.h>
#include <SDL2/SDL.h>
#include <vulkan/vulkan.h>
#include <optional>

Loading…
Cancel
Save