Greetings everyone,
I'm still new to DirectX and I'm trying to get some basic functions to work in .net
What I'm trying to do, is display a video on a texture.
I've searched online and got the basic stuff down,
and it works for the most part.
But I'm having a problem with the way its redrawn on the object
it seems as if the video starts out at a scale of 0 and the fills in the object.
Makes for a odd looking zoom in effect.
And the screen doesn't redraw correctly.
perhaps someone can take a look at my code and spot whats wrong.
VB.net 2005Code:Imports System Imports System.Drawing Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports System.Data Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Imports D3D = Microsoft.DirectX.Direct3D Imports Microsoft.DirectX.AudioVideoPlayback Namespace DirectX_Tutorial Public Class Form1 Inherits System.Windows.Forms.Form Private device As D3D.Device Private vertices(6) As CustomVertex.PositionTextured Private texture As Texture Private material As Material Private Vid As New GM2D_Video Public Sub New() InitializeComponent() Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) End Sub Public Sub InitializeDevice() Dim presentParams As New PresentParameters() presentParams.Windowed = True presentParams.SwapEffect = SwapEffect.Discard presentParams.AutoDepthStencilFormat = DepthFormat.D16 presentParams.EnableAutoDepthStencil = True device = New D3D.Device(0, D3D.DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams) device.RenderState.Lighting = False Vid.Create("c:\ice.mpg") Vid.RenderToTexture(device) End Sub Protected Overloads Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0F, 0) device.BeginScene() device.VertexFormat = CustomVertex.PositionTextured.Format device.SetTexture(0, Vid.Get_Texture) device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices) device.EndScene() device.Present() Me.Invalidate() End Sub Private Sub SetUpCamera() device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI) / 4, Me.Width / Me.Height, 0.3F, 500.0F) device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 30), New Vector3(0, 0, 0), New Vector3(0, 1, 0)) 'nextline End Sub Private Sub VertexDeclaration() vertices(6) = New CustomVertex.PositionTextured vertices(0).Position = New Vector3(10.0F, 10.0F, 0.0F) vertices(0).Tu = 0 vertices(0).Tv = 0 vertices(1).Position = New Vector3(-10.0F, -10.0F, 0.0F) vertices(1).Tu = 1 vertices(1).Tv = 1 vertices(2).Position = New Vector3(10.0F, -10.0F, 0.0F) vertices(2).Tu = 0 vertices(2).Tv = 1 vertices(3).Position = New Vector3(-10.0F, -10.0F, 0.0F) vertices(3).Tu = 1 vertices(3).Tv = 1 vertices(4).Position = New Vector3(10.0F, 10.0F, 0.0F) vertices(4).Tu = 0 vertices(4).Tv = 0 vertices(5).Position = New Vector3(-10.0F, 10.0F, 0.0F) vertices(5).Tu = 1 vertices(5).Tv = 0 End Sub Private Sub LoadTexturesAndMaterials() material = New Material material.Diffuse = Color.White material.Specular = Color.LightGray 'nextline material.SpecularSharpness = 15.0F device.Material = material texture = TextureLoader.FromFile(device, "texture1.jpg") End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If components IsNot Nothing Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private Sub InitializeComponent() Me.SuspendLayout() ' 'Form1 ' Me.ClientSize = New System.Drawing.Size(492, 466) Me.Name = "Form1" Me.Text = "Riemer's DirectX Tutorial using C -- Season 2" Me.ResumeLayout(False) End Sub Public Shared Sub Main() Using our_directx_form As New Form1() our_directx_form.InitializeDevice() our_directx_form.SetUpCamera() our_directx_form.VertexDeclaration() ' our_directx_form.LoadTexturesAndMaterials() Application.Run(our_directx_form) End Using End Sub Private components As System.ComponentModel.IContainer End Class Public Class GM2D_Video Private AV_Dev As Microsoft.DirectX.AudioVideoPlayback.Video Private AV_Texture As Microsoft.DirectX.Direct3D.Texture 'Private dd As Microsoft.DirectX.Direct3D.Device Public Sub Create(ByVal File As String) AV_Dev = New Microsoft.DirectX.AudioVideoPlayback.Video(File) End Sub Public Sub Play() AV_Dev.Play() End Sub Public Sub RenderToTexture(ByVal Device3d9 As Microsoft.DirectX.Direct3D.Device) AddHandler AV_Dev.TextureReadyToRender, AddressOf RenderIt AV_Dev.RenderToTexture(Device3d9) ' dd = Device3d9 End Sub Private Sub RenderIt(ByVal sender As Object, ByVal e As Microsoft.DirectX.AudioVideoPlayback.TextureRenderEventArgs) SyncLock (Me) AV_Texture = e.Texture ' dd.SetTexture(0, Get_Texture) End SyncLock End Sub Public Function Get_Texture() As Microsoft.DirectX.Direct3D.Texture Return AV_Texture End Function End Class End Namespace
If you remove the
from the OnPaint Event you can see the video scaling i was talking about.Code:device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0F, 0)
Please any help would be appreciated.
Thank you,
I found this link with a lot of good help
Managed DX Video - GameDev.Net Discussion Forums
please if you have any other tips please post.
the movie didn't play correctly (perhaps flicker occur) because the rate of transfer (of the texture) is not in sync (between the texture generated by GM2D_Video Class and OnPaint Event). i've modified the code a little so it can help with the flickering.
note that the modification only meant to help you with the flicker problem only. HTHCode:Imports System Imports System.Drawing Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Imports System.Data Imports Microsoft.DirectX Imports Microsoft.DirectX.Direct3D Imports D3D = Microsoft.DirectX.Direct3D Imports Microsoft.DirectX.AudioVideoPlayback Namespace DirectX_Tutorial Public Class Form1 Inherits System.Windows.Forms.Form Private device As D3D.Device Private vertices(6) As CustomVertex.PositionTextured Private texture As Texture Private material As Material Private Vid As New GM2D_Video Public Sub New() InitializeComponent() Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'InitializeDevice() 'SetUpCamera() 'LoadTexturesAndMaterials() 'VertexDeclaration() End Sub Public Sub InitializeDevice() Dim presentParams As New PresentParameters() presentParams.Windowed = True presentParams.SwapEffect = SwapEffect.Discard presentParams.AutoDepthStencilFormat = DepthFormat.D16 presentParams.EnableAutoDepthStencil = True device = New D3D.Device(0, D3D.DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams) device.RenderState.Lighting = False Vid.Create(Application.StartupPath & "\ice.mpg") Vid.RenderToTexture(device) AddHandler Vid.ReadyToDisplay, AddressOf RTD End Sub Private Sub RTD(ByVal t As Microsoft.DirectX.Direct3D.Texture) device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Red, 1.0F, 0) device.BeginScene() device.VertexFormat = CustomVertex.PositionTextured.Format device.SetTexture(0, t) device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices) device.EndScene() device.Present() End Sub Protected Overloads Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) 'device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.Red, 1.0F, 0) 'device.BeginScene() 'device.VertexFormat = CustomVertex.PositionTextured.Format 'device.SetTexture(0, Vid.Get_Texture) 'device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices) 'device.EndScene() 'device.Present() 'Me.Invalidate() End Sub Private Sub SetUpCamera() device.Transform.Projection = Matrix.PerspectiveFovLH(CSng(Math.PI) / 4, Me.Width / Me.Height, 0.3F, 500.0F) device.Transform.View = Matrix.LookAtLH(New Vector3(0, 0, 30), New Vector3(0, 0, 0), New Vector3(0, 1, 0)) 'nextline End Sub Private Sub VertexDeclaration() vertices(6) = New CustomVertex.PositionTextured vertices(0).Position = New Vector3(10.0F, 10.0F, 0.0F) vertices(0).Tu = 0 vertices(0).Tv = 0 vertices(1).Position = New Vector3(-10.0F, -10.0F, 0.0F) vertices(1).Tu = 1 vertices(1).Tv = 1 vertices(2).Position = New Vector3(10.0F, -10.0F, 0.0F) vertices(2).Tu = 0 vertices(2).Tv = 1 vertices(3).Position = New Vector3(-10.0F, -10.0F, 0.0F) vertices(3).Tu = 1 vertices(3).Tv = 1 vertices(4).Position = New Vector3(10.0F, 10.0F, 0.0F) vertices(4).Tu = 0 vertices(4).Tv = 0 vertices(5).Position = New Vector3(-10.0F, 10.0F, 0.0F) vertices(5).Tu = 1 vertices(5).Tv = 0 End Sub Private Sub LoadTexturesAndMaterials() material = New Material material.Diffuse = Color.White material.Specular = Color.LightGray 'nextline material.SpecularSharpness = 15.0F device.Material = material texture = TextureLoader.FromFile(device, Application.StartupPath & "\texture1.jpg") End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If components IsNot Nothing Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private Sub InitializeComponent() Me.SuspendLayout() ' 'Form1 ' Me.ClientSize = New System.Drawing.Size(492, 466) Me.Name = "Form1" Me.Text = "Riemer's DirectX Tutorial using C -- Season 2" Me.ResumeLayout(False) End Sub Public Shared Sub Main() Using our_directx_form As New Form1() our_directx_form.InitializeDevice() our_directx_form.SetUpCamera() our_directx_form.VertexDeclaration() ' our_directx_form.LoadTexturesAndMaterials() Application.Run(our_directx_form) End Using End Sub Private components As System.ComponentModel.IContainer End Class Public Class GM2D_Video Private AV_Dev As Microsoft.DirectX.AudioVideoPlayback.Video Private AV_Texture As Microsoft.DirectX.Direct3D.Texture 'Private dd As Microsoft.DirectX.Direct3D.Device Public Event ReadyToDisplay(ByVal t As Microsoft.DirectX.Direct3D.Texture) Public Sub Create(ByVal File As String) AV_Dev = New Microsoft.DirectX.AudioVideoPlayback.Video(File) AddHandler AV_Dev.TextureReadyToRender, AddressOf RenderIt End Sub Public Sub Play() AV_Dev.Play() End Sub Public Sub RenderToTexture(ByVal Device3d9 As Microsoft.DirectX.Direct3D.Device) AV_Dev.RenderToTexture(Device3d9) ' dd = Device3d9 End Sub Private Sub RenderIt(ByVal sender As Object, ByVal e As Microsoft.DirectX.AudioVideoPlayback.TextureRenderEventArgs) SyncLock (Me) 'AV_Texture = e.Texture RaiseEvent ReadyToDisplay(e.Texture) ' dd.SetTexture(0, Get_Texture) End SyncLock End Sub Public Function Get_Texture() As Microsoft.DirectX.Direct3D.Texture Return AV_Texture End Function End Class End Namespace
That did the trick.
Thank you excavator
The last solution worked great.
Now I have another little problem. The code above works for a single video texture. Now if I wanted to do several video textures.
What I’m trying to do is build a wrapper that allows me to create simple 3D planes with a video or image texture. Now I can make a video texture with a image texture above it with no problem as long as the video was the first texture rendered.
I start running into problems when I try to do 2 videos or more or if the first texture rendered was a image texture.
What seems to be happening is that the device.settexture( is retaining the old information and when the new texture is to the device it adds it on top making a odd inner mirror effect.
I know the problem lies with the video refreshing the device texture.
This is what the main loop looks like
Code:Public Sub Refresh() Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, BackColor, 1.0F, 0) Device.BeginScene() Device.VertexFormat = CustomVertex.PositionTextured.Format For t As Integer = 0 To VertextList.Count - 1 If VertextList(t).Type = "Image" Then Device.SetTexture(0, VertextList(t).Texture) Dim a As Integer = VertextList(t).Items.Length Device.DrawUserPrimitives(PrimitiveType.TriangleList, Int(VertextList(t).Items.Length / 3), VertextList(t).Items) End If If VertextList(t).Type = "Video" Then Device.SetTexture(0, VertextList(t).GVideo.Get_Texture) Device.DrawUserPrimitives(PrimitiveType.TriangleList, Int(VertextList(t).Items.Length / 3), VertextList(t).Items) End If Next Device.EndScene() Device.Present() End Sub
How would I go about rendering more than 2 videos to 2 separate objects.
The refresh method is being called with the form1 OnPaint event.
As far as video flickering there is no problem only when its more than one video.
I know this is hard to follow. You can download from the link below if you want to take a look at it.
2 video files that I'm using are just some sample videos they are not included.
C:\ice.mpg
C:\vid.wmv
http://www.tv-graphics.com/DirectX_3DWrapper 5-28-8.zip
VB Studio 2005
Sorry for the mess this is still being worked on.
I made a correction in the code above, the device integer is 0 in the zip file it will come up as c witch is then increased by the number of video fields.
Last edited by lore7460; 05-28-2008 at 12:16 PM.
After playing around with the code a little while I found that my problem lied with the video sources I was using. One was of HD quality with a great frame rate while the other was just a old 20 frame movie.
I have 2 movies now running and they both seem to stay synced up for the most part. I still have a very slight flicker.
I'm also noticing a big jump in process power, I'm going to see if i can put each video to its own thread. That should help i think.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks