Direct3D 9 Tutorial 2 - Rendering Vertices

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Direct3D 9 Tutorial 2 - Rendering Vertices

Post by Fluid Byte »

http://msdn.microsoft.com/en-us/library/bb206282.aspx

Code: Select all

; ------------------------------------------------------------------------------------------------------
; Tutorial 2 - Rendering Vertices
; ------------------------------------------------------------------------------------------------------

; Constants
#D3DFMT_UNKNOWN = 0
#D3DSWAPEFFECT_DISCARD = 1
#D3DDEVTYPE_HAL = 1
#D3DADAPTER_DEFAULT = 0
#D3DCREATE_SOFTWARE_VERTEXPROCESSING = 32
#D3D_SDK_VERSION = 31
#D3DCLEAR_TARGET = 1
#D3DPOOL_DEFAULT = 0
#D3DFVF_XYZRHW = 4
#D3DFVF_DIFFUSE = 64
#D3DPT_TRIANGLELIST = 4

; Structures
Structure D3DPRESENT_PARAMETERS
	BackBufferWidth.l
	BackBufferHeight.l
	BackBufferFormat.l
	BackBufferCount.l
	MultiSampleType.l
	MultiSampleQuality.l
	SwapEffect.l
	hDeviceWindow.l
	Windowed.l
	EnableAutoDepthStencil.l
	AutoDepthStencilFormat.l
	flags.l
	FullScreen_RefreshRateInHz.l
	PresentationInterval.l
EndStructure

; Macros
Macro D3DCOLOR_ARGB(A,R,G,B)
	(A & $FF) << 24 | (R & $FF) << 16 | (G & $FF) << 8 | (B & $FF)
EndMacro

Macro D3DCOLOR_XRGB(R,G,B)
	D3DCOLOR_ARGB($FF,R,G,B)
EndMacro

; ------------------------------------------------------------------------------------------------------
; EXAMPLE
; ------------------------------------------------------------------------------------------------------

Declare WindowProc(hWnd,uMsg,wParam,lParam)
Declare Render()
Declare Cleanup()

; Direct3D Interfaces
Global g_pD3D.IDirect3D9
Global g_pd3dDevice.IDirect3DDevice9
Global g_pVB.IDirect3DVertexBuffer9

