2013-03-26 57 views
0

请告诉我,我在做什么错误: 这是我的相机类SlimDX摄像机设置

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; 
    } 
} 

这是我的常量缓冲区结构:

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.5707963267 F;)和@Nico Schertler,太感谢您了,我把它使用由

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

,但没有效果还是......也许是我的.fx文件是错误的?出于什么目的,我需要这样的:

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

Attepmpt第三: @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 
       }); 

现在的问题 - 我看到我的三角BUT THE相机不会移动

+0

问题是什么?我找不到实际设置常量缓冲区的位置('context.VertexShader.SetConstantBuffer(...)')。 – 2013-03-26 15:47:36

+0

唯一的问题我看到ATM是那台相机。fov永远不会被初始化(它会是0.0 - >你什么都看不到)。但我不知道这只是示例代码中的复制和粘贴错误。 – paiden 2013-03-26 17:04:45

回答

0

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

http://pastebin.com/JtiUSiHZ

呼叫在每个帧中的 “TakeALook()” 方法(或当移动相机)。

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

而与“CameraRotate()”,你可以把它转过来 - 它需要一个Vector2作为左右旋转和上下旋转。

它很容易。我使用EventHandlers来调用这两个函数,但可以随意编辑。

1

你有你的相机的nearplane设置为0,这使得零的矩阵把所有的价值,所以你充满了“NAN的

使用约0.01附近平面值矩阵你的情况,它会解决问题