Jump to content

  • Log In with Google      Sign In   
  • Create Account


BlackJoker

Member Since 28 Feb 2013
Offline Last Active Yesterday, 03:49 PM

Topics I've Started

Vector3.Unproject return NAN [SharpDX]

24 August 2014 - 09:59 AM

Hi, I am trying to setup picking in my project, but there is strange issue, which not let me do this. Vecto3.Unproject returns NAN always.

 

I found out that its because of Matrix.Inverse inside that method and as a result inversed matrix have 0 in all fields.

All transmitted parameters are correct.

 

So, I wonder isis that correct that from WVP matrix during inversion I always receive Zero matrix?

 

I leave here some code to be sure I transfer parameters correct:
 

ZNear = 0.01;

ZFar = 10000000000000;

mouse coords and ViewPort also correct;

But both vectors receive NAN as a result value;

private void Pick(int mouseX, int mouseY, ref Matrix wvpMatrix, BoundingBox boundingBox)
        {

            Vector3 mouseNearVector = new Vector3(mouseX, mouseY, basicCamera.ZNear);
            Matrix m = Matrix.Identity;
            m = Matrix.Invert(wvpMatrix);
            Vector3 pointNear = Vector3.Unproject(mouseNearVector, 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, basicCamera.ZNear, basicCamera.ZFar,
                wvpMatrix);

            Vector3 mouseFarVector = new Vector3(mouseX, mouseY, basicCamera.ZFar);
            Vector3 pointFar = Vector3.Unproject(mouseFarVector, 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, basicCamera.ZNear, basicCamera.ZFar,
                wvpMatrix);

            Ray ray = new Ray(pointNear, Vector3.Normalize(pointFar - pointNear));
            float distance = 0;
            bool intersect = boundingBox.Intersects(ref ray, out distance);
            D2DText = "Intersect: " + intersect.ToString() + " distance = "+distance;
            intersect = false;
        }

Does Vector3.Unproject() method working correct or I am missing something?

 

Also it works really strange - sometime it returns true if no model under mouse and in the same time returns NAN for distance or return true and real distance value when no model under cursor is present.

It very strange that sometime picking is working at least somehow and the other time its not working at all.


Render to a texture in D2D+DWrite+SharpDX

23 August 2014 - 04:01 PM

Hello.

At last I setup my D2D render target for drawing directly to backbuffer.

I did it like this:

private void Initialize()
        {
            var device = (SharpDX.Direct3D11.Device)_graphicsDevice;
            using (var dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device>())
            {
                DeviceCreationFlags flags = new DeviceCreationFlags();

                d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(dxgiDevice, new CreationProperties() { DebugLevel = DebugLevel.Information }));
                d2dContext = ToDispose(new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None));
            }

            _directWriteFactory = ToDispose(new SharpDX.DirectWrite.Factory1());

            textBrush = new SolidColorBrush(d2dContext, Color4.White);
            textFormat = new TextFormat(_directWriteFactory, "Cambria", 24.0f)
            {
                FlowDirection = FlowDirection.TopToBottom,
                ParagraphAlignment = ParagraphAlignment.Center,
                TextAlignment = TextAlignment.Center,
                WordWrapping = WordWrapping.WholeWord
            };
            var parameters = new PresentationParameters((int)swapchainPanel.ActualWidth, (int)swapchainPanel.ActualHeight, swapchainPanel);
            _presenter = ToDispose(new SwapChainGraphicsPresenter(_graphicsDevice, parameters));
            _graphicsDevice.Presenter = _presenter;
            CreateD2DRenderTarget();
        }

        private void CreateD2DRenderTarget()
        {
            var renderTarget = _presenter.BackBuffer;
            var dpi = DisplayProperties.LogicalDpi;

            // 1. Use same format as the underlying render target with premultiplied alpha mode
            // 2. Use correct DPI
            // 3. Deny drawing direct calls and specify that this is a render target.
            var bitmapProperties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(renderTarget.Format, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                                                             dpi,
                                                             dpi,
                                                             BitmapOptions.CannotDraw | BitmapOptions.Target);

            // create the bitmap render target and assign it to the device context
            d2dTarget = ToDispose(new Bitmap1(d2dContext, renderTarget, bitmapProperties));
            d2dContext.Target = d2dTarget;
        }

But now I want to render NOT not backbuffer, but to texture, so I tried to change render target from _presenter.BackBuffer to my loaded texture2d, but when It runs, framework says:

D2D DEBUG ERROR - The bitmap options [0x5] must be a subset of the flags associated with the DXGI surface.

 

What I am missing here and what I need to do to draw text or whatever d2d content to a texture (2d or other) to apply it after that to the object, for example?

 

