2010-07-03 45 views

回答

0

下面是一个窗体,用于初始化Direct3D,创建几个三角形顶点并在其上显示视频。因为我对顶点不了解,所以它不能正确排列,但它成功加载视频并将其作为纹理渲染到三角形上。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private Microsoft.DirectX.Direct3D.Device _device; 
    private Microsoft.DirectX.Direct3D.Font font; 
    private Microsoft.DirectX.AudioVideoPlayback.Video _video; 

    CustomVertex.PositionTextured[] vertices; 

    public void Init() 
    { 
     PresentParameters present_params = new PresentParameters(); 
     present_params.Windowed = true; 
     present_params.SwapEffect = SwapEffect.Discard; 

     _device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, present_params); 

     font = new Microsoft.DirectX.Direct3D.Font(_device, 20, 0, FontWeight.Bold, 0, false, CharacterSet.Default, Precision.Default, FontQuality.Default, PitchAndFamily.DefaultPitch, "Arial"); 

     _video = new Video("clock.avi"); 
     _video.TextureReadyToRender += new TextureRenderEventHandler(_video_TextureReadyToRender); 
     _video.RenderToTexture(_device); 

     vertices = new CustomVertex.PositionTextured[6]; 
     vertices[0].Position = new Vector3(1f, 1f, 0f); 
     vertices[0].Tu = 0; 
     vertices[0].Tv = 1; 
     vertices[1].Position = new Vector3(-1f, -1f, 0f); 
     vertices[1].Tu = 1; 
     vertices[1].Tv = 1; 
     vertices[2].Position = new Vector3(1f, -1f, 0f); 
     vertices[2].Tu = 0; 
     vertices[2].Tv = 0; 
     vertices[3].Position = new Vector3(-1.1f, -0.99f, 0f); 
     vertices[3].Tu = 1; 
     vertices[3].Tv = 0; 
     vertices[4].Position = new Vector3(0.99f, 1.1f, 0f); 
     vertices[4].Tu = 0; 
     vertices[4].Tv = 0; 
     vertices[5].Position = new Vector3(-1.1f, 1.1f, 0f); 
     vertices[5].Tu = 1; 
     vertices[5].Tv = 1; 
    } 
    void _video_TextureReadyToRender(object sender, TextureRenderEventArgs e) 
    { 
     _device.BeginScene(); 
     _device.SetTexture(0, e.Texture); 
     _device.VertexFormat = CustomVertex.PositionTextured.Format; 
     _device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices); 

     font.DrawText(null, "test overlay", 5, 5, Color.Red); 

     _device.EndScene(); 
     _device.Present(); 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     _device.Clear(ClearFlags.Target, Color.Blue, 0.0F, 0); 

    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     _video.Play(); 
    } 
} 

你需要把你的场景绘制TextureReadyToRender事件中的代码,否则您的影片就不会显示出来。

+0

这看起来完全像我需要的,但视频根本不显示。我不希望视频在三角形内部显示,我只想在视频顶部显示文字。你介意告诉我怎么做? – Mavamaarten 2013-06-29 16:54:25