【问题标题】:Lighting with Direct3D9, everything is black用 Direct3D9 照明,一切都是黑色的
【发布时间】:2010-04-02 11:32:27
【问题描述】:

我正在创建一个简单的应用程序来熟悉 SlimDX 库。我发现了一些用 MDX 编写的代码,我正在尝试将其转换为在 SlimDX 上运行。我的灯有一些问题,因为一切都显示为黑色。代码是:

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();
    }
}

}

我使用的顶点格式如下

    [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;
    }

对可能出现的问题有什么建议吗? 提前致谢

【问题讨论】:

  • 我们也能看到 SlimDX 代码吗?这似乎只是 MDX

标签: lighting slimdx


【解决方案1】:

你为什么打电话给device.SetRenderState(RenderState.Lighting, false)?我没看到你在哪里重新打开照明。

【讨论】:

    【解决方案2】:

    Normal 必须是结构中的第二个值(不是第三个)——但除此之外,我不能在我当前的项目中使用它(来自 TAO openGL 的端口,其中照明工作正常)。

    【讨论】:

      猜你喜欢
      • 2014-01-30
      • 2022-11-04
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多