finish refactor

This commit is contained in:
tylermurphy534 2022-09-19 21:28:41 -04:00
parent 9f3ed45b12
commit 8cc0b081ef
16 changed files with 195 additions and 223 deletions

View file

@ -1 +0,0 @@
VULKAN_SDK=/home/tylerm/Documents/Vulkan

View file

@ -1,12 +1,9 @@
include .env
CC = clang++ CC = clang++
INCFLAGS = -Isrc INCFLAGS = -Isrc
INCFLAGS += -Iengine INCFLAGS += -Iengine
INCFLAGS += -Ilib/glfw/include INCFLAGS += -Ilib/glfw/include
INCFLAGS += -Ilib/glm INCFLAGS += -Ilib/glm
INCFLAGS += -L${VULKAN_SDK}/lib -lvulkan
CCFLAGS = -std=c++17 -O2 -g CCFLAGS = -std=c++17 -O2 -g
CCFLAGS += $(INCFLAGS) CCFLAGS += $(INCFLAGS)
@ -15,6 +12,7 @@ LDFLAGS = -lm
LDFLAGS += $(INCFLAGS) LDFLAGS += $(INCFLAGS)
LDFLAGS += lib/glfw/src/libglfw3.a LDFLAGS += lib/glfw/src/libglfw3.a
LDFLAGS += lib/glm/glm/libglm_static.a LDFLAGS += lib/glm/glm/libglm_static.a
LDFLAGS += -lvulkan
SRC = $(shell find src -name "*.cpp") SRC = $(shell find src -name "*.cpp")
SRC += $(shell find engine -name "*.cpp") SRC += $(shell find engine -name "*.cpp")

View file

