-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainWindow.axaml.cs
209 lines (164 loc) · 6.04 KB
/
MainWindow.axaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using Avalonia.Controls;
using static example.Toolkit;
namespace example.Views;
// example-0008
// draw nurb tube with lighting
//
// - slide light positions
// - toggle shade with edge using ctrl+w
// - toggle show normals using n
// - zoom to shadow curve and change shadow resolution using radio button on the left side controls
public enum ShadowResTypeEnum
{
LowRes,
Default,
HiRes,
}
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region property changed
public new event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// invoke this method to forward propertchanged event notification.
/// note: not needed to specify propertyName set by compiler service to called property.
/// </summary>
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region ShadowResType
private ShadowResTypeEnum _ShadowResType = ShadowResTypeEnum.Default;
/// <summary>
/// ShadowResType
/// </summary>
public ShadowResTypeEnum ShadowResType
{
get => _ShadowResType;
set
{
var changed = value != _ShadowResType;
if (changed)
{
_ShadowResType = value;
OnPropertyChanged();
if (AvaloniaGLControl is not null)
{
switch (value)
{
case ShadowResTypeEnum.Default:
{
AvaloniaGLControl.GLControl.ShadowWidth = SHADOW_WIDTH;
AvaloniaGLControl.GLControl.ShadowHeight = SHADOW_HEIGHT;
}
break;
case ShadowResTypeEnum.LowRes:
{
AvaloniaGLControl.GLControl.ShadowWidth = SHADOW_WIDTH / 2;
AvaloniaGLControl.GLControl.ShadowHeight = SHADOW_HEIGHT / 2;
}
break;
case ShadowResTypeEnum.HiRes:
{
AvaloniaGLControl.GLControl.ShadowWidth = 2 * SHADOW_WIDTH;
AvaloniaGLControl.GLControl.ShadowHeight = 2 * SHADOW_HEIGHT;
}
break;
}
AvaloniaGLControl?.GLControl.InvalidateAll();
}
}
}
}
#endregion
GLModel glModel;
#region AvaloniaGLControl
private AvaloniaGLControl? _AvaloniaGLControl = null;
/// <summary>
/// AvaloniaGLControl
/// </summary>
public AvaloniaGLControl? AvaloniaGLControl
{
get => _AvaloniaGLControl;
set
{
var changed = value != _AvaloniaGLControl;
if (changed)
{
_AvaloniaGLControl = value;
OnPropertyChanged();
}
}
}
#endregion
public MainWindow()
{
InitializeComponent();
Title = AppDomain.CurrentDomain.FriendlyName;
Width = DEFAULT_WINDOW_WIDTH;
Height = DEFAULT_WINDOW_HEIGHT;
var glCtx = new GLContext();
glModel = new GLModel(glCtx);
glModel.BuildModel = (glCtl, isInitial) =>
{
if (!isInitial) return;
var glModel = glCtl.GLModel;
// tube
{
var fig = Example0007(SURF_DIVS: 20);
var figColor = new Vector3(0.27f, 0.38f, 0.58f);
var figColor2 = new Vector3(0.5f, 0.5f, 0.5f);
var VTR = 500 * Vector3.UnitZ;
fig.ObjectMatrix = Matrix4x4.CreateTranslation(VTR);
glModel.AddFigure(fig);
}
// base
{
var cuboidCenter = new Vector3(1000, 0, -50);
var cuboidSize = new Vector3(2 * 4000, 2 * 3000, 100);
var box = new Box(MakeCS(cuboidCenter - Vector3.UnitZ * cuboidSize.Z / 2, Vector3.UnitZ), cuboidSize, Color.White);
glModel.AddFigure(box.Sides);
}
// light obstacle
{
var cuboidCenter = new Vector3(-1500, 0, 500);
var cuboidSize = new Vector3(10, 2500, 1000);
var box = new Box(MakeCS(cuboidCenter - Vector3.UnitZ * cuboidSize.Z / 2, Vector3.UnitZ), cuboidSize, Color.Pink);
glModel.AddFigure(box.Sides);
}
var light1 = new GLPointLight(-1700, 0, 1100);
light1.Constant = 10;
light1.ShowPoint = true;
var light2 = new GLPointLight(-1700, 500, 900);
light2.Constant = 10;
light2.ShowPoint = true;
glModel.PointLights.Add(light1);
glModel.PointLights.Add(light2);
// setup light attenuation based on model bbox size
glModel.SetupLightAttenuation(adjustLinear: 5e-1f, adjustQuadratic: 1e-4f);
};
this.AttachGLControlSplit(grGL, glModel,
onFocusedControlChanged: (glSplit, avaloniaGLControl, isInitial) =>
{
AvaloniaGLControl = avaloniaGLControl;
},
onControlCreated: avaloniaGLControl =>
{
var glCtl = avaloniaGLControl.GLControl;
// track GLControl IsControlInvalidated property to force scene keep in sync at each change
glCtl.PropertyChanged += (a, b) =>
{
if (b.PropertyName == nameof(GLControl.IsControlInvalidated))
{
if (glCtl.IsControlInvalidated)
glCtl.Invalidate();
}
};
});
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
AvaloniaGLControl?.HandleKeyDown(e);
}
}