Presentation is loading. Please wait.

Presentation is loading. Please wait.

Displacement mapping.

Similar presentations


Presentation on theme: "Displacement mapping."— Presentation transcript:

1 Displacement mapping

2 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

3 Próba

4 Material scheme OIS::InputManager* OISInputManager; int schemecount = 1; int currentScheme = 0; String qualitySchemes[] = { "VeryLow", "Low", "Medium", "Height", "VeryHeight", "Ultra" }; void setupScene() renderWindow->setActive(true); renderWindow->getViewport(0)->setMaterialScheme(qualitySchemes[0]); material Phong { technique scheme VeryLow pass : PhongPass …

5 Minden sémához új technika a phong.material-ba
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 ++)

6 Próba

7 normal mapping material Phong { technique scheme VeryLow … } scheme Low pass vertex_program_ref PhongShadingDisplacedVS fragment_program_ref PhongShadingNormalMappedPS

8 Phong.program vertex_program PhongShadingVS hlsl { } fragment_program PhongShadingPS hlsl vertex_program PhongShadingDisplacedVS hlsl source PhongDisplaced.hlsl entry_point displacedVS target vs_2_0 fragment_program PhongShadingNormalMappedPS hlsl entry_point normalMappedPS target ps_2_0

9 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;

10 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; }

11 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; float bumpScale = 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 phong shaderünk… következő dia 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));

12 … float d = length(In. L); float3 L = In
… float d = length(In.L); float3 L = In.L / d; float3 V = normalize(In.V); float4 texColor = tex2D(colorTex, In.texCoord); float attenuation = 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; return texColor * (ambient + diffuse) + specular + emissiveColor; }

13 Próba (int schemecount = 2;)

14 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; //parallax with slope 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); float attenuation = …

15 Próba (int schemecount = 3;)

16 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);

17 Próba (int schemecount = 4;)

18 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);

19 Próba (int schemecount = 5;)

20 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);

21 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 > h ) shadow = 0.3; float4 texColor = tex2D(colorTex, In.texCoord); … float attenuation = … attenuation *= shadow;

22 Próba (int schemecount = 6;)


Download ppt "Displacement mapping."

Similar presentations


Ads by Google