Page 1 of 1

DirectX for PB Tutorial Series

Posted: Sun Oct 16, 2005 9:26 am
by Dreglor
Here is the first of my series, using my includes from here viewtopic.php?p=106882#106882
you can follow along with the source to learn Directx9

DirectX_For_PB_-_Direct3d_Initialization_.zip
Image

notes: if there is any problems/errors with the tutorial then either email me or pm me.

Posted: Sun Oct 16, 2005 2:26 pm
by sigi
Cool, thank You. :)

Posted: Mon Oct 17, 2005 9:03 am
by Comtois
thank you
In the near future I hope to get those october includes for directx 3d fully surported and implemnted.
can't wait :)

Posted: Tue Nov 22, 2005 4:29 am
by Dreglor
sorry guys for taking so much time in completing the second tutorial
problem was that when i was converting the october includes i didn't relize that bools were considered as longs in pb which screwed up some structures and function api very frustrating ugh
its now fixed

you need to download the new includes (that come with the libs so no need for downloading the sdk)

once you have those you can download the source and pick it apart :)

DirectX_For_PB_-_Direct3d_FullScreen_Initialization_.zip
Image

Posted: Thu Nov 24, 2005 8:23 am
by Dreglor
well heres another one
im amazed to see how easy it was to do after i got the bugs worked out of the includes :D

pick up the 3 part of the directx for pb tutorial series

DirectX_For_PB_-_Direct3d_Triangle.zip
Image

Posted: Thu Nov 24, 2005 10:55 pm
by Shannara
Hot dang and awesome! So let me get this straight. You are converting the tutorials/samples from the October 2005 DirectX 9.0c SDK? If so, super awesome. I was dinking around with the Managed tutorials from that SDK on Monday. They have one called, CustomUI .. .. .. .. :) Are you planning on heading that direction?

Posted: Fri Nov 25, 2005 10:14 am
by Dreglor
im going by samples but not all are from the directx sdk
the managed code is somthing i haven't looked into too much
im not sure whats the difference of the normal C and the managed besides the different methods they use.
i'll look into that example for you if it isn't to steep to read then i might go and make a example out of it ;)

Posted: Sat Dec 17, 2005 2:09 pm
by DarkDragon
Doesn't work here. I have DX 9 here, but this:

Code: Select all

Procedure Render()
  ;right now it's rendering nothing a empty scene with the clear color as bright green
  ;the following function clears the entire target and zbuffer buffers, once, and replaces it with a bright green color and with a depth of 1
  *D3DDevice\Clear(0, #Null, #D3DCLEAR_TARGET | #D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(0.0,1.0,0.0,1.0), 1.0, 0 )
  ;In most cases you will used this clear with these paramerter maybe a color change but you get the idea.
  ;We now start the rendering process by calling BeginScene().
  *D3DDevice\BeginScene()
  ;Sense these are empty scenes we promptly end the scene with EndScene().
  *D3DDevice\EndScene()
  ;Now we flip buffers with present teh 4 nulls are paramerters for what to flip using null it defaults to the entire back buffer.
  *D3DDevice\Present(#Null, #Null, #Null, #Null)
  ;If it wasn't already self evident purebasic's start/stopdrawing clearscreen and flipbuffers are not to off from these functions.
EndProcedure
Doesn't clear the window. You just see this:
http://www.bradan.net/images/056.jpg

[EDIT]
And in the DX_Triangles_Sample is an invalid memory access in this line:

Code: Select all

*D3DDevice\Clear(0,#Null, #D3DCLEAR_TARGET | #D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(0.0,0.0,0.0,1.0), 1.0, 0 ) ;clear green

Posted: Wed Aug 23, 2006 1:12 pm
by Vallan
Could you write an example how to use lights?

Edit: I did try it again and I did made it:

But: Is the light rotating, too, or only the Mesh?

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.