Frame rate independent animation updates

This commit is contained in:
saschawillems 2016-03-11 20:09:12 +01:00
parent 710ff2d511
commit e502785b1f
5 changed files with 76 additions and 59 deletions

View file

@ -60,7 +60,7 @@ public:
struct android_app* app;
uint32_t width;
uint32_t height;
float frameTimer = 0;
float frameTimer = 1.0f;
struct Texture {
VkSampler sampler;

View file

@ -196,7 +196,7 @@ public:
void updateUniformBuffers()
{
computeUbo.deltaT = (1.0f / 60.0f) * 4.0f;
computeUbo.deltaT = (1.0f / frameTimer) * 0.15f;
computeUbo.destX = sin(glm::radians(timer*360.0)) * 0.75f;
computeUbo.destY = cos(glm::radians(timer*360.0)) * 0.10f;
uint8_t *pData;
@ -314,8 +314,8 @@ public:
pipelineCreateInfo.renderPass = renderPass;
// Additive blending
blendAttachmentState.colorWriteMask = 0xF;
blendAttachmentState.blendEnable = VK_TRUE;
blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
@ -323,6 +323,7 @@ public:
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_DST_ALPHA;
err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid);
assert(!err);
}
@ -714,6 +715,33 @@ public:
assert(!err);
}
void render()
{
// Render frame
if (prepared)
{
startTiming();
if (animating)
{
if (animStart > 0.0f)
{
animStart -= 0.15f * (1.0f / frameTimer);
}
if ((animate) & (animStart <= 0.0f))
{
timer += 0.5f * (1.0f / frameTimer);
if (timer > 1.0)
{
timer -= 1.0f;
}
}
updateUniformBuffers();
}
draw();
endTiming();
}
}
};
static int32_t handleInput(struct android_app* app, AInputEvent* event)
@ -759,7 +787,6 @@ void android_main(struct android_app* state)
{
VulkanExample *engine = new VulkanExample();
//memset(&engine, 0, sizeof(engine));
state->userData = engine;
state->onAppCmd = handleCommand;
state->onInputEvent = handleInput;
@ -790,26 +817,6 @@ void android_main(struct android_app* state)
}
}
// Render frame
if (engine->prepared)
{
if (engine->animating)
{
if (engine->animStart > 0.0f)
{
engine->animStart -= (1.0f / 60.0f) * 5.0f;
}
if ((engine->animate) & (engine->animStart <= 0.0f))
{
engine->timer += (1.0f / 60.0f) * 0.1f;
if (engine->timer > 1.0)
{
engine->timer -= 1.0f;
}
}
engine->updateUniformBuffers();
}
engine->draw();
}
engine->render();
}
}

View file

@ -611,6 +611,27 @@ public:
err = swapChain.queuePresent(queue, currentBuffer, semaphores.submitSignal);
assert(!err);
}
void render()
{
if (prepared)
{
startTiming();
if (animating)
{
// Update rotation
state.rotation.y += 0.05f * frameTimer;
if (state.rotation.y > 360.0f)
{
state.rotation.y -= 360.0f;
}
updateUniformBuffers();
}
draw();
endTiming();
}
}
};
static int32_t handleInput(struct android_app* app, AInputEvent* event)
@ -656,7 +677,6 @@ void android_main(struct android_app* state)
{
VulkanExample *engine = new VulkanExample();
//memset(&engine, 0, sizeof(engine));
state->userData = engine;
state->onAppCmd = handleCommand;
state->onInputEvent = handleInput;
@ -687,21 +707,6 @@ void android_main(struct android_app* state)
}
}
// Render frame
if (engine->prepared)
{
if (engine->animating)
{
// Update rotation
engine->state.rotation.y += 0.25f;
if (engine->state.rotation.y > 360.0f)
{
engine->state.rotation.y -= 360.0f;
}
engine->updateUniformBuffers();
}
engine->draw();
}
engine->render();
}
}

View file

@ -570,6 +570,26 @@ public:
assert(!err);
}
void render()
{
if (prepared)
{
startTiming();
if (animating)
{
// Update rotation
state.rotation.y += 0.15f * frameTimer;
if (state.rotation.y > 360.0f)
{
state.rotation.y -= 360.0f;
}
updateUniformBuffers();
}
draw();
endTiming();
}
}
};
@ -616,7 +636,6 @@ void android_main(struct android_app* state)
{
VulkanExample *engine = new VulkanExample();
//memset(&engine, 0, sizeof(engine));
state->userData = engine;
state->onAppCmd = handleCommand;
state->onInputEvent = handleInput;
@ -647,21 +666,7 @@ void android_main(struct android_app* state)
}
}
// Render frame
if (engine->prepared)
{
if (engine->animating)
{
// Update rotation
engine->state.rotation.y += 0.5f;
if (engine->state.rotation.y > 360.0f)
{
engine->state.rotation.y -= 360.0f;
engine->render();
}
engine->updateUniformBuffers();
}
engine->draw();
}
}
}

View file

@ -40,7 +40,7 @@ struct VulkanExample
uint32_t width;
uint32_t height;
struct saved_state state;
float frameTimer;
float frameTimer = 0;
// Vulkan
VkInstance instance;