MAHDIACTION
چهارشنبه 05 تیر 1392, 11:47 صبح
با سلام خدمت تمام دوستان عزيز
مسله اي برخوردم كه اميدوارم تو اين تايپيك حل بشه ،
شرح مسله
مي خوام كاربر روي فرم كليك كرد در فضاي سه بعدي دقيقا در همان محل يك نقطه رسم كنم.
چگونه مي توانم اين مشخصات را به نحو احسن تبديل كنم؟
چرا تايپيك زدم؟
خيلي جاها دنبال راه حل گشتم متاسفانه جواب قطعي پيدا نكردم، راه حل هاي مختلف زياده ولي سردر گم شدم ، اميدوارم راهنمايي كيند.
نمونه كد
كد پايين يك خط در فضاي 3 بعدي روي فرم رسم مي كنه حالا مي خواستم كنار اين خط با موس يه نقطه رسم كنم.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Direct3D = Microsoft.DirectX.Direct3D
Namespace DeviceTutorial
Public Class CreateDevice
Inherits Form
' Our global variables for this project
Private device As Device = Nothing
' Our rendering device
' Public ourArcball As GraphicsArcBall = Nothing
Public Sub New()
' Set the initial size of our form
Me.ClientSize = New System.Drawing.Size(400, 300)
' And it's caption
Me.Text = "D3D Tutorial 01: CreateDevice"
End Sub 'New
Public Function InitializeGraphics() As Boolean
Try
' Now let's setup our D3D stuff
Dim presentParams As New PresentParameters()
presentParams.Windowed = True
presentParams.SwapEffect = SwapEffect.Discard
device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams)
' ourArcball = New GraphicsArcBall(Me)
Return True
Catch e As DirectXException
Return False
End Try
End Function 'InitializeGraphics
Dim view As Matrix
Dim Projection As Matrix
Private Sub Render()
If device Is Nothing Then
Return
End If
'Clear the backbuffer to a blue color
If device Is Nothing Then
Return
End If
device.SetTransform(TransformType.World, Matrix.Identity)
view = Matrix.LookAtLH(New Vector3(0, 0, -25), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
Projection = Matrix.PerspectiveFovRH(Math.PI / 2, Me.Width / Me.Height, 0, 1000)
device.SetTransform(TransformType.Projection, Projection)
device.Clear(ClearFlags.Target, Color.White, 0, 0)
device.BeginScene()
Dim d3dLine As New Line(device)
Dim vertices(4) As Vector3
vertices(0) = New Vector3(0, 0, 0) 'ourArcball.internalvectorDown
vertices(1) = New Vector3(0, 20, 0)
' vertices(5) = New Vector3(20, 0, 0)
d3dLine.Antialias = True
d3dLine.Begin()
Dim x As Integer = 1
Dim y As Integer = 1
Dim world As Matrix = Matrix.Identity
world.Translate(x, y, 0)
Dim wvp As Matrix = Matrix.Multiply(world, view)
wvp = Matrix.Multiply(wvp, Projection)
d3dLine.DrawTransform(vertices, wvp, Color.Black)
d3dLine.End()
device.EndScene()
device.Present()
' Rendering of scene objects can happen here
'End the scene
End Sub 'Render
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Me.Render() ' Render on painting
End Sub 'OnPaint
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Asc(e.KeyChar) = CInt(System.Windows.Forms.Keys.Escape) Then
Me.Close() ' Esc was pressed
End If
End Sub 'OnKeyPress
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
' Show and select the form
Me.Show()
Me.Select()
End Sub 'OnLoad
'<summary>
'The main entry point for the application.
'</summary>
Shared Sub Main()
Dim frm As New CreateDevice()
If Not frm.InitializeGraphics() Then ' Initialize Direct3D
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.")
Return
End If
frm.Show()
While frm.Created
frm.Render()
Application.DoEvents()
End While
End Sub 'Main
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'CreateDevice
'
Me.ClientSize = New System.Drawing.Size(685, 476)
Me.Name = "CreateDevice"
Me.ResumeLayout(False)
End Sub
Private Sub CreateDevice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Dim origin, posfar As Vector3
Private Sub CreateDevice_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
' If currentMouseState.LeftButton = ButtonState.Pressed Then
Dim nearScreenPoint As New Vector3(e.X, e.Y, 0)
Dim farScreenPoint As New Vector3(e.X, e.Y, 1)
Dim nearWorldPoint As Vector3 = Vector3.Unproject(nearScreenPoint, device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity)
Dim farWorldPoint As Vector3 = Vector3.Unproject(farScreenPoint, device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity)
Dim posfar As Vector3 = farWorldPoint - nearWorldPoint
'If direction.Z < 0 Then
' Dim zFactor As Single = -nearWorldPoint.Z / direction.Z
' Dim zeroWorldPoint As Vector3 = nearWorldPoint + direction * zFactor
' heightData(CInt(zeroWorldPoint.X), CInt(zeroWorldPoint.Y)) += 1
' SetUpTerrainVertices()
'End If
'' End If
End Sub
End Class 'CreateDevice
مسله اي برخوردم كه اميدوارم تو اين تايپيك حل بشه ،
شرح مسله
مي خوام كاربر روي فرم كليك كرد در فضاي سه بعدي دقيقا در همان محل يك نقطه رسم كنم.
چگونه مي توانم اين مشخصات را به نحو احسن تبديل كنم؟
چرا تايپيك زدم؟
خيلي جاها دنبال راه حل گشتم متاسفانه جواب قطعي پيدا نكردم، راه حل هاي مختلف زياده ولي سردر گم شدم ، اميدوارم راهنمايي كيند.
نمونه كد
كد پايين يك خط در فضاي 3 بعدي روي فرم رسم مي كنه حالا مي خواستم كنار اين خط با موس يه نقطه رسم كنم.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
Imports Direct3D = Microsoft.DirectX.Direct3D
Namespace DeviceTutorial
Public Class CreateDevice
Inherits Form
' Our global variables for this project
Private device As Device = Nothing
' Our rendering device
' Public ourArcball As GraphicsArcBall = Nothing
Public Sub New()
' Set the initial size of our form
Me.ClientSize = New System.Drawing.Size(400, 300)
' And it's caption
Me.Text = "D3D Tutorial 01: CreateDevice"
End Sub 'New
Public Function InitializeGraphics() As Boolean
Try
' Now let's setup our D3D stuff
Dim presentParams As New PresentParameters()
presentParams.Windowed = True
presentParams.SwapEffect = SwapEffect.Discard
device = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, presentParams)
' ourArcball = New GraphicsArcBall(Me)
Return True
Catch e As DirectXException
Return False
End Try
End Function 'InitializeGraphics
Dim view As Matrix
Dim Projection As Matrix
Private Sub Render()
If device Is Nothing Then
Return
End If
'Clear the backbuffer to a blue color
If device Is Nothing Then
Return
End If
device.SetTransform(TransformType.World, Matrix.Identity)
view = Matrix.LookAtLH(New Vector3(0, 0, -25), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
Projection = Matrix.PerspectiveFovRH(Math.PI / 2, Me.Width / Me.Height, 0, 1000)
device.SetTransform(TransformType.Projection, Projection)
device.Clear(ClearFlags.Target, Color.White, 0, 0)
device.BeginScene()
Dim d3dLine As New Line(device)
Dim vertices(4) As Vector3
vertices(0) = New Vector3(0, 0, 0) 'ourArcball.internalvectorDown
vertices(1) = New Vector3(0, 20, 0)
' vertices(5) = New Vector3(20, 0, 0)
d3dLine.Antialias = True
d3dLine.Begin()
Dim x As Integer = 1
Dim y As Integer = 1
Dim world As Matrix = Matrix.Identity
world.Translate(x, y, 0)
Dim wvp As Matrix = Matrix.Multiply(world, view)
wvp = Matrix.Multiply(wvp, Projection)
d3dLine.DrawTransform(vertices, wvp, Color.Black)
d3dLine.End()
device.EndScene()
device.Present()
' Rendering of scene objects can happen here
'End the scene
End Sub 'Render
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Me.Render() ' Render on painting
End Sub 'OnPaint
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If Asc(e.KeyChar) = CInt(System.Windows.Forms.Keys.Escape) Then
Me.Close() ' Esc was pressed
End If
End Sub 'OnKeyPress
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
' Show and select the form
Me.Show()
Me.Select()
End Sub 'OnLoad
'<summary>
'The main entry point for the application.
'</summary>
Shared Sub Main()
Dim frm As New CreateDevice()
If Not frm.InitializeGraphics() Then ' Initialize Direct3D
MessageBox.Show("Could not initialize Direct3D. This tutorial will exit.")
Return
End If
frm.Show()
While frm.Created
frm.Render()
Application.DoEvents()
End While
End Sub 'Main
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'CreateDevice
'
Me.ClientSize = New System.Drawing.Size(685, 476)
Me.Name = "CreateDevice"
Me.ResumeLayout(False)
End Sub
Private Sub CreateDevice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Dim origin, posfar As Vector3
Private Sub CreateDevice_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
' If currentMouseState.LeftButton = ButtonState.Pressed Then
Dim nearScreenPoint As New Vector3(e.X, e.Y, 0)
Dim farScreenPoint As New Vector3(e.X, e.Y, 1)
Dim nearWorldPoint As Vector3 = Vector3.Unproject(nearScreenPoint, device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity)
Dim farWorldPoint As Vector3 = Vector3.Unproject(farScreenPoint, device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity)
Dim posfar As Vector3 = farWorldPoint - nearWorldPoint
'If direction.Z < 0 Then
' Dim zFactor As Single = -nearWorldPoint.Z / direction.Z
' Dim zeroWorldPoint As Vector3 = nearWorldPoint + direction * zFactor
' heightData(CInt(zeroWorldPoint.X), CInt(zeroWorldPoint.Y)) += 1
' SetUpTerrainVertices()
'End If
'' End If
End Sub
End Class 'CreateDevice