but now i want to use pure directx and some of the best of irrlicht like models material .
to make more simple the shader on the wrapper i modify the engine just 2 render directx
i change the setVertexShader
Code: Select all
//! sets right vertex shader
void CD3D9Driver::setVertexShader(E_VERTEX_TYPE newType)
{
if (newType != LastVertexType)
{
LastVertexType = newType;
HRESULT hr = 0;
switch(newType)
{
case EVT_STANDARD:
{
D3DVERTEXELEMENT9 vertDecln [ ] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL , 0 },
{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR , 0 },
{ 0, 28, D3DDECLTYPE_FLOAT2 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()};
LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration = NULL;
pID3DDevice->CreateVertexDeclaration( vertDecln, &pVertexDeclaration );
hr=pID3DDevice->SetVertexDeclaration( pVertexDeclaration );
}
break;
case EVT_2TCOORDS:
{
D3DVERTEXELEMENT9 vertDecl2t [ ] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL , 0 },
{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR , 0 },
{ 0, 28, D3DDECLTYPE_FLOAT2 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 36, D3DDECLTYPE_FLOAT2 , D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 },
D3DDECL_END()
};
LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration = NULL;
pID3DDevice->CreateVertexDeclaration( vertDecl2t, &pVertexDeclaration );
hr=pID3DDevice->SetVertexDeclaration( pVertexDeclaration );
}
break;
case EVT_TANGENTS:
{
D3DVERTEXELEMENT9 declaration[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
{ 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, 28, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 },
{ 0, 48, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 },
D3DDECL_END()
};
LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration = NULL;
pID3DDevice->CreateVertexDeclaration( declaration, &pVertexDeclaration );
hr=pID3DDevice->SetVertexDeclaration( pVertexDeclaration );
}
break;
}
if (FAILED(hr))
{
os::Printer::log("Could not set vertex Shader.", ELL_ERROR);
return;
}
}
}
Code: Select all
//=============================================================================
// NormalMap.fx by Frank Luna And Terry Myke (C) 2007 All Rights Reserved.
//
// Does per-pixel lighting where the normals are obtained from a normal map.
//=============================================================================
struct Mtrl
{
float4 ambient;
float4 diffuse;
float4 spec;
float specPower;
};
struct DirLight
{
float4 ambient;
float4 diffuse;
float4 spec;
};
uniform extern float4x4 gWorldInv;
uniform extern float4x4 gWVP;
uniform extern Mtrl gMtrl;
uniform extern DirLight gLight;
uniform extern float3 gEyePosW;
uniform extern float3 dirW;
uniform extern texture gTex;
uniform extern texture gNormalMap;
sampler TexS = sampler_state
{
Texture = <gTex>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler NormalMapS = sampler_state
{
Texture = <gNormalMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
struct OutputVS
{
float4 posH : POSITION0;
float3 toEyeT : TEXCOORD0;
float3 lightDirT : TEXCOORD1;
float2 tex0 : TEXCOORD2;
};
OutputVS NormalMapVS(float3 posL : POSITION0,
float3 tangentL : TANGENT0,
float3 binormalL : BINORMAL0,
float3 normalL : NORMAL0,
float2 tex0 : TEXCOORD0)
{
// Zero out our output.
OutputVS outVS = (OutputVS)0;
// Build TBN-basis.
float3x3 TBN;
TBN[0] = tangentL;
TBN[1] = binormalL;
TBN[2] = normalL;
// Matrix transforms from object space to tangent space.
float3x3 toTangentSpace = transpose(TBN);
// Transform eye position to local space.
float3 eyePosL = mul(float4(gEyePosW, 1.0f), gWorldInv);
// Transform to-eye vector to tangent space.
float3 toEyeL = eyePosL - posL;
outVS.toEyeT = mul(toEyeL, toTangentSpace);
// Transform light direction to tangent space.
float3 lightDirL = mul(float4(dirW, 0.0f), gWorldInv).xyz;
outVS.lightDirT = mul(lightDirL, toTangentSpace);
// Transform to homogeneous clip space.
outVS.posH = mul(float4(posL, 1.0f), gWVP);
// Pass on texture coordinates to be interpolated in rasterization.
outVS.tex0 = tex0;
// Done--return the output.
return outVS;
}
float4 NormalMapPS(float3 toEyeT : TEXCOORD0,
float3 lightDirT : TEXCOORD1,
float2 tex0 : TEXCOORD2) : COLOR
{
// Interpolated normals can become unnormal--so normalize.
toEyeT = normalize(toEyeT);
lightDirT = normalize(lightDirT);
// Light vector is opposite the direction of the light.
float3 lightVecT = -lightDirT;
// Sample normal map.
float3 normalT = tex2D(NormalMapS, tex0);
// Expand from [0, 1] compressed interval to true [-1, 1] interval.
normalT = 2.0f*normalT - 1.0f;
// Make it a unit vector.
normalT = normalize(normalT);
// Compute the reflection vector.
float3 r = reflect(-lightVecT, normalT);
// Determine how much (if any) specular light makes it into the eye.
float t = pow(max(dot(r, toEyeT), 0.0f), gMtrl.specPower);
// Determine the diffuse light intensity that strikes the vertex.
float s = max(dot(lightVecT, normalT), 0.0f);
// If the diffuse light intensity is low, kill the specular lighting term.
// It doesn't look right to add specular light when the surface receives
// little diffuse light.
if(s <= 0.0f)
t = 0.0f;
// Compute the ambient, diffuse and specular terms separatly.
float3 spec = t*(gMtrl.spec*gLight.spec).rgb;
float3 diffuse = s*(gMtrl.diffuse*gLight.diffuse).rgb;
float3 ambient = gMtrl.ambient*gLight.ambient;
// Get the texture color.
float4 texColor = tex2D(TexS, tex0);
// Combine the color from lighting with the texture color.
float3 color = (ambient + diffuse)*texColor.rgb + spec;
// Output the color and the alpha.
return float4(color, gMtrl.diffuse.a*texColor.a);
}
technique NormalMapTech
{
pass P0
{
// Specify the vertex and pixel shader associated with this pass.
vertexShader = compile vs_2_0 NormalMapVS();
pixelShader = compile ps_2_0 NormalMapPS();
}
}
this function is 4 debug the mesh tangent (Normal,Binormal,Tangent)
Code: Select all
DllExp void xDrawMeshDebug(IMesh* mesh)
{
if (!mesh) return;
SMaterial material;
material.Lighting = false;
core::matrix4 tras;
driver->setMaterial(material);
driver->setTransform(ETS_WORLD,tras);
int iLoop,iMeshBuffer;
S3DVertex *s3d_verts;
S3DVertex2TCoords *texture_verts;
S3DVertexTangents *tangent_verts;
for ( iMeshBuffer = 0; iMeshBuffer < mesh->getMeshBufferCount(); iMeshBuffer++ )
{
// mesh->getMeshBuffer(0)->getVertexCount
IMeshBuffer *mb = mesh->getMeshBuffer(iMeshBuffer);
int iVertexCount = mb->getVertexCount();
switch ( mb->getVertexType())
{
case EVT_STANDARD:
s3d_verts = (S3DVertex *)mb->getVertices();
for ( iLoop = 0; iLoop < iVertexCount; iLoop++ )
{
core::vector3df pA = s3d_verts[iLoop].Pos;
core::vector3df nA = s3d_verts[iLoop].Normal;
driver->draw3DLine(pA,pA+nA,0xFF00FF00);
}
break;
case EVT_2TCOORDS:
texture_verts = (S3DVertex2TCoords *)mb->getVertices();
for ( iLoop = 0; iLoop < iVertexCount; iLoop++ )
{
core::vector3df pA = texture_verts[iLoop].Pos;
core::vector3df nA = texture_verts[iLoop].Normal;
driver->draw3DLine(pA,pA+nA,0xFF00FF00);
}
break;
case EVT_TANGENTS:
tangent_verts = (S3DVertexTangents *)mb->getVertices();
for ( iLoop = 0; iLoop < iVertexCount; iLoop++ )
{
core::vector3df pA = tangent_verts[iLoop].Pos;
core::vector3df nA = tangent_verts[iLoop].Normal;
core::vector3df bA = tangent_verts[iLoop].Binormal;
core::vector3df tA = tangent_verts[iLoop].Tangent;
driver->draw3DLine(pA,pA+nA,0xFF00FF00);
driver->draw3DLine(pA,pA+bA,0xFFFF0000);
driver->draw3DLine(pA,pA+tA,0xFF0000FF);
}
break;
}
}//
}