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

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Dolphin: Blending Meshes in Real Time"
  4.    ClientHeight    =   4290
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5580
  8.    Icon            =   "dolphin.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   286
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   372
  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. '  Copyright (C) 1998-2000 Microsoft Corporation.  All Rights Reserved.
  21. '  File:       Dolphin.frm
  22. '  Content:    Sample of swimming dolphin
  23. '              This code uses the D3D Framework helper library.
  24. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  25. Option Explicit
  26. '-----------------------------------------------------------------------------
  27. ' Globals variables and definitions
  28. '-----------------------------------------------------------------------------
  29. Const WATER_COLOR = &H6688&
  30. Const AMBIENT_COLOR = &H33333333
  31. Const kMesh1 = 0
  32. Const kMesh2 = 1
  33. Const kMesh3 = 2
  34. 'Vertex type to be sent to D3D
  35. Private Type DOLPHINVERTEX
  36.     p As D3DVECTOR              'position of vertex
  37.     n As D3DVECTOR              'normal of vertex
  38.     tu As Single                'texture coordinate u
  39.     tv As Single                'texture coordinate v
  40. End Type
  41. 'VertexFormat to be sent to D3D to describe what
  42. 'elements DOLPHINVERTEX uses
  43. Const VertexFVF& = D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1
  44. 'Helper structure to manage moving vertex information
  45. 'from d3dvertex buffers to a vb array
  46. Private Type MESHTOOL
  47.     VertB As Direct3DVertexBuffer8
  48.     NumVertices As Long
  49.     Vertices() As DOLPHINVERTEX
  50. End Type
  51. 'Dolphin objects
  52. Dim m_DolphinGroupObject As CD3DFrame   ' Frame that contains all mesh poses
  53. Dim m_DolphinMesh01 As CD3DMesh         ' Dolphin Mesh in pose 1
  54. Dim m_DolphinMesh02 As CD3DMesh         ' Dolphin Mesh in pose 2  (rest pose)
  55. Dim m_DolphinMesh03 As CD3DMesh         ' Dolphin Mesh in pose 3
  56. Dim m_DolphinObject As CD3DFrame        ' Frame that contains current pose
  57. Dim m_DolphinMesh As CD3DMesh           ' Dolphin Mesh in current pose
  58. Dim m_DolphinTex As Direct3DTexture8    ' Dolphin texture
  59. 'Seafloor objects
  60. Dim m_FloorObject As CD3DFrame          ' Frame that contains seafloor mesh
  61. Dim m_SeaFloorMesh As CD3DMesh          ' Seafloor Mesh
  62. Dim m_meshtool(3) As MESHTOOL           ' VertexInformation on the 3 poses
  63. Dim m_dest As MESHTOOL                  ' VertexInformation on the current pose
  64. 'Textures for the water caustics
  65. Dim m_CausticTextures() As Direct3DTexture8         ' Array of caustic textures
  66. Dim m_CurrentCausticTexture As Direct3DTexture8     ' Current texture
  67. Dim m_media As String                   ' Path to media
  68. Dim g_ftime As Single                   ' Current time in simulation
  69. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  70. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  71. '-----------------------------------------------------------------------------
  72. ' Name: Form_Load()
  73. ' Desc: Main entry point for the sample
  74. '-----------------------------------------------------------------------------
  75. Private Sub Form_Load()
  76.     ' Show the form
  77.     Me.Show
  78.     DoEvents
  79.     ' Initialize D3D
  80.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  81.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  82.     ' If all fail it will display a message box indicating so.
  83.     '
  84.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  85.     If Not (m_bInit) Then End
  86.             
  87.     ' Find and set the path to our media
  88.     m_media = FindMediaDir("dolphin_group.x")
  89.     D3DUtil_SetMediaPath m_media
  90.     ' Create new D3D mesh objects and loads content from disk
  91.     InitDeviceObjects
  92.     ' Sets the state for those objects and the current D3D device
  93.     RestoreDeviceObjects
  94.     ' Start our timer
  95.     DXUtil_Timer TIMER_start
  96.     ' Run the simulation forever
  97.     ' See Form_Keydown for exit processing
  98.     Do While True
  99.         ' Increment the simulation
  100.         FrameMove
  101.         
  102.         ' Render one image of the simulation
  103.         Render
  104.         
  105.         ' Present the image to the screen
  106.         D3DUtil_PresentAll g_focushwnd
  107.         
  108.         ' Allow for events to get processed
  109.         DoEvents
  110.         
  111.     Loop
  112. End Sub
  113. '-----------------------------------------------------------------------------
  114. ' Name: Form_KeyDown()
  115. ' Desc: Process key messages for exit and change device
  116. '-----------------------------------------------------------------------------
  117. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  118.      Select Case KeyCode
  119.         
  120.         Case vbKeyEscape
  121.             Unload Me
  122.             
  123.         Case vbKeyF2
  124.                 
  125.             ' Pause the timer
  126.             DXUtil_Timer TIMER_STOP
  127.             
  128.             ' Bring up the device selection dialog
  129.             ' we pass in the form so the selection process
  130.             ' can make calls into InitDeviceObjects
  131.             ' and RestoreDeviceObjects
  132.             frmSelectDevice.SelectDevice Me
  133.             
  134.             ' Restart the timer
  135.             DXUtil_Timer TIMER_start
  136.             
  137.         Case vbKeyReturn
  138.         
  139.             ' Check for Alt-Enter if not pressed exit
  140.             If Shift <> 4 Then Exit Sub
  141.             
  142.             ' If we are windowed go fullscreen
  143.             ' If we are fullscreen returned to windowed
  144.             If g_d3dpp.Windowed Then
  145.                  D3DUtil_ResetFullscreen
  146.             Else
  147.                  D3DUtil_ResetWindowed
  148.             End If
  149.                              
  150.             ' Call Restore after ever mode change
  151.             ' because calling reset looses state that needs to
  152.             ' be reinitialized
  153.             RestoreDeviceObjects
  154.            
  155.     End Select
  156. End Sub
  157. '-----------------------------------------------------------------------------
  158. ' Name: Form_Resize()
  159. ' Desc: hadle resizing of the D3D backbuffer
  160. '-----------------------------------------------------------------------------
  161. Private Sub Form_Resize()
  162.     ' If D3D is not initialized then exit
  163.     If Not m_bInit Then Exit Sub
  164.     ' If we are in a minimized state stop the timer and exit
  165.     If Me.WindowState = vbMinimized Then
  166.         DXUtil_Timer TIMER_STOP
  167.         m_bMinimized = True
  168.         Exit Sub
  169.         
  170.     ' If we just went from a minimized state to maximized
  171.     ' restart the timer
  172.     Else
  173.         If m_bMinimized = True Then
  174.             DXUtil_Timer TIMER_start
  175.             m_bMinimized = False
  176.         End If
  177.     End If
  178.     ' Dont let the window get too small
  179.     If Me.ScaleWidth < 10 Then
  180.         Me.width = Screen.TwipsPerPixelX * 10
  181.         Exit Sub
  182.     End If
  183.     If Me.ScaleHeight < 10 Then
  184.         Me.height = Screen.TwipsPerPixelY * 10
  185.         Exit Sub
  186.     End If
  187.     'reset and resize our D3D backbuffer to the size of the window
  188.     D3DUtil_ResizeWindowed Me.hwnd
  189.     'All state get losts after a reset so we need to reinitialze it here
  190.     RestoreDeviceObjects
  191. End Sub
  192. '-----------------------------------------------------------------------------
  193. ' Name: Form_Unload()
  194. ' Desc:
  195. '-----------------------------------------------------------------------------
  196. Private Sub Form_Unload(Cancel As Integer)
  197.     DeleteDeviceObjects
  198.     End
  199. End Sub
  200. '-----------------------------------------------------------------------------
  201. ' Name: InitDeviceObjects()
  202. ' Desc: Create mesh and texture objects
  203. '-----------------------------------------------------------------------------
  204. Function InitDeviceObjects() As Boolean
  205.     Dim b As Boolean
  206.     Dim t As Long
  207.     Dim strName As String
  208.     Dim i As Long
  209.         
  210.      
  211.     'Allocate an array for the caustic textures
  212.     ReDim m_CausticTextures(32)
  213.         
  214.     'Load caustic textures into an array
  215.     For t = 0 To 31
  216.         strName = m_media + "Caust" + format$(t, "00") + ".tga"
  217.         Set m_CausticTextures(t) = D3DUtil_CreateTexture(g_dev, strName, D3DFMT_UNKNOWN)
  218.         If m_CausticTextures(t) Is Nothing Then Debug.Print "Unable to find media " + strName
  219.     Next
  220.     ' Load the file-based mesh objects
  221.     Set m_DolphinGroupObject = D3DUtil_LoadFromFile(m_media + "dolphin_group.x", Nothing, Nothing)
  222.     Set m_DolphinObject = D3DUtil_LoadFromFile(m_media + "dolphin.x", Nothing, Nothing)
  223.     Set m_FloorObject = D3DUtil_LoadFromFile(m_media + "seafloor.x", Nothing, Nothing)
  224.     '  Gain access to the meshes from the parent frames
  225.     Set m_DolphinMesh01 = m_DolphinGroupObject.FindChildObject("Dolph01", 0)
  226.     Set m_DolphinMesh02 = m_DolphinGroupObject.FindChildObject("Dolph02", 0)
  227.     Set m_DolphinMesh03 = m_DolphinGroupObject.FindChildObject("Dolph03", 0)
  228.     Set m_DolphinMesh = m_DolphinObject.FindChildObject("Dolph02", 0).GetChildMesh(0)
  229.     Set m_SeaFloorMesh = m_FloorObject.FindChildObject("SeaFloor", 0)
  230.     ' Set the FVF (flexible vertex format) to one we reconginze
  231.     Call m_DolphinMesh01.SetFVF(g_dev, VertexFVF)
  232.     Call m_DolphinMesh02.SetFVF(g_dev, VertexFVF)
  233.     Call m_DolphinMesh03.SetFVF(g_dev, VertexFVF)
  234.     Call m_DolphinMesh.SetFVF(g_dev, VertexFVF)
  235.     Call m_SeaFloorMesh.SetFVF(g_dev, VertexFVF)
  236.     ' Load the texture for the dolphin's skin
  237.     Set m_DolphinTex = D3DUtil_CreateTexture(g_dev, m_media + "dolphin.bmp", D3DFMT_UNKNOWN)
  238.     ' The folowing scales the sea floor vertices, and adds some bumpiness
  239.     Dim seafloortool As MESHTOOL
  240.     ' Meshtool init copies mesh vertices from the mesh object into the
  241.     ' seafloortool.vertices array
  242.     MESHTOOL_INIT seafloortool, m_SeaFloorMesh.mesh
  243.     ' Loop through  and modify height (y) of vertices
  244.     For i = 0 To seafloortool.NumVertices - 1
  245.        seafloortool.Vertices(i).p.y = seafloortool.Vertices(i).p.y + Rnd(1) + Rnd(1) + Rnd(1)
  246.        seafloortool.Vertices(i).tu = seafloortool.Vertices(i).tu * 10
  247.        seafloortool.Vertices(i).tv = seafloortool.Vertices(i).tv * 10
  248.     Next
  249.     ' Save modified vertices back to the vertex buffer and cleanup seafloortool object
  250.     D3DVertexBuffer8SetData seafloortool.VertB, 0, Len(seafloortool.Vertices(0)) * seafloortool.NumVertices, 0, seafloortool.Vertices(0)
  251.     MESHTOOL_DESTROY seafloortool
  252.     ' Extract vertex information for the 3 dolphin poses
  253.     MESHTOOL_INIT m_meshtool(kMesh1), m_DolphinMesh01.mesh
  254.     MESHTOOL_INIT m_meshtool(kMesh2), m_DolphinMesh02.mesh
  255.     MESHTOOL_INIT m_meshtool(kMesh3), m_DolphinMesh03.mesh
  256.     ' size Vertices array for the current pose
  257.     MESHTOOL_INIT m_dest, m_DolphinMesh.mesh
  258.     InitDeviceObjects = True
  259.         
  260. End Function
  261. '-----------------------------------------------------------------------------
  262. ' Name: RestoreDeviceObjects()
  263. ' Desc: Restore device-memory objects and state after a device is created or
  264. '       resized.
  265. '-----------------------------------------------------------------------------
  266. Public Sub RestoreDeviceObjects()
  267.     'Restore Mesh objects
  268.     m_DolphinGroupObject.RestoreDeviceObjects g_dev
  269.     m_DolphinObject.RestoreDeviceObjects g_dev
  270.     m_FloorObject.RestoreDeviceObjects g_dev
  271.         
  272.     With g_dev
  273.         
  274.         ' Set world transform
  275.         Dim matWorld As D3DMATRIX
  276.         D3DXMatrixIdentity matWorld
  277.         .SetTransform D3DTS_WORLD, matWorld
  278.        ' Set the  view matrix for normal viewing
  279.         Dim vEyePt As D3DVECTOR, vLookatPt As D3DVECTOR, vUpVec As D3DVECTOR
  280.         Dim matView As D3DMATRIX
  281.         vEyePt = vec3(0, 0, -5)
  282.         vLookatPt = vec3(0, 0, 0)
  283.         vUpVec = vec3(0, 1, 0)
  284.         D3DXMatrixLookAtLH matView, vEyePt, vLookatPt, vUpVec
  285.         .SetTransform D3DTS_VIEW, matView
  286.         
  287.         ' Set the projection matrix
  288.         Dim matProj As D3DMATRIX
  289.         Dim fAspect As Single
  290.         fAspect = Me.ScaleHeight / Me.ScaleWidth
  291.         D3DXMatrixPerspectiveFovLH matProj, g_pi / 3, fAspect, 1, 10000
  292.         .SetTransform D3DTS_PROJECTION, matProj
  293.         ' Set texture stages to modulate the diffuse color with the texture color
  294.         .SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
  295.         .SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE
  296.         .SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
  297.         .SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  298.         .SetTextureStageState 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
  299.         .SetTextureStageState 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  300.         .SetTextureStageState 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR
  301.         ' Set default render states
  302.         .SetRenderState D3DRS_DITHERENABLE, 1 'True
  303.         .SetRenderState D3DRS_SPECULARENABLE, 0 'False
  304.         .SetRenderState D3DRS_ZENABLE, 1 'True
  305.         .SetRenderState D3DRS_NORMALIZENORMALS, 1 'True
  306.         ' Turn on fog, for underwater effect
  307.         Dim fFogStart  As Single
  308.         Dim fFogEnd As Single
  309.         fFogStart = 1
  310.         fFogEnd = 50
  311.         .SetRenderState D3DRS_FOGENABLE, 1 ' True
  312.         .SetRenderState D3DRS_FOGCOLOR, WATER_COLOR
  313.         .SetRenderState D3DRS_FOGTABLEMODE, D3DFOG_NONE
  314.         .SetRenderState D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR
  315.         .SetRenderState D3DRS_RANGEFOGENABLE, 0 'False
  316.         .SetRenderState D3DRS_FOGSTART, FtoDW(fFogStart)
  317.         .SetRenderState D3DRS_FOGEND, FtoDW(fFogEnd)
  318.             
  319.         ' Create a directional light pointing straight down
  320.         Dim light As D3DLIGHT8
  321.         D3DUtil_InitLight light, D3DLIGHT_DIRECTIONAL, 0, -1, 0
  322.         .SetLight 0, light
  323.         .LightEnable 0, 1 'True
  324.         .SetRenderState D3DRS_LIGHTING, 1 'TRUE
  325.         .SetRenderState D3DRS_AMBIENT, AMBIENT_COLOR
  326.     End With
  327. End Sub
  328. '-----------------------------------------------------------------------------
  329. ' Name: MESHTOOL_INIT()
  330. ' Desc:
  331. '-----------------------------------------------------------------------------
  332. Private Sub MESHTOOL_INIT(mt As MESHTOOL, m As D3DXMesh)
  333.     Set mt.VertB = m.GetVertexBuffer
  334.     mt.NumVertices = m.GetNumVertices
  335.     ReDim mt.Vertices(mt.NumVertices)
  336.     D3DVertexBuffer8GetData mt.VertB, 0, mt.NumVertices * Len(mt.Vertices(0)), 0, mt.Vertices(0)
  337. End Sub
  338. '-----------------------------------------------------------------------------
  339. ' Name: MESHTOOL_DESTROY()
  340. ' Desc:
  341. '-----------------------------------------------------------------------------
  342. Private Sub MESHTOOL_DESTROY(mt As MESHTOOL)
  343.    Set mt.VertB = Nothing
  344.    ReDim mt.Vertices(0)
  345. End Sub
  346. '-----------------------------------------------------------------------------
  347. ' Name: FrameMove()
  348. ' Desc: Called once per image frame, the call is the entry point for animating
  349. '       the scene.
  350. '-----------------------------------------------------------------------------
  351. Sub FrameMove()
  352.     'Dont do anything if in a minimized state
  353.     If m_bMinimized = True Then Exit Sub
  354.     'Get the time as a single
  355.     g_ftime = DXUtil_Timer(TIMER_GETAPPTIME) * 0.9
  356.     Dim fKickFreq As Single, fPhase As Single, fBlendWeight As Single
  357.     'compute time based inputs
  358.     fKickFreq = g_ftime * 2
  359.     fPhase = g_ftime / 3
  360.     fBlendWeight = Sin(fKickFreq)
  361.     ' Blend the meshes (which makes the dolphin appear to swim)
  362.     Call BlendMeshes(fBlendWeight)
  363.     ' Move the dolphin in a circle and have it undulate
  364.     Dim vTrans As D3DVECTOR
  365.     Dim qRot As D3DQUATERNION
  366.     Dim matDolphin As D3DMATRIX
  367.     Dim matTrans As D3DMATRIX, matRotate1 As D3DMATRIX, matRotate2 As D3DMATRIX
  368.     'Scale dolphin geometery to 1/100 original
  369.     D3DXMatrixScaling matDolphin, 0.01, 0.01, 0.01
  370.     'add up and down roation (since modeled along x axis)
  371.     D3DXMatrixRotationZ matRotate1, -Cos(fKickFreq) / 6
  372.     D3DXMatrixMultiply matDolphin, matDolphin, matRotate1
  373.     'add rotation to make dolphin point at tangent to the circle
  374.     D3DXMatrixRotationY matRotate2, fPhase
  375.     D3DXMatrixMultiply matDolphin, matDolphin, matRotate2
  376.     'add traslation to make the dolphin move in a circle and bob up and down
  377.     'in sync with its flippers
  378.     D3DXMatrixTranslation matTrans, -5 * Sin(fPhase), Sin(fKickFreq) / 2, 10 - 10 * Cos(fPhase)
  379.     D3DXMatrixMultiply matDolphin, matDolphin, matTrans
  380.         
  381.     m_DolphinObject.SetMatrix matDolphin
  382.     ' Animate the caustic textures
  383.     Dim tex As Long
  384.     tex = CLng((g_ftime * 32)) Mod 32
  385.     Set m_CurrentCausticTexture = m_CausticTextures(tex)   
  386. End Sub
  387. '-----------------------------------------------------------------------------
  388. ' Name: BlendMeshes()
  389. ' Desc: Does a linear interpolation between all vertex positions and normals
  390. '       in two source meshes and outputs the result to the destination mesh.
  391. '       Note: all meshes must contain the same number of vertices, and the
  392. '       destination mesh must be in device memory.
  393. '-----------------------------------------------------------------------------
  394. Sub BlendMeshes(ByVal fWeight As Single)
  395.     Dim fWeight1 As Single, fWeight2 As Single
  396.     Dim vTemp1 As D3DVECTOR, vTemp2 As D3DVECTOR
  397.     Dim i As Long, j As Long
  398.     If (fWeight < 0) Then
  399.         j = kMesh3
  400.     Else
  401.         j = kMesh1
  402.     End If
  403.      
  404.     ' compute blending factors
  405.     fWeight1 = fWeight
  406.     If fWeight < 0 Then fWeight1 = -fWeight1
  407.     fWeight2 = 1 - fWeight1
  408.     ' Linearly Interpolate (LERP)positions and normals
  409.     For i = 0 To m_dest.NumVertices - 1
  410.         D3DXVec3Scale vTemp1, m_meshtool(kMesh2).Vertices(i).p, fWeight2
  411.         D3DXVec3Scale vTemp2, m_meshtool(j).Vertices(i).p, fWeight1
  412.         D3DXVec3Add m_dest.Vertices(i).p, vTemp1, vTemp2
  413.         
  414.         D3DXVec3Scale vTemp1, m_meshtool(kMesh2).Vertices(i).n, fWeight2
  415.         D3DXVec3Scale vTemp2, m_meshtool(j).Vertices(i).n, fWeight1
  416.         D3DXVec3Add m_dest.Vertices(i).n, vTemp1, vTemp2
  417.     Next
  418.     'Copy the data into the d3dvertex buffer
  419.     D3DVertexBuffer8SetData m_dest.VertB, 0, m_dest.NumVertices * Len(m_dest.Vertices(0)), 0, m_dest.Vertices(0)
  420. End Sub
  421. '-----------------------------------------------------------------------------
  422. ' Name: Render()
  423. ' Desc: Called once per frame, the call is the entry point for 3d
  424. '       rendering. This function sets up render states, clears the
  425. '       viewport, and renders the scene.
  426. '-----------------------------------------------------------------------------
  427. Sub Render()
  428.     'Dont do anything if in a minimized state
  429.     If m_bMinimized = True Then Exit Sub
  430.     On Local Error Resume Next
  431.     Dim mat As D3DMATRIX
  432.     Dim mat2 As D3DMATRIX
  433.     Dim hr As Long
  434.     'See what state the device is in.
  435.     hr = g_dev.TestCooperativeLevel
  436.     If hr = D3DERR_DEVICENOTRESET Then
  437.         g_dev.Reset g_d3dpp
  438.         RestoreDeviceObjects
  439.     End If
  440.     'dont bother rendering if we are not ready yet
  441.     If hr <> 0 Then Exit Sub
  442.     ' Clear the backbuffer
  443.     D3DUtil_ClearAll WATER_COLOR
  444.     With g_dev
  445.         .BeginScene
  446.                         
  447.         ' Render the Seafloor. For devices that support one-pass multi-
  448.         ' texturing, use the second texture stage to blend in the animated
  449.         ' water caustics texture.
  450.         If (g_d3dCaps.MaxTextureBlendStages > 1) Then
  451.             ' Set up the 2nd texture stage for the animated water caustics
  452.             .SetTexture 1, m_CurrentCausticTexture
  453.             .SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_MODULATE
  454.             .SetTextureStageState 1, D3DTSS_COLORARG1, D3DTA_TEXTURE
  455.             .SetTextureStageState 1, D3DTSS_COLORARG2, D3DTA_CURRENT
  456.             ' Tell D3D to automatically generate texture coordinates from the
  457.             ' model's position in camera space. The texture transform matrix is
  458.             ' setup so that the 'x' and 'z' coordinates are scaled to become the
  459.             ' resulting 'tu' and 'tv' texture coordinates. The resulting effect
  460.             ' is that the caustic texture is draped over the geometry from above.
  461.             mat.m11 = 0.05:           mat.m12 = 0#
  462.             mat.m21 = 0#:             mat.m22 = 0#
  463.             mat.m31 = 0#:             mat.m32 = 0.05
  464.             mat.m41 = Sin(g_ftime) / 8: mat.m42 = (Cos(g_ftime) / 10) - (g_ftime / 10)
  465.             .SetTransform D3DTS_TEXTURE1, mat
  466.             .SetTextureStageState 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION
  467.             .SetTextureStageState 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2
  468.         End If
  469.         g_dev.SetRenderState D3DRS_AMBIENT, &HB0B0B0B0
  470.         
  471.         
  472.         ' Finally, render the actual seafloor with the above states
  473.         m_FloorObject.Render g_dev
  474.         
  475.         
  476.         ' Disable the second texture stage
  477.         If (g_d3dCaps.MaxTextureBlendStages > 1) Then
  478.             .SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_DISABLE
  479.         End If
  480.         ' Render the dolphin in it's first pass.
  481.         .SetRenderState D3DRS_AMBIENT, AMBIENT_COLOR
  482.         m_DolphinObject.Render g_dev
  483.         ' For devices that support one-pass multi-texturing, use the second
  484.         ' texture stage to blend in the animated water caustics texture for
  485.         ' the dolphin. This a little tricky because we only want caustics on
  486.         ' the part of the dolphin that is lit from above. To acheive this
  487.         ' effect, the dolphin is rendered alpha-blended with a second pass
  488.         ' which has the caustic effects modulating the diffuse component
  489.         '  which contains lighting-only information) of the geometry.
  490.         If (g_d3dCaps.MaxTextureBlendStages > 1) Then
  491.             ' For the 2nd pass of rendering the dolphin, turn on the caustic
  492.             ' effects. Start with setting up the 2nd texture stage state, which
  493.             ' will modulate the texture with the diffuse component. This actually
  494.             ' only needs one stage, except that using a CD3DFile object makes that
  495.             ' a little tricky.
  496.             .SetTexture 1, m_CurrentCausticTexture
  497.             .SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_MODULATE
  498.             .SetTextureStageState 1, D3DTSS_COLORARG1, D3DTA_TEXTURE
  499.             .SetTextureStageState 1, D3DTSS_COLORARG2, D3DTA_DIFFUSE
  500.             ' Now, set up D3D to generate texture coodinates. This is the same as
  501.             ' with the seafloor  the 'x' and 'z' position coordinates in camera
  502.             ' space are used to generate the 'tu' and 'tv' texture coordinates),
  503.             ' except our scaling factors are different in the texture matrix, to
  504.             ' get a better looking result.
  505.             mat2.m11 = 0.5: mat2.m12 = 0#
  506.             mat2.m21 = 0#: mat2.m22 = 0#
  507.             mat2.m31 = 0#: mat2.m32 = 0.5
  508.             mat2.m41 = 0#: mat2.m42 = 0#
  509.             .SetTransform D3DTS_TEXTURE1, mat2
  510.             .SetTextureStageState 1, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION
  511.             .SetTextureStageState 1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2
  512.             ' Set the ambient color and fog color to pure black. Ambient is set
  513.             ' to black because we still have a light shining from above, but we
  514.             ' don't want any caustic effects on the dolphin's underbelly. Fog is
  515.             ' set to black because we want the caustic effects to fade out in the
  516.             ' distance just as the model does with the WATER_COLOR.
  517.             .SetRenderState D3DRS_AMBIENT, &H0&
  518.             .SetRenderState D3DRS_FOGCOLOR, &H0&
  519.             ' Set up blending modes to add this caustics-only pass with the
  520.             ' previous pass.
  521.             .SetRenderState D3DRS_ALPHABLENDENABLE, 1 ' True
  522.             .SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR
  523.             .SetRenderState D3DRS_DESTBLEND, D3DBLEND_ONE
  524.             ' Finally, render the caustic effects for the dolphin
  525.             m_DolphinObject.Render g_dev
  526.             ' After all is well and done, restore any munged texture stage states
  527.             .SetTextureStageState 1, D3DTSS_COLOROP, D3DTOP_DISABLE
  528.             .SetRenderState D3DRS_AMBIENT, AMBIENT_COLOR
  529.             .SetRenderState D3DRS_FOGCOLOR, WATER_COLOR
  530.             .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  531.         End If
  532. skipcaustic:
  533.         ' End the scene.
  534.         .EndScene
  535.     End With
  536. End Sub
  537. '-----------------------------------------------------------------------------
  538. ' Name: InvalidateDeviceObjects()
  539. ' Desc: Called when the device-dependant objects are about to be lost.
  540. '-----------------------------------------------------------------------------
  541. Public Sub InvalidateDeviceObjects()
  542.     m_FloorObject.InvalidateDeviceObjects
  543.     m_DolphinGroupObject.InvalidateDeviceObjects
  544.     m_DolphinObject.InvalidateDeviceObjects
  545. End Sub
  546. '-----------------------------------------------------------------------------
  547. ' Name: DeleteDeviceObjects()
  548. ' Desc: Called when the app is exitting, or the device is being changed,
  549. '       this function deletes any device dependant objects.
  550. '-----------------------------------------------------------------------------
  551. Public Sub DeleteDeviceObjects()
  552.     m_FloorObject.Destroy
  553.     m_DolphinGroupObject.Destroy
  554.     m_DolphinObject.Destroy
  555.     MESHTOOL_DESTROY m_meshtool(0)
  556.     MESHTOOL_DESTROY m_meshtool(1)
  557.     MESHTOOL_DESTROY m_meshtool(2)
  558.     MESHTOOL_DESTROY m_dest
  559.     Set m_DolphinGroupObject = Nothing
  560.     Set m_DolphinObject = Nothing
  561.     Set m_DolphinMesh = Nothing
  562.     Set m_DolphinMesh01 = Nothing
  563.     Set m_DolphinMesh02 = Nothing
  564.     Set m_DolphinMesh03 = Nothing
  565.     Set m_FloorObject = Nothing
  566.     Set m_SeaFloorMesh = Nothing
  567.     Set m_DolphinTex = Nothing
  568.     ReDim m_CausticTextures(0)
  569.     Set m_CurrentCausticTexture = Nothing    
  570.     m_bInit=False    
  571. End Sub
  572. '-----------------------------------------------------------------------------
  573. ' Name: VerifyDevice()
  574. ' Desc: Called when the app is trying to find valid display modes
  575. '-----------------------------------------------------------------------------
  576. Public Function VerifyDevice(usageflags As Long, format As CONST_D3DFORMAT) As Boolean
  577.     VerifyDevice = True
  578. End Function
  579.