DirectX10教程9环境光照.docx
- 文档编号:659167
- 上传时间:2022-10-11
- 格式:DOCX
- 页数:25
- 大小:91.65KB
DirectX10教程9环境光照.docx
《DirectX10教程9环境光照.docx》由会员分享,可在线阅读,更多相关《DirectX10教程9环境光照.docx(25页珍藏版)》请在冰豆网上搜索。
DirectX10教程9环境光照
DirectX10教程9:
环境光照
原文地址:
Tutorial9:
AmbientLighting。
本教程介绍如何使用环境光照。
我使用了一个例子解释环境光照。
加入你在一个房间里,唯一的光源是通过窗口照射进来的太阳光。
阳光并没有直接照射到房间的每个面上,但房间中的每样东西都会被散射光照亮,这种在阳光并没有直接照射在物体表面的光照效果称之为环境光照。
我们使用了一个非常简单的公式模拟环境光照,只是在像素着色器的开始将每个像素的颜色设置为环境光颜色,之后再进行其他颜色操作叠加在环境光颜色上。
通过这个方法我们让每个对象至少都有环境光的颜色。
环境光照还能让3D场景更加真实。
下图中的立方体只被X轴方向的漫反射光照射:
图像看起来不够真实,因为即使光照很弱,充斥在周围环境光也会让物体有一个大致的轮廓。
现在,如果我们添加15%的白色环境光照,则图像如下图所示:
这样就获得了一个更加真实的光照效果。
本教程是建立在上一个漫反射光照教程的基础上的。
要添加环境光分量只需做很小一点的改动。
Light.fx
////////////////////////////////////////////////////////////////////////////////
//Filename:
light.fx
////////////////////////////////////////////////////////////////////////////////
/////////////
//GLOBALS//
/////////////
matrixworldMatrix;
matrixviewMatrix;
matrixprojectionMatrix;
Texture2DshaderTexture;
首先添加一个float4类型的环境光颜色变量,这样环境光就可以被这个shader之外的类进行设置。
float4ambientColor;
float4diffuseColor;
float3lightDirection;
///////////////////
//SAMPLESTATES//
///////////////////
SamplerStateSampleType
{
Filter=MIN_MAG_MIP_LINEAR;
AddressU=Wrap;
AddressV=Wrap;
};
//////////////
//TYPEDEFS//
//////////////
structVertexInputType
{
float4position:
POSITION;
float2tex:
TEXCOORD0;
float3normal:
NORMAL;
};
structPixelInputType
{
float4position:
SV_POSITION;
float2tex:
TEXCOORD0;
float3normal:
NORMAL;
};
////////////////////////////////////////////////////////////////////////////////
//VertexShader
////////////////////////////////////////////////////////////////////////////////
PixelInputTypeLightVertexShader(VertexInputTypeinput)
{
PixelInputTypeoutput;
//Changethepositionvectortobe4unitsforpropermatrixcalculations.
input.position.w=1.0f;
//Calculatethepositionofthevertexagainsttheworld,view,andprojectionmatrices.
output.position=mul(input.position,worldMatrix);
output.position=mul(output.position,viewMatrix);
output.position=mul(output.position,projectionMatrix);
//Storethetexturecoordinatesforthepixelshader.
output.tex=input.tex;
//Calculatethenormalvectoragainsttheworldmatrixonly.
output.normal=mul(input.normal,(float3x3)worldMatrix);
//Normalizethenormalvector.
output.normal=normalize(output.normal);
returnoutput;
}
////////////////////////////////////////////////////////////////////////////////
//PixelShader
////////////////////////////////////////////////////////////////////////////////
float4LightPixelShader(PixelInputTypeinput):
SV_Target
{
float4textureColor;
float3lightDir;
floatlightIntensity;
float4color;
//Samplethepixelcolorfromthetextureusingthesampleratthistexturecoordinatelocation.
textureColor=shaderTexture.Sample(SampleType,input.tex);
我们首先将输出颜色设置为环境光颜色,这样所有像素的颜色至少会是环境光颜色。
//Setthedefaultoutputcolortotheambientlightvalueforallpixels.
color=ambientColor;
//Invertthelightdirectionforcalculations.
lightDir=-lightDirection;
//Calculatetheamountoflightonthispixel.
lightIntensity=saturate(dot(input.normal,lightDir));
检查一下N与L的点乘是否大于零。
如果是则将漫反射颜色加上环境光颜色,如果不是则无需加上漫反射颜色,这是因为漫反射强度可能为负值,导致会在环境光颜色中减去漫反射颜色,导致错误的光照效果。
if(lightIntensity>0.0f)
{
//Determinethefinaldiffusecolorbasedonthediffusecolorandtheamountoflightintensity.
color+=(diffuseColor*lightIntensity);
}
有可能环境光加漫反射光的值大于1,所以需要进行saturate操作。
//Saturatethefinallightcolor.
color=saturate(color);
//Multiplythetexturepixelandthefinaldiffusecolortogetthefinalpixelcolorresult.
color=color*textureColor;
returncolor;
}
////////////////////////////////////////////////////////////////////////////////
//Technique
////////////////////////////////////////////////////////////////////////////////
technique10LightTechnique
{
passpass0
{
SetVertexShader(CompileShader(vs_4_0,LightVertexShader()));
SetPixelShader(CompileShader(ps_4_0,LightPixelShader()));
SetGeometryShader(NULL);
}
}
Lightshaderclass.h
////////////////////////////////////////////////////////////////////////////////
//Filename:
lightshaderclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef_LIGHTSHADERCLASS_H_
#define_LIGHTSHADERCLASS_H_
//////////////
//INCLUDES//
//////////////
#include
#include
#include
usingnamespacestd;
////////////////////////////////////////////////////////////////////////////////
//Classname:
LightShaderClass
////////////////////////////////////////////////////////////////////////////////
classLightShaderClass
{
public:
LightShaderClass();
LightShaderClass(constLightShaderClass&);
~LightShaderClass();
boolInitialize(ID3D10Device*,HWND);
voidShutdown();
voidRender(ID3D10Device*,int,D3DXMATRIX,D3DXMATRIX,D3DXMATRIX,ID3D10ShaderResourceView*,D3DXVECTOR3,D3DXVECTOR4,D3DXVECTOR4);
private:
boolInitial
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DirectX10 教程 环境 光照