C++在执行操作之前等待一帧

发布于 2024-11-08 00:12:08 字数 442 浏览 0 评论 0原文

在 C++ 中你会如何等待一个帧。

我不想让程序休眠或发生任何事情。

它会像“

在本帧中执行此操作 (1)

继续程序的其余部分

在下一帧中执行此操作 (2)”

,其中操作 1 仅发生在第一帧中,操作 2 仅发生在下一帧中。就这样继续下去。 1, 2, 1 再次,2

我在帧之间有时间,我使用 c++,并且使用 Visual Studio 2008 进行编译。

编辑:

我使用的是 Opengl,我的操作系统是 Windows 7。

Frame - http://en.wikipedia.org/维基/Frame_rate 就像给定时间段内打印到屏幕上的场景的每个图像

How would you wait a frame in c++.

I don't want the program to sleep or anything.

It would go soemthing like

Do this in this frame (1)

Continue with rest of program

Do this in the next frame (2)

where action 1 happens only in the first frame and action 2 happens only in the next frame. It would continue like this. 1, 2, 1 again, 2

I have the time between frames, I use c++ and i'm using Visual Studio 2008 to compile.

Edit:

I'm using Opengl my OS is Windows 7.

Frame - http://en.wikipedia.org/wiki/Frame_rate
like each image of the scene printed to the screen over a given time period

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮云落日 2024-11-15 00:12:08

我在这里做一些假设。

假设您有一个模型想要显示其状态。您可能希望最大限度地利用用于演化模型而不是渲染的 CPU 时间。

因此,您将目标帧速率固定为 25 fps。

再次假设您已经优化了渲染,以便可以在不到 0.04 秒的时间内完成。

所以你可能想要类似的东西(伪代码):

Time lastRendertime = now();
while(forever)
{
    Time current = now();
    if ((current - lastRenderTime > 0.04))
    {
        renderEverything();
        lastRenderTime = current;
    } 
    else
    {
        evolveModelABit();  
    }
}

当然,你可能有一个输入处理程序来打破循环。请注意,此方法假设您不希望模型演化受到实时流逝的影响。如果您这样做(游戏也可能这样做),请将当前时间传递给evolveModelABit();

对于 Windows 上的时间函数,您可以使用:

    LARGE_INTEGER frequency;        // ticks per second
    LARGE_INTEGER t1;           // ticks
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&t1);

请注意,此方法适用于科学类型模拟。模型演化将不依赖于帧速率、渲染等,并且很快就会给出相同的结果。

对于游戏来说,通常会要求最大化 fps。这意味着主循环的形式为:

Time lastRendertime = now();
while(forever)
{
    Time current = now();
    evolveModelABit(current, lastRenderTime);  
    renderEverything();
    lastRenderTime = current;
}

I'm making some assumptions here.

Suppose you have a model for which you wish to show the state. You might wish to maximise the CPU time spent evolving the model rather than rendering.

So you fix the target frame rate, at e.g. 25 fps.

Again, assume you have optimised rendering so that it can be done in much less than 0.04 seconds.

So you might want something like (pseudo-code):

Time lastRendertime = now();
while(forever)
{
    Time current = now();
    if ((current - lastRenderTime > 0.04))
    {
        renderEverything();
        lastRenderTime = current;
    } 
    else
    {
        evolveModelABit();  
    }
}

Of course, you probably have an input handler to break the loop. Note that this approach assumes that you do not want the model evolution affected by elapsed real time. If you do, and may games do, then pass in the current time to the evolveModelABit();.

For time functions on Windows, you can use:

    LARGE_INTEGER frequency;        // ticks per second
    LARGE_INTEGER t1;           // ticks
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&t1);

Note that this approach is suitable for a scientific type simulation. The model evolution will not depend on the frame rate, rendering etc, and gives the same result very time.

For a game, typically there is a push for maximising the fps. This means that the main loop is of the form:

Time lastRendertime = now();
while(forever)
{
    Time current = now();
    evolveModelABit(current, lastRenderTime);  
    renderEverything();
    lastRenderTime = current;
}
白衬杉格子梦 2024-11-15 00:12:08

如果启用垂直同步,SwapBuffers 将阻塞当前线程,直到显示下一帧。因此,如果您创建一个工作线程,并释放锁,或者在调用 SwapBuffers 之前恢复其执行,您的程序将收到 CPU 时间,否则它将在等待期间让给系统的其余部分 - for-交换块。如果工作线程正在操作 GPU 资源,则最好使用高分辨率/性能计数器来确定距离交换还剩多少时间,减去一些余量并在工作线程中使用此计时,以便工作线程将自己置于在交换发生时休眠,这样 GPU 就不必在工作线程和渲染线程之间进行上下文切换。

If V-Sync is enabled, SwapBuffers will block the current thread until the next frame has been shown. So if you create a worker thread, and release a lock, or resume its execution right before the call of SwapBuffers your programm recieves the CPU time it would otherwise yield to the rest of the system during the wait-for-swap block. If the worker thread is manipulating GPU resources, it is a good idea using high resolution/performance counters to determine how much time is left until the swap, minus some margin and use this timing in the worker thread, so that the worker thread puts itself to sleep at about the time the swap happens, so that the GPU will not have to context switch between worker and renderer thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文