home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / direct3d / scattergraph / scattergraph.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-10-02  |  32.0 KB  |  913 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form GraphForm 
  4.    Caption         =   "Data Analysis Scatter Graph"
  5.    ClientHeight    =   6420
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   7875
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   12
  12.       Charset         =   0
  13.       Weight          =   400
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "ScatterGraph.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   428
  21.    ScaleMode       =   3  'Pixel
  22.    ScaleWidth      =   525
  23.    StartUpPosition =   3  'Windows Default
  24.    Begin VB.CommandButton Command1 
  25.       Caption         =   "Command1"
  26.       BeginProperty Font 
  27.          Name            =   "MS Sans Serif"
  28.          Size            =   18
  29.          Charset         =   0
  30.          Weight          =   700
  31.          Underline       =   0   'False
  32.          Italic          =   0   'False
  33.          Strikethrough   =   0   'False
  34.       EndProperty
  35.       Height          =   435
  36.       Left            =   1920
  37.       TabIndex        =   0
  38.       Top             =   5820
  39.       Visible         =   0   'False
  40.       Width           =   495
  41.    End
  42.    Begin MSComDlg.CommonDialog CommonDialog1 
  43.       Left            =   1080
  44.       Top             =   5760
  45.       _ExtentX        =   847
  46.       _ExtentY        =   847
  47.       _Version        =   393216
  48.    End
  49.    Begin VB.Timer Timer1 
  50.       Enabled         =   0   'False
  51.       Interval        =   10
  52.       Left            =   240
  53.       Top             =   5760
  54.    End
  55.    Begin VB.Menu MENU_POPUP 
  56.       Caption         =   "POPUPMENU"
  57.       Visible         =   0   'False
  58.       Begin VB.Menu MENU_EXITMENU 
  59.          Caption         =   "Exit Menu!"
  60.       End
  61.       Begin VB.Menu MENU_LOAD 
  62.          Caption         =   "Load Data From File!"
  63.       End
  64.       Begin VB.Menu MENU_RESET 
  65.          Caption         =   "Reset Orientation!"
  66.       End
  67.       Begin VB.Menu MENU_CONNECT 
  68.          Caption         =   "Show connecting lines"
  69.          Checked         =   -1  'True
  70.       End
  71.       Begin VB.Menu MENU_LINES 
  72.          Caption         =   "Show height lines"
  73.          Checked         =   -1  'True
  74.       End
  75.       Begin VB.Menu MENU_FOOTLINES 
  76.          Caption         =   "Show foot lines"
  77.          Checked         =   -1  'True
  78.       End
  79.       Begin VB.Menu MENU_BASE 
  80.          Caption         =   "Show base plane"
  81.          Checked         =   -1  'True
  82.       End
  83.       Begin VB.Menu MENU_ROTATE 
  84.          Caption         =   "Auto Rotate"
  85.          Checked         =   -1  'True
  86.       End
  87.    End
  88. Attribute VB_Name = "GraphForm"
  89. Attribute VB_GlobalNameSpace = False
  90. Attribute VB_Creatable = False
  91. Attribute VB_PredeclaredId = True
  92. Attribute VB_Exposed = False
  93. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  94. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  95. '  File:       ScatterGraph.frm
  96. '  Content:    Implementation of a plot graph in 3 dimensions
  97. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  98. Option Explicit
  99. Dim m_maxX As Double
  100. Dim m_minX As Double
  101. Dim m_maxY As Double
  102. Dim m_minY As Double
  103. Dim m_maxZ As Double
  104. Dim m_minZ As Double
  105. Dim m_maxsize As Double
  106. Dim m_minSize As Double
  107. Dim m_extX As Double
  108. Dim m_extY As Double
  109. Dim m_extZ As Double
  110. Dim m_extSize As Double
  111. Dim m_scalex As Single
  112. Dim m_scaley As Single
  113. Dim m_scalez As Single
  114. Dim m_scalesize As Single
  115. Dim m_xHeader As String
  116. Dim m_yHeader As String
  117. Dim m_zHeader As String
  118. Dim m_sizeHeader As String
  119. Dim m_binit As Boolean
  120. Dim m_bGraphInit As Boolean
  121. Dim m_bMinimized As Boolean
  122. Dim m_graphroot As CD3DFrame
  123. Dim m_quad1 As CD3DFrame
  124. Dim m_quad2 As CD3DFrame
  125. Dim m_XZPlaneFrame As CD3DFrame
  126. Dim m_bRot As Boolean
  127. Dim m_bHeightLines As Boolean
  128. Dim m_bConnectlines As Boolean
  129. Dim m_bShowBase As Boolean
  130. Dim m_bFootLines As Boolean
  131. Dim m_drawtext As String
  132. Dim m_drawtextpos As RECT
  133. Dim m_drawtextEnable As Boolean
  134. Dim m_formatX As String
  135. Dim m_formatY As String
  136. Dim m_formatZ As String
  137. Dim m_formatSize As String
  138. Dim m_data As Collection
  139. Dim m_hwnd As Long
  140. Dim m_vbfont As IFont
  141. Dim m_vbfont2 As IFont
  142. Dim m_font2height  As Long
  143. Dim m_lastX As Single
  144. Dim m_lasty As Single
  145. Dim m_bMouseDown As Boolean
  146. Dim m_Tex As Direct3DTexture8
  147. Dim m_LabelX As CD3DFrame
  148. Dim m_LabelY As CD3DFrame
  149. Dim m_LabelZ As CD3DFrame
  150. Dim m_meshobj As D3DXMesh
  151. Dim m_meshplane As D3DXMesh
  152. Dim m_font As D3DXFont
  153. Dim m_font2 As D3DXFont
  154. 'Camera variables
  155. Dim m_fElapsedTime As Single
  156. Dim m_vVelocity  As D3DVECTOR
  157. Dim m_fYawVelocity As Single
  158. Dim m_fPitchVelocity As Single
  159. Dim m_fYaw As Single
  160. Dim m_fPitch As Single
  161. Dim m_vPosition As D3DVECTOR
  162. Dim m_bKey(256) As Boolean
  163. Dim m_matView As D3DMATRIX
  164. Dim m_matOrientation As D3DMATRIX
  165. Dim m_MediaDir As String
  166. Const kdx = 256&
  167. Const kdy = 256&
  168. Const D3DFVF_VERTEX = D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1
  169. Friend Sub Init(hwnd As Long, font As IFontDisp, font2 As IFontDisp)
  170.     Dim i As Long
  171.     'Save hwnd
  172.     m_hwnd = hwnd
  173.     'convert IFontDisp to Ifont
  174.     Set m_vbfont = font
  175.     Set m_vbfont2 = font2
  176.     'initialized d3d
  177.     m_binit = D3DUtil_Init(hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  178.         
  179.     'exit if initialization failed
  180.     If m_binit = False Then End
  181.     DeleteDeviceObjects
  182.     InitDeviceObjects
  183.     BuildDefaultDataList
  184.     ComputeDataExtents
  185.     BuildGraph
  186.     RestoreDeviceObjects
  187.     DoEvents
  188.     m_bRot = True
  189.     m_xHeader = "X Axis"
  190.     m_yHeader = "Y Axis"
  191.     m_zHeader = "Z Axis"
  192.     m_sizeHeader = "s"
  193.     m_vPosition = vec3(0, 0, -20)
  194.     'Initialze camera matrices
  195.     g_dev.GetTransform D3DTS_VIEW, m_matView
  196.     D3DXMatrixTranslation m_matOrientation, 0, 0, 0
  197.     Timer1.Enabled = True
  198.     Call DXUtil_Timer(TIMER_start)
  199. End Sub
  200. Private Sub BuildDefaultDataList()
  201.     Set m_data = New Collection
  202.     Dim i As Single
  203.     For i = 1 To 40 Step 2
  204.         AddEntry "pt" + CStr(i), 1 / CSng(i), (i * i) - 25 * i, CSng(i), (0.7 + i / 16), D3DCOLORVALUEtoLONG(ColorValue4(1, 1, 0.5 + i / 20, i / 80)), ""
  205.     Next
  206.     m_formatX = "0.000"
  207.     m_formatY = "0.000"
  208.     m_formatZ = "0.000"
  209.     m_formatSize = "0.000"
  210.     m_bConnectlines = True
  211.     m_bHeightLines = True
  212.     m_bShowBase = True
  213.     m_bFootLines = True
  214.     m_xHeader = "X Axis"
  215.     m_yHeader = "Y Axis"
  216.     m_zHeader = "Z Axis"
  217.     m_sizeHeader = "s"
  218. End Sub
  219. Sub RestoreDeviceObjects()
  220.     g_lWindowWidth = Me.ScaleWidth
  221.     g_lWindowHeight = Me.ScaleHeight
  222.     D3DUtil_SetupDefaultScene
  223.     D3DUtil_SetupCamera vec3(0, 5, -20), vec3(0, 0, 0), vec3(0, 1, 0)
  224.     'allow the application to show both sides of all surfaces
  225.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  226.     'turn on min filtering since our text is often smaller
  227.     'than original size
  228.     g_dev.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  229.      Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  230.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  231.         
  232. End Sub
  233. Private Sub ComputeDataExtents()
  234.     Dim mind As Single
  235.     Dim maxd As Single
  236.     Dim entry As DataEntry
  237.     mind = -9E+20
  238.     maxd = 9E+20
  239.     m_maxX = mind:    m_maxY = mind:    m_maxZ = mind:    m_maxsize = mind
  240.     m_minX = maxd:    m_minY = maxd:    m_minZ = maxd:    m_minSize = maxd
  241.     'Dim entry As DataEntry
  242.     For Each entry In m_data
  243.                         
  244.         If entry.datax > m_maxX Then m_maxX = entry.datax
  245.         If entry.datay > m_maxY Then m_maxY = entry.datay
  246.         If entry.dataz > m_maxZ Then m_maxZ = entry.dataz
  247.         If entry.dataSize > m_maxsize Then m_maxsize = entry.dataSize
  248.         
  249.         If entry.datax < m_minX Then m_minX = entry.datax
  250.         If entry.datay < m_minY Then m_minY = entry.datay
  251.         If entry.dataz < m_minZ Then m_minZ = entry.dataz
  252.         If entry.dataSize < m_minSize Then m_minSize = entry.dataSize
  253.                 
  254.     Next
  255.     m_extX = m_maxX - m_minX
  256.     m_extY = m_maxY - m_minY
  257.     m_extZ = m_maxZ - m_minZ
  258.     m_extSize = m_maxsize - m_minSize
  259.     Dim kScale As Single
  260.     kScale = 5
  261.     m_scalex = 1
  262.     m_scaley = 1
  263.     m_scalez = 1
  264.     m_scalesize = 1
  265.     If m_maxX > Abs(m_minX) Then
  266.         If m_maxX <> 0 Then m_scalex = kScale / m_maxX
  267.     Else
  268.         If m_minX <> 0 Then m_scalex = kScale / Abs(m_minX)
  269.     End If
  270.     If m_maxY > Abs(m_minY) Then
  271.         If m_maxY <> 0 Then m_scaley = kScale / m_maxY
  272.     Else
  273.         If m_minY <> 0 Then m_scaley = kScale / Abs(m_minY)
  274.     End If
  275.     If m_maxZ > Abs(m_minZ) Then
  276.         If m_maxZ <> 0 Then m_scalez = kScale / m_maxZ
  277.     Else
  278.         If m_minZ <> 0 Then m_scalez = kScale / Abs(m_minZ)
  279.     End If
  280.     If m_maxsize = 0 Then m_maxsize = 1
  281.     m_scalesize = 1 * (kScale) / m_maxsize
  282.         
  283.     'scale graph data to fit
  284.     For Each entry In m_data
  285.                      
  286.         entry.x = entry.datax * m_scalex
  287.         entry.y = entry.datay * m_scaley
  288.         entry.z = entry.dataz * m_scalez
  289.         entry.size = entry.dataSize * m_scalesize
  290.     Next
  291. End Sub
  292. Public Sub AddEntry(sName As String, x As Double, y As Double, z As Double, size As Double, color As Long, data As Variant)
  293.     On Local Error GoTo errOut
  294.     Dim entry As New DataEntry
  295.     entry.dataname = sName
  296.     entry.datax = x
  297.     entry.datay = y
  298.     entry.dataz = z
  299.     entry.dataSize = size
  300.     entry.color = color
  301.     entry.data = data
  302.     m_data.Add entry
  303.     Exit Sub
  304. errOut:
  305.     MsgBox "unable to add entry"
  306. End Sub
  307. Public Sub DrawGraph()
  308.     Dim entry As DataEntry
  309.     Dim hr As Long
  310.     If m_binit = False Then Exit Sub
  311.     'See what state the device is in.
  312.     hr = g_dev.TestCooperativeLevel
  313.     If hr = D3DERR_DEVICENOTRESET Then
  314.         g_dev.Reset g_d3dpp
  315.         RestoreDeviceObjects
  316.     End If
  317.     m_graphroot.UpdateFrames
  318.              
  319.     'Clear the previous render with the backgroud color
  320.     'We clear to grey but notice that we are using a hexidecimal
  321.     'number to represent Alpha Red Green and blue
  322.     D3DUtil_ClearAll &HFF707070
  323.     'set the ambient lighting level
  324.     g_dev.SetRenderState D3DRS_AMBIENT, &HFFC0C0C0
  325.     g_dev.BeginScene
  326.         
  327.         
  328.     'only render objects underneath the xzplane
  329.     m_quad1.Enabled = False
  330.     m_quad2.Enabled = True
  331.     m_XZPlaneFrame.Enabled = False
  332.     m_graphroot.Render g_dev
  333.     'render the objects in front of xz plane
  334.     m_quad1.Enabled = True
  335.     m_quad2.Enabled = False
  336.     m_XZPlaneFrame.Enabled = False
  337.     m_graphroot.Render g_dev
  338.         
  339.         
  340.         
  341.     DrawLines 0
  342.     DrawAxisNameSquare 0    'x axis
  343.     DrawAxisNameSquare 2    'z axis
  344.         
  345.     'draw pop up text
  346.     If m_drawtextEnable Then
  347.         g_d3dx.DrawText m_font, &HFF00FFFF, m_drawtext, m_drawtextpos, 0
  348.     End If
  349.     Dim rc As RECT
  350.     rc.Top = 20:    rc.Left = 10
  351.     g_d3dx.DrawText m_font, &HFF00FFFF, "Height = " + m_yHeader, rc, 0
  352.     rc.Top = 40:    rc.Left = 10
  353.     g_d3dx.DrawText m_font, &HFF00FFFF, "Size = " + m_sizeHeader, rc, 0
  354.     'render the xzplane with transparency
  355.     If m_bShowBase Then
  356.         m_quad1.Enabled = False
  357.         m_quad2.Enabled = False
  358.         m_XZPlaneFrame.Enabled = True
  359.         m_graphroot.Render g_dev
  360.     End If
  361.     g_dev.EndScene
  362.     D3DUtil_PresentAll m_hwnd
  363. End Sub
  364. Public Sub BuildGraph()
  365.     Dim entry As DataEntry
  366.     Dim material As D3DMATERIAL8
  367.     Dim newFrame As CD3DFrame
  368.     Dim i As Long
  369.     Dim d3ddm As D3DDISPLAYMODE
  370.         
  371.     If m_binit = False Then Exit Sub
  372.     'Create rotatable root object
  373.     Set m_graphroot = D3DUtil_CreateFrame(Nothing)
  374.                 
  375.     'Create XZ plane for reference
  376.     material.diffuse = LONGtoD3DCOLORVALUE(&H6FC0C0C0)
  377.     material.Ambient = material.diffuse
  378.     Set m_XZPlaneFrame = D3DUtil_CreateFrame(m_graphroot)
  379.     m_XZPlaneFrame.AddD3DXMesh(m_meshplane).SetMaterialOverride material
  380.     m_XZPlaneFrame.SetOrientation D3DUtil_RotationAxis(1, 0, 0, 90)
  381.     Set m_quad1 = D3DUtil_CreateFrame(m_graphroot)
  382.     Set m_quad2 = D3DUtil_CreateFrame(m_graphroot)
  383.     Set m_LabelX = D3DUtil_CreateFrame(m_graphroot)
  384.     m_LabelX.SetPosition vec3(0, 0, -6)
  385.     Set m_LabelY = D3DUtil_CreateFrame(Nothing)
  386.     m_LabelY.SetPosition vec3(-8, 8, 0)
  387.     Set m_LabelZ = D3DUtil_CreateFrame(m_graphroot)
  388.     m_LabelZ.SetPosition vec3(6, 0, 0)
  389.     m_LabelZ.SetOrientation D3DUtil_RotationAxis(0, 1, 0, -90)
  390.     Dim quadframe As CD3DFrame
  391.     For Each entry In m_data
  392.         If entry.y >= 0 Then Set quadframe = m_quad1
  393.         If entry.y < 0 Then Set quadframe = m_quad2
  394.                 
  395.         'Set material of objects
  396.         material.diffuse = LONGtoD3DCOLORVALUE(entry.color)
  397.         material.Ambient = material.diffuse
  398.                 
  399.         'Create individual objects
  400.         Set newFrame = D3DUtil_CreateFrame(quadframe)
  401.         newFrame.SetScale entry.size
  402.         newFrame.SetPosition vec3(entry.x, entry.y, entry.z)
  403.         newFrame.AddD3DXMesh(m_meshobj).SetMaterialOverride material
  404.         i = i + 1
  405.         newFrame.ObjectName = Str(i)
  406.    Next
  407.    'Take care of labels
  408.     Dim surf As Direct3DSurface8
  409.     Dim rc As RECT
  410.     Dim rts As D3DXRenderToSurface
  411.     Dim rtsviewport As D3DVIEWPORT8
  412.     Set surf = m_Tex.GetSurfaceLevel(0)
  413.     rtsviewport.height = kdx
  414.     rtsviewport.width = kdy
  415.     rtsviewport.MaxZ = 1
  416.     Call g_dev.GetDisplayMode(d3ddm)
  417.     Set rts = g_d3dx.CreateRenderToSurface(g_dev, kdx, kdy, d3ddm.format, 1, D3DFMT_D16)
  418.     rts.BeginScene surf, rtsviewport
  419.     g_dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFFC0C0C0, 1, 0
  420.         
  421.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, DT_CALCRECT
  422.     m_font2height = rc.bottom
  423.     rc.Top = m_font2height * 0: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  424.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, DT_CALCRECT
  425.     g_d3dx.DrawText m_font2, &HFF000000, m_xHeader, rc, 0
  426.     rc.Top = m_font2height * 1: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  427.     g_d3dx.DrawText m_font2, &HFF000000, m_yHeader, rc, DT_CALCRECT
  428.     g_d3dx.DrawText m_font2, &HFF000000, m_yHeader, rc, 0
  429.     rc.Top = m_font2height * 2: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  430.     g_d3dx.DrawText m_font2, &HFF000000, m_zHeader, rc, DT_CALCRECT
  431.     g_d3dx.DrawText m_font2, &HFF000000, m_zHeader, rc, 0
  432.     rts.EndScene
  433.    m_bGraphInit = True
  434. End Sub
  435. Public Sub InitDeviceObjects()
  436.     Dim d3ddm As D3DDISPLAYMODE
  437.     If m_binit = False Then Exit Sub
  438.     Dim rc As RECT
  439.     Set m_meshobj = g_d3dx.CreateSphere(g_dev, 0.1, 16, 16, Nothing)
  440.     Set m_meshplane = g_d3dx.CreateBox(g_dev, 10, 10, 0.1, Nothing)
  441.     Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  442.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  443.     Call g_dev.GetDisplayMode(d3ddm)
  444.     'Create Textures
  445.     Set m_Tex = g_d3dx.CreateTexture(g_dev, kdx, kdx, 0, 0, d3ddm.format, D3DPOOL_MANAGED)
  446.        
  447. End Sub
  448. Private Sub DrawLines(quad As Long)
  449.     Dim entry As DataEntry
  450.     Dim vLast As D3DVECTOR, vNext As D3DVECTOR
  451.     Dim vGround As D3DVECTOR
  452.     Dim vGround1 As D3DVECTOR
  453.     Dim vGround2 As D3DVECTOR
  454.     Dim i As Long
  455.     'Link lines
  456.     g_dev.SetTransform D3DTS_WORLD, m_graphroot.GetMatrix
  457.     Set entry = m_data.item(1)
  458.     vLast = vec3(entry.x, entry.y, entry.z)
  459.     vGround = vLast
  460.     vGround.y = 0
  461.     Call DrawLine(vGround, vLast, &HFFFF0000)
  462.     For i = 2 To m_data.count
  463.         Set entry = m_data.item(i)
  464.         vNext = vec3(entry.x, entry.y, entry.z)
  465.         
  466.         If m_bConnectlines Then
  467.             Call DrawLine(vLast, vNext, &HFFFF00FF)
  468.         End If
  469.         
  470.         vGround = vNext
  471.         vGround.y = 0
  472.         vGround1 = vGround
  473.         vGround1.y = 0.1
  474.         vGround2 = vLast
  475.         vGround2.y = 0.1
  476.         
  477.         If m_bHeightLines Then
  478.             Call DrawLine(vGround, vNext, &HFFFF0000)
  479.         End If
  480.         
  481.         If m_bFootLines Then
  482.             Call DrawLine(vGround1, vGround2, &HFF10FF30)
  483.         End If
  484.         
  485.         vLast = vNext
  486.     Next
  487.     DrawLine vec3(-5, 0.1, 0), vec3(5, 0.1, 0), &HFF0&
  488.     DrawLine vec3(0, 0.1, -5), vec3(0, 0.1, 5), &HFF0&
  489. End Sub
  490. Private Sub DrawLine(v1 As D3DVECTOR, v2 As D3DVECTOR, color As Long)
  491.     Dim mat As D3DMATERIAL8
  492.     mat.diffuse = LONGtoD3DCOLORVALUE(color)
  493.     mat.Ambient = mat.diffuse
  494.     g_dev.SetMaterial mat
  495.     Dim dataOut(2) As D3DVERTEX
  496.     LSet dataOut(0) = v1
  497.     LSet dataOut(1) = v2
  498.     g_dev.SetVertexShader D3DFVF_VERTEX
  499.     g_dev.DrawPrimitiveUP D3DPT_LINELIST, 1, dataOut(0), Len(dataOut(0))
  500. End Sub
  501. Public Sub MouseOver(Button As Integer, Shift As Integer, x As Single, y As Single)
  502.     If m_binit = False Then Exit Sub
  503.     Dim pick As New CD3DPick
  504.     Dim frame As CD3DFrame
  505.     Dim nid As Long
  506.     Dim entry As DataEntry
  507.     'remove the XZ plane from consideration for pick
  508.     m_XZPlaneFrame.Enabled = False
  509.     m_quad1.Enabled = True
  510.     m_quad2.Enabled = True
  511.     pick.ViewportPick m_graphroot, x, y
  512.     nid = pick.FindNearest()
  513.     If nid < 0 Then
  514.         m_drawtextEnable = False
  515.         Exit Sub
  516.     End If
  517.         
  518.     Set frame = pick.GetFrame(nid)
  519.     'have matrices pre computed for scene graph
  520.     m_graphroot.UpdateFrames
  521.     'due some math to get position of item in screen space
  522.     Dim viewport As D3DVIEWPORT8
  523.     Dim projmatrix As D3DMATRIX
  524.     Dim viewmatrix As D3DMATRIX
  525.     Dim vOut As D3DVECTOR
  526.     g_dev.GetViewport viewport
  527.     g_dev.GetTransform D3DTS_PROJECTION, projmatrix
  528.     g_dev.GetTransform D3DTS_VIEW, viewmatrix
  529.     D3DXVec3Project vOut, vec3(0, 0, 0), viewport, projmatrix, viewmatrix, frame.GetUpdatedMatrix
  530.             
  531.     Debug.Print vOut.x, vOut.y, frame.ObjectName
  532.     Dim destRect As RECT
  533.     m_drawtextpos.Left = x - 20
  534.     m_drawtextpos.Top = y - 70
  535.     If m_drawtextpos.Left < 0 Then m_drawtextpos.Left = 1
  536.     If m_drawtextpos.Top < 0 Then m_drawtextpos.Top = 1
  537.     Set entry = m_data.item(val(frame.ObjectName))
  538.     With entry
  539.         m_drawtext = .dataname + Chr(13)
  540.         m_drawtext = m_drawtext + " " + m_xHeader + "=" + format$(.datax, m_formatX) + Chr(13)
  541.         m_drawtext = m_drawtext + " " + m_yHeader + "=" + format$(.datay, m_formatY) + Chr(13)
  542.         m_drawtext = m_drawtext + " " + m_zHeader + "=" + format$(.dataz, m_formatZ) + Chr(13)
  543.         m_drawtext = m_drawtext + " " + m_sizeHeader + "=" + format$(.dataSize, m_formatSize)
  544.     End With
  545.     m_drawtextEnable = True
  546. End Sub
  547. Sub FrameMove()
  548.     'for camera movement
  549.     m_fElapsedTime = DXUtil_Timer(TIMER_GETELLAPSEDTIME) * 1.3
  550.     If m_fElapsedTime < 0 Then Exit Sub
  551.         
  552.         
  553.     If m_bRot And m_bMouseDown = False Then
  554.         m_graphroot.AddRotation COMBINE_BEFORE, 0, 1, 0, (g_pi / 40) * m_fElapsedTime
  555.     End If
  556.         
  557.         
  558.     ' Slow things down for the REF device
  559.     If (g_devType = D3DDEVTYPE_REF) Then m_fElapsedTime = 0.05
  560.     Dim fSpeed As Single
  561.     Dim fAngularSpeed
  562.     fSpeed = 5 * m_fElapsedTime
  563.     fAngularSpeed = 1 * m_fElapsedTime
  564.     ' Slowdown the camera movement
  565.     D3DXVec3Scale m_vVelocity, m_vVelocity, 0.9
  566.     m_fYawVelocity = m_fYawVelocity * 0.9
  567.     m_fPitchVelocity = m_fPitchVelocity * 0.9
  568.     ' Process keyboard input
  569.     If (m_bKey(vbKeyRight)) Then m_vVelocity.x = m_vVelocity.x + fSpeed        '  Slide Right
  570.     If (m_bKey(vbKeyLeft)) Then m_vVelocity.x = m_vVelocity.x - fSpeed         '  Slide Left
  571.     If (m_bKey(vbKeyUp)) Then m_vVelocity.y = m_vVelocity.y + fSpeed           '  Move up
  572.     If (m_bKey(vbKeyDown)) Then m_vVelocity.y = m_vVelocity.y - fSpeed         '  Move down
  573.     If (m_bKey(vbKeyW)) Then m_vVelocity.z = m_vVelocity.z + fSpeed            '  Move Forward
  574.     If (m_bKey(vbKeyS)) Then m_vVelocity.z = m_vVelocity.z - fSpeed            '  Move Backward
  575.     If (m_bKey(vbKeyE)) Then m_fYawVelocity = m_fYawVelocity + fSpeed          '  Yaw right
  576.     If (m_bKey(vbKeyQ)) Then m_fYawVelocity = m_fYawVelocity - fSpeed          '  Yaw left
  577.     If (m_bKey(vbKeyZ)) Then m_fPitchVelocity = m_fPitchVelocity + fSpeed      '  turn down
  578.     If (m_bKey(vbKeyA)) Then m_fPitchVelocity = m_fPitchVelocity - fSpeed      '  turn up
  579.     ' Update the position vector
  580.     Dim vT As D3DVECTOR, vTemp As D3DVECTOR
  581.     D3DXVec3Scale vTemp, m_vVelocity, fSpeed
  582.     D3DXVec3Add vT, vT, vTemp
  583.     D3DXVec3TransformNormal vT, vT, m_matOrientation
  584.     D3DXVec3Add m_vPosition, m_vPosition, vT
  585.     If (m_vPosition.y < 1) Then m_vPosition.y = 1
  586.     ' Update the yaw-pitch-rotation vector
  587.     m_fYaw = m_fYaw + fAngularSpeed * m_fYawVelocity
  588.     m_fPitch = m_fPitch + fAngularSpeed * m_fPitchVelocity
  589.     If (m_fPitch < 0) Then m_fPitch = 0
  590.     If (m_fPitch > g_pi / 2) Then m_fPitch = g_pi / 2
  591.     Dim qR As D3DQUATERNION, det As Single
  592.     D3DXQuaternionRotationYawPitchRoll qR, m_fYaw, m_fPitch, 0
  593.     D3DXMatrixAffineTransformation m_matOrientation, 1.25, vec3(0, 0, 0), qR, m_vPosition
  594.     D3DXMatrixInverse m_matView, det, m_matOrientation
  595.         'set new view matrix
  596.     g_dev.SetTransform D3DTS_VIEW, m_matView
  597. End Sub
  598. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  599.     m_bKey(KeyCode) = True
  600. End Sub
  601. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  602.     m_bKey(KeyCode) = False
  603. End Sub
  604. Private Sub Form_Load()
  605.     'Show the form
  606.     Me.Show
  607.     DoEvents
  608.         
  609.     m_MediaDir = FindMediaDir("ScatterData.csv")
  610.     D3DUtil.D3DUtil_SetMediaPath m_MediaDir
  611.     'initialize the graph
  612.     Init Me.hwnd, Me.font, Command1.font
  613. End Sub
  614. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  615.     If Button = 2 Then
  616.         Me.PopupMenu MENU_POPUP
  617.     Else
  618.         '- save our current position
  619.         m_bMouseDown = True
  620.         m_lastX = x
  621.         m_lasty = y
  622.         
  623.     End If
  624. End Sub
  625. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  626.         
  627.     If m_binit = False Then Exit Sub
  628.     If Button = 2 Then Exit Sub
  629.     If m_bMouseDown = False Then
  630.         Call MouseOver(Button, Shift, x, y)
  631.     Else
  632.         '- Rotate the object
  633.         RotateTrackBall CInt(x), CInt(y)
  634.     End If
  635.     FrameMove
  636.     DrawGraph
  637. End Sub
  638. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  639.     m_bMouseDown = False
  640. End Sub
  641. '-----------------------------------------------------------------------------
  642. ' Name: Form_Resize()
  643. ' Desc: hadle resizing of the D3D backbuffer
  644. '-----------------------------------------------------------------------------
  645. Private Sub Form_Resize()
  646.     Timer1.Enabled = False
  647.     ' If D3D is not initialized then exit
  648.     If Not m_binit Then Exit Sub
  649.     ' If we are in a minimized state stop the timer and exit
  650.     If Me.WindowState = vbMinimized Then
  651.         DXUtil_Timer TIMER_STOP
  652.         m_bMinimized = True
  653.         Exit Sub
  654.         
  655.     ' If we just went from a minimized state to maximized
  656.     ' restart the timer
  657.     Else
  658.         If m_bMinimized = True Then
  659.             DXUtil_Timer TIMER_start
  660.             m_bMinimized = False
  661.         End If
  662.     End If
  663.     ' Dont let the window get too small
  664.     If Me.ScaleWidth < 10 Then
  665.         Me.width = Screen.TwipsPerPixelX * 10
  666.         Exit Sub
  667.     End If
  668.     If Me.ScaleHeight < 10 Then
  669.         Me.height = Screen.TwipsPerPixelY * 10
  670.         Exit Sub
  671.     End If
  672.      
  673.     DeleteDeviceObjects
  674.     'reset and resize our D3D backbuffer to the size of the window
  675.     D3DUtil_ResizeWindowed Me.hwnd
  676.     'All state get losts after a reset so we need to reinitialze it here
  677.     RestoreDeviceObjects
  678.     Timer1.Enabled = True
  679. End Sub
  680. '- Rotate Track ball
  681. '  given a point on the screen the mouse was moved to
  682. '  simulate a track ball
  683. Private Sub RotateTrackBall(x As Integer, y As Integer)
  684.     Dim delta_x As Single, delta_y As Single
  685.     Dim delta_r As Single, radius As Single, denom As Single, angle As Single
  686.     ' rotation axis in camcoords, worldcoords, sframecoords
  687.     Dim axisC As D3DVECTOR
  688.     Dim wc As D3DVECTOR
  689.     Dim axisS As D3DVECTOR
  690.     Dim base As D3DVECTOR
  691.     Dim origin As D3DVECTOR
  692.     delta_x = x - m_lastX
  693.     delta_y = y - m_lasty
  694.     m_lastX = x
  695.     m_lasty = y
  696.             
  697.      delta_r = Sqr(delta_x * delta_x + delta_y * delta_y)
  698.      radius = 50
  699.      denom = Sqr(radius * radius + delta_r * delta_r)
  700.     If (delta_r = 0 Or denom = 0) Then Exit Sub
  701.     angle = (delta_r / denom)
  702.     axisC.x = (-delta_y / delta_r)
  703.     axisC.y = (-delta_x / delta_r)
  704.     axisC.z = 0
  705.     'transform camera space vector to world space
  706.     'm_largewindow.m_cameraFrame.Transform wc, axisC
  707.     g_dev.GetTransform D3DTS_VIEW, g_viewMatrix
  708.     D3DXVec3TransformCoord wc, axisC, g_viewMatrix
  709.     'transform world space vector into Model space
  710.     m_graphroot.UpdateFrames
  711.     axisS = m_graphroot.InverseTransformCoord(wc)
  712.         
  713.     'transform origen camera space to world coordinates
  714.     'm_largewindow.m_cameraFrame.Transform  wc, origin
  715.     D3DXVec3TransformCoord wc, origin, g_viewMatrix
  716.     'transfer cam space origen to model space
  717.     base = m_graphroot.InverseTransformCoord(wc)
  718.     axisS.x = axisS.x - base.x
  719.     axisS.y = axisS.y - base.y
  720.     axisS.z = axisS.z - base.z
  721.     m_graphroot.AddRotation COMBINE_BEFORE, axisS.x, axisS.y, axisS.z, angle
  722. End Sub
  723. Private Sub Form_Paint()
  724.     If Not m_binit Then Exit Sub
  725.     If Not m_bGraphInit Then Exit Sub
  726.     DrawGraph
  727. End Sub
  728. Private Sub Form_Unload(Cancel As Integer)
  729.     End
  730. End Sub
  731. Private Sub MENU_BASE_Click()
  732.     m_bShowBase = Not m_bShowBase
  733.     MENU_BASE.Checked = m_bShowBase
  734. End Sub
  735. Private Sub MENU_CONNECT_Click()
  736.     m_bConnectlines = Not m_bConnectlines
  737.     MENU_CONNECT.Checked = m_bConnectlines
  738. End Sub
  739. Private Sub MENU_FOOTLINES_Click()
  740.     m_bFootLines = Not m_bFootLines
  741.     MENU_FOOTLINES.Checked = m_bFootLines
  742. End Sub
  743. Private Sub MENU_LINES_Click()
  744.     m_bHeightLines = Not m_bHeightLines
  745.     MENU_LINES.Checked = m_bHeightLines
  746. End Sub
  747. Private Sub MENU_LOAD_Click()
  748.     Dim sFile As String
  749.     CommonDialog1.FileName = ""
  750.     CommonDialog1.DefaultExt = "csv"
  751.     CommonDialog1.filter = "csv|*.csv"
  752.     CommonDialog1.InitDir = m_MediaDir
  753.     On Local Error Resume Next
  754.     CommonDialog1.ShowOpen
  755.     sFile = CommonDialog1.FileName
  756.     If sFile = "" Then Exit Sub
  757.     LoadFile sFile
  758.     Set m_graphroot = Nothing
  759.     Set m_quad1 = Nothing
  760.     Set m_quad2 = Nothing
  761.     Set m_XZPlaneFrame = Nothing
  762.     ComputeDataExtents
  763.     BuildGraph
  764.     RestoreDeviceObjects
  765. End Sub
  766. Private Sub MENU_RESET_Click()
  767.     m_graphroot.SetMatrix g_identityMatrix
  768.     m_vPosition = vec3(0, 0, -20)
  769.     m_fYaw = 0
  770.     m_fPitch = 0
  771.     Call D3DXMatrixTranslation(m_matOrientation, 0, 0, 0)
  772. End Sub
  773. Private Sub MENU_ROTATE_Click()
  774.     m_bRot = Not m_bRot
  775.     MENU_ROTATE.Checked = m_bRot
  776. End Sub
  777. Private Sub Timer1_Timer()
  778.     If Not m_binit Then Exit Sub
  779.     FrameMove
  780.     DrawGraph
  781. End Sub
  782. Sub LoadFile(sFile As String)
  783.     If Dir$(sFile) = "" Then
  784.         MsgBox "Unable to find " + sFile
  785.         Exit Sub
  786.     End If
  787.     Dim fl As Long
  788.     Dim strIn As String
  789.     Dim strTrim As String
  790.     Dim strFirstChar As String
  791.     Dim splitArray
  792.     Dim cols As Long
  793.     Dim bFoundData As Boolean
  794.     Dim sName As String
  795.     Dim x As Double
  796.     Dim y As Double
  797.     Dim z As Double
  798.     Dim size As Double
  799.     Dim color As Long
  800.     Dim data
  801.     Dim i As Long
  802.     Dim olddata As Collection
  803.     fl = FreeFile
  804.     On Local Error GoTo errOut
  805.     Set olddata = m_data
  806.     Set m_data = New Collection
  807.     Open sFile For Input As fl
  808.         
  809.     Do While Not EOF(fl)
  810.         Line Input #fl, strIn
  811.         strTrim = Trim(strIn)
  812.         
  813.         'skip comment lines
  814.         strFirstChar = Mid$(strTrim, 1, 1)
  815.         If strFirstChar = "#" Or strFirstChar = ";" Then GoTo nextLine
  816.         If strTrim = "" Then GoTo nextLine
  817.         
  818.         splitArray = Split(strTrim, ",")
  819.         
  820.         cols = UBound(splitArray)
  821.         If cols < 4 Then
  822.             MsgBox "Comma delimited file must have at least 4 columns (name,x,y,z)"
  823.             Exit Sub
  824.         End If
  825.                 
  826.         
  827.         'If we have not found numbers see if we found a header row
  828.         If Not bFoundData Then
  829.             If IsNumeric(splitArray(1)) = False Then
  830.                 'assume data is a header row
  831.                 m_xHeader = CStr(splitArray(1))
  832.                 m_yHeader = CStr(splitArray(2))
  833.                 m_zHeader = CStr(splitArray(3))
  834.                 m_sizeHeader = CStr(splitArray(4))
  835.                 GoTo nextLine
  836.             Else
  837.                 bFoundData = True
  838.             End If
  839.         End If
  840.         
  841.         sName = CStr(splitArray(0))
  842.         x = val(splitArray(1))
  843.         y = val(splitArray(2))
  844.         z = val(splitArray(3))
  845.         
  846.         'set defaults
  847.         i = i + 1
  848.         size = 1
  849.         color = D3DCOLORVALUEtoLONG(ColorValue4(1, (10 + i Mod 20) / 30, 0.3, (10 + (i Mod 40)) / 50))
  850.         data = ""
  851.         
  852.         If cols >= 4 Then size = val(splitArray(4))
  853.         If cols >= 5 Then color = val(splitArray(5))
  854.         If cols >= 6 Then data = splitArray(6)
  855.         
  856.         AddEntry sName, x, y, z, size, color, data
  857.         
  858.         
  859. nextLine:
  860.     Loop
  861.     Set olddata = Nothing
  862.     Close fl
  863.     Exit Sub
  864. errOut:
  865.     Set m_data = olddata
  866.     MsgBox "there was an error loading " + sFile
  867.     Close fl
  868. End Sub
  869. Sub DrawAxisNameSquare(i As Long)
  870.     Dim verts(4) As D3DVERTEX
  871.     Dim w As Single
  872.     Dim h As Single
  873.     Dim mat As D3DMATERIAL8
  874.     Dim sv As Single
  875.     Dim ev As Single
  876.     w = 2:    h = 0.25
  877.         
  878.     mat.Ambient = ColorValue4(1, 1, 1, 1)
  879.     mat.diffuse = ColorValue4(1, 1, 1, 1)
  880.     sv = (m_font2height * (i) / kdy)
  881.     ev = (m_font2height * (i + 1) / kdy)
  882.     Select Case i
  883.         Case 0
  884.             g_dev.SetTransform D3DTS_WORLD, m_LabelX.GetUpdatedMatrix
  885.             
  886.         Case 1
  887.             'Y axis now part of HUD
  888.             Exit Sub
  889.         Case 2
  890.             g_dev.SetTransform D3DTS_WORLD, m_LabelZ.GetUpdatedMatrix
  891.             
  892.     End Select
  893.         
  894.     g_dev.SetTexture 0, m_Tex
  895.     g_dev.SetMaterial mat
  896.     With verts(0): .x = -w: .y = -h: .tu = 0: .tv = ev: .nz = -1: End With
  897.     With verts(1): .x = w: .y = -h: .tu = 1: .tv = ev: .nz = -1: End With
  898.     With verts(2): .x = w: .y = h: .tu = 1: .tv = sv: .nz = -1: End With
  899.     With verts(3): .x = -w: .y = h: .tu = 0: .tv = sv: .nz = -1: End With
  900.     g_dev.SetVertexShader D3DFVF_VERTEX
  901.     g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  902.     With verts(0): .z = 0.01: .x = w: .y = -h: .tu = 0: .tv = ev: .nz = 1: End With
  903.     With verts(1): .z = 0.01: .x = -w: .y = -h: .tu = 1: .tv = ev: .nz = 1: End With
  904.     With verts(2): .z = 0.01: .x = -w: .y = h: .tu = 1: .tv = sv: .nz = 1: End With
  905.     With verts(3): .z = 0.01: .x = w: .y = h: .tu = 0: .tv = sv: .nz = 1: End With
  906.     g_dev.SetVertexShader D3DFVF_VERTEX
  907.     g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  908. End Sub
  909. Sub DeleteDeviceObjects()
  910.     Set m_font = Nothing
  911.     Set m_font2 = Nothing
  912. End Sub
  913.