@ -1,16 +1,14 @@
#include "xe_engine.hpp" #include "xe_engine.hpp"
#include "xe_descriptors.hpp" #include <chrono>
#include <string>
namespace xe { namespace xe {
XeEngine::XeEngine(int width, int height, std::string name) XeEngine::XeEngine(int width, int height, std::string name) : xeWindow{width, height, name},
: xeWindow{width, height, name}, xeDevice{xeWindow},
xeDevice{xeWindow}, xeRenderer{xeWindow, xeDevice},
xeRenderer{xeWindow, xeDevice}, xeCamera{} {
xeCamera{} { loadDescriptors();
loadDescriptors(); };
};
void XeEngine::loadDescriptors() { void XeEngine::loadDescriptors() {
xeDescriptorPool = XeDescriptorPool::Builder(xeDevice) xeDescriptorPool = XeDescriptorPool::Builder(xeDevice)
@ -24,45 +22,27 @@ void XeEngine::loadDescriptors() {
.build(); .build();
} }
std::unique_ptr<XeRenderSystem> XeEngine::createRenderSystem(const std::string &vert, const std::string &frag, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize) { std::shared_ptr<XeModel> XeEngine::loadModelFromFile(const std::string &filename) {
return std::make_unique<XeRenderSystem>(
xeDevice,
xeRenderer,
*xeDescriptorPool,
*xeDescriptorSetLayout,
vert,
frag,
pushCunstantDataSize,
uniformBufferDataSize
);
}
std::shared_ptr<XeModel> XeEngine::createModel(const std::string &filename) {
return XeModel::createModelFromFile(xeDevice, filename); return XeModel::createModelFromFile(xeDevice, filename);
} }
void XeEngine::render( std::shared_ptr<XeModel> XeEngine::loadModelFromData(std::vector<XeModel::Vertex> vertices, std::vector<uint32_t> indices) {
XeRenderSystem& xeRenderSystem, XeModel::Builder builder{};
std::vector<XeGameObject>& gameObjects, builder.vertices = vertices;
void* pushConstantData, if(&indices == NULL) {
uint32_t pushConstantSize, builder.indices = indices;
void* uniformBufferData, }
uint32_t uniformBufferSize) { return std::make_shared<XeModel>(xeDevice, builder);
}
auto commandBuffer = xeRenderer.getCurrentCommandBuffer(); bool XeEngine::poll() {
xeRenderer.beginSwapChainRenderPass(commandBuffer); glfwPollEvents();
auto newTime = std::chrono::high_resolution_clock::now();
xeRenderSystem.renderGameObjects( frameTime = std::chrono::duration<float, std::chrono::seconds::period>(newTime - currentTime).count();
xeRenderer.getFrameIndex(), currentTime = newTime;
commandBuffer, float aspect = xeRenderer.getAspectRatio();
gameObjects, xeCamera.setPerspectiveProjection(glm::radians(FOV), aspect, 0.1f, 100.f);
pushConstantData, return !xeWindow.shouldClose();
pushConstantSize,
uniformBufferData,
uniformBufferSize
);
xeRenderer.endSwapChainRenderPass(commandBuffer);
} }
} }

View file

@ -3,8 +3,9 @@
#include "xe_window.hpp" #include "xe_window.hpp"
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_render_system.hpp" #include "xe_camera.hpp"
#include "xe_descriptors.hpp" #include "xe_descriptors.hpp"
#include <chrono>
#include <string> #include <string>
namespace xe { namespace xe {
@ -14,41 +15,25 @@ class XeEngine {
public: public:
XeEngine(int width, int height, std::string name); XeEngine(int width, int height, std::string name);
~XeEngine() {}; ~XeEngine() {};
XeEngine(const XeEngine&) = delete; XeEngine(const XeEngine&) = delete;
XeEngine operator=(const XeEngine&) = delete; XeEngine operator=(const XeEngine&) = delete;
XeWindow& getWindow() { XeWindow& getWindow() {return xeWindow;}
return xeWindow; XeCamera& getCamera() {return xeCamera;}
};
XeRenderer& getRenderer() { std::shared_ptr<XeModel> loadModelFromFile(const std::string &filename);
return xeRenderer; std::shared_ptr<XeModel> loadModelFromData(std::vector<XeModel::Vertex> vertices, std::vector<uint32_t> indices);
};
XeCamera& getCamera() {
return xeCamera;
};
std::unique_ptr<XeRenderSystem> createRenderSystem(const std::string &vert, const std::string &frag, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize);
std::shared_ptr<XeModel> createModel(const std::string &filename);
bool beginFrame() { return xeRenderer.beginFrame(); } bool beginFrame() { return xeRenderer.beginFrame(); }
void endFrame() { xeRenderer.endFrame(); }
void endFrame() { return xeRenderer.endFrame(); }
void render(
XeRenderSystem& xeRenderSystem,
std::vector<XeGameObject>& gameObjects,
void* pushConstantData,
uint32_t pushConstantSize,
void* uniformBufferData,
uint32_t uniformBufferSize);
void close() { vkDeviceWaitIdle(xeDevice.device()); } void close() { vkDeviceWaitIdle(xeDevice.device()); }
bool poll();
float getFrameTime() { return frameTime; }
private: private:
void loadDescriptors(); void loadDescriptors();
@ -58,8 +43,15 @@ class XeEngine {
XeRenderer xeRenderer; XeRenderer xeRenderer;
XeCamera xeCamera; XeCamera xeCamera;
std::chrono::_V2::system_clock::time_point currentTime;
float frameTime;
float FOV = 50.f;
std::unique_ptr<XeDescriptorPool> xeDescriptorPool; std::unique_ptr<XeDescriptorPool> xeDescriptorPool;
std::unique_ptr<XeDescriptorSetLayout> xeDescriptorSetLayout; std::unique_ptr<XeDescriptorSetLayout> xeDescriptorSetLayout;
friend class XeRenderSystem;
}; };
} }

View file

@ -1,16 +0,0 @@
#pragma once
#include "xe_camera.hpp"
#include <vulkan/vulkan.h>
namespace xe {
struct XeFrameInfo {
int frameIndex;
float frameTime;
VkCommandBuffer commandBuffer;
XeCamera &camera;
};
}

View file

@ -3,11 +3,11 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_pipeline.hpp" #include "xe_pipeline.hpp"
#include "xe_frame_info.hpp"
#include "xe_game_object.hpp" #include "xe_game_object.hpp"
#include "xe_swap_chain.hpp" #include "xe_swap_chain.hpp"
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_descriptors.hpp" #include "xe_descriptors.hpp"
#include "xe_engine.hpp"
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
@ -17,18 +17,15 @@
namespace xe { namespace xe {
XeRenderSystem::XeRenderSystem( XeRenderSystem::XeRenderSystem(
XeDevice &device, XeEngine &xeEngine,
XeRenderer &renderer,
XeDescriptorPool &xeDescriptorPool,
XeDescriptorSetLayout &xeDescriptorSetLayout,
std::string vert, std::string vert,
std::string frag, std::string frag,
uint32_t pushCunstantDataSize, uint32_t pushCunstantDataSize,
uint32_t uniformBufferDataSize) uint32_t uniformBufferDataSize)
: xeDevice{device} { : xeDevice{xeEngine.xeDevice}, xeRenderer{xeEngine.xeRenderer} {
createUniformBuffers(xeDescriptorPool, xeDescriptorSetLayout, uniformBufferDataSize); createUniformBuffers(*xeEngine.xeDescriptorPool, *xeEngine.xeDescriptorSetLayout, uniformBufferDataSize);
createPipelineLayout(xeDescriptorSetLayout, pushCunstantDataSize, uniformBufferDataSize); createPipelineLayout(*xeEngine.xeDescriptorSetLayout, pushCunstantDataSize, uniformBufferDataSize);
createPipeline(renderer.getSwapChainRenderPass(), vert, frag); createPipeline(xeRenderer.getSwapChainRenderPass(), vert, frag);
} }
@ -108,55 +105,54 @@ void XeRenderSystem::createPipeline(VkRenderPass renderPass, std::string vert, s
); );
} }
void XeRenderSystem::renderGameObjects( void XeRenderSystem::loadPushConstant(void *pushConstantData, uint32_t pushConstantSize) {
int frameIndex, if(!boundPipeline) {
VkCommandBuffer commandBuffer, xeRenderer.beginSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
std::vector<XeGameObject> &gameObjects, xePipeline->bind(xeRenderer.getCurrentCommandBuffer());
void *pushConstantData, boundPipeline = true;
uint32_t pushConstantSize, }
void* uniformBufferData, if(!boundDescriptor) {
uint32_t uniformBufferSize) {
uboBuffers[frameIndex]->writeToBuffer(uniformBufferData);
uboBuffers[frameIndex]->flush();
xePipeline->bind(commandBuffer);
if(pushConstantSize > 0) {
vkCmdBindDescriptorSets( vkCmdBindDescriptorSets(
commandBuffer, xeRenderer.getCurrentCommandBuffer(),
VK_PIPELINE_BIND_POINT_GRAPHICS, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout, pipelineLayout,
0, 0,
1, 1,
&descriptorSets[frameIndex], &descriptorSets[xeRenderer.getFrameIndex()],
0, 0,
nullptr); nullptr);
boundDescriptor = true;
} }
vkCmdPushConstants(
for (auto& obj: gameObjects) { xeRenderer.getCurrentCommandBuffer(),
struct PushConstant {
glm::mat4 modelMatrix;
glm::mat4 normalMatrix;
};
PushConstant pc = PushConstant{obj.transform.mat4(), obj.transform.normalMatrix()};
if(pushConstantSize > 0) {
vkCmdPushConstants(
commandBuffer,
pipelineLayout, pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0, 0,
pushConstantSize, pushConstantSize,
&pc); pushConstantData);
} }
obj.model->bind(commandBuffer); void XeRenderSystem::loadUniformObject(void *uniformBufferData, uint32_t uniformBufferSize) {
obj.model->draw(commandBuffer); uboBuffers[xeRenderer.getFrameIndex()]->writeToBuffer(uniformBufferData);
uboBuffers[xeRenderer.getFrameIndex()]->flush();
}
void XeRenderSystem::render(XeGameObject &gameObject) {
if(!boundPipeline){
xeRenderer.beginSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
xePipeline->bind(xeRenderer.getCurrentCommandBuffer());
boundPipeline = true;
} }
gameObject.model->bind(xeRenderer.getCurrentCommandBuffer());
gameObject.model->draw(xeRenderer.getCurrentCommandBuffer());
}
void XeRenderSystem::stop() {
boundPipeline = false;
boundDescriptor = false;
xeRenderer.endSwapChainRenderPass(xeRenderer.getCurrentCommandBuffer());
} }
} }

