Commit 09f52dfd authored by Philip Trettner's avatar Philip Trettner

Made update routine in mainLoop more robust

parent edffb3ae
......@@ -83,16 +83,16 @@ bool GlfwApp::shouldClose() const
void GlfwApp::init()
{
if (!mUseDefaultCamera && mUseDefaultRendering)
glow::error() << "Cannot use default rendering without default camera in GlfwApp.";
if (!mUseDefaultCamera && mUseDefaultRendering)
glow::error() << "Cannot use default rendering without default camera in GlfwApp.";
if (mUseDefaultCamera || mUseDefaultRendering)
{
// create camera with some sensible defaults
mCamera = std::make_shared<camera::GenericCamera>();
mCamera->setPosition({ 2, 2, 2 });
mCamera->setTarget({ 0, 0, 0 });
}
if (mUseDefaultCamera || mUseDefaultRendering)
{
// create camera with some sensible defaults
mCamera = std::make_shared<camera::GenericCamera>();
mCamera->setPosition({2, 2, 2});
mCamera->setTarget({0, 0, 0});
}
if (mUseDefaultRendering)
{
......@@ -143,8 +143,8 @@ void GlfwApp::render(float elapsedSeconds)
void GlfwApp::renderPass(const pipeline::RenderPass &pass, float elapsedSeconds)
{
if (mUseDefaultRendering)
debug()->renderPass(pass);
if (mUseDefaultRendering)
debug()->renderPass(pass);
}
void GlfwApp::onResize(int w, int h)
......@@ -324,33 +324,59 @@ void GlfwApp::mainLoop()
double lastTime = glfwGetTime();
double lastStatsTime = lastTime;
double timeAccum = 0.000001;
double renderTimestep = 1.0 / mUpdateRate;
mCurrentTime = 0.0;
size_t primitives = 0;
size_t fragments = 0;
double gpuTime = 0;
double cpuTime = 0;
int updatesPerFrame = 1;
double renderTimestep = 1.0 / mUpdateRate; // tracks time between renders, init is for first frame only
while (!shouldClose())
{
updateInput();
// Update
auto maxTicks = mMaxFrameSkip;
while (timeAccum > 0)
{
if (maxTicks-- < 0)
double dt = 1.0 / mUpdateRate;
// # of updates
auto updates = updatesPerFrame;
if (timeAccum > updatesPerFrame * dt) // lags one behind: do one more
++updates;
if (timeAccum < -dt) // is more than one ahead: skip one
--updates;
// do updates
for (auto i = 0; i < updates; ++i)
{
auto cpuStart = glfwGetTime();
update(dt);
cpuTime += glfwGetTime() - cpuStart;
timeAccum -= dt;
mCurrentTime += dt;
}
// update adjustment (AFTER updates! timeAccum should be within -dt..dt now)
if (timeAccum > 2.5 * dt)
{
++updatesPerFrame;
// glow::info() << "increasing frames per sec";
}
else if (timeAccum < -2.5 * dt)
{
if (updatesPerFrame > 0)
--updatesPerFrame;
// glow::info() << "decreasing frames per sec";
}
// frameskip
if (timeAccum > mMaxFrameSkip * dt)
{
glow::warning() << "Too many updates queued, frame skip of " << timeAccum << " secs";
timeAccum = 0.0;
break;
timeAccum = mMaxFrameSkip * dt * 0.5;
}
auto dt = 1.0 / mUpdateRate;
auto cpuStart = glfwGetTime();
update(dt);
cpuTime += glfwGetTime() - cpuStart;
timeAccum -= dt;
mCurrentTime += dt;
// glow::info() << updates << ", " << timeAccum / dt;
}
beginRender();
......@@ -649,5 +675,5 @@ int GlfwApp::run(int argc, char *argv[])
GlfwApp::~GlfwApp()
{
// empty
// empty
}
......@@ -79,7 +79,7 @@ class GlfwApp
private:
std::string mTitle = "GLFW/GLOW Application"; ///< window title
int mUpdateRate = 60; ///< rate at which update(...) is called
double mUpdateRate = 60; ///< rate at which update(...) is called
int mMaxFrameSkip = 4; ///< maximum number of update(...) steps that are performed without rendering
GLFWwindow* mWindow = nullptr; ///< current GLFW window
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment