由于BLOOD渲染器的API由DX9轉向DX10,在轉化架構和改寫代碼的過程中,出現很多異常。
在測試過程中,發現很多錯誤都是由于沒有進行正確的初始化造成的。比如分配內存的容間,變量的正確賦值等等。已調完VB,IB,TEXTURE,RENDERTARGET等部分,還有MESH等大部未調。
特此MARK,以作記錄。
2008年12月8日 #
2008年11月9日 #
DX9下使用
D3DXPLANE plane;
D3DXPlaneFromPointNormal( &plane, &vPoint, &vNormal ); //生成這個平面
D3DXMatrixReflect( &matReflect, &plane ); //取得該平面的反射矩陣
//設置剪切平面,使反射面上的內容被渲染,面下的被丟棄
m_pd3dDevice->SetClipPlane( 0, plane );
m_pd3dDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, 0x01 );
DX10下模擬:
User Clip PlanesUser Clip Planes are emulated by specifying a clip distance output from the Vertex Shader with the SV_ClipDistance[n] flag, where n is either 0 or 1. Each component can hold up to 4 clip distances in x, y, z, and w giving a total of 8 clip distances.
用戶裁減平面通過在VS里設定一個SV_ClipDistance[n]標記,定義一個裁減距離輸出得到,n為0或1。這里一共能存放8個Clip Plane距離,分別使用數組兩個元素的x,y,z,w通道。
In this scenario, each clip planes is defined by a plane equation of the form:
在這個場景里,每個clip plane被一個平面方程定義:
Ax + By + Cz + D =0;
Where <A,B,C> is the normal of the plane, and D is the distance of the plane from the origin. Plugging in any point <x,y,z> into this equation gives its distance from the plane. Therefore, all points <x,y,z> that satisfy the equation Ax + By + Cz + D = 0 are on the plane. All points that satisfy Ax + By + Cz + D < 0 are below the plane. All points that satisfy Ax + By + Cz + D > 0 are above the plane.
<A,B,C>是平面法向,D是平面到原點的距離。把任意點<x,y,z>代入方程能得到它到平面的距離。所有滿足方程=0的點在平面上,<0的點在平面下而 >0的點在平面上。
In the Vertex Shader, each vertex is tested against each plane equation to produce a distance to the clip plane. Each of the three clip distances are stored in the first three components of the output component with the semantic SV_ClipDistance0. These clip distances get interpolated over the triangle during rasterization and clipped if the value every goes below 0.
在VS中,每個頂點會帶入平面方程做測試。每個三角形的Clip距離存在SV_ClipDistance0語義輸出的前三個通道中。這個距離在光柵化中被線性插值,所有小于0的像素被剔除。
2008年10月23日 #
2008年9月18日 #
2008年8月11日 #
2008年8月6日 #
為了這次面試,我休息了很多天,都沒工作。結果,得到的又是一個失敗的結局。分析原因,都認為我的C++基礎很薄弱吧,要不就是因為我以前從事的工作和現在的工作差別太大了。
作為一名新手,我很迷茫,在學習的道路我沒有被打敗,卻在兩次面試中,深受打擊。我不僅在想,這是否是我要選擇的路。僅剩半個多月的時間,暑期就結束了,我再不出去,就沒機會了。事實上,做不做游戲,我無所謂,但現在的生活一直不是我想要的。我不斷地在否定自已的路上,越走越遠,找不到真實的我。
兩個月很快就過去了,我只能靜下心來去做事情,把這些煩人的事,都拋于腦后。路還是要走,人總是要成長的,希望能從失敗中走出來。
記下今天的失敗,以作自勉。
2008年7月20日 #
At the top level, the rendering steps look like the following:
Fixed-function Lighting Pipeline
ColorsOutput CalcLighting( float3 worldNormal, float3 worldPos, float3 cameraPos )
{
ColorsOutput output = (ColorsOutput)0.0;
for(int i=0; i<8; i++)
{
//光線方向
float3 toLight = g_lights[i].Position.xyz - worldPos;
//離光源距離
float lightDist = length( toLight );
//atten=(1/(a[2]*d*d+a[1]*d+a[1])
float fAtten = 1.0/dot( g_lights[i].Atten, float4(1,lightDist,lightDist*lightDist,0) );
float3 lightDir = normalize( toLight );
//H
float3 halfAngle = normalize( normalize(-cameraPos) + lightDir );
//Phong方程,逐頂點光照
output.Diffuse += max(0,dot( lightDir, worldNormal ) * g_lights[i].Diffuse * fAtten) + g_lights[i].Ambient;
output.Specular += max(0,pow( dot( halfAngle, worldNormal ), 64 ) * g_lights[i].Specular * fAtten );
}
return output;
}
簡單ALPHA測試
//
// PS for rendering with alpha test
//
float4 PSAlphaTestmain(PSSceneIn input) : COLOR0
{
float4 color = tex2D( g_samLinear, g_txDiffuse, input.tex ) * input.colorD;
if( color.a < 0.5 )
discard;
return color;
}
霧化:
//
// Calculates fog factor based upon distance
//
// E is defined as the base of the natural logarithm (2.71828)
float CalcFogFactor( float d )
{
float fogCoeff = 1.0;
if( FOGMODE_LINEAR == g_fogMode )
{
fogCoeff = (g_fogEnd - d)/(g_fogEnd - g_fogStart);
}
else if( FOGMODE_EXP == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*g_fogDensity );
}
else if( FOGMODE_EXP2 == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*d*g_fogDensity*g_fogDensity );
}
return clamp( fogCoeff, 0, 1 );
}
Finally, the pixel shader uses the fog factor to determine how much of the original color and how much of the fog color to output to the pixel.
最后,PS使用霧化參數確定霧顏色和紋理顏色的混合程度。
return fog * normalColor + (1.0 - fog)*g_fogColor;
2008年7月17日 #
剛開始學習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文件。
2008年7月15日 #