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

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Vertex Blend"
  4.    ClientHeight    =   4485
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5640
  8.    Icon            =   "vertexblend.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   299
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   376
  13.    StartUpPosition =   3  'Windows Default
  14. Attribute VB_Name = "Form1"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '-----------------------------------------------------------------------------
  20. ' File: VertexBlend.frm
  21. ' Desc: Example code showing how to do a skinning effect, using the vertex
  22. '       blending feature of Direct3D. Normally, Direct3D transforms each
  23. '       vertex through the world matrix. The vertex blending feature,
  24. '       however, uses mulitple world matrices and a per-vertex blend factor
  25. '       to transform each vertex.
  26. ' Copyright (c) 1997-2000 Microsoft Corporation. All rights reserved.
  27. '-----------------------------------------------------------------------------
  28. Option Explicit
  29. Const D3DVBF_DISABLE = 0    '     Disable vertex blending
  30. Const D3DVBF_1WEIGHTS = 1   '     2 matrix blending
  31. Const D3DVBF_2WEIGHTS = 2   '     3 matrix blending
  32. Const D3DVBF_3WEIGHTS = 3   '     4 matrix blending
  33. Const D3DVBF_0WEIGHTS = 256 '     one matrix is used with weight 1.0
  34. '-----------------------------------------------------------------------------
  35. ' Name: struct D3DBLENDVERTEX
  36. ' Desc: Custom vertex which includes a blending factor
  37. '-----------------------------------------------------------------------------
  38. Private Type D3DBLENDVERTEX
  39.     v As D3DVECTOR
  40.     blend As Single
  41.     n As D3DVECTOR
  42.     tu As Single
  43.     tv As Single
  44. End Type
  45. Const D3DFVF_BLENDVERTEX = (D3DFVF_XYZB1 Or D3DFVF_NORMAL Or D3DFVF_TEX1)
  46. Dim m_Object As CD3DMesh
  47. Dim m_matUpperArm As D3DMATRIX
  48. Dim m_matLowerArm As D3DMATRIX
  49. Dim m_mediadir As String
  50. Dim g_ftime As Single
  51. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  52. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  53. '-----------------------------------------------------------------------------
  54. ' Name: Form_Load()
  55. ' Desc:
  56. '-----------------------------------------------------------------------------
  57. Private Sub Form_Load()
  58.     ' Show the form
  59.     Me.Show
  60.     DoEvents
  61.     Me.Caption = "VertexBlend: Surface Skinning Example"
  62.         
  63.     ' Initialize D3D
  64.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  65.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  66.     ' If all fail it will display a message box indicating so.
  67.     '
  68.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  69.     If Not (m_bInit) Then End
  70.             
  71.     ' Find and set the path to our media
  72.     m_mediadir = FindMediaDir("mslogo.x")
  73.     D3DUtil_SetMediaPath m_mediadir
  74.     ' Create new D3D mesh objects and loads content from disk
  75.     InitDeviceObjects
  76.     ' Sets the state for those objects and the current D3D device
  77.     RestoreDeviceObjects
  78.     ' Start our timer
  79.     DXUtil_Timer TIMER_start
  80.     ' Run the simulation forever
  81.     ' See Form_Keydown for exit processing
  82.     Do While True
  83.         ' Increment the simulation
  84.         FrameMove
  85.         
  86.         ' Render one image of the simulation
  87.         Render
  88.         
  89.         ' Present the image to the screen
  90.         D3DUtil_PresentAll g_focushwnd
  91.         
  92.         ' Allow for events to get processed
  93.         DoEvents
  94.         
  95.     Loop
  96. End Sub
  97. '-----------------------------------------------------------------------------
  98. ' Name: Form_KeyDown()
  99. ' Desc: Process key messages for exit and change device
  100. '-----------------------------------------------------------------------------
  101. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  102.      Select Case KeyCode
  103.         
  104.         Case vbKeyEscape
  105.             Unload Me
  106.             
  107.         Case vbKeyF2
  108.                 
  109.             ' Pause the timer
  110.             DXUtil_Timer TIMER_STOP
  111.             
  112.             ' Bring up the device selection dialog
  113.             ' we pass in the form so the selection process
  114.             ' can make calls into InitDeviceObjects
  115.             ' and RestoreDeviceObjects
  116.             frmSelectDevice.SelectDevice Me
  117.             
  118.             ' Restart the timer
  119.             DXUtil_Timer TIMER_start
  120.             
  121.         Case vbKeyReturn
  122.         
  123.             ' Check for Alt-Enter if not pressed exit
  124.             If Shift <> 4 Then Exit Sub
  125.             
  126.             ' If we are windowed go fullscreen
  127.             ' If we are fullscreen returned to windowed
  128.             If g_d3dpp.Windowed Then
  129.                  D3DUtil_ResetFullscreen
  130.             Else
  131.                  D3DUtil_ResetWindowed
  132.             End If
  133.                              
  134.             ' Call Restore after ever mode change
  135.             ' because calling reset looses state that needs to
  136.             ' be reinitialized
  137.             RestoreDeviceObjects
  138.            
  139.     End Select
  140. End Sub
  141. '-----------------------------------------------------------------------------
  142. ' Name: Form_Resize()
  143. ' Desc: hadle resizing of the D3D backbuffer
  144. '-----------------------------------------------------------------------------
  145. Private Sub Form_Resize()
  146.     ' If D3D is not initialized then exit
  147.     If Not m_bInit Then Exit Sub
  148.     ' If we are in a minimized state stop the timer and exit
  149.     If Me.WindowState = vbMinimized Then
  150.         DXUtil_Timer TIMER_STOP
  151.         m_bMinimized = True
  152.         Exit Sub
  153.         
  154.     ' If we just went from a minimized state to maximized
  155.     ' restart the timer
  156.     Else
  157.         If m_bMinimized = True Then
  158.             DXUtil_Timer TIMER_start
  159.             m_bMinimized = False
  160.         End If
  161.     End If
  162.     ' Dont let the window get too small
  163.     If Me.ScaleWidth < 10 Then
  164.         Me.width = Screen.TwipsPerPixelX * 10
  165.         Exit Sub
  166.     End If
  167.     If Me.ScaleHeight < 10 Then
  168.         Me.height = Screen.TwipsPerPixelY * 10
  169.         Exit Sub
  170.     End If
  171.     'reset and resize our D3D backbuffer to the size of the window
  172.     D3DUtil_ResizeWindowed Me.hwnd
  173.     'All state get losts after a reset so we need to reinitialze it here
  174.     g_lWindowWidth = Me.ScaleWidth
  175.     g_lWindowHeight = Me.ScaleHeight
  176.     RestoreDeviceObjects
  177. End Sub
  178. '-----------------------------------------------------------------------------
  179. ' Name: Form_Unload()
  180. ' Desc:
  181. '-----------------------------------------------------------------------------
  182. Private Sub Form_Unload(Cancel As Integer)
  183.     DeleteDeviceObjects
  184.     End
  185. End Sub
  186. '-----------------------------------------------------------------------------
  187. ' Name: FrameMove()
  188. ' Desc: Called once per frame, the call is the entry point for animating
  189. '       the scene.
  190. '-----------------------------------------------------------------------------
  191. Sub FrameMove()
  192.     g_ftime = DXUtil_Timer(TIMER_GETAPPTIME)
  193.     ' Set the vertex blending matrices for this frame
  194.     D3DXMatrixIdentity m_matUpperArm
  195.     Dim vAxis As D3DVECTOR
  196.     vAxis = vec3(2 + Sin(g_ftime * 3.1), 2 + Sin(g_ftime * 3.3), Sin(g_ftime * 3.5))
  197.     D3DXMatrixRotationAxis m_matLowerArm, vAxis, Sin(3 * g_ftime)
  198. End Sub
  199. '-----------------------------------------------------------------------------
  200. ' Name: Render()
  201. ' Desc: Called once per frame, the call is the entry point for 3d
  202. '       rendering. This function sets up render states, clears the
  203. '       viewport, and renders the scene.
  204. '-----------------------------------------------------------------------------
  205. Sub Render()
  206.     Dim hr As Long
  207.     'See what state the device is in.
  208.     hr = g_dev.TestCooperativeLevel
  209.     If hr = D3DERR_DEVICENOTRESET Then
  210.         g_dev.Reset g_d3dpp
  211.         RestoreDeviceObjects
  212.     End If
  213.     'dont bother rendering if we are not ready yet
  214.     If hr <> 0 Then Exit Sub
  215.     ' Clear the backbuffer
  216.     D3DUtil_ClearAll &HFF&
  217.     With g_dev
  218.         .BeginScene
  219.         
  220.         ' Enable vertex blending
  221.         .SetRenderState D3DRS_VERTEXBLEND, D3DVBF_1WEIGHTS
  222.         .SetTransform D3DTS_WORLD, m_matUpperArm
  223.         .SetTransform D3DTS_WORLD1, m_matLowerArm
  224.         
  225.         ' Display the object
  226.         m_Object.Render g_dev
  227.         ' End the scene.
  228.         .EndScene
  229.     End With
  230. End Sub
  231. '-----------------------------------------------------------------------------
  232. ' Name: InitDeviceObjects()
  233. ' Desc: Initialize scene objects.
  234. '-----------------------------------------------------------------------------
  235. Function InitDeviceObjects() As Boolean
  236.     Dim b As Boolean
  237.     Set m_Object = New CD3DMesh
  238.     b = m_Object.InitFromFile(g_dev, m_mediadir + "mslogo.x")
  239.     If Not b Then
  240.         MsgBox "media not found"
  241.         End
  242.     End If
  243.     ' Set a custom FVF for the mesh
  244.     m_Object.SetFVF g_dev, D3DFVF_BLENDVERTEX
  245.     Dim VertB As Direct3DVertexBuffer8
  246.     Dim Vertices() As D3DBLENDVERTEX
  247.     Dim NumVertices As Long
  248.     Dim MinX As Single
  249.     Dim MaxX As Single
  250.     Dim a As Single
  251.     Dim i As Long
  252.     NumVertices = m_Object.mesh.GetNumVertices()
  253.     Set VertB = m_Object.mesh.GetVertexBuffer()
  254.     MinX = 10000000000#
  255.     MaxX = -10000000000#
  256.     ReDim Vertices(NumVertices)
  257.     'copy data into our own array
  258.     D3DVertexBuffer8GetData VertB, 0, NumVertices * Len(Vertices(0)), 0, Vertices(0)
  259.     ' Calculate the min/max z values for all the vertices
  260.     For i = 0 To NumVertices - 1
  261.         If Vertices(i).v.x < MinX Then MinX = Vertices(i).v.x
  262.         If Vertices(i).v.x > MaxX Then MaxX = Vertices(i).v.x
  263.     Next
  264.     ' Set the blend factors for the vertices
  265.     For i = 0 To NumVertices - 1
  266.         a = (Vertices(i).v.x - MinX) / (MaxX - MinX)
  267.         Vertices(i).blend = 1 - Sin(a * g_pi * 1)
  268.     Next
  269.     D3DVertexBuffer8SetData VertB, 0, NumVertices * Len(Vertices(0)), 0, Vertices(0)
  270.     Set VertB = Nothing
  271.     InitDeviceObjects = True
  272. End Function
  273. '-----------------------------------------------------------------------------
  274. ' Name: RestoreDeviceObjects()
  275. ' Desc: Restore device-memory objects and state after a device is created or
  276. '       resized.
  277. '-----------------------------------------------------------------------------
  278. Sub RestoreDeviceObjects()
  279.     ' Restore mesh's local memory objects
  280.     m_Object.RestoreDeviceObjects g_dev
  281.     ' Set miscellaneous render states
  282.     With g_dev
  283.         .SetRenderState D3DRS_ZENABLE, 1  'TRUE
  284.         .SetRenderState D3DRS_AMBIENT, &H444444
  285.         
  286.         ' Set the projection matrix
  287.         Dim matProj As D3DMATRIX
  288.         D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, Me.ScaleHeight / Me.ScaleWidth, 1#, 10000#
  289.         .SetTransform D3DTS_PROJECTION, matProj
  290.         ' Set the app view matrix for normal viewing
  291.         Dim vEyePt As D3DVECTOR, vLookatPt As D3DVECTOR, vUpVec As D3DVECTOR
  292.         Dim matView As D3DMATRIX
  293.         vEyePt = vec3(0#, -5#, -10#)
  294.         vLookatPt = vec3(0#, 0#, 0#)
  295.         vUpVec = vec3(0#, 1#, 0#)
  296.         D3DXMatrixLookAtLH matView, vEyePt, vLookatPt, vUpVec
  297.         .SetTransform D3DTS_VIEW, matView
  298.         ' Create a directional light
  299.         Dim light As D3DLIGHT8
  300.         D3DUtil_InitLight light, D3DLIGHT_DIRECTIONAL, 1, -1, 1
  301.         .SetLight 1, light
  302.         .LightEnable 1, 1  'True
  303.         .SetRenderState D3DRS_LIGHTING, 1 'TRUE
  304.         
  305.         
  306.     End With
  307. End Sub
  308. '-----------------------------------------------------------------------------
  309. ' Name: InvalidateDeviceObjects()
  310. ' Desc: Called when the device-dependant objects are about to be lost.
  311. '-----------------------------------------------------------------------------
  312. Sub InvalidateDeviceObjects()
  313.     m_Object.InvalidateDeviceObjects
  314. End Sub
  315. '-----------------------------------------------------------------------------
  316. ' Name: DeleteDeviceObjects()
  317. ' Desc: Called when the app is exitting, or the device is being changed,
  318. '       this function deletes any device dependant objects.
  319. '-----------------------------------------------------------------------------
  320. Sub DeleteDeviceObjects()
  321.     m_Object.Destroy
  322.     Set m_Object = Nothing
  323.     m_bInit = False
  324. End Sub
  325.