2022-09-19 01:20:51 +00:00
|
|
|
#include "first_app.hpp"
|
|
|
|
|
|
|
|
#include "xe_camera.hpp"
|
|
|
|
#include "xe_game_object.hpp"
|
|
|
|
#include "xe_model.hpp"
|
|
|
|
#include "keyboard_movement_controller.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/constants.hpp>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cassert>
|
|
|
|
#include <stdexcept>
|
2022-09-19 20:35:45 +00:00
|
|
|
#include <iostream>
|
2022-09-19 01:20:51 +00:00
|
|
|
namespace xe {
|
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
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};
|
2022-09-19 01:20:51 +00:00
|
|
|
};
|
|
|
|
|
2022-09-19 11:08:42 +00:00
|
|
|
FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Hello, Vulkan!"} {
|
2022-09-19 01:20:51 +00:00
|
|
|
loadGameObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
FirstApp::~FirstApp() {}
|
|
|
|
|
|
|
|
void FirstApp::run() {
|
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
std::unique_ptr<XeRenderSystem> simpleRenderSystem = xeEngine.createRenderSystem(
|
|
|
|
"res/shaders/simple_shader.vert.spv",
|
|
|
|
"res/shaders/simple_shader.frag.spv",
|
|
|
|
sizeof(PushConstant),
|
|
|
|
sizeof(UniformBuffer));
|
|
|
|
|
2022-09-19 01:20:51 +00:00
|
|
|
XeCamera camera{};
|
|
|
|
camera.setViewTarget(glm::vec3(-1.f, -2.f, 20.f), glm::vec3(0.f, 0.f, 2.5f));
|
|
|
|
|
|
|
|
auto viewerObject = XeGameObject::createGameObject();
|
|
|
|
KeyboardMovementController cameraController{};
|
|
|
|
|
|
|
|
auto currentTime = std::chrono::high_resolution_clock::now();
|
|
|
|
|
2022-09-19 16:54:23 +00:00
|
|
|
while (!xeEngine.getWindow().shouldClose()) {
|
2022-09-19 01:20:51 +00:00
|
|
|
glfwPollEvents();
|
|
|
|
|
|
|
|
auto newTime = std::chrono::high_resolution_clock::now();
|
|
|
|
float frameTime = std::chrono::duration<float, std::chrono::seconds::period>(newTime - currentTime).count();
|
|
|
|
currentTime = newTime;
|
|
|
|
|
2022-09-19 16:54:23 +00:00
|
|
|
cameraController.moveInPlaneXZ(xeEngine.getWindow().getGLFWwindow(), frameTime, viewerObject);
|
2022-09-19 01:20:51 +00:00
|
|
|
camera.setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation);
|
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
float aspect = xeEngine.getRenderer().getAspectRatio();
|
2022-09-19 01:20:51 +00:00
|
|
|
camera.setPerspectiveProjection(glm::radians(50.f), aspect, 0.1f, 100.f);
|
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
if(xeEngine.beginFrame()) {
|
2022-09-19 01:20:51 +00:00
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
PushConstant pc{};
|
|
|
|
pc.modelMatrix = gameObjects[0].transform.mat4();
|
|
|
|
pc.normalMatrix = gameObjects[0].transform.normalMatrix();
|
|
|
|
|
|
|
|
UniformBuffer ubo{};
|
2022-09-19 01:20:51 +00:00
|
|
|
ubo.projectionView = camera.getProjection() * camera.getView();
|
2022-09-19 20:35:45 +00:00
|
|
|
|
|
|
|
xeEngine.render(
|
|
|
|
*simpleRenderSystem,
|
|
|
|
gameObjects,
|
|
|
|
&pc,
|
|
|
|
sizeof(pc),
|
|
|
|
&ubo,
|
|
|
|
sizeof(ubo)
|
|
|
|
);
|
2022-09-19 01:20:51 +00:00
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
xeEngine.endFrame();
|
|
|
|
|
|
|
|
}
|
2022-09-19 01:20:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-19 20:35:45 +00:00
|
|
|
xeEngine.close();
|
2022-09-19 01:20:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FirstApp::loadGameObjects() {
|
2022-09-19 20:35:45 +00:00
|
|
|
std::shared_ptr<XeModel> xeModel = xeEngine.createModel("res/models/stanford-dragon.obj");
|
2022-09-19 01:20:51 +00:00
|
|
|
|
|
|
|
auto cube = XeGameObject::createGameObject();
|
|
|
|
cube.model = xeModel;
|
|
|
|
cube.transform.translation = {.0f, .0f, 2.5f};
|
|
|
|
cube.transform.scale = {.5f, .5f, .5f};
|
|
|
|
gameObjects.push_back(std::move(cube));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|