home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / d3dim / src / tutorials / triangle / triangle.frm (.txt) next >
Encoding:
Visual Basic Form  |  1999-06-23  |  9.5 KB  |  229 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "TriangleVB"
  5.    ClientHeight    =   3570
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   3810
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   238
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   254
  15.    StartUpPosition =   3  'Windows Default
  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. Dim g_dx As New DirectX7
  24. Dim g_dd As DirectDraw7
  25. Dim g_ddsd As DDSURFACEDESC2
  26. Dim g_ddsPrimary As DirectDrawSurface7, _
  27.     g_ddsBackBuffer As DirectDrawSurface7
  28. Dim g_d3d As Direct3D7
  29. Dim g_d3dDevice As Direct3DDevice7
  30. Dim g_TriangleVert(5) As D3DVERTEX
  31. Dim g_rcDest As RECT, _
  32.     g_rcSrc As RECT
  33. Dim g_d3drcViewport(0) As D3DRECT
  34. Dim g_bRunning As Boolean
  35. Private Sub Form_Load()
  36.     Dim CNT As Single
  37.     Dim j As Long
  38.     ' Initialize the DirectDraw and Direct3D objects that this
  39.     ' sample application will use to render and display the triangle.
  40.     InitDDraw
  41.     InitD3D
  42.     InitGeometry
  43.     Me.Show
  44.     g_bRunning = True
  45.     Do While g_bRunning = True
  46.         CNT = CNT + 1
  47.         RenderScene
  48.         FrameMove (CNT / 360)
  49.         
  50.         g_dx.GetWindowRect Me.hWnd, g_rcDest
  51.         j = g_ddsPrimary.Blt(g_rcDest, g_ddsBackBuffer, g_rcSrc, DDBLT_WAIT)
  52.         If j <> DD_OK Then
  53.             MsgBox "Couldn't copy the source rectangle to the destination surface." & Chr$(13) & Hex(j)
  54.             End
  55.         End If
  56.         DoEvents
  57.     Loop
  58. End Sub
  59. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  60. '   Spin the triangle
  61. '   For this simple tutorial, we are just rotating the triangle about the y-axis.
  62. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  63. Private Sub FrameMove(stepVal As Single)
  64.     Dim matSpinY As D3DMATRIX
  65.     Call g_dx.RotateYMatrix(matSpinY, stepVal)
  66.     Call g_d3dDevice.SetTransform(D3DTRANSFORMSTATE_WORLD, matSpinY)
  67. End Sub
  68. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  69. ' Render the scene
  70. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  71. Private Sub RenderScene()
  72.     ' Clear the viewport to a blue color.
  73.     g_d3dDevice.Clear 1, g_d3drcViewport(), D3DCLEAR_TARGET, &HFF, 0, 0
  74.     ' Begin the scene, render the triangle, then complete the scene
  75.     g_d3dDevice.BeginScene
  76.         Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLELIST, D3DFVF_VERTEX, _
  77.                                        g_TriangleVert(0), 6, D3DDP_DEFAULT)
  78.     g_d3dDevice.EndScene
  79. End Sub
  80. Private Sub Form_Unload(Cancel As Integer)
  81.     g_bRunning = False
  82. End Sub
  83. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  84. ' Initalize DirectDraw
  85. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  86. Private Sub InitDDraw()
  87.     ' Create the DirectDraw object and set the application
  88.     ' cooperative level.
  89.     Set g_dd = g_dx.DirectDrawCreate("")
  90.     g_dd.SetCooperativeLevel Me.hWnd, DDSCL_NORMAL
  91.     ' Prepare and create the primary surface.
  92.     g_ddsd.lFlags = DDSD_CAPS
  93.     g_ddsd.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  94.     Set g_ddsPrimary = g_dd.CreateSurface(g_ddsd)
  95.     ' Now create the render-target surface. We are reusing g_ddsd here.
  96.     g_ddsd.lFlags = DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_CAPS
  97.     g_ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE
  98.     ' Use the size of the form to determine the size of the render target
  99.     ' and viewport rectangle.
  100.     g_dx.GetWindowRect Me.hWnd, g_rcDest
  101.     ' Set the dimensions of the surface description
  102.     g_ddsd.lWidth = g_rcDest.Right - g_rcDest.Left
  103.     g_ddsd.lHeight = g_rcDest.Bottom - g_rcDest.Top
  104.     ' Create the render-target surface
  105.     Set g_ddsBackBuffer = g_dd.CreateSurface(g_ddsd)
  106.         
  107.     ' Cache the dimensions of the render target. We'll use
  108.     ' it for blitting operations.
  109.     With g_rcSrc
  110.         .Left = 0: .Top = 0
  111.         .Bottom = g_ddsd.lHeight
  112.         .Right = g_ddsd.lWidth
  113.     End With
  114.     ' Create a DirectDrawClipper and attach it to the primary surface.
  115.     Dim pcClipper As DirectDrawClipper
  116.     Set pcClipper = g_dd.CreateClipper(0)
  117.     pcClipper.SetHWnd Me.hWnd
  118.     g_ddsPrimary.SetClipper pcClipper
  119. End Sub
  120. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  121. ' Initalize Direct3D, including the rendering device, lighting,
  122. ' the viewport, and the material
  123. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  124. Sub InitD3D()
  125.     Dim d3d As Direct3D7
  126.     Dim ddsd As DDSURFACEDESC2
  127.         
  128.     ' Retrieve a reference to the Direct3D7 class from the
  129.     ' DirectDraw7 object.
  130.     Set d3d = g_dd.GetDirect3D
  131.     ' Query DirectDraw for the current display mode. For simplicity, this
  132.     ' does not support palleted display modes (8-bit and lower).
  133.     g_dd.GetDisplayMode ddsd
  134.     If ddsd.ddpfPixelFormat.lRGBBitCount <= 8 Then
  135.         MsgBox "This application does not support screen display " & _
  136.                "modes lower than 16-bit."
  137.         End
  138.     End If
  139.     '
  140.     ' Create the device. The GUID is hardcoded for now, but should come from
  141.     ' device enumeration, which is the topic of a future tutorial. The device
  142.     ' is created off of our back buffer, which becomes the render target for
  143.     ' the newly created device.
  144.     '
  145.     Set g_d3dDevice = d3d.CreateDevice("IID_IDirect3DHALDevice", g_ddsBackBuffer)
  146.     ' Define the viewport rectangle.
  147.     Dim VPDesc As D3DVIEWPORT7
  148.         
  149.     VPDesc.lWidth = g_rcDest.Right - g_rcDest.Left
  150.     VPDesc.lHeight = g_rcDest.Bottom - g_rcDest.Top
  151.     VPDesc.minz = 0#
  152.     VPDesc.maxz = 1#
  153.     g_d3dDevice.SetViewport VPDesc
  154.     ' Cache the viewport rectangle for use in clearing operations later.
  155.     With g_d3drcViewport(0)
  156.         .X1 = 0: .Y1 = 0
  157.         .X2 = VPDesc.lWidth
  158.         .Y2 = VPDesc.lHeight
  159.     End With
  160.         
  161.     ' Enable ambient lighting
  162.     g_d3dDevice.SetRenderState D3DRENDERSTATE_AMBIENT, g_dx.CreateColorRGBA(1#, 1#, 1#, 1#)
  163.     '
  164.     ' Prepare a material
  165.     '
  166.     Dim mtrl As D3DMATERIAL7
  167.     mtrl.Ambient.r = 1#: mtrl.Ambient.g = 1#: mtrl.Ambient.b = 0#
  168.     ' Commit the material to the device.
  169.     g_d3dDevice.SetMaterial mtrl
  170.     '
  171.     ' Set up transformation matrices.
  172.     '
  173.     ' The world matrix controls the position and orientation of the polygons
  174.     ' in world space. We'll use it later to spin the triangle.
  175.     Dim matWorld As D3DMATRIX
  176.     g_dx.IdentityMatrix matWorld
  177.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_WORLD, matWorld
  178.     ' The view matrix defines the position and orientation of the camera.
  179.     ' Here, we are just moving it back along the z-axis by 15 units.
  180.     Dim matView  As D3DMATRIX
  181.     g_dx.IdentityMatrix matView
  182.     Call g_dx.ViewMatrix(matView, MakeVector(0, 0, -15), MakeVector(0, 0, 0), _
  183.                          MakeVector(0, 1, 0), 0)
  184.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matView
  185.     ' The projection matrix defines how the 3D scene is "projected" onto the
  186.     ' 2D render target (the backbuffer surface). Refer to the docs for more
  187.     ' info about projection matrices.
  188.     Dim matProj As D3DMATRIX
  189.     g_dx.IdentityMatrix matProj
  190.     Call g_dx.ProjectionMatrix(matProj, 1, 1000, pi / 2)
  191.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
  192. End Sub
  193. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  194. ' Initalize the geometry for the triangle.
  195. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  196. Private Sub InitGeometry()
  197.     ' Set up the geometry for the triangle.
  198.     Dim p1 As D3DVECTOR, _
  199.         p2 As D3DVECTOR, _
  200.         p3 As D3DVECTOR, _
  201.         vNormal As D3DVECTOR
  202.     ' Set values for three points used to define a triangle.
  203.     p1.x = 0#: p1.y = 5#: p1.z = 0#
  204.     p2.x = 5#: p2.y = -5#: p2.z = 0#
  205.     p3.x = -5#: p3.y = -5#: p3.z = 0#
  206.     ' Create a normal vector--shared by the points of the
  207.     ' triangle--that points toward the viewpoint. Note that
  208.     ' we reverse the Z coordinate for the normal of the points
  209.     ' that define the backside of the triangle.
  210.     vNormal.x = 0#: vNormal.y = 0#: vNormal.z = -1#
  211.     ' Create the 3 vertices for the front of the triangle.
  212.     g_dx.CreateD3DVertex p1.x, p1.y, p1.z, vNormal.x, vNormal.y, vNormal.z, 0, 0, g_TriangleVert(0)
  213.     g_dx.CreateD3DVertex p2.x, p2.y, p2.z, vNormal.x, vNormal.y, vNormal.z, 0, 0, g_TriangleVert(1)
  214.     g_dx.CreateD3DVertex p3.x, p3.y, p3.z, vNormal.x, vNormal.y, vNormal.z, 0, 0, g_TriangleVert(2)
  215.     ' Now do the same for the back of the triangle.
  216.     g_dx.CreateD3DVertex p3.x, p3.y, p3.z, vNormal.x, vNormal.y, -vNormal.z, 0, 0, g_TriangleVert(3)
  217.     g_dx.CreateD3DVertex p2.x, p2.y, p2.z, vNormal.x, vNormal.y, -vNormal.z, 0, 0, g_TriangleVert(4)
  218.     g_dx.CreateD3DVertex p1.x, p1.y, p1.z, vNormal.x, vNormal.y, -vNormal.z, 0, 0, g_TriangleVert(5)
  219. End Sub
  220. Private Function MakeVector(a As Double, b As Double, c As Double) As D3DVECTOR
  221.     Dim vecOut As D3DVECTOR
  222.     With vecOut
  223.         .x = a
  224.         .y = b
  225.         .z = c
  226.     End With
  227.     MakeVector = vecOut
  228. End Function
  229.