using
System;
using
System.Windows.Threading;
using
System.Windows.Controls;
using
System.Windows.Media;
using
Balder.Math;
using
Balder.Objects.Geometries;
using
Balder.View;
using
Balder.Lighting;
using
Balder.Execution;
namespace
Balder_Studio
{
public
partial
class
Lesson05 : UserControl
{
//
选择的灯光
Light _SelectedLight
=
null
;
public
Lesson05()
{
InitializeComponent();
slider_axis_x.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_axis_ValueChanged);
slider_axis_y.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_axis_ValueChanged);
slider_axis_z.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_axis_ValueChanged);
slider_Strength.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_axis_ValueChanged);
slider_axis_x1.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_Offset_ValueChanged);
slider_axis_y1.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_Offset_ValueChanged);
slider_axis_z1.ValueChanged
+=
new
System.Windows.RoutedPropertyChangedEventHandler
<
double
>
(slider_Offset_ValueChanged);
OmniLight _OmniLight
=
new
OmniLight();
ViewLight _ViewLight
=
new
ViewLight();
DirectionalLight _DirectionalLight
=
new
DirectionalLight() {Direction
=
new
Coordinate(
-
1
,
-
1
,
0
) };
//
设定在同一个默认位置
_OmniLight.Position
=
_ViewLight.Position
=
_DirectionalLight.Position
=
new
Coordinate(
0
,
0
,
0
);
//
最开始的时候都不会亮
_OmniLight.IsEnabled
=
_ViewLight.IsEnabled
=
_DirectionalLight.IsEnabled
=
false
;
LightType.Items.Add(
new
TextBlock() { Text
=
"
点光源
"
, Tag
=
_OmniLight });
LightType.Items.Add(
new
TextBlock() { Text
=
"
聚光源
"
, Tag
=
_ViewLight });
LightType.Items.Add(
new
TextBlock() { Text
=
"
方向光
"
, Tag
=
_DirectionalLight });
LightType.SelectedIndex
=
0
;
LightType.SelectionChanged
+=
new
SelectionChangedEventHandler(LightType_SelectionChanged);
#region
L1 - L4的代码
//
L1
Game game
=
new
Game() { Width
=
600
, Height
=
400
};
game.Camera
=
new
Camera();
game.Camera.Position
=
new
Coordinate(
100
,
120
,
150
);
game.Camera.Target
=
new
Coordinate(
0
,
0
,
0
);
#endregion
game.Children.Add(_OmniLight);
_OmniLight.IsEnabled
=
true
;
_SelectedLight
=
_OmniLight;
game.Children.Add(_ViewLight);
game.Children.Add(_DirectionalLight);
#region
L1 - L4的代码
//
L3
Game_Axis axis_x
=
new
Game_Axis(
new
Vertex(
-
300
,
0
,
0
),
new
Vertex(
300
,
0
,
0
), Colors.Red);
Game_Axis axis_y
=
new
Game_Axis(
new
Vertex(
0
,
-
300
,
0
),
new
Vertex(
0
,
300
,
0
), Colors.Blue);
Game_Axis axis_z
=
new
Game_Axis(
new
Vertex(
0
,
0
,
-
300
),
new
Vertex(
0
,
0
,
300
), Colors.Green);
//
L4
Balder.Objects.Geometries.Geometry group
=
new
Balder.Objects.Geometries.Geometry() { Name
=
"
MeshGroup
"
};
group.InteractionEnabled
=
true
;
group.Children.Add(axis_x);
group.Children.Add(axis_y);
group.Children.Add(axis_z);
//
L2
Mesh Teapot
=
new
Mesh();
Teapot.Position
=
new
Coordinate(
0
,
0
,
0
);
Teapot.AssetName
=
new
Uri(
"
/Balder_Studio;component/Res/teapot.ase
"
, UriKind.Relative);
//
L4
group.Children.Add(Teapot);
group.Children.Add(
new
Box() { Dimension
=
new
Coordinate(
10
,
20
,
30
), Position
=
new
Coordinate(
100
,
10
,
0
), Name
=
"
MyBox
"
});
group.Children.Add(
new
Box() { Dimension
=
new
Coordinate(
10
,
30
,
10
), Position
=
new
Coordinate(
10
,
10
,
-
100
) });
game.Children.Add(group);
LayoutRoot.Children.Add(game);
#endregion
}
#region
控制灯光的界面事件
void
LightType_SelectionChanged(
object
sender, SelectionChangedEventArgs e)
{
//
选择一个新的光源之时
if
(_SelectedLight
!=
null
)
_SelectedLight.IsEnabled
=
false
;
_SelectedLight
=
((LightType.SelectedItem
as
TextBlock).Tag
as
Light);
_SelectedLight.IsEnabled
=
true
;
if
(_SelectedLight
is
OmniLight)
LightExSetting.Visibility
=
System.Windows.Visibility.Collapsed;
else
LightExSetting.Visibility
=
System.Windows.Visibility.Visible;
}
void
slider_axis_ValueChanged(
object
sender, System.Windows.RoutedPropertyChangedEventArgs
<
double
>
e)
{
axis_z.Text
=
((
int
)slider_axis_z.Value).ToString();
_SelectedLight.Position.Z
=
slider_axis_z.Value;
axis_y.Text
=
((
int
)slider_axis_y.Value).ToString();
_SelectedLight.Position.Y
=
slider_axis_y.Value;
axis_x.Text
=
((
int
)slider_axis_x.Value).ToString();
_SelectedLight.Position.X
=
slider_axis_x.Value;
StrengthText.Text
=
slider_Strength.Value.ToString();
_SelectedLight.Strength
=
slider_Strength.Value;
}
void
slider_Offset_ValueChanged(
object
sender, System.Windows.RoutedPropertyChangedEventArgs
<
double
>
e)
{
axis_z1.Text
=
((
int
)slider_axis_z1.Value).ToString();
axis_y1.Text
=
((
int
)slider_axis_y1.Value).ToString();
axis_x1.Text
=
((
int
)slider_axis_x1.Value).ToString();
if
(_SelectedLight
is
ViewLight)
{
var l
=
_SelectedLight
as
ViewLight;
l.XAngleOffset
=
slider_axis_x1.Value;
l.YAngleOffset
=
slider_axis_y1.Value;
l.ZAngleOffset
=
slider_axis_z1.Value;
}
else
{
var l
=
_SelectedLight
as
DirectionalLight;
var X
=
slider_axis_x1.Value
/
slider_axis_x1.Maximum;
var Y
=
slider_axis_y1.Value
/
slider_axis_y1.Maximum;
var Z
=
slider_axis_z1.Value
/
slider_axis_z1.Maximum;
l.Direction
=
new
Coordinate(X, Y, Z);
}
}
#endregion
}
}