Toon shader question

i have trouble by this question few days
i use a 32*1 texture,it contains four luminance
///////////////////////////////////////////////////
LPDIRECT3DTEXTURE9 g_pTexture = NULL;
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "Toon.bmp", &g_pTexture) ) )
{
if( FAILED(D3DXCreateTextureFromFile( g_pd3dDevice, "..\\Toon.bmp", &g_pTexture) ) )
{
MessageBox(NULL, "Could not find Toon.bmp", "tiger.bmp", MB_OK);
return E_FAIL;
}
}
///////////////////////////////////////////////////
then i set the effect
g_pEffect->SetTexture("Toon",g_pTexture);
in the fx files (the while below)
////////////////////////////////////////////////////
texture Toon;
//////////////
// Globals //
////////////
float4x4 gProjMat; // The projection matrix (Project geometery from 3D to 2D window)
float4x4 gViewMat; // The view matrix (Our current camera view of the world)
float4x4 gWorldMat; // The world matrix (Current world orientation of our object)
float4 view_position;
float4 lightDir;

struct VS_OUTPUT
{
float4 Pos : POSITION0;
float2 TexCoord : TEXCOORD0;
};

sampler ToonShaderTexture= sampler_state
{
Texture = (Toon);
};
/////////////////////////////
// Vertex Shaders //
/////////////////////////////
VS_OUTPUT vs_main(float3 inPos : POSITION0,float3 inNorm : NORMAL0)
{
VS_OUTPUT Out;
float4x4 worldViewProjMat = mul(mul(gWorldMat, gViewMat), gProjMat);
float4x4 WorldViewMat=mul(gWorldMat, gViewMat);
Out.Pos = mul(float4(inPos, 1), worldViewProjMat);

float3 normalW = mul((float3x3)WorldViewMat, inNorm);
// float3 normalW = mul((float3x3)gViewMat, inNorm);
// float3 normalW = mul((float3x3)gWorldMat, inNorm);
// float3 normalW = mul((float3x3)worldViewProjMat, inNorm);

float diffuse =max(0,dot(-lightDir,normalW));
Out.TexCoord.x = diffuse;
Out.TexCoord.y = 0.0f;
return Out;
}
////////////////////////////////////////////////////
struct PS_OUTPUT
{
float4 Color : COLOR0;
};

////////////////////////
// Pixel Shader //
////////////////////////
PS_OUTPUT ps_main( float2 TexCoord : TEXCOORD0)
{
PS_OUTPUT Out;
Out.Color = tex2D(ToonShaderTexture,TexCoord);

return Out;
}
///////////////////////
// Techniques //
///////////////////////
technique DiffuseColor
{
pass First
{
Lighting =FALSE; // We don't want to use vertex lighting
ZEnable = TRUE; // We want z-buffering enabled
VertexShader = compile vs_1_1 vs_main();
PixelShader = compile ps_1_1 ps_main();
}
}
///////////////////////////////////////////////////
when i only use vertexShader it works right
but i turn on the pixelshader it always render the mesh as black color

then i modify the HLSL to
Out.TexCoord.x = 0.5f;
Out.TexCoord.y = 0.5f;

discover whatever coordinate i specify,it doesn't
works but i copy this HLSL code to ATI rendermokey
it works right,
i am so puzzle about this mechanism,
please give me a satisfiable answer
thank every one very much~~

[3309 byte] By [SOLIDBOSS] at [2007-12-25]
# 1

1) In which space are you specifying your light direction?

2) (although it's not the cause of your problem) You're probably not using clamp in your sampler which would look funny for toon shading.

Finally, you can use PIX to debug your shaders and see how your output pixels are computed...

WessamBahnassi at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 2

first give my great thanks to you

i estimate the error could be happen in this area。
but puzzle me a great:i use this framework to do Environment Mapping ,perfect
right,it also use the texture in same place, render in same code,(except creat texture use D3DXCreateCubeTextureFromFile,and different fx),so i get into knots.
i write the my own framework,i use class like this

class mesh
{
public:
D3DXMATRIXA16 mesh_matWorld;
D3DXMATRIXA16 mesh_matProj;
LPD3DXMESH g_pMesh; // Our mesh object in sysmem
D3DMATERIAL9* g_pMeshMaterials ; // Materials for our mesh
LPDIRECT3DTEXTURE9* g_pMeshTextures; // Textures for our mesh
DWORD g_dwNumMaterials ; // Number of mesh materials
LPD3DXEFFECT g_pEffect;
LPD3DXBUFFER pD3DXMtrlBuffer;

public:
void Setmesh_matworld(LPDIRECT3DDEVICE9 pd3dDevice, D3DXMATRIX* pd3dmatrix);
HRESULT CreateEffect(LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strFilename);
HRESULT Create(LPDIRECT3DDEVICE9 pd3dDevice, TCHAR* strFilename);
HRESULT Render(LPDIRECT3DDEVICE9 pd3dDevice);
HRESULT RenderNoEffect(LPDIRECT3DDEVICE9 pd3dDevice);
HRESULT Destroy();

virtual ~mesh();
};
--
i consider about it that i can render each mesh by using a own effect files respective.
in main function
mesh TheMesh; mesh SkyBox;(it is same trouble when render only one mesh)
TheMesh.g_pEffect->SetTexture("Toon",g_pTexture);
TheMesh.Render(g_pd3dDevice);
the Render code as follow:
-
HRESULT mesh::Render(LPDIRECT3DDEVICE9 pd3dDevice)
{
LPDIRECT3DDEVICE9 d3dDevice=pd3dDevice;
d3dDevice->SetTransform( D3DTS_WORLD, &mesh_matWorld);

D3DXMatrixPerspectiveFovLH(&mesh_matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
d3dDevice->SetTransform( D3DTS_PROJECTION, &mesh_matProj);
//D3DXVECTOR4 temp;
D3DXVECTOR4 lightvec;
//lightvec=D3DXVECTOR4(cosf(timeGetTime()/350.0f),sinf(timeGetTime()/350.0f),1.0f,0.0f);
lightvec=D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f);
g_pEffect->SetVector("lightDir",&lightvec);
g_pEffect->SetMatrix( "gWorldMat", &mesh_matWorld);
g_pEffect->SetMatrix( "gProjMat", &mesh_matProj);
g_pEffect->SetTechnique( "DiffuseColor" );
UINT nPass = 0;
g_pEffect->Begin( &nPass, D3DXFX_DONOTSAVESAMPLERSTATE); for( int i = 0; i < nPass ; i++ )
{
g_pEffect->BeginPass( i );
g_pEffect->CommitChanges();
for( int j=0; j< g_dwNumMaterials; j++ )
{
d3dDevice->SetMaterial( &g_pMeshMaterialsIdea );
d3dDevice->SetTexture( 0, g_pMeshTextures[j] );
g_pMesh->DrawSubset( j );
}
g_pEffect->EndPass();
}
g_pEffect->End();
return S_OK;
}
-
is that right?
look forward to reply ,it's very kind of you~:)

by the way why i can't use PIX to debug ,i did it as Tutorials,it doesn,t work because my own framwork~?

SOLIDBOSS at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 3
This line of yours:
SOLIDBOSS wrote:

d3dDevice->SetTexture( 0, g_pMeshTextures[j] );

Is overwriting the results of this line:
SOLIDBOSS wrote:

TheMesh.g_pEffect->SetTexture("Toon",g_pTexture);

Since you're using the effects framework to manage your device state then you should not break it by directly calling into the device to overwrite its states.
Also, your call to SetMaterial() is ineffective because you're already using shaders to shade your objects instead of the fixed-function pipeline.

WessamBahnassi at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 4

thank you very much

this is the point ~~:)

SOLIDBOSS at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...