2010-04-02 28 views
0

我正在創建一個簡單的應用程序來熟悉SlimDX庫。我發現了一些用MDX編寫的代碼,我試圖將它轉換爲在SlimDX上運行。我在燈光方面遇到了一些問題,因爲一切都顯示爲黑色。該代碼是:SlimDX:Direct3D9閃電問題

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

任何建議的頂點格式? 由於提前

+0

我們能否看到SlimDX代碼?這似乎只是MDX – 2011-02-09 17:15:06

回答

1

你爲什麼打電話device.SetRenderState(RenderState.Lighting, false)?我沒有看到你打開照明的位置。

0

正常需要是在結構中的第二個值(而不是3) - 比不過其他的我不能正常的工作無論是在我目前的項目(從TAO openGL的一個端口,照明工作得很好)。