14.04.2013 Views

Untitled - Departamento de Ciencias e Ingeniería de la Computación

Untitled - Departamento de Ciencias e Ingeniería de la Computación

Untitled - Departamento de Ciencias e Ingeniería de la Computación

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.4.4 Pixel sha<strong>de</strong>r<br />

Por fin llegamos al pixel sha<strong>de</strong>r. Como vemos, aquí se realiza <strong>la</strong> mayor parte <strong>de</strong> los cálculos, algo que se da en <strong>la</strong><br />

mayoría <strong>de</strong> los sha<strong>de</strong>rs.<br />

El proceso se pue<strong>de</strong> resumir <strong>de</strong> <strong>la</strong> siguiente manera. Para cada luz, calcu<strong>la</strong>mos como el<strong>la</strong> inci<strong>de</strong> en el color difuso y<br />

en el color especu<strong>la</strong>r <strong>de</strong>l material en ese fragmento en particu<strong>la</strong>r. Por último, utilizamos estos valores más <strong>la</strong>s<br />

texturas involucradas (en este caso solo una) para calcu<strong>la</strong>r el color final <strong>de</strong>l fragmento.<br />

//////////////////////////////////////////////<br />

/////////////// Pixel Sha<strong>de</strong>r /////////////////<br />

//////////////////////////////////////////////<br />

float4 PixelSha<strong>de</strong>r(vertexOutput IN) : COLOR<br />

{<br />

}<br />

float3 diffContrib;<br />

float3 specContrib;<br />

// La interpo<strong>la</strong>ción no normaliza los valores //<br />

float3 Nn = normalize(IN.WorldNormal);<br />

float3 Vn = normalize(IN.WorldView);<br />

// Luz punto //<br />

float3 Ln = normalize(IN.PointLightVec);<br />

float3 Hn = normalize(Vn + Ln);<br />

float hdn = dot(Hn,Nn);<br />

float ldn = dot(Ln,Nn);<br />

float4 litVec = lit(ldn,hdn,SpecExponent);<br />

diffContrib = litVec.y * PointLightColor;<br />

specContrib = SpecIntensity * litVec.z * diffContrib;<br />

// Luz direccional //<br />

float3 Ln3 = DirectionalLightDir;<br />

float3 Hn3 = normalize(Vn + Ln3);<br />

float hdn3 = dot(Hn3,Nn);<br />

float ldn3 = dot(Ln3,Nn);<br />

float4 litVec3 = lit(ldn3,hdn3,SpecExponent);<br />

diffContrib += litVec3.y * DirectionalLightColor;<br />

specContrib += ((litVec3.z * SpecIntensity) * litVec3.y * DirectionalLightColor);<br />

// Luz spot //<br />

float3 Ln2 = normalize(IN.SpotLightVec);<br />

float CosSpotAng = cos(SpotLightCone*(float)(3.141592/180.0));<br />

float dl = dot(SpotLightDir,Ln2);<br />

dl = ((dl-CosSpotAng)/(((float)1.0)-CosSpotAng));<br />

if (dl>0)<br />

{<br />

float ldn2 = dot(Ln2,Nn);<br />

float3 Hn2 = normalize(Vn + Ln2);<br />

float hdn2 = dot(Hn2,Nn);<br />

float4 litVec2 = lit(ldn2,hdn2,SpecExponent);<br />

ldn = litVec2.y * SpotLightIntensity;<br />

ldn *= dl;<br />

diffContrib += ldn * SpotLightColor;<br />

specContrib += ((ldn * litVec2.z * SpecIntensity) * SpotLightColor);<br />

}<br />

// Calcu<strong>la</strong>mos el color final <strong>de</strong>l pixel //<br />

// Consi<strong>de</strong>rando <strong>la</strong> información anteriormente calcu<strong>la</strong> más <strong>la</strong> textura utilizada //<br />

float3 colorTex = tex2D(DiffuseSampler, IN.UV).xyz;<br />

float3 result = colorTex * (diffContrib + AmbientLightColor) + specContrib;<br />

return float4(result.xyz, AlphaBlending);<br />

Desafortunadamente, como dijimos antes, escapa al alcance <strong>de</strong> este texto discutir en <strong>de</strong>talle cómo se calcu<strong>la</strong>n los<br />

distintos pasos.<br />

Página 77

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!