public
override
void
Render(DrawArgs drawArgs)
{
if
(downloadThread
!=
null
&&
downloadThread.IsAlive)
//DirectX知识点,可以学习一下
RenderProgress(drawArgs
);
if
(
!
this
.isInitialized)
return
;
try
{
if
(texture
==
null
||
m_SurfaceImage
!=
null
)
return
;
//这里是DirectX渲染的关键点之一,
设置渲染纹理。
drawArgs.device.SetTexture(
0, this
.texture);
if
(
this
._disableZbuffer)
{
if
(drawArgs.device.RenderState.ZBufferEnable)
drawArgs.device.RenderState.ZBufferEnable
=
false
;
}
else
{
if
(
!
drawArgs.device.RenderState.ZBufferEnable)
drawArgs.device.RenderState.ZBufferEnable
=
true
;
}
drawArgs.device.RenderState.ZBufferEnable
=
true
;
drawArgs.device.Clear(ClearFlags.ZBuffer,
0
,
1.0f
,
0
);
//设置device.Transform.World
drawArgs.device.Transform.World
=
Matrix.Translation(
(
float
)
-
drawArgs.WorldCamera.ReferenceCenter.X,
(
float
)
-
drawArgs.WorldCamera.ReferenceCenter.Y,
(
float
)
-
drawArgs.WorldCamera.ReferenceCenter.Z
);
//设置顶点格式为CustomVertex.PositionNormalTextured.Format
device.VertexFormat
=
CustomVertex.PositionNormalTextured.Format;
//device.DeviceCaps.PixelShaderVersion.Major 获取DirectX中Device的PixelShaderVersion实现主版本号,可以学习一下(device.DeviceCaps)
if
(
!
RenderGrayscale
||
(device.DeviceCaps.PixelShaderVersion.Major
<
1
))
{
if
(World.Settings.EnableSunShading)
{
//获取太阳在三维空间的照射位置点
Point3d sunPosition
=
SunCalculator.GetGeocentricPosition(TimeKeeper.CurrentTimeUtc);
//转换为笛卡尔坐标系下的矢量点
Vector3 sunVector
=
new
Vector3(
(
float
)sunPosition.X,
(
float
)sunPosition.Y,
(
float
)sunPosition.Z);
//以下都是设置Device的渲染参数的,DirectX编程中很常见的
//使用灯光
device.RenderState.Lighting
=
true
;
Material material
=
new
Material();
material.Diffuse
=
System.Drawing.Color.White;
material.Ambient
=
System.Drawing.Color.White;
//设置材料Material
device.Material
=
material;
device.RenderState.AmbientColor
=
World.Settings.ShadingAmbientColor.ToArgb(); //使用法向量
device.RenderState.NormalizeNormals
=
true
;
device.RenderState.AlphaBlendEnable
=
true
;
//设置灯光参数
device.Lights[
0
].Enabled
=
true
;
device.Lights[
0
].Type
=
LightType.Directional;
device.Lights[
0
].Diffuse
=
System.Drawing.Color.White;
//灯光方向,来自太阳
device.Lights[
0
].Direction
=
sunVector;
//设置纹理参数
device.TextureState[
0
].ColorOperation
=
TextureOperation.Modulate;
device.TextureState[
0
].ColorArgument1
=
TextureArgument.Diffuse;
device.TextureState[
0
].ColorArgument2
=
TextureArgument.TextureColor;
}
else
{
device.RenderState.Lighting
=
false
;
device.RenderState.Ambient
=
World.Settings.StandardAmbientColor;
drawArgs.device.TextureState[
0
].ColorOperation
=
TextureOperation.SelectArg1;
drawArgs.device.TextureState[
0
].ColorArgument1
=
TextureArgument.TextureColor;
}
device.RenderState.TextureFactor
=
System.Drawing.Color.FromArgb(m_opacity,
0
,
0
,
0
).ToArgb();
device.TextureState[
0
].AlphaOperation
=
TextureOperation.BlendFactorAlpha;
device.TextureState[
0
].AlphaArgument1
=
TextureArgument.TextureColor;
device.TextureState[
0
].AlphaArgument2
=
TextureArgument.TFactor;
drawArgs.device.VertexFormat
=
CustomVertex.PositionNormalTextured.Format;
//Device实现渲染绘制,调用的是DrawIndexedUserPrimitives。(如果看不懂,请参考DirectX编程相关知识)
drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList,
0
,
vertices.Length, indices.Length
/
3
, indices,
true
, vertices);
}
else
{
//以下是使用Effect绘制的
if
(grayscaleEffect
==
null
)
{
device.DeviceReset
+=
new
EventHandler(device_DeviceReset);
device_DeviceReset(device,
null
);
}
//设置Effect对象参数
grayscaleEffect.Technique
=
"
RenderGrayscaleBrightness
"
;
grayscaleEffect.SetValue(
"
WorldViewProj
"
, Matrix.Multiply(device.Transform.World, Matrix.Multiply(device.Transform.View, device.Transform.Projection)));
grayscaleEffect.SetValue(
"
Tex0
"
, texture);
grayscaleEffect.SetValue(
"
Brightness
"
, GrayscaleBrightness);
float
opacity
=
(
float
)m_opacity
/
255.0f
;
grayscaleEffect.SetValue(
"
Opacity
"
, opacity);
//以下是Effect渲染的关键
int numPasses = grayscaleEffect.Begin(0
);
for
(
int
i
=
0
; i
<
numPasses; i
++
)
{
grayscaleEffect.BeginPass(i);
drawArgs.device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList,
0
,
vertices.Length, indices.Length
/
3
, indices,
true
, vertices
);
grayscaleEffect.EndPass();
}
grayscaleEffect.End();
}
drawArgs.device.Transform.World
=
drawArgs.WorldCamera.WorldMatrix;
}
finally
{
if
(m_opacity
<
255
)
{
//
Restore alpha blend state
device.RenderState.SourceBlend
=
Blend.SourceAlpha;
device.RenderState.DestinationBlend
=
Blend.InvSourceAlpha;
}
if
(
this
._disableZbuffer)
drawArgs.device.RenderState.ZBufferEnable
=
true
;
}
}