在OpenGL中使物体看起来振动的最好方法是什么?我有一个立方体的集合,我想要以不同的强度“振动”,我认为最好的方法是稍微移动它们渲染的位置。我应该使用计时器来实现这一点吗?或者有更好的方法吗?下面是我的简单drawCube函数:
void drawCube(float x, float y, float z, float opacity, float col[], float shake)
{
glTranslatef(-x, -y, -z);
glColor4f(col[0], col[1], col[2], opacity);
glutWireCube(20);
glTranslatef(x, y, z);
}发布于 2013-04-16 02:23:46
考虑到振动基本上对我们的眼睛来说是一种太快的运动:是的,你需要移动立方体。
只要您的应用程序以足够高的帧速率运行,这将是令人信服的。对于低帧率(~15fps或更低),您需要其他技巧。
至于如何做:我建议使用一个由计时器驱动的简单函数来简单地计算当前帧的平移。
这里一个容易使用的函数是sin,它也表示清晰的声波(=空气中的振动)。
给定double/float time,它表示自应用程序启动以来的秒数(以及表示毫秒的分数)
void drawCube(float x, float y, float z, float opacity, float col[], float time)
{
float offset = sin(2.0f * 3.14159265359f * time); // 1 Hz, for more Hz just multiply with higher value than 2.0f
glTranslatef(-x + offset, -y + offset, -z + offset);
glColor4f(col[0], col[1], col[2], opacity);
glutWireCube(20);
glTranslatef(x, y, z);
}编辑:这将使它在原始位置周围的-1,1间隔内振动。对于更大的振动,将sin的结果与比例因子相乘。
发布于 2013-04-16 02:31:27
就个人而言,我会使用rand()或我自己的实现。也许是一个Noise函数。我将使用该噪声函数来获得- 1,1范围内的随机或伪随机偏移量,然后将其乘以shake变量。
// returns a "random" value between -1.0f and 1.0f
float Noise()
{
// make the number
}
void drawCube(float x, float y, float z, float opacity, float col[], float shake)
{
float x, y, z;
ox = Noise();
oy = Noise();
oz = Noise();
glPushMatrix();
glTranslatef( x + (shake * ox), y + (shake * oy), z + (shake * oz) );
glColor4f(col[0], col[1], col[2], opacity);
glutWireCube(20);
glPopMatrix();
}你可能已经注意到我做了一些调整,我在glPushMatrix()和glPopMatrix中添加了一些东西。它们非常有用,并且会自动撤销它们之间所做的任何事情。
glPushMatrix();
glTranslatef(1, 1, 1);
// draw something at the location (1, 1, 1)
glPushMatrix();
glTranslatef(2, 2, 2);
// draw something at the location (3, 3, 3)
glPopMatrix();
// draw at the location (1, 1, 1) again
glPopMatrix()https://stackoverflow.com/questions/16021705
复制相似问题