alphablending with directx9

Share your advanced PureBasic knowledge/code with the community.
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

alphablending with directx9

Post by coma »

(See here for how using dx9 in pb : viewtopic.php?t=9637)

Code: Select all

;DirectX9 Fullscreen and AlphaBlending

IncludeFile "D3D9.pbi"
IncludeFile "D3D9Interface.pbi"
;D3DBLEND - Defines the supported blend mode
    #D3DBLEND_ZERO = 1
    #D3DBLEND_ONE = 2
    #D3DBLEND_SRCCOLOR = 3
    #D3DBLEND_INVSRCCOLOR = 4
    #D3DBLEND_SRCALPHA = 5
    #D3DBLEND_INVSRCALPHA = 6
    #D3DBLEND_DESTALPHA = 7
    #D3DBLEND_INVDESTALPHA = 8
    #D3DBLEND_DESTCOLOR = 9
    #D3DBLEND_INVDESTCOLOR = 10
    #D3DBLEND_SRCALPHASAT = 11
    #D3DBLEND_BOTHSRCALPHA = 12
    #D3DBLEND_BOTHINVSRCALPHA = 13
    #D3DBLEND_BLENDFACTOR = 14
    #D3DBLEND_INVBLENDFACTOR = 15
    #D3DBLEND_FORCE_DWORD = $7fffffff

;D3DSAMPLERSTATETYPE 
    #D3DSAMP_ADDRESSU = 1
    #D3DSAMP_ADDRESSV = 2
    #D3DSAMP_ADDRESSW = 3
    #D3DSAMP_BORDERCOLOR = 4
    #D3DSAMP_MAGFILTER = 5
    #D3DSAMP_MINFILTER = 6
    #D3DSAMP_MIPFILTER = 7
    #D3DSAMP_MIPMAPLODBIAS = 8
    #D3DSAMP_MAXMIPLEVEL = 9
    #D3DSAMP_MAXANISOTROPY = 10
    #D3DSAMP_SRGBTEXTURE = 11
    #D3DSAMP_ELEMENTINDEX = 12
    #D3DSAMP_DMAPOFFSET = 13
    #D3DSAMP_FORCE_DWORD = $7fffffff

;D3DTEXTUREFILTERTYPE
    #D3DTEXF_NONE            = 0 ; filtering disabled (valid For mip filter only)
    #D3DTEXF_POINT           = 1 ; nearest
    #D3DTEXF_LINEAR          = 2 ; linear interpolation
    #D3DTEXF_ANISOTROPIC     = 3 ; anisotropic
    #D3DTEXF_PYRAMIDALQUAD   = 6 ; 4-sample tent
    #D3DTEXF_GAUSSIANQUAD    = 7 ; 4-sample gaussian
    #D3DTEXF_FORCE_DWORD     = $7fffffff ;force 32-bit size enum
    
Global D3D.IDirect3D9 
Global D3DDevice.IDirect3DDevice9
Global D3DTexture.IDirect3DTexture9
Global D3DVertexBuffer.IDirect3DVertexBuffer9 
Global hWnd.l 
Global fullscreen
Global blending
Global CustomSize
Global ElapsedTime.f, CurrentTime.f, LastTime.f 
Global Xrot.f, Yrot.f, Zrot.f, add1.f, add2.f, add3.f

Global matWorld.D3DXMATRIX 
Global matTranslation.D3DXMATRIX 
Global matRotation.D3DXMATRIX
#D3DFVF_CUBE_VERTEX = #D3DFVF_XYZ | #D3DFVF_TEX1

Structure CubeVertex
    x.f : y.f : z.f    ; vertices coordinates
    u.f : v.f           ; texture coordinates
