网格在相机旋转/移动时开始跳跃

3

大家好,

这是我第一次在这里发帖,因为我卡住了......

当一个网格远离原点(0, 0, 0)时,当旋转或移动相机时,它会更多地“跳动”/“闪烁”。很难描述这种效果:就像网格有点抖动/颤抖/震动,而且随着你离原点越来越远,这种颤动会越来越大。
对我来说,在距离原点约100000个单位的地方开始观察到这种现象,例如在(0, 0, 100000)处。无论是平移轴还是网格类型(使用Mesh.Create...创建的默认网格或使用assimp.NET导入的3ds网格),都不会影响这种效果。当出现这种效果时,网格的位置值不会改变,通过记录位置进行了检查。

如果我没有漏掉什么,那么问题就只剩下两种可能性:

  1. 我的相机代码
  2. DirectX设备

至于DirectX设备,这是我的设备初始化代码:

    private void InitializeDevice()
    {
        //Initialize D3D
        _d3dObj = new D3D9.Direct3D();

        //Set presentation parameters
        _presParams = new D3D9.PresentParameters();
        _presParams.Windowed = true;
        _presParams.SwapEffect = D3D9.SwapEffect.Discard;
        _presParams.AutoDepthStencilFormat = D3D9.Format.D16;
        _presParams.EnableAutoDepthStencil = true;
        _presParams.PresentationInterval = D3D9.PresentInterval.One;
        _presParams.BackBufferFormat = _d3dObj.Adapters.DefaultAdapter.CurrentDisplayMode.Format;
        _presParams.BackBufferHeight = _d3dObj.Adapters.DefaultAdapter.CurrentDisplayMode.Height;
        _presParams.BackBufferWidth = _d3dObj.Adapters.DefaultAdapter.CurrentDisplayMode.Width;

        //Set form width and height to current backbuffer width und height
        this.Width = _presParams.BackBufferWidth;
        this.Height = _presParams.BackBufferHeight;

        //Checking device capabilities
        D3D9.Capabilities caps = _d3dObj.GetDeviceCaps(0, D3D9.DeviceType.Hardware);
        D3D9.CreateFlags devFlags = D3D9.CreateFlags.SoftwareVertexProcessing;
        D3D9.DeviceType devType = D3D9.DeviceType.Reference;

        //setting device flags according to device capabilities
        if ((caps.VertexShaderVersion >= new Version(2, 0)) && (caps.PixelShaderVersion >= new Version(2, 0)))
        {
            //if device supports vertexshader and pixelshader >= 2.0
            //then use the hardware device
            devType = D3D9.DeviceType.Hardware;

            if (caps.DeviceCaps.HasFlag(D3D9.DeviceCaps.HWTransformAndLight))
            {
                devFlags = D3D9.CreateFlags.HardwareVertexProcessing;
            }
            if (caps.DeviceCaps.HasFlag(D3D9.DeviceCaps.PureDevice))
            {
                devFlags |= D3D9.CreateFlags.PureDevice;
            }
        }

        //initialize the device
        _device = new D3D9.Device(_d3dObj, 0, devType, this.Handle, devFlags, _presParams);
        //set culling
        _device.SetRenderState(D3D9.RenderState.CullMode, D3D9.Cull.Counterclockwise);
        //set texturewrapping (needed for seamless spheremapping)
        _device.SetRenderState(D3D9.RenderState.Wrap0, D3D9.TextureWrapping.All);
        //set lighting
        _device.SetRenderState(D3D9.RenderState.Lighting, false);
        //enabling the z-buffer
        _device.SetRenderState(D3D9.RenderState.ZEnable, D3D9.ZBufferType.UseZBuffer);
        //and setting write-access exlicitly to true...
        //i'm a little paranoid about this since i had to struggle for a few days with weirdly overlapping meshes
        _device.SetRenderState(D3D9.RenderState.ZWriteEnable, true);
    }

我是否遗漏了某个标志或渲染状态?是否有什么可能导致这样奇怪/扭曲的行为?
我的相机类基于Michael Silvermans C++ Quaternion Camera
//every variable prefixed with an underscore is 
//a private static variable initialized beforehand
public static class Camera
{
    //gets called every frame
    public static void Update()
    {
        if (_filter)
        {
            _filteredPos = Vector3.Lerp(_filteredPos, _pos, _filterAlpha);
            _filteredRot = Quaternion.Slerp(_filteredRot, _rot, _filterAlpha);
        }

        _device.SetTransform(D3D9.TransformState.Projection, Matrix.PerspectiveFovLH(_fov, _screenAspect, _nearClippingPlane, _farClippingPlane));
        _device.SetTransform(D3D9.TransformState.View, GetViewMatrix());
    }

