• Welcome to Dark Bit Factory & Gravity.
 

BlackWhiteShaderMovieDemo / OgreMagic

Started by Emil_halim, March 03, 2007

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Emil_halim

Hi all

Here is an other demo , I was madding it for Ogre.

It is playing movie and uses the tweety sprite as a maske to part of movie that played with some effect such as grayscale , Sharpen and  Emboss effect by using pixel shader program, also use the time line for control the timing of each effect , i.e when the  effect will start and duration of playing for each effect.


''------------------------------------------------------------------------------
''
''              Magic Library For Ogre
''                beta version 0.40
''                  by Emil Halim
''
''        Name    :  ShaderMovieDemo
''        Date    :  03/03/2007
''        Purpose :  applying many shader effects to 
''                   Movie.
''
''       website  :  http://www.freewebs.com/ogremagic/index.htm
''
''------------------------------------------------------------------------------

/'
It is playing movie and uses the tweety sprite as a maske to part of movie
that played with some effect such as grayscale , Sharpen and  Emboss effect by
using pixel shader program, also use the time line for control the timing
of each effect , i.e when the  effect will start and the duration of playing for each effect.
'/

OPTION EXPLICIT

#include once "windows.bi"
#include once "win/d3d9.bi"
#include once "win/d3dx9.bi"

#include once "MagicLibrary.bi"

''-----------------------------------------------------------------------------
'' GLOBALS
''-----------------------------------------------------------------------------
dim shared as HWND              g_hWnd         = NULL
dim shared as LPDIRECT3D9       g_pD3D         = NULL
dim shared as LPDIRECT3DDEVICE9 g_pd3dDevice   = NULL

dim shared as MovieData         ptr   Movie                ' pointer to movie structure
dim shared as SpriteData        ptr   MovieSprite          ' uesed to display the movie
dim shared as SpriteData        ptr   Tweety               ' used as a mask when playing movie
dim shared as TimeLineData      ptr   BlackWhiteTime       ' holds the start time and duration of blackWhite effect   
dim shared as TimeLineData      ptr   SharpenTime          ' holds the start time and duration of Sharpen effect 
dim shared as TimeLineData      ptr   EmbossTime           ' holds the start time and duration of Emboss effect
dim shared as HLSLProgramData   ptr   BlackWhiteProgram    ' shader program for doing blackWhite effect
dim shared as HLSLProgramData   ptr   SharpenProgram       ' shader program for doing Sharpen effect
dim shared as HLSLProgramData   ptr   EmbossProgram        ' shader program for doing Emboss effect 

