就个人而言,我会使用rand()或我自己的暗示。也许是噪声函数。我将使用该噪声函数来获得 [-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()