    public static void Move(Vector3 delta)
    {
        _pos += delta;
    }

    public static void RotationYaw(float theta)
    {
        _rot = Quaternion.Multiply(Quaternion.RotationAxis(_up, -theta), _rot);
    }

    public static void RotationPitch(float theta)
    {
        _rot = Quaternion.Multiply(_rot, Quaternion.RotationAxis(_right, theta));
    }

    public static void SetTarget(Vector3 target, Vector3 up)
    {
        SetPositionAndTarget(_pos, target, up);
    }

    public static void SetPositionAndTarget(Vector3 position, Vector3 target, Vector3 upVec)
    {
        _pos = position;

        Vector3 up, right, lookAt = target - _pos;

        lookAt = Vector3.Normalize(lookAt);
        right = Vector3.Cross(upVec, lookAt);
        right = Vector3.Normalize(right);
        up = Vector3.Cross(lookAt, right);
        up = Vector3.Normalize(up);

        SetAxis(lookAt, up, right);
    }

    public static void SetAxis(Vector3 lookAt, Vector3 up, Vector3 right)
    {
        Matrix rot = Matrix.Identity;

        rot.M11 = right.X;
        rot.M12 = up.X;
        rot.M13 = lookAt.X;

        rot.M21 = right.Y;
        rot.M22 = up.Y;
        rot.M23 = lookAt.Y;

        rot.M31 = right.Z;
        rot.M32 = up.Z;
        rot.M33 = lookAt.Z;

        _rot = Quaternion.RotationMatrix(rot);
    }

    public static void ViewScene(BoundingSphere sphere)
    {
        SetPositionAndTarget(sphere.Center - new Vector3((sphere.Radius + 150) / (float)Math.Sin(_fov / 2), 0, 0), sphere.Center, new Vector3(0, 1, 0));
    }

    public static Vector3 GetLookAt()
    {
        Matrix rot = Matrix.RotationQuaternion(_rot);
        return new Vector3(rot.M13, rot.M23, rot.M33);
    }

    public static Vector3 GetRight()
    {
        Matrix rot = Matrix.RotationQuaternion(_rot);
        return new Vector3(rot.M11, rot.M21, rot.M31);
    }

    public static Vector3 GetUp()
    {
        Matrix rot = Matrix.RotationQuaternion(_rot);
        return new Vector3(rot.M12, rot.M22, rot.M32);
    }

    public static Matrix GetViewMatrix()
    {
        Matrix viewMatrix, translation = Matrix.Identity;
        Vector3 position;
        Quaternion rotation;

        if (_filter)
        {
            position = _filteredPos;
            rotation = _filteredRot;
        }
        else
        {
            position = _pos;
            rotation = _rot;
        }

        translation = Matrix.Translation(-position.X, -position.Y, -position.Z);
        viewMatrix = Matrix.Multiply(translation, Matrix.RotationQuaternion(rotation));

        return viewMatrix;
    }
}

你在相机代码中发现了什么可能导致这种行为的问题吗?

我无法想象DirectX不能处理大于100k的距离。我应该渲染太阳系,我使用1个单位=1km。因此,地球将在其到太阳的最大距离处渲染为(0,0,152100000)(仅作为示例)。如果这些“跳跃”继续发生,这将变得不可能。 最后,我考虑缩小所有内容,以使系统永远不会超出原点100k / -100k的距离,但我认为这行不通,因为随着距离原点的距离越来越远,“抖动”会变得更大。缩小所有内容也会缩小跳跃行为。


2
我认为这很可能是你的问题。DirectX使用浮点数,通常具有6-7个小数位的精度,这意味着当它们为152100000时,最小的可表示变化约为10000个单位。 - jcoder
谢谢您的评论,我从未考虑过一切背后的数据类型。然后缩小整个系统应该可以解决问题。 - KeyNone
1
缩小它,或将您的世界分成更小的块,并使用更本地的坐标系统,这显然需要更多的工作,并要求您以某种方式处理区域之间的移动。 - jcoder
1个回答

1

为了不让这个问题无解(感谢@jcoder,请参见问题的评论):

网格的奇怪行为来自于DX的浮点精度。你的世界越大,计算位置的精度就越低。

有两种可能解决这个问题:

  1. 缩小整个世界
    在“银河系风格”的世界中,这可能会有问题,因为您有非常大的位置偏移以及非常小的位置偏移(即行星到其太阳的距离非常大,但绕行星轨道的宇宙飞船的距离可能非常小)
  2. 将世界分成较小的块
    这样,您要么必须相对于其他东西表达所有位置(请参见stackoverflow.com/questions/1930421),要么制作多个世界并以某种方式在它们之间移动

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接