刚开始学习DX10,发觉模型文件已经从原来的X格式变为SDKMESH格式,也就是说DX10不直接支持X文件了,
那现在该怎么办,我在NVSDK下找到了他的解决方案,先用DX9的接口打开X文件,再用DX10接口来渲染文件。
在DX10下,缺少了很多以前在DX9下的元素。比如,光照,材质等。
要实现这些元素,就必须在SHADER下手动去实现,那就意味着你必须熟悉图形学的内容,特别是其中的光照模型等内容。
比如,方向光的实现:
//directional light-----------------------------------------------------------------
float3 lightDir = g_lightPos - In.worldPos;
float3 lightDirNorm = normalize(lightDir);
float3 SDir = normalize( g_lightPos - g_eyePos);
float cosGammaDir = dot(SDir, V);
float dirLighting = g_Kd*dirLightIntensity*saturate( dot( N,lightDirNorm ) );
//diffuse
float3 diffuseDirLight = dirLighting*exDir;
//airlight
float3 dirAirLight = phaseFunctionSchlick(cosGammaDir)* dirLightIntensity*float3(1-exDir.x,1-exDir.y,1-exDir.z);
//specular
float3 specularDirLight = saturate( pow( dot(lightDirNorm,reflVect),g_specPower)) * dirLightIntensity * g_KsDir * exDir;
点光源的实现:
//point light 1---------------------------------------------------------------------
//diffuse surface radiance and airlight due to point light
float3 pointLightDir = g_PointLightPos - In.worldPos;
//diffuse
float3 diffusePointLight1 = calculateDiffusePointLight(0.1,Dvp,g_DSVPointLight,pointLightDir,N,V);
//airlight
float3 airlight1 = calculateAirLightPointLight(Dvp,g_DSVPointLight,g_VecPointLightEye,V);
//specular
float3 specularPointLight = Specular(g_PointLightIntensity, g_KsPoint, length(pointLightDir), Dvp, g_specPower, normalize(pointLightDir), reflVect);
计算点光源的漫射光:
float3 calculateDiffusePointLight(float Kd,float Dvp,float Dsv,float3 pointLightDir,float3 N,float3 V)
{
float Dsp = length(pointLightDir);
float3 L = pointLightDir/Dsp;
float thetas = acos(dot(N, L));
float lightIntensity = g_PointLightIntensity * 100;
//spotlight
float angleToSpotLight = dot(-L, g_SpotLightDir);
if(g_useSpotLight)
{ if(angleToSpotLight > g_cosSpotlightAngle)
lightIntensity *= abs((angleToSpotLight - g_cosSpotlightAngle)/(1-g_cosSpotlightAngle));
else
lightIntensity = 0;
}
//diffuse contribution
float t1 = exp(-g_beta.x*Dsp)*max(cos(thetas),0)/Dsp;
float4 t2 = g_beta.x*Gtable.SampleLevel(samLinearClamp, float2((g_beta.x*Dsp-g_diffXOffset)*g_diffXScale, (thetas-g_diffYOffset)*g_diffYScale),0)/(2*PI);
float rCol = (t1+t2.x)*exp(-g_beta.x*Dvp)*Kd*lightIntensity/Dsp;
float diffusePointLight = float3(rCol,rCol,rCol);
return diffusePointLight.xxx;
}
计算高光:
float3 Specular(float lightIntensity, float Ks, float Dsp, float Dvp, float specPow, float3 L, float3 VReflect)
{
lightIntensity = lightIntensity * 100;
float LDotVReflect = dot(L,VReflect);
float thetas = acos(LDotVReflect);
float t1 = exp(-g_beta*Dsp)*pow(max(LDotVReflect,0),specPow)/Dsp;
float4 t2 = g_beta.x*G_20table.SampleLevel(samLinearClamp, float2((g_beta.x*Dsp-g_20XOffset)*g_20XScale, (thetas-g_20YOffset)*g_20YScale),0)/(2*PI);
float specular = (t1+t2.x)*exp(-g_beta.x*Dvp)*Ks*lightIntensity/Dsp;
return specular.xxx;
}
下一步,考虑如何不通过DX9接口,直接导入X文件。