home *** CD-ROM | disk | FTP | other *** search
/ Microsoft DirectX SDK 7.0 / Dx7.bin / DXF / samples / multimedia / vbsamples / d3dim / src / tutorials / texture / texture.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-23  |  14.7 KB  |  349 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "TextureVB"
  5.    ClientHeight    =   3195
  6.    ClientLeft      =   90
  7.    ClientTop       =   660
  8.    ClientWidth     =   3720
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MinButton       =   0   'False
  12.    ScaleHeight     =   213
  13.    ScaleMode       =   0  'User
  14.    ScaleWidth      =   254
  15.    StartUpPosition =   3  'Windows Default
  16. Attribute VB_Name = "Form1"
  17. Attribute VB_GlobalNameSpace = False
  18. Attribute VB_Creatable = False
  19. Attribute VB_PredeclaredId = True
  20. Attribute VB_Exposed = False
  21. Option Explicit
  22. Const pi As Single = 3.141592
  23. ' Define the cube.
  24. Const NUM_CUBE_VERTICES As Integer = 4 * 6
  25. Dim g_vCube(NUM_CUBE_VERTICES) As D3DVERTEX
  26. ' Textures for the cube.
  27. Dim g_ddsTexture1 As DirectDrawSurface7, _
  28.     g_ddsTexture2 As DirectDrawSurface7, _
  29.     g_ddsTexture3 As DirectDrawSurface7
  30. Dim g_dx As New DirectX7
  31. Dim g_dd As DirectDraw7
  32. Dim g_ddsd As DDSURFACEDESC2
  33. Dim g_ddsPrimary As DirectDrawSurface7, _
  34.     g_ddsBackBuffer As DirectDrawSurface7
  35. Dim g_d3dDevice As Direct3DDevice7
  36. Dim g_rcDest As RECT, _
  37.     g_rcSrc As RECT
  38. Dim g_d3drcViewport(0) As D3DRECT
  39. Dim g_bRunning As Boolean
  40. Private Sub Form_Load()
  41.     Dim CNT As Single
  42.     Dim j As Long
  43.     ' Initialize the DirectDraw and Direct3D objects that this
  44.     ' sample application will use to render and display the triangle.
  45.     InitDDraw
  46.     InitD3D
  47.     InitDeviceObjects
  48.     Me.Show
  49.     g_bRunning = True
  50.     Do While g_bRunning = True
  51.         CNT = CNT + 1
  52.         RenderScene
  53.         FrameMove (CNT / 360)
  54.         
  55.         g_dx.GetWindowRect Me.hWnd, g_rcDest
  56.         j = g_ddsPrimary.Blt(g_rcDest, g_ddsBackBuffer, g_rcSrc, DDBLT_WAIT)
  57.         If j <> DD_OK Then
  58.             MsgBox "Couldn't copy the source rectangle to the destination surface." & Chr$(13) & Hex(j)
  59.             End
  60.         End If
  61.         DoEvents
  62.     Loop
  63. End Sub
  64. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  65. '   Animate the scene.
  66. '   Called once per frame, the call is used for animating the scene. The device is
  67. '   used for changing various render states, and the stepVal parameter is used for
  68. '   the timing of the dynamics of the scene.
  69. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  70. Private Sub FrameMove(stepVal As Single)
  71.     ' Set the view matrix so that the camera is backed out along the z-axis, and looks
  72.     ' down on the cube (rotating along the x-axis by -0.5 radians).
  73.     Dim matView  As D3DMATRIX
  74.     g_dx.IdentityMatrix matView
  75.     matView.rc11 = 1
  76.     matView.rc22 = Cos(-0.5)
  77.     matView.rc23 = Sin(-0.5)
  78.     matView.rc32 = -Sin(-0.5)
  79.     matView.rc33 = Cos(-0.5)
  80.     matView.rc43 = 5
  81.     matView.rc44 = 1
  82.             
  83.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_VIEW, matView
  84.     ' Set the world matrix to rotate along the y-axis
  85.     Dim matWorld As D3DMATRIX
  86.     g_dx.IdentityMatrix matWorld
  87.     g_dx.RotateYMatrix matWorld, stepVal
  88.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_WORLD, matWorld
  89.       
  90. End Sub
  91. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  92. ' Render the scene
  93. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  94. Private Sub RenderScene()
  95.     Dim i As Integer
  96.           
  97.     ' Clear the viewport to a blue color, and clear the z-buffer.
  98.     g_d3dDevice.Clear 1, g_d3drcViewport(), D3DCLEAR_TARGET, &HFF, 1, 0
  99.     ' Begin the scene.
  100.     g_d3dDevice.BeginScene
  101.     ' Draw the front and back faces of the cube using texture 1
  102.     g_d3dDevice.SetTexture 0, g_ddsTexture1
  103.     Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, g_vCube(0), _
  104.          4, D3DDP_DEFAULT)
  105.     Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, g_vCube(4), _
  106.          4, D3DDP_DEFAULT)
  107.          
  108.     ' Draw the top and bottom faces of the cube using texture 2
  109.     g_d3dDevice.SetTexture 0, g_ddsTexture2
  110.     Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, g_vCube(8), _
  111.          4, D3DDP_DEFAULT)
  112.     Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, g_vCube(12), _
  113.          4, D3DDP_DEFAULT)
  114.     ' Draw the left and right faces of the cube using texture 3
  115.     g_d3dDevice.SetTexture 0, g_ddsTexture3
  116.     Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, g_vCube(16), _
  117.          4, D3DDP_DEFAULT)
  118.     Call g_d3dDevice.DrawPrimitive(D3DPT_TRIANGLESTRIP, D3DFVF_VERTEX, g_vCube(20), _
  119.          4, D3DDP_DEFAULT)
  120.     ' End the scene.
  121.     g_d3dDevice.EndScene
  122. End Sub
  123. Private Sub Form_Unload(Cancel As Integer)
  124.     g_bRunning = False
  125. End Sub
  126. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  127. ' Initalize DirectDraw.
  128. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  129. Private Sub InitDDraw()
  130.     ' Create the DirectDraw object and set the application
  131.     ' cooperative level.
  132.     Set g_dd = g_dx.DirectDrawCreate("")
  133.     g_dd.SetCooperativeLevel Me.hWnd, DDSCL_NORMAL
  134.     ' Prepare and create the primary surface.
  135.     g_ddsd.lFlags = DDSD_CAPS
  136.     g_ddsd.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  137.     Set g_ddsPrimary = g_dd.CreateSurface(g_ddsd)
  138.     ' Now create the render-target surface. We are reusing g_ddsd here.
  139.     g_ddsd.lFlags = DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_CAPS
  140.     g_ddsd.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE
  141.     ' Use the size of the form to determine the size of the render target
  142.     ' and viewport rectangle.
  143.     g_dx.GetWindowRect Me.hWnd, g_rcDest
  144.     ' Set the dimensions of the surface description
  145.     g_ddsd.lWidth = g_rcDest.Right - g_rcDest.Left
  146.     g_ddsd.lHeight = g_rcDest.Bottom - g_rcDest.Top
  147.     ' Create the render-target surface
  148.     Set g_ddsBackBuffer = g_dd.CreateSurface(g_ddsd)
  149.         
  150.     ' Cache the dimensions of the render target. We'll use
  151.     ' it for blitting operations.
  152.     With g_rcSrc
  153.         .Left = 0: .Top = 0
  154.         .Bottom = g_ddsd.lHeight
  155.         .Right = g_ddsd.lWidth
  156.     End With
  157.     ' Create a DirectDrawClipper and attach it to the primary surface.
  158.     Dim pcClipper As DirectDrawClipper
  159.     Set pcClipper = g_dd.CreateClipper(0)
  160.     pcClipper.SetHWnd Me.hWnd
  161.     g_ddsPrimary.SetClipper pcClipper
  162. End Sub
  163. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  164. ' Initalize Direct3D, including the rendering device, lighting,
  165. ' the viewport, and the material.
  166. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  167. Sub InitD3D()
  168.     Dim d3d As Direct3D7
  169.     Dim ddsd As DDSURFACEDESC2
  170.         
  171.     ' Retrieve a reference to the Direct3D7 class from the
  172.     ' DirectDraw7 object.
  173.     Set d3d = g_dd.GetDirect3D
  174.     ' Query DirectDraw for the current display mode. For simplicity, this
  175.     ' does not support palleted display modes (8-bit and lower).
  176.     g_dd.GetDisplayMode ddsd
  177.     If ddsd.ddpfPixelFormat.lRGBBitCount <= 8 Then
  178.         MsgBox "This application does not support screen display " & _
  179.                "modes lower than 16-bit."
  180.         End
  181.     End If
  182.     '
  183.     ' Create the device. The GUID is hardcoded for now, but should come from
  184.     ' device enumeration, which is the topic of a tutorial. The device
  185.     ' is created off of our back buffer, which becomes the render target for
  186.     ' the newly created device.
  187.     '
  188.     On Error Resume Next
  189.     Set g_d3dDevice = d3d.CreateDevice("IID_IDirect3DHALDevice", g_ddsBackBuffer)
  190.     If g_d3dDevice Is Nothing Then
  191.         Set g_d3dDevice = d3d.CreateDevice("IID_IDirect3DRGBDevice", g_ddsBackBuffer)
  192.     End If
  193.     ' Define the viewport rectangle.
  194.     Dim VPDesc As D3DVIEWPORT7
  195.         
  196.     VPDesc.lWidth = g_rcDest.Right - g_rcDest.Left
  197.     VPDesc.lHeight = g_rcDest.Bottom - g_rcDest.Top
  198.     VPDesc.minz = 0#
  199.     VPDesc.maxz = 1#
  200.     g_d3dDevice.SetViewport VPDesc
  201.     ' Cache the viewport rectangle for use in clearing operations later.
  202.     With g_d3drcViewport(0)
  203.         .X1 = 0: .Y1 = 0
  204.         .X2 = VPDesc.lWidth
  205.         .Y2 = VPDesc.lHeight
  206.     End With
  207.         
  208.  End Sub
  209. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  210. ' Initalize the geometry.
  211. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  212. Private Sub InitDeviceObjects()
  213.     ' Generate the vertices for the cube.
  214.     CreateCube g_vCube
  215.     ' Set our directory for the bitmap images to be used as textures.
  216.     FindMediaDir "tree1.bmp"
  217.      
  218.     ' Create textures from file-based bitmaps.
  219.     Set g_ddsTexture1 = CreateTextureSurface("tree1.bmp")
  220.     Set g_ddsTexture2 = CreateTextureSurface("tex1.bmp")
  221.     Set g_ddsTexture3 = CreateTextureSurface("earth.bmp")
  222.    ' For simplicity, use ambient lighting and a white material.
  223.     Dim mtrl As D3DMATERIAL7
  224.     mtrl.diffuse.r = 1#: mtrl.diffuse.g = 1#: mtrl.diffuse.b = 1#
  225.     mtrl.Ambient.r = 1#: mtrl.Ambient.g = 1#: mtrl.Ambient.b = 1#
  226.     ' Commit the material to the device.
  227.     g_d3dDevice.SetMaterial mtrl
  228.     ' Enable ambient lighting
  229.     g_d3dDevice.SetRenderState D3DRENDERSTATE_AMBIENT, g_dx.CreateColorRGBA(1#, 1#, 1#, 1#)
  230.     ' Set the projection matrix. Note that the view and world matrices are set in the
  231.     ' FrameMove function, so that they can be animated each frame.
  232.     Dim matProj As D3DMATRIX
  233.     g_dx.IdentityMatrix matProj
  234.     Call g_dx.ProjectionMatrix(matProj, 1, 1000, pi / 3#)
  235.     g_d3dDevice.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
  236. End Sub
  237. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  238. ' Create the cube by passing this subroutine the array of the cube's vertices.
  239. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  240. Private Sub CreateCube(vertices() As D3DVERTEX)
  241.            
  242.     ' Create vertices describing the front face of the cube.
  243.     g_dx.CreateD3DVertex -1, 1, -1, 0, 0, -1, 0, 0, vertices(0)
  244.     g_dx.CreateD3DVertex 1, 1, -1, 0, 0, -1, 1, 0, vertices(1)
  245.     g_dx.CreateD3DVertex -1, -1, -1, 0, 0, -1, 0, 1, vertices(2)
  246.     g_dx.CreateD3DVertex 1, -1, -1, 0, 0, -1, 1, 1, vertices(3)
  247.         
  248.     ' Create vertices describing the back face of the cube.
  249.     g_dx.CreateD3DVertex -1, 1, 1, 0, 0, 1, 1, 0, vertices(4)
  250.     g_dx.CreateD3DVertex -1, -1, 1, 0, 0, 1, 1, 1, vertices(5)
  251.     g_dx.CreateD3DVertex 1, 1, 1, 0, 0, 1, 0, 0, vertices(6)
  252.     g_dx.CreateD3DVertex 1, -1, 1, 0, 0, 1, 0, 1, vertices(7)
  253.         
  254.     ' Create vertices describing the top face of the cube.
  255.     g_dx.CreateD3DVertex -1, 1, 1, 0, 1, 0, 0, 0, vertices(8)
  256.     g_dx.CreateD3DVertex 1, 1, 1, 0, 1, 0, 1, 0, vertices(9)
  257.     g_dx.CreateD3DVertex -1, 1, -1, 0, 1, 0, 0, 1, vertices(10)
  258.     g_dx.CreateD3DVertex 1, 1, -1, 0, 1, 0, 1, 1, vertices(11)
  259.         
  260.     ' Create vertices describing the bottom face of the cube.
  261.     g_dx.CreateD3DVertex -1, -1, 1, 0, -1, 0, 0, 0, vertices(12)
  262.     g_dx.CreateD3DVertex -1, -1, -1, 0, -1, 0, 0, 1, vertices(13)
  263.     g_dx.CreateD3DVertex 1, -1, 1, 0, -1, 0, 1, 0, vertices(14)
  264.     g_dx.CreateD3DVertex 1, -1, -1, 0, -1, 0, 1, 1, vertices(15)
  265.         
  266.     ' Create vertices describing the right face of the cube.
  267.     g_dx.CreateD3DVertex 1, 1, -1, 1, 0, 0, 0, 0, vertices(16)
  268.     g_dx.CreateD3DVertex 1, 1, 1, 1, 0, 0, 1, 0, vertices(17)
  269.     g_dx.CreateD3DVertex 1, -1, -1, 1, 0, 0, 0, 1, vertices(18)
  270.     g_dx.CreateD3DVertex 1, -1, 1, 1, 0, 0, 1, 1, vertices(19)
  271.         
  272.     ' Create vertices describing the left face of the cube.
  273.     g_dx.CreateD3DVertex -1, 1, -1, -1, 0, 0, 1, 0, vertices(20)
  274.     g_dx.CreateD3DVertex -1, -1, -1, -1, 0, 0, 1, 1, vertices(21)
  275.     g_dx.CreateD3DVertex -1, 1, 1, -1, 0, 0, 0, 0, vertices(22)
  276.     g_dx.CreateD3DVertex -1, -1, 1, -1, 0, 0, 0, 1, vertices(23)
  277. End Sub
  278. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  279. ' Creates a texture surface from a file-based bitmap. The name of the bitmap is passed in
  280. ' as the sFile parameter.
  281. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  282. Public Function CreateTextureSurface(sFile As String) As DirectDrawSurface7
  283.     Dim ddsTexture As DirectDrawSurface7
  284.     Dim i As Long
  285.     Dim bIsFound As Boolean
  286.     ' Prepare the texture surface.
  287.     Dim ddsd As DDSURFACEDESC2
  288.     ddsd.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_PIXELFORMAT Or DDSD_TEXTURESTAGE
  289.     ' Enumerate the texture formats, and find a device-supported texture pixel format. This
  290.     ' simple tutorial is simply looking for a 16-bit texture. Real applications may be interested in
  291.     ' other formats, for alpha textures, bumpmaps, etc..
  292.     Dim TextureEnum As Direct3DEnumPixelFormats
  293.     Set TextureEnum = g_d3dDevice.GetTextureFormatsEnum()
  294.     For i = 1 To TextureEnum.GetCount()
  295.         bIsFound = True
  296.         Call TextureEnum.GetItem(i, ddsd.ddpfPixelFormat)
  297.         
  298.         With ddsd.ddpfPixelFormat
  299.             ' Skip unusual modes.
  300.             If .lFlags And (DDPF_LUMINANCE Or DDPF_BUMPLUMINANCE Or DDPF_BUMPDUDV) Then bIsFound = False
  301.             
  302.             ' Skip any FourCC formats.
  303.             If .lFourCC <> 0 Then bIsFound = False
  304.             
  305.             'Skip alpha modes.
  306.             If .lFlags And DDPF_ALPHAPIXELS Then bIsFound = False
  307.             
  308.             'We only want 16-bit formats, so skip all others.
  309.             If .lRGBBitCount <> 16 Then bIsFound = False
  310.         End With
  311.         
  312.         If bIsFound Then Exit For
  313.         
  314.     Next i
  315.     ' If we did not find surface support, we should exit the application.
  316.     If Not bIsFound Then
  317.         MsgBox "Unable to locate 16-bit surface support on your hardware."
  318.         End
  319.     End If
  320.         
  321.         
  322.     ' Turn on texture managment for the device.
  323.     ddsd.ddsCaps.lCaps = DDSCAPS_TEXTURE
  324.     ddsd.ddsCaps.lCaps2 = DDSCAPS2_TEXTUREMANAGE
  325.     ddsd.lTextureStage = 0
  326.     ' Create a new surface for the texture.
  327.     Set ddsTexture = g_dd.CreateSurfaceFromFile(sFile, ddsd)
  328.     ' Return the newly created texture.
  329.     Set CreateTextureSurface = ddsTexture
  330. End Function
  331. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  332. ' Locates the directory where the bitmaps are placed.
  333. ''''''''''''''''''''''''''''''''''''''''''''''''''''''
  334. Private Sub FindMediaDir(sLoadFile As String)
  335.     On Local Error Resume Next
  336.     If Dir$(sLoadFile) = "" Then
  337.         If Mid$(App.Path, 2, 1) = ":" Then
  338.             ChDrive Mid$(App.Path, 1, 1)
  339.         End If
  340.         ChDir App.Path
  341.         ChDir "..\media"
  342.     End If
  343.     If Dir$(sLoadFile) = "" Then
  344.         ChDir App.Path
  345.         ChDir "..\..\media"
  346.     End If
  347.     Err.Number = 0
  348. End Sub
  349.