EndStructure
CustomSize.l = SizeOf(CubeVertex)
Dim cubeVertices.CubeVertex(23)
cubeVertices(0)\x=-1 : cubeVertices(0)\y= 1 : cubeVertices(0)\z=-1 : cubeVertices(0)\u=0.0 : cubeVertices(0)\v=0.0
cubeVertices(1)\x= 1 : cubeVertices(1)\y= 1 : cubeVertices(1)\z=-1 : cubeVertices(1)\u=1.0 : cubeVertices(1)\v=0.0
cubeVertices(2)\x=-1 : cubeVertices(2)\y=-1 : cubeVertices(2)\z=-1 : cubeVertices(2)\u=0.0 : cubeVertices(2)\v=1.0
cubeVertices(3)\x= 1 : cubeVertices(3)\y=-1 : cubeVertices(3)\z=-1 : cubeVertices(3)\u=1.0 : cubeVertices(3)\v=1.0
cubeVertices(4)\x=-1 : cubeVertices(4)\y= 1 : cubeVertices(4)\z=1 : cubeVertices(4)\u=1 : cubeVertices(4)\v=0
cubeVertices(5)\x=-1 : cubeVertices(5)\y=-1 : cubeVertices(5)\z=1 : cubeVertices(5)\u=1 : cubeVertices(5)\v=1
cubeVertices(6)\x= 1 : cubeVertices(6)\y= 1 : cubeVertices(6)\z=1 : cubeVertices(6)\u=0 : cubeVertices(6)\v=0
cubeVertices(7)\x= 1 : cubeVertices(7)\y=-1 : cubeVertices(7)\z=1 : cubeVertices(7)\u=0 : cubeVertices(7)\v=1
cubeVertices(8)\x=-1 : cubeVertices(8)\y= 1 : cubeVertices(8)\z= 1 : cubeVertices(8)\u=0 : cubeVertices(8)\v=0
cubeVertices(9)\x= 1 : cubeVertices(9)\y= 1 : cubeVertices(9)\z= 1 : cubeVertices(9)\u=1 : cubeVertices(9)\v=0
cubeVertices(10)\x=-1 : cubeVertices(10)\y= 1 : cubeVertices(10)\z=-1 : cubeVertices(10)\u=0 : cubeVertices(10)\v=1
cubeVertices(11)\x= 1 : cubeVertices(11)\y= 1 : cubeVertices(11)\z=-1 : cubeVertices(11)\u=1 : cubeVertices(11)\v=1
cubeVertices(12)\x=-1 : cubeVertices(12)\y=-1 : cubeVertices(12)\z= 1 : cubeVertices(12)\u=0 : cubeVertices(12)\v=0
cubeVertices(13)\x=-1 : cubeVertices(13)\y=-1 : cubeVertices(13)\z=-1 : cubeVertices(13)\u=0 : cubeVertices(13)\v=1
cubeVertices(14)\x= 1 : cubeVertices(14)\y=-1 : cubeVertices(14)\z= 1 : cubeVertices(14)\u=1 : cubeVertices(14)\v=0
cubeVertices(15)\x= 1 : cubeVertices(15)\y=-1 : cubeVertices(15)\z=-1 : cubeVertices(15)\u=1 : cubeVertices(15)\v=1
cubeVertices(16)\x= 1 : cubeVertices(16)\y= 1 : cubeVertices(16)\z=-1 : cubeVertices(16)\u=0 : cubeVertices(16)\v=0
cubeVertices(17)\x= 1 : cubeVertices(17)\y= 1 : cubeVertices(17)\z= 1 : cubeVertices(17)\u=1 : cubeVertices(17)\v=0
cubeVertices(18)\x= 1 : cubeVertices(18)\y=-1 : cubeVertices(18)\z=-1 : cubeVertices(18)\u=0 : cubeVertices(18)\v=1
cubeVertices(19)\x= 1 : cubeVertices(19)\y=-1 : cubeVertices(19)\z= 1 : cubeVertices(19)\u=1 : cubeVertices(19)\v=1
cubeVertices(20)\x=-1 : cubeVertices(20)\y= 1 : cubeVertices(20)\z=-1 : cubeVertices(20)\u=1 : cubeVertices(20)\v=0
cubeVertices(21)\x=-1 : cubeVertices(21)\y=-1 : cubeVertices(21)\z=-1 : cubeVertices(21)\u=1 : cubeVertices(21)\v=1
cubeVertices(22)\x=-1 : cubeVertices(22)\y= 1 : cubeVertices(22)\z= 1 : cubeVertices(22)\u=0 : cubeVertices(22)\v=0
cubeVertices(23)\x=-1 : cubeVertices(23)\y=-1 : cubeVertices(23)\z= 1 : cubeVertices(23)\u=0 : cubeVertices(23)\v=1

