diff --git a/dev/CMakeLists.txt b/dev/CMakeLists.txt index 99304fa..febcb5b 100644 --- a/dev/CMakeLists.txt +++ b/dev/CMakeLists.txt @@ -66,9 +66,9 @@ if (NOT TINYOBJ_PATH) message(STATUS "TINYOBJ_PATH not specified in .env.cmake, using external/tinyobjloader") set(TINYOBJ_PATH external/tinyobjloader) endif() - -file(GLOB_RECURSE SOURCES "../src/*.cpp") - + +file(GLOB_RECURSE SOURCES "../src1/*.cpp") + add_executable(${PROJECT_NAME} ${SOURCES}) target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_17) @@ -88,7 +88,7 @@ if (WIN32) endif() target_include_directories(${PROJECT_NAME} PUBLIC - ../src + ../src1 ${Vulkan_INCLUDE_DIRS} ${TINYOBJ_PATH} ${GLFW_INCLUDE_DIRS} diff --git a/src/collision/collision.cpp b/src/collision/collision.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/collision/collision.h b/src/collision/collision.h new file mode 100644 index 0000000..e69de29 diff --git a/src/input/input.cpp b/src/input/input.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/input/input.h b/src/input/input.h new file mode 100644 index 0000000..e69de29 diff --git a/src/main.cpp b/src/main.cpp index c538db2..9bb7cb1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include #include #include -#include "./render/AvantGardeRender.hpp" +#include "./renderer/vulkan/AvantGardeRender.hpp" int main() { diff --git a/src/shaders/compile-shaders.bat b/src/renderer/shaders/compile-shaders.bat similarity index 100% rename from src/shaders/compile-shaders.bat rename to src/renderer/shaders/compile-shaders.bat diff --git a/src/shaders/compile-shaders.sh b/src/renderer/shaders/compile-shaders.sh similarity index 100% rename from src/shaders/compile-shaders.sh rename to src/renderer/shaders/compile-shaders.sh diff --git a/src/shaders/shader.frag b/src/renderer/shaders/shader.frag similarity index 100% rename from src/shaders/shader.frag rename to src/renderer/shaders/shader.frag diff --git a/src/shaders/shader.vert b/src/renderer/shaders/shader.vert similarity index 58% rename from src/shaders/shader.vert rename to src/renderer/shaders/shader.vert index 9249ae9..64ad809 100644 --- a/src/shaders/shader.vert +++ b/src/renderer/shaders/shader.vert @@ -6,13 +6,17 @@ layout(binding = 0) uniform UniformBufferObject { mat4 proj; } ubo; +layout(std140, binding = 1) readonly buffer storageBuffer { + mat4 model[]; +} ObjectData; + layout(location = 0) in vec2 inPosition; layout(location = 1) in vec3 inColor; layout(location = 0) out vec3 fragColor; void main() { - gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 0.0, 1.0); + gl_Position = ubo.proj * ubo.view * ObjectData.model[gl_InstanceIndex] * vec4(inPosition, 0.0, 1.0); fragColor = inColor; } diff --git a/src/render/AvantGardeRender.hpp b/src/renderer/vulkan/AvantGardeRender.hpp similarity index 100% rename from src/render/AvantGardeRender.hpp rename to src/renderer/vulkan/AvantGardeRender.hpp diff --git a/src/render/drawing.cpp b/src/renderer/vulkan/drawing.cpp similarity index 100% rename from src/render/drawing.cpp rename to src/renderer/vulkan/drawing.cpp diff --git a/src/render/mainLoop.cpp b/src/renderer/vulkan/mainLoop.cpp similarity index 61% rename from src/render/mainLoop.cpp rename to src/renderer/vulkan/mainLoop.cpp index 19a0d08..b989eeb 100644 --- a/src/render/mainLoop.cpp +++ b/src/renderer/vulkan/mainLoop.cpp @@ -8,9 +8,71 @@ #include #include "AvantGardeRender.hpp" +bool KEY_W = false; +bool KEY_S = false; +bool KEY_A = false; +bool KEY_D = false; + +int height = 0; +float offset_y = 0.0f; +float offset_x = 0.0f; + +void GLFW_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { + glfwSetWindowShouldClose(window, GLFW_TRUE); + } + + if (key == GLFW_KEY_W && action == GLFW_PRESS) { + KEY_W = true; + std::cout << 1 << std::endl; + } + if (key == GLFW_KEY_W && action == GLFW_RELEASE) { + KEY_W = false; + std::cout << 0 << std::endl; + } + + if (key == GLFW_KEY_S && action == GLFW_PRESS) { + KEY_S = true; + } + if (key == GLFW_KEY_S && action == GLFW_RELEASE) { + KEY_S = false; + } + + if (key == GLFW_KEY_A && action == GLFW_PRESS) { + KEY_A = true; + } + if (key == GLFW_KEY_A && action == GLFW_RELEASE) { + KEY_A = false; + } + + if (key == GLFW_KEY_D && action == GLFW_PRESS) { + KEY_D = true; + } + if (key == GLFW_KEY_D && action == GLFW_RELEASE) { + KEY_D = false; + } +} void AvantGardeRender::mainLoop() { + glfwSetKeyCallback(window, GLFW_KeyCallback); + while (!glfwWindowShouldClose(window)) { + if (KEY_W) { + std::cout << "W" << std::endl; + offset_y += 10.0f; + } + if (KEY_S) { + std::cout << "S" << std::endl; + offset_y -= 10.0f; + } + if (KEY_A) { + std::cout << "A" << std::endl; + offset_x += 10.0f; + } + if (KEY_D) { + std::cout << "D" << std::endl; + offset_x -= 10.0f; + } glfwPollEvents(); drawFrame(); } @@ -83,14 +145,19 @@ void AvantGardeRender::drawFrame() { void AvantGardeRender::updateUniformBuffer(uint32_t currentImage) { static auto startTime = std::chrono::high_resolution_clock::now(); - auto currentTime = std::chrono::high_resolution_clock::now(); float time = std::chrono::duration(currentTime - startTime).count(); + // UniformBufferObject ubo{}; + // ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(60.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + // ubo.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)); + // ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float) swapChainExtent.height, 0.1f, 10.0f); + // ubo.proj[1][1] *= -1; + UniformBufferObject ubo{}; - ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(60.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.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)); - ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float) swapChainExtent.height, 0.1f, 10.0f); + ubo.proj = glm::perspective(glm::radians(45.0f), (swapChainExtent.width + offset_y) / ((float) swapChainExtent.height + offset_x), 0.1f, 10.0f); ubo.proj[1][1] *= -1; memcpy(uniformBuffersMapped[currentImage], &ubo, sizeof(ubo)); diff --git a/src/render/pipeline.cpp b/src/renderer/vulkan/pipeline.cpp similarity index 100% rename from src/render/pipeline.cpp rename to src/renderer/vulkan/pipeline.cpp diff --git a/src/render/presentation.cpp b/src/renderer/vulkan/presentation.cpp similarity index 100% rename from src/render/presentation.cpp rename to src/renderer/vulkan/presentation.cpp diff --git a/src/render/setupVulkan.cpp b/src/renderer/vulkan/setupVulkan.cpp similarity index 100% rename from src/render/setupVulkan.cpp rename to src/renderer/vulkan/setupVulkan.cpp diff --git a/src/sound/sound.cpp b/src/sound/sound.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/sound/sound.h b/src/sound/sound.h new file mode 100644 index 0000000..e69de29 diff --git a/src/sys/sys.cpp b/src/sys/sys.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/sys/sys.h b/src/sys/sys.h new file mode 100644 index 0000000..e69de29 diff --git a/src/ui/ui_game.cpp b/src/ui/ui_game.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/ui/ui_game.h b/src/ui/ui_game.h new file mode 100644 index 0000000..e69de29 diff --git a/src/ui/ui_home.cpp b/src/ui/ui_home.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/ui/ui_home.h b/src/ui/ui_home.h new file mode 100644 index 0000000..e69de29 diff --git a/src1/README.md b/src1/README.md new file mode 100644 index 0000000..c6935ed --- /dev/null +++ b/src1/README.md @@ -0,0 +1,4 @@ +GetIntoGameDev Tutorial Playlist: https://www.youtube.com/playlist?list=PLn3eTxaOtL2NH5nbPHMK7gE07SqhcAjmk +His Repository: https://github.com/amengede/getIntoGameDev/tree/main/vulkan%202022 + +Using this tutorial to learn and refactor. We really liked his formatting and explanation comparing to the Khronos Vulkan tutorial. \ No newline at end of file diff --git a/src1/engine/config.h b/src1/engine/config.h new file mode 100644 index 0000000..a1a86c3 --- /dev/null +++ b/src1/engine/config.h @@ -0,0 +1,7 @@ +// config.h loads all the libraries +#pragma once + +// #include // c api +#include // cpp api, static load vulkan + +#include diff --git a/src1/engine/engine.cpp b/src1/engine/engine.cpp new file mode 100644 index 0000000..50fdf16 --- /dev/null +++ b/src1/engine/engine.cpp @@ -0,0 +1,50 @@ +#include "engine.h" +#include "instance.h" +#include + +const char* projectName = "AvantGarde"; + +Engine::Engine() { + + if (debugMode) { + std::cout << "Making a graphics engine...\n"; + } + + build_glfw_window(); + + make_instance(); +} + +void Engine::build_glfw_window() { + // init glfw + glfwInit(); + + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + + // window resizing, breaks swapchain, disabled for now + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + if ((window = glfwCreateWindow(width, height, projectName, nullptr, nullptr))) { + if (debugMode) { + std::cout << "Success: glfw window creation, width: " << width << ", height: " << height << '\n'; + } + } else { + if (debugMode) { + std::cout << "Failed: glfw window creation\n"; + } + } +} + +void Engine::make_instance() { + instance = vkInit::make_instance(debugMode, projectName); +} + +Engine::~Engine() { + if (debugMode) { + std::cout << "Closing Engine...\n"; + } + + instance.destroy(); + + glfwTerminate(); +} \ No newline at end of file diff --git a/src1/engine/engine.h b/src1/engine/engine.h new file mode 100644 index 0000000..765f148 --- /dev/null +++ b/src1/engine/engine.h @@ -0,0 +1,31 @@ +#pragma once +// #define GLFW_INCLUDE_VULKAN // includes the vulkan headers +#include + +#include "config.h" +class Engine { + +public: + + Engine(); + + ~Engine(); + +private: + + bool debugMode = true; // to print debug messages in functions + + // glfw parameters + int width { 640 }; + int height { 480 }; + GLFWwindow* window { nullptr }; + + // vulkan instance + vk::Instance instance {nullptr }; + + // glfw setup + void build_glfw_window(); + + // instance setup + void make_instance(); +}; \ No newline at end of file diff --git a/src1/engine/instance.h b/src1/engine/instance.h new file mode 100644 index 0000000..d3a36d3 --- /dev/null +++ b/src1/engine/instance.h @@ -0,0 +1,68 @@ +#pragma once + +// namespace for createion functions/definitions +namespace vkInit { + vk::Instance make_instance(bool debug, const char* applicationName) { + if (debug) { + std::cout << "Making an instance...\n"; + } + + uint32_t version { 0 }; + vkEnumerateInstanceVersion(&version); + if (debug) { + std::cout << "System can support vulkan variant: " << VK_API_VERSION_VARIANT(version) + << ", Major: " << VK_API_VERSION_MAJOR(version) + << ", Minor: " << VK_API_VERSION_MINOR(version) + << ", Patch: " << VK_API_VERSION_PATCH(version) << '\n'; + } + + version &= ~(0xFFFU); // set the patch to 0 for best compatibility/stability + // ^^^ lower 12 bytes are patch + + // or use VK_MAKE_API_VERSION(variant, major, minor, patch) + version = VK_MAKE_API_VERSION(0, 1, 0, 0); + + vk::ApplicationInfo appInfo = vk::ApplicationInfo( + applicationName, + version, + "text", + version, + version + ); + + uint32_t glfwExtensionCount = 0; + const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); + + std::vector extensions(glfwExtensions, glfwExtensions + glfwExtensionCount); + + if (debug) { + std::cout << "Extensions to be requested: \n"; + for (const char* extensionName : extensions) { + std::cout << "\t\"" << extensionName << "\"\n"; + } + } + + + vk::InstanceCreateInfo createInfo = vk::InstanceCreateInfo( + vk::InstanceCreateFlags(). + &appInfo, + 0, nullptr, // enabled layers + static_cast(extension.size()), extensions,data() // enabled extensions + ); + + try { + return vk::createInstance(createInfo, nullptr); + } + catch (vk::SystemError err) { + if (debug) { + std::cout << "Failed: create instance" + } + } + + if (debug) { + std::cout << "Success: instance creation" + } + + return nullptr; + } +} \ No newline at end of file diff --git a/src1/graphics_pipeline_notes.pdf b/src1/graphics_pipeline_notes.pdf new file mode 100644 index 0000000..ba22312 Binary files /dev/null and b/src1/graphics_pipeline_notes.pdf differ diff --git a/src1/main.cpp b/src1/main.cpp new file mode 100644 index 0000000..6cc5acd --- /dev/null +++ b/src1/main.cpp @@ -0,0 +1,9 @@ +#include "./engine/engine.h" + +int main() { + Engine* graphicsEngine = new Engine(); + + delete graphicsEngine; + + return 0; +} \ No newline at end of file