''-----------------------------------------------------------------------------
'' PROTOTYPES
''-----------------------------------------------------------------------------
declare function WindowProc( byval hWnd as HWND, byval msg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
declare sub init()
declare sub shutDown()
declare sub render()

''-----------------------------------------------------------------------------
'' Name: init()
'' Desc: Initializes Direct3D under DirectX 9.0
''-----------------------------------------------------------------------------
sub init( )
    g_pD3D = Direct3DCreate9( D3D_SDK_VERSION )

    dim as D3DDISPLAYMODE d3ddm

    IDirect3D9_GetAdapterDisplayMode( g_pD3D, D3DADAPTER_DEFAULT, @d3ddm )

    dim as D3DPRESENT_PARAMETERS d3dpp
    clear( d3dpp, 0, len(d3dpp) )

    d3dpp.Windowed               = TRUE
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD
    d3dpp.BackBufferFormat       = d3ddm.Format
    d3dpp.EnableAutoDepthStencil = TRUE
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE

    IDirect3D9_CreateDevice( g_pD3D, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, _
                          D3DCREATE_SOFTWARE_VERTEXPROCESSING, _
                          @d3dpp, @g_pd3dDevice )
    IDirect3DDevice9_SetRenderState(g_pd3dDevice,D3DRS_LIGHTING, FALSE)
    IDirect3DDevice9_SetRenderState(g_pd3dDevice,D3DRS_CULLMODE, D3DCULL_NONE)

dim as D3DXMATRIX mProjection
    D3DXMatrixPerspectiveFovLH( @mProjection, D3DXToRadian( 45.0f ), 1.0f, 1.0f, 100.0f )
    IDirect3DDevice9_SetTransform(g_pd3dDevice, D3DTS_PROJECTION, @mProjection )

                     
   
    '' Initial Magic Library here
    InitialMagicLibrary(g_pd3dDevice,640, 480)
   
    ML_SetMediaDirectory("../MagicMedia/")

    Movie = ML_LoadMovie("movie/ahly_america1-0.wmv")     ' load ahly_america1-0.wmv movie
    ML_SetMovieFramePerSocand(Movie,25)                   ' set movie frame per second to 25
    dim as integer wwidth = ML_GetMovieWidth(Movie)       ' get the movie width for later using
    dim as integer height = ML_GetMovieHeight(Movie)      ' get the movie height for later using
    MovieSprite = ML_CreateStaticSprite(wwidth,height)    ' create an empty sprite with the same size of movie

    Tweety  = ML_LoadStaticSprite("Tweety.Bmp")           ' load Tweety sprite , uesed as a musk
    ML_SetSpriteColorKeyFromPoint(Tweety,0,0)             ' Set the transparent color of Tweety sprite

    ML_SetBaseTimeNow()                                   ' must be called before initializing timers
    BlackWhiteTime = ML_CreateNewTimeLine(0000,30000)     ' start at 0 second and play for 30 seconds
    SharpenTime    = ML_CreateNewTimeLine(30000,40000)    ' start at 30 seconds and play for 40 seconds
    EmbossTime     = ML_CreateNewTimeLine(70000,40000)    ' start at 70 seconds and play for 40 seconds

    BlackWhiteProgram = ML_CreateHLSLProgram()            ' creating BlackWhite shader program

    ML_EditPixelProgram(BlackWhiteProgram)   
    ML_AddToProgram("sampler2D tex0;                                                       ")
    ML_AddToProgram("sampler2D tex1;                                                       ")
    ML_AddToProgram("float4   main(  float2 TexCoord : TEXCOORD0) : COLOR0                 ")
    ML_AddToProgram("{                                                                     ")
    ML_AddToProgram("       float4 col = tex2D( tex0, TexCoord );                          ")
    ML_AddToProgram("       float4 col1= tex2D( tex1, TexCoord );                          ")
    ML_AddToProgram("       float  gray = col1.r * 0.299 + col1.g * 0.587 +col1.b * 0.184; ")
    ML_AddToProgram("       return float4(gray,gray,gray,col.a);                           ")
    ML_AddToProgram("}                                                                     ")
    ML_CompilePixelProgram(BlackWhiteProgram,"main")

    SharpenProgram = ML_CreateHLSLProgram()               ' creating Sharpen shader program
    ML_EditPixelProgram(SharpenProgram)     
    ML_AddToProgram("sampler2D tex0;                                                       ")
    ML_AddToProgram("sampler2D tex1;                                                       ")
    ML_AddToProgram("float4   main(  float2 TexCoord : TEXCOORD0) : COLOR0                 ")
    ML_AddToProgram("{                                                                     ")
    ML_AddToProgram("       float4 col = tex2D( tex0, TexCoord );                          ")
    ML_AddToProgram("       float4 col1= tex2D( tex1, TexCoord );                          ")
    ML_AddToProgram("       float4 ncol1 = tex2D( tex1, TexCoord + float2(0.001,0));       ")
    ML_AddToProgram("       float4 ncol2 = tex2D( tex1, TexCoord + float2(-0.001,0));      ")
    ML_AddToProgram("       float4 ncol3 = tex2D( tex1, TexCoord + float2(0,0.001));       ")
    ML_AddToProgram("       float4 ncol4 = tex2D( tex1, TexCoord + float2(0,-0.001));      ")
    ML_AddToProgram("       float4 ncol5 = tex2D( tex1, TexCoord + float2(0.001,0.001));   ")
    ML_AddToProgram("       float4 ncol6 = tex2D( tex1, TexCoord + float2(0.001,-0.001));  ")
    ML_AddToProgram("       float4 ncol7 = tex2D( tex1, TexCoord + float2(-0.001,0.001));  ")
    ML_AddToProgram("       float4 ncol8 = tex2D( tex1, TexCoord + float2(-0.001,-0.001)); ")
    ML_AddToProgram("       float4 sum   = ncol1+ncol2+ncol3+ncol4+ncol5+ncol6+ncol7+ncol8;")
    ML_AddToProgram("       float4 sharp = (27*col1-2*sum)/9;                              ")
    ML_AddToProgram("       return float4(sharp.r,sharp.g,sharp.b,col.a) ;                 ")
    ML_AddToProgram("}                                                                     ")
    ML_CompilePixelProgram(SharpenProgram,"main")

    EmbossProgram = ML_CreateHLSLProgram()               ' creating Emboss shader program
    ML_EditPixelProgram(EmbossProgram)
    ML_AddToProgram("sampler2D tex0;                                                       ")
    ML_AddToProgram("sampler2D tex1;                                                       ")
    ML_AddToProgram("float4   main(  float2 TexCoord : TEXCOORD0) : COLOR0                 ")
    ML_AddToProgram("{                                                                     ")
    ML_AddToProgram("       float4 col = tex2D( tex0, TexCoord );                          ")
    ML_AddToProgram("       float4 col1= tex2D( tex1, TexCoord );                          ")
    ML_AddToProgram("       float4 ncol1 = tex2D( tex1, TexCoord + float2(0.001,0));       ")
    ML_AddToProgram("       float4 ncol2 = tex2D( tex1, TexCoord + float2(-0.001,0));      ")
    ML_AddToProgram("       float4 Emboss= (2*ncol1-2*ncol2)+float4(.5,.5,.5,1);           ")
    ML_AddToProgram("       float  avrg = (Emboss.r+Emboss.g+Emboss.b)/3;                  ")
    ML_AddToProgram("       return  float4(avrg,avrg,avrg,col.a);                          ")
    ML_AddToProgram("}                                                                     ")
    ML_CompilePixelProgram(EmbossProgram,"main")
   
end sub

''-----------------------------------------------------------------------------
'' Name: render()
'' Desc: Render or draw our scene to the monitor.
''-----------------------------------------------------------------------------
sub render( )
    IDirect3DDevice9_Clear( g_pd3dDevice, 0, NULL, D3DCLEAR_TARGET or D3DCLEAR_ZBUFFER, _
                            D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 )
''
'' begin drawing
''
IDirect3DDevice9_BeginScene(g_pd3dDevice)
   
   
    '' Magic Stuff
    ''
        ML_UseOrthogonalView                                               ' starting 2d mode
       
        ML_SetAlpha(1)                                                     ' set alpha blending factor
        ML_SetColor(255,255,255)
       
        ML_SetBlendMode(ALPHABLEND)                                        ' set blending mod
       
        ML_GetNextMovieFrame(Movie)                                        ' update the movie for next Frame
        ML_SetScale(2,2)                                                   ' set the scale in X-X and Y-Y axis   
        dim as integer wwidth = ML_GetMovieWidth(Movie)                    ' get the wdith of movie for uesed when drawing it to sprite
        dim as integer height= ML_GetMovieHeight(Movie)                    ' get the height of movie for uesed when drawing it to sprite
        ML_DrawMovieFrameToSprite(MovieSprite,0,0,Movie,0,0,wwidth,height) ' draw the movie frame to sprite
        ML_SetSpriteTexture(MovieSprite)                                   ' we must set the sprite texture before drawing it
        ML_DrawSprite(MovieSprite,320,240)                                 ' actually drawing the sprite at 320 , 240 coordinates

        ML_SetSpriteTexture(Tweety)                                        ' pass the sprite image to shader program
        ML_SetSpriteTexture(MovieSprite,1)                                 ' pass the movie image to shader program
        if(ML_CheckFireTime(BlackWhiteTime)) then ML_StartPixelProgram(BlackWhiteProgram) ' if we are in BlackWhite period effect
        if(ML_CheckFireTime(SharpenTime))    then ML_StartPixelProgram(SharpenProgram)    ' if we are in Sharpen period effect
        if(ML_CheckFireTime(EmbossTime))     then ML_StartPixelProgram(EmbossProgram)     ' if we are in Emboss period effect
        ML_DrawSprite(MovieSprite,320,240)                                 ' draw the teh movie by using shader program
        ML_StopShaderProgram()                                             ' we finished our effect and did not need the shader progrem

        ML_UsePerspectiveView                                              ' stop 2D mode and return to Original 3D mode
   
''
'' end drawing
''
IDirect3DDevice9_EndScene(g_pd3dDevice)
    IDirect3DDevice9_Present(g_pd3dDevice, NULL, NULL, NULL, NULL )
   
end sub


''-----------------------------------------------------------------------------
'' Name: WinMain()
'' Desc: The application's entry point
''-----------------------------------------------------------------------------
function WinMain(byval hInstance as HINSTANCE, _
byval hPrevInstance as HINSTANCE, _
byval lpCmdLine as string, _
byval nCmdShow as integer _
) as integer

dim as WNDCLASSEX winClass
dim as MSG        uMsg

with winClass
.lpszClassName = @"MY_WINDOWS_CLASS"
.cbSize        = len(WNDCLASSEX)
.style         = CS_HREDRAW or CS_VREDRAW
.lpfnWndProc   = @WindowProc
.hInstance     = hInstance
.hIcon        = LoadIcon(hInstance, cast(LPCTSTR,IDI_APPLICATION))
    .hIconSm    = LoadIcon(hInstance, cast(LPCTSTR,IDI_APPLICATION))
.hCursor       = LoadCursor(NULL, IDC_ARROW)
.hbrBackground = cast(HBRUSH,GetStockObject(BLACK_BRUSH))
.lpszMenuName  = NULL
.cbClsExtra    = 0
.cbWndExtra    = 0
end with

if( RegisterClassEx( @winClass ) = FALSE ) then
return E_FAIL
end if

g_hWnd = CreateWindowEx( NULL, "MY_WINDOWS_CLASS", _
                             "Direct3D (DX9) - Primitive Types", _
     WS_OVERLAPPEDWINDOW or WS_VISIBLE, _
         0, 0, 640, 480, NULL, NULL, hInstance, NULL )

if( g_hWnd = NULL ) then
return E_FAIL
end if

    ShowWindow( g_hWnd, nCmdShow )
    UpdateWindow( g_hWnd )

init()

do while( uMsg.message <> WM_QUIT )
if( PeekMessage( @uMsg, NULL, 0, 0, PM_REMOVE ) ) then
TranslateMessage( @uMsg )
DispatchMessage( @uMsg )
        else
    render()
end if
loop

shutDown()

    UnregisterClass( "MY_WINDOWS_CLASS", winClass.hInstance )

return uMsg.wParam
end function

''-----------------------------------------------------------------------------
'' Name: WindowProc()
'' Desc: The window's message handler
''-----------------------------------------------------------------------------
function WindowProc( byval hWnd as HWND, _
byval msg as UINT, _
byval wParam as WPARAM, _
byval lParam as LPARAM _
   ) as LRESULT

    select case ( msg )
        case WM_KEYDOWN
select case( wParam )
case VK_ESCAPE
PostQuitMessage(0)

end select

case WM_CLOSE
PostQuitMessage(0)

        case WM_DESTROY
            PostQuitMessage(0)

case else
return DefWindowProc( hWnd, msg, wParam, lParam )
end select

return 0
end function

''-----------------------------------------------------------------------------
'' Name: shutDown()
'' Desc: Release all Direct3D resources.
''-----------------------------------------------------------------------------
sub shutDown( )

    if( g_pd3dDevice <> NULL ) then
        IDirect3DDevice9_Release(g_pd3dDevice)
    end if

    if( g_pD3D <> NULL ) then
        IDirect3D9_Release( g_pD3D )
    end if
end sub

''
''
''
end WinMain( GetModuleHandle( null ), null, Command( ), SW_NORMAL )


Shockwave

Hi Emil, thanks for the code :) Will not work here becuse of my crappy graphics card. Have some Karma for the source.  :)
Shockwave ^ Codigos
Challenge Trophies Won:

Emil_halim

thanks shocky.  :)

oh , does that mean you successfully compiled it with out any error , then you tried it and did not work correctlly?

thank you also for giving plus Karma.

Shockwave

Well at first it would not compile because there were some comments in the source without rem or ' before them.
Then I tried to compile it and it failed on;

declare function ML_GetTextLength cdecl alias "ML_GetTextLength" (byval text as zstring ptr, byval BitmapFont as BitmapFontData ptr=ActiveFont) as single

Expected constant, found )

I think this is because I havean old version of fb, this would not work on this laptop though even if it did compile, I can't run things with shaders. Sorry Emil, I should have posted in more detail.

As for the Karma, you deserve it.
Shockwave ^ Codigos
Challenge Trophies Won: