[SlimDx] Problem with enabling perspective

Started by
2 comments, last by krajniak 15 years, 4 months ago
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
Advertisement
Your aspect ratio should typically be the width of your backbuffer divided by the height. You're just using a value of 10, which will look very weird even for widescreen resolutions.
Also, you are specifying your position coordinates as being pre-transformed, so Direct3D won't transform them at all. If you don't want this to happen, don't specify PositionRhw in the vertex format.
Mike Popoloski | Journal | SlimDX
thanks for reponse,
what kind of structure and what vertex format should i use if i just want a 3d position and color for each vertex ?

This topic is closed to new replies.

Advertisement