View file

@ -3,10 +3,10 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include "xe_device.hpp" #include "xe_device.hpp"
#include "xe_pipeline.hpp" #include "xe_pipeline.hpp"
#include "xe_frame_info.hpp"
#include "xe_game_object.hpp" #include "xe_game_object.hpp"
#include "xe_descriptors.hpp" #include "xe_descriptors.hpp"
#include "xe_renderer.hpp" #include "xe_renderer.hpp"
#include "xe_engine.hpp"
#include <memory> #include <memory>
@ -14,42 +14,36 @@ namespace xe {
class XeRenderSystem { class XeRenderSystem {
public: public:
struct XeData{};
XeRenderSystem( XeRenderSystem(
XeDevice &device, XeEngine &xeEngine,
XeRenderer &renderer,
XeDescriptorPool &xeDescriptorPool,
XeDescriptorSetLayout &xeDescriptorSetLayout,
std::string vert, std::string vert,
std::string frag, std::string frag,
uint32_t pushCunstantDataSize, uint32_t pushCunstantDataSize,
uint32_t uniformBufferDataSize uint32_t uniformBufferDataSize
); );
~XeRenderSystem(); ~XeRenderSystem();
XeRenderSystem(const XeRenderSystem &) = delete; XeRenderSystem(const XeRenderSystem &) = delete;
XeRenderSystem operator=(const XeRenderSystem &) = delete; XeRenderSystem operator=(const XeRenderSystem &) = delete;
void setUnifroms(XeFrameInfo & frameInfo); void loadPushConstant(void *pushConstantData, uint32_t pushConstantSize);
void loadUniformObject(void *uniformBufferData, uint32_t uniformBufferSize);
void renderGameObjects( void render(XeGameObject &gameObject);
int frameIndex, void stop();
VkCommandBuffer commandBuffer,
std::vector<XeGameObject> &gameObjects,
void *pushConstantData,
uint32_t pushConstantSize,
void* uniformBufferData,
uint32_t uniformBufferSize);
private: private:
void createUniformBuffers(XeDescriptorPool &xeDescriptorPool, XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t uniformBufferDataSize); void createUniformBuffers(XeDescriptorPool &xeDescriptorPool, XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t uniformBufferDataSize);
void createPipelineLayout(XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize); void createPipelineLayout(XeDescriptorSetLayout &xeDescriptorSetLayout, uint32_t pushCunstantDataSize, uint32_t uniformBufferDataSize);
void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag); void createPipeline(VkRenderPass renderPass, std::string vert, std::string frag);
bool boundPipeline{false};
bool boundDescriptor{false};
XeDevice& xeDevice; XeDevice& xeDevice;
XeRenderer& xeRenderer;
std::unique_ptr<XePipeline> xePipeline; std::unique_ptr<XePipeline> xePipeline;
std::vector<std::unique_ptr<XeBuffer>> uboBuffers; std::vector<std::unique_ptr<XeBuffer>> uboBuffers;

View file

@ -26,5 +26,5 @@ void main() {
float lightIntensity = AMBIENT + max(dot(normalWorldSpace, ubo.directionToLight), 0); float lightIntensity = AMBIENT + max(dot(normalWorldSpace, ubo.directionToLight), 0);
fragColor = lightIntensity * color; fragColor = lightIntensity * vec3(1/position.y,position.y,clamp(sin(position.x - position.z), 0, 1));
} }

Binary file not shown.

View file

@ -1,32 +1,20 @@
#include "first_app.hpp" #include "first_app.hpp"
#include "xe_camera.hpp" #include "xe_camera.hpp"
#include "xe_engine.hpp"
#include "xe_game_object.hpp" #include "xe_game_object.hpp"
#include "xe_model.hpp" #include "xe_model.hpp"
#include "xe_render_system.hpp"
#include "keyboard_movement_controller.hpp" #include "keyboard_movement_controller.hpp"
#include "simple_renderer.hpp"
#define GLM_FORCE_RADIANS #define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE #define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
#include <array> #include <array>
#include <chrono>
#include <cassert>
#include <stdexcept>
#include <iostream>
namespace xe {
struct UniformBuffer { namespace app {
alignas(16) glm::mat4 projectionView{1.f};
alignas(4) glm::vec3 lightDirection = glm::normalize(glm::vec3{-1.f, 3.f, 1.f});
};
struct PushConstant {
alignas(16) glm::mat4 modelMatrix{1.f};
alignas(16) glm::mat4 normalMatrix{1.f};
};
FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Hello, Vulkan!"} { FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Hello, Vulkan!"} {
loadGameObjects(); loadGameObjects();
@ -36,53 +24,23 @@ FirstApp::~FirstApp() {}
void FirstApp::run() { void FirstApp::run() {
std::unique_ptr<XeRenderSystem> simpleRenderSystem = xeEngine.createRenderSystem( SimpleRenderer renderer{xeEngine};
"res/shaders/simple_shader.vert.spv",
"res/shaders/simple_shader.frag.spv", xeEngine.getCamera().setViewTarget(glm::vec3(-1.f, -2.f, 20.f), glm::vec3(0.f, 0.f, 2.5f));
sizeof(PushConstant),
sizeof(UniformBuffer));
XeCamera camera{}; auto viewerObject = xe::XeGameObject::createGameObject();
camera.setViewTarget(glm::vec3(-1.f, -2.f, 20.f), glm::vec3(0.f, 0.f, 2.5f));
auto viewerObject = XeGameObject::createGameObject();
KeyboardMovementController cameraController{}; KeyboardMovementController cameraController{};
auto currentTime = std::chrono::high_resolution_clock::now(); while (xeEngine.poll()) {
while (!xeEngine.getWindow().shouldClose()) { float frameTime = xeEngine.getFrameTime();
glfwPollEvents();
auto newTime = std::chrono::high_resolution_clock::now();
float frameTime = std::chrono::duration<float, std::chrono::seconds::period>(newTime - currentTime).count();
currentTime = newTime;
cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject); cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject);
camera.setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);
float aspect = xeEngine.getRenderer().getAspectRatio();
camera.setPerspectiveProjection(glm::radians(50.f), aspect, 0.1f, 100.f);
if(xeEngine.beginFrame()) { if(xeEngine.beginFrame()) {
renderer.render(gameObjects, xeEngine.getCamera());
PushConstant pc{};
pc.modelMatrix = gameObjects[0].transform.mat4();
pc.normalMatrix = gameObjects[0].transform.normalMatrix();
UniformBuffer ubo{};
ubo.projectionView = camera.getProjection() * camera.getView();
xeEngine.render(
*simpleRenderSystem,
gameObjects,
&pc,
sizeof(pc),
&ubo,
sizeof(ubo)
);
xeEngine.endFrame(); xeEngine.endFrame();
} }
} }
@ -91,13 +49,20 @@ void FirstApp::run() {
} }
void FirstApp::loadGameObjects() { void FirstApp::loadGameObjects() {
std::shared_ptr<XeModel> xeModel = xeEngine.createModel("res/models/stanford-dragon.obj"); std::shared_ptr<xe::XeModel> xeModel = xeEngine.loadModelFromFile("res/models/stanford-dragon.obj");
auto cube = XeGameObject::createGameObject(); auto dragon = xe::XeGameObject::createGameObject();
cube.model = xeModel; dragon.model = xeModel;
cube.transform.translation = {.0f, .0f, 2.5f}; dragon.transform.translation = {.0f, .0f, 2.5f};
cube.transform.scale = {.5f, .5f, .5f}; dragon.transform.scale = {.5f, .5f, .5f};
gameObjects.push_back(std::move(cube)); gameObjects.push_back(std::move(dragon));
auto dragon2 = xe::XeGameObject::createGameObject();
dragon2.model = xeModel;
dragon2.transform.translation = {5.0f, .0f, -1.5f};
dragon2.transform.rotation.y = glm::radians(90.f);
dragon2.transform.scale = {.35f, .35f, .35f};
gameObjects.push_back(std::move(dragon2));
} }
} }

