home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD89078132000.psc / MOD_DX_3D.bas < prev    next >
Encoding:
BASIC Source File  |  2000-08-13  |  10.3 KB  |  350 lines

  1. Attribute VB_Name = "MOD_DX_3D"
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. '
  4. '            MOD_DX_3D.BAS - BY SIMON PRICE
  5. '
  6. '        BASICS OF USING DIRECT 3D IMMEDIATE MODE
  7. '
  8. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  9.  
  10. ' the main direct 3D object
  11. Public DX_3D As Direct3D7
  12. ' created from DX_3D
  13. Public DX3DDEV As Direct3DDevice7
  14. ' viewport
  15. Public Viewport(0) As D3DRECT
  16. ' the rendering target
  17. Public D3DSurf As DirectDrawSurface7
  18. Public D3DSurfdesc As DDSURFACEDESC2
  19. ' z buffer
  20. Public ZBuff As DirectDrawSurface7
  21. ' enum stuff
  22. Public ddEnum As DirectDrawEnum
  23. Public d3dEnumDevices As Direct3DEnumDevices
  24. Public ddEnumModes As DirectDrawEnumModes
  25. Public DriverGUID As String
  26. Public DeviceGUID As String
  27. Public ddsdMode As DDSURFACEDESC2
  28. Public UsingFullScreen As Boolean
  29. Public Using3DHardware As Boolean
  30. Public enumInfo As DDSURFACEDESC2
  31.  
  32. ' constants
  33. Const PI As Single = 3.141592
  34.  
  35. Public Type tCamera
  36.    x As Single
  37.    y As Single
  38.    z As Single
  39.    Pitch As Single
  40.    Rotation As Single
  41.    Roll As Single
  42. End Type
  43.  
  44. Public Camera As tCamera
  45.  
  46. Sub CrankItUp()
  47. ' create direct 3d
  48. Set DX_3D = DX_DRAW.GetDirect3D
  49.  
  50. DX_DRAW.GetDisplayMode SurfDesc
  51. If SurfDesc.ddpfPixelFormat.lRGBBitCount <= 8 Then
  52.     MsgBox "Stop being so cheap on the colours! I only support 16 bit colour or more!"
  53.     End
  54. End If
  55.  
  56. ' create the direct 3D device
  57. Set DX3DDEV = DX_3D.CreateDevice("IID_IDirect3DRGBDevice", Scene)
  58.  
  59. Dim VPDesc As D3DVIEWPORT7
  60. VPDesc.lWidth = DestRect.Right - DestRect.LEFT
  61. VPDesc.lHeight = DestRect.Bottom - DestRect.Top
  62. VPDesc.minz = 0#
  63. VPDesc.maxz = 1#
  64. DX3DDEV.SetViewport VPDesc
  65.  
  66. ' remember viewport rectangle
  67. With Viewport(0)
  68.     .X1 = 0: .Y1 = 0
  69.     .X2 = VPDesc.lWidth
  70.     .Y2 = VPDesc.lHeight
  71. End With
  72. End Sub
  73.  
  74. Function MakeVector(x As Double, y As Double, z As Double) As D3DVECTOR
  75. ' make a vector with 3 points
  76. Dim Vector As D3DVECTOR
  77. With Vector
  78.     .x = x
  79.     .y = y
  80.     .z = z
  81. End With
  82. MakeVector = Vector
  83. End Function
  84.  
  85. Sub EnumDrivers()
  86. Dim i As Long
  87. ' get driver info
  88. Set ddEnum = DX.GetDDEnum()
  89. For i = 1 To ddEnum.GetCount()
  90.     DriverGUID = ddEnum.GetDescription(i)
  91. Next i
  92. End Sub
  93.  
  94. Sub EnumDevices(cmbDevice As ComboBox)
  95. Dim i As Long
  96. ' Get device information and place device user-friendly names in a combo box.
  97. cmbDevice.Clear
  98. Set d3dEnumDevices = DX_3D.GetDevicesEnum()
  99. For i = 1 To d3dEnumDevices.GetCount()
  100.     cmbDevice.AddItem d3dEnumDevices.GetName(i)
  101. Next
  102. cmbDevice.ListIndex = 0
  103. End Sub
  104.  
  105. Sub EnumModes()
  106. Set ddEnumModes = DX_DRAW.GetDisplayModesEnum(DDEDM_DEFAULT, enumInfo)
  107. End Sub
  108.  
  109. Sub AttachZbuffer()
  110. '' create the z-buffer and attach to backbuffer
  111. 'Dim ddpfZBuffer As DDPIXELFORMAT
  112. 'Dim d3dEnumPFs As Direct3DEnumPixelFormats
  113. '
  114. 'Set DX_3D = DX_DRAW.GetDirect3D
  115. 'Set d3dEnumPFs = DX_3D.GetEnumZBufferFormats("IID_IDirect3DRGBDevice")
  116. '
  117. 'Dim i As Long
  118. '
  119. 'For i = 1 To d3dEnumPFs.GetCount()
  120. 'Call d3dEnumPFs.GetItem(i, ddpfZBuffer)
  121. 'If ddpfZBuffer.lFlags = DDPF_ZBUFFER Then
  122. '  Exit For
  123. 'End If
  124. 'Next i
  125. '
  126. 'SetRect DestRect, 0, 0, 640, 480
  127. '' Prepare and create the z-buffer surface.
  128. 'SurfDesc.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT Or DDSD_PIXELFORMAT
  129. 'SurfDesc.ddsCaps.lCaps = DDSCAPS_ZBUFFER
  130. 'SurfDesc.lWidth = DestRect.Right - DestRect.LEFT
  131. 'SurfDesc.lHeight = DestRect.Bottom - DestRect.Top
  132. 'SurfDesc.ddpfPixelFormat = ddpfZBuffer
  133. 'SurfDesc.ddsCaps.lCaps = SurfDesc.ddsCaps.lCaps Or DDSCAPS_SYSTEMMEMORY
  134. '
  135. 'Set ZBuff = DX_DRAW.CreateSurface(SurfDesc)
  136. '
  137. '' attach the z-buffer to the back buffer
  138. 'Scene.AddAttachedSurface ZBuff
  139. End Sub
  140.  
  141. Public Function CreateTextureSurface(File As String) As DirectDrawSurface7
  142. Dim ddsTexture As DirectDrawSurface7
  143. Dim i As Long
  144. Dim IsFound As Boolean
  145. Dim ddsd As DDSURFACEDESC2
  146.  
  147. ddsd.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_PIXELFORMAT Or DDSD_TEXTURESTAGE
  148.  
  149. ' Enumerate the texture formats, and find a device-supported texture pixel format. This
  150. ' simple tutorial is simply looking for a 16-bit texture. Real applications may be interested in
  151. ' other formats, for alpha textures, bumpmaps, etc..
  152. Dim TextureEnum As Direct3DEnumPixelFormats
  153. Set TextureEnum = DX3DDEV.GetTextureFormatsEnum()
  154.  
  155. For i = 1 To TextureEnum.GetCount()
  156.     IsFound = True
  157.     TextureEnum.GetItem i, ddsd.ddpfPixelFormat
  158.     With ddsd.ddpfPixelFormat
  159.         ' Skip unusual modes.
  160.         If .lFlags And (DDPF_LUMINANCE Or DDPF_BUMPLUMINANCE Or DDPF_BUMPDUDV) Then IsFound = False
  161.         ' Skip any FourCC formats.
  162.         If .lFourCC <> 0 Then IsFound = False
  163.         'Skip alpha modes.
  164.         If .lFlags And DDPF_ALPHAPIXELS Then IsFound = False
  165.         'We only want 16-bit formats, so skip all others.
  166.         If .lRGBBitCount <> 16 Then IsFound = False
  167.     End With
  168.     If IsFound Then Exit For
  169. Next i
  170. ' If we did not find surface support, we should exit the application.
  171. If Not IsFound Then
  172.     MsgBox "Unable to locate 16-bit surface support on your hardware."
  173.     End
  174. End If
  175. ' Turn on texture managment for the device.
  176. ddsd.ddsCaps.lCaps = DDSCAPS_TEXTURE
  177. ddsd.ddsCaps.lCaps2 = DDSCAPS2_TEXTUREMANAGE
  178. ddsd.lTextureStage = 0
  179. ' Create a new surface for the texture.
  180. Set ddsTexture = DX_DRAW.CreateSurfaceFromFile(File, ddsd)
  181. ' Return the newly created texture.
  182. Set CreateTextureSurface = ddsTexture
  183. End Function
  184.  
  185. Public Function CreateTextureSurfaceCK(File As String) As DirectDrawSurface7
  186. Dim ddsTexture As DirectDrawSurface7
  187. Dim i As Long
  188. Dim IsFound As Boolean
  189. Dim ddsd As DDSURFACEDESC2
  190.  
  191. ddsd.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_PIXELFORMAT Or DDSD_TEXTURESTAGE Or DDSD_CKSRCBLT
  192.  
  193. ' Enumerate the texture formats, and find a device-supported texture pixel format. This
  194. ' simple tutorial is simply looking for a 16-bit texture. Real applications may be interested in
  195. ' other formats, for alpha textures, bumpmaps, etc..
  196. Dim TextureEnum As Direct3DEnumPixelFormats
  197. Set TextureEnum = DX3DDEV.GetTextureFormatsEnum()
  198.  
  199. For i = 1 To TextureEnum.GetCount()
  200.     IsFound = True
  201.     TextureEnum.GetItem i, ddsd.ddpfPixelFormat
  202.     With ddsd.ddpfPixelFormat
  203.         ' Skip unusual modes.
  204.         If .lFlags And (DDPF_LUMINANCE Or DDPF_BUMPLUMINANCE Or DDPF_BUMPDUDV) Then IsFound = False
  205.         ' Skip any FourCC formats.
  206.         If .lFourCC <> 0 Then IsFound = False
  207.         'Skip alpha modes.
  208.         If .lFlags And DDPF_ALPHAPIXELS Then IsFound = False
  209.         'We only want 16-bit formats, so skip all others.
  210.         If .lRGBBitCount <> 16 Then IsFound = False
  211.     End With
  212.     If IsFound Then Exit For
  213. Next i
  214. ' If we did not find surface support, we should exit the application.
  215. If Not IsFound Then
  216.     MsgBox "Unable to locate 16-bit surface support on your hardware."
  217.     End
  218. End If
  219. ' Turn on texture managment for the device.
  220. ddsd.ddsCaps.lCaps = DDSCAPS_TEXTURE
  221. ddsd.ddsCaps.lCaps2 = DDSCAPS2_TEXTUREMANAGE
  222. ddsd.lTextureStage = 0
  223. ' Create a new surface for the texture.
  224. Set ddsTexture = DX_DRAW.CreateSurfaceFromFile(File, ddsd)
  225. ' Return the newly created texture.
  226. Set CreateTextureSurfaceCK = ddsTexture
  227. End Function
  228.  
  229. Sub LoadTextures()
  230. ' *** app specific textures here ***
  231.  
  232. Set Tex(TEX_SIDEWALL) = CreateTextureSurface(App.Path & "\sidewall.bmp")
  233. Set Tex(TEX_WALL) = CreateTextureSurface(App.Path & "\wall.bmp")
  234. Set Tex(TEX_GRASS) = CreateTextureSurface(App.Path & "\grass.bmp")
  235. Set Tex(TEX_WATER) = CreateTextureSurface(App.Path & "\water.bmp")
  236.  
  237. DX3DDEV.SetRenderState D3DRENDERSTATE_COLORKEYENABLE, True
  238. Set Tex(TEX_FENCE) = CreateTextureSurfaceCK(App.Path & "\fence.bmp")
  239. MOD_DX_DRAW.AddColorKey Tex(TEX_FENCE), vbBlack, vbBlack
  240.  
  241. Set Tex(TEX_ROOF) = CreateTextureSurface(App.Path & "\roof.bmp")
  242. Set Tex(TEX_PLANE_BOAT) = CreateTextureSurfaceCK(App.Path & "\plane_boat.bmp")
  243. MOD_DX_DRAW.AddColorKey Tex(TEX_PLANE_BOAT), vbBlack, vbBlack
  244. Set Tex(TEX_TREE) = CreateTextureSurfaceCK(App.Path & "\tree.bmp")
  245. MOD_DX_DRAW.AddColorKey Tex(TEX_TREE), vbBlack, vbBlack
  246. End Sub
  247.  
  248. Sub LoadMaterials()
  249. ' *** app specific materials
  250.  
  251. DX3DDEV.SetMaterial MakeMaterial(1, 1, 1, 1, 1, 1, 1, 1)
  252. End Sub
  253.  
  254. Sub LoadLighting()
  255. ' *** app specific lighting
  256.  
  257. ' Enable ambient lighting
  258. DX3DDEV.SetRenderState D3DRENDERSTATE_AMBIENT, DX.CreateColorRGBA(1, 1, 1, 1)
  259. DX3DDEV.SetRenderState D3DRENDERSTATE_LIGHTING, False
  260. DX3DDEV.SetRenderState D3DRENDERSTATE_SHADEMODE, D3DSHADE_FLAT
  261. End Sub
  262.  
  263. Sub LoadMatrices()
  264. ' *** app specific matrices
  265.  
  266. ' Set the projection matrix. Note that the view and world matrices are set in the
  267. ' FrameMove function, so that they can be animated each frame.
  268. Dim matProj As D3DMATRIX
  269. DX.IdentityMatrix matProj
  270. DX.ProjectionMatrix matProj, 1, 1000, PI / 3
  271. DX3DDEV.SetTransform D3DTRANSFORMSTATE_PROJECTION, matProj
  272. End Sub
  273.  
  274. Sub Load3DScene()
  275. ' *** app specific loading ***
  276.  
  277.  
  278. End Sub
  279.  
  280. Sub LoadCameras()
  281. Camera.y = 0.7
  282. Camera.z = 5
  283. End Sub
  284.  
  285. Sub UnloadTextures()
  286.  
  287. ' *** app specific textures here ***
  288.  
  289. Dim i As Byte
  290. For i = 1 To NUM_TEX
  291.    Set Tex(i) = Nothing
  292. Next
  293. End Sub
  294.  
  295. Function MakeMaterial(Optional aa As Byte = 0, Optional ar As Byte = 0, Optional ag As Byte = 0, Optional ab As Byte = 0, Optional da As Byte = 0, Optional dr As Byte = 0, Optional dg As Byte = 0, Optional db As Byte = 0, Optional ea As Byte = 0, Optional er As Byte = 0, Optional eg As Byte = 0, Optional eb As Byte = 0, Optional sa As Byte = 0, Optional sr As Byte = 0, Optional sg As Byte = 0, Optional sb As Byte = 0, Optional p As Byte = 0) As D3DMATERIAL7
  296. With MakeMaterial
  297.     With .Ambient
  298.         .a = aa
  299.         .r = ar
  300.         .g = ag
  301.         .b = ab
  302.     End With
  303.     With .diffuse
  304.         .a = da
  305.         .r = dr
  306.         .g = dg
  307.         .b = db
  308.     End With
  309.     With .emissive
  310.         .a = ea
  311.         .r = er
  312.         .g = eg
  313.         .b = eb
  314.     End With
  315.     With .specular
  316.         .a = sa
  317.         .r = sr
  318.         .g = sg
  319.         .b = sb
  320.     End With
  321.     .power = p
  322. End With
  323. End Function
  324.  
  325. Sub CopyVec2Vert(srcVec As D3DVECTOR, destVert As D3DVERTEX)
  326. destVert.x = srcVec.x
  327. destVert.y = srcVec.y
  328. destVert.z = srcVec.z
  329. End Sub
  330.  
  331. Sub CopyVert2Vec(srcVert As D3DVECTOR, destVec As D3DVERTEX)
  332. destVec.x = srcVert.x
  333. destVec.y = srcVert.y
  334. destVec.z = srcVert.z
  335. End Sub
  336.  
  337. Function MakeVertex(Vec As D3DVECTOR, nx As Single, ny As Single, nz As Single, tu As Single, tv As Single) As D3DVERTEX
  338. With MakeVertex
  339.     .x = Vec.x
  340.     .y = Vec.y
  341.     .z = Vec.z
  342.     .nx = nx
  343.     .ny = ny
  344.     .nz = nz
  345.     .tu = tu
  346.     .tv = tv
  347. End With
  348. End Function
  349.  
  350.