; Create the application's window
OpenWindow(0,0,0,300,300,"Direct3D Tutorial 02: Rendering Vertices",#WS_OVERLAPPEDWINDOW | 1)
SetClassLongPtr_(WindowID(0),#GCL_STYLE,#CS_DBLCLKS | #CS_CLASSDC)
SetWindowCallback(@WindowProc())

; Initializing Direct3D
OpenLibrary(0,"D3D9.dll")

g_pD3D = CallFunction(0,"Direct3DCreate9",#D3D_SDK_VERSION)

d3dpp.D3DPRESENT_PARAMETERS
d3dpp\Windowed = #True
d3dpp\SwapEffect = #D3DSWAPEFFECT_DISCARD
d3dpp\BackBufferFormat = #D3DFMT_UNKNOWN

g_pD3D\CreateDevice(#D3DADAPTER_DEFAULT,#D3DDEVTYPE_HAL,WindowID(0),#D3DCREATE_SOFTWARE_VERTEXPROCESSING,d3dpp,@g_pd3dDevice)

; Defining a Custom Vertex Type
#D3DFVF_CUSTOMVERTEX = #D3DFVF_XYZRHW | #D3DFVF_DIFFUSE

Structure CUSTOMVERTEX
	X.f : Y.f : Z.f : RHW.f	; The transformed position for the vertex.
	Color.l						; The vertex color.
EndStructure

; Initialize values for three custom vertices
Global Dim vertices.CUSTOMVERTEX(2)

Procedure SetVertex(Index,X,Y,Z,RHW,Color)
	vertices(Index)\X = X
	vertices(Index)\Y = Y
	vertices(Index)\Z = Z
	vertices(Index)\RHW = RHW
	vertices(Index)\Color = Color
EndProcedure

SetVertex(0,150.0, 50.0,0.5,1.0,$FFFF0000) ; X, Y, Z, RHW, Color
SetVertex(1,250.0,250.0,0.5,1.0,$FF00FF00)
SetVertex(2, 50.0,250.0,0.5,1.0,$FF00FFFF)

; Setting up the vertex buffer
g_pd3dDevice\CreateVertexBuffer(3 * SizeOf(CUSTOMVERTEX),0,#D3DFVF_CUSTOMVERTEX,#D3DPOOL_DEFAULT,@g_pVB,0)
 
; Fill buffer with data from the custom vertices
g_pVB\Lock(0,3 * SizeOf(CUSTOMVERTEX),@*pVertices,0)
CopyMemory(vertices(),*pVertices,3 * SizeOf(CUSTOMVERTEX))
g_pVB\Unlock()

; // Main Loop //
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend

; ------------------------------------------------------------------------------------------------------
; FUNCTIONS
; ------------------------------------------------------------------------------------------------------

Procedure WindowProc(hWnd,uMsg,wParam,lParam)
	Select uMsg
		Case #WM_DESTROY
		Cleanup()
		ProcedureReturn 0
		
		Case #WM_PAINT
		Render()
		ValidateRect_(hWnd,0)
		ProcedureReturn 0
	EndSelect
    
   ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Procedure Render()
	g_pd3dDevice\Clear(0,0,#D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0,0)
	g_pd3dDevice\BeginScene()
	
	; Rendering the Display
	g_pd3dDevice\SetStreamSource(0,g_pVB,0,SizeOf(CUSTOMVERTEX))
	g_pd3dDevice\SetFVF(#D3DFVF_CUSTOMVERTEX)
	g_pd3dDevice\DrawPrimitive(#D3DPT_TRIANGLELIST,0,1)

	g_pd3dDevice\EndScene()	
	g_pd3dDevice\Present(0,0,0,0)
EndProcedure

; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

Procedure Cleanup()
	If g_pd3dDevice ! 0 : g_pd3dDevice\Release() : EndIf
	If g_pD3D       ! 0 : g_pD3D\Release() : EndIf
	If g_pVB        ! 0 : g_pVB\Release() : EndIf
EndProcedure
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

thanks fluid
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I just get an plain, empty window (no black background or anything). The window's client area won't refresh if I resize it though.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

What system you're on? It runs fine here with Vista 64 SP1 and the x86 compiler (4.30b2).
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

XP 32-bit PB 4.30 with plenty of DirectX 9 power.
User avatar
IceSoft
Addict
Addict
Posts: 1695
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

Same problem here. WXPpro 32bit
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Same issue here. System specs are in my sig.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

comment out

SetClassLongPtr_(WindowID(0),#GCL_STYLE, #CS_CLASSDC)

works then
User avatar
IceSoft
Addict
Addict
Posts: 1695
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

idle wrote:comment out
SetClassLongPtr_(WindowID(0),#GCL_STYLE, #CS_CLASSDC)
works then
Right. Thanks.
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Well spotted idle. Dunno why that causes it on XP to fail. I guess that part is redundant anyway since I don't register a window class but use a native PD window instead. Applying this style afterwards seems to cause problems even though I don't know if it's working at all. But that's the way it was demonstrated in the tutorial.

Btw, I'm gonna merge all 6 MSDN tutorials to one and post it in this thread. So stay tuned for updates.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yep works nicely here on Vista 32. Renders blisteringly fast!
I may look like a mule, but I'm not a complete ass.
User avatar
idle
Always Here
Always Here
Posts: 5899
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

just cleaned it up a bit and messed it up a bit more

glad I posted it, as, I managed to crash the PC due to my @#$$% crappy video card driver.

put the render into a thread instead, though it really needs to be monitored for event like if the window get minimized or you pop up task manager.

Code: Select all


; ------------------------------------------------------------------------------------------------------
; Tutorial 2 - Rendering Vertices
; ------------------------------------------------------------------------------------------------------
EnableExplicit
; Constants
#D3DFMT_UNKNOWN = 0
#D3DSWAPEFFECT_DISCARD = 1
#D3DDEVTYPE_HAL = 1
#D3DADAPTER_DEFAULT = 0
#D3DCREATE_SOFTWARE_VERTEXPROCESSING = 32
#D3D_SDK_VERSION = 31
#D3DCLEAR_TARGET = 1
#D3DPOOL_DEFAULT = 0
#D3DFVF_XYZRHW = 4
#D3DFVF_DIFFUSE = 64
#D3DPT_TRIANGLELIST = 4

#D3DFVF_CUSTOMVERTEX = #D3DFVF_XYZRHW | #D3DFVF_DIFFUSE

; Structures
Structure D3DPRESENT_PARAMETERS
   BackBufferWidth.l
   BackBufferHeight.l
   BackBufferFormat.l
   BackBufferCount.l
   MultiSampleType.l
   MultiSampleQuality.l
   SwapEffect.l
   hDeviceWindow.l
   Windowed.l
   EnableAutoDepthStencil.l
   AutoDepthStencilFormat.l
   flags.l
   FullScreen_RefreshRateInHz.l
   PresentationInterval.l
EndStructure

Structure CUSTOMVERTEX
   X.f
   Y.f
   Z.f
   RHW.f     ; The transformed position for the vertex.
   Color.l   ; The vertex color.
EndStructure

; Macros
Macro D3DCOLOR_ARGB(A,R,G,B)
   (A & $FF) << 24 | (R & $FF) << 16 | (G & $FF) << 8 | (B & $FF)
EndMacro

Macro D3DCOLOR_XRGB(R,G,B)
   D3DCOLOR_ARGB($FF,R,G,B)
EndMacro

Declare Render(void)
Declare Cleanup()
Declare InitDX9(hwnd)
Declare SetVertex(Index,X,Y,Z,RHW,Color)
; Direct3D Interfaces
Global g_pD3D.IDirect3D9
Global g_pd3dDevice.IDirect3DDevice9
Global g_pVB.IDirect3DVertexBuffer9
Global d3dpp.D3DPRESENT_PARAMETERS
;app vars
Global WID, hWnd, *pVertices, Event ,VertextBufferSize 
Global Dim vertices.CUSTOMVERTEX(2)

Procedure SetVertex(Index,X,Y,Z,RHW,Color)
  vertices(Index)\X = X
  vertices(Index)\Y = Y
  vertices(Index)\Z = Z
  vertices(Index)\RHW = RHW
  vertices(Index)\Color = Color
EndProcedure

Procedure InitDX9(hwnd)

 OpenLibrary(0,"D3D9.dll")

 g_pD3D = CallFunction(0,"Direct3DCreate9",#D3D_SDK_VERSION)
 
 d3dpp\Windowed = #True
 d3dpp\SwapEffect = #D3DSWAPEFFECT_DISCARD
 d3dpp\BackBufferFormat = #D3DFMT_UNKNOWN

 g_pD3D\CreateDevice(#D3DADAPTER_DEFAULT,#D3DDEVTYPE_HAL,hwnd,#D3DCREATE_SOFTWARE_VERTEXPROCESSING,d3dpp,@g_pd3dDevice)
 
 ProcedureReturn g_pD3D

EndProcedure
 
Procedure Render(void)
  
  While 1
   
  g_pd3dDevice\Clear(0,0,#D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0,0)
  g_pd3dDevice\BeginScene()
  g_pd3dDevice\SetStreamSource(0,g_pVB,0,SizeOf(CUSTOMVERTEX))
  g_pd3dDevice\SetFVF(#D3DFVF_CUSTOMVERTEX)
  g_pd3dDevice\DrawPrimitive(#D3DPT_TRIANGLELIST,0,1)
  g_pd3dDevice\EndScene()   
  g_pd3dDevice\Present(0,0,0,0)
  
  Delay(20)
  
  Wend 
 
EndProcedure
 
Procedure Cleanup()
  If g_pd3dDevice
    g_pd3dDevice\Release()
    g_pD3D\Release()
    g_pVB\Release()
  EndIf
EndProcedure

 
If OpenWindow(0,0,0,300,300,"Direct3D Tutorial 02: Rendering Vertices",#WS_OVERLAPPEDWINDOW | 1)
  hwnd = WindowID(0)
  If initDX9(hwnd)
   
   
    SetVertex(0,150.0, 50.0,0.5,1.0,$FFFF0000) 
    SetVertex(1,250.0,250.0,0.5,1.0,$FF00FF00)
    SetVertex(2, 50.0,250.0,0.5,1.0,$FF00FFFF)
    VertextBufferSize = 3 * SizeOf(CUSTOMVERTEX)
   
    g_pd3dDevice\CreateVertexBuffer(VertextBufferSize,0,#D3DFVF_CUSTOMVERTEX,#D3DPOOL_DEFAULT,@g_pVB,0)
    g_pVB\Lock(0,VertextBufferSize,@*pVertices,0)
    CopyMemory(vertices(),*pVertices,VertextBufferSize)
    g_pVB\Unlock()
    
    CreateThread(@render(),0)
       
    Repeat
     
    Event = WaitWindowEvent()
         
     
    Until Event = #PB_Event_CloseWindow
        
    Cleanup()
 
  Else
     MessageRequester("DX9","failed To create surface")
  EndIf

EndIf 
Post Reply