Sign in to follow this  
krajniak

[SlimDx] Problem with enabling perspective

Recommended Posts

Welcome to all gamedev users. I have a problem with enabling perspective view. I have a panel on which I want to draw static perspective view on a 3D object. My code below: Initialization:
Direct3D direct3D = new Direct3D();

PresentParameters presentParams = new PresentParameters();
presentParams.BackBufferHeight = panel.ClientRectangle.Height;
presentParams.BackBufferWidth = panel.ClientRectangle.Width;
presentParams.DeviceWindowHandle = panel.Handle;
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;

device = new Device(direct3D, 0, DeviceType.Hardware, panel.Handle,
         CreateFlags.SoftwareVertexProcessing, presentParams);

Vertex Struct declaration:
[StructLayout(LayoutKind.Sequential)]
private struct Vertex
{
    public Vector4 PositionRhw;
    public int Color;
}

and this is my panel on paint event
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
            device.BeginScene();

            Vector3 eyePosition = new Vector3(100, 0, 100f);
            Vector3 direction = new Vector3(0, 0, 0);
            Vector3 upDirection = new Vector3(0, -1, 0);
            Matrix view = Matrix.LookAtRH(eyePosition, direction, upDirection);
            device.SetTransform(TransformState.View, view);

            float fieldOfView = (float)Math.PI / 4;
            float aspectRatio = 10.0f;
            float nearPlane = 1.0f;
            float farPlane = 1000.0f;
            Matrix projection = Matrix.PerspectiveFovRH(fieldOfView, aspectRatio, nearPlane, farPlane);
            device.SetTransform(TransformState.Projection, projection); 
            

            //drawUnitaryCube(0, 0, -2, Color.Black, device);

            Vertex[] vertexData = new Vertex[3];
            vertexData[0].Color = Color.Red.ToArgb();
            vertexData[1].Color = Color.Green.ToArgb();
            vertexData[2].Color = Color.Blue.ToArgb();
            vertexData[0].PositionRhw = new Vector4(630.0f, 000.0f, 1.0f, 1.0f);
            vertexData[1].PositionRhw = new Vector4(1250.0f, 878.0f, 1.0f, 1.0f);
            vertexData[2].PositionRhw = new Vector4(0.0f, 878.0f, 1.0f, 1.0f);
            VertexBuffer vertices = new VertexBuffer(device, 3 * 20,
                Usage.WriteOnly, VertexFormat.None, Pool.Managed);
            DataStream stream = vertices.Lock(0, 0, LockFlags.None);
            stream.WriteRange(vertexData);
            vertices.Unlock();
            device.SetStreamSource(0, vertices, 0, 20);
            device.VertexFormat = VertexFormat.PositionRhw | VertexFormat.Diffuse;
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

            vertexData[0].Color = Color.Yellow.ToArgb();
            vertexData[1].Color = Color.Magenta.ToArgb();
            vertexData[2].Color = Color.Purple.ToArgb();
            vertexData[0].PositionRhw = new Vector4(410.0f, 110.0f, 0.0f, 1.0f);
            vertexData[1].PositionRhw = new Vector4(660.0f, 510.0f, 0.0f, 1.0f);
            vertexData[2].PositionRhw = new Vector4(160.0f, 510.0f, 0.0f, 1.0f);
            vertices = new VertexBuffer(device, 3 * 20,
                Usage.WriteOnly, VertexFormat.None, Pool.Managed);
            stream = vertices.Lock(0, 0, LockFlags.None);
            stream.WriteRange(vertexData);
            vertices.Unlock();
            device.SetStreamSource(0, vertices, 0, 20);
            device.VertexFormat = VertexFormat.PositionRhw | VertexFormat.Diffuse;
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

            device.EndScene();

            device.Present();

and its seems that view and projection matrix have no imact on viewed scene couse what you see is orthographic projection on (0,0,0) to about (1250,878,1). I'm new to the directX and slimdx thing, so i will really appreciate help

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this