Added mesh create info structure for passing info on scaling and centering upon mesh creation
This commit is contained in:
parent
93724b523f
commit
14f82969be
3 changed files with 53 additions and 37 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Simple wrapper for getting an index buffer and vertices out of an assimp mesh
|
* Mesh loader for creating Vulkan resources from models loaded with ASSIMP
|
||||||
*
|
*
|
||||||
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
||||||
*
|
*
|
||||||
|
|
@ -64,6 +64,14 @@ namespace vkMeshLoader
|
||||||
glm::vec3 dim;
|
glm::vec3 dim;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Used to set parameters upon mesh creation
|
||||||
|
struct MeshCreateInfo
|
||||||
|
{
|
||||||
|
glm::vec3 center;
|
||||||
|
glm::vec3 scale;
|
||||||
|
glm::vec2 uvscale;
|
||||||
|
};
|
||||||
|
|
||||||
// Get vertex size from vertex layout
|
// Get vertex size from vertex layout
|
||||||
static uint32_t vertexSize(std::vector<vkMeshLoader::VertexLayout> layout)
|
static uint32_t vertexSize(std::vector<vkMeshLoader::VertexLayout> layout)
|
||||||
{
|
{
|
||||||
|
|
@ -349,7 +357,7 @@ public:
|
||||||
|
|
||||||
Vertex v(glm::vec3(pPos->x, -pPos->y, pPos->z),
|
Vertex v(glm::vec3(pPos->x, -pPos->y, pPos->z),
|
||||||
glm::vec2(pTexCoord->x , pTexCoord->y),
|
glm::vec2(pTexCoord->x , pTexCoord->y),
|
||||||
glm::vec3(pNormal->x, pNormal->y, pNormal->z),
|
glm::vec3(pNormal->x, -pNormal->y, pNormal->z),
|
||||||
glm::vec3(pTangent->x, pTangent->y, pTangent->z),
|
glm::vec3(pTangent->x, pTangent->y, pTangent->z),
|
||||||
glm::vec3(pBiTangent->x, pBiTangent->y, pBiTangent->z),
|
glm::vec3(pBiTangent->x, pBiTangent->y, pBiTangent->z),
|
||||||
glm::vec3(pColor.r, pColor.g, pColor.b)
|
glm::vec3(pColor.r, pColor.g, pColor.b)
|
||||||
|
|
@ -418,11 +426,27 @@ public:
|
||||||
VkPhysicalDeviceMemoryProperties deviceMemoryProperties,
|
VkPhysicalDeviceMemoryProperties deviceMemoryProperties,
|
||||||
vkMeshLoader::MeshBuffer *meshBuffer,
|
vkMeshLoader::MeshBuffer *meshBuffer,
|
||||||
std::vector<vkMeshLoader::VertexLayout> layout,
|
std::vector<vkMeshLoader::VertexLayout> layout,
|
||||||
float scale,
|
vkMeshLoader::MeshCreateInfo *createInfo,
|
||||||
bool useStaging,
|
bool useStaging,
|
||||||
VkCommandBuffer copyCmd,
|
VkCommandBuffer copyCmd,
|
||||||
VkQueue copyQueue)
|
VkQueue copyQueue)
|
||||||
{
|
{
|
||||||
|
glm::vec3 scale;
|
||||||
|
glm::vec2 uvscale;
|
||||||
|
glm::vec3 center;
|
||||||
|
if (createInfo == nullptr)
|
||||||
|
{
|
||||||
|
scale = glm::vec3(1.0f);
|
||||||
|
uvscale = glm::vec2(1.0f);
|
||||||
|
center = glm::vec3(0.0f);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
scale = createInfo->scale;
|
||||||
|
uvscale = createInfo->uvscale;
|
||||||
|
center = createInfo->center;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<float> vertexBuffer;
|
std::vector<float> vertexBuffer;
|
||||||
for (int m = 0; m < m_Entries.size(); m++)
|
for (int m = 0; m < m_Entries.size(); m++)
|
||||||
{
|
{
|
||||||
|
|
@ -434,9 +458,9 @@ public:
|
||||||
// Position
|
// Position
|
||||||
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_POSITION)
|
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_POSITION)
|
||||||
{
|
{
|
||||||
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_pos.x * scale);
|
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_pos.x * scale.x + center.x);
|
||||||
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_pos.y * scale);
|
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_pos.y * scale.y + center.y);
|
||||||
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_pos.z * scale);
|
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_pos.z * scale.z + center.z);
|
||||||
}
|
}
|
||||||
// Normal
|
// Normal
|
||||||
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_NORMAL)
|
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_NORMAL)
|
||||||
|
|
@ -448,8 +472,8 @@ public:
|
||||||
// Texture coordinates
|
// Texture coordinates
|
||||||
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_UV)
|
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_UV)
|
||||||
{
|
{
|
||||||
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_tex.s);
|
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_tex.s * uvscale.s);
|
||||||
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_tex.t);
|
vertexBuffer.push_back(m_Entries[m].Vertices[i].m_tex.t * uvscale.t);
|
||||||
}
|
}
|
||||||
// Color
|
// Color
|
||||||
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_COLOR)
|
if (layoutDetail == vkMeshLoader::VERTEX_LAYOUT_COLOR)
|
||||||
|
|
@ -634,23 +658,4 @@ public:
|
||||||
vkUnmapMemory(device, meshBuffer->indices.mem);
|
vkUnmapMemory(device, meshBuffer->indices.mem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create vertex and index buffer with given layout
|
|
||||||
void createVulkanBuffers(
|
|
||||||
VkDevice device,
|
|
||||||
VkPhysicalDeviceMemoryProperties deviceMemoryProperties,
|
|
||||||
vkMeshLoader::MeshBuffer *meshBuffer,
|
|
||||||
std::vector<vkMeshLoader::VertexLayout> layout,
|
|
||||||
float scale)
|
|
||||||
{
|
|
||||||
createBuffers(
|
|
||||||
device,
|
|
||||||
deviceMemoryProperties,
|
|
||||||
meshBuffer,
|
|
||||||
layout,
|
|
||||||
scale,
|
|
||||||
false,
|
|
||||||
VK_NULL_HANDLE,
|
|
||||||
VK_NULL_HANDLE);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
@ -362,11 +362,16 @@ VkBool32 VulkanExampleBase::createBuffer(VkBufferUsageFlags usage, VkMemoryPrope
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanExampleBase::loadMesh(
|
void VulkanExampleBase::loadMesh(std::string filename, vkMeshLoader::MeshBuffer * meshBuffer, std::vector<vkMeshLoader::VertexLayout> vertexLayout, float scale)
|
||||||
std::string filename,
|
{
|
||||||
vkMeshLoader::MeshBuffer * meshBuffer,
|
vkMeshLoader::MeshCreateInfo meshCreateInfo;
|
||||||
std::vector<vkMeshLoader::VertexLayout> vertexLayout,
|
meshCreateInfo.scale = glm::vec3(scale);
|
||||||
float scale)
|
meshCreateInfo.center = glm::vec3(0.0f);
|
||||||
|
meshCreateInfo.uvscale = glm::vec2(1.0f);
|
||||||
|
loadMesh(filename, meshBuffer, vertexLayout, &meshCreateInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VulkanExampleBase::loadMesh(std::string filename, vkMeshLoader::MeshBuffer * meshBuffer, std::vector<vkMeshLoader::VertexLayout> vertexLayout, vkMeshLoader::MeshCreateInfo *meshCreateInfo)
|
||||||
{
|
{
|
||||||
VulkanMeshLoader *mesh = new VulkanMeshLoader();
|
VulkanMeshLoader *mesh = new VulkanMeshLoader();
|
||||||
|
|
||||||
|
|
@ -384,7 +389,7 @@ void VulkanExampleBase::loadMesh(
|
||||||
deviceMemoryProperties,
|
deviceMemoryProperties,
|
||||||
meshBuffer,
|
meshBuffer,
|
||||||
vertexLayout,
|
vertexLayout,
|
||||||
scale,
|
meshCreateInfo,
|
||||||
true,
|
true,
|
||||||
copyCmd,
|
copyCmd,
|
||||||
queue);
|
queue);
|
||||||
|
|
@ -1147,7 +1152,7 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
|
||||||
int32_t posy = HIWORD(lParam);
|
int32_t posy = HIWORD(lParam);
|
||||||
rotation.x += (mousePos.y - (float)posy) * 1.25f * rotationSpeed;
|
rotation.x += (mousePos.y - (float)posy) * 1.25f * rotationSpeed;
|
||||||
rotation.y -= (mousePos.x - (float)posx) * 1.25f * rotationSpeed;
|
rotation.y -= (mousePos.x - (float)posx) * 1.25f * rotationSpeed;
|
||||||
camera.rotate(glm::vec3((mousePos.y - (float)posy), -(mousePos.x - (float)posx), 0.0f));
|
camera.rotate(glm::vec3((mousePos.y - (float)posy) * camera.rotationSpeed, -(mousePos.x - (float)posx) * camera.rotationSpeed, 0.0f));
|
||||||
mousePos = glm::vec2((float)posx, (float)posy);
|
mousePos = glm::vec2((float)posx, (float)posy);
|
||||||
viewChanged();
|
viewChanged();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -341,6 +341,12 @@ public:
|
||||||
vkMeshLoader::MeshBuffer *meshBuffer,
|
vkMeshLoader::MeshBuffer *meshBuffer,
|
||||||
std::vector<vkMeshLoader::VertexLayout> vertexLayout,
|
std::vector<vkMeshLoader::VertexLayout> vertexLayout,
|
||||||
float scale);
|
float scale);
|
||||||
|
void loadMesh(
|
||||||
|
std::string filename,
|
||||||
|
vkMeshLoader::MeshBuffer *meshBuffer,
|
||||||
|
std::vector<vkMeshLoader::VertexLayout>
|
||||||
|
vertexLayout,
|
||||||
|
vkMeshLoader::MeshCreateInfo *meshCreateInfo);
|
||||||
|
|
||||||
// Start the main render loop
|
// Start the main render loop
|
||||||
void renderLoop();
|
void renderLoop();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue