Page 1 of 1

Direct3D 9 Tutorial 2 - Rendering Vertices

Posted: Fri May 29, 2009 6:18 pm
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

Posted: Fri May 29, 2009 9:20 pm
by idle
thanks fluid

Posted: Sat May 30, 2009 12:00 am
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.

Posted: Sat May 30, 2009 12:07 am
by Fluid Byte
What system you're on? It runs fine here with Vista 64 SP1 and the x86 compiler (4.30b2).

Posted: Sat May 30, 2009 1:21 am
by Mistrel
XP 32-bit PB 4.30 with plenty of DirectX 9 power.

Posted: Sat May 30, 2009 1:47 am
by IceSoft
Same problem here. WXPpro 32bit

Posted: Sat May 30, 2009 4:18 am
by J. Baker
Same issue here. System specs are in my sig.

Posted: Sat May 30, 2009 7:11 am
by idle
comment out

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

works then

Posted: Sat May 30, 2009 11:01 am
by IceSoft
idle wrote:comment out
SetClassLongPtr_(WindowID(0),#GCL_STYLE, #CS_CLASSDC)
works then
Right. Thanks.

Posted: Sat May 30, 2009 1:07 pm
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.

Posted: Sat May 30, 2009 1:30 pm
by srod
Yep works nicely here on Vista 32. Renders blisteringly fast!

Posted: Sun May 31, 2009 3:30 am
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