;***********************************************
;               Init DX9 Screen
;***********************************************
Procedure InitD3D()
    hWnd = OpenWindow(1, 0, 0, 640, 480, #WS_OVERLAPPEDWINDOW|#PB_Window_ScreenCentered , "DirectX 9 - texture blending")
    D3D = Direct3DCreate9_(#D3D_SDK_VERSION) 
    If fullscreen=0
        D3DPP.D3DPRESENT_PARAMETERS
        D3DPP\Windowed = #TRUE
        D3DPP\SwapEffect = #D3DSWAPEFFECT_DISCARD
        D3DPP\BackBufferFormat = #D3DFMT_UNKNOWN 
        D3DPP\EnableAutoDepthStencil = #TRUE
        D3DPP\AutoDepthStencilFormat = #D3DFMT_D16      
    Else
        D3DPP\BackBufferWidth = 640
        D3DPP\BackBufferHeight = 480
        D3DPP\BackBufferFormat = #D3DFMT_R5G6B5 ;16 bit mode - #D3DFMT_XR8G8B8 for 32bit
        D3DPP\BackBufferCount = 1
        D3DPP\MultiSampleType = #D3DMULTISAMPLE_NONE
        D3DPP\SwapEffect = #D3DSWAPEFFECT_DISCARD
        D3DPP\Windowed = #FALSE
        D3DPP\EnableAutoDepthStencil = #TRUE
        D3DPP\AutoDepthStencilFormat = #D3DFMT_D16
        D3DPP\FullScreen_RefreshRateInHz = #D3DPRESENT_RATE_DEFAULT
    EndIf 
    D3D\CreateDevice(#D3DADAPTER_DEFAULT,#D3DDEVTYPE_HAL,hWnd,#D3DCREATE_SOFTWARE_VERTEXPROCESSING,@D3DPP,@D3DDevice)
EndProcedure
;***********************************************
;               Init Scene
;***********************************************
Procedure InitScene()  
    ; Load texture
    file$ = OpenFileRequester("Select texture file (bmp/dds/dib/hdr/jpg/pfm/png/ppm/tga)", "", "*.*", 0)
    If Len(file$)=0 : End : EndIf
    D3DXCreateTextureFromFile_(D3DDevice, file$, @D3DTexture)
    D3DDevice\SetSamplerState(0, #D3DSAMP_MINFILTER, #D3DTEXF_LINEAR)
    D3DDevice\SetSamplerState(0, #D3DSAMP_MAGFILTER, #D3DTEXF_LINEAR)

    D3DDevice\SetRenderState(#D3DRS_LIGHTING, #FALSE) ; disable Lighting
    If blending=0
        D3DDevice\SetRenderState(#D3DRS_ZENABLE, #TRUE)
        D3DDevice\SetRenderState(#D3DRS_CULLMODE, #D3DCULL_CCW) ; counterclockwise culling
    Else    
        D3DDevice\SetRenderState(#D3DRS_ALPHABLENDENABLE, #TRUE) ; Enable alphablending
        D3DDevice\SetRenderState(#D3DRS_SRCBLEND, #D3DBLEND_SRCALPHA)
        D3DDevice\SetRenderState(#D3DRS_DESTBLEND, #D3DBLEND_ONE)
        D3DDevice\SetRenderState(#D3DRS_CULLMODE, #D3DCULL_NONE) ; turn off culling
        D3DDevice\SetRenderState(#D3DRS_ZENABLE, #FALSE)
    EndIf

    ; create the projection matrix    
    matProj.D3DXMATRIX
    D3DXMatrixPerspectiveFovLH_(@matProj, #D3DX_PI/4, 640.0/480.0, 1.0, 1000.0)
    D3DDevice\SetTransform(#D3DTS_PROJECTION, @matProj)

    ;Create VertexBuffer
    Str(D3DDevice\CreateVertexBuffer (24*customsize, 0, #D3DFVF_CUBE_VERTEX, #D3DPOOL_DEFAULT, @D3DVertexBuffer, #NULL))
    *Ptr = 0 
    D3DVertexBuffer\Lock(0,0, @*Ptr, 0) 
        CopyMemory(@cubeVertices(0), *Ptr, 24*customsize) 
    D3DVertexBuffer\Unlock() 
EndProcedure
;***********************************************
;               Render
;***********************************************
Procedure Render()
    D3DDevice\Clear( 0, #NULL, #D3DCLEAR_TARGET | #D3DCLEAR_ZBUFFER,  0, 1.0, 0 ) ; Clear screen and zbuffer
    D3DDevice\BeginScene()  

    Xrot.f+0.04*Sin(add1.f)
    Yrot.f+0.04*Sin(add2.f)
    Zrot+0.04*Sin(add3.f)
    add1.f+0.01
    add2.f+0.05
    add3.f+0.02

    D3DXMatrixTranslation_(@matTranslation, 0.0, 0.0, 5.0+2*Sin(add1))
    D3DXMatrixRotationYawPitchRoll_(@matRotation, Xrot, Yrot, Zrot)
    d3dxMatrixmultiply_(@matWorld, @matRotation, @matTranslation)
    D3DDevice\SetTransform( #D3DTS_WORLD, @matWorld )

    D3DDevice\SetStreamSource( 0, D3DVertexBuffer, 0, Customsize )
    D3DDevice\SetTexture(0, D3DTexture)
    D3DDevice\SetFVF(#D3DFVF_CUBE_VERTEX)   
  
    D3DDevice\DrawPrimitive( #D3DPT_TRIANGLESTRIP,  0, 2 ); // Side #1 starts at vertex 0 and has two triangles.
    D3DDevice\DrawPrimitive( #D3DPT_TRIANGLESTRIP,  4, 2 ); // Side #2 starts at vertex 4 and has two triangles.
    D3DDevice\DrawPrimitive( #D3DPT_TRIANGLESTRIP,  8, 2 ); // Side #3 starts at vertex 8 and has two triangles.
    D3DDevice\DrawPrimitive( #D3DPT_TRIANGLESTRIP, 12, 2 ); // Side #4 starts at vertex 12 and has two triangles.
    D3DDevice\DrawPrimitive( #D3DPT_TRIANGLESTRIP, 16, 2 ); // Side #5 starts at vertex 16 and has two triangles.
    D3DDevice\DrawPrimitive( #D3DPT_TRIANGLESTRIP, 20, 2 ); // Side #6 starts at vertex 20 and has two triangles.

    D3DDevice\EndScene()
    D3DDevice\Present( #NULL, #NULL, #NULL, #NULL )
EndProcedure

;*********************************************

If MessageRequester("Select", "Fullscreen mode ?", #PB_MessageRequester_YesNo)=6 : fullscreen=1 : EndIf
If MessageRequester("Select", "Alpha Blending ?", #PB_MessageRequester_YesNo)=6 : blending=1 : EndIf
InitD3D()
InitScene()

;MAIN LOOP
Repeat 
    Render()
Until WindowEvent()=#PB_EventCloseWindow Or GetAsyncKeyState_(#VK_ESCAPE)

;cleanup
D3DVertexBuffer\Release()
D3DTexture\Release()
D3DDevice\Release()
D3D\Release()
End
Motu23
New User
New User
Posts: 9
Joined: Mon Aug 04, 2003 9:48 pm
Location: Germany
Contact:

Post by Motu23 »

Nice work.
Anyway I have a Compiler (3.90) problem with it.


Direct3DCreate9_(#D3D_SDK_VERSION)
"Direct3DCreate9_() ist not a function, an array or a linked list"

I have removed the InterfaceDX.res from the res foulder and downloaded the Include files from Codemonger.com.
What can be wrong :?:
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

You have to put the appropriate files in your PureLibraries directory.
these files are in dx-pack.zip (viewtopic.php?t=9637), but it seems that the links are broken.

So, I have made another little pack > http://azazello.free.fr/dx9.zip

with this, it should work.
benny
Enthusiast
Enthusiast
Posts: 465
Joined: Fri Apr 25, 2003 7:44 pm
Location: end of www
Contact:

Post by benny »

@coma:
really nice examples ... :D
regards,
benny!
-
pe0ple ar3 str4nge!!!
Motu23
New User
New User
Posts: 9
Joined: Mon Aug 04, 2003 9:48 pm
Location: Germany
Contact:

It's working!

Post by Motu23 »

Thank you very much!

But i still have a question left...

What exactly is included in the Lib files? Do they include all directX9 graphics commands, or just the one you used in the example?
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

d3d9 only include the function Direct3DCreate9 (the only needed)
d3dx9 include a lot of functions (almost all the functions of d3dx9.dll)

(these libs are made with the dll importer of purebasic)
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Thank you very much coma! With this, we can import png images with alpha! Everyone doing graphics is looking for this. It's coming exactly when I need it. :P

You're the best ;)
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

djes wrote:Thank you very much coma! With this, we can import png images with alpha! Everyone doing graphics is looking for this. It's coming exactly when I need it. :P

You're the best ;)
@djes!??
hi man! are you the djes from france and possible we were in contact some years ago (we talked about a 32k version of bubble bobble clone i released on amiga) - if so, its great to see you here! welcome ;-) how are you? (just PM me)
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

Hey! Did you receive my private message? 8)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Coma,
Your example don't work here. At first it says that all Interfaces in d3dinterface.pbi are already declared, except IDirect3DXBuffer.
When i delete all the already declared from d3dinterface.pbi, it is displayed message saying that the GetCommandLineA function is not found in D3D9.DLL, and i have it in /windows/system32/ folder :cry:
Of course i copied purelibraries and d3dx9.dll
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Post by djes »

You have another library that is declaring this function. Try to remove them.
Post Reply