home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / SkinnedMesh / SkinnedMesh.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-10-08  |  8.8 KB  |  244 lines

  1. VERSION 5.00
  2. Begin VB.Form frmSkinnedMesh 
  3.    Caption         =   "Skinned Mesh"
  4.    ClientHeight    =   6015
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   7530
  8.    Icon            =   "SkinnedMesh.frx":0000
  9.    LinkTopic       =   "Form3"
  10.    ScaleHeight     =   401
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   502
  13.    StartUpPosition =   3  'Windows Default
  14. Attribute VB_Name = "frmSkinnedMesh"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  20. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  21. '  File:       skinnedMesh.frm
  22. '  Content:    Animate Skinned Geometry
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. Option Explicit
  25. Dim Character As CD3DFrame
  26. Dim Animation As CD3DAnimation
  27. Dim MediaDir As String
  28. Dim m_bInit As Boolean
  29. Dim m_bMinimized As Boolean
  30. Private Sub Form_Load()
  31.     Dim hr As Long
  32.     Me.Show
  33.     DoEvents
  34.     'find a path to our media
  35.     MediaDir = FindMediaDir("tiny.x")
  36.     D3DUtil_SetMediaPath MediaDir
  37.     ' Initialize D3D
  38.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  39.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  40.     ' If all fail it will display a message box indicating so.
  41.     '
  42.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  43.     If Not (m_bInit) Then End
  44.                 
  45.     ' Create new D3D mesh and animation objects
  46.     InitDeviceObjects
  47.     ' Sets the state for those objects and the current D3D device
  48.     RestoreDeviceObjects
  49.     ' Start our timer
  50.     DXUtil_Timer TIMER_start
  51.     ' Run the simulation forever
  52.     ' See Form_Keydown for exit processing
  53.     Do While True
  54.         ' Increment the simulation
  55.         FrameMove
  56.         
  57.         ' Render one image of the simulation
  58.         If Render Then
  59.             
  60.             ' Present the image to the screen
  61.             D3DUtil_PresentAll g_focushwnd
  62.         End If
  63.         
  64.         ' Allow for events to get processed
  65.         DoEvents
  66.         
  67.     Loop
  68.                    
  69. End Sub
  70. '-----------------------------------------------------------------------------
  71. ' Name: FrameMove()
  72. ' Desc:
  73. '-----------------------------------------------------------------------------
  74. Sub FrameMove()
  75.     Dim apptime As Single
  76.     'get ellapsed time since start of application
  77.     apptime = DXUtil_Timer(TIMER_GETAPPTIME)
  78.     'Have our animation pose our character
  79.     Animation.SetTime (apptime) * 4000
  80.             
  81.     'Rotate the character
  82.     Character.AddRotation COMBINE_replace, 0, 0, 1, 3.14 + (apptime) / 8
  83.     'Update all frame matrices (required for skinning)
  84.     Character.UpdateFrames
  85. End Sub
  86. '-----------------------------------------------------------------------------
  87. ' Name: Render()
  88. ' Desc:
  89. '-----------------------------------------------------------------------------
  90. Function Render() As Boolean
  91.     Dim hr As Long
  92.     Render = False
  93.     'See what state the device is in.
  94.     hr = g_dev.TestCooperativeLevel
  95.     If hr = D3DERR_DEVICENOTRESET Then
  96.         g_dev.Reset g_d3dpp
  97.         RestoreDeviceObjects
  98.     End If
  99.     'dont bother rendering if we are not ready yet
  100.     If hr <> 0 Then Exit Function
  101.     Render = True
  102.     'Clear the background to ARGB grey
  103.     D3DUtil_ClearAll &HFF9090FF
  104.     'Start the Scene
  105.     g_dev.BeginScene
  106.     'Render the character
  107.     Character.RenderSkins
  108.     'End the scene
  109.     g_dev.EndScene
  110. End Function
  111. '-----------------------------------------------------------------------------
  112. ' Name: InitDeviceObjects()
  113. ' Desc:
  114. '-----------------------------------------------------------------------------
  115. Sub InitDeviceObjects()
  116.     'Create an Animation object to hold any animations
  117.     Set Animation = New CD3DAnimation
  118.     'Load a skinned character
  119.     Set Character = D3DUtil_LoadFromFileAsSkin(MediaDir + "tiny.x", Nothing, Animation)
  120. End Sub
  121. '-----------------------------------------------------------------------------
  122. ' Name: RestoreDeviceObjects()
  123. ' Desc:
  124. '-----------------------------------------------------------------------------
  125. Sub RestoreDeviceObjects()
  126.     'Set up some lights and camera
  127.     g_lWindowWidth = Me.ScaleWidth
  128.     g_lWindowHeight = Me.ScaleHeight
  129.     D3DUtil_SetupDefaultScene
  130.     'position the camera
  131.     D3DUtil_SetupCamera vec3(0, 800, 200), vec3(0, 0, 200), vec3(0, 0, 1)
  132. End Sub
  133. '-----------------------------------------------------------------------------
  134. ' Name: InvalidateDeviceObjects()
  135. ' Desc: Place code to release non managed objects here
  136. '-----------------------------------------------------------------------------
  137. Sub InvalidateDeviceObjects()
  138.     'all objects are managed
  139. End Sub
  140. '-----------------------------------------------------------------------------
  141. ' Name: DeleteDeviceObjects()
  142. ' Desc:
  143. '-----------------------------------------------------------------------------
  144. Sub DeleteDeviceObjects()
  145.     Set Animation = Nothing
  146.     Set Character = Nothing
  147.     m_bInit = False
  148. End Sub
  149. '-----------------------------------------------------------------------------
  150. ' Name: Form_KeyDown()
  151. ' Desc: Process key messages for exit and change device
  152. '-----------------------------------------------------------------------------
  153. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  154.      Select Case KeyCode
  155.         
  156.         Case vbKeyEscape
  157.             Unload Me
  158.             
  159.         Case vbKeyF2
  160.                 
  161.             ' Pause the timer
  162.             DXUtil_Timer TIMER_STOP
  163.             
  164.             ' Bring up the device selection dialog
  165.             ' we pass in the form so the selection process
  166.             ' can make calls into InitDeviceObjects
  167.             ' and RestoreDeviceObjects
  168.             frmSelectDevice.SelectDevice Me
  169.             
  170.             ' Restart the timer
  171.             DXUtil_Timer TIMER_start
  172.             
  173.         Case vbKeyReturn
  174.         
  175.             ' Check for Alt-Enter if not pressed exit
  176.             If Shift <> 4 Then Exit Sub
  177.             
  178.             ' If we are windowed go fullscreen
  179.             ' If we are fullscreen returned to windowed
  180.             If g_d3dpp.Windowed Then
  181.                  D3DUtil_ResetFullscreen
  182.             Else
  183.                  D3DUtil_ResetWindowed
  184.             End If
  185.                              
  186.             ' Call Restore after ever mode change
  187.             ' because calling reset looses state that needs to
  188.             ' be reinitialized
  189.             RestoreDeviceObjects
  190.            
  191.     End Select
  192. End Sub
  193. '-----------------------------------------------------------------------------
  194. ' Name: Form_Resize()
  195. ' Desc: hadle resizing of the D3D backbuffer
  196. '-----------------------------------------------------------------------------
  197. Private Sub Form_Resize()
  198.     ' If D3D is not initialized then exit
  199.     If Not m_bInit Then Exit Sub
  200.     ' If we are in a minimized state stop the timer and exit
  201.     If Me.WindowState = vbMinimized Then
  202.         DXUtil_Timer TIMER_STOP
  203.         m_bMinimized = True
  204.         Exit Sub
  205.         
  206.     ' If we just went from a minimized state to maximized
  207.     ' restart the timer
  208.     Else
  209.         If m_bMinimized = True Then
  210.             DXUtil_Timer TIMER_start
  211.             m_bMinimized = False
  212.         End If
  213.     End If
  214.     ' Dont let the window get too small
  215.     If Me.ScaleWidth < 10 Then
  216.         Me.width = Screen.TwipsPerPixelX * 10
  217.         Exit Sub
  218.     End If
  219.     If Me.ScaleHeight < 10 Then
  220.         Me.height = Screen.TwipsPerPixelY * 10
  221.         Exit Sub
  222.     End If
  223.     'reset and resize our D3D backbuffer to the size of the window
  224.     D3DUtil_ResizeWindowed Me.hwnd
  225.     'All state get losts after a reset so we need to reinitialze it here
  226.     RestoreDeviceObjects
  227. End Sub
  228. '-----------------------------------------------------------------------------
  229. ' Name: Form_Unload()
  230. ' Desc:
  231. '-----------------------------------------------------------------------------
  232. Private Sub Form_Unload(Cancel As Integer)
  233.     DeleteDeviceObjects
  234.     End
  235. End Sub
  236. '-----------------------------------------------------------------------------
  237. ' Name: Form_Unload()
  238. ' Desc:
  239. '-----------------------------------------------------------------------------
  240. Public Function VerifyDevice(flags As Long, format As CONST_D3DFORMAT) As Boolean
  241.     If flags = D3DCREATE_HARDWARE_VERTEXPROCESSING Then Exit Function
  242.     VerifyDevice = True
  243. End Function
  244.