SlimDX相机设置

时间:2013-03-26 15:10:24

标签: directx slimdx perspectivecamera

请告诉我,我做错了什么: 这是我的Camera类

public class Camera
{
    public Matrix view;
    public Matrix world;
    public Matrix projection;

    public Vector3 position;
    public Vector3 target;

    public float fov;

    public Camera(Vector3 pos, Vector3 tar)
    {
        this.position = pos;
        this.target = tar;
        view = Matrix.LookAtLH(position, target, Vector3.UnitY);
        projection = Matrix.PerspectiveFovLH(fov, 1.6f, 0.001f, 100.0f);
        world = Matrix.Identity;
    }
}

这是我的Constant buffer struct:

struct ConstantBuffer 
{
    internal Matrix mWorld;
    internal Matrix mView;
    internal Matrix mProjection;
};

在这里我画三角形并设置相机:

x+= 0.01f;
camera.position = new Vector3(x, 0.0f, 0.0f);
camera.view = Matrix.LookAtLH(camera.position, camera.target, Vector3.UnitY);
camera.projection = Matrix.PerspectiveFovLH(camera.fov, 1.6f, 0.0f, 100.0f);

var buffer = new Buffer(device, new BufferDescription
{
  Usage = ResourceUsage.Default,
  SizeInBytes = sizeof(ConstantBuffer),
  BindFlags = BindFlags.ConstantBuffer
});


////////////////////////////// camera setup 

 ConstantBuffer cb;
 cb.mProjection =  Matrix.Transpose(camera.projection);
 cb.mView = Matrix.Transpose(camera.view);
 cb.mWorld =  Matrix.Transpose(camera.world);

 var data = new DataStream(sizeof(ConstantBuffer), true, true);
 data.Write(cb);
 data.Position = 0;

 context.UpdateSubresource(new DataBox(0, 0, data), buffer, 0);


 //////////////////////////////////////////////////////////////////////

 // set the shaders
 context.VertexShader.Set(vertexShader);
 context.PixelShader.Set(pixelShader);
 // draw the triangle
 context.Draw(4, 0);
 swapChain.Present(0, PresentFlags.None);

如果你能看出什么问题,请告诉我! :)我已经花了两天时间写这个..


尝试第二次:

@paiden我现在初始化fov(非常感谢:))但仍然没有效果(现在它是fov = 1.5707963267f;)和@Nico Schertler,谢谢你,我把它用于

context.VertexShader.SetConstantBuffer(buffer, 0); 
context.PixelShader.SetConstantBuffer(buffer, 0); 

但仍然没有效果......可能我的.fx文件错了?出于什么目的我需要这个:

cbuffer ConstantBuffer : register( b0 ) { matrix World; matrix View; matrix Projection; }

尝试第三个: @MHGameWork 非常感谢你,但仍然没有效果;) 如果有人有5分钟的时间,我可以将源代码放到他/她的电子邮件中,然后我们会发布答案......我想这会对像我这样的新手有所帮助:)。

unsafe
{
                x+= 0.01f;
                camera.position = new Vector3(x, 0.0f, 0.0f);
                camera.view = Matrix.LookAtLH(camera.position, camera.target, Vector3.UnitY);
                camera.projection = Matrix.PerspectiveFovLH(camera.fov, 1.6f, 0.01f, 100.0f);

                var buffer = new Buffer(device, new BufferDescription
                {
                    Usage = ResourceUsage.Default,
                    SizeInBytes = sizeof(ConstantBuffer),
                    BindFlags = BindFlags.ConstantBuffer
                });

现在的问题 - 我看到我的三角形但摄像机没有移动

2 个答案:

答案 0 :(得分:1)

您已将相机的近平面设置为0.这会使矩阵中的所有值除以零,因此您会得到一个填充'NAN'的矩阵

在你的情况下使用大约0.01的近平面值,它将解决问题

答案 1 :(得分:0)

我希望你还需要帮助。这是我的相机类,可以使用,可以使用鼠标/键盘在场景中轻松移动。

http://pastebin.com/JtiUSiHZ

在每个帧中(或移动相机时)调用“TakeALook()”方法。

您可以使用“CameraMove”方法移动它。它需要一个Vector3 - 你想要移动你的相机(不要给它巨大的值,我每帧使用0,001f)

使用“CameraRotate()”你可以将它转过来 - 它将Vector2作为左右和上下旋转。

非常简单。我使用EventHandlers来调用两个函数,但可以随意编辑。

相关问题