View file

@ -7,11 +7,9 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
namespace xe { namespace app {
class FirstApp { class FirstApp {
public: public:
static constexpr int WIDTH = 800;
static constexpr int HEIGHT = 600;
FirstApp(); FirstApp();
~FirstApp(); ~FirstApp();
@ -22,10 +20,14 @@ class FirstApp {
void run(); void run();
private: private:
static constexpr int WIDTH = 800;
static constexpr int HEIGHT = 600;
void loadGameObjects(); void loadGameObjects();
XeEngine xeEngine; xe::XeEngine xeEngine;
std::vector<XeGameObject> gameObjects; std::vector<xe::XeGameObject> gameObjects;
}; };
} }

View file

@ -4,9 +4,9 @@
#include <glm/geometric.hpp> #include <glm/geometric.hpp>
#include <limits> #include <limits>
namespace xe { namespace app {
void KeyboardMovementController::moveInPlaneXZ(GLFWwindow* window, float dt, XeGameObject& gameObject) { void KeyboardMovementController::moveInPlaneXZ(GLFWwindow* window, float dt, xe::XeGameObject& gameObject) {
glm::vec3 rotate{0}; glm::vec3 rotate{0};
if(glfwGetKey(window, keys.lookRight) == GLFW_PRESS) rotate.y += 1.f; if(glfwGetKey(window, keys.lookRight) == GLFW_PRESS) rotate.y += 1.f;
if(glfwGetKey(window, keys.lookLeft) == GLFW_PRESS) rotate.y -= 1.f; if(glfwGetKey(window, keys.lookLeft) == GLFW_PRESS) rotate.y -= 1.f;

View file

@ -4,7 +4,7 @@
#include "xe_window.hpp" #include "xe_window.hpp"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace xe { namespace app {
class KeyboardMovementController { class KeyboardMovementController {
public: public:
@ -21,7 +21,7 @@ namespace xe {
int lookDown = GLFW_KEY_DOWN; int lookDown = GLFW_KEY_DOWN;
}; };
void moveInPlaneXZ(GLFWwindow* window, float dt, XeGameObject& gameObject); void moveInPlaneXZ(GLFWwindow* window, float dt, xe::XeGameObject& gameObject);
KeyMappings keys{}; KeyMappings keys{};
float moveSpeed{3.f}; float moveSpeed{3.f};

View file

@ -5,7 +5,7 @@
#include <stdexcept> #include <stdexcept>
int main() { int main() {
xe::FirstApp app{}; app::FirstApp app{};
try { try {
app.run(); app.run();

26
src/simple_renderer.cpp Normal file
View file

@ -0,0 +1,26 @@
#include "simple_renderer.hpp"
namespace app {
SimpleRenderer::SimpleRenderer(xe::XeEngine &xeEngine)
: xeRenderSystem{xeEngine, "res/shaders/simple_shader.vert.spv", "res/shaders/simple_shader.frag.spv", sizeof(PushConstant), sizeof(UniformBuffer)} {};
void SimpleRenderer::render(std::vector<xe::XeGameObject> &gameObjects, xe::XeCamera &xeCamera) {
UniformBuffer ubo{};
ubo.projectionView = xeCamera.getProjection() * xeCamera.getView();
xeRenderSystem.loadUniformObject(&ubo, sizeof(ubo));
for(auto &obj : gameObjects) {
PushConstant pc{};
pc.modelMatrix = obj.transform.mat4();
pc.normalMatrix = obj.transform.normalMatrix();
xeRenderSystem.loadPushConstant(&pc, sizeof(pc));
xeRenderSystem.render(obj);
}
xeRenderSystem.stop();
}
}

36
src/simple_renderer.hpp Normal file
View file

@ -0,0 +1,36 @@
#pragma once
#include "xe_render_system.hpp"
#include <string>
namespace app {
struct UniformBuffer {
alignas(16) glm::mat4 projectionView{1.f};
alignas(4) glm::vec3 lightDirection = glm::normalize(glm::vec3{-1.f, 3.f, 1.f});
};
struct PushConstant {
alignas(16) glm::mat4 modelMatrix{1.f};
alignas(16) glm::mat4 normalMatrix{1.f};
};
class SimpleRenderer {
public:
SimpleRenderer(xe::XeEngine &xeEngine);
~SimpleRenderer() {};
SimpleRenderer(const SimpleRenderer&) = delete;
SimpleRenderer operator=(const SimpleRenderer&) = delete;
void render(std::vector<xe::XeGameObject> &gameObjects, xe::XeCamera &xeCamera);
private:
xe::XeRenderSystem xeRenderSystem;
};
}