Does someone alredy did this before in sharpDX?


How to get rid of camera z-axis rotation

20 August 2014 - 12:56 AM

Hello. 

I have an issue with my quternion based camera: when I move it only on X and Y axis fast it is also begin rotate around Z axis, but it shouldn`t.

I use sharpDX lib and my code is following:

public void RotateX(float _degree_angle)
        {
            if (cameraType == CamType.ThirdPerson)
            {
                _degree_angle = -_degree_angle;
            }

            qRotation = Quaternion.Multiply(Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(_degree_angle)), qRotation);
            
            UpdateViewMatrix();
        }

        public void RotateY(float _degree_angle)
        {
            if (cameraType == CamType.ThirdPerson)
            {
                _degree_angle = -_degree_angle;
            }

            qRotation = Quaternion.Multiply(Quaternion.RotationAxis(Vector3.UnitY, MathUtil.DegreesToRadians(_degree_angle)), qRotation);
            
            UpdateViewMatrix();
        }

private void UpdateViewMatrix()
        {
            qRotation = Quaternion.Normalize(qRotation);

            if (CameraType == CamType.ThirdPerson)
            {
                ViewMatrix = Matrix.Translation(Vector3.Negate(position)) * Matrix.RotationQuaternion(qRotation);

                position -= new Vector3(ViewMatrix.M13, ViewMatrix.M23, ViewMatrix.M33) * radius;

                ViewMatrix = Matrix.LookAtLH(position, lookAt, new Vector3(ViewMatrix.M12, ViewMatrix.M22, ViewMatrix.M32));
            }
            else
            {
                ViewMatrix = Matrix.Translation(Vector3.Negate(position)) * Matrix.RotationQuaternion(qRotation);
            }
        }

I tried to change order of multiplying for x and y quaternion rotations like this:

qRotation = Quaternion.Multiply(Quaternion.RotationAxis(Vector3.UnitY, MathUtil.DegreesToRadians(_degree_angle)), qRotation);

qRotation = Quaternion.Multiply(qRotation, Quaternion.RotationAxis(Vector3.UnitX, MathUtil.DegreesToRadians(_degree_angle2))); 

This helps - after that camera no longer rotate on Z-axis during fast moving on x-y axis, but if I move it on diagonal, if begins to rotate around all 3 axis, describing virtual 8 digit.

 

Could someone please point me the right way working with quaternions to stop camera rotation around Z-axis when I don`t need this?

 

This issue is driving me crazy.

 


Camera view matrix issue

16 August 2014 - 12:16 PM

Hello.

I faced with the issue when creating my camera in SharpDX.

Whaen I create camera instance I could set any camera lookAt vector and it works, but when I rebuild view matrix with other VectorUP (form Y to Z for ex) camera starts to look at the center of coordinates from the same position, but it must only change its orientation and not more.

public void SetUp(Vector3 _up)
        {
            Matrix tmpMatrix = ViewMatrix;

            Vector3 currentLookAt = new Vector3(tmpMatrix.M13, tmpMatrix.M23, tmpMatrix.M33);
         

            ViewMatrix = Matrix.LookAtLH(position, currentLookAt, _up);


Vector3 scale;
            Quaternion rotation;
            Vector3 translation;

            ViewMatrix.Decompose(out scale, out rotation, out translation);
            rotation = Quaternion.Normalize(rotation);
 ViewMatrix = Matrix.Multiply(Matrix.Translation(Vector3.Negate(position)), Matrix.RotationQuaternion(rotation));
            xAxis = new Vector3(ViewMatrix.M11, ViewMatrix.M21, ViewMatrix.M31);
            yAxis = new Vector3(ViewMatrix.M12, ViewMatrix.M22, ViewMatrix.M32);
            zAxis = new Vector3(ViewMatrix.M13, ViewMatrix.M23, ViewMatrix.M33);
        }

Can someone say what I am doing wrong here? 


Using SharpDX.Toolkit.Effect classes as shader replacement

09 August 2014 - 12:06 PM

SharpDX has highlevel effect API to work with shaders to replace HLSL in C++ DX version.

 

So, I want to ask someone to post or provide link for at least 3-4 Effect samples which aplies to D3D and D2D. Or just write it here if you can.

I want to see:

 - how to set/get values to/from shader?

 - how to write shader code and compile it?

 - how to create make different simple effects?

 

Could someone be so kind to help me with this? I am only start to learn SharpDX. I see sharpDX samples already, but there is almost nothing about effects API. Just on basic level to display something. I am interested in a little bit more complex examples with Direct3D.


PARTNERS