修复模型自旋转,允许相机根据模型自适应距离

已知问题:加载其他ktx环境贴图会发生贴图倒置的情况
pull/3/head
ink-soul 2024-04-11 16:28:54 +08:00
parent 9858f7fa56
commit 02d6974134
11 changed files with 34 additions and 21 deletions

View File

@ -2049,7 +2049,7 @@ void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y)
camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f * camera.movementSpeed));
}
if (mouseButtons.middle) {
camera.translate(glm::vec3(-dx * 0.01f, -dy * 0.01f, 0.0f));
camera.translate(glm::vec3(-dx * 0.01f, dy * 0.01f, 0.0f));
}
mousePos = glm::vec2((float)x, (float)y);
}

View File

@ -113,7 +113,7 @@ public:
uint32_t lastFPS = 0;
struct Settings {
bool validation = false; // 校验层开关
bool validation = true; // 校验层开关
bool fullscreen = false; // 全屏开关
bool vsync = false; // 垂直同步开关
bool multiSampling = true; // 多重采样
@ -121,7 +121,7 @@ public:
bool headless = false; // 无头开关
bool outputPNGimage = false;
bool enableSaveToImageSequeue = true; // 图片序列开关(暂时弃用)
uint32_t outputFrameCount = 75; // 图片序列结束帧
uint32_t outputFrameCount = 500; // 图片序列结束帧
bool takeScreenShot = false; // 截屏(暂时弃用)
uint32_t startFrameCount = 1; // 图片序列开始帧

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -196,13 +196,14 @@ PlumageRender::PlumageRender()
{
std::cout << "Loading scene from " << filename << std::endl;
models.scene.destroy(device);
animationIndex = 0;
animationTimer = 0.0f;
auto tStart = std::chrono::high_resolution_clock::now();
models.scene.loadFromFile(filename, vulkanDevice, queue);
auto tFileLoad = std::chrono::duration<double, std::milli>(std::chrono::high_resolution_clock::now() - tStart).count();
std::cout << "Loading took " << tFileLoad << " ms" << std::endl;
camera.setPosition({ 0.0f, 0.0f, -1.0f });
camera.setPosition({ 0.0f, 0.0f, -6.0f });
camera.setRotation({ 0.0f, 0.0f, 0.0f });
}
@ -262,7 +263,7 @@ PlumageRender::PlumageRender()
loadScene(sceneFile.c_str());
models.skybox.loadFromFile(PlumageRender::filePath.skyboxModleFilePath, vulkanDevice, queue);
loadEnvironment(envMapFile.c_str());
}
@ -684,17 +685,17 @@ PlumageRender::PlumageRender()
switch (target) {
case IRRADIANCE:
format = VK_FORMAT_R32G32B32A32_SFLOAT;
dim = 64;
dim = 128;
break;
case PREFILTEREDENV:
format = VK_FORMAT_R16G16B16A16_SFLOAT;
dim = 512;
dim = 4096;
break;
};
const uint32_t numMips = static_cast<uint32_t>(floor(log2(dim))) + 1;
// Create target cubemap
// Create target cubemap static_cast<uint32_t>(floor(log2(dim))) +
{
// Image
VkImageCreateInfo imageCI{};
@ -1084,7 +1085,7 @@ PlumageRender::PlumageRender()
break;
case PREFILTEREDENV:
prefilterPushBlock.mvp = glm::perspective((float)(M_PI / 2.0), 1.0f, 0.1f, 512.0f) * matrices[f];
prefilterPushBlock.roughness = (float)m / (float)(numMips - 1);
prefilterPushBlock.roughness = 0.0;//(float)m / (float)(numMips - 1);
vkCmdPushConstants(cmdBuf, pipelinelayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PrefilterPushBlock), &prefilterPushBlock);
break;
};
@ -1211,7 +1212,7 @@ PlumageRender::PlumageRender()
auto tStart = std::chrono::high_resolution_clock::now();
const VkFormat format = VK_FORMAT_R16G16_SFLOAT;
const int32_t dim = 512;
const int32_t dim = 2048;
// Image
VkImageCreateInfo imageCI{};
@ -1480,23 +1481,35 @@ PlumageRender::PlumageRender()
shaderDataScene.projection = camera.matrices.perspective;
shaderDataScene.view = camera.matrices.view;
float modelSize = std::max(models.scene.aabb[0][0], std::max(models.scene.aabb[1][1], models.scene.aabb[2][2]));
// Center and scale model
float scale = (1.0f / std::max(models.scene.aabb[0][0], std::max(models.scene.aabb[1][1], models.scene.aabb[2][2]))) * 0.5f;
float scale = (1.0f / modelSize ) * 0.5f;
glm::vec3 translate = -glm::vec3(models.scene.aabb[3][0], models.scene.aabb[3][1], models.scene.aabb[3][2]);
translate += -0.5f * glm::vec3(models.scene.aabb[0][0], models.scene.aabb[1][1], models.scene.aabb[2][2]);
camera.setPosition(glm::vec3(0, 0, -modelSize - 2));
shaderDataScene.model = glm::mat4(1.0f);
shaderDataScene.model[0][0] = scale;
shaderDataScene.model[1][1] = scale;
shaderDataScene.model[2][2] = scale;
shaderDataScene.model = glm::translate(shaderDataScene.model, translate);
if (settings.rotateModel)
{
shaderDataScene.model = glm::mat4(1.0f);
shaderDataScene.model = glm::rotate(shaderDataScene.model, glm::radians(modelrot), glm::vec3(0, 1, 0));
}
shaderDataScene.camPos = glm::vec3(
-camera.position.z * sin(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x)),
-camera.position.z * sin(glm::radians(camera.rotation.x)),
camera.position.z * cos(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x))
);
// Skybox
shaderDataSkybox.projection = camera.matrices.perspective;
shaderDataSkybox.view = camera.matrices.view;
@ -1510,6 +1523,7 @@ PlumageRender::PlumageRender()
sin(glm::radians(lightSource.rotation.y)),
cos(glm::radians(lightSource.rotation.x)) * cos(glm::radians(lightSource.rotation.y)),
0.0f);
}
void PlumageRender::windowResized()
@ -1530,8 +1544,7 @@ PlumageRender::PlumageRender()
camera.setPerspective(45.0f, (float)width / (float)height, 0.1f, 256.0f);
camera.rotationSpeed = 0.25f;
camera.movementSpeed = 0.1f;
camera.setPosition({ 0.0f, 0.0f, -1.0f });
camera.setRotation({ 0.0f, 0.0f, 0.0f });
waitFences.resize(renderAhead);
presentCompleteSemaphores.resize(renderAhead);
@ -2009,11 +2022,11 @@ PlumageRender::PlumageRender()
frameIndex += 1;
frameIndex %= renderAhead;
if (!paused) {
if (settings.rotateModel) {
modelrot.y += frameTimer * 35.0f;
if (modelrot.y > 360.0f) {
modelrot.y -= 360.0f;
modelrot += frameTimer * 2.0f;
if (modelrot > 360.0f) {
modelrot -= 360.0f;
}
}
if ((animate) && (models.scene.animations.size() > 0)) {
@ -2027,7 +2040,7 @@ PlumageRender::PlumageRender()
if (settings.rotateModel) {
updateUniformBuffers();
}
}
if (camera.updated) {
updateUniformBuffers();
}

View File

@ -147,7 +147,7 @@ public:
std::string glTFModelFilePath = getAssetPath() + "models/DamagedHelmet/DamagedHelmet.gltf";
std::string modelVertShaderPath = getAssetPath() + "buster_drone/shaders/glsl/mesh.vert.spv";
std::string modelFragShaderPath = getAssetPath() + "buster_drone/shaders/glsl/mesh.frag.spv";
//ui
std::string uiVertShaderPath = getAssetPath() + "shaders/ui.vert.spv";
std::string uiFragShaderPath = getAssetPath() + "shaders/ui.frag.spv";
@ -157,7 +157,7 @@ public:
std::string skyboxVertShaderPath = getAssetPath() + "shaders/skybox.vert.spv";
std::string skyboxFragShaderPath = getAssetPath() + "shaders/skybox.frag.spv";
std::string iblTexturesFilePath = getAssetPath() + "textures/hdr/gcanyon_cube.ktx";
std::string iblTexturesFilePath = getAssetPath() + "textures/hdr/pisa_cube.ktx";
//tonemapping
std::string tonemappingVertShaderPath = getAssetPath() + "buster_drone/shaders/glsl/genbrdflut.vert.spv";
std::string tonemappingEnableFragShaderPath = getAssetPath() + "buster_drone/shaders/glsl/tonemapping_enable.frag.spv";
@ -197,7 +197,7 @@ public:
} filePath;
glm::vec3 modelrot = glm::vec3(0.0f);
float modelrot = 0.0f;
glm::vec3 modelPos = glm::vec3(0.0f);