I've been looking around in the DirectX section for a little while, and i've noticed that people seem to have trouble with pixel shaders. So i decided i might as well post the way that i do it, it is a bit different then the others that i've seen.
So here we go.
First off, you will need my CreateMyShader(...) function:
Code:
HRESULT CreateMyShader(IDirect3DPixelShader9 **Shader_Name, IDirect3DDevice9 *Device, float red, float green, float blue, float alpha )
{
ID3DXBuffer *MyBuffer = NULL;
char MyShader[ 256 ];
sprintf( MyShader, "ps.1.1\ndef c0, %f, %f, %f, %f\nmov r0,c0", red/255, green/255, blue/255, alpha/255 );
D3DXAssembleShader( MyShader, sizeof( MyShader ), NULL, NULL, 0, &MyBuffer , NULL );
if( FAILED( Device->CreatePixelShader((const DWORD*)MyBuffer ->GetBufferPointer(), pShader)) )return E_FAIL;
return S_OK;
}
That is really the hardest park of it all, if you know basic C/DirectX you should pretty much know the rest. Since Some of you might not, ill explain the usage of this function in a bit more detail.
You must first Let the compiler know that a shader will be defined, be declaring:
Code:
IDIRECT3DPIXELSHADER9 Color;
Globally.
Then you must initialize the shader, like so:
Code:
CreateMyShader( Device, Color, Red, Blue, Green, Alpha );
i.e.
//Declaring
IDIRECT3DPIXELSHADER9 Red;
//Initializing
CreateMyShader( Device, Red, 255, 0, 0, 255 );
Hopefully this helped some of you.
Credits:
Myself/J.T. For the CreateMyShader Function
Myself for the guidance
EDIT:
I Forgot to add a few points of interest.
1. If you feel it nessesary, you can initialize the shaders in the CreatePixelShader(...) function.
2. Using a pixel shader
In your DrawIndexedPrimitive (or DrawIndexedPrimitiveUP if you have a memory pointer) Function you can apply a pixel shader to objects.
i.e.
Code:
HRESULT DrawIndexedPrimitive(...)
{
Device->SetPixelShader( Color );
}
I personally use Pixel Shaders to do shadows on players, as well as underwater effects, here are some examples:
Shadows:
i.e.
Code:
IDIRECT3DPIXELSHADER9 Shadow;
Code:
CreateMyShader( Device, Shadow, 10, 10, 10, 15);
// The reason i Put the alpha channel to 15, is so that it will overlay on the model, not cover it
Code:
HRESULT DrawIndexedPrimitive(...)
{
if( Vertex Stride)
{
Device->SetPixelShader( NULL );//clear any previous shaders
Device->SetPixelShader( Shadow );//apply new shader
}
}
Water Glow:
i.e.
Code:
IDIRECT3DPIXELSHADER9 Glow;
Code:
CreateMyShader( Device, Glow, 60, 180, 200, 35);
//Notice i changed the Alpha value to 35, because i want the user to NOT be able to see the model as well (since we are underwater)
TIP: You can use MS Paint to find the color you are looking for in RGB values
Code:
HRESULT DrawIndexedPrimitive(...)
{
if( Vertex Stride)
{
Device->SetPixelShader( NULL );//clear any previous shaders
Device->SetPixelShader( Glow );//apply new shader
}
}
there are LOTS of possibilities with pixel shaders, like using it for lighting (along with other things)
Again, i hope this helped somebody out there, i may or may not add on to this post in the future.
EDIT 2:
Well, again i forgot a important topic: Retrieving Shaders from a DirectX Application
Essencially the way this works, is that DirectX stores the information about the shaders used ingame, and you must retrieve this information.
i.e.
Code:
HRESULT GetPixelShader(...)
{
GetPixelShader(aShader)
}
Then you can alter the shader, then set it back to the model in DrawIntexedPrimitive:
Code:
HRESULT DrawIndexedPrimitive(...)
{
if( Vertex Stride)
{
Device->SetPixelShader( aShader );//apply modified shader
}
}
For the Third time, i hope this helped somebody.
EDIT: why am i getting -Reputation from this post? for "Obviously a rip"