diff --git a/android/skeletalanimation/build.bat b/android/skeletalanimation/build.bat index 01ebd582..14d1906e 100644 --- a/android/skeletalanimation/build.bat +++ b/android/skeletalanimation/build.bat @@ -1,7 +1,6 @@ cd jni call ndk-build if %ERRORLEVEL% EQU 0 ( - echo ndk-build has failed, build cancelled cd.. mkdir "assets\shaders\base" @@ -18,7 +17,7 @@ if %ERRORLEVEL% EQU 0 ( xcopy "..\..\data\models\plane_z.obj" "assets\models" /Y mkdir "assets\textures" - xcopy "..\..\data\textures\pattern_35_bc3.ktx" "assets\textures" /Y + xcopy "..\..\data\textures\trail_bc3.ktx" "assets\textures" /Y xcopy "..\..\data\textures\goblin_bc3.ktx" "assets\textures" /Y mkdir "res\drawable" diff --git a/data/shaders/skeletalanimation/mesh.vert b/data/shaders/skeletalanimation/mesh.vert index 79abee9b..b73b7b4d 100644 --- a/data/shaders/skeletalanimation/mesh.vert +++ b/data/shaders/skeletalanimation/mesh.vert @@ -40,14 +40,13 @@ void main() boneTransform += ubo.bones[inBoneIDs[2]] * inBoneWeights[2]; boneTransform += ubo.bones[inBoneIDs[3]] * inBoneWeights[3]; - outNormal = inNormal; outColor = inColor; outUV = inUV; gl_Position = ubo.projection * ubo.view * ubo.model * boneTransform * vec4(inPos.xyz, 1.0); vec4 pos = ubo.model * vec4(inPos, 1.0); - outNormal = mat3(inverse(transpose(ubo.model))) * inNormal; + outNormal = mat3(inverse(transpose(ubo.model * boneTransform))) * inNormal; outLightVec = ubo.lightPos.xyz - pos.xyz; outViewVec = ubo.viewPos.xyz - pos.xyz; } \ No newline at end of file diff --git a/data/shaders/skeletalanimation/mesh.vert.spv b/data/shaders/skeletalanimation/mesh.vert.spv index 76d380cf..1702a0cb 100644 Binary files a/data/shaders/skeletalanimation/mesh.vert.spv and b/data/shaders/skeletalanimation/mesh.vert.spv differ diff --git a/data/shaders/skeletalanimation/texture.frag b/data/shaders/skeletalanimation/texture.frag index 3a917346..dce65c36 100644 --- a/data/shaders/skeletalanimation/texture.frag +++ b/data/shaders/skeletalanimation/texture.frag @@ -19,14 +19,13 @@ void main() float distSqr = dot(inLightVec, inLightVec); vec3 lVec = inLightVec * inversesqrt(distSqr); - float invRadius = 1.0/4500.0; - - float atten = max(clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0), 0.0); + const float attInvRadius = 1.0/5000.0; + float atten = max(clamp(1.0 - attInvRadius * sqrt(distSqr), 0.0, 1.0), 0.0); // Fake drop shadow - invRadius = 1.0/2500.0; - float dropshadow = max(clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0), 0.0); + const float shadowInvRadius = 1.0/2500.0; + float dropshadow = max(clamp(1.0 - shadowInvRadius * sqrt(distSqr), 0.0, 1.0), 0.0); - outFragColor = vec4(color.g * (1.0 - dropshadow)); + outFragColor = vec4(color.rgba * (1.0 - dropshadow)); outFragColor.rgb *= atten; } \ No newline at end of file diff --git a/data/shaders/skeletalanimation/texture.frag.spv b/data/shaders/skeletalanimation/texture.frag.spv index 66468d36..0ef8ff67 100644 Binary files a/data/shaders/skeletalanimation/texture.frag.spv and b/data/shaders/skeletalanimation/texture.frag.spv differ diff --git a/data/shaders/skeletalanimation/texture.vert b/data/shaders/skeletalanimation/texture.vert index fcddff4d..fc197b1a 100644 --- a/data/shaders/skeletalanimation/texture.vert +++ b/data/shaders/skeletalanimation/texture.vert @@ -29,7 +29,7 @@ out gl_PerVertex void main() { - outUV = inUV * 2.0 + ubo.uvOffset; + outUV = inUV + ubo.uvOffset; vec4 pos = vec4(inPos, 1.0); gl_Position = ubo.projection * ubo.view * ubo.model * vec4(pos); diff --git a/data/shaders/skeletalanimation/texture.vert.spv b/data/shaders/skeletalanimation/texture.vert.spv index ef7fae6f..276a824d 100644 Binary files a/data/shaders/skeletalanimation/texture.vert.spv and b/data/shaders/skeletalanimation/texture.vert.spv differ diff --git a/data/textures/trail_bc3.ktx b/data/textures/trail_bc3.ktx new file mode 100644 index 00000000..579927c1 Binary files /dev/null and b/data/textures/trail_bc3.ktx differ diff --git a/skeletalanimation/skeletalanimation.cpp b/skeletalanimation/skeletalanimation.cpp index e2858866..2f3dfde9 100644 --- a/skeletalanimation/skeletalanimation.cpp +++ b/skeletalanimation/skeletalanimation.cpp @@ -20,6 +20,7 @@ #include #include "vulkanexamplebase.h" +#include "vulkanbuffer.hpp" #define VERTEX_BUFFER_BIND_ID 0 #define ENABLE_VALIDATION false @@ -351,9 +352,9 @@ public: SkinnedMesh *skinnedMesh = nullptr; struct { - vkTools::UniformData vsScene; - vkTools::UniformData floor; - } uniformData; + vk::Buffer mesh; + vk::Buffer floor; + } uniformBuffers; struct { glm::mat4 projection; @@ -402,6 +403,7 @@ public: enableTextOverlay = true; title = "Vulkan Example - Skeletal animation"; cameraPos = { 0.0f, 0.0f, 12.0f }; + paused = true; } ~VulkanExample() @@ -417,8 +419,8 @@ public: textureLoader->destroyTexture(textures.colorMap); textureLoader->destroyTexture(textures.floor); - vkTools::destroyUniformData(device, &uniformData.vsScene); - vkTools::destroyUniformData(device, &uniformData.floor); + uniformBuffers.mesh.destroy(); + uniformBuffers.floor.destroy(); // Destroy and free mesh resources vkMeshLoader::freeMeshBufferResources(device, &meshes.floor); @@ -536,19 +538,19 @@ public: vertexBuffer.push_back(vertex); } } - uint32_t vertexBufferSize = vertexBuffer.size() * sizeof(Vertex); + VkDeviceSize vertexBufferSize = vertexBuffer.size() * sizeof(Vertex); // Generate index buffer from loaded mesh file std::vector indexBuffer; for (uint32_t m = 0; m < skinnedMesh->meshLoader->m_Entries.size(); m++) { - uint32_t indexBase = indexBuffer.size(); + uint32_t indexBase = static_cast(indexBuffer.size()); for (uint32_t i = 0; i < skinnedMesh->meshLoader->m_Entries[m].Indices.size(); i++) { indexBuffer.push_back(skinnedMesh->meshLoader->m_Entries[m].Indices[i] + indexBase); } } - uint32_t indexBufferSize = indexBuffer.size() * sizeof(uint32_t); + VkDeviceSize indexBufferSize = indexBuffer.size() * sizeof(uint32_t); skinnedMesh->meshBuffer.indexCount = indexBuffer.size(); bool useStaging = true; @@ -645,21 +647,10 @@ public: } } - void loadTextures() - { - textureLoader->loadTexture( - getAssetPath() + "textures/goblin_bc3.ktx", - VK_FORMAT_BC3_UNORM_BLOCK, - &textures.colorMap); - - textureLoader->loadTexture( - getAssetPath() + "textures/pattern_35_bc3.ktx", - VK_FORMAT_BC3_UNORM_BLOCK, - &textures.floor); - } - - void loadMeshes() + void loadAssets() { + textureLoader->loadTexture(getAssetPath() + "textures/goblin_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.colorMap); + textureLoader->loadTexture(getAssetPath() + "textures/trail_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.floor); VulkanExampleBase::loadMesh(getAssetPath() + "models/plane_z.obj", &meshes.floor, vertexLayout, 512.0f); } @@ -798,7 +789,7 @@ public: descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, - &uniformData.vsScene.descriptor), + &uniformBuffers.mesh.descriptor), // Binding 1 : Color map vkTools::initializers::writeDescriptorSet( descriptorSet, @@ -823,7 +814,7 @@ public: descriptorSets.floor, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, - &uniformData.floor.descriptor)); + &uniformBuffers.floor.descriptor)); // Binding 1 : Color map writeDescriptorSets.push_back( vkTools::initializers::writeDescriptorSet( @@ -917,31 +908,23 @@ public: // Prepare and initialize uniform buffer containing shader uniforms void prepareUniformBuffers() { - // Vertex shader uniform buffer block - createBuffer( + // Mesh uniform buffer block + vulkanDevice->createBuffer( VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - sizeof(uboVS), - nullptr, - &uniformData.vsScene.buffer, - &uniformData.vsScene.memory, - &uniformData.vsScene.descriptor); + &uniformBuffers.mesh, + sizeof(uboVS)); + // Map persistant + VK_CHECK_RESULT(uniformBuffers.mesh.map()); - // Map for host access - VK_CHECK_RESULT(vkMapMemory(device, uniformData.vsScene.memory, 0, sizeof(uboVS), 0, (void **)&uniformData.vsScene.mapped)); - - // Floor - createBuffer( + // Floor uniform buffer block + vulkanDevice->createBuffer( VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - sizeof(uboFloor), - nullptr, - &uniformData.floor.buffer, - &uniformData.floor.memory, - &uniformData.floor.descriptor); - - // Map for host access - VK_CHECK_RESULT(vkMapMemory(device, uniformData.floor.memory, 0, sizeof(uboFloor), 0, (void **)&uniformData.floor.mapped)); + &uniformBuffers.floor, + sizeof(uboFloor)); + // Map persistant + VK_CHECK_RESULT(uniformBuffers.floor.map()); updateUniformBuffers(true); } @@ -950,7 +933,7 @@ public: { if (viewChanged) { - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 512.0f); + uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 1024.0f); glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom)); viewMatrix = glm::rotate(viewMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); @@ -980,11 +963,11 @@ public: uboVS.bones[i] = glm::transpose(glm::make_mat4(&skinnedMesh->boneTransforms[i].a1)); } - memcpy(uniformData.vsScene.mapped, &uboVS, sizeof(uboVS)); + uniformBuffers.mesh.copyTo(&uboVS, sizeof(uboVS)); // Update floor animation - uboFloor.uvOffset.t -= 0.5f * skinnedMesh->animationSpeed * frameTimer; - memcpy(uniformData.floor.mapped, &uboFloor, sizeof(uboFloor)); + uboFloor.uvOffset.t -= 0.25f * skinnedMesh->animationSpeed * frameTimer; + uniformBuffers.floor.copyTo(&uboFloor, sizeof(uboFloor)); } void draw() @@ -1001,9 +984,8 @@ public: void prepare() { VulkanExampleBase::prepare(); - loadTextures(); + loadAssets(); loadMesh(); - loadMeshes(); setupVertexDescriptions(); prepareUniformBuffers(); setupDescriptorSetLayout(); @@ -1068,63 +1050,4 @@ public: } }; -VulkanExample *vulkanExample; - -#if defined(_WIN32) -LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - if (vulkanExample != NULL) - { - vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam); - } - return (DefWindowProc(hWnd, uMsg, wParam, lParam)); -} -#elif defined(__linux__) && !defined(__ANDROID__) -static void handleEvent(const xcb_generic_event_t *event) -{ - if (vulkanExample != NULL) - { - vulkanExample->handleEvent(event); - } -} -#endif - -// Main entry point -#if defined(_WIN32) -// Windows entry point -int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) -#elif defined(__ANDROID__) -// Android entry point -void android_main(android_app* state) -#elif defined(__linux__) -// Linux entry point -int main(const int argc, const char *argv[]) -#endif -{ -#if defined(__ANDROID__) - // Removing this may cause the compiler to omit the main entry point - // which would make the application crash at start - app_dummy(); -#endif - vulkanExample = new VulkanExample(); -#if defined(_WIN32) - vulkanExample->setupWindow(hInstance, WndProc); -#elif defined(__ANDROID__) - // Attach vulkan example to global android application state - state->userData = vulkanExample; - state->onAppCmd = VulkanExample::handleAppCommand; - state->onInputEvent = VulkanExample::handleAppInput; - vulkanExample->androidApp = state; -#elif defined(__linux__) - vulkanExample->setupWindow(); -#endif -#if !defined(__ANDROID__) - vulkanExample->initSwapchain(); - vulkanExample->prepare(); -#endif - vulkanExample->renderLoop(); - delete(vulkanExample); -#if !defined(__ANDROID__) - return 0; -#endif -} +VULKAN_EXAMPLE_MAIN() \ No newline at end of file diff --git a/skeletalanimation/skeletalanimation.vcxproj b/skeletalanimation/skeletalanimation.vcxproj index 8b9f7a7a..34a5e5a9 100644 --- a/skeletalanimation/skeletalanimation.vcxproj +++ b/skeletalanimation/skeletalanimation.vcxproj @@ -21,6 +21,12 @@ + + + + + + {469A1996-9BC5-4003-9564-C3816E3822E0} skelatalanimation @@ -54,6 +60,7 @@ Disabled true /FS %(AdditionalOptions) + Level3 ..\libs\vulkan\vulkan-1.lib;..\libs\assimp\assimp.lib;%(AdditionalDependencies) diff --git a/skeletalanimation/skeletalanimation.vcxproj.filters b/skeletalanimation/skeletalanimation.vcxproj.filters index 13de17c1..3dbcb78f 100644 --- a/skeletalanimation/skeletalanimation.vcxproj.filters +++ b/skeletalanimation/skeletalanimation.vcxproj.filters @@ -13,6 +13,9 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + {a7e2f71f-eff6-4f0b-9c6f-60b729319523} + @@ -39,4 +42,18 @@ Header Files + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + \ No newline at end of file