home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / direct3d / skinnedmesh / skinnedmesh.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-10-09  |  8.7 KB  |  241 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) 2000 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.         Render
  59.         
  60.         ' Present the image to the screen
  61.         D3DUtil_PresentAll g_focushwnd
  62.         
  63.         ' Allow for events to get processed
  64.         DoEvents
  65.         
  66.     Loop
  67.                    
  68. End Sub
  69. '-----------------------------------------------------------------------------
  70. ' Name: FrameMove()
  71. ' Desc:
  72. '-----------------------------------------------------------------------------
  73. Sub FrameMove()
  74.     Dim apptime As Single
  75.     'get ellapsed time since start of application
  76.     apptime = DXUtil_Timer(TIMER_GETAPPTIME)
  77.     'Have our animation pose our character
  78.     Animation.SetTime (apptime) * 4000
  79.             
  80.     'Rotate the character
  81.     Character.AddRotation COMBINE_replace, 0, 0, 1, 3.14 + (apptime) / 8
  82.     'Update all frame matrices (required for skinning)
  83.     Character.UpdateFrames
  84. End Sub
  85. '-----------------------------------------------------------------------------
  86. ' Name: Render()
  87. ' Desc:
  88. '-----------------------------------------------------------------------------
  89. Sub Render()
  90.     Dim hr As Long
  91.     'See what state the device is in.
  92.     hr = g_dev.TestCooperativeLevel
  93.     If hr = D3DERR_DEVICENOTRESET Then
  94.         g_dev.Reset g_d3dpp
  95.         RestoreDeviceObjects
  96.     End If
  97.     'dont bother rendering if we are not ready yet
  98.     If hr <> 0 Then Exit Sub
  99.     'Clear the background to ARGB grey
  100.     D3DUtil_ClearAll &HFF9090FF
  101.     'Start the Scene
  102.     g_dev.BeginScene
  103.     'Render the character
  104.     Character.RenderSkins
  105.     'End the scene
  106.     g_dev.EndScene
  107. End Sub
  108. '-----------------------------------------------------------------------------
  109. ' Name: InitDeviceObjects()
  110. ' Desc:
  111. '-----------------------------------------------------------------------------
  112. Sub InitDeviceObjects()
  113.     'Create an Animation object to hold any animations
  114.     Set Animation = New CD3DAnimation
  115.     'Load a skinned character
  116.     Set Character = D3DUtil_LoadFromFileAsSkin(MediaDir + "tiny.x", Nothing, Animation)
  117. End Sub
  118. '-----------------------------------------------------------------------------
  119. ' Name: RestoreDeviceObjects()
  120. ' Desc:
  121. '-----------------------------------------------------------------------------
  122. Sub RestoreDeviceObjects()
  123.     'Set up some lights and camera
  124.     g_lWindowWidth = Me.ScaleWidth
  125.     g_lWindowHeight = Me.ScaleHeight
  126.     D3DUtil_SetupDefaultScene
  127.     'position the camera
  128.     D3DUtil_SetupCamera vec3(0, 800, 200), vec3(0, 0, 200), vec3(0, 0, 1)
  129. End Sub
  130. '-----------------------------------------------------------------------------
  131. ' Name: InvalidateDeviceObjects()
  132. ' Desc: Place code to release non managed objects here
  133. '-----------------------------------------------------------------------------
  134. Sub InvalidateDeviceObjects()
  135.     'all objects are managed
  136. End Sub
  137. '-----------------------------------------------------------------------------
  138. ' Name: DeleteDeviceObjects()
  139. ' Desc:
  140. '-----------------------------------------------------------------------------
  141. Sub DeleteDeviceObjects()
  142.     Set Animation = Nothing
  143.     Set Character = Nothing
  144.     m_bInit = False
  145. End Sub
  146. '-----------------------------------------------------------------------------
  147. ' Name: Form_KeyDown()
  148. ' Desc: Process key messages for exit and change device
  149. '-----------------------------------------------------------------------------
  150. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  151.      Select Case KeyCode
  152.         
  153.         Case vbKeyEscape
  154.             Unload Me
  155.             
  156.         Case vbKeyF2
  157.                 
  158.             ' Pause the timer
  159.             DXUtil_Timer TIMER_STOP
  160.             
  161.             ' Bring up the device selection dialog
  162.             ' we pass in the form so the selection process
  163.             ' can make calls into InitDeviceObjects
  164.             ' and RestoreDeviceObjects
  165.             frmSelectDevice.SelectDevice Me
  166.             
  167.             ' Restart the timer
  168.             DXUtil_Timer TIMER_start
  169.             
  170.         Case vbKeyReturn
  171.         
  172.             ' Check for Alt-Enter if not pressed exit
  173.             If Shift <> 4 Then Exit Sub
  174.             
  175.             ' If we are windowed go fullscreen
  176.             ' If we are fullscreen returned to windowed
  177.             If g_d3dpp.Windowed Then
  178.                  D3DUtil_ResetFullscreen
  179.             Else
  180.                  D3DUtil_ResetWindowed
  181.             End If
  182.                              
  183.             ' Call Restore after ever mode change
  184.             ' because calling reset looses state that needs to
  185.             ' be reinitialized
  186.             RestoreDeviceObjects
  187.            
  188.     End Select
  189. End Sub
  190. '-----------------------------------------------------------------------------
  191. ' Name: Form_Resize()
  192. ' Desc: hadle resizing of the D3D backbuffer
  193. '-----------------------------------------------------------------------------
  194. Private Sub Form_Resize()
  195.     ' If D3D is not initialized then exit
  196.     If Not m_bInit Then Exit Sub
  197.     ' If we are in a minimized state stop the timer and exit
  198.     If Me.WindowState = vbMinimized Then
  199.         DXUtil_Timer TIMER_STOP
  200.         m_bMinimized = True
  201.         Exit Sub
  202.         
  203.     ' If we just went from a minimized state to maximized
  204.     ' restart the timer
  205.     Else
  206.         If m_bMinimized = True Then
  207.             DXUtil_Timer TIMER_start
  208.             m_bMinimized = False
  209.         End If
  210.     End If
  211.     ' Dont let the window get too small
  212.     If Me.ScaleWidth < 10 Then
  213.         Me.width = Screen.TwipsPerPixelX * 10
  214.         Exit Sub
  215.     End If
  216.     If Me.ScaleHeight < 10 Then
  217.         Me.height = Screen.TwipsPerPixelY * 10
  218.         Exit Sub
  219.     End If
  220.     'reset and resize our D3D backbuffer to the size of the window
  221.     D3DUtil_ResizeWindowed Me.hwnd
  222.     'All state get losts after a reset so we need to reinitialze it here
  223.     RestoreDeviceObjects
  224. End Sub
  225. '-----------------------------------------------------------------------------
  226. ' Name: Form_Unload()
  227. ' Desc:
  228. '-----------------------------------------------------------------------------
  229. Private Sub Form_Unload(Cancel As Integer)
  230.     DeleteDeviceObjects
  231.     End
  232. End Sub
  233. '-----------------------------------------------------------------------------
  234. ' Name: Form_Unload()
  235. ' Desc:
  236. '-----------------------------------------------------------------------------
  237. Public Function VerifyDevice(flags As Long, format As CONST_D3DFORMAT) As Boolean
  238.     If flags = D3DCREATE_HARDWARE_VERTEXPROCESSING Then Exit Function
  239.     VerifyDevice = True
  240. End Function
  241.