在场景中添加光线——在反光表面添加镜面高光

问题

就算开启了逐像素明暗,有些金属或闪光表面仍显得有点暗淡。在现实生活中,当观察诸如金属、玻璃或一些塑料时,你会发现某些区域的反光非常强烈。这样的区域如图6-6的虚线圆圈所示。这些高亮的区域叫做镜面高光(specular highlights)。

image

图6-6 镜面高光

解决方案

和逐像素光照一样,你只需简单地告知BasicEffect创建镜面高光就可以开启它。

注意:镜面高光只应添加到发光材质上。不要将它们添加到诸如衣服或草地的柔软表面,或至少让反光效果非常微弱。

工作原理

使用BasicEffect开启镜面高光非常简单。对每个光源,你指定高光颜色。然后,设置高光强度。这个强度可以指定高光的宽度。强度越大,镜面高光越窄。可见教程6-8学习相关细节。

basicEffect.LightingEnabled = true;
basicEffect.DirectionalLight0.Direction = lightDirection; 
basicEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3(); 
basicEffect.DirectionalLight0.Enabled = true; 
basicEffect.PreferPerPixelLighting = true; 
basicEffect.DirectionalLight0.SpecularColor = Color.White.ToVector3(); 
basicEffect.SpecularPower = 32; 

注意:当使用镜面高光时,总要使用逐像素光照,这是因为镜面高光通常是一个小的,非线性的点,所以它们不应该被插值,而是应该逐像素地计算。

代码

下面的代码定义顶点并绘制一个矩形:

private void InitVertices() 
{
    vertices = new VertexPositionNormalTexture[4]; 
    
    vertices[0] = new VertexPositionNormalTexture(new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector2(0, 1)); 
    vertices[1] = new VertexPositionNormalTexture(new Vector3(0, 0, - 10), new Vector3(0, 1, 0), new Vector2(0, 0)); 
    vertices[2] = new VertexPositionNormalTexture(new Vector3(10, 0, 0), new Vector3(0, 1, 0), new Vector2(1, 1)); 
    vertices[3] = new VertexPositionNormalTexture(new Vector3(10, 0,- 10), new Vector3(0, 1, 0), new Vector2(1, 0)); 
    
    myVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements); 
} 

在Draw方法中,添加如下代码创建BasicEffect,在矩形上添加镜面高光:

basicEffect.World = Matrix.Identity; 
basicEffect.View = fpsCam.ViewMatrix; 
basicEffect.Projection = fpsCam.ProjectionMatrix; 
basicEffect.Texture = blueTexture;
basicEffect.TextureEnabled = true; 
Vector3 lightDirection = new Vector3(0, -3, -10); 
lightDirection.Normalize(); 
basicEffect.LightingEnabled = true; 
basicEffect.DirectionalLight0.Direction = lightDirection; 
basicEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3(); 
basicEffect.DirectionalLight0.Enabled = true; 
basicEffect.PreferPerPixelLighting = true;
basicEffect.DirectionalLight0.SpecularColor = Color.White.ToVector3(); 
basicEffect.SpecularPower = 32; 

basicEffect.Begin(); 
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) 
{
    pass.Begin(); 
    device.VertexDeclaration = myVertexDeclaration; 
    device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleStrip, vertices, 0, 2); 
    pass.End(); 
}
basicEffect.End(); 

image

转载于:https://www.cnblogs.com/AlexCheng/archive/2011/02/14/2120099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值