toon shader question

Started by
2 comments, last by solidboss 17 years, 6 months ago
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~~
Advertisement
The shader looks ok, the texture loading looks ok (and checks for errors) and you say it works in RenderMonkey...


That makes me think that either:

A) the way you're using the effect system is causing the texture to not be set on the device.

OR/AND

B) you're doing something between setting the texture in the effect and drawing the object that prevents the texture from being seen.


Some questions/things to check:

1) Do you call ID3DXEffect::SetTexture() inside a ID3DXEffect::BeginPass()/
ID3DXEffect::EndPass() block?
If so, have you remembered to call ID3DXEffect::CommitChanges() so the change is set on the device?

2) Do you do your rendering inside a ID3DXEffect::Begin()/
ID3DXEffect::End() and ID3DXEffect::BeginPass()/
ID3DXEffect::EndPass() blocks? If you don't, then the shader may not have been correctly set on the device.

3) Try linking with d3dx9d.lib to see if it gives you any extra debug output compared to d3dx9.lib.

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

TRY: turn off distance fog
http://www.vectorstudios.nethttp://engine.vectorstudios.net
first give my great thanks to you

A) the way you're using the effect system is causing the texture to not be set on the device.
B) you're doing something between setting the texture in the effect and drawing the object that prevents the texture from being seen.

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_pMeshMaterials );
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~:)

[Edited by - solidboss on October 11, 2006 9:52:46 AM]

This topic is closed to new replies.

Advertisement