220 likes | 376 Views
Displacement mapping. Kezdeti teendők. Letöltés: OgreDisplacementMappingBase.zip Kicsomagol Futtat: OgreDisplacement.sln Include és library útvonalak beállítása Working directory beállítása Fordít Futtat. Próba. Material scheme. … OIS:: InputManager * OISInputManager ;
E N D
Kezdeti teendők • Letöltés: OgreDisplacementMappingBase.zip • Kicsomagol • Futtat: OgreDisplacement.sln • Include és library útvonalak beállítása • Working directory beállítása • Fordít • Futtat
Material scheme … OIS::InputManager* OISInputManager; int schemecount = 1; int currentScheme = 0; StringqualitySchemes[] = { "VeryLow", "Low", "Medium", "Height", "VeryHeight", "Ultra" }; … voidsetupScene() { … renderWindow->setActive(true); renderWindow->getViewport(0)->setMaterialScheme(qualitySchemes[0]); … materialPhong { technique { schemeVeryLow pass : PhongPass …
if( mKeyboard->isKeyDown(OIS::KC_SPACE) && mTimeUntilNextToggle < 0) { currentScheme = (++currentScheme)%schemecount; renderWindow->getViewport(0)->setMaterialScheme(qualitySchemes[currentScheme]); mTimeUntilNextToggle = 0.2f; } • Minden sémához új technika a phong.material-ba • Minden technikának új pixel shadert írunk • (ha implementáltunk egy új sémát schemecount ++)
normal mapping materialPhong { technique { schemeVeryLow … } technique { schemeLow pass { vertex_program_refPhongShadingDisplacedVS { } fragment_program_refPhongShadingNormalMappedPS { } } } }
Phong.program vertex_program PhongShadingVShlsl { } fragment_program PhongShadingPShlsl { } vertex_program PhongShadingDisplacedVShlsl { sourcePhongDisplaced.hlsl entry_pointdisplacedVS targetvs_2_0 } fragment_program PhongShadingNormalMappedPShlsl { sourcePhongDisplaced.hlsl entry_pointnormalMappedPS targetps_2_0 }
PhongDisplaced.hlsl struct VERTEX_IN { float4 position : POSITION; float4 normal : NORMAL; float2 texCoord : TEXCOORD0; float4 tangent : TANGENT; float4 binormal : BINORMAL; }; struct VERTEX_OUT { float4 hPos : POSITION; float3 N : TEXCOORD0; float3 L : TEXCOORD1; float3 V : TEXCOORD2; float2 texCoord : TEXCOORD3; float3 T : TEXCOORD4; float3 B : TEXCOORD5; };
VERTEX_OUT displacedVS(VERTEX_IN In, uniform float4x4 worldViewProj, uniform float4x4 worldView, uniform float4x4 worldViewIT, uniform float4 lightPos) { VERTEX_OUT Out = (VERTEX_OUT) 0; Out.hPos = mul(worldViewProj, In.position); Out.texCoord = In.texCoord; Out.V = -mul(worldView, In.position).xyz; Out.L = lightPos.xyz + lightPos.w * Out.V; Out.N = mul(worldViewIT, In.normal).xyz; Out.T = mul(worldViewIT, In.tangent).xyz; Out.B = mul(worldViewIT, In.binormal).xyz; return Out; }
float2 dtdx = ddx(In.texCoord); float2 dtdy = ddy(In.texCoord); float3 dpdx = ddx(-In.V); float3 dpdy = ddy(-In.V); float3 T = normalize(dpdx * dtdy.y - dpdy * dtdx.y); float3 B =normalize(dpdy * dtdx.x - dpdx * dtdy.x) ; float3 N = cross(T, B); N *= sign(-dot(N, -In.V)); sampler2D colorTex : register(s0); sampler2D normalMap : register(s1); float4 amientLightColor; float4 diffuseLightColor; float4 specularLightColor; float4 lightAttenuation; float4 ambientColor; float4 diffuseColor; float4 specularColor; float4 emissiveColor; float HEIGHT_SCALE = 0.1; float HEIGHT_BIAS = -0.08; floatbumpScale = 1; float4 normalMappedPS(VERTEX_OUT In):COLOR0 { float3 N = normalize(In.N); float3 T = normalize(In.T); float3 B = normalize(In.B); float3x3 TangentToView = float3x3(T, B, N); float3 Nt = normalize(tex2D(normalMap, In.texCoord).xyz - float3(0.5, 0.5, 0.0)); Nt.z /= bumpScale; Nt = normalize(Nt); N = mul(Nt, TangentToView); //innentől ugyanaz, mint a sima phongshaderünk… következő dia
… float d = length(In.L); float3 L = In.L / d; float3 V = normalize(In.V); float4 texColor = tex2D(colorTex, In.texCoord); floatattenuation = 1.0 / (lightAttenuation.y + lightAttenuation.z * d + lightAttenuation.w * d * d); float4 ambient = amientLightColor * ambientColor; float4 diffuse = max(0,dot(N,L)) * diffuseColor * diffuseLightColor * attenuation; float3 H = normalize(L + V); float4 specular = pow(max(0, dot(N,H)), specularColor.w) * float4(specularColor.xyz, 1) * specularLightColor * attenuation; returntexColor * (ambient + diffuse) + specular + emissiveColor; }
Parallax Mapping float4 parallaxMappedPS(VERTEX_OUT In):COLOR0 { … float3 Nt = normalize(tex2D(normalMap, In.texCoord).xyz - float3(0.5, 0.5, 0.0)); float d = length(In.L); float3 L = In.L / d; float3 V = normalize(In.V); float3 Vt = mul(TangentToView, V); float h = tex2D(normalMap, In.texCoord).a * HEIGHT_SCALE + HEIGHT_BIAS; //In.texCoord += h * Vt.xy / Vt.z; //parallax //In.texCoord += h * Vt.xy; //parallax with offset limit In.texCoord += h * Nt.z * Vt.xy; //parallaxwithslope Nt = normalize(tex2D(normalMap, In.texCoord).xyz - float3(0.5, 0.5, 0.0)); Nt.z /= bumpScale; Nt = normalize(Nt); N = mul(Nt, TangentToView); float4 texColor = tex2D(colorTex, In.texCoord); floatattenuation = …
Iteratív parallax #define PARALLAX_ITERATION 5 float2 PARALLAX_MAPPING_ITER(float3 TexCoord, float3 View) { for(int i = 0; i < PARALLAX_ITERATION; i++) { float3 N = tex2D(normalMap, TexCoord).xyz; float h = tex2D(normalMap, TexCoord).a * HEIGHT_SCALE + HEIGHT_BIAS; TexCoord += (h - TexCoord.z) * N.z * View; } return TexCoord.xy; } float4 iterativeParallaxMappedPS(VERTEX_OUT In):COLOR0 { … float3 Vt = mul(TangentToView, V); In.texCoord = PARALLAX_MAPPING_ITER(float3(In.texCoord, 0), Vt); float4 texColor = tex2D(colorTex, In.texCoord);
Relief mapping float4 reliefMappedPS(VERTEX_OUT In):COLOR0 { … float3 Vt = mul(TangentToView, V); float3 sRange = -Vt * HEIGHT_SCALE / Vt.z * 0.5; float3 sPos = float3(In.texCoord, 0) - sRange; for( int i=0; i<12; i++ ) { float bump = tex2D(normalMap, sPos.xy).a; sRange *= 0.5; if (sPos.z > bump * HEIGHT_SCALE) // If outside sPos += sRange; // Move forward else sPos -= sRange; // Move backward } In.texCoord = sPos.xy; float4 texColor = tex2D(colorTex, In.texCoord);
Relief mapping + árnyékok float4 reliefShadowMappedPS(VERTEX_OUT In):COLOR0 { … for( int i=0; i<12; i++ ) { … } In.texCoord = sPos.xy; float shadow = 1.0; float h1 = sPos.z; float h = tex2D(normalMap, In.texCoord).a; float3 Lt = mul(TangentToView, L); sRange = -Lt * HEIGHT_SCALE / Lt.z * 0.5; sPos -= sRange * (1.0 - h - 0.5);
Folytatás… for( int i=0; i<10; i++ ) { float bump = tex2D(normalMap, sPos.xy).a; sRange *= 0.5; if (sPos.z > bump * HEIGHT_SCALE) // If outside sPos += sRange; // Move forward else sPos -= sRange; // Move backward } float h2 = sPos.z; if(h2 > h1 + 0.01) shadow = 0.3; float4 texColor = tex2D(colorTex, In.texCoord); … float attenuation = … attenuation *= shadow;