Search code examples
directxslimdxperspectivecamera

SlimDX Camera setup


Please, tell me what I'm doing wrongly: that's my Camera class

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

that's my Constant buffer struct:

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

and here I'm drawing the triangle and setting camera:

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

Please, if you can see what's wrong, tell me! :) I have spent two days writing this already..


Attempt the second:

@paiden I initialized fov now ( thanks very much :) ) but still no effect (now it's fov = 1.5707963267f;) and @Nico Schertler , thank you too, I put it in use by

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

but no effect still... probably my .fx file is wrong? for what purpose do I need this:

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

Attepmpt the third: @MHGameWork Thank you very much too, but no effect still ;) If anyone has 5 minutes time, I can just drop source code to his/her e-mail and then we will publish the answer... I guess it will help much to some newbies like me :)

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

THE PROBLEM NOW - I SEE MY TRIANGLE BUT THE CAMERA DOESN'T MOVE


Solution

  • I hope you still need help. Here is my camera class, which can be used, and can be easily moved around the scene using mouse/keyboard.

    http://pastebin.com/JtiUSiHZ

    Call the "TakeALook()" method in each frame (or when you move the camera).

    You can move around it with the "CameraMove" method. It takes a Vector3 - where you want to move your camera (dont give it huge values, I use 0,001f for each frame)

    And with the "CameraRotate()" you can turn it around - it take a Vector2 as a Left-Right and Up-Down rotation.

    Its pretty easy. I use EventHandlers to call there two function, but feel free to edit as you wish.