protected override void OnPaint(PaintEventArgs e) { if (device == null) { // draw text StringFormat strFormatting = new StringFormat(); strFormatting.Alignment = StringAlignment.Center; strFormatting.LineAlignment = StringAlignment.Center; e.Graphics.DrawString("No preview available", Font, new SolidBrush(ForeColor), new RectangleF(0, 0, Width, Height), strFormatting); base.OnPaint(e); } else { Texture rt = null; Surface oldRt = null; Matrix proj = projMat; if (Monitor.TryEnter(this, 0)) { while (true) { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, BackColor, 1.0f, 0); if (status == Status.NoData) { device.BeginScene(); // draw text deviceFont.DrawText(null, "No preview available", new Rectangle(0, 0, Width, Height), DrawTextFormat.Center | DrawTextFormat.VerticalCenter, ForeColor); device.EndScene(); } else if (status == Status.Generating) { device.BeginScene(); // draw loading text & progress bar deviceFont.DrawText(null, "Generating preview", new Rectangle(0, 0, Width, Height), DrawTextFormat.Center | DrawTextFormat.VerticalCenter, ForeColor); device.EndScene(); } else { device.TestCooperativeLevel(); // render preview pipeline.ProjectionMatrix = proj; pipeline.ViewMatrix = viewMat; pipeline.WorldMatrix = Matrix.Identity; // NOTE: Need to allow SM bypass of lighting?? /*device.Lights[0].Direction = new Vector3(-1, -1, 0); * device.Lights[0].DiffuseColor = ColorValue.FromColor(Color.White); * device.Lights[0].Type = LightType.Directional; * device.Lights[0].Enabled = true;*/ sceneManger.RenderSceneFrame(pipeline, this.Width, this.Height); } device.Present(); if (wantPreview && status == Status.Rendering) { // do another render into new RT surface rt = new Texture(device, 128, 128, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default); oldRt = device.GetRenderTarget(0); device.SetRenderTarget(0, rt.GetSurfaceLevel(0)); proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 1f, 1f, 40); wantPreview = false; } else if (oldRt != null) { device.SetRenderTarget(0, oldRt); if (OnNewPreview != null) { OnNewPreview(rt, null); } break; } else { break; } } Monitor.Exit(this); } } }