Managed Direct3D - problem ze światłami/materiałami

0

Witam, uczyłem się właśnie Managed DX na podstawie msdn: http://msdn.microsoft.com/en-us/library/windows/desktop/ms920768.aspx
Mam jednak problem z 'CustomVertex.PositionNormal' - po wklejeniu przykładowego kodu z tutka, wyrzuca mi puste okno (bez cylindru), na wierzchołkach 'PositionColored' wszystko było OK.

Daje kod:

 
Device device = null;
VertexBuffer vb = null;

            public CreateDevice()
            {
                this.ClientSize = new System.Drawing.Size(640, 360);
                this.Text = "DirectX'er";
            }

            public bool InitializeGraphics()
            {
                try
                {
                    PresentParameters presentParams = new PresentParameters();
                    presentParams.Windowed = true;
                    presentParams.SwapEffect = SwapEffect.Discard;

                    // swiatla i format
                    presentParams.EnableAutoDepthStencil = true;
                    presentParams.AutoDepthStencilFormat = DepthFormat.D16;

                    device = new Device(0, DeviceType.Hardware, this,
                            CreateFlags.SoftwareVertexProcessing, presentParams);
                    device.DeviceReset += new EventHandler(
                    this.OnResetDevice);
                    this.OnResetDevice(device, null);


                    //(oncreatedevice)

                    CreateVertexBuffer();

                    return true;
                }
                catch (DirectXException)
                {
                    return false;
                }
            }

            public void OnResetDevice(object sender, EventArgs e)
            {
                Device dev = (Device)sender;
                device.RenderState.CullMode = Cull.None;

              //swiatla
                device.RenderState.ZBufferEnable = true;
                device.RenderState.Lighting = true;   
            }

            public void Render(int ms)
            {
                if (device == null)
                    return;

                //tlo
                device.Clear(ClearFlags.Target, System.Drawing.Color.Lavender, 1.0f, 0);

                device.BeginScene();

                SetupMatrices(ms);
                SetupLights();
                device.SetStreamSource(0, vb, 0);
                device.VertexFormat = CustomVertex.PositionNormalColored.Format;

                device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
               
                device.EndScene();
                device.Present();
            }

            public void CreateVertexBuffer()
            {
                vb = new VertexBuffer(typeof(CustomVertex.PositionNormal),
                                    100,
                                    device,
                                    Usage.WriteOnly,
                                    CustomVertex.PositionNormal.Format,
                                     Pool.Default);
                  vb.Created +=
                    new System.EventHandler(this.OnCreateVertexBuffer);
                      this.OnCreateVertexBuffer(vb, null);
            }

            public void OnCreateVertexBuffer(object sender, EventArgs e)
            {
               // VertexBuffer vb = (VertexBuffer)sender;
                CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vb.Lock(0, 0);

                for (int i = 0; i < 50; i++)
                {
                    float theta = (float)(2 * Math.PI * i) / 49;
                    verts[2 * i].Position = new Vector3(
                            (float)Math.Sin(theta), -1, (float)Math.Cos(theta));
                    verts[2 * i].Normal = new Vector3(
                            (float)Math.Sin(theta), 0, (float)Math.Cos(theta));
                    verts[2 * i + 1].Position = new Vector3(
                            (float)Math.Sin(theta), 1, (float)Math.Cos(theta));
                    verts[2 * i + 1].Normal = new Vector3(
                            (float)Math.Sin(theta), 0, (float)Math.Cos(theta));
                }

                vb.Unlock();
            }

                    private void SetupLights()
{

                        //material
    System.Drawing.Color col = System.Drawing.Color.White;
    
    Microsoft.DirectX.Direct3D.Material mtrl = new Microsoft.DirectX.Direct3D.Material();
    mtrl.Diffuse = col;
    mtrl.Ambient = col;
    device.Material = mtrl;

                        //swiatlo
    device.Lights[0].Type = LightType.Directional;
    device.Lights[0].Diffuse = System.Drawing.Color.DarkTurquoise;
    device.Lights[0].Direction = new Vector3(
                    (float)Math.Cos(Environment.TickCount / 250.0f),
                    1.0f,
                    (float)Math.Sin(Environment.TickCount / 250.0f));
    device.Lights[0].Enabled = true; 
    device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x202020);
}

            public void CreateVertex()
            {
                CustomVertex.PositionNormal[] verts = (CustomVertex.PositionNormal[])vb.Lock(0, 0);

                verts[0].X = -1.0f;
                verts[0].Y = -1.0f;
                verts[0].Z = 0.0f;
               // verts[0].Color = System.Drawing.Color.Red.ToArgb();

                verts[1].X = 1.0f;
                verts[1].Y = -1.0f;
                verts[1].Z = 0.0f;
               // verts[1].Color = System.Drawing.Color.Blue.ToArgb();

                verts[2].X = 0.0f;
                verts[2].Y = 1.0f;
                verts[2].Z = 2.0f;
               // verts[2].Color = System.Drawing.Color.Yellow.ToArgb();

                //unlock i copy
                vb.Unlock();
            }


            public void SetupMatrices(int ms)
            {
                int iTime = Environment.TickCount % ms;
                float fAngle = iTime * (2.0f * (float)Math.PI) / (Single)ms;
                device.Transform.World = Matrix.RotationY(fAngle);
                device.Transform.View = Matrix.LookAtLH(
                    new Vector3(0.0f, 3.0f, -5.0f),
                    new Vector3(0.0f, 0.0f, 0.0f),
                    new Vector3(0.0f, 1.0f, 0.0f));
                device.Transform.Projection = Matrix.PerspectiveFovLH(
                    (float)Math.PI / 4,
                    1.0f,
                    1.0f,
                    100.0f);
            }


        }



        public void DXForm()
        {
            frm = new CreateDevice();

                if (!frm.InitializeGraphics()) 
                {
                    MessageBox.Show("D3D :(");
                    return;
                }

                    frm.Show();
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    byte[] buffer = encoder.GetBytes("g_on");
                    gameOn = true;

                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                   
                   //petla
                    while (frm.Created)
                    {
                        frm.Render(ms);
                        Application.DoEvents();
                    }

                    gameOn = false;
                    if (on)
                    {
                        buffer = encoder.GetBytes("g_off");
                        clientStream.Write(buffer, 0, buffer.Length);
                        clientStream.Flush();
                    }
        }
0

Wystarczyło zamienić

device.Clear(ClearFlags.Target, System.Drawing.Color.Lavender, 1.0f, 0);

na

device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Lavender, 1.0f, 0);
1

Managed DX jest w sumie już nierozwijany, wspieramy przez Microsoft jest XNA - zastanów się nad przejściem na tą bibliotekę.
(PS. fajną godzinę na napisanie posta wybrałeś ;) )

1 użytkowników online, w tym zalogowanych: 0, gości: 1