GLUT 无法获得毫秒精度的时间

Posted

技术标签:

【中文标题】GLUT 无法获得毫秒精度的时间【英文标题】:GLUT does not get time with millis accuracy 【发布时间】:2018-09-21 19:12:12 【问题描述】:

我在 GLUT 中遇到了计时器问题。

glutGet(GLUT_ELAPSED_TIME) 只能获得秒精度的时间(1000、2000、3000...) 和 glutTimerFunc(...)仅在millis参数设置大于1000时有效。

我不知道 GLUT 是如何衡量时间的 但我认为我的系统时间设置有问题。

如何在 OpenGL 中获得毫秒精度的时间?

【问题讨论】:

GPU:lighthouse3d.com/tutorials/opengl-timer-query CPU:en.cppreference.com/w/cpp/chrono std::chrono::high_resolution_clock::now()? 【参考方案1】:

正如上面 cmets 中已经提到的,您可以使用更可靠的 C++ 日期和时间实用程序,例如 std::chrono 库。这是一个简单的例子:

#include <iostream>
#include <chrono>

int main()

    const auto start = std::chrono::high_resolution_clock::now();

    // do something...

    const auto end = std::chrono::high_resolution_clock::now();

    std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms\n";

    return 0;

【讨论】:

感谢您的回答。实际上,我通过在 ctime 中使用 clock() 而不是 glutGet 来避免这个问题,但是当我尝试使用 glutTimerFunc 时,我不得不依赖 glut 的时间。

以上是关于GLUT 无法获得毫秒精度的时间的主要内容,如果未能解决你的问题,请参考以下文章

Mac 上的 OpenGL/GLUT 应用程序无法获得焦点 [关闭]

C中函数的毫秒精度计时 - 跨平台

如何在 GLUT 中获得显示功能?

将时间与亚秒级精度进行比较

将时间与亚秒级精度进行比较

如何在GLUT中获得显示功能?