Jump to content
  • Advertisement
Sign in to follow this  
gomidas

C# SharpDX how to set first person view ?

This topic is 558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I rendered a triangle and XYZ direction arrows using SharpDx. Now I want to rotate my view. However I do not have any idea to look from different perspective.

private void Draw()
        {
            d3dDeviceContext.OutputMerger.SetRenderTargets(renderTargetView);
            // Clear the screen
            d3dDeviceContext.ClearRenderTargetView(renderTargetView, new SharpDX.Color(32, 103, 178));


            d3dDeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.LineList;
            d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(VertexBufferXYZ, Utilities.SizeOf<Vector4>(), 0));

//---I ADDED THIS ADDITIONAL CODE BUT I THINK I ABSOLUTELY DID SOMETHING WRONG




            // Initialize the world matrix
            var worldMatrix = Matrix.Identity;
            // Set camera position slightly to the right (x), above (y)
            // and behind (-z)
            var cameraPosition = new Vector3(1, 1, -2);
            var cameraTarget = Vector3.Zero; // Looking at origin 0,0,0
            var cameraUp = Vector3.UnitZ; // Z+ is Up
                                          // Create view matrix from our camera pos, target and up
            var viewMatrix = Matrix.LookAtLH(cameraPosition, cameraTarget,
            cameraUp);
            // Create the projection matrix
            // Field of View 60degrees = Pi/3 radians
            // Aspect ratio (based on window size), Near clip, Far clip
            var projectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI /
            3f, Width / (float)Height, 0.5f, 100f);
            // Maintain the correct aspect ratio on resize
            renderForm.Resize += (s, e) => {
                projectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 3f,
                Width / (float)Height, 0.5f, 100f);
            };
            // Clear depth stencil view
            /*d3dDeviceContext.ClearDepthStencilView(DepthStencilView,
            DepthStencilClearFlags.Depth | DepthStencilClearFlags.
            Stencil, 1.0f, 0);
            // Clear render target view*/
            // Create viewProjection matrix
            var viewProjection = Matrix.Multiply(viewMatrix,
            projectionMatrix);
            // Create WorldViewProjection Matrix
            var worldViewProjection = worldMatrix * viewProjection;
            // HLSL defaults to "column-major" order matrices so
            // transpose first (SharpDX uses row-major matrices).
            worldViewProjection.Transpose();
            // Write the worldViewProjection to the constant buffer
            d3dDeviceContext.UpdateSubresource(ref worldViewProjection,
            VertexBuffer);
//---//
         
d3dDeviceContext.Draw(vertices2.Count(), 0);
VertexBufferXYZ = D3D11.Buffer.Create<Vector4>(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices2);
d3dDeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
d3dDeviceContext.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(VertexBuffer, Utilities.SizeOf<Vector3>(), 0));
d3dDeviceContext.Draw(vertices.Count(), 0);
VertexBuffer = D3D11.Buffer.Create<Vector3>(d3dDevice, D3D11.BindFlags.VertexBuffer, vertices);
// Swap front and back buffer
swapChain.Present(1, PresentFlags.None);
}

Now it looks like:

DZPump.jpg

What I got after tried to rotate view but I am not really sure: (I think I am missing something)

wR8S36.jpg

This is what I want look to world.

EjD9mH.jpg

What should I do to obtain this view ?

Edited by gomidas

Share this post


Link to post
Share on other sites
Advertisement

First of all, in my opinion using var instead of strong type is really a bad practice.

Yea, if you have a class named SomeVeryVeryVeryLongNameOfARandomClass then using:

var i = new SomeVeryVeryVeryLongNameOfARandomClass();

is a ok, as you save a lot of typing.

But using

var i = Vector3.UnitZ;

is just really bad. I have trouble understanding which variable is what.

 

Since I mentioned Vector3.UnitZ, and you say that it's your UP direction, then this part:

// Set camera position slightly to the right (x), above (y)
// and behind (-z)
var cameraPosition = new Vector3(1, 1, -2);

translates to: 1 to the right (1x) , one forward (1y) , and 2 below (-2z).

Share this post


Link to post
Share on other sites

First of all, in my opinion using var instead of strong type is really a bad practice.

Yea, if you have a class named SomeVeryVeryVeryLongNameOfARandomClass then using:

var i = new SomeVeryVeryVeryLongNameOfARandomClass();

is a ok, as you save a lot of typing.

But using

var i = Vector3.UnitZ;

is just really bad. I have trouble understanding which variable is what.

 

Since I mentioned Vector3.UnitZ, and you say that it's your UP direction, then this part:

// Set camera position slightly to the right (x), above (y)
// and behind (-z)
var cameraPosition = new Vector3(1, 1, -2);

translates to: 1 to the right (1x) , one forward (1y) , and 2 below (-2z).

Alright I will keep in mind that. Then, did I do the perspective correctly ? So I can continue rotating my arrows and other meshes...?

Share this post


Link to post
Share on other sites

What happens if you set camera position to new Vector3(1, -2, 1);

I just see nothing on the screen for Vector3(1, -2, 1);

Share this post


Link to post
Share on other sites

Try switching the Up direction to the usual Vector3.Up, as some functions like PerspectivFovLH have axis specific parameters:

public static Matrix PerspectiveFovLH(
    float fieldOfViewY,
    float aspectRatio,
    float znearPlane,
    float zfarPlane
);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!