I pretty much got it working but there are still some depth issues...
Public Class Form1
Inherits System.Windows.Forms.Form
Private mobjGraphics As DX9ToolsR4.DX9Graphics
Private mobjFont As Direct3D.Font
Private Verts(4) As Direct3D.CustomVertex.PositionColoredTextured
Private VertRect(3) As Direct3D.CustomVertex.PositionColoredTextured
Private mobjSize As SizeF = New SizeF(320, 240)
Private msngDepth As Single = 0
Private mobjPrevMouse As Vector2
Private Tex As Direct3D.Texture
Private mblnShowLine As Boolean = True
Private mblnRotate As Boolean = False
Private mintRotation As Integer = 0
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents tmrTimer As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.tmrTimer = New System.Windows.Forms.Timer(Me.components)
Me.AutoScaleBaseSize = New System.Drawing.Size(10, 22)
Me.ClientSize = New System.Drawing.Size(586, 464)
Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Me.Focus()
ResizeForm(New Size(640, 480))
mobjGraphics = New DX9ToolsR4.DX9Graphics(Me)
With mobjGraphics.Device
.RenderState.Lighting = True
.RenderState.FillMode = DirectX.Direct3D.FillMode.Solid
.RenderState.ShadeMode = DirectX.Direct3D.ShadeMode.Gouraud
.RenderState.CullMode = DirectX.Direct3D.Cull.None
.RenderState.Ambient = Color.White
End With
With mobjGraphics.Camera
.AspectRatio = CSng(Me.ClientSize.Width / Me.ClientSize.Height)
.SetPosition(DX9ToolsR4.Camera.SetVector.Z, CSng(Me.ClientSize.Width) / 2)
.NearPlane = 1
.FarPlane = 1000
End With
mobjFont = New Direct3D.Font(mobjGraphics.Device, Me.Font)
Tex = Direct3D.TextureLoader.FromFile(mobjGraphics.Device, "../exp.png")
SetupVerts(0, 0)
SetupVertRect(mobjSize.Width / 2, mobjSize.Height / 2)
End Sub
Private Sub SetupVerts(ByVal X As Single, ByVal Y As Single)
Dim D As Direct3D.Device
Dim Half As SizeF
D = mobjGraphics.Device
Half = New SizeF(mobjSize.Width / 2, mobjSize.Height / 2)
Verts(0) = New Direct3D.CustomVertex.PositionColoredTextured(ToObjSpace(D, X - Half.Width, Y - Half.Height, msngDepth), Color.White.ToArgb, 0, 0)
Verts(1) = New Direct3D.CustomVertex.PositionColoredTextured(ToObjSpace(D, X + Half.Width, Y - Half.Height, msngDepth), Color.White.ToArgb, 0, 0)
Verts(2) = New Direct3D.CustomVertex.PositionColoredTextured(ToObjSpace(D, X + Half.Width, Y + Half.Height, msngDepth), Color.White.ToArgb, 0, 0)
Verts(3) = New Direct3D.CustomVertex.PositionColoredTextured(ToObjSpace(D, X - Half.Width, Y + Half.Height, msngDepth), Color.White.ToArgb, 0, 0)
Verts(4) = New Direct3D.CustomVertex.PositionColoredTextured(ToObjSpace(D, X - Half.Width, Y - Half.Height, msngDepth), Color.White.ToArgb, 0, 0)
End Sub
Private Sub SetupVertRect(ByVal X As Single, ByVal Y As Single)
Dim D As Direct3D.Device
Dim Half As SizeF
Dim V As Vector3
D = mobjGraphics.Device
Half = New SizeF(256, 256)
V = ToObjSpace(D, X - Half.Width, Y + Half.Height, msngDepth)
VertRect(0) = New Direct3D.CustomVertex.PositionColoredTextured(V, Color.White.ToArgb, 0, 1)
V = ToObjSpace(D, X - Half.Width, Y - Half.Height, msngDepth)
VertRect(1) = New Direct3D.CustomVertex.PositionColoredTextured(V, Color.White.ToArgb, 0, 0)
V = ToObjSpace(D, X + Half.Width, Y + Half.Height, msngDepth)
VertRect(2) = New Direct3D.CustomVertex.PositionColoredTextured(V, Color.White.ToArgb, 1, 1)
V = ToObjSpace(D, X + Half.Width, Y - Half.Height, msngDepth)
VertRect(3) = New Direct3D.CustomVertex.PositionColoredTextured(V, Color.White.ToArgb, 1, 0)
End Sub
Private Sub ResizeForm(ByVal S As Size)
Me.Size = S
Me.Size = New Size(S.Width + (Me.Width - Me.ClientSize.Width), _
S.Height + (Me.Height - Me.ClientSize.Height))
End Sub
Private Sub SetupLights(ByVal Dev As Direct3D.Device)
Dim L As Direct3D.Light = Dev.Lights(0)
L = LightHelpers.CreateDirectionalLight(Color.White, DX9ToolsR4.Direction.Z)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim Msg As String
Dim Mat As Direct3D.Material
Dim MT As Matrix
Dim Rad As Single
Rad = Direct3D.Geometry.DegreeToRadian(mintRotation)
With mobjGraphics
.Clear()
.Device.BeginScene()
SetupLights(.Device)
Mat.Ambient = Color.White
Mat.Diffuse = Mat.Ambient
.Device.Material = Mat
.Device.VertexFormat = Verts(0).Format
.Device.TextureState(0).ColorOperation = Direct3D.TextureOperation.Modulate
.Device.TextureState(0).ColorArgument1 = Direct3D.TextureArgument.TextureColor
.Device.TextureState(0).ColorArgument2 = Direct3D.TextureArgument.Diffuse
.Device.TextureState(0).AlphaOperation = Direct3D.TextureOperation.SelectArg1
.Device.TextureState(0).AlphaArgument1 = Direct3D.TextureArgument.TextureColor
.Device.SamplerState(0).MinFilter = Direct3D.TextureFilter.Linear
.Device.SamplerState(0).MagFilter = Direct3D.TextureFilter.Linear
.Device.SamplerState(0).MipFilter = Direct3D.TextureFilter.Linear
.Device.SamplerState(0).AddressU = Direct3D.TextureAddress.Wrap
.Device.SamplerState(0).AddressV = Direct3D.TextureAddress.Wrap
.Device.Transform.World = Matrix.Identity
SetupVertRect(mobjSize.Width / 2, mobjSize.Height / 2)
.Device.SetTexture(0, Tex)
.Device.RenderState.FillMode = DirectX.Direct3D.FillMode.Solid
.Device.DrawUserPrimitives(DirectX.Direct3D.PrimitiveType.TriangleStrip, 2, VertRect)
.Device.SetTexture(0, Nothing)
.Device.RenderState.FillMode = DirectX.Direct3D.FillMode.WireFrame
.Device.DrawUserPrimitives(DirectX.Direct3D.PrimitiveType.TriangleStrip, 2, VertRect)
If mblnShowLine Then
If mblnRotate Then
MT = Matrix.Identity
MT.RotateYawPitchRoll(Rad, Rad, Rad)
.Device.Transform.World = MT
End If
.Device.SetTexture(0, Nothing)
.Device.DrawUserPrimitives(DirectX.Direct3D.PrimitiveType.LineStrip, 4, Verts)
Else
SetupVertRect(mobjPrevMouse.X, mobjPrevMouse.Y)
If mblnRotate Then
MT = Matrix.Identity
MT.RotateYawPitchRoll(rad, rad, rad)
.Device.Transform.World = MT
End If
.Device.SetTexture(0, Tex)
.Device.RenderState.FillMode = DirectX.Direct3D.FillMode.Solid
.Device.DrawUserPrimitives(DirectX.Direct3D.PrimitiveType.TriangleStrip, 2, VertRect)
.Device.SetTexture(0, Nothing)
.Device.RenderState.FillMode = DirectX.Direct3D.FillMode.WireFrame
.Device.DrawUserPrimitives(DirectX.Direct3D.PrimitiveType.TriangleStrip, 2, VertRect)
End If
Msg = mobjPrevMouse.ToString & vbCrLf & "------------------------------" & vbCrLf
Msg &= mobjSize.ToString & vbCrLf
Msg &= "Depth: " & msngDepth.ToString & vbCrLf
Msg &= "Show Line: " & mblnShowLine.ToString & vbCrLf
Msg &= "Rotating: " & mblnRotate.ToString & vbCrLf
mobjFont.DrawText(Msg, Me.ClientRectangle, Direct3D.DrawTextFormat.Top Or Direct3D.DrawTextFormat.Left, Color.White)
.Device.EndScene()
.Present()
End With
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Dim V As Vector3
Dim vp As Direct3D.Viewport
If mobjGraphics Is Nothing Then Exit Sub
mobjPrevMouse = New Vector2(e.X, e.Y)
If mblnShowLine Then
SetupVerts(mobjPrevMouse.X, mobjPrevMouse.Y)
Else
SetupVertRect(mobjPrevMouse.X, mobjPrevMouse.Y)
End If
Form1_Paint(Nothing, Nothing)
End Sub
Public Shared Function ToObjSpace(ByVal Dev As Direct3D.Device, ByVal X As Single, ByVal Y As Single, ByVal Depth As Single) As Vector3
Dim vNear, vFar As Vector3
vNear = New Vector3(X, Y, 0)
vNear.Unproject(Dev.Viewport, Dev.Transform.Projection, Dev.Transform.View, Dev.Transform.World)
vFar = New Vector3(X, Y, 1)
vFar.Unproject(Dev.Viewport, Dev.Transform.Projection, Dev.Transform.View, Dev.Transform.World)
vFar.Subtract(vNear)
Return Vector3.Lerp(vNear, vFar, Depth)
End Function
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Left
mobjSize.Width -= 1
Case Keys.Right
mobjSize.Width += 1
Case Keys.Up
mobjSize.Height -= 1
Case Keys.Down
mobjSize.Height += 1
Case Keys.A
msngDepth += CSng(0.01)
Case Keys.Z
msngDepth -= CSng(0.01)
Case Keys.L
mblnShowLine = Not mblnShowLine
Case Keys.R
mblnRotate = Not mblnRotate
If mblnRotate Then
tmrTimer.Start()
Else
tmrTimer.Stop()
mintRotation = 0
End If
End Select
If mblnShowLine Then
SetupVerts(mobjPrevMouse.X, mobjPrevMouse.Y)
Else
SetupVertRect(mobjPrevMouse.X, mobjPrevMouse.Y)
End If
Form1_Paint(Nothing, Nothing)
Application.DoEvents()
End Sub
Private Sub tmrTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
mintRotation += 5
If mintRotation > 359 Then mintRotation = mintRotation - 360
Form1_Paint(Nothing, Nothing)
End Sub
End Class
Any ideas on how to fix the code to make it able to rotate the verts and not have them dissapear? I have included the sample code exe here ...
Brinkster does not allow file leeching so here first
http://www10.brinkster.com/cbx/indexb.htm" target="_blank">http://www10.brinkster.com/cbx/indexb.htm
... then type this address into the address bar.
http://www10.brinkster.com/cbx/unproject.zip" target="_blank">http://www10.brinkster.com/cbx/unproject.zip
you should be able to download the file then.