home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / d3dim / src / tutorials / enumerate / enumerate.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-07-26  |  17.1 KB  |  434 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Device Enumeration"
  5.    ClientHeight    =   3195
  6.    ClientLeft      =   840
  7.    ClientTop       =   1125
  8.    ClientWidth     =   3720
  9.    KeyPreview      =   -1  'True
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   213
  14.    ScaleMode       =   0  'User
  15.    ScaleWidth      =   254
  16. Attribute VB_Name = "Form1"
  17. Attribute VB_GlobalNameSpace = False
  18. Attribute VB_Creatable = False
  19. Attribute VB_PredeclaredId = True
  20. Attribute VB_Exposed = False
  21. Option Explicit
  22. Const pi As Single = 3.141592
  23. Const NUM_CONES As Integer = 10
  24. ' Define the cone.
  25. Private Type MyFlexibleVertex
  26.     vPosition As D3DVECTOR
  27.     vNormal As D3DVECTOR
  28. End Type
  29. Const CONE_HEIGHT As Single = 8#
  30. Const CONE_RADIUS As Single = 1#
  31. Const NUM_CONE_SIDES As Integer = 20
  32. Const NUM_CONE_VERTICES = NUM_CONE_SIDES + 1
  33. Dim g_vCone(NUM_CONE_VERTICES) As MyFlexibleVertex
  34. Dim g_matConePos(NUM_CONES) As D3DMATRIX
  35. Dim g_afConeColor(0 To NUM_CONES - 1, 0 To 2) As Double
  36. Dim g_dx As New DirectX7
  37. Dim g_dd As DirectDraw7
  38. Dim g_ddsd As DDSURFACEDESC2
  39. Dim g_ddsPrimary As DirectDrawSurface7, _
  40.     g_ddsBackBuffer As DirectDrawSurface7, _
  41.     g_ddsZBuffer As DirectDrawSurface7
  42. Dim g_d3d As Direct3D7
  43. Dim g_d3dDevice As Direct3DDevice7
  44. Dim g_rcDest As RECT, _
  45.     g_rcSrc As RECT
  46. Dim g_d3drcViewport(0) As D3DRECT
  47. Dim g_bRunning As Boolean
  48. ' Variables to hold enumerated values
  49. Dim g_sDriverGUID As String
  50. Dim g_sDeviceGUID As String
  51. Dim g_ddsdMode As DDSURFACEDESC2
  52. Dim g_bUsingFullScreen As Boolean
  53. Dim g_bUsing3DHardware As Boolean
  54. Public Sub RunApp()
  55.     Dim CNT As Single
  56.     Dim j As Long
  57.     ' Initialize the DirectDraw and Direct3D objects that this
  58.     ' sample application will use to render and display the triangle.
  59.     InitDDraw
  60.     InitD3D
  61.     InitGeometry
  62.     Me.Show
  63.     g_bRunning = True
  64.     Do While g_bRunning = True
  65.         CNT = CNT + 1
  66.         RenderScene
  67.         FrameMove (CNT / 360)
  68.         
  69.         If Not g_bUsingFullScreen Then
  70.             g_dx.GetWindowRect Me.hWnd, g_rcDest
  71.             j = g_ddsPrimary.Blt(g_rcDest, g_ddsBackBuffer, g_rcSrc, DDBLT_WAIT)
  72.             If j <> DD_OK Then
  73.                 MsgBox "Couldn't copy the source rectangle to the destination surface." & Chr$(13) & Hex(j)
  74.                 End
  75.             End If
  76.         Else
  77.             g_ddsPrimary.Flip Nothing, DDFLIP_WAIT
  78.         End If
  79.         DoEvents
  80.     Loop
  81. End Sub
  82. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  83. '   Animate the scene.
  84. '   Called once per frame, the call is used for animating the scene. The device is
  85. '   used for changing various render states, and the stepVal parameter is used for
  86. '   the timing of the dynamics of the scene.
  87. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  88. Private Sub FrameMove(stepVal As Single)
  89.     ' Move the viewpoint around in a circle. The view is most conveniently defined by an
  90.     ' eye point, a lookat point, and a vector defining the up direction.
  91.     Dim vEyePt As D3DVECTOR, _
  92.         vLookatPt As D3DVECTOR, _
  93.         vUpVec As D3DVECTOR
  94.         
  95.     vEyePt = MakeVector(5 * Sin(stepVal), 3, 5 * Cos(stepVal))
  96.     vLookatPt = MakeVector(4 * Sin(stepVal + 0.1), 2.5, 4 * Cos(stepVal + 0.1))
  97.     vUpVec = MakeVector(0, 1, 0)
  98.     ' Use the above parameters to build a new view matrix and put it into effect.
  99.     Dim matView As D3DMATRIX
  100.     ' Initialize the matrix to the identity.
  101.     g_dx.IdentityMatrix matView
  102.     Call g_dx.ViewMatrix(matView, vEyePt, vLookatPt, vUpVec, stepVal / 360)
  103.                               
  104.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matView
  105.         
  106. End Sub
  107. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  108. ' Render the scene. This tutorial draws a bunch of random-colored cones.
  109. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  110. Private Sub RenderScene()
  111.     Dim i As Integer
  112.           
  113.     ' Clear the viewport to a blue color, and clear the z-buffer.
  114.     g_d3dDevice.Clear 1, g_d3drcViewport(), D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFF, 1, 0
  115.     ' Begin the scene.
  116.     g_d3dDevice.BeginScene
  117.     ' Draw the set of cones.
  118.     For i = 0 To NUM_CONES - 1
  119.         ' Set the base material color for the cone
  120.         Dim mtrl As D3DMATERIAL7
  121.         mtrl.diffuse.r = g_afConeColor(i, 0)
  122.         mtrl.diffuse.g = g_afConeColor(i, 1)
  123.         mtrl.diffuse.b = g_afConeColor(i, 2)
  124.         g_d3dDevice.SetMaterial mtrl
  125.         
  126.         ' Position the cone with the world matrix
  127.         g_d3dDevice.SetTransform D3DTRANSFORMSTATE_WORLD, g_matConePos(i)
  128.         
  129.         ' Draw the cone, which is a triangle fan of custom, flexible vertices.
  130.          Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLEFAN, D3DFVF_XYZ Or D3DFVF_NORMAL, g_vCone(0), _
  131.               NUM_CONE_VERTICES, D3DDP_DEFAULT)
  132.         
  133.     Next i
  134.     ' End the scene.
  135.     g_d3dDevice.EndScene
  136. End Sub
  137. Private Sub Form_KeyPress(KeyAscii As Integer)
  138.     If KeyAscii = vbKeyEscape Then
  139.         Unload Me
  140.     End If
  141. End Sub
  142. Private Sub Form_Unload(Cancel As Integer)
  143.     g_bRunning = False
  144. End Sub
  145. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  146. ' Initalize DirectDraw.
  147. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  148. Private Sub InitDDraw()
  149.     ' Get information about the currently selected driver and device. This
  150.     ' information is from driver/device/mode enumeration.
  151.     Call Dialog.GetSelectedDriver(g_sDriverGUID, g_sDeviceGUID, g_ddsdMode.lWidth, g_ddsdMode.lHeight, g_ddsdMode.ddpfPixelFormat.lRGBBitCount, g_bUsingFullScreen, g_bUsing3DHardware)
  152.     ' Get the enumerated constant values.
  153.     Dim ddEnum As DirectDrawEnum
  154.     Set ddEnum = g_dx.GetDDEnum()
  155.     ' Create the DirectDraw object and set the application
  156.     ' cooperative level.
  157.     Set g_dd = g_dx.DirectDrawCreate(g_sDriverGUID)
  158.         
  159.     ' Do we want to render in windowed mode or in full screen exclusive mode?
  160.     If g_bUsingFullScreen Then
  161.         g_dd.SetCooperativeLevel Me.hWnd, DDSCL_EXCLUSIVE Or DDSCL_FULLSCREEN
  162.     Else
  163.         g_dd.SetCooperativeLevel Me.hWnd, DDSCL_NORMAL
  164.     End If
  165.     ' If full screen, we need to set the display mode.
  166.     If g_bUsingFullScreen Then
  167.         g_dd.SetDisplayMode g_ddsdMode.lWidth, g_ddsdMode.lHeight, g_ddsdMode.ddpfPixelFormat.lRGBBitCount, 0, DDSDM_STANDARDVGAMODE
  168.     End If
  169.     ' Prepare the primary surface depending on the mode.
  170.     If g_bUsingFullScreen Then
  171.         g_ddsd.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  172.         g_ddsd.ddscaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX Or DDSCAPS_3DDEVICE
  173.         g_ddsd.lBackBufferCount = 1
  174.     Else
  175.         g_ddsd.lFlags = DDSD_CAPS
  176.         g_ddsd.ddscaps.lCaps = DDSCAPS_PRIMARYSURFACE
  177.     End If
  178.     ' Create the primary surface.
  179.     Set g_ddsPrimary = g_dd.CreateSurface(g_ddsd)
  180.     If Not g_bUsingFullScreen Then
  181.         ' Create a DirectDrawClipper and attach it to the primary surface.
  182.         Dim pcClipper As DirectDrawClipper
  183.         Set pcClipper = g_dd.CreateClipper(0)
  184.         pcClipper.SetHWnd Me.hWnd
  185.         g_ddsPrimary.SetClipper pcClipper
  186.     End If
  187.     If Not g_bUsingFullScreen Then
  188.         ' Now create the render-target surface. We are reusing g_ddsd here.
  189.         g_ddsd.lFlags = DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_CAPS
  190.         g_ddsd.ddscaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE
  191.         ' Use the size of the form to determine the size of the render target
  192.         ' and viewport rectangle.
  193.         g_dx.GetWindowRect Me.hWnd, g_rcDest
  194.         ' Set the dimensions of the surface description
  195.         g_ddsd.lWidth = g_rcDest.Right - g_rcDest.Left
  196.         g_ddsd.lHeight = g_rcDest.Bottom - g_rcDest.Top
  197.         ' Create the render-target surface
  198.         Set g_ddsBackBuffer = g_dd.CreateSurface(g_ddsd)
  199.         
  200.         ' Cache the dimensions of the render target. We'll use
  201.         ' it for blitting operations.
  202.         With g_rcSrc
  203.             .Left = 0: .Top = 0
  204.             .Bottom = g_ddsd.lHeight
  205.             .Right = g_ddsd.lWidth
  206.         End With
  207.     Else
  208.         With g_rcSrc
  209.             .Left = 0: .Top = 0
  210.             .Bottom = g_ddsdMode.lHeight
  211.             .Right = g_ddsdMode.lWidth
  212.         End With
  213.         
  214.         With g_rcDest
  215.             .Left = 0: .Top = 0
  216.             .Bottom = g_ddsdMode.lHeight
  217.             .Right = g_ddsdMode.lWidth
  218.         End With
  219.         
  220.         ' Create the render-target surface
  221.         Dim ddscaps As DDSCAPS2
  222.         
  223.         With ddscaps
  224.             .lCaps = DDSCAPS_BACKBUFFER
  225.             .lCaps2 = 0
  226.             .lCaps3 = 0
  227.             .lCaps4 = 0
  228.         End With
  229.         
  230.        ' Retrieve the back buffer, which will be our render target.
  231.        Set g_ddsBackBuffer = g_ddsPrimary.GetAttachedSurface(ddscaps)
  232.         
  233.     End If
  234.     '
  235.     ' Create the z-buffer after creating the backbuffer and before creating the
  236.     ' Direct3D device. Note: before creating the z-buffer, applications may want to check
  237.     ' the device capabilities for the D3DPRASTERCAPS_ZBUFFERLESSHSR flag. This flag is true
  238.     ' for certain hardware that can do HSR (hidden-surface removal) without a z-buffer.
  239.     ' For those devices, there is no need to create a z-buffer.
  240.     '
  241.     Dim ddpfZBuffer As DDPIXELFORMAT
  242.     Dim d3dEnumPFs As Direct3DEnumPixelFormats
  243.         
  244.     Set g_d3d = g_dd.GetDirect3D
  245.     Set d3dEnumPFs = g_d3d.GetEnumZBufferFormats(g_sDeviceGUID)
  246.     ' For this tutorial we are only interested in z-buffers, so ignore any other
  247.     ' formats (e.g. DDPF_STENCILBUFFER) the get enumerated. An application could also
  248.     ' check the depth fo the z-buffer (16-bit, etc. and make a choice based on that,
  249.     ' as well. For this tutorial, we'll take the first one that we get.
  250.     Dim i As Long
  251.           
  252.     For i = 1 To d3dEnumPFs.GetCount()
  253.         Call d3dEnumPFs.GetItem(i, ddpfZBuffer)
  254.         If ddpfZBuffer.lFlags = DDPF_ZBUFFER Then
  255.             Exit For
  256.         End If
  257.     Next i
  258.     ' Prepare and create the z-buffer surface.
  259.     g_ddsd.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT Or DDSD_PIXELFORMAT
  260.     g_ddsd.ddscaps.lCaps = DDSCAPS_ZBUFFER
  261.     g_ddsd.lWidth = g_rcDest.Right - g_rcDest.Left
  262.     g_ddsd.lHeight = g_rcDest.Bottom - g_rcDest.Top
  263.     g_ddsd.ddpfPixelFormat = ddpfZBuffer
  264.     ' For hardware devices, the z-buffer should be in video memory. For software
  265.     ' devices, create the z-buffer in system memory.
  266.     If g_sDeviceGUID = "IID_IDirect3DHALDevice" Then
  267.         g_ddsd.ddscaps.lCaps = g_ddsd.ddscaps.lCaps Or DDSCAPS_VIDEOMEMORY
  268.     Else
  269.         g_ddsd.ddscaps.lCaps = g_ddsd.ddscaps.lCaps Or DDSCAPS_SYSTEMMEMORY
  270.     End If
  271.        
  272.     On Error Resume Next
  273.     Set g_ddsZBuffer = g_dd.CreateSurface(g_ddsd)
  274.     If g_ddsZBuffer Is Nothing Then
  275.         MsgBox ("Surface creation failed.")
  276.         End
  277.     End If
  278.        
  279.     ' Attach the z-buffer surface to the back buffer surface.
  280.     g_ddsBackBuffer.AddAttachedSurface g_ddsZBuffer
  281. End Sub
  282. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  283. ' Initalize Direct3D, including the rendering device, lighting,
  284. ' the viewport, and the material.
  285. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  286. Sub InitD3D()
  287.     Dim d3d As Direct3D7
  288.     Dim ddsd As DDSURFACEDESC2
  289.         
  290.     ' Retrieve a reference to the Direct3D7 class from the
  291.     ' DirectDraw7 object.
  292.     Set d3d = g_dd.GetDirect3D
  293.     ' Query DirectDraw for the current display mode. For simplicity, this
  294.     ' does not support palleted display modes (8-bit and lower).
  295.     g_dd.GetDisplayMode ddsd
  296.     If ddsd.ddpfPixelFormat.lRGBBitCount <= 8 Then
  297.         MsgBox "This application does not support screen display " & _
  298.                "modes lower than 16-bit."
  299.         End
  300.     End If
  301.     '
  302.     ' Create the device. The device is created off of our back buffer, which
  303.     ' becomes the render target for the newly created device.
  304.     '
  305.     On Error Resume Next
  306.     Set g_d3dDevice = d3d.CreateDevice(g_sDeviceGUID, g_ddsBackBuffer)
  307.     If g_d3dDevice Is Nothing Then
  308.         MsgBox ("Device creation failed.")
  309.         End
  310.     End If
  311.     ' Define the viewport rectangle.
  312.     Dim VPDesc As D3DVIEWPORT7
  313.         
  314.     VPDesc.lWidth = g_rcDest.Right - g_rcDest.Left
  315.     VPDesc.lHeight = g_rcDest.Bottom - g_rcDest.Top
  316.     VPDesc.minz = 0#
  317.     VPDesc.maxz = 1#
  318.     g_d3dDevice.SetViewport VPDesc
  319.     ' Cache the viewport rectangle for use in clearing operations later.
  320.     With g_d3drcViewport(0)
  321.         .X1 = 0: .Y1 = 0
  322.         .X2 = VPDesc.lWidth
  323.         .Y2 = VPDesc.lHeight
  324.     End With
  325.         
  326.     '
  327.     ' Prepare a material
  328.     '
  329.     Dim mtrl As D3DMATERIAL7
  330.     mtrl.diffuse.r = 1#: mtrl.diffuse.g = 1#: mtrl.diffuse.b = 1#
  331.     mtrl.Ambient.r = 1#: mtrl.Ambient.g = 1#: mtrl.Ambient.b = 1#
  332.     ' Commit the material to the device.
  333.     g_d3dDevice.SetMaterial mtrl
  334.     '
  335.     ' Set up transformation matrices.
  336.     '
  337.     ' The world matrix controls the position and orientation of the polygons
  338.     ' in world space. We'll use it later to spin the triangle.
  339.     Dim matWorld As D3DMATRIX
  340.     g_dx.IdentityMatrix matWorld
  341.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_WORLD, matWorld
  342.     ' The view matrix defines the position and orientation of the camera.
  343.     ' Here, we are just moving it back along the z-axis by 15 units.
  344.     Dim matView  As D3DMATRIX
  345.     g_dx.IdentityMatrix matView
  346.     Call g_dx.ViewMatrix(matView, MakeVector(0, 0, -15), MakeVector(0, 0, 0), _
  347.                          MakeVector(0, 1, 0), 0)
  348.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matView
  349.     ' The projection matrix defines how the 3D scene is "projected" onto the
  350.     ' 2D render target (the backbuffer surface). Refer to the docs for more
  351.     ' info about projection matrices.
  352.     Dim matProj As D3DMATRIX
  353.     g_dx.IdentityMatrix matProj
  354.     Call g_dx.ProjectionMatrix(matProj, 1, 1000, pi / 2#)
  355.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
  356.     ' Enable z-buffering for the application.
  357.     g_d3dDevice.SetRenderState D3DRENDERSTATE_ZENABLE, D3DZB_TRUE
  358.     'Set up the light.
  359.     Dim light As D3DLIGHT7
  360.     light.dltType = D3DLIGHT_POINT
  361.     light.diffuse.r = 1#
  362.     light.diffuse.g = 1#
  363.     light.diffuse.b = 1#
  364.     light.position.x = light.direction.x = 5#
  365.     light.position.y = light.direction.y = 5#
  366.     light.position.z = light.direction.z = 0#
  367.     light.range = 100#       ' D3DLIGHT_RANGE_MAX
  368.     light.attenuation0 = 1#
  369.         
  370.     g_d3dDevice.SetLight 0, light
  371.     g_d3dDevice.LightEnable 0, True
  372.     g_d3dDevice.SetRenderState D3DRENDERSTATE_LIGHTING, True
  373. End Sub
  374. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  375. ' Initalize the geometry.
  376. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  377. Private Sub InitGeometry()
  378.     Dim i As Integer
  379.     Dim x As Double, _
  380.         y As Double, _
  381.         z As Double
  382.    ' Set the cone's vertices. The cone is a triangle fan, so the zeroth vertex is the
  383.    ' tip of the cone (center of the fan) and the rest of the vertices are on the base
  384.    ' on the cone.
  385.     Dim vectorNorm As D3DVECTOR
  386.     vectorNorm = MakeVector(0, 1, 0)
  387.     Call g_dx.VectorNormalize(vectorNorm)
  388.     g_vCone(0).vNormal = vectorNorm
  389.         
  390.     For i = 0 To NUM_CONE_SIDES - 1
  391.         x = Sin(2 * pi * i / (NUM_CONE_SIDES - 1))
  392.         y = -CONE_HEIGHT / 2
  393.         z = Cos(2 * pi * i / (NUM_CONE_SIDES - 1))
  394.         
  395.         g_vCone(i + 1).vPosition = MakeVector(x * CONE_RADIUS, y * CONE_RADIUS, z * CONE_RADIUS)
  396.         
  397.         vectorNorm = MakeVector(x, 0.5, z)
  398.         Call g_dx.VectorNormalize(vectorNorm)
  399.         g_vCone(i + 1).vNormal = vectorNorm
  400.     Next i
  401.     'Set the position and color attributes for each cone.
  402.     For i = 0 To NUM_CONES - 1
  403.         ' Set the cone's scale.
  404.         g_matConePos(i).rc11 = 1#
  405.         g_matConePos(i).rc22 = 1#
  406.         g_matConePos(i).rc33 = 1#
  407.         g_matConePos(i).rc44 = 1#
  408.         
  409.         ' Set the cone's position.
  410.         g_matConePos(i).rc41 = 10 * RandomVal
  411.         g_matConePos(i).rc42 = 0#
  412.         g_matConePos(i).rc43 = 10 * RandomVal
  413.         
  414.         ' Set the cone's color
  415.         g_afConeColor(i, 0) = (1 + RandomVal) / 2
  416.         g_afConeColor(i, 1) = (1 + RandomVal) / 2
  417.         g_afConeColor(i, 2) = (1 + RandomVal) / 2
  418.                 
  419.     Next i
  420. End Sub
  421. Private Function MakeVector(a As Double, b As Double, c As Double) As D3DVECTOR
  422.     Dim vecOut As D3DVECTOR
  423.     With vecOut
  424.         .x = a
  425.         .y = b
  426.         .z = c
  427.     End With
  428.     MakeVector = vecOut
  429. End Function
  430. Private Function RandomVal() As Double
  431.     ' Here we just use the Rnd() function.
  432.     RandomVal = Rnd()
  433. End Function
  434.