|
|
发表于 2006-2-8 23:22:00
|
显示全部楼层
Re:这是怎么实现的?
贴上我的相关代码吧~
我是用的渲染组来管理渲染物件的,每个屏幕上面显示的东西都是一个物件,我贴上我的图形对象的创建和渲染代码:
显示CreateVertexBuffer创建顶点缓存:
Public Sub CreateVertexBuffer()
Dim rectf1 As New RECTF(0.0!, 0.0!, 1.0!, 1.0!)
If (Not Me.m_ani.D3DTexture Is Nothing) Then
Dim description1 As SurfaceDescription = Me.m_ani.D3DTexture.GetLevelDescription(0)
Dim single1 As Single = description1.Width
Dim description2 As SurfaceDescription = Me.m_ani.D3DTexture.GetLevelDescription(0)
Dim single2 As Single = description2.Height
rectf1.left = (Me.m_UVrect.left / single1)
rectf1.top = (Me.m_UVrect.top / single2)
rectf1.right = (Me.m_UVrect.right / single1)
rectf1.bottom = (Me.m_UVrect.bottom / single2)
Me.m_UVrect.left = rectf1.left
Me.m_UVrect.top = rectf1.top
Me.m_UVrect.right = rectf1.right
Me.m_UVrect.bottom = rectf1.bottom
End If
Me.vertexBuffer = New vertexBuffer(GetType(Direct3D.CustomVertex.TransformedColoredTextured), 4, MyBase.m_device.m_device, Usage.None, (VertexFormats.Texture1 Or (VertexFormats.Diffuse Or VertexFormats.Transformed)), Pool.Default)
Dim stream1 As GraphicsStream = Me.vertexBuffer.Lock(0, 0, LockFlags.None)
Dim texturedArray1 As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(4 - 1) {}
texturedArray1(0).X = Me.m_rect.left
texturedArray1(0).Y = Me.m_rect.top
texturedArray1(0).Z = 0.5!
texturedArray1(0).Rhw = 1.0!
texturedArray1(0).Color = Me.m_color.ToArgb
texturedArray1(0).Tu = rectf1.left
texturedArray1(0).Tv = rectf1.top
texturedArray1(1).X = Me.m_rect.right
texturedArray1(1).Y = Me.m_rect.top
texturedArray1(1).Z = 0.5!
texturedArray1(1).Rhw = 1.0!
texturedArray1(1).Color = Me.m_color.ToArgb
texturedArray1(1).Tu = rectf1.right
texturedArray1(1).Tv = rectf1.top
texturedArray1(2).X = Me.m_rect.left
texturedArray1(2).Y = Me.m_rect.bottom
texturedArray1(2).Z = 0.5!
texturedArray1(2).Rhw = 1.0!
texturedArray1(2).Color = Me.m_color.ToArgb
texturedArray1(2).Tu = rectf1.left
texturedArray1(2).Tv = rectf1.bottom
texturedArray1(3).X = Me.m_rect.right
texturedArray1(3).Y = Me.m_rect.bottom
texturedArray1(3).Z = 0.5!
texturedArray1(3).Rhw = 1.0!
texturedArray1(3).Color = Me.m_color.ToArgb
texturedArray1(3).Tu = rectf1.right
texturedArray1(3).Tv = rectf1.bottom
stream1.Write(texturedArray1)
Me.vertexBuffer.Unlock()
End Sub
然后是渲染代码:
Public Overrides Function OnRender() As Boolean
Dim material1 As New Material
If Me.m_Render = False Then Return True
MyBase.m_device.m_device.SetRenderState(RenderStates.AlphaBlendEnable, True)
MyBase.m_device.m_device.SetRenderState(RenderStates.AlphaTestEnable, False)
MyBase.m_device.m_device.SetRenderState(RenderStates.SourceBlend, 5)
MyBase.m_device.m_device.SetRenderState(RenderStates.DestinationBlend, 6)
MyBase.m_device.m_device.SetRenderState(RenderStates.Clipping, 2)
MyBase.m_device.m_device.SetRenderState(RenderStates.ZBufferWriteEnable, False)
MyBase.m_device.m_device.SetRenderState(RenderStates.ZEnable, False)
MyBase.m_device.m_device.SetRenderState(RenderStates.Lighting, False)
MyBase.m_device.m_device.SetRenderState(RenderStates.FogEnable, False)
If Not Me.m_ShadowTex Is Nothing Then '渲染影子图层
MyBase.m_device.m_device.SetTexture(0, Me.m_ShadowTex)
MyBase.m_device.m_device.TextureState.TextureState(0).ColorOperation = TextureOperation.Modulate
MyBase.m_device.m_device.TextureState.TextureState(0).AlphaOperation = TextureOperation.Modulate
MyBase.m_device.m_device.TextureState.TextureState(0).ColorArgument1 = TextureArgument.TextureColor
MyBase.m_device.m_device.TextureState.TextureState(0).ColorArgument2 = TextureArgument.Diffuse
MyBase.m_device.m_device.TextureState.TextureState(0).AlphaArgument1 = TextureArgument.TextureColor
MyBase.m_device.m_device.TextureState.TextureState(0).AlphaArgument2 = TextureArgument.Diffuse
MyBase.m_device.m_device.SetStreamSource(0, Me.vertexBuffer, 0)
MyBase.m_device.m_device.VertexFormat = (VertexFormats.Texture1 Or (VertexFormats.Diffuse Or VertexFormats.Transformed))
MyBase.m_device.m_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2)
End If
MyBase.m_device.m_device.SetTexture(0, Me.m_ani.D3DTexture)
MyBase.m_device.m_device.TextureState.TextureState(0).ColorOperation = TextureOperation.Modulate
MyBase.m_device.m_device.TextureState.TextureState(0).AlphaOperation = TextureOperation.Modulate
MyBase.m_device.m_device.TextureState.TextureState(0).ColorArgument1 = TextureArgument.TextureColor
MyBase.m_device.m_device.TextureState.TextureState(0).ColorArgument2 = TextureArgument.Diffuse
MyBase.m_device.m_device.TextureState.TextureState(0).AlphaArgument1 = TextureArgument.TextureColor
MyBase.m_device.m_device.TextureState.TextureState(0).AlphaArgument2 = TextureArgument.Diffuse
MyBase.m_device.m_device.SetStreamSource(0, Me.vertexBuffer, 0)
MyBase.m_device.m_device.VertexFormat = (VertexFormats.Texture1 Or (VertexFormats.Diffuse Or VertexFormats.Transformed))
If CurrentEffect <> RenderObj.ShaderEffect.Null And Not Shader Is Nothing Then ' 判断是否使用Shader,这个是我自己设置的Enum
MyBase.Shader.Begin(FX.None)
If CurrentEffect = RenderObj.ShaderEffect.GrayScale Then MyBase.Shader.BeginPass(0)
If CurrentEffect = RenderObj.ShaderEffect.Highlight Then MyBase.Shader.BeginPass(1)
MyBase.m_device.m_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2)
MyBase.Shader.EndPass()
MyBase.Shader.End()
Else
MyBase.m_device.m_device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2)
End If
Return True
End Function
|
|