home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / Chip_2002-01_cd1.bin / oddech / eracer / cEnvironment.cls < prev    next >
Text File  |  2001-02-16  |  26KB  |  587 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cEnvironment"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. Public D3Frame              As Direct3DRMFrame3
  17. Public D3FrameStation       As Direct3DRMFrame3
  18. Public StationHealth        As Single
  19. Public StationDeactivating  As Single
  20.  
  21. Public DDSMap               As DirectDrawSurface4
  22.  
  23. Public IslandSize           As Long                      ' Size of current map
  24. Public IslandName           As String                    ' Name of current map
  25. Public DayTime              As eDayTime                  ' Current time of day in environment
  26.  
  27. Private I_oD3FrameWater     As Direct3DRMFrame3          ' D3DRM Water frame
  28. Private I_oD3FrameSky       As Direct3DRMFrame3          ' D3DRM Sky frame
  29. Private I_oD3FrameFloor     As Direct3DRMFrame3          ' D3DRM Floor frame
  30.  
  31. Private I_oD3FrameLight     As Direct3DRMFrame3          ' D3DRM Light frame
  32.  
  33. Private I_oD3FrameFlare()   As Direct3DRMFrame3          ' D3DRM lensflare frames
  34.  
  35. Private I_oD3MeshWater      As Direct3DRMMeshBuilder3    ' D3DRM Mesh holding water
  36. Private I_oD3MeshSky        As Direct3DRMMeshBuilder3    ' D3DRM Mesh holding sky
  37.  
  38. Private I_oD3MeshFloor      As Direct3DRMMeshBuilder3    ' D3DRM Mesh holding floor
  39.  
  40. Private I_oD3TFloor()       As Direct3DRMTexture3         ' D3DRM Textures for floor
  41.  
  42. Private I_oD3LightAmb       As Direct3DRMLight           ' D3DRM Light, environment
  43. Private I_oD3LightDir       As Direct3DRMLight           ' D3DRM Light, directional
  44.  
  45. Private I_oDSBDie           As DirectSoundBuffer
  46. Private I_oD3BDie           As DirectSound3DBuffer
  47.  
  48. Public Sub Initialize()
  49.  
  50.     Dim L_oD3Texture    As Direct3DRMTexture3       ' Texture for local generation
  51.     Dim L_dD3MatOvr     As D3DRMMATERIALOVERRIDE    ' Override for alpha
  52.     Dim L_nRunT         As Long
  53.     
  54.     ' Create base frame
  55.     Set D3Frame = Application.D3Instance.CreateFrame(Application.D3Frame)
  56.     
  57.     ' Create D3DRM frame for light: Still frame
  58.     Set I_oD3FrameLight = Application.D3Instance.CreateFrame(D3Frame)
  59.        
  60.     ' Initialize light frame
  61.     I_oD3FrameLight.SetOrientation Nothing, 0, -1, -1, 0, 1, 0
  62.     
  63.     ' Create and add ambient light
  64.     Set I_oD3LightAmb = Application.D3Instance.CreateLightRGB(D3DRMLIGHT_AMBIENT, 0.5, 0.5, 0.5)
  65.     I_oD3FrameLight.AddLight I_oD3LightAmb
  66.     
  67.     ' Create and add directional light
  68.     Set I_oD3LightDir = Application.D3Instance.CreateLightRGB(D3DRMLIGHT_DIRECTIONAL, 0.75, 0.75, 0.75)
  69.     I_oD3FrameLight.AddLight I_oD3LightDir
  70.        
  71.     ' Create D3DRM frame for Sky: Moves with racer (smooth)
  72.     Set I_oD3FrameSky = Application.D3Instance.CreateFrame(Application.D3Frame)
  73.        
  74.     ' Create D3DRM frame for water: Still frame
  75.     Set I_oD3FrameWater = Application.D3Instance.CreateFrame(D3Frame)
  76.                                              
  77.     ' Create D3DRM frame for Station: Players camp
  78.     Set D3FrameStation = Application.D3Instance.CreateFrame(D3Frame)
  79.     
  80.     ' Water
  81.     Set I_oD3MeshWater = Application.D3Instance.CreateMeshBuilder
  82.     I_oD3MeshWater.LoadFromFile App.Path + "\mdl\w.x", 0, D3DRMLOAD_FROMFILE, Nothing, Nothing
  83.     I_oD3MeshWater.Optimize
  84.     I_oD3MeshWater.SetAppData 1 ' Remember for identification on fly-over
  85.  
  86.     I_oD3FrameWater.AddVisual I_oD3MeshWater
  87.     
  88.     ' Sky
  89.     Set I_oD3MeshSky = Application.D3Instance.CreateMeshBuilder
  90.     I_oD3MeshSky.LoadFromFile App.Path + "\mdl\s.x", 0, D3DRMLOAD_FROMFILE, Nothing, Nothing
  91.     I_oD3MeshSky.Optimize
  92.     I_oD3FrameSky.AddVisual I_oD3MeshSky
  93.  
  94.     ' Floor
  95.     Set I_oD3FrameFloor = Application.D3Instance.CreateFrame(D3Frame)
  96.     Set I_oD3MeshFloor = Application.D3Instance.CreateMeshBuilder
  97.     I_oD3FrameFloor.AddVisual I_oD3MeshFloor
  98.     I_oD3MeshFloor.SetAppData 2 ' Remember for identification on fly-over
  99.     
  100.     ' Initialize lensflare system ...
  101.     
  102.         ' Dimension flare frames
  103.         ReDim I_oD3FrameFlare(4)
  104.         
  105.         ' Element #1 ...
  106.         
  107.             ' Prepare texture
  108.             Set L_oD3Texture = Application.D3Instance.LoadTexture(App.Path + "\gfx\f01.bmp")
  109.             L_oD3Texture.SetDecalTransparency D_TRUE
  110.             L_oD3Texture.SetDecalTransparentColor 0
  111.             
  112.             ' Create and add geometry
  113.             Set I_oD3FrameFlare(0) = Application.D3Instance.CreateFrame(Application.D3Frame)
  114.             I_oD3FrameFlare(0).AddVisual GenerateBaseMesh(0.75, L_oD3Texture)
  115.         
  116.         ' Element #2 ...
  117.         
  118.             ' Prepare texture
  119.             Set L_oD3Texture = Application.D3Instance.LoadTexture(App.Path + "\gfx\f02.bmp")
  120.             L_oD3Texture.SetDecalTransparency D_TRUE
  121.             L_oD3Texture.SetDecalTransparentColor 0
  122.         
  123.             ' Create and add geometry
  124.             Set I_oD3FrameFlare(1) = Application.D3Instance.CreateFrame(Application.D3Frame)
  125.             I_oD3FrameFlare(1).AddVisual GenerateBaseMesh(0.3, L_oD3Texture)
  126.         
  127.         ' Element #3 ...
  128.         
  129.             ' Prepare texture
  130.             Set L_oD3Texture = Application.D3Instance.LoadTexture(App.Path + "\gfx\f02.bmp")
  131.             L_oD3Texture.SetDecalTransparency D_TRUE
  132.             L_oD3Texture.SetDecalTransparentColor 0
  133.         
  134.             ' Create and add geometry
  135.             Set I_oD3FrameFlare(2) = Application.D3Instance.CreateFrame(Application.D3Frame)
  136.             I_oD3FrameFlare(2).AddVisual GenerateBaseMesh(0.05, L_oD3Texture)
  137.          
  138.         ' Element #4 ...
  139.         
  140.             ' Prepare texture
  141.             Set L_oD3Texture = Application.D3Instance.LoadTexture(App.Path + "\gfx\f03.bmp")
  142.             L_oD3Texture.SetDecalTransparency D_TRUE
  143.             L_oD3Texture.SetDecalTransparentColor 0
  144.         
  145.             ' Create and add geometry
  146.             Set I_oD3FrameFlare(3) = Application.D3Instance.CreateFrame(Application.D3Frame)
  147.             I_oD3FrameFlare(3).AddVisual GenerateBaseMesh(0.75, L_oD3Texture)
  148.             
  149.         ' Load floor textures ...
  150.         
  151.             ' Reserve space
  152.             ReDim I_oD3TFloor(4)
  153.             
  154.             ' Load from file
  155.             Set I_oD3TFloor(0) = Application.D3Instance.LoadTexture(App.Path + "\gfx\s2.bmp")
  156.             For L_nRunT = 1 To 4
  157.                 Set I_oD3TFloor(L_nRunT) = Application.D3Instance.LoadTexture(App.Path + "\gfx\s2" & L_nRunT & ".bmp")
  158.                 I_oD3TFloor(L_nRunT).GenerateMIPMap
  159.             Next
  160.         
  161.         ' Set general data
  162.             DayTime = dtDay
  163.             
  164.         If (Not Application.DSInstance Is Nothing) Then
  165.             Dim L_ddsbd As DSBUFFERDESC
  166.             With L_ddsbd
  167.                 .lBufferBytes = 0
  168.                 .lFlags = DSBCAPS_CTRL3D Or DSBCAPS_CTRLFREQUENCY Or DSBCAPS_CTRLPAN Or DSBCAPS_CTRLVOLUME Or DSBCAPS_STATIC
  169.             End With
  170.             Set I_oDSBDie = Application.DSInstance.CreateSoundBufferFromFile(App.Path + "\snd\explobig.wav", L_ddsbd, GetWaveFileFormat(App.Path + "\snd\explobig.wav"))
  171.             Set I_oD3BDie = I_oDSBDie.GetDirectSound3DBuffer
  172.             With I_oD3BDie
  173.                 .SetConeAngles DS3D_MINCONEANGLE, DS3D_MAXCONEANGLE, DS3D_IMMEDIATE
  174.                 .SetConeOutsideVolume 0, DS3D_IMMEDIATE
  175.                 .SetMinDistance 1, DS3D_IMMEDIATE
  176.                 .SetMaxDistance 15, DS3D_IMMEDIATE
  177.                 .SetVelocity 0, 0, 0, DS3D_IMMEDIATE
  178.                 .SetMode DS3DMODE_NORMAL, DS3D_IMMEDIATE
  179.             End With
  180.         End If
  181.         
  182.         ' Finish ...
  183.         
  184.             ChangeMap
  185.             Reset
  186.         
  187. End Sub
  188.  
  189. Public Sub ChangeMap()
  190.  
  191.     Dim L_sMapFile      As String                   ' Map file to load
  192.     Dim L_nMapCount     As Long                     ' Counter over maps
  193.     Static S_nMapNumber As Long
  194.     
  195.     S_nMapNumber = S_nMapNumber + 1
  196.     L_sMapFile = Dir(App.Path + "\map\" & S_nMapNumber & "*.map")
  197.     If L_sMapFile = "" Then
  198.         S_nMapNumber = 1
  199.         L_sMapFile = Dir(App.Path + "\map\" & S_nMapNumber & "*.map")
  200.     End If
  201.     
  202.     IslandName = Left(L_sMapFile, Len(L_sMapFile) - 4)
  203.     
  204.     LoadMap
  205.  
  206. End Sub
  207.  
  208. Public Sub ChangeDaytime()
  209.     DayTime = IIf(DayTime = dtDay, dtNight, dtDay)
  210. End Sub
  211.  
  212. Public Sub Reset()
  213.  
  214.     Dim L_nIndex As Long
  215.     Dim L_nHeight As Single
  216.     Dim L_dD3Pos As D3DVECTOR
  217.     
  218.     
  219.     
  220.     ' Day
  221.     If DayTime = dtDay Then
  222.     
  223.         ' Sky
  224.         I_oD3MeshSky.SetTexture Application.D3Instance.LoadTexture(App.Path + "\gfx\s5.bmp")
  225.         
  226.         ' Water
  227.         I_oD3MeshWater.SetTexture Application.D3Instance.LoadTexture(App.Path + "\gfx\s1.bmp")
  228.         
  229.         ' Flares
  230.         For L_nIndex = 0 To 3
  231.             I_oD3FrameFlare(L_nIndex).SetTraversalOptions D3DRMFRAME_RENDERENABLE
  232.         Next
  233.         
  234.     End If
  235.     
  236.     ' Night
  237.     If DayTime = dtNight Then
  238.         
  239.         ' Sky
  240.         I_oD3MeshSky.SetTexture Application.D3Instance.LoadTexture(App.Path + "\gfx\s6.bmp")
  241.         
  242.         ' Water
  243.         I_oD3MeshWater.SetTexture Application.D3Instance.LoadTexture(App.Path + "\gfx\s7.bmp")
  244.         
  245.         ' Flares
  246.         For L_nIndex = 0 To 3
  247.             I_oD3FrameFlare(L_nIndex).SetTraversalOptions 0
  248.         Next
  249.         
  250.     End If
  251.     
  252. ' Set lightstates according to daytime ...
  253.  
  254.     ' Day
  255.     If DayTime = dtDay Then
  256.         I_oD3LightAmb.SetColorRGB 0.3, 0.3, 0.3
  257.         I_oD3LightDir.SetColorRGB 0.6, 0.6, 0.6
  258.     End If
  259.     
  260.     ' Night
  261.     If DayTime = dtNight Then
  262.         I_oD3LightAmb.SetColorRGB 0.2, 0.15, 0.2
  263.         I_oD3LightDir.SetColorRGB 0, 0, 0
  264.     End If
  265.     
  266.     ' Station
  267.     StationHealth = 2000
  268.     StationDeactivating = 0
  269.     D3FrameStation.SetRotation Nothing, 0, 0, 0, 0
  270.     D3FrameStation.SetOrientation Nothing, 0, 0, 1, 0, 1, 0
  271.     I_oD3MeshFloor.GetVertex (IslandSize \ 2) * IslandSize + IslandSize \ 2, L_dD3Pos
  272.     L_nHeight = L_dD3Pos.Y
  273.     D3FrameStation.SetPosition Nothing, (IslandSize \ 2), L_nHeight - 0.2, (IslandSize \ 2)
  274.     
  275. End Sub
  276.  
  277. Public Sub Update()
  278.  
  279.         Dim L_dD3Pos        As D3DVECTOR
  280.         Dim L_dD3Ori        As D3DVECTOR
  281.         Dim L_dD3Nor        As D3DVECTOR
  282.         Dim L_nIdx          As Long
  283.         Dim L_dD3MatOvr     As D3DRMMATERIALOVERRIDE
  284.         
  285.             ' Update water animation ...
  286.             
  287.                 ' Raise/lower
  288.                 I_oD3FrameWater.GetPosition Nothing, L_dD3Pos
  289.                 L_dD3Pos.Y = Sin(Application.FrameCount / 10) * 0.05
  290.                 I_oD3FrameWater.SetPosition Nothing, L_dD3Pos.X, L_dD3Pos.Y, L_dD3Pos.Z
  291.                    
  292.                  ' Animate texture
  293.                 I_oD3MeshWater.SetTextureCoordinates 0, Sin(Application.FrameCount / 50), Cos(Application.FrameCount / 50)
  294.                 I_oD3MeshWater.SetTextureCoordinates 1, 200 + Sin(Application.FrameCount / 50), Cos(Application.FrameCount / 50)
  295.                 I_oD3MeshWater.SetTextureCoordinates 2, 200 + Sin(Application.FrameCount / 50), 200 + Cos(Application.FrameCount / 50)
  296.                 I_oD3MeshWater.SetTextureCoordinates 3, Sin(Application.FrameCount / 50), 200 + Cos(Application.FrameCount / 50)
  297.                 
  298.             ' Update lensflare system ...
  299.                      
  300.                 ' Get camera data
  301.                 Application.D3Camera.GetPosition Nothing, L_dD3Pos
  302.                 Application.D3Camera.GetOrientation Nothing, L_dD3Ori, L_dD3Nor
  303.                 
  304.                 ' Set flare frames position accordingly
  305.                 I_oD3FrameFlare(0).SetPosition Nothing, L_dD3Pos.X + L_dD3Ori.X * 6, L_dD3Pos.Y + 0.2 + (L_dD3Ori.Y) * 3, L_dD3Pos.Z + 0.5
  306.                 I_oD3FrameFlare(1).SetPosition Nothing, L_dD3Pos.X + L_dD3Ori.X * 4, L_dD3Pos.Y + 0.2 + (L_dD3Ori.Y) * 2, L_dD3Pos.Z + 0.65
  307.                 I_oD3FrameFlare(2).SetPosition Nothing, L_dD3Pos.X + L_dD3Ori.X * 2, L_dD3Pos.Y + 0.2 + (L_dD3Ori.Y) * 1, L_dD3Pos.Z + 0.75
  308.                 I_oD3FrameFlare(3).SetPosition Nothing, L_dD3Pos.X, 5, L_dD3Pos.Z + 10
  309.     
  310.                 ' Make flare frames face camera
  311.                I_oD3FrameFlare(0).SetOrientation Nothing, L_dD3Ori.X, L_dD3Ori.Y, L_dD3Ori.Z, L_dD3Nor.X, L_dD3Nor.Y, L_dD3Nor.Z
  312.                I_oD3FrameFlare(1).SetOrientation Nothing, L_dD3Ori.X, L_dD3Ori.Y, L_dD3Ori.Z, L_dD3Nor.X, L_dD3Nor.Y, L_dD3Nor.Z
  313.                I_oD3FrameFlare(2).SetOrientation Nothing, L_dD3Ori.X, L_dD3Ori.Y, L_dD3Ori.Z, L_dD3Nor.X, L_dD3Nor.Y, L_dD3Nor.Z
  314.                I_oD3FrameFlare(3).SetOrientation Nothing, L_dD3Ori.X, L_dD3Ori.Y, L_dD3Ori.Z, L_dD3Nor.X, L_dD3Nor.Y, L_dD3Nor.Z
  315.  
  316.                 ' Set flare brightness
  317.                 L_dD3MatOvr.lFlags = D3DRMMATERIALOVERRIDE_DIFFUSE_ALPHAONLY Or D3DRMMATERIALOVERRIDE_DIFFUSE_ALPHAMULTIPLY
  318.                 L_dD3MatOvr.dcDiffuse.a = 0.1 * (1 - Abs(L_dD3Ori.X) * 1.5)
  319.                 I_oD3FrameFlare(0).SetMaterialOverride L_dD3MatOvr
  320.                 L_dD3MatOvr.dcDiffuse.a = 0.15 * (1 - Abs(L_dD3Ori.X) * 1.5)
  321.                 I_oD3FrameFlare(1).SetMaterialOverride L_dD3MatOvr
  322.                 I_oD3FrameFlare(2).SetMaterialOverride L_dD3MatOvr
  323.  
  324.             ' Update according to racer position ...
  325.                     
  326.                 ' Camera and sky
  327.                 If Application.Environment.StationDeactivating = 0 Then
  328.                     If Application.Player.Deactivating > 0 Then Application.Player.Heading = Application.Player.Heading + 0.05
  329.                     Application.D3Camera.SetPosition Nothing, Application.Player.X - 2 * Cos(Application.Player.Heading), 2 + 1.5 + -Application.Player.Y * 0.25 + IIf(Application.Player.Deactivating > 0, (100 - Application.Player.Deactivating) * 0.025, 0), Application.Player.Z - 2 * Sin(Application.Player.Heading)
  330.                     Application.D3Camera.LookAt Application.Player.D3Frame, Nothing, D3DRMCONSTRAIN_Z
  331.                     I_oD3FrameSky.SetPosition Nothing, Application.Player.X, 0, Application.Player.Z
  332.                 Else
  333.                     Application.Environment.D3FrameStation.GetPosition Nothing, L_dD3Pos
  334.                     Application.D3Camera.SetPosition Nothing, L_dD3Pos.X + 3 * Cos(StationDeactivating / 50), 5, L_dD3Pos.Z + 3 * Sin(StationDeactivating / 50)
  335.                     Application.D3Camera.LookAt Application.Environment.D3FrameStation, Nothing, D3DRMCONSTRAIN_Z
  336.                     I_oD3FrameSky.SetPosition Nothing, L_dD3Pos.X, 0, L_dD3Pos.Z
  337.                 End If
  338.                 
  339.                 ' Listener
  340.                 If (Not Application.DSInstance Is Nothing) And Application.SoundEnabled Then
  341.                     With Application.DSListener
  342.                         .SetPosition L_dD3Pos.X, L_dD3Pos.Y, L_dD3Pos.Z, DS3D_IMMEDIATE
  343.                         .SetOrientation L_dD3Ori.X, L_dD3Ori.Y, L_dD3Ori.Z, L_dD3Nor.X, L_dD3Nor.Y, L_dD3Nor.Z, DS3D_IMMEDIATE
  344.                     End With
  345.                 End If
  346.                 
  347.             ' Deactivating procedures ...
  348.                 If StationDeactivating > 0 Then
  349.                 
  350.                     ' Play explo
  351.                     If StationDeactivating Mod 40 = 0 Then
  352.                         If (Not Application.DSInstance Is Nothing) And Application.SoundEnabled Then
  353.                             D3FrameStation.GetPosition Nothing, L_dD3Pos
  354.                             I_oD3BDie.SetPosition L_dD3Pos.X, L_dD3Pos.Y, L_dD3Pos.Z, DS3D_IMMEDIATE
  355.                             I_oD3BDie.SetConeOrientation 0, 1, 0, DS3D_IMMEDIATE
  356.                             I_oDSBDie.Play DSBPLAY_DEFAULT
  357.                         End If
  358.                     End If
  359.                     
  360.                     ' Rock station
  361.                     If StationDeactivating Mod 10 = 0 Then
  362.                         D3FrameStation.GetPosition Nothing, L_dD3Pos
  363.                         L_dD3Pos.Y = L_dD3Pos.Y - 0.005
  364.                         D3FrameStation.SetPosition Nothing, L_dD3Pos.X, L_dD3Pos.Y, L_dD3Pos.Z
  365.                         D3FrameStation.SetRotation Nothing, Rnd, Rnd, Rnd, Rnd * 0.005
  366.                     End If
  367.                                     
  368.                     ' Place particles
  369.                     If StationDeactivating Mod 5 = 0 Then
  370.                         D3FrameStation.GetPosition Nothing, L_dD3Pos
  371.                         With L_dD3Ori
  372.                             .X = Rnd * 0.01
  373.                             .Y = 0.075
  374.                             .Z = Rnd * 0.01
  375.                         End With
  376.                         With L_dD3Pos
  377.                             .X = .X + Rnd * 2 - 1
  378.                             .Y = .Y + 3
  379.                             .Z = .Z + Rnd * 2 - 1
  380.                         End With
  381.                         Application.Particles.Add ptDustDay, L_dD3Pos, L_dD3Ori
  382.                     End If
  383.                 
  384.                     ' Place explosions
  385.                     If StationDeactivating Mod 5 = 0 Then
  386.                         D3FrameStation.GetPosition Nothing, L_dD3Pos
  387.                         With L_dD3Ori
  388.                             .X = 0
  389.                             .Y = 0
  390.                             .Z = 0
  391.                         End With
  392.                         With L_dD3Pos
  393.                             .X = .X + Rnd * 3 - 1.5
  394.                             .Y = .Y + Rnd * 2 + 2
  395.                             .Z = .Z + Rnd * 3 - 1.5
  396.                         End With
  397.                         Application.Effects.Add etExplo, L_dD3Pos, L_dD3Ori
  398.                     End If
  399.                     
  400.                     StationDeactivating = StationDeactivating - 1
  401.                     If StationDeactivating = 0 Then
  402.                         Application.Enemies.Active = False
  403.                         Application.Reset
  404.                     End If
  405.                                         
  406.                     Exit Sub
  407.                     
  408.                 Else
  409.                     StationHealth = StationHealth + 1
  410.                     If StationHealth > 2000 Then StationHealth = 2000
  411.                 End If
  412.                 
  413.             ' Check for station damage
  414.                 If StationHealth < 0 Then
  415.                     StationDeactivating = 200
  416.                     Application.Interface.DisplayMessage "station destroyed ..."
  417.                 End If
  418.  
  419. End Sub
  420.  
  421. Private Sub LoadMap()
  422.  
  423. ' Declare local variables ...
  424.  
  425.     Dim L_oD3Face       As Direct3DRMFace2          ' Face, temporary holds data for creation of floor
  426.     Dim L_oD3Material   As Direct3DRMMaterial2      ' Material for faces
  427.     Dim L_dD3MatOvr     As D3DRMMATERIALOVERRIDE    ' MAterial override for map display
  428.     
  429.     Dim L_dD3Vertex(3)  As D3DVECTOR                ' Vertex array for terrain creation
  430.     Dim L_dD3Normal     As D3DVECTOR                ' Normal array for terrain creation
  431.     Dim L_nRunX         As Long                     ' Runs over X coords during ground mesh generation
  432.     Dim L_nRunZ         As Long                     ' Runs over Z coords during ground mesh generation
  433.     Dim L_nRunI         As Long                     ' Runs over various arrays
  434.     Dim L_nHeight       As Single                   ' Height of point being processed
  435.     
  436.     Dim L_bMap()        As Byte                     ' Holds loaded map data
  437.     Dim L_dDDSD         As DDSURFACEDESC2           ' Surface description for map surface
  438.     Dim L_dArea         As RECT                     ' Rectangle for locking
  439.     Dim L_dDDCK         As DDCOLORKEY               ' Color key for making map transparent
  440.     
  441. ' Code ...
  442.     ' Generate floor data from file ...
  443.                             
  444.         ' Load map into surface
  445.         With L_dDDSD
  446.             .lFlags = DDSD_CAPS
  447.             .ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
  448.         End With
  449.         Set DDSMap = Application.DDInstance.CreateSurfaceFromFile(App.Path + "\map\" + IslandName + ".map", L_dDDSD)
  450.         
  451.         ' Get map properties
  452.         DDSMap.GetSurfaceDesc L_dDDSD
  453.         
  454.         ' Lock surface, retrieve data
  455.         With L_dArea
  456.             .Right = L_dDDSD.lWidth
  457.             .Bottom = L_dDDSD.lHeight
  458.         End With
  459.         DDSMap.Lock L_dArea, L_dDDSD, DDLOCK_NOSYSLOCK Or DDLOCK_WAIT, ByVal 0&
  460.         DDSMap.GetLockedArray L_bMap
  461.         Application.Environment.IslandSize = L_dArea.Right
  462.         
  463.     ' Rebuild floor mesh ...
  464.     
  465.         ' Create floor material
  466.         Set L_oD3Material = Application.D3Instance.CreateMaterial(5)
  467.         L_oD3Material.SetAmbient 1, 1, 1
  468.         
  469.         ' Empty mesh
  470.         I_oD3MeshFloor.Empty
  471.         
  472.         ' Add vertices ...
  473.         
  474.             L_nRunI = 0
  475.             For L_nRunX = 0 To L_dArea.Right - 1
  476.                 For L_nRunZ = 0 To L_dArea.Bottom - 1
  477.                     L_nHeight = ((L_bMap(L_nRunX * 2, L_nRunZ) And 31) / 32)
  478.                     I_oD3MeshFloor.AddVertex L_nRunX, L_nHeight, L_nRunZ
  479.                 Next
  480.             Next
  481.          
  482.         ' Add dummy normal
  483.         I_oD3MeshFloor.AddNormal 0, 1, 0
  484.         
  485.         ' Add faces ...
  486.         
  487.             For L_nRunX = 0 To L_dArea.Right - 2
  488.                 For L_nRunZ = 0 To L_dArea.Bottom - 2
  489.                     
  490.                     ' Check if vertices demand face
  491.                     I_oD3MeshFloor.GetVertex L_nRunX + L_nRunZ * L_dArea.Right, L_dD3Vertex(0)
  492.                     I_oD3MeshFloor.GetVertex L_nRunX + 1 + L_nRunZ * L_dArea.Right, L_dD3Vertex(1)
  493.                     I_oD3MeshFloor.GetVertex L_nRunX + 1 + (L_nRunZ + 1) * L_dArea.Right, L_dD3Vertex(2)
  494.                     I_oD3MeshFloor.GetVertex L_nRunX + (L_nRunZ + 1) * L_dArea.Right, L_dD3Vertex(3)
  495.                     
  496.                     ' Yes: Create face
  497.                     If (L_dD3Vertex(0).Y + L_dD3Vertex(1).Y + L_dD3Vertex(2).Y + L_dD3Vertex(3).Y) > 0 Then
  498.                         
  499.                         ' Set geometry
  500.                         Set L_oD3Face = I_oD3MeshFloor.CreateFace
  501.                         L_oD3Face.AddVertexAndNormalIndexed L_nRunX + L_nRunZ * L_dArea.Right, 0
  502.                         L_oD3Face.AddVertexAndNormalIndexed L_nRunX + 1 + L_nRunZ * L_dArea.Right, 0
  503.                         L_oD3Face.AddVertexAndNormalIndexed L_nRunX + 1 + (L_nRunZ + 1) * L_dArea.Right, 0
  504.                         L_oD3Face.AddVertexAndNormalIndexed L_nRunX + (L_nRunZ + 1) * L_dArea.Right, 0
  505.                         
  506.                         ' Set material
  507.                         L_oD3Face.SetMaterial L_oD3Material
  508.  
  509.                     End If
  510.                     
  511.                 Next
  512.             Next
  513.                         
  514.         ' Adjust vertices and waterline ...
  515.         
  516.             For L_nRunI = 0 To I_oD3MeshFloor.GetVertexCount - 1
  517.                 I_oD3MeshFloor.GetVertex L_nRunI, L_dD3Vertex(0)
  518.                 If L_dD3Vertex(0).Y < 0.15 Then
  519.                     L_dD3Vertex(0).Y = -0.15
  520.                 Else
  521.                     L_dD3Vertex(0).Y = L_dD3Vertex(0).Y * 2
  522.                 End If
  523.                 I_oD3MeshFloor.SetVertex L_nRunI, L_dD3Vertex(0).X, L_dD3Vertex(0).Y, L_dD3Vertex(0).Z
  524.             Next
  525.         
  526.         ' Generate normals
  527.         I_oD3MeshFloor.GenerateNormals 1, D3DRMGENERATENORMALS_PRECOMPACT Or D3DRMGENERATENORMALS_USECREASEANGLE
  528.                             
  529.         ' NOTE: Only now can we apply texture coordinates and mappings,
  530.         ' as applying different coords and mappings to faces sharing
  531.         ' vertices duplicates that vertices, rendering normal calculation
  532.         ' impossible. So the steps are: Build mesh using shared vertices,
  533.         ' let normals be calculated, texture mesh (now that the normals
  534.         ' are known it doesnt matter if the vertices are duplicated!)
  535.             
  536.         ' Set surfaces ...
  537.         For L_nRunI = 0 To I_oD3MeshFloor.GetFaceCount - 1
  538.             With I_oD3MeshFloor.GetFace(L_nRunI)
  539.                                                 
  540.                 ' Acquire position
  541.                 .GetVertex 0, L_dD3Vertex(0), L_dD3Normal
  542.                 L_nRunX = L_dD3Vertex(0).X
  543.                 L_nRunZ = L_dD3Vertex(0).Z
  544.                 
  545.                 'Set texture coordinates
  546.                 .SetTextureCoordinates 0, L_nRunX, L_nRunZ
  547.                 .SetTextureCoordinates 1, L_nRunX + 1, L_nRunZ
  548.                 .SetTextureCoordinates 2, L_nRunX + 1, L_nRunZ + 1
  549.                 .SetTextureCoordinates 3, L_nRunX, L_nRunZ + 1
  550.  
  551.                 ' Set text ure
  552.                  If L_dD3Vertex(0).Y < 0.25 * 2 Or Rnd > 0.2 Then
  553.                     .SetTexture I_oD3TFloor(0)
  554.                 Else
  555.                     .SetTexture I_oD3TFloor(Int(Rnd * 4) + 1)
  556.                 End If
  557.             
  558.             End With
  559.         Next
  560.         
  561.     ' Finish ...
  562.     
  563.         ' Remember height at center
  564.         I_oD3MeshFloor.GetVertex (L_dArea.Bottom \ 2) * L_dArea.Right + L_dArea.Right \ 2, L_dD3Normal
  565.         L_nHeight = L_dD3Normal.Y
  566.  
  567.         ' Unlock surface
  568.         DDSMap.Unlock L_dArea
  569.  
  570.         ' Make surface transparent
  571.         With L_dDDCK
  572.             .low = 0
  573.             .high = 0
  574.         End With
  575.         DDSMap.SetColorKey DDCKEY_SRCBLT, L_dDDCK
  576.           
  577.         ' Set Station pos
  578.         D3Frame.DeleteChild D3FrameStation
  579.         Set D3FrameStation = Application.D3Instance.CreateFrame(D3Frame)
  580.         D3FrameStation.SetPosition Nothing, 0, 0, 0
  581.         D3FrameStation.LoadFromFile App.Path + "\mdl\st1.x", "", D3DRMLOAD_FROMFILE Or D3DRMLOAD_FIRST, Nothing, Nothing
  582.         D3FrameStation.LoadFromFile App.Path + "\mdl\st2.x", "", D3DRMLOAD_FROMFILE Or D3DRMLOAD_FIRST, Nothing, Nothing
  583.         D3FrameStation.GetChildren.GetElement(1).SetRotation Nothing, 0, 1, 0, 0.02
  584.         D3FrameStation.SetPosition Nothing, L_dArea.Right \ 2, L_nHeight - 0.2, L_dArea.Bottom \ 2
  585.  
  586. End Sub
  587.