OpenGl让对象“跳” [英] OpenGl make objects “jump”

查看:52
本文介绍了OpenGl让对象“跳”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作我正在绘制的立方体似乎每隔几秒就跳一次。这是我的代码:



I am trying to make cubes that I am drawing appear to "jump" every few seconds. Here is my code:

for (int i=0; i<25; i++)
{
    if(j<rows)
    {

        //Timer used in vibration calculation when drawing cubes
        float time = (std::clock() - timer);
        //Calculate the amount of simulated vibration based on amount of distortion (intensity)
        float offset = sin(1.0f * 3.14159265359f * time) * shake;

        //Draw cubes right and left of centre point
        drawCube((x+xShift), y, (-300 +depth), 1.0f, cubeColour, offset);
        drawCube((x-xShift), y, (-300 +depth), 1.0f, cubeColour, offset);
        xShift -= gap;
    }
}





而drawCube代码是:





And the drawCube code is:

void drawCube(float x, float y, float z, float opacity, float col[], float offset)
{
    //Draw cube taking into account any offset caused by simulated vibration
    glTranslatef((-x+offset), (-y+offset), -z);
    glColor4f(col[0], col[1], col[2], opacity);
    glutWireCube(20);
    glTranslatef((x-offset), (y-offset), z);

}





我假设我需要使用一个可以提高y值的计时器N秒,所以立方体似乎跳了但不确定如何做到这一点?

基本上每N秒我想要一排立方体上升和下降的快速序列,所以它看起来像是反弹。所以我想我希望它们在y轴上升到10.0f左右然后回落到原来的位置。我希望每N秒触发一次此动作。

谢谢



I''m assuming that I need to use a timer that raises the y value every N seconds so the cube appears to jump but am unsure how to do this?
Basically every N seconds I want the row of cubes to rise and fall in a quick sequence, so it looks like they bounce. So i guess I want them to rise 10.0f or so on the y-axis then fall back down to their original position. I want this action to be triggered every N seconds.
Thanks

推荐答案

您正在尝试创建动画。你对跳跃的解释还不够详细,所以我会很一般。



要使OpenGL代码正常工作你可以放一个或多个sleep()在你的主循环中创建一个延迟。

当你对你的vizualization感到满意然后创建一个等待计时器。



查看这篇文章:计时器教程 [ ^ ]



您可以在主线程中等待此计时器会阻止线程做任何其他事情,或者你可以创建另一个(工作者)线程,它阻塞了计时器事件。



你想让立方体跳起来然后回到原来的位置需要在上下之间有一个延迟(如果不是你不会看到比闪烁更多)。如果这与ups之间的延迟(周期性振动)相同,则很容易 - 只需使用状态变量来跟踪位置。



如果不是,你会需要第二个计时机制。
You are trying to create an animation. You have not been sufficiently detailed in your explanation of "jump" so I will be general.

To get the OpenGL code working you can put one or more sleep()s in your main loop to create a delay.
When you are happy with your vizualization then create a waitable timer.

See this article: Timers Tutorial[^]

You can wait on this timer in your main thread which will prevent the thread from doing anything else or you can create another (worker) thread which is blocking on the timer event.

As you want the cubes to jump up and then return to original position there needs to be a delay beteween up and down (if not you won''t see much more than flicker). If this is the same delay as that between the ups (periodic vibration) then thats easy - just use a state variable to keep track of position.

If not you will need a second timing mechanism.


这篇关于OpenGl让对象“跳”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