home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8vbsdk.exe / samples / multimedia / vbsamples / direct3d / vertexshader / vertexshader.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-10-09  |  17.4 KB  |  427 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            =   "vertexshader.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. '  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
  21. '  File:       VertexShader.frm
  22. '  Content:    Example code showing how to use vertex shaders in D3D.
  23. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  24. Option Explicit
  25. ' Scene
  26. Dim m_VB As Direct3DVertexBuffer8
  27. Dim m_IB As Direct3DIndexBuffer8
  28. Dim m_NumVertices As Long
  29. Dim m_NumIndices As Long
  30. Dim m_Shader As Long
  31. Dim m_Size As Long
  32. ' Transforms
  33. Dim m_matPosition As D3DMATRIX
  34. Dim m_matView As D3DMATRIX
  35. Dim m_matProj As D3DMATRIX
  36. 'Navigation
  37. Dim m_bKey(256) As Boolean
  38. Dim m_fSpeed As Single
  39. Dim m_fAngularSpeed As Single
  40. Dim m_vVelocity As D3DVECTOR
  41. Dim m_vAngularVelocity As D3DVECTOR
  42. 'Shader
  43. Dim m_Decl(3) As Long
  44. Dim m_ShaderArray() As Long
  45. Dim m_bInit As Boolean                  ' Indicates that d3d has been initialized
  46. Dim m_bMinimized As Boolean             ' Indicates that display window is minimized
  47. '-----------------------------------------------------------------------------
  48. ' Name: Form_Load()
  49. ' Desc:
  50. '-----------------------------------------------------------------------------
  51. Private Sub Form_Load()
  52.     Me.Show
  53.     DoEvents
  54.     'setup defaults
  55.     Init
  56.     ' Initialize D3D
  57.     ' Note: D3DUtil_Init will attempt to use D3D Hardware acceleartion.
  58.     ' If it is not available it attempt to use the Software Reference Rasterizer.
  59.     ' If all fail it will display a message box indicating so.
  60.     '
  61.     m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  62.     If Not (m_bInit) Then End
  63.     ' Create new D3D vertexbuffer objects and vertex shader
  64.     InitDeviceObjects
  65.     ' Sets the state for those objects and the current D3D device
  66.     RestoreDeviceObjects
  67.     ' Start our timer
  68.     DXUtil_Timer TIMER_start
  69.     ' Run the simulation forever
  70.     ' See Form_Keydown for exit processing
  71.     Do While True
  72.         ' Increment the simulation
  73.         FrameMove
  74.         
  75.         ' Render one image of the simulation
  76.         Render
  77.         
  78.         ' Present the image to the screen
  79.         D3DUtil_PresentAll g_focushwnd
  80.         
  81.         ' Allow for events to get processed
  82.         DoEvents
  83.         
  84.     Loop
  85. End Sub
  86. '-----------------------------------------------------------------------------
  87. ' Name: Form_Unload()
  88. ' Desc:
  89. '-----------------------------------------------------------------------------
  90. Private Sub Form_Unload(Cancel As Integer)
  91.     DeleteDeviceObjects
  92.     End
  93. End Sub
  94. '-----------------------------------------------------------------------------
  95. ' Name: Init()
  96. ' Desc: Sets attributes for the app.
  97. '-----------------------------------------------------------------------------
  98. Sub Init()
  99.     Me.Caption = "VertexShader"
  100.     Set m_IB = Nothing
  101.     Set m_VB = Nothing
  102.     m_Size = 32
  103.     m_NumIndices = (m_Size - 1) * (m_Size - 1) * 6
  104.     m_NumVertices = m_Size * m_Size
  105.     m_Shader = 0
  106.     m_fSpeed = 5
  107.     m_fAngularSpeed = 1
  108.     m_vVelocity = vec3(0, 0, 0)
  109.     m_vAngularVelocity = vec3(0, 0, 0)
  110.     ' Setup the view matrix
  111.     Dim veye As D3DVECTOR, vat As D3DVECTOR, vUp As D3DVECTOR
  112.     veye = vec3(2, 3, 3)
  113.     vat = vec3(0, 0, 0)
  114.     vUp = vec3(0, 1, 0)
  115.     D3DXMatrixLookAtRH m_matView, veye, vat, vUp
  116.     ' Set the position matrix
  117.     Dim det As Single
  118.     D3DXMatrixInverse m_matPosition, det, m_matView
  119. End Sub
  120. '-----------------------------------------------------------------------------
  121. ' Name: FrameMove()
  122. ' Desc: Called once per frame, the call is the entry point for animating
  123. '       the scene.
  124. '-----------------------------------------------------------------------------
  125. Sub FrameMove()
  126.     Dim fSecsPerFrame As Single
  127.     Dim fTime As Single
  128.     Dim det As Single
  129.     fSecsPerFrame = DXUtil_Timer(TIMER_GETELLAPSEDTIME)
  130.     fTime = DXUtil_Timer(TIMER_GETAPPTIME)
  131.     ' Process keyboard input
  132.     Dim vT As D3DVECTOR, vR As D3DVECTOR
  133.     vT = vec3(0, 0, 0)
  134.     vR = vec3(0, 0, 0)
  135.     If (m_bKey(vbKeyA) Or m_bKey(vbKeyNumpad1) Or m_bKey(vbKeyLeft)) Then vT.x = vT.x - 1  ' Slide Left
  136.     If (m_bKey(vbKeyD) Or m_bKey(vbKeyNumpad3) Or m_bKey(vbKeyRight)) Then vT.x = vT.x + 1 ' Slide Right
  137.     If (m_bKey(vbKeyDown)) Then vT.y = vT.y - 1                                      ' Slide Down
  138.     If (m_bKey(vbKeyUp)) Then vT.y = vT.y + 1                                        ' Slide Up
  139.     If (m_bKey(vbKeyW)) Then vT.z = vT.z - 2                                         ' Move Forward
  140.     If (m_bKey(vbKeyS)) Then vT.z = vT.z + 2                                         ' Move Backward
  141.     If (m_bKey(vbKeyNumpad8)) Then vR.x = vR.x - 1                                   ' Pitch Down
  142.     If (m_bKey(vbKeyNumpad2)) Then vR.x = vR.x + 1                                   ' Pitch Up
  143.     If (m_bKey(vbKeyE) Or m_bKey(vbKeyNumpad6)) Then vR.y = vR.y - 1                 ' Turn Right
  144.     If (m_bKey(vbKeyQ) Or m_bKey(vbKeyNumpad4)) Then vR.y = vR.y + 1                 ' Turn Left
  145.     If (m_bKey(vbKeyNumpad9)) Then vR.z = vR.z - 2                                   ' Roll CW
  146.     If (m_bKey(vbKeyNumpad7)) Then vR.z = vR.z + 2                                   ' Roll CCW
  147.     m_vVelocity.x = m_vVelocity.x * 0.9 + vT.x * 0.1
  148.     m_vVelocity.y = m_vVelocity.y * 0.9 + vT.y * 0.1
  149.     m_vVelocity.z = m_vVelocity.z * 0.9 + vT.z * 0.1
  150.     m_vAngularVelocity.x = m_vAngularVelocity.x * 0.9 + vR.x * 0.1
  151.     m_vAngularVelocity.y = m_vAngularVelocity.x * 0.9 + vR.y * 0.1
  152.     m_vAngularVelocity.z = m_vAngularVelocity.x * 0.9 + vR.z * 0.1
  153.     ' Update position and view matricies
  154.     Dim matT As D3DMATRIX, matR As D3DMATRIX, qR As D3DQUATERNION
  155.     D3DXVec3Scale vT, m_vVelocity, fSecsPerFrame * m_fSpeed
  156.     D3DXVec3Scale vR, m_vAngularVelocity, fSecsPerFrame * m_fAngularSpeed
  157.     D3DXMatrixTranslation matT, vT.x, vT.y, vT.z
  158.     D3DXMatrixMultiply m_matPosition, matT, m_matPosition
  159.     D3DXQuaternionRotationYawPitchRoll qR, vR.y, vR.x, vR.z
  160.     D3DXMatrixRotationQuaternion matR, qR
  161.     D3DXMatrixMultiply m_matPosition, matR, m_matPosition
  162.     D3DXMatrixInverse m_matView, det, m_matPosition
  163.     g_dev.SetTransform D3DTS_VIEW, m_matView
  164.     ' Set up the vertex shader constants
  165.     Dim mat As D3DMATRIX
  166.     Dim vA As D3DVECTOR4, vD As D3DVECTOR4
  167.     Dim vSin As D3DVECTOR4, vCos As D3DVECTOR4
  168.     D3DXMatrixMultiply mat, m_matView, m_matProj
  169.     D3DXMatrixTranspose mat, mat
  170.     vA = vec4(Sin(fTime) * 15, 0, 0.5, 1)
  171.     vD = vec4(g_pi, 1 / (2 * g_pi), 2 * g_pi, 0.05)
  172.     ' Taylor series coefficients for sin and cos
  173.     vSin = vec4(1, -1 / 6, 1 / 120, -1 / 5040)
  174.     vCos = vec4(1, -1 / 2, 1 / 24, -1 / 720)
  175.     g_dev.SetVertexShaderConstant 0, mat, 4
  176.     g_dev.SetVertexShaderConstant 4, vA, 1
  177.     g_dev.SetVertexShaderConstant 7, vD, 1
  178.     g_dev.SetVertexShaderConstant 10, vSin, 1
  179.     g_dev.SetVertexShaderConstant 11, vCos, 1
  180. End Sub
  181. '-----------------------------------------------------------------------------
  182. ' Name: Render()
  183. ' Desc: Called once per frame, the call is the entry point for 3d
  184. '       rendering. This function sets up render states, clears the
  185. '       viewport, and renders the scene.
  186. '-----------------------------------------------------------------------------
  187. Sub Render()
  188.     Dim v2 As D3DVECTOR2
  189.     Dim hr As Long
  190.     'See what state the device is in.
  191.     hr = g_dev.TestCooperativeLevel
  192.     If hr = D3DERR_DEVICENOTRESET Then
  193.         g_dev.Reset g_d3dpp
  194.         RestoreDeviceObjects
  195.     End If
  196.     'dont bother rendering if we are not ready yet
  197.     If hr <> 0 Then Exit Sub
  198.     'Clear the scene
  199.     D3DUtil_ClearAll &HFF&
  200.     With g_dev
  201.         ' Begin the scene
  202.         .BeginScene
  203.         
  204.         .SetVertexShader m_Shader
  205.         .SetStreamSource 0, m_VB, Len(v2)
  206.         .SetIndices m_IB, 0
  207.         
  208.         .DrawIndexedPrimitive D3DPT_TRIANGLELIST, 0, m_NumVertices, _
  209.                                             0, m_NumIndices / 3
  210.         ' End the scene.
  211.         .EndScene
  212.     End With
  213. End Sub
  214. '-----------------------------------------------------------------------------
  215. ' Name: RestoreDeviceObjects()
  216. ' Desc: Initialize scene objects.
  217. '-----------------------------------------------------------------------------
  218. Sub InitDeviceObjects()
  219.     Dim Indices() As Integer    'Integer are 4 bytes wide in VB
  220.     Dim Vertices() As D3DVECTOR2
  221.     Dim v As D3DVECTOR2, x As Integer, y As Integer, i As Integer
  222.             
  223.     ' Fill in our index array with triangles indices to make a grid
  224.     ReDim Indices(m_NumIndices)
  225.     For y = 1 To m_Size - 1
  226.         For x = 1 To m_Size - 1
  227.             Indices(i) = (y - 1) * m_Size + (x - 1): i = i + 1
  228.             Indices(i) = (y - 0) * m_Size + (x - 1): i = i + 1
  229.             Indices(i) = (y - 1) * m_Size + (x - 0): i = i + 1
  230.             Indices(i) = (y - 1) * m_Size + (x - 0): i = i + 1
  231.             Indices(i) = (y - 0) * m_Size + (x - 1): i = i + 1
  232.             Indices(i) = (y - 0) * m_Size + (x - 0): i = i + 1
  233.         Next
  234.     Next
  235.     ' Create index buffer and copy the VB array into it
  236.     Set m_IB = g_dev.CreateIndexBuffer(m_NumIndices * 2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED)
  237.     D3DIndexBuffer8SetData m_IB, 0, m_NumIndices * 2, 0, Indices(0)
  238.     i = 0
  239.         
  240.     'Fill our vertex array with the coordinates of our grid
  241.     ReDim Vertices(m_NumVertices)
  242.     For y = 0 To m_Size - 1
  243.         For x = 0 To m_Size - 1
  244.             Vertices(i) = vec2(((CSng(x) / CSng(m_Size - 1)) - 0.5) * g_pi, _
  245.                             ((CSng(y) / CSng(m_Size - 1)) - 0.5) * g_pi)
  246.                            
  247.             i = i + 1
  248.         Next
  249.     Next
  250.     ' Create a vertex buffer and copy our vertex array into it
  251.     Set m_VB = g_dev.CreateVertexBuffer(m_NumVertices * Len(v), 0, 0, D3DPOOL_MANAGED)
  252.     D3DVertexBuffer8SetData m_VB, 0, m_NumVertices * Len(v), 0, Vertices(0)
  253.     ' Create vertex shader
  254.     Dim strVertexShaderPath As String
  255.     Dim VShaderCode As D3DXBuffer
  256.     m_Decl(0) = D3DVSD_STREAM(0)
  257.     m_Decl(1) = D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT2)
  258.     m_Decl(2) = D3DVSD_END()
  259.         
  260.     ' Find the vertex shader file
  261.     strVertexShaderPath = FindMediaDir("ripple.vsh") + "ripple.vsh"
  262.     'Assemble the vertex shader from the file
  263.     Set VShaderCode = g_d3dx.AssembleShaderFromFile(strVertexShaderPath, 0, "", Nothing)
  264.             
  265.     'Move VShader code into an array
  266.     ReDim m_ShaderArray(VShaderCode.GetBufferSize() / 4)
  267.     g_d3dx.BufferGetData VShaderCode, 0, 1, VShaderCode.GetBufferSize(), m_ShaderArray(0)
  268.     Set VShaderCode = Nothing
  269. End Sub
  270. '-----------------------------------------------------------------------------
  271. ' Name: RestoreDeviceObjects()
  272. ' Desc: Initialize scene objects.
  273. '-----------------------------------------------------------------------------
  274. Sub RestoreDeviceObjects()
  275.     Dim bufferdesc As D3DSURFACE_DESC
  276.     g_dev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO).GetDesc bufferdesc
  277.     ' Set up right handed projection matrix
  278.     Dim fAspectRatio As Single
  279.     fAspectRatio = bufferdesc.width / bufferdesc.height
  280.     D3DXMatrixPerspectiveFovRH m_matProj, 60 * g_pi / 180, fAspectRatio, 0.1, 100
  281.     g_dev.SetTransform D3DTS_PROJECTION, m_matProj
  282.     ' Setup render states
  283.     g_dev.SetRenderState D3DRS_LIGHTING, 0 'FALSE
  284.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  285.      
  286.     ' Create the vertex shader
  287.     ' NOTE returns value in m_Shader
  288.     g_dev.CreateVertexShader m_Decl(0), m_ShaderArray(0), m_Shader, 0
  289. End Sub
  290. '-----------------------------------------------------------------------------
  291. ' Name: InvalidateDeviceObjects()
  292. ' Desc:
  293. '-----------------------------------------------------------------------------
  294. Sub InvalidateDeviceObjects()
  295.     On Local Error Resume Next
  296.     g_dev.DeleteVertexShader m_Shader    
  297. End Sub
  298. '-----------------------------------------------------------------------------
  299. ' Name: DeleteDeviceObjects()
  300. ' Desc: Called when the app is exitting, or the device is being changed,
  301. '       this function deletes any device dependant objects.
  302. '-----------------------------------------------------------------------------
  303. Sub DeleteDeviceObjects()
  304.     Set m_IB = Nothing
  305.     Set m_VB = Nothing
  306.     InvalidateDeviceObjects
  307.     m_bInit = False
  308. End Sub
  309. '-----------------------------------------------------------------------------
  310. ' Name: FinalCleanup()
  311. ' Desc: Called before the app exits, this function gives the app the chance
  312. '       to cleanup after itself.
  313. '-----------------------------------------------------------------------------
  314. Sub FinalCleanup()
  315. End Sub
  316. '-----------------------------------------------------------------------------
  317. ' Name: ConfirmDevice()
  318. ' Desc: Called during device intialization, this code checks the device
  319. '       for some minimum set of capabilities
  320. '-----------------------------------------------------------------------------
  321. Function VerifyDevice(Behavior As Long, format As CONST_D3DFORMAT) As Boolean
  322.     If (Behavior <> D3DCREATE_SOFTWARE_VERTEXPROCESSING) Then
  323.         If (g_d3dCaps.VertexShaderVersion < D3DVS_VERSION(1, 0)) Then Exit Function
  324.     End If
  325.     VerifyDevice = True
  326. End Function
  327. '-----------------------------------------------------------------------------
  328. ' Name: Form_KeyDown()
  329. ' Desc: Process key messages for exit and change device
  330. '-----------------------------------------------------------------------------
  331. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  332.      Dim hr As Long
  333.      
  334.      m_bKey(KeyCode) = True
  335.      Select Case KeyCode
  336.         
  337.         Case vbKeyEscape
  338.             Unload Me
  339.             
  340.         Case vbKeyF2
  341.                 
  342.             ' Pause the timer
  343.             DXUtil_Timer TIMER_STOP
  344.             
  345.             ' Bring up the device selection dialog
  346.             ' we pass in the form so the selection process
  347.             ' can make calls into InitDeviceObjects
  348.             ' and RestoreDeviceObjects
  349.             frmSelectDevice.SelectDevice Me
  350.             
  351.             ' Restart the timer
  352.             DXUtil_Timer TIMER_start
  353.             
  354.         Case vbKeyReturn
  355.         
  356.             ' Check for Alt-Enter if not pressed exit
  357.             If Shift <> 4 Then Exit Sub
  358.             
  359.             ' If we are windowed go fullscreen
  360.             ' If we are fullscreen returned to windowed
  361.             If g_d3dpp.Windowed Then
  362.                  hr = D3DUtil_ResetFullscreen
  363.             Else
  364.                  hr = D3DUtil_ResetWindowed
  365.             End If
  366.                                                           
  367.             If hr = D3DERR_DEVICELOST Then
  368.                 
  369.                 DeleteDeviceObjects
  370.                 
  371.                 m_bInit = D3DUtil_Init(Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Me)
  372.                 If Not (m_bInit) Then End
  373.                 
  374.                 InitDeviceObjects
  375.             End If
  376.             
  377.             ' Call Restore after ever mode change
  378.             ' because calling reset looses state that needs to
  379.             ' be reinitialized
  380.             RestoreDeviceObjects
  381.            
  382.     End Select
  383. End Sub
  384. '-----------------------------------------------------------------------------
  385. ' Name: Form_Resize()
  386. ' Desc: hadle resizing of the D3D backbuffer
  387. '-----------------------------------------------------------------------------
  388. Private Sub Form_Resize()
  389.     ' If D3D is not initialized then exit
  390.     If Not m_bInit Then Exit Sub
  391.     ' If we are in a minimized state stop the timer and exit
  392.     If Me.WindowState = vbMinimized Then
  393.         DXUtil_Timer TIMER_STOP
  394.         m_bMinimized = True
  395.         Exit Sub
  396.         
  397.     ' If we just went from a minimized state to maximized
  398.     ' restart the timer
  399.     Else
  400.         If m_bMinimized = True Then
  401.             DXUtil_Timer TIMER_start
  402.             m_bMinimized = False
  403.         End If
  404.     End If
  405.     ' Dont let the window get too small
  406.     If Me.ScaleWidth < 10 Then
  407.         Me.width = Screen.TwipsPerPixelX * 10
  408.         Exit Sub
  409.     End If
  410.     If Me.ScaleHeight < 10 Then
  411.         Me.height = Screen.TwipsPerPixelY * 10
  412.         Exit Sub
  413.     End If
  414.         
  415.     'reset and resize our D3D backbuffer to the size of the window
  416.     D3DUtil_ResizeWindowed Me.hwnd
  417.     'All state get losts after a reset so we need to reinitialze it here
  418.     RestoreDeviceObjects
  419. End Sub
  420. '-----------------------------------------------------------------------------
  421. ' Name: Picture1_KeyUp
  422. ' Desc:
  423. '-----------------------------------------------------------------------------
  424. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  425.     m_bKey(KeyCode) = False
  426. End Sub
  427.