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

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Point Sprites"
  4.    ClientHeight    =   4050
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   5055
  8.    Icon            =   "PointSprites.frx":0000
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   270
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   337
  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:       PointSprites.frm
  22. '  Content:    Sample showing how to use point sprites to do particle effects
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. Option Explicit
  25. Private Type CUSTOMVERTEX
  26.     v As D3DVECTOR
  27.     color As Long
  28.     tu As Single
  29.     tv As Single
  30. End Type
  31. Const D3DFVF_COLORVERTEX = (D3DFVF_XYZ Or D3DFVF_DIFFUSE Or D3DFVF_TEX1)
  32. Const GROUND_GRIDSIZE = 8
  33. Const GROUND_WIDTH = 256
  34. Const GROUND_HEIGHT = 256
  35. Const GROUND_TILE = 32
  36. Const GROUND_COLOR = &HBBEEEEEE
  37. Private Enum PARTICLE_COLORS
  38.     COLOR_WHITE = 0
  39.     COLOR_RED = 1
  40.     COLOR_GREEN = 2
  41.     COLOR_BLUE = 3
  42.     NUM_COLORS = 4
  43. End Enum
  44. Dim g_clrColor(4) As D3DCOLORVALUE
  45. Dim g_clrColorFade(4) As D3DCOLORVALUE
  46. Dim m_media As String
  47. Dim m_ParticleSystem As CParticle
  48. Dim m_ParticleTexture As Direct3DTexture8
  49. Dim m_NumParticlesToEmit As Long
  50. Dim m_bStaticParticle As Boolean
  51. Dim m_nParticleColor As Long
  52. Dim m_GroundTexture As Direct3DTexture8
  53. Dim m_NumGroundVertices As Long
  54. Dim m_NumGroundIndices As Long
  55. Dim m_GroundIB As Direct3DIndexBuffer8
  56. Dim m_GroundVB As Direct3DVertexBuffer8
  57. Dim m_planeGround As D3DPLANE
  58. Dim m_bDrawReflection As Boolean
  59. Dim m_bCanDoAlphaBlend  As Boolean
  60. Dim m_bCanDoClipPlanes  As Boolean
  61. Dim m_bDrawHelp As Boolean
  62. Dim m_matView As D3DMATRIX
  63. Dim m_matOrientation As D3DMATRIX
  64. Dim m_vPosition As D3DVECTOR
  65. Dim m_vVelocity As D3DVECTOR
  66. Dim m_fYaw              As Single
  67. Dim m_fYawVelocity      As Single
  68. Dim m_fPitch            As Single
  69. Dim m_fPitchVelocity    As Single
  70. Dim m_fElapsedTime As Single
  71. Dim m_bKey(256) As Boolean
  72. Dim g_fTime As Single
  73. Dim g_fLastTime As Single
  74. Dim m_grVerts() As CUSTOMVERTEX
  75. Dim m_grVerts2() As CUSTOMVERTEX
  76. Dim m_binit As Boolean
  77. Dim m_bMinimized As Boolean
  78. Dim m_bStopSim As Boolean
  79. Const kMaxParticles = 128
  80. Const kParticleRadius = 0.01
  81. '-----------------------------------------------------------------------------
  82. ' Name: Form_Load()
  83. ' Desc:
  84. '-----------------------------------------------------------------------------
  85. Private Sub Form_Load()
  86.     Me.Show
  87.     DoEvents
  88.     'setup defaults
  89.     Init
  90.     ' Initialize D3D
  91.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  92.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  93.     ' If all fail it will display a message box indicating so.
  94.     '
  95.     m_binit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  96.     If Not (m_binit) Then End
  97.     ' find Media and set media path
  98.     m_media = FindMediaDir("ground2.bmp")
  99.     D3DUtil_SetMediaPath m_media
  100.     ' Set initial state
  101.     OneTimeSceneInit
  102.     ' Load Mesh and textures from media
  103.     InitDeviceObjects
  104.     ' Set device render states, lighting, camera
  105.     RestoreDeviceObjects
  106.     ' Start Timer
  107.     DXUtil_Timer TIMER_start
  108.     ' Start our timer
  109.     DXUtil_Timer TIMER_start
  110.     ' Run the simulation forever
  111.     ' See Form_Keydown for exit processing
  112.     Do While True
  113.         ' Increment the simulation
  114.         FrameMove
  115.         
  116.         ' Render one image of the simulation
  117.         Render
  118.         
  119.         ' Present the image to the screen
  120.         D3DUtil_PresentAll g_focushwnd
  121.         
  122.         ' Allow for events to get processed
  123.         DoEvents
  124.         
  125.     Loop
  126. End Sub
  127. '-----------------------------------------------------------------------------
  128. ' Name: Form_KeyDown()
  129. ' Desc: Process key messages for exit and change device
  130. '-----------------------------------------------------------------------------
  131. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  132.      
  133.      m_bKey(KeyCode) = True
  134.      
  135.      Select Case KeyCode
  136.         
  137.         Case vbKeyEscape
  138.             Unload Me
  139.             
  140.         Case vbKeyF2
  141.                 
  142.             ' Pause the timer
  143.             DXUtil_Timer TIMER_STOP
  144.             m_bStopSim = True
  145.             ' Bring up the device selection dialog
  146.             ' we pass in the form so the selection process
  147.             ' can make calls into InitDeviceObjects
  148.             ' and RestoreDeviceObjects
  149.             frmSelectDevice.SelectDevice Me
  150.             
  151.             ' Restart the timer
  152.             m_bStopSim = False
  153.             DXUtil_Timer TIMER_start
  154.             
  155.         Case vbKeyReturn
  156.         
  157.             ' Check for Alt-Enter if not pressed exit
  158.             If Shift <> 4 Then Exit Sub
  159.             
  160.             ' stop simulation
  161.             DXUtil_Timer TIMER_STOP
  162.             m_bStopSim = True
  163.             
  164.             ' If we are windowed go fullscreen
  165.             ' If we are fullscreen returned to windowed
  166.             If g_d3dpp.Windowed Then
  167.                  D3DUtil_ResetFullscreen
  168.             Else
  169.                  D3DUtil_ResetWindowed
  170.             End If
  171.                              
  172.             ' Call Restore after ever mode change
  173.             ' because calling reset looses state that needs to
  174.             ' be reinitialized
  175.             RestoreDeviceObjects
  176.            
  177.             ' Restart simulation
  178.             DXUtil_Timer TIMER_STOP
  179.             m_bStopSim = False
  180.     End Select
  181. End Sub
  182. '-----------------------------------------------------------------------------
  183. ' Name: Form_KeyUp()
  184. ' Desc: Process key messages for exit and change device
  185. '-----------------------------------------------------------------------------
  186. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  187.     m_bKey(KeyCode) = False
  188. End Sub
  189. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  190.     DXUtil_Timer (TIMER_STOP)
  191.     m_bStopSim = True
  192. End Sub
  193. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  194.    DXUtil_Timer (TIMER_start)
  195.     m_bStopSim = False
  196. End Sub
  197. '-----------------------------------------------------------------------------
  198. ' Name: Form_Resize()
  199. ' Desc: hadle resizing of the D3D backbuffer
  200. '-----------------------------------------------------------------------------
  201. Private Sub Form_Resize()
  202.     ' If D3D is not initialized then exit
  203.     If Not m_binit Then Exit Sub
  204.     ' If we are in a minimized state stop the timer and exit
  205.     If Me.WindowState = vbMinimized Then
  206.         DXUtil_Timer TIMER_STOP
  207.         m_bMinimized = True
  208.         m_bStopSim = True
  209.         Exit Sub
  210.         
  211.     ' If we just went from a minimized state to maximized
  212.     ' restart the timer
  213.     Else
  214.         If m_bMinimized = True Then
  215.             DXUtil_Timer TIMER_start
  216.             m_bMinimized = False
  217.             m_bStopSim = False
  218.         End If
  219.     End If
  220.             
  221.     ' Dont let the window get too small
  222.     If Me.ScaleWidth < 10 Then
  223.         Me.width = Screen.TwipsPerPixelX * 10
  224.         Exit Sub
  225.     End If
  226.     If Me.ScaleHeight < 10 Then
  227.         Me.height = Screen.TwipsPerPixelY * 10
  228.         Exit Sub
  229.     End If
  230.     m_ParticleSystem.DeleteDeviceObjects
  231.     Set m_ParticleSystem = Nothing
  232.     Set m_ParticleSystem = New CParticle
  233.     'reset and resize our D3D backbuffer to the size of the window
  234.     D3DUtil_ResizeWindowed Me.hwnd
  235.     'All state get losts after a reset so we need to reinitialze it here
  236.     RestoreDeviceObjects
  237.     DXUtil_Timer TIMER_STOP
  238.     m_ParticleSystem.Init kMaxParticles, kParticleRadius
  239.     m_ParticleSystem.InitDeviceObjects g_dev
  240.     DXUtil_Timer TIMER_RESET
  241. End Sub
  242. '-----------------------------------------------------------------------------
  243. ' Name: Form_Unload()
  244. ' Desc:
  245. '-----------------------------------------------------------------------------
  246. Private Sub Form_Unload(Cancel As Integer)
  247.     DeleteDeviceObjects
  248.     End
  249. End Sub
  250. '-----------------------------------------------------------------------------
  251. ' Name: Init()
  252. ' Desc: Constructor
  253. '-----------------------------------------------------------------------------
  254. Sub Init()
  255.     Me.Caption = "PointSprites: Using particle effects"
  256.         
  257.        
  258.     Set m_ParticleSystem = New CParticle
  259.     m_ParticleSystem.Init kMaxParticles, kParticleRadius
  260.     Set m_ParticleTexture = Nothing
  261.     m_NumParticlesToEmit = 10
  262.     m_bStaticParticle = True
  263.     m_nParticleColor = COLOR_WHITE
  264.     Set m_GroundTexture = Nothing
  265.     m_NumGroundVertices = (GROUND_GRIDSIZE + 1) * (GROUND_GRIDSIZE + 1)
  266.     m_NumGroundIndices = (GROUND_GRIDSIZE * GROUND_GRIDSIZE) * 6
  267.     Set m_GroundVB = Nothing
  268.     Set m_GroundIB = Nothing
  269.     m_planeGround = D3DPLANE4(0, 1, 0, 0)
  270.     m_bDrawReflection = False
  271.     m_bCanDoAlphaBlend = False
  272.     m_bCanDoClipPlanes = False
  273.     m_bDrawHelp = False
  274.     m_vPosition = vec3(0, 3, -4)
  275.     m_vVelocity = vec3(0, 0, 0)
  276.     m_fYaw = 0
  277.     m_fYawVelocity = 0
  278.     m_fPitch = 0.5
  279.     m_fPitchVelocity = 0
  280.     g_clrColor(0) = ColorValue4(1, 1, 1, 1)
  281.     g_clrColor(1) = ColorValue4(1, 0.5, 0.5, 1)
  282.     g_clrColor(2) = ColorValue4(0.5, 1, 0.5, 1)
  283.     g_clrColor(3) = ColorValue4(0.125, 0.5, 1, 1)
  284.     g_clrColorFade(0) = ColorValue4(1, 0.25, 0.25, 1)
  285.     g_clrColorFade(1) = ColorValue4(1, 0.25, 0.25, 1)
  286.     g_clrColorFade(2) = ColorValue4(0.25, 0.75, 0.25, 1)
  287.     g_clrColorFade(3) = ColorValue4(0.125, 0.25, 0.75, 1)
  288. End Sub
  289. '-----------------------------------------------------------------------------
  290. ' Name: OneTimeSceneInit()
  291. ' Desc: Called during initial app startup, this function performs all the
  292. '       permanent initialization.
  293. '-----------------------------------------------------------------------------
  294. Sub OneTimeSceneInit()
  295.     D3DXMatrixTranslation m_matView, 0, 0, 10
  296.     D3DXMatrixTranslation m_matOrientation, 0, 0, 0
  297. End Sub
  298. '-----------------------------------------------------------------------------
  299. ' Name: FrameMove()
  300. ' Desc: Called once per frame, the call is the entry point for animating
  301. '       the scene.
  302. '-----------------------------------------------------------------------------
  303. Sub FrameMove()
  304.             
  305.     If m_bStopSim = True Then Exit Sub
  306.         
  307.     g_fTime = DXUtil_Timer(TIMER_GETAPPTIME) * 1.3
  308.     m_fElapsedTime = g_fTime - g_fLastTime
  309.     g_fLastTime = g_fTime
  310.     If m_fElapsedTime < 0 Then Exit Sub
  311.         
  312.     ' Slow things down for the REF device
  313.     If (g_devType = D3DDEVTYPE_REF) Then m_fElapsedTime = 0.05
  314.     Dim fSpeed As Single
  315.     Dim fAngularSpeed
  316.     fSpeed = 5 * m_fElapsedTime
  317.     fAngularSpeed = 1 * m_fElapsedTime
  318.     ' Slowdown the camera movement
  319.     D3DXVec3Scale m_vVelocity, m_vVelocity, 0.9
  320.     m_fYawVelocity = m_fYawVelocity * 0.9
  321.     m_fPitchVelocity = m_fPitchVelocity * 0.9
  322.     ' Process keyboard input
  323.     If (m_bKey(vbKeyRight)) Then m_vVelocity.x = m_vVelocity.x + fSpeed        '  Slide Right
  324.     If (m_bKey(vbKeyLeft)) Then m_vVelocity.x = m_vVelocity.x - fSpeed         '  Slide Left
  325.     If (m_bKey(vbKeyUp)) Then m_vVelocity.y = m_vVelocity.y + fSpeed           '  Move up
  326.     If (m_bKey(vbKeyDown)) Then m_vVelocity.y = m_vVelocity.y - fSpeed         '  Move down
  327.     If (m_bKey(vbKeyW)) Then m_vVelocity.z = m_vVelocity.z + fSpeed            '  Move Forward
  328.     If (m_bKey(vbKeyS)) Then m_vVelocity.z = m_vVelocity.z - fSpeed            '  Move Backward
  329.     If (m_bKey(vbKeyE)) Then m_fYawVelocity = m_fYawVelocity + fSpeed          '  Yaw right
  330.     If (m_bKey(vbKeyQ)) Then m_fYawVelocity = m_fYawVelocity - fSpeed          '  Yaw left
  331.     If (m_bKey(vbKeyZ)) Then m_fPitchVelocity = m_fPitchVelocity + fSpeed      '  turn down
  332.     If (m_bKey(vbKeyA)) Then m_fPitchVelocity = m_fPitchVelocity - fSpeed      '  turn up
  333.     If (m_bKey(vbKeyAdd)) Then
  334.         If (m_NumParticlesToEmit < 10) Then m_NumParticlesToEmit = m_NumParticlesToEmit + 1
  335.     End If
  336.     If (m_bKey(vbKeySubtract)) Then
  337.         If (m_NumParticlesToEmit > 0) Then m_NumParticlesToEmit = m_NumParticlesToEmit - 1
  338.     End If
  339.     ' Update the position vector
  340.     Dim vT As D3DVECTOR, vTemp As D3DVECTOR
  341.     D3DXVec3Scale vTemp, m_vVelocity, fSpeed
  342.     D3DXVec3Add vT, vT, vTemp
  343.     D3DXVec3TransformNormal vT, vT, m_matOrientation
  344.     D3DXVec3Add m_vPosition, m_vPosition, vT
  345.     If (m_vPosition.y < 1) Then m_vPosition.y = 1
  346.     ' Update the yaw-pitch-rotation vector
  347.     m_fYaw = m_fYaw + fAngularSpeed * m_fYawVelocity
  348.     m_fPitch = m_fPitch + fAngularSpeed * m_fPitchVelocity
  349.     If (m_fPitch < 0) Then m_fPitch = 0
  350.     If (m_fPitch > g_pi / 2) Then m_fPitch = g_pi / 2
  351.     Dim qR As D3DQUATERNION, det As Single
  352.     D3DXQuaternionRotationYawPitchRoll qR, m_fYaw, m_fPitch, 0
  353.     D3DXMatrixAffineTransformation m_matOrientation, 1.25, vec3(0, 0, 0), qR, m_vPosition
  354.     D3DXMatrixInverse m_matView, det, m_matOrientation
  355.     ' Update particle system
  356.     If (m_bStaticParticle) Then
  357.         m_ParticleSystem.Update m_fElapsedTime, m_NumParticlesToEmit, _
  358.                          g_clrColor(m_nParticleColor), _
  359.                          g_clrColorFade(m_nParticleColor), 8, _
  360.                          vec3(0, 0, 0)
  361.     Else
  362.         m_ParticleSystem.Update m_fElapsedTime, m_NumParticlesToEmit, _
  363.                          g_clrColor(m_nParticleColor), _
  364.                          g_clrColorFade(m_nParticleColor), 8, _
  365.                          vec3(3 * Sin(g_fTime), 0, 3 * Cos(g_fTime))
  366.     End If
  367. End Sub
  368. '-----------------------------------------------------------------------------
  369. ' Name: Render()
  370. ' Desc: Called once per frame, the call is the entry point for 3d
  371. '       rendering. This function sets up render states, clears the
  372. '       viewport, and renders the scene.
  373. '-----------------------------------------------------------------------------
  374. Sub Render()
  375.     Dim v As CUSTOMVERTEX
  376.     Dim hr As Long
  377.      'See what state the device is in.
  378.     hr = g_dev.TestCooperativeLevel
  379.     If hr = D3DERR_DEVICENOTRESET Then
  380.         g_dev.Reset g_d3dpp
  381.         RestoreDeviceObjects
  382.     End If
  383.     'dont bother rendering if we are not ready yet
  384.     If hr <> 0 Then Exit Sub
  385.     ' Clear the backbuffer
  386.     D3DUtil_ClearAll &HFF&
  387.     With g_dev
  388.         .BeginScene
  389.                 
  390.         
  391.         ' Draw reflection of particles
  392.         m_bDrawReflection = False
  393.         If (m_bDrawReflection) Then
  394.             Dim matReflectedView As D3DMATRIX
  395.             
  396.             D3DXMatrixReflect matReflectedView, m_planeGround
  397.             D3DXMatrixMultiply matReflectedView, matReflectedView, m_matView
  398.             .SetTransform D3DTS_VIEW, matReflectedView
  399.             'Dim clipplane As D3DCLIPPLANE
  400.             'LSet clipplane = m_planeGround
  401.             '.SetClipPlane 0, clipplane
  402.             .SetRenderState D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0
  403.             ' Draw particles
  404.             .SetTexture 0, m_ParticleTexture
  405.             .SetRenderState D3DRS_ZWRITEENABLE, 0 'FALSE
  406.             .SetRenderState D3DRS_ALPHABLENDENABLE, 1 'TRUE
  407.             m_ParticleSystem.Render g_dev
  408.             .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  409.             .SetRenderState D3DRS_ZWRITEENABLE, 1 'True
  410.             .SetRenderState D3DRS_CLIPPLANEENABLE, 0 'FALSE
  411.             .SetRenderState D3DRS_ALPHABLENDENABLE, 1 'True
  412.             .SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
  413.             .SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
  414.             .SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
  415.         End If
  416.         .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  417.         .SetRenderState D3DRS_ZWRITEENABLE, 1 'True
  418.         .SetRenderState D3DRS_CLIPPLANEENABLE, 0 'FALSE
  419.         .SetRenderState D3DRS_ALPHABLENDENABLE, 1 '1 'True
  420.         .SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
  421.         .SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
  422.         .SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
  423.         
  424.         
  425.         ' Draw ground
  426.         .SetTransform D3DTS_VIEW, m_matView
  427.         .SetTexture 0, m_GroundTexture
  428.         .SetVertexShader D3DFVF_COLORVERTEX
  429.         .SetStreamSource 0, m_GroundVB, Len(v)
  430.         .SetIndices m_GroundIB, 0
  431.         .DrawIndexedPrimitive D3DPT_TRIANGLELIST, _
  432.                                             0, m_NumGroundVertices, _
  433.                                             0, (m_NumGroundIndices / 3)
  434.         ' Draw particles
  435.         .SetRenderState D3DRS_ALPHABLENDENABLE, 1 'True
  436.         .SetRenderState D3DRS_SRCBLEND, D3DBLEND_ONE
  437.         .SetRenderState D3DRS_DESTBLEND, D3DBLEND_ONE
  438.         .SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1
  439.         .SetRenderState D3DRS_ZWRITEENABLE, 0 'False
  440.         .SetRenderState D3DRS_ZENABLE, 0 'False
  441.         .SetTexture 0, m_ParticleTexture
  442.         .SetRenderState D3DRS_ZENABLE, 1 'TRUE
  443.         .SetTexture 0, m_ParticleTexture
  444.         m_ParticleSystem.Render g_dev
  445.         .SetRenderState D3DRS_ALPHABLENDENABLE, 0 'False
  446.         .SetRenderState D3DRS_ZWRITEENABLE, 1 'True
  447.         .EndScene
  448.     End With
  449. End Sub
  450. '-----------------------------------------------------------------------------
  451. ' Name: InitDeviceObjects()
  452. ' Desc: Initialize scene objects.
  453. '-----------------------------------------------------------------------------
  454. Function InitDeviceObjects() As Boolean
  455.     Dim i As Long
  456.     Dim v As CUSTOMVERTEX
  457.     Set m_GroundTexture = D3DUtil_CreateTexture(g_dev, "Ground2.bmp", D3DFMT_UNKNOWN)
  458.     Set m_ParticleTexture = D3DUtil_CreateTexture(g_dev, "Particle.bmp", D3DFMT_UNKNOWN)
  459.           
  460.     ' Check if we can do the reflection effect
  461.     m_bCanDoAlphaBlend = ((g_d3dCaps.SrcBlendCaps And D3DPBLENDCAPS_SRCALPHA) = D3DPBLENDCAPS_SRCALPHA) And _
  462.                          ((g_d3dCaps.DestBlendCaps And D3DPBLENDCAPS_INVSRCALPHA) = D3DPBLENDCAPS_INVSRCALPHA)
  463.     m_bCanDoClipPlanes = (g_d3dCaps.MaxUserClipPlanes >= 1)
  464.     ' Note: all HW with Software Vertex Processing can do clipplanes
  465.     m_bCanDoClipPlanes = True
  466.         
  467.     If (m_bCanDoAlphaBlend And m_bCanDoClipPlanes) Then m_bDrawReflection = True
  468.     ' Create ground object
  469.         
  470.     ' Create vertex buffer for ground object
  471.     Set m_GroundVB = g_dev.CreateVertexBuffer(m_NumGroundVertices * Len(v), _
  472.                       0, D3DFVF_COLORVERTEX, D3DPOOL_MANAGED)
  473.         
  474.     ' Fill vertex buffer
  475.      Dim zz As Long, xx As Long
  476.      
  477.      ReDim m_grVerts(GROUND_GRIDSIZE * GROUND_GRIDSIZE * 6)
  478.      
  479.      
  480.      i = 0
  481.      For zz = 0 To GROUND_GRIDSIZE
  482.         For xx = 0 To GROUND_GRIDSIZE
  483.             
  484.             m_grVerts(i).v.x = GROUND_WIDTH * ((xx / GROUND_GRIDSIZE) - 0.5)
  485.             m_grVerts(i).v.y = 0
  486.             m_grVerts(i).v.z = GROUND_HEIGHT * ((zz / GROUND_GRIDSIZE) - 0.5)
  487.             m_grVerts(i).color = GROUND_COLOR
  488.             m_grVerts(i).tu = xx * (GROUND_TILE / GROUND_GRIDSIZE)
  489.             m_grVerts(i).tv = zz * (GROUND_TILE / GROUND_GRIDSIZE)
  490.             i = i + 1
  491.         Next
  492.     Next
  493.     D3DVertexBuffer8SetData m_GroundVB, 0, Len(v) * (GROUND_GRIDSIZE + 1) * (GROUND_GRIDSIZE + 1), 0, m_grVerts(0)
  494.     Dim vtx As Long
  495.     Dim m_Indices() As Integer
  496.     ReDim m_Indices(m_NumGroundIndices * 4)
  497.     Dim z As Long, x As Long
  498.     ' Create the index buffer
  499.     Set m_GroundIB = g_dev.CreateIndexBuffer(m_NumGroundIndices * 2, _
  500.                             0, _
  501.                             D3DFMT_INDEX16, D3DPOOL_MANAGED)
  502.             
  503.     ' Fill in indices
  504.     i = 0
  505.     For z = 0 To GROUND_GRIDSIZE - 1
  506.         For x = 0 To GROUND_GRIDSIZE - 1
  507.                 
  508.                 vtx = x + z * (GROUND_GRIDSIZE + 1)
  509.                 m_Indices(i) = vtx + 1: i = i + 1
  510.                 m_Indices(i) = vtx + 0: i = i + 1
  511.                 m_Indices(i) = vtx + 0 + (GROUND_GRIDSIZE + 1): i = i + 1
  512.                 m_Indices(i) = vtx + 1: i = i + 1
  513.                 m_Indices(i) = vtx + 0 + (GROUND_GRIDSIZE + 1): i = i + 1
  514.                 m_Indices(i) = vtx + 1 + (GROUND_GRIDSIZE + 1): i = i + 1
  515.             
  516.         Next
  517.     Next
  518.     D3DIndexBuffer8SetData m_GroundIB, 0, 2 * m_NumGroundIndices, 0, m_Indices(0)
  519.     ' Initialize the particle system
  520.     m_ParticleSystem.InitDeviceObjects g_dev
  521.         
  522.     InitDeviceObjects = True
  523. End Function
  524. '-----------------------------------------------------------------------------
  525. ' Name: VerifyDevice()
  526. '-----------------------------------------------------------------------------
  527. Function VerifyDevice(Behavior As Long, format As CONST_D3DFORMAT) As Boolean
  528.     ' Make sure device can do ONE:ONE alphablending
  529.     If (0 = (g_d3dCaps.SrcBlendCaps And D3DPBLENDCAPS_ONE) = D3DPBLENDCAPS_ONE) Then Exit Function
  530.     If (0 = (g_d3dCaps.DestBlendCaps And D3DPBLENDCAPS_ONE) = D3DPBLENDCAPS_ONE) Then Exit Function
  531.         
  532.     ' We will run this app using software vertex processing
  533.     If (Behavior = D3DCREATE_HARDWARE_VERTEXPROCESSING) Then Exit Function
  534.     VerifyDevice = True
  535. End Function
  536. '-----------------------------------------------------------------------------
  537. ' Name: DeleteDeviceObjects()
  538. ' Desc: Called when the app is exitting, or the device is being changed,
  539. '       this function deletes any device dependant objects.
  540. '-----------------------------------------------------------------------------
  541. Sub DeleteDeviceObjects()
  542.     Set m_GroundTexture = Nothing
  543.     Set m_ParticleTexture = Nothing
  544.     Set m_GroundVB = Nothing
  545.     Set m_GroundIB = Nothing
  546.     If (m_ParticleSystem Is Nothing) Then Exit Sub
  547.     m_ParticleSystem.DeleteDeviceObjects
  548.     m_binit = False
  549. End Sub
  550. '-----------------------------------------------------------------------------
  551. ' Name: FinalCleanup()
  552. ' Desc: Called before the app exits, this function gives the app the chance
  553. '       to cleanup after itself.
  554. '-----------------------------------------------------------------------------
  555. Sub FinalCleanup()
  556.     Set m_GroundTexture = Nothing
  557.     Set m_ParticleTexture = Nothing
  558.     Set m_ParticleSystem = Nothing
  559. End Sub
  560. '-----------------------------------------------------------------------------
  561. ' Name: InvalidateDeviceObjects()
  562. ' Desc: Place code to release non managed objects here
  563. '-----------------------------------------------------------------------------
  564. Sub InvalidateDeviceObjects()
  565.     'all objects are managed in this sample
  566. End Sub
  567. '-----------------------------------------------------------------------------
  568. ' Name: RestoreDeviceObjects()
  569. ' Desc:
  570. '-----------------------------------------------------------------------------
  571. Sub RestoreDeviceObjects()
  572.     ' Set the world matrix
  573.     Dim matWorld As D3DMATRIX
  574.     D3DXMatrixIdentity matWorld
  575.     g_dev.SetTransform D3DTS_WORLD, matWorld
  576.     ' Set projection matrix
  577.     Dim matProj As D3DMATRIX
  578.     D3DXMatrixPerspectiveFovLH matProj, g_pi / 4, Me.ScaleHeight / Me.ScaleWidth, 0.1, 100
  579.     g_dev.SetTransform D3DTS_PROJECTION, matProj
  580.     ' Set renderstates
  581.     With g_dev
  582.         Call .SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR)
  583.         Call .SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR)
  584.         Call .SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR)
  585.         Call .SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE)
  586.         Call .SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1)
  587.         Call .SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE)
  588.         Call .SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE)
  589.         Call .SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE)
  590.         Call .SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE)
  591.         Call .SetRenderState(D3DRS_LIGHTING, 0)     'FALSE
  592.         Call .SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)
  593.         Call .SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT)
  594.     End With
  595. End Sub
  596.