English 中文(简体)
Lighting with Direct3D9, everything is black
原标题:

I am creating a simple application to get familiar with SlimDX library. I found some code written in MDX and I m trying to convert it to run on SlimDX. I am having some problems with the light because everything is being shown as black. The code is:

public partial class DirectTest : Form
{
    private Device device= null;
    private float angle = 0.0f;
    Light light = new Light();

    public DirectTest()
    {
        InitializeComponent();

        this.Size = new Size(800, 600);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    }
    /// <summary>
    /// We will initialize our graphics device here
    /// </summary>
    public void InitializeGraphics()
    {
        PresentParameters pres_params = new PresentParameters()
        {
            Windowed = true,
            SwapEffect = SwapEffect.Discard
        };
        device = new Device(new Direct3D(), 0, DeviceType.Hardware, this.Handle, 
                    CreateFlags.SoftwareVertexProcessing, pres_params);
    }

    private void SetupCamera()
    {
        device.SetRenderState(RenderState.CullMode, Cull.None);
        device.SetTransform(TransformState.World, Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f),
                         angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)),
                        angle / (float)Math.PI));
        angle += 0.1f;
        device.SetTransform(TransformState.Projection, Matrix.PerspectiveFovLH((float)Math.PI /
            4, this.Width / this.Height, 1.0f, 100.0f));
        device.SetTransform(TransformState.View, Matrix.LookAtLH(new Vector3(0, 0, 5.0f),
            new Vector3(), new Vector3(0, 1, 0)));
        device.SetRenderState(RenderState.Lighting, false);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);
        SetupCamera();

        CustomVertex.PositionColored[] verts = new CustomVertex.PositionColored[3];
        verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
        verts[0].Normal = new Vector3(0.0f, 0.0f, -1.0f);
        verts[0].Color = System.Drawing.Color.White.ToArgb();
        verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
        verts[1].Normal = new Vector3(0.0f, 0.0f, -1.0f);
        verts[1].Color = System.Drawing.Color.White.ToArgb();
        verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
        verts[2].Normal = new Vector3(0.0f, 0.0f, -1.0f);
        verts[2].Color = System.Drawing.Color.White.ToArgb();

        light.Type = LightType.Point;
        light.Position = new Vector3();
        light.Diffuse = System.Drawing.Color.White;
        light.Attenuation0 = 0.2f;
        light.Range = 10000.0f;

        device.SetLight(0, light);
        device.EnableLight(0, true);

        device.BeginScene();

        device.VertexFormat = CustomVertex.PositionColored.format;
        device.DrawUserPrimitives<CustomVertex.PositionColored>(PrimitiveType.TriangleList, 1, verts);
        device.EndScene();

        device.Present();
        this.Invalidate();
    }
}

}

The Vertex Format that I am using is the following

    [StructLayout(LayoutKind.Sequential)]
    public struct PositionNormalColored
    {
        public Vector3 Position;
        public int Color;
        public Vector3 Normal;
        public static readonly VertexFormat format = VertexFormat.Diffuse | VertexFormat.Position | VertexFormat.Normal;
    }

Any suggestions on what the problem might be? Thanks in Advance

最佳回答

Why are you calling device.SetRenderState(RenderState.Lighting, false)? I don t see where you turn lighting back on.

问题回答

Normal needs to be the second value in the struct (not the 3rd) - but other than that I cant it to work either in my current project (a port from TAO openGL where lighting worked fine).





相关问题
openGL rotating with Lighting problem

I want to draw car in my world. but i have problem with lighting when my car rotate ,it seems light position change when car is rotating; but when i draw simple cube with glut function it work ...

Lighting with Direct3D9, everything is black

I am creating a simple application to get familiar with SlimDX library. I found some code written in MDX and I m trying to convert it to run on SlimDX. I am having some problems with the light because ...

Java 3d: Unable to get Shape3D to be affected by lights

I am attempting to get a custom Shape3D to be affected by a DirectedLight in java 3D, but nothing I do seems to work. The Shape has a geometry that is an IndexedQuadArray, with the NORMAL flag set ...

Are there any algorithms for removing lighting from a video?

Are there any algorithms for removing lighting from video? I have a video in which some objects are too strongly lit. How might I remove lighting from that objects without corrupting the entire video?

Working with Lights

Trying to get a grasp on lights and working through the OpenGL Superbible book. Below is what I am currently using for my lighting. It s placed in the SetupRC function. The lighting is mostly working ...

How can I make a ball of light in openGL?

I m trying to make a orb of light (Like a sun) but I can t seem to make it visible at all. I ll give you some snipets of code I have. It s in Java LWJGL, so it might look a little different. private ...

开放式轻型问题

我装上了物体,在我提取物体时,我把颜色划入绿线。

热门标签