Skip to content

Commit

Permalink
Mesh import
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyJazz committed Dec 23, 2023
1 parent f791353 commit dcca828
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 8 deletions.
64 changes: 63 additions & 1 deletion src/GameX/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ void Application::Cleanup() {
}

void Application::Update() {
double current_time = glfwGetTime();
static double last_time = current_time;
double delta_time = current_time - last_time;

static float omega = 0.0f;

static_entity_->SetEntitySettings(Entity::EntitySettings{
glm::translate(glm::mat4(1.0f), glm::vec3(-0.5, 0.0, 0.0)) *
glm::rotate(glm::mat4(1.0f), omega, glm::vec3(0, 1, 0))});
dynamic_entity_->SetEntitySettings(Entity::EntitySettings{
glm::translate(glm::mat4(1.0f), glm::vec3(0.5, 0.0, 0.0)) *
glm::rotate(glm::mat4(1.0f), omega, glm::vec3(0, 1, 0))});

omega += delta_time;

last_time = current_time;
}

void Application::Render() {
Expand Down Expand Up @@ -123,6 +139,52 @@ void Application::Render() {
renderer_->RenderPipeline()->Render(cmd_buffer->Handle(), *scene_, *camera_,
*film_);

auto output_image = film_->albedo_image.get();
// Blit output_image to frame_image_
VkImageBlit blit_region = {};
blit_region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
blit_region.srcSubresource.layerCount = 1;
blit_region.srcOffsets[1].x = output_image->Extent().width;
blit_region.srcOffsets[1].y = output_image->Extent().height;
blit_region.srcOffsets[1].z = 1;
blit_region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
blit_region.dstSubresource.layerCount = 1;
blit_region.dstOffsets[1].x = core_->SwapChain()->Extent().width;
blit_region.dstOffsets[1].y = core_->SwapChain()->Extent().height;
blit_region.dstOffsets[1].z = 1;

grassland::vulkan::TransitImageLayout(
cmd_buffer->Handle(), output_image->Handle(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_SHADER_READ_BIT, VK_ACCESS_TRANSFER_READ_BIT,
VK_IMAGE_ASPECT_COLOR_BIT);
grassland::vulkan::TransitImageLayout(
cmd_buffer->Handle(), frame_image, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_MEMORY_READ_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_IMAGE_ASPECT_COLOR_BIT);

vkCmdBlitImage(cmd_buffer->Handle(), output_image->Handle(),
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, frame_image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit_region,
VK_FILTER_NEAREST);

grassland::vulkan::TransitImageLayout(
cmd_buffer->Handle(), output_image->Handle(),
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_ACCESS_TRANSFER_READ_BIT,
VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_ASPECT_COLOR_BIT);

grassland::vulkan::TransitImageLayout(
cmd_buffer->Handle(), frame_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_ACCESS_MEMORY_READ_BIT, VK_IMAGE_ASPECT_COLOR_BIT);

core_->EndFrame();
}

Expand All @@ -143,7 +205,7 @@ void Application::CreateCube() {
2, 6, 7, 2, 7, 3, 0, 4, 6, 0, 6, 2, 1, 3, 7, 1, 7, 5,
};

cube_ = Mesh(vertices, indices);
cube_ = Mesh(renderer_->AssetManager()->GetAssetPath("models/cube.ply"));

static_cube_ = std::make_unique<StaticModel>(Renderer(), cube_);
dynamic_cube_ = std::make_unique<DynamicModel>(Renderer(), &cube_);
Expand Down
1 change: 0 additions & 1 deletion src/GameX/renderer/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "GameX/application/application.h"
#include "GameX/renderer/renderer.h"
#include "glm/gtc/matrix_transform.hpp"

namespace GameX {

Expand Down
4 changes: 4 additions & 0 deletions src/GameX/renderer/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Camera {
return descriptor_sets_[frame_index].get();
}

void SetCameraData(const CameraData &data) {
camera_buffer_->At(0) = data;
}

private:
Scene *scene_;
std::unique_ptr<grassland::vulkan::DynamicBuffer<CameraData>> camera_buffer_;
Expand Down
4 changes: 4 additions & 0 deletions src/GameX/renderer/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class Entity {
return model_;
}

void SetEntitySettings(const EntitySettings &settings) {
entity_buffer_->At(0) = settings;
}

private:
class Scene *scene_;
const class Model *model_;
Expand Down
12 changes: 8 additions & 4 deletions src/GameX/renderer/render_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,20 @@ std::unique_ptr<RenderPipeline::Film> RenderPipeline::CreateFilm(int width,
static_cast<uint32_t>(height)};
film->albedo_image = std::make_unique<grassland::vulkan::Image>(
renderer_->App()->Core(), VK_FORMAT_R32G32B32A32_SFLOAT, extent,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
film->normal_image = std::make_unique<grassland::vulkan::Image>(
renderer_->App()->Core(), VK_FORMAT_R32G32B32A32_SFLOAT, extent,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
film->position_image = std::make_unique<grassland::vulkan::Image>(
renderer_->App()->Core(), VK_FORMAT_R32G32B32A32_SFLOAT, extent,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
film->depth_image = std::make_unique<grassland::vulkan::Image>(
renderer_->App()->Core(), VK_FORMAT_D32_SFLOAT, extent,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
VK_IMAGE_ASPECT_DEPTH_BIT);
film->framebuffer = std::make_unique<grassland::vulkan::Framebuffer>(
renderer_->App()->Core(), extent, geometry_pass_render_pass_->Handle(),
Expand Down
4 changes: 3 additions & 1 deletion src/GameX/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
set(LIB_NAME GameXUtils)

find_package(assimp CONFIG REQUIRED)

file(GLOB_RECURSE SOURCES *.cpp *.h)

add_library(${LIB_NAME} ${SOURCES}
common.cpp
common.h)

target_include_directories(${LIB_NAME} PUBLIC ${COMMON_INCLUDE_DIRS})
target_link_libraries(${LIB_NAME} PUBLIC grassland)
target_link_libraries(${LIB_NAME} PUBLIC grassland assimp::assimp)

list(APPEND GAMEX_LIBS ${LIB_NAME})
set(GAMEX_LIBS ${GAMEX_LIBS} PARENT_SCOPE)
Expand Down
54 changes: 54 additions & 0 deletions src/GameX/utils/mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "GameX/utils/mesh.h"

// Include assimp
#include "assimp/Importer.hpp"
#include "assimp/postprocess.h"
#include "assimp/scene.h"

namespace GameX {
Mesh::Mesh(const std::vector<Vertex> &vertices,
const std::vector<uint32_t> &indices) {
Expand Down Expand Up @@ -35,4 +40,53 @@ std::vector<uint32_t> &Mesh::Indices() {
const std::vector<uint32_t> &Mesh::Indices() const {
return indices_;
}

Mesh::Mesh(const std::string &path) {
LAND_INFO("Loading model: {}", path);
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(
path, aiProcess_Triangulate | aiProcess_FlipUVs |
aiProcess_CalcTangentSpace | aiProcess_JoinIdenticalVertices);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE ||
!scene->mRootNode) {
LAND_ERROR("Failed to load model: {}", path);
}

std::vector<Vertex> vertices;
std::vector<uint32_t> indices;

for (uint32_t i = 0; i < scene->mNumMeshes; ++i) {
const aiMesh *mesh = scene->mMeshes[i];
for (uint32_t j = 0; j < mesh->mNumVertices; ++j) {
Vertex vertex;
vertex.position = {mesh->mVertices[j].x, mesh->mVertices[j].y,
mesh->mVertices[j].z};
vertex.color = {1.0f, 1.0f, 1.0f};
if (mesh->mColors[0]) {
vertex.color = {mesh->mColors[0][j].r, mesh->mColors[0][j].g,
mesh->mColors[0][j].b};
}
vertex.texCoord = {0.0f, 0.0f};
if (mesh->mTextureCoords[0]) {
vertex.texCoord = {mesh->mTextureCoords[0][j].x,
mesh->mTextureCoords[0][j].y};
}
vertex.normal = {mesh->mNormals[j].x, mesh->mNormals[j].y,
mesh->mNormals[j].z};
vertex.tangent = {mesh->mTangents[j].x, mesh->mTangents[j].y,
mesh->mTangents[j].z};
vertices.push_back(vertex);
}

for (uint32_t j = 0; j < mesh->mNumFaces; ++j) {
const aiFace &face = mesh->mFaces[j];
for (uint32_t k = 0; k < face.mNumIndices; ++k) {
indices.push_back(face.mIndices[k]);
}
}
}

vertices_ = vertices;
indices_ = indices;
}
} // namespace GameX
3 changes: 3 additions & 0 deletions src/GameX/utils/mesh.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "GameX/utils/vertex.h"
#include "glm/gtc/matrix_transform.hpp"

namespace GameX {
struct Mesh {
Expand All @@ -17,6 +18,8 @@ struct Mesh {
glm::vec3 normal = glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3 tangent = glm::vec3(0.0f, 0.0f, 0.0f));

Mesh(const std::string &path);

std::vector<Vertex> &Vertices();

const std::vector<Vertex> &Vertices() const;
Expand Down
62 changes: 62 additions & 0 deletions src/GameX/utils/vertex.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,67 @@ struct Vertex {

return attribute_descriptions;
}

bool operator<(const Vertex &other) const {
if (position.x < other.position.x) {
return true;
} else if (position.x > other.position.x) {
return false;
} else if (position.y < other.position.y) {
return true;
} else if (position.y > other.position.y) {
return false;
} else if (position.z < other.position.z) {
return true;
} else if (position.z > other.position.z) {
return false;
} else if (normal.x < other.normal.x) {
return true;
} else if (normal.x > other.normal.x) {
return false;
} else if (normal.y < other.normal.y) {
return true;
} else if (normal.y > other.normal.y) {
return false;
} else if (normal.z < other.normal.z) {
return true;
} else if (normal.z > other.normal.z) {
return false;
} else if (texCoord.x < other.texCoord.x) {
return true;
} else if (texCoord.x > other.texCoord.x) {
return false;
} else if (texCoord.y < other.texCoord.y) {
return true;
} else if (texCoord.y > other.texCoord.y) {
return false;
} else if (tangent.x < other.tangent.x) {
return true;
} else if (tangent.x > other.tangent.x) {
return false;
} else if (tangent.y < other.tangent.y) {
return true;
} else if (tangent.y > other.tangent.y) {
return false;
} else if (tangent.z < other.tangent.z) {
return true;
} else if (tangent.z > other.tangent.z) {
return false;
} else if (color.x < other.color.x) {
return true;
} else if (color.x > other.color.x) {
return false;
} else if (color.y < other.color.y) {
return true;
} else if (color.y > other.color.y) {
return false;
} else if (color.z < other.color.z) {
return true;
} else if (color.z > other.color.z) {
return false;
} else {
return false;
}
}
};
} // namespace GameX
3 changes: 2 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"name": "d3dx12",
"platform": "windows"
},
"eigen3"
"eigen3",
"assimp"
]
}

0 comments on commit dcca828

Please sign in to comment.