Code: Select all
;////////////////////////////////////////////////////////////////
;//
;// Project Title: DirectX 9C for PureBasic
;// File Title: Light Test
;// Author :  Vallan
;// Author ot inclusdes and the basic: Steven "Dreglor" Garcia
;// Based on "Vertex Buffers"
;//
;////////////////////////////////////////////////////////////////
;Note: This code is Not optimised And I am new With it. It dosent have a better visualisation, because I am Not so familar With Rtoations And meshes...
;- Includes
IncludePath "Includes\DirectX\"
XIncludeFile "d3dx9.pbi"
;- Constants
#D3DFVF_MY_VERTEX = #D3DFVF_XYZ | #D3DFVF_DIFFUSE 
;- Structures
Structure Vertex
  x.f
  y.f
  z.f
  Color.l
EndStructure
;- Globals
Global *D3D.IDIRECT3D9
Global *D3DDevice.IDIRECT3DDEVICE9
Global *VertexBuffer.IDIRECT3DVERTEXBUFFER9
;- Procedures
Procedure InitD3D9()
  
  If hwnd = #Null
    
    *D3D = Direct3DCreate9(#D3D_SDK_VERSION)
    ProcedureReturn #True
  Else
    
    ProcedureReturn #False
  EndIf
EndProcedure
Procedure D3D9_OpenWindowedScreen(Window.l, x.l, y.l, Width.l, Height.l, Title.s, WindowFlags.l = #PB_Window_ScreenCentered | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_TitleBar)
  
  If *D3D <> #Null And *D3DDevice = #Null
    
    If *D3DDevice <> #Null
      
      ;Device Check
      *D3DDevice\Release()
      *D3DDevice = #Null
    EndIf
    
    Current.D3DDISPLAYMODE
    If *D3D\GetAdapterDisplayMode(#D3DADAPTER_DEFAULT, Current) = #D3D_Ok
      
      ;Back Buffer Check
      If *D3D\CheckDeviceType(#D3DADAPTER_DEFAULT, #D3DDEVTYPE_HAL, Current\Format, Current\Format, #True) = #D3D_Ok
        
        ;Z-buffer Check
        If *D3D\CheckDeviceFormat(#D3DADAPTER_DEFAULT, #D3DDEVTYPE_HAL, Current\Format, #D3DUSAGE_DEPTHSTENCIL, #D3DRTYPE_SURFACE, #D3DFMT_D16) = #D3D_Ok
          
          DevCaps.d3dcaps9
          If *D3D\GetDeviceCaps(#D3DADAPTER_DEFAULT, #D3DDEVTYPE_HAL, DevCaps) = #D3D_Ok
            
            If DevCaps\VertexProcessingCaps <> 0
              
              BehaviorFlags = BehaviorFlags | #D3DCREATE_HARDWARE_VERTEXPROCESSING ;if it can
            Else
              
              BehaviorFlags = BehaviorFlags | #D3DCREATE_SOFTWARE_VERTEXPROCESSING ;if it can't
            EndIf
          Else
            
            ;"GetDeviceCaps" Failed
            ProcedureReturn #False
          EndIf
          
          hwnd.l = OpenWindow(Window, x, y, Width, Height, Title, WindowFlags)
          
          If hwnd <> #Null
            
            Rendering.D3DPRESENT_PARAMETERS
            Rendering\BackBufferCount = 1
            Rendering\MultiSampleType = #D3DMULTISAMPLE_NONE
            Rendering\MultiSampleQuality = 0
            Rendering\SwapEffect = #D3DSWAPEFFECT_DISCARD
            Rendering\hDeviceWindow = hwnd
            Rendering\flags = #Null
            Rendering\FullScreen_RefreshRateInHz = #D3DPRESENT_RATE_DEFAULT
            Rendering\PresentationInterval = #D3DPRESENT_INTERVAL_DEFAULT
            Rendering\BackBufferFormat = Current\Format
            Rendering\EnableAutoDepthStencil = #True
            Rendering\AutoDepthStencilFormat = #D3DFMT_D16
            Rendering\Windowed = #True
            Rendering\BackBufferWidth = 0
            Rendering\BackBufferHeight = 0
            
            If *D3D\CreateDevice(#D3DADAPTER_DEFAULT, #D3DDEVTYPE_HAL, hwnd, BehaviorFlags, Rendering, @*D3DDevice) = #D3D_Ok
              
              ProcedureReturn #True
            Else
              
              ;Creation of the device failed
              CloseWindow(Window)
              ProcedureReturn #False
            EndIf
          Else
            
            ;Window Failed
            ProcedureReturn #False
          EndIf
        Else
          
          ;16bit z-buffer does not work
          ProcedureReturn #False
        EndIf
      Else
        
        ;the back buffer format failed
        ProcedureReturn #False
      EndIf
    Else
      
      ;Failed to get the current mode.
      ProcedureReturn #False
    EndIf
  Else
    
    ;D3D not initilized!
    ProcedureReturn #False
  EndIf
EndProcedure
Procedure D3D9_CloseScreen(Window)
  
  If *D3DDevice <> #Null
    
    ;Check D3DDevice
    *D3DDevice\Release()
    *D3DDevice = #Null
  EndIf
  
  If IsWindow(Window)
    
    CloseWindow(Window)
  EndIf
  
  ProcedureReturn #True
EndProcedure
Procedure D3D9_Shutdown(Window)
  If *D3DDevice <> #Null
    
    *D3DDevice\Release()
    *D3DDevice = #Null
  EndIf
  
  If IsWindow(Window)
    
    CloseWindow(Window)
  EndIf
  
  
  If *D3D <> #Null
    
    *D3D\Release()
    *D3D = #Null
  EndIf
  
  ProcedureReturn #True
EndProcedure
Macro D3D9_Clear(Color = 0)
  *D3DDevice\Clear(#Null, #Null, #D3DCLEAR_TARGET | #D3DCLEAR_ZBUFFER, Color, 1.0, 0)
EndMacro
Macro D3D9_FlipBuffers(Void = 0)
  *D3DDevice\Present(#Null, #Null, #Null, #Null )
EndMacro
Macro D3D9_StartDrawing(Void = 0)
  *D3DDevice\BeginScene()
EndMacro
Macro D3D9_StopDrawing(Void = 0)
  *D3DDevice\EndScene()
EndMacro
If InitD3D9() = #True
  If D3D9_OpenWindowedScreen(0, 10, 10, 640, 480, "DirectX For PB: Vertex Buffers")  = #True
    
    
    
 Structure CUSTOMVERTEX
 x.f
 y.f
 z.f
 Normalx.f
 Normaly.f
 Normalz.f
EndStructure
    
      #ZahlTris = 12
   
   Dim Tri.CUSTOMVERTEX(   #ZahlTris  -1)
   
   ;Front First Triangle   : Is there a way to render WQuads instead of Triangles ?
   
   Tri(0)\x = -1
   Tri(0)\y = 1
   Tri(0)\z = 2 
   Tri(0)\Normalx = 0
   Tri(0)\Normaly = 0 
   Tri(0)\Normalz = -1
   
   Tri(1)\x = 1
   Tri(1)\y = 1
   Tri(1)\z = 2 
   Tri(1)\Normalx = 0
   Tri(1)\Normaly = 0 
   Tri(1)\Normalz = -1
   
   Tri(2)\x = 1
   Tri(2)\y = -1
   Tri(2)\z = 2 
   Tri(2)\Normalx = 0
   Tri(2)\Normaly = 0 
   Tri(2)\Normalz = -1
    ;Front second Triangle
 
   
   Tri(3)\x = 1 
   Tri(3)\y = -1
   Tri(3)\z = 2 
   Tri(3)\Normalx = 0
   Tri(3)\Normaly = 0 
   Tri(3)\Normalz = -1
   
   
   Tri(4)\x = -1
   Tri(4)\y = -1
   Tri(4)\z = 2 
   Tri(4)\Normalx = 0
   Tri(4)\Normaly = 0 
   Tri(4)\Normalz = -1
   
   Tri(5)\x = -1
   Tri(5)\y = 1
   Tri(5)\z = 2 
   Tri(5)\Normalx = 0
   Tri(5)\Normaly = 0 
   Tri(5)\Normalz = -1
   
   
   ; The same For the back.
   
   
   Tri(11)\x = -1 
   Tri(11)\y = 1
   Tri(11)\z = 2 
   Tri(11)\Normalx = 0
   Tri(11)\Normaly = 0 
   Tri(11)\Normalz = 1
   
   Tri(10)\x = 1
   Tri(10)\y = 1
   Tri(10)\z = 2 
   Tri(10)\Normalx = 0
   Tri(10)\Normaly = 0 
   Tri(10)\Normalz = 1
   
   Tri(9)\x = 1
   Tri(9)\y = -1
   Tri(9)\z = 2 
   Tri(9)\Normalx = 0
   Tri(9)\Normaly = 0 
   Tri(9)\Normalz = 1
   
   Tri(8)\x = 1
   Tri(8)\y = -1
   Tri(8)\z = 2 
   Tri(8)\Normalx = 0
   Tri(8)\Normaly = 0 
   Tri(8)\Normalz = 1
   
   
   Tri(7)\x = -1
   Tri(7)\y = -1
   Tri(7)\z = 2 
   Tri(7)\Normalx = 0
   Tri(7)\Normaly = 0 
   Tri(7)\Normalz = 1
   
   Tri(6)\x = -1
   Tri(6)\y = 1
   Tri(6)\z = 2 
   Tri(6)\Normalx = 0
   Tri(6)\Normaly = 0 
   Tri(6)\Normalz = 1
   
#D3DFVF_CUSTOMVERTEX = #D3DFVF_XYZ|#D3DFVF_NORMAL
    
    If *D3DDevice\CreateVertexBuffer( #ZahlTris * SizeOf(CUSTOMVERTEX), 0, #D3DFVF_CUSTOMVERTEX, #D3DPOOL_DEFAULT, @*VertexBuffer.IDIRECT3DVERTEXBUFFER9, #Null) = #D3D_Ok
      
      VertexMemory = #Null
      If *VertexBuffer\Lock(0, #ZahlTris * SizeOf(CUSTOMVERTEX), @VertexMemory, 0) = #D3D_Ok
        
        CopyMemory(Tri(), VertexMemory, #ZahlTris * SizeOf(CUSTOMVERTEX))
        
        *VertexBuffer\Unlock()
      Else
        
        ;vertex buffer failed to lock
        End
      EndIf
    Else
      
      ;vertex buffer failed to be created
      End
    EndIf
    
    
    
    
      Material.D3DMATERIAL9 
  
Material\Ambient\r = 1.0
Material\Ambient\g = 1.0
Material\Ambient\b = 0.0
Material\Ambient\a = 1.0
Material\Diffuse\r = Material\Ambient\r 
Material\Diffuse\g = Material\Ambient\g 
Material\Diffuse\b = Material\Ambient\b 
Material\Diffuse\a = Material\Ambient\a 
*D3DDevice\SetMaterial( @Material );
    
DirectionVector.D3DXVECTOR3
Light.D3DLight9 
Light\Type = #D3DLIGHT_DIRECTIONAL
Light\Diffuse\r = 1.0
Light\Diffuse\g = 1.0
Light\Diffuse\b = 1.0
Light\Range = 1000.0
 DirectionVector\x = 0
 DirectionVector\y = 1
 DirectionVector\z = 0
D3DXVec3Normalize( @Light\Direction, @DirectionVector )
*D3DDevice\SetLight( 0, @Light )
    
*D3DDevice\LightEnable( 0, #True)  
    
 
    
    
    *D3DDevice\SetRenderState( #D3DRS_LIGHTING, #True)
    *D3DDevice\SetRenderState( #D3DRS_AMBIENT, $00202020 )
    
    Projection.D3DXMATRIX 
    D3DXMatrixPerspectiveFovLH(Projection, D3DXToRadian(45.0), (640.0 / 480.0), 0.1, 100.0)
    *D3DDevice\SetTransform(#D3DTS_PROJECTION, Projection)
    
    Translate.D3DMATRIX
    Rotate.D3DMATRIX
    World.D3DMATRIX    
    D3DXMatrixTranslation(Translate, 0.0, 0.0, 20.0)
    
   
    
    Repeat
    
      
    
      CurrentTime.l = timeGetTime_()
      ElapsedTime.f = ((CurrentTime - LastTime) * 0.01)
      LastTime.l    = CurrentTime
      
      fXrot.f + (5 * ElapsedTime)
      fYrot.f + (6 * ElapsedTime)
      fZrot.f + (7 * ElapsedTime)
      
       D3DXMatrixRotationYawPitchRoll(Rotate, D3DXToRadian(fXrot), D3DXToRadian(fYrot), D3DXToRadian(fZrot))
     
     
      D3DXMatrixMultiply(World, Rotate, Translate)
 
;
       *D3DDevice\SetTransform(#D3DTS_WORLD, World)
      
      D3D9_Clear(D3DCOLOR_COLORVALUE(0.75, 0.75, 1.0, 1.0))
      D3D9_StartDrawing()
      
          *D3DDevice\SetMaterial( @Material )
      
      *D3DDevice\SetStreamSource( 0, *VertexBuffer, 0, SizeOf(CUSTOMVERTEX) )
      *D3DDevice\SetFVF(#D3DFVF_CUSTOMVERTEX)
      
      *D3DDevice\DrawPrimitive(#D3DPT_TRIANGLELIST,  0, 4)
   
   
      
      D3D9_StopDrawing()
      D3D9_FlipBuffers()
    Until WindowEvent() = #PB_Event_CloseWindow ;check for the “X” to be click
  EndIf
EndIf
D3D9_Shutdown(0)
; IDE Options = PureBasic v4.00 (Windows - x86)
; CursorPosition = 24
; Folding = w-
It is not very good, but I think this is the effect of the light.