• R/O
  • SSH
  • HTTPS

tdcgexplorer: 提交


Commit MetaInfo

修訂2273 (tree)
時間2017-12-22 09:02:09
作者nomeu

Log Message

indent

Change Summary

差異

--- trunk/tsoview/TDCG/Viewer.cs (revision 2272)
+++ trunk/tsoview/TDCG/Viewer.cs (revision 2273)
@@ -39,1433 +39,1435 @@
3939 /// <summary>
4040 /// TSOFileをDirect3D上でレンダリングします。
4141 /// </summary>
42-public class Viewer : IDisposable
43-{
44- /// <summary>
45- /// control
46- /// </summary>
47- protected Control control;
42+ public class Viewer : IDisposable
43+ {
44+ /// <summary>
45+ /// control
46+ /// </summary>
47+ protected Control control;
4848
49- /// <summary>
50- /// device
51- /// </summary>
52- protected Device device;
49+ /// <summary>
50+ /// device
51+ /// </summary>
52+ protected Device device;
5353
54- /// <summary>
55- /// effect
56- /// </summary>
57- protected Effect effect;
54+ /// <summary>
55+ /// effect
56+ /// </summary>
57+ protected Effect effect;
5858
59- /// <summary>
60- /// toonshader.cgfx に渡す頂点宣言
61- /// </summary>
62- protected VertexDeclaration vd;
59+ /// <summary>
60+ /// toonshader.cgfx に渡す頂点宣言
61+ /// </summary>
62+ protected VertexDeclaration vd;
6363
64- /// <summary>
65- /// effect handle for LocalBoneMats
66- /// since v0.90
67- /// </summary>
68- protected EffectHandle handle_LocalBoneMats;
64+ /// <summary>
65+ /// effect handle for LocalBoneMats
66+ /// since v0.90
67+ /// </summary>
68+ protected EffectHandle handle_LocalBoneMats;
6969
70- /// <summary>
71- /// effect handle for LightDirForced
72- /// since v0.90
73- /// </summary>
74- protected EffectHandle handle_LightDirForced;
70+ /// <summary>
71+ /// effect handle for LightDirForced
72+ /// since v0.90
73+ /// </summary>
74+ protected EffectHandle handle_LightDirForced;
7575
76- /// <summary>
77- /// effect handle for HohoAlpha
78- /// since v0.90
79- /// </summary>
80- protected EffectHandle handle_HohoAlpha;
76+ /// <summary>
77+ /// effect handle for HohoAlpha
78+ /// since v0.90
79+ /// </summary>
80+ protected EffectHandle handle_HohoAlpha;
8181
82- /// <summary>
83- /// effect handle for UVSCR
84- /// since v0.91
85- /// </summary>
86- protected EffectHandle handle_UVSCR;
82+ /// <summary>
83+ /// effect handle for UVSCR
84+ /// since v0.91
85+ /// </summary>
86+ protected EffectHandle handle_UVSCR;
8787
88- /// <summary>
89- /// effect handle for ShadowMap
90- /// TSOView extension
91- /// </summary>
92- protected EffectHandle handle_ShadowMap;
88+ /// <summary>
89+ /// effect handle for ShadowMap
90+ /// TSOView extension
91+ /// </summary>
92+ protected EffectHandle handle_ShadowMap;
9393
94- bool shadow_map_enabled;
95- /// <summary>
96- /// シャドウマップを作成するか
97- /// </summary>
98- public bool ShadowMapEnabled { get { return shadow_map_enabled; } }
94+ bool shadow_map_enabled;
95+ /// <summary>
96+ /// シャドウマップを作成するか
97+ /// </summary>
98+ public bool ShadowMapEnabled { get { return shadow_map_enabled; } }
9999
100- /// <summary>
101- /// ztexture
102- /// </summary>
103- protected Texture ztex = null;
104- /// <summary>
105- /// surface of ztexture
106- /// </summary>
107- protected Surface ztex_surface = null;
108- /// <summary>
109- /// zbuffer of ztexture
110- /// </summary>
111- protected Surface ztex_zbuf = null;
100+ /// <summary>
101+ /// ztexture
102+ /// </summary>
103+ protected Texture ztex = null;
104+ /// <summary>
105+ /// surface of ztexture
106+ /// </summary>
107+ protected Surface ztex_surface = null;
108+ /// <summary>
109+ /// zbuffer of ztexture
110+ /// </summary>
111+ protected Surface ztex_zbuf = null;
112112
113- /// sprite
114- public Sprite sprite = null;
115- internal Line line = null;
116- float w_scale = 1.0f;
117- float h_scale = 1.0f;
118- Rectangle ztex_rect;
113+ /// sprite
114+ public Sprite sprite = null;
115+ internal Line line = null;
116+ float w_scale = 1.0f;
117+ float h_scale = 1.0f;
118+ Rectangle ztex_rect;
119119
120- /// <summary>
121- /// surface of device
122- /// </summary>
123- protected Surface dev_surface = null;
124- /// <summary>
125- /// zbuffer of device
126- /// </summary>
127- protected Surface dev_zbuf = null;
120+ /// <summary>
121+ /// surface of device
122+ /// </summary>
123+ protected Surface dev_surface = null;
124+ /// <summary>
125+ /// zbuffer of device
126+ /// </summary>
127+ protected Surface dev_zbuf = null;
128128
129- /// config: BackBufferWidth BackBufferHeight
130- public Size DeviceSize { get; set; }
129+ /// config: BackBufferWidth BackBufferHeight
130+ public Size DeviceSize { get; set; }
131131
132- /// <summary>
133- /// viewerが保持しているフィギュアリスト
134- /// </summary>
135- public List<Figure> FigureList = new List<Figure>();
132+ /// <summary>
133+ /// viewerが保持しているフィギュアリスト
134+ /// </summary>
135+ public List<Figure> FigureList = new List<Figure>();
136136
137- /// <summary>
138- /// マウスポイントしているスクリーン座標
139- /// </summary>
140- protected Point lastScreenPoint = Point.Empty;
137+ /// <summary>
138+ /// マウスポイントしているスクリーン座標
139+ /// </summary>
140+ protected Point lastScreenPoint = Point.Empty;
141141
142- /// <summary>
143- /// viewerを生成します。
144- /// </summary>
145- public Viewer()
146- {
147- DeviceSize = new Size(0, 0);
148- ScreenColor = Color.LightGray;
149- HohoAlpha = 1.0f;
150- }
151-
152- /// <summary>
153- /// 選択フィギュアの光源方向を設定します。
154- /// </summary>
155- /// <param name="dir">選択フィギュアの光源方向</param>
156- public void SetLightDirection(Vector3 dir)
157- {
158- foreach (Figure fig in FigureList)
159- fig.LightDirection = dir;
160- }
161-
162- /// マウスボタンを押したときに実行するハンドラ
163- protected virtual void form_OnMouseDown(object sender, MouseEventArgs e)
164- {
165- switch (e.Button)
142+ /// <summary>
143+ /// viewerを生成します。
144+ /// </summary>
145+ public Viewer()
166146 {
167- case MouseButtons.Left:
168- if (Control.ModifierKeys == Keys.Control)
169- SetLightDirection(ScreenToOrientation(e.X, e.Y));
170- break;
147+ DeviceSize = new Size(0, 0);
148+ ScreenColor = Color.LightGray;
149+ HohoAlpha = 1.0f;
171150 }
172151
173- lastScreenPoint.X = e.X;
174- lastScreenPoint.Y = e.Y;
175- }
176-
177- /// マウスを移動したときに実行するハンドラ
178- protected virtual void form_OnMouseMove(object sender, MouseEventArgs e)
179- {
180- int dx = e.X - lastScreenPoint.X;
181- int dy = e.Y - lastScreenPoint.Y;
182-
183- switch (e.Button)
152+ /// <summary>
153+ /// 選択フィギュアの光源方向を設定します。
154+ /// </summary>
155+ /// <param name="dir">選択フィギュアの光源方向</param>
156+ public void SetLightDirection(Vector3 dir)
184157 {
185- case MouseButtons.Left:
186- if (Control.ModifierKeys == Keys.Control)
187- SetLightDirection(ScreenToOrientation(e.X, e.Y));
188- else
189- Camera.Move(dx, -dy, 0.0f);
190- break;
191- case MouseButtons.Middle:
192- Camera.MoveView(-dx*0.125f, dy*0.125f);
193- break;
194- case MouseButtons.Right:
195- Camera.Move(0.0f, 0.0f, -dy*0.125f);
196- break;
158+ foreach (Figure fig in FigureList)
159+ fig.LightDirection = dir;
197160 }
198161
199- lastScreenPoint.X = e.X;
200- lastScreenPoint.Y = e.Y;
201- }
162+ /// マウスボタンを押したときに実行するハンドラ
163+ protected virtual void form_OnMouseDown(object sender, MouseEventArgs e)
164+ {
165+ switch (e.Button)
166+ {
167+ case MouseButtons.Left:
168+ if (Control.ModifierKeys == Keys.Control)
169+ SetLightDirection(ScreenToOrientation(e.X, e.Y));
170+ break;
171+ }
202172
203- // 選択フィギュアindex
204- int fig_index = 0;
173+ lastScreenPoint.X = e.X;
174+ lastScreenPoint.Y = e.Y;
175+ }
205176
206- // スクリーンの中心座標
207- private float screenCenterX = 800 / 2.0f;
208- private float screenCenterY = 600 / 2.0f;
177+ /// マウスを移動したときに実行するハンドラ
178+ protected virtual void form_OnMouseMove(object sender, MouseEventArgs e)
179+ {
180+ int dx = e.X - lastScreenPoint.X;
181+ int dy = e.Y - lastScreenPoint.Y;
209182
210- /// <summary>
211- /// controlを保持します。スクリーンの中心座標を更新します。
212- /// </summary>
213- /// <param name="control">control</param>
214- protected void SetControl(Control control)
215- {
216- this.control = control;
217- screenCenterX = control.ClientSize.Width / 2.0f;
218- screenCenterY = control.ClientSize.Height / 2.0f;
219- }
183+ switch (e.Button)
184+ {
185+ case MouseButtons.Left:
186+ if (Control.ModifierKeys == Keys.Control)
187+ SetLightDirection(ScreenToOrientation(e.X, e.Y));
188+ else
189+ Camera.Move(dx, -dy, 0.0f);
190+ break;
191+ case MouseButtons.Middle:
192+ Camera.MoveView(-dx * 0.125f, dy * 0.125f);
193+ break;
194+ case MouseButtons.Right:
195+ Camera.Move(0.0f, 0.0f, -dy * 0.125f);
196+ break;
197+ }
220198
221- /// <summary>
222- /// 指定スクリーン座標からスクリーン中心へ向かうベクトルを得ます。
223- /// </summary>
224- /// <param name="screenPointX">スクリーンX座標</param>
225- /// <param name="screenPointY">スクリーンY座標</param>
226- /// <returns>方向ベクトル</returns>
227- public Vector3 ScreenToOrientation(float screenPointX, float screenPointY)
228- {
229- float radius = 1.0f;
230- float x = -(screenPointX - screenCenterX) / (radius * screenCenterX);
231- float y = +(screenPointY - screenCenterY) / (radius * screenCenterY);
232- float z = 0.0f;
233- float mag = (x*x) + (y*y);
234-
235- if (mag > 1.0f)
236- {
237- float scale = 1.0f / (float)Math.Sqrt(mag);
238- x *= scale;
239- y *= scale;
199+ lastScreenPoint.X = e.X;
200+ lastScreenPoint.Y = e.Y;
240201 }
241- else
242- z = (float)-Math.Sqrt(1.0f - mag);
243202
244- return new Vector3(x, y, z);
245- }
203+ // 選択フィギュアindex
204+ int fig_index = 0;
246205
247- /// <summary>
248- /// 任意のファイルを読み込みます。
249- /// </summary>
250- /// <param name="source_file">任意のパス</param>
251- public void LoadAnyFile(string source_file)
252- {
253- LoadAnyFile(source_file, false);
254- }
206+ // スクリーンの中心座標
207+ private float screenCenterX = 800 / 2.0f;
208+ private float screenCenterY = 600 / 2.0f;
255209
256- /// <summary>
257- /// 任意のファイルを読み込みます。
258- /// </summary>
259- /// <param name="source_file">任意のパス</param>
260- /// <param name="append">FigureListを消去せずに追加するか</param>
261- public void LoadAnyFile(string source_file, bool append)
262- {
263- switch (Path.GetExtension(source_file).ToLower())
210+ /// <summary>
211+ /// controlを保持します。スクリーンの中心座標を更新します。
212+ /// </summary>
213+ /// <param name="control">control</param>
214+ protected void SetControl(Control control)
264215 {
265- case ".tso":
266- if (! append)
267- ClearFigureList();
268- LoadTSOFile(source_file);
269- break;
270- case ".tmo":
271- LoadTMOFile(source_file);
272- break;
273- case ".png":
274- AddFigureFromPNGFile(source_file, append);
275- break;
276- default:
277- if (! append)
278- ClearFigureList();
279- if (Directory.Exists(source_file))
280- AddFigureFromTSODirectory(source_file);
281- break;
216+ this.control = control;
217+ screenCenterX = control.ClientSize.Width / 2.0f;
218+ screenCenterY = control.ClientSize.Height / 2.0f;
282219 }
283- }
284220
285- /// <summary>
286- /// フィギュア選択時に呼び出されるハンドラ
287- /// </summary>
288- public event EventHandler FigureEvent;
221+ /// <summary>
222+ /// 指定スクリーン座標からスクリーン中心へ向かうベクトルを得ます。
223+ /// </summary>
224+ /// <param name="screenPointX">スクリーンX座標</param>
225+ /// <param name="screenPointY">スクリーンY座標</param>
226+ /// <returns>方向ベクトル</returns>
227+ public Vector3 ScreenToOrientation(float screenPointX, float screenPointY)
228+ {
229+ float radius = 1.0f;
230+ float x = -(screenPointX - screenCenterX) / (radius * screenCenterX);
231+ float y = +(screenPointY - screenCenterY) / (radius * screenCenterY);
232+ float z = 0.0f;
233+ float mag = (x * x) + (y * y);
289234
290- /// <summary>
291- /// フィギュアを選択します。
292- /// </summary>
293- /// <param name="fig_index">フィギュア番号</param>
294- public void SetFigureIndex(int fig_index)
295- {
296- if (fig_index < 0)
297- fig_index = 0;
298- if (fig_index > FigureList.Count - 1)
299- fig_index = 0;
300- this.fig_index = fig_index;
301- if (FigureEvent != null)
302- FigureEvent(this, EventArgs.Empty);
303- }
304-
305- /// <summary>
306- /// 指定ディレクトリからフィギュアを作成して追加します。
307- /// </summary>
308- /// <param name="source_file">TSOFileを含むディレクトリ</param>
309- public void AddFigureFromTSODirectory(string source_file)
310- {
311- List<TSOFile> tso_list = new List<TSOFile>();
312- try
313- {
314- string[] files = Directory.GetFiles(source_file, "*.TSO");
315- foreach (string file in files)
235+ if (mag > 1.0f)
316236 {
317- TSOFile tso = new TSOFile();
318- Debug.WriteLine("loading " + file);
319- tso.Load(file);
320- tso_list.Add(tso);
237+ float scale = 1.0f / (float)Math.Sqrt(mag);
238+ x *= scale;
239+ y *= scale;
321240 }
241+ else
242+ z = (float)-Math.Sqrt(1.0f - mag);
243+
244+ return new Vector3(x, y, z);
322245 }
323- catch (Exception ex)
246+
247+ /// <summary>
248+ /// 任意のファイルを読み込みます。
249+ /// </summary>
250+ /// <param name="source_file">任意のパス</param>
251+ public void LoadAnyFile(string source_file)
324252 {
325- Console.WriteLine("Error: " + ex);
253+ LoadAnyFile(source_file, false);
326254 }
327- Figure fig = new Figure();
328- foreach (TSOFile tso in tso_list)
255+
256+ /// <summary>
257+ /// 任意のファイルを読み込みます。
258+ /// </summary>
259+ /// <param name="source_file">任意のパス</param>
260+ /// <param name="append">FigureListを消去せずに追加するか</param>
261+ public void LoadAnyFile(string source_file, bool append)
329262 {
330- tso.Open(device, effect);
331- fig.TSOList.Add(tso);
263+ switch (Path.GetExtension(source_file).ToLower())
264+ {
265+ case ".tso":
266+ if (!append)
267+ ClearFigureList();
268+ LoadTSOFile(source_file);
269+ break;
270+ case ".tmo":
271+ LoadTMOFile(source_file);
272+ break;
273+ case ".png":
274+ AddFigureFromPNGFile(source_file, append);
275+ break;
276+ default:
277+ if (!append)
278+ ClearFigureList();
279+ if (Directory.Exists(source_file))
280+ AddFigureFromTSODirectory(source_file);
281+ break;
282+ }
332283 }
333- fig.UpdateNodeMapAndBoneMatrices();
334- int idx = FigureList.Count;
335- FigureList.Add(fig);
336- SetFigureIndex(idx);
337- if (FigureEvent != null)
338- FigureEvent(this, EventArgs.Empty);
339- }
340284
341- /// <summary>
342- /// 選択フィギュアを得ます。
343- /// </summary>
344- public Figure GetSelectedFigure()
345- {
346- Figure fig;
347- if (FigureList.Count == 0)
348- fig = null;
349- else
350- fig = FigureList[fig_index];
351- return fig;
352- }
285+ /// <summary>
286+ /// フィギュア選択時に呼び出されるハンドラ
287+ /// </summary>
288+ public event EventHandler FigureEvent;
353289
354- /// <summary>
355- /// 選択フィギュアを得ます。なければ作成します。
356- /// </summary>
357- public Figure GetSelectedOrCreateFigure()
358- {
359- Figure fig;
360- if (FigureList.Count == 0)
361- fig = new Figure();
362- else
363- fig = FigureList[fig_index];
364- if (FigureList.Count == 0)
290+ /// <summary>
291+ /// フィギュアを選択します。
292+ /// </summary>
293+ /// <param name="fig_index">フィギュア番号</param>
294+ public void SetFigureIndex(int fig_index)
365295 {
296+ if (fig_index < 0)
297+ fig_index = 0;
298+ if (fig_index > FigureList.Count - 1)
299+ fig_index = 0;
300+ this.fig_index = fig_index;
301+ if (FigureEvent != null)
302+ FigureEvent(this, EventArgs.Empty);
303+ }
304+
305+ /// <summary>
306+ /// 指定ディレクトリからフィギュアを作成して追加します。
307+ /// </summary>
308+ /// <param name="source_file">TSOFileを含むディレクトリ</param>
309+ public void AddFigureFromTSODirectory(string source_file)
310+ {
311+ List<TSOFile> tso_list = new List<TSOFile>();
312+ try
313+ {
314+ string[] files = Directory.GetFiles(source_file, "*.TSO");
315+ foreach (string file in files)
316+ {
317+ TSOFile tso = new TSOFile();
318+ Debug.WriteLine("loading " + file);
319+ tso.Load(file);
320+ tso_list.Add(tso);
321+ }
322+ }
323+ catch (Exception ex)
324+ {
325+ Console.WriteLine("Error: " + ex);
326+ }
327+ Figure fig = new Figure();
328+ foreach (TSOFile tso in tso_list)
329+ {
330+ tso.Open(device, effect);
331+ fig.TSOList.Add(tso);
332+ }
333+ fig.UpdateNodeMapAndBoneMatrices();
366334 int idx = FigureList.Count;
367335 FigureList.Add(fig);
368336 SetFigureIndex(idx);
337+ if (FigureEvent != null)
338+ FigureEvent(this, EventArgs.Empty);
369339 }
370- return fig;
371- }
372340
373- /// <summary>
374- /// 指定パスからTSOFileを読み込みます。
375- /// </summary>
376- /// <param name="source_file">パス</param>
377- public void LoadTSOFile(string source_file)
378- {
379- Debug.WriteLine("loading " + source_file);
380- using (Stream source_stream = File.OpenRead(source_file))
381- LoadTSOFile(source_stream, source_file);
382- }
341+ /// <summary>
342+ /// 選択フィギュアを得ます。
343+ /// </summary>
344+ public Figure GetSelectedFigure()
345+ {
346+ Figure fig;
347+ if (FigureList.Count == 0)
348+ fig = null;
349+ else
350+ fig = FigureList[fig_index];
351+ return fig;
352+ }
383353
384- /// <summary>
385- /// 指定ストリームからTSOFileを読み込みます。
386- /// </summary>
387- /// <param name="source_stream">ストリーム</param>
388- public void LoadTSOFile(Stream source_stream)
389- {
390- LoadTSOFile(source_stream, null);
391- }
392-
393- /// <summary>
394- /// 指定ストリームからTSOFileを読み込みます。
395- /// </summary>
396- /// <param name="source_stream">ストリーム</param>
397- /// <param name="file">ファイル名</param>
398- public void LoadTSOFile(Stream source_stream, string file)
399- {
400- List<TSOFile> tso_list = new List<TSOFile>();
401- try
354+ /// <summary>
355+ /// 選択フィギュアを得ます。なければ作成します。
356+ /// </summary>
357+ public Figure GetSelectedOrCreateFigure()
402358 {
403- TSOFile tso = new TSOFile();
404- tso.Load(source_stream);
405- tso.FileName = file != null ? Path.GetFileNameWithoutExtension(file) : null;
406- tso_list.Add(tso);
359+ Figure fig;
360+ if (FigureList.Count == 0)
361+ fig = new Figure();
362+ else
363+ fig = FigureList[fig_index];
364+ if (FigureList.Count == 0)
365+ {
366+ int idx = FigureList.Count;
367+ FigureList.Add(fig);
368+ SetFigureIndex(idx);
369+ }
370+ return fig;
407371 }
408- catch (Exception ex)
372+
373+ /// <summary>
374+ /// 指定パスからTSOFileを読み込みます。
375+ /// </summary>
376+ /// <param name="source_file">パス</param>
377+ public void LoadTSOFile(string source_file)
409378 {
410- Console.WriteLine("Error: " + ex);
379+ Debug.WriteLine("loading " + source_file);
380+ using (Stream source_stream = File.OpenRead(source_file))
381+ LoadTSOFile(source_stream, source_file);
411382 }
412- Figure fig = GetSelectedOrCreateFigure();
413- foreach (TSOFile tso in tso_list)
383+
384+ /// <summary>
385+ /// 指定ストリームからTSOFileを読み込みます。
386+ /// </summary>
387+ /// <param name="source_stream">ストリーム</param>
388+ public void LoadTSOFile(Stream source_stream)
414389 {
415- tso.Open(device, effect);
416- fig.TSOList.Add(tso);
390+ LoadTSOFile(source_stream, null);
417391 }
418- fig.UpdateNodeMapAndBoneMatrices();
419- if (FigureEvent != null)
420- FigureEvent(this, EventArgs.Empty);
421- }
422392
423- /// <summary>
424- /// 選択フィギュアを得ます。
425- /// </summary>
426- public bool TryGetFigure(out Figure fig)
427- {
428- fig = null;
429- if (fig_index < FigureList.Count)
430- fig = FigureList[fig_index];
431- return fig != null;
432- }
433-
434- /// 次のフィギュアを選択します。
435- public void NextFigure()
436- {
437- SetFigureIndex(fig_index+1);
438- }
439-
440- /// <summary>
441- /// 指定パスからTMOFileを読み込みます。
442- /// </summary>
443- /// <param name="source_file">パス</param>
444- public void LoadTMOFile(string source_file)
445- {
446- using (Stream source_stream = File.OpenRead(source_file))
447- LoadTMOFile(source_stream);
448- }
449-
450- /// <summary>
451- /// 指定ストリームからTMOFileを読み込みます。
452- /// </summary>
453- /// <param name="source_stream">ストリーム</param>
454- public void LoadTMOFile(Stream source_stream)
455- {
456- Figure fig;
457- if (TryGetFigure(out fig))
393+ /// <summary>
394+ /// 指定ストリームからTSOFileを読み込みます。
395+ /// </summary>
396+ /// <param name="source_stream">ストリーム</param>
397+ /// <param name="file">ファイル名</param>
398+ public void LoadTSOFile(Stream source_stream, string file)
458399 {
400+ List<TSOFile> tso_list = new List<TSOFile>();
459401 try
460402 {
461- TMOFile tmo = new TMOFile();
462- tmo.Load(source_stream);
463- fig.Tmo = tmo;
403+ TSOFile tso = new TSOFile();
404+ tso.Load(source_stream);
405+ tso.FileName = file != null ? Path.GetFileNameWithoutExtension(file) : null;
406+ tso_list.Add(tso);
464407 }
465408 catch (Exception ex)
466409 {
467410 Console.WriteLine("Error: " + ex);
468411 }
412+ Figure fig = GetSelectedOrCreateFigure();
413+ foreach (TSOFile tso in tso_list)
414+ {
415+ tso.Open(device, effect);
416+ fig.TSOList.Add(tso);
417+ }
469418 fig.UpdateNodeMapAndBoneMatrices();
470419 if (FigureEvent != null)
471420 FigureEvent(this, EventArgs.Empty);
472421 }
473- }
474422
475- /// <summary>
476- /// 指定パスからPNGFileを読み込みフィギュアを作成して追加します。
477- /// </summary>
478- /// <param name="source_file">PNGFile のパス</param>
479- /// <param name="append">FigureListを消去せずに追加するか</param>
480- public void AddFigureFromPNGFile(string source_file, bool append)
481- {
482- PNGSaveFile sav = LoadPNGFile(source_file);
483- if (sav.FigureList.Count == 0) //POSE png
423+ /// <summary>
424+ /// 選択フィギュアを得ます。
425+ /// </summary>
426+ public bool TryGetFigure(out Figure fig)
484427 {
485- Debug.Assert(sav.Tmo != null, "save.Tmo should not be null");
428+ fig = null;
429+ if (fig_index < FigureList.Count)
430+ fig = FigureList[fig_index];
431+ return fig != null;
432+ }
433+
434+ /// 次のフィギュアを選択します。
435+ public void NextFigure()
436+ {
437+ SetFigureIndex(fig_index + 1);
438+ }
439+
440+ /// <summary>
441+ /// 指定パスからTMOFileを読み込みます。
442+ /// </summary>
443+ /// <param name="source_file">パス</param>
444+ public void LoadTMOFile(string source_file)
445+ {
446+ using (Stream source_stream = File.OpenRead(source_file))
447+ LoadTMOFile(source_stream);
448+ }
449+
450+ /// <summary>
451+ /// 指定ストリームからTMOFileを読み込みます。
452+ /// </summary>
453+ /// <param name="source_stream">ストリーム</param>
454+ public void LoadTMOFile(Stream source_stream)
455+ {
486456 Figure fig;
487457 if (TryGetFigure(out fig))
488458 {
489- if (sav.LightDirection != Vector3.Empty)
490- fig.LightDirection = sav.LightDirection;
491- fig.Tmo = sav.Tmo;
492- //fig.TransformTpo();
459+ try
460+ {
461+ TMOFile tmo = new TMOFile();
462+ tmo.Load(source_stream);
463+ fig.Tmo = tmo;
464+ }
465+ catch (Exception ex)
466+ {
467+ Console.WriteLine("Error: " + ex);
468+ }
493469 fig.UpdateNodeMapAndBoneMatrices();
494470 if (FigureEvent != null)
495471 FigureEvent(this, EventArgs.Empty);
496472 }
497473 }
498- else
474+
475+ /// <summary>
476+ /// 指定パスからPNGFileを読み込みフィギュアを作成して追加します。
477+ /// </summary>
478+ /// <param name="source_file">PNGFile のパス</param>
479+ /// <param name="append">FigureListを消去せずに追加するか</param>
480+ public void AddFigureFromPNGFile(string source_file, bool append)
499481 {
500- if (! append)
501- ClearFigureList();
502-
503- int idx = FigureList.Count;
504- foreach (Figure fig in sav.FigureList)
482+ PNGSaveFile sav = LoadPNGFile(source_file);
483+ if (sav.FigureList.Count == 0) //POSE png
505484 {
506- fig.OpenTSOFile(device, effect);
507- fig.UpdateNodeMapAndBoneMatrices();
508- FigureList.Add(fig);
485+ Debug.Assert(sav.Tmo != null, "save.Tmo should not be null");
486+ Figure fig;
487+ if (TryGetFigure(out fig))
488+ {
489+ if (sav.LightDirection != Vector3.Empty)
490+ fig.LightDirection = sav.LightDirection;
491+ fig.Tmo = sav.Tmo;
492+ //fig.TransformTpo();
493+ fig.UpdateNodeMapAndBoneMatrices();
494+ if (FigureEvent != null)
495+ FigureEvent(this, EventArgs.Empty);
496+ }
509497 }
510- SetFigureIndex(idx);
498+ else
499+ {
500+ if (!append)
501+ ClearFigureList();
502+
503+ int idx = FigureList.Count;
504+ foreach (Figure fig in sav.FigureList)
505+ {
506+ fig.OpenTSOFile(device, effect);
507+ fig.UpdateNodeMapAndBoneMatrices();
508+ FigureList.Add(fig);
509+ }
510+ SetFigureIndex(idx);
511+ }
511512 }
512- }
513513
514- /// <summary>
515- /// 指定テクスチャを開き直します。
516- /// </summary>
517- /// <param name="tex">テクスチャ</param>
518- public void OpenTexture(TSOTex tex)
519- {
520- tex.Open(device);
521- }
514+ /// <summary>
515+ /// 指定テクスチャを開き直します。
516+ /// </summary>
517+ /// <param name="tex">テクスチャ</param>
518+ public void OpenTexture(TSOTex tex)
519+ {
520+ tex.Open(device);
521+ }
522522
523- private SimpleCamera camera = new SimpleCamera();
523+ private SimpleCamera camera = new SimpleCamera();
524524
525- /// <summary>
526- /// カメラ
527- /// </summary>
528- public SimpleCamera Camera
529- {
530- get {
531- return camera;
525+ /// <summary>
526+ /// カメラ
527+ /// </summary>
528+ public SimpleCamera Camera
529+ {
530+ get
531+ {
532+ return camera;
533+ }
534+ set
535+ {
536+ camera = value;
537+ }
532538 }
533- set {
534- camera = value;
539+
540+ /// <summary>
541+ /// world行列
542+ /// </summary>
543+ protected Matrix world_matrix = Matrix.Identity;
544+ /// <summary>
545+ /// view変換行列
546+ /// </summary>
547+ protected Matrix Transform_View = Matrix.Identity;
548+ /// <summary>
549+ /// projection変換行列
550+ /// </summary>
551+ protected Matrix Transform_Projection = Matrix.Identity;
552+ /// <summary>
553+ /// 光源view変換行列
554+ /// </summary>
555+ protected Matrix Light_View = Matrix.Identity;
556+ /// <summary>
557+ /// 光源projection変換行列
558+ /// </summary>
559+ protected Matrix Light_Projection = Matrix.Identity;
560+
561+ /// <summary>
562+ /// deviceを作成します。
563+ /// </summary>
564+ /// <param name="control">レンダリング先となるcontrol</param>
565+ /// <returns>deviceの作成に成功したか</returns>
566+ public bool InitializeApplication(Control control)
567+ {
568+ return InitializeApplication(control, false);
535569 }
536- }
537570
538- /// <summary>
539- /// world行列
540- /// </summary>
541- protected Matrix world_matrix = Matrix.Identity;
542- /// <summary>
543- /// view変換行列
544- /// </summary>
545- protected Matrix Transform_View = Matrix.Identity;
546- /// <summary>
547- /// projection変換行列
548- /// </summary>
549- protected Matrix Transform_Projection = Matrix.Identity;
550- /// <summary>
551- /// 光源view変換行列
552- /// </summary>
553- protected Matrix Light_View = Matrix.Identity;
554- /// <summary>
555- /// 光源projection変換行列
556- /// </summary>
557- protected Matrix Light_Projection = Matrix.Identity;
571+ /// <summary>
572+ /// deviceを作成します。
573+ /// </summary>
574+ /// <param name="control">レンダリング先となるcontrol</param>
575+ /// <param name="shadow_map_enabled">シャドウマップを作成するか</param>
576+ /// <returns>deviceの作成に成功したか</returns>
577+ public bool InitializeApplication(Control control, bool shadow_map_enabled)
578+ {
579+ this.shadow_map_enabled = shadow_map_enabled;
580+ SetControl(control);
558581
559- /// <summary>
560- /// deviceを作成します。
561- /// </summary>
562- /// <param name="control">レンダリング先となるcontrol</param>
563- /// <returns>deviceの作成に成功したか</returns>
564- public bool InitializeApplication(Control control)
565- {
566- return InitializeApplication(control, false);
567- }
582+ control.MouseDown += new MouseEventHandler(form_OnMouseDown);
583+ control.MouseMove += new MouseEventHandler(form_OnMouseMove);
568584
569- /// <summary>
570- /// deviceを作成します。
571- /// </summary>
572- /// <param name="control">レンダリング先となるcontrol</param>
573- /// <param name="shadow_map_enabled">シャドウマップを作成するか</param>
574- /// <returns>deviceの作成に成功したか</returns>
575- public bool InitializeApplication(Control control, bool shadow_map_enabled)
576- {
577- this.shadow_map_enabled = shadow_map_enabled;
578- SetControl(control);
585+ PresentParameters pp = new PresentParameters();
586+ try
587+ {
588+ pp.Windowed = true;
589+ pp.SwapEffect = SwapEffect.Discard;
590+ pp.BackBufferFormat = Format.X8R8G8B8;
591+ pp.BackBufferWidth = DeviceSize.Width;
592+ pp.BackBufferHeight = DeviceSize.Height;
593+ pp.BackBufferCount = 1;
594+ pp.EnableAutoDepthStencil = true;
579595
580- control.MouseDown += new MouseEventHandler(form_OnMouseDown);
581- control.MouseMove += new MouseEventHandler(form_OnMouseMove);
596+ int adapter_ordinal = Manager.Adapters.Default.Adapter;
597+ DisplayMode display_mode = Manager.Adapters.Default.CurrentDisplayMode;
582598
583- PresentParameters pp = new PresentParameters();
584- try
585- {
586- pp.Windowed = true;
587- pp.SwapEffect = SwapEffect.Discard;
588- pp.BackBufferFormat = Format.X8R8G8B8;
589- pp.BackBufferWidth = DeviceSize.Width;
590- pp.BackBufferHeight = DeviceSize.Height;
591- pp.BackBufferCount = 1;
592- pp.EnableAutoDepthStencil = true;
599+ int ret;
600+ if (Manager.CheckDepthStencilMatch(adapter_ordinal, DeviceType.Hardware, display_mode.Format, pp.BackBufferFormat, DepthFormat.D24S8, out ret))
601+ pp.AutoDepthStencilFormat = DepthFormat.D24S8;
602+ else
603+ if (Manager.CheckDepthStencilMatch(adapter_ordinal, DeviceType.Hardware, display_mode.Format, pp.BackBufferFormat, DepthFormat.D24X8, out ret))
604+ pp.AutoDepthStencilFormat = DepthFormat.D24X8;
605+ else
606+ pp.AutoDepthStencilFormat = DepthFormat.D16;
593607
594- int adapter_ordinal = Manager.Adapters.Default.Adapter;
595- DisplayMode display_mode = Manager.Adapters.Default.CurrentDisplayMode;
608+ int quality;
609+ if (Manager.CheckDeviceMultiSampleType(adapter_ordinal, DeviceType.Hardware, pp.BackBufferFormat, pp.Windowed, MultiSampleType.FourSamples, out ret, out quality))
610+ {
611+ pp.MultiSample = MultiSampleType.FourSamples;
612+ pp.MultiSampleQuality = quality - 1;
613+ }
596614
597- int ret;
598- if (Manager.CheckDepthStencilMatch(adapter_ordinal, DeviceType.Hardware, display_mode.Format, pp.BackBufferFormat, DepthFormat.D24S8, out ret))
599- pp.AutoDepthStencilFormat = DepthFormat.D24S8;
600- else
601- if (Manager.CheckDepthStencilMatch(adapter_ordinal, DeviceType.Hardware, display_mode.Format, pp.BackBufferFormat, DepthFormat.D24X8, out ret))
602- pp.AutoDepthStencilFormat = DepthFormat.D24X8;
603- else
604- pp.AutoDepthStencilFormat = DepthFormat.D16;
615+ CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
616+ Caps caps = Manager.GetDeviceCaps(adapter_ordinal, DeviceType.Hardware);
617+ if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
618+ flags = CreateFlags.HardwareVertexProcessing;
619+ if (caps.DeviceCaps.SupportsPureDevice)
620+ flags |= CreateFlags.PureDevice;
621+ device = new Device(adapter_ordinal, DeviceType.Hardware, control, flags, pp);
605622
606- int quality;
607- if (Manager.CheckDeviceMultiSampleType(adapter_ordinal, DeviceType.Hardware, pp.BackBufferFormat, pp.Windowed, MultiSampleType.FourSamples, out ret, out quality))
623+ device.DeviceLost += new EventHandler(OnDeviceLost);
624+ device.DeviceReset += new EventHandler(OnDeviceReset);
625+ }
626+ catch (DirectXException ex)
608627 {
609- pp.MultiSample = MultiSampleType.FourSamples;
610- pp.MultiSampleQuality = quality - 1;
628+ Console.WriteLine("Error: " + ex);
629+ return false;
611630 }
612631
613- CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
614- Caps caps = Manager.GetDeviceCaps(adapter_ordinal, DeviceType.Hardware);
615- if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
616- flags = CreateFlags.HardwareVertexProcessing;
617- if (caps.DeviceCaps.SupportsPureDevice)
618- flags |= CreateFlags.PureDevice;
619- device = new Device(adapter_ordinal, DeviceType.Hardware, control, flags, pp);
632+ Stopwatch sw = new Stopwatch();
633+ sw.Start();
620634
621- device.DeviceLost += new EventHandler(OnDeviceLost);
622- device.DeviceReset += new EventHandler(OnDeviceReset);
623- }
624- catch (DirectXException ex)
625- {
626- Console.WriteLine("Error: " + ex);
627- return false;
628- }
629-
630- Stopwatch sw = new Stopwatch();
631- sw.Start();
632-
633- string effect_file = Path.Combine(Application.StartupPath, @"toonshader.cgfx");
634- if (! File.Exists(effect_file))
635- {
636- Console.WriteLine("File not found: " + effect_file);
637- return false;
638- }
639- using (FileStream effect_stream = File.OpenRead(effect_file))
640- {
641- string compile_error;
642- effect = Effect.FromStream(device, effect_stream, null, ShaderFlags.None, null, out compile_error);
643- if (compile_error != null)
635+ string effect_file = Path.Combine(Application.StartupPath, @"toonshader.cgfx");
636+ if (!File.Exists(effect_file))
644637 {
645- Console.WriteLine(compile_error);
638+ Console.WriteLine("File not found: " + effect_file);
646639 return false;
647640 }
648- }
641+ using (FileStream effect_stream = File.OpenRead(effect_file))
642+ {
643+ string compile_error;
644+ effect = Effect.FromStream(device, effect_stream, null, ShaderFlags.None, null, out compile_error);
645+ if (compile_error != null)
646+ {
647+ Console.WriteLine(compile_error);
648+ return false;
649+ }
650+ }
649651
650- sw.Stop();
651- Console.WriteLine("toonshader.cgfx read time: " + sw.Elapsed);
652+ sw.Stop();
653+ Console.WriteLine("toonshader.cgfx read time: " + sw.Elapsed);
652654
653- handle_LocalBoneMats = effect.GetParameter(null, "LocalBoneMats");
654- handle_LightDirForced = effect.GetParameter(null, "LightDirForced");
655- handle_HohoAlpha = effect.GetParameter(null, "HohoAlpha");
656- handle_UVSCR = effect.GetParameter(null, "UVSCR");
657- if (shadow_map_enabled)
658- {
659- handle_ShadowMap = effect.GetTechnique("ShadowMap");
660- }
661- sprite = new Sprite(device);
662- line = new Line(device);
663- camera.Update();
664- OnDeviceReset(device, null);
655+ handle_LocalBoneMats = effect.GetParameter(null, "LocalBoneMats");
656+ handle_LightDirForced = effect.GetParameter(null, "LightDirForced");
657+ handle_HohoAlpha = effect.GetParameter(null, "HohoAlpha");
658+ handle_UVSCR = effect.GetParameter(null, "UVSCR");
659+ if (shadow_map_enabled)
660+ {
661+ handle_ShadowMap = effect.GetTechnique("ShadowMap");
662+ }
663+ sprite = new Sprite(device);
664+ line = new Line(device);
665+ camera.Update();
666+ OnDeviceReset(device, null);
665667
666- return true;
667- }
668-
669- private void OnDeviceLost(object sender, EventArgs e)
670- {
671- Console.WriteLine("OnDeviceLost");
672- if (ztex_zbuf != null)
673- ztex_zbuf.Dispose();
674- if (ztex_surface != null)
675- ztex_surface.Dispose();
676- if (ztex != null)
677- ztex.Dispose();
678- if (dev_zbuf != null)
679- dev_zbuf.Dispose();
680- if (dev_surface != null)
681- dev_surface.Dispose();
682- }
683-
684- private void OnDeviceReset(object sender, EventArgs e)
685- {
686- Console.WriteLine("OnDeviceReset");
687- int devw = 0;
688- int devh = 0;
689- dev_surface = device.GetRenderTarget(0);
690- {
691- devw = dev_surface.Description.Width;
692- devh = dev_surface.Description.Height;
668+ return true;
693669 }
694- Console.WriteLine("dev {0}x{1}", devw, devh);
695670
696- int dev_zbufw = 0;
697- int dev_zbufh = 0;
698- dev_zbuf = device.DepthStencilSurface;
671+ private void OnDeviceLost(object sender, EventArgs e)
699672 {
700- dev_zbufw = dev_surface.Description.Width;
701- dev_zbufh = dev_surface.Description.Height;
673+ Console.WriteLine("OnDeviceLost");
674+ if (ztex_zbuf != null)
675+ ztex_zbuf.Dispose();
676+ if (ztex_surface != null)
677+ ztex_surface.Dispose();
678+ if (ztex != null)
679+ ztex.Dispose();
680+ if (dev_zbuf != null)
681+ dev_zbuf.Dispose();
682+ if (dev_surface != null)
683+ dev_surface.Dispose();
702684 }
703- Console.WriteLine("dev_zbuf {0}x{1}", dev_zbufw, dev_zbufh);
704685
705- if (shadow_map_enabled)
686+ private void OnDeviceReset(object sender, EventArgs e)
706687 {
707- ztex = new Texture(device, 1024, 1024, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
708- ztex_surface = ztex.GetSurfaceLevel(0);
688+ Console.WriteLine("OnDeviceReset");
689+ int devw = 0;
690+ int devh = 0;
691+ dev_surface = device.GetRenderTarget(0);
692+ {
693+ devw = dev_surface.Description.Width;
694+ devh = dev_surface.Description.Height;
695+ }
696+ Console.WriteLine("dev {0}x{1}", devw, devh);
709697
710- effect.SetValue("texShadowMap", ztex);
711- int texw = ztex_surface.Description.Width;
712- int texh = ztex_surface.Description.Height;
713- Console.WriteLine("ztex {0}x{1}", texw, texh);
698+ int dev_zbufw = 0;
699+ int dev_zbufh = 0;
700+ dev_zbuf = device.DepthStencilSurface;
701+ {
702+ dev_zbufw = dev_surface.Description.Width;
703+ dev_zbufh = dev_surface.Description.Height;
704+ }
705+ Console.WriteLine("dev_zbuf {0}x{1}", dev_zbufw, dev_zbufh);
714706
715- ztex_zbuf = device.CreateDepthStencilSurface(texw, texh, DepthFormat.D16, MultiSampleType.None, 0, false);
707+ if (shadow_map_enabled)
708+ {
709+ ztex = new Texture(device, 1024, 1024, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
710+ ztex_surface = ztex.GetSurfaceLevel(0);
716711
717- w_scale = (float)devw / texw;
718- h_scale = (float)devh / texh;
719- ztex_rect = new Rectangle(0, 0, texw, texh);
720- }
712+ effect.SetValue("texShadowMap", ztex);
713+ int texw = ztex_surface.Description.Width;
714+ int texh = ztex_surface.Description.Height;
715+ Console.WriteLine("ztex {0}x{1}", texw, texh);
721716
722- Transform_Projection = Matrix.PerspectiveFovRH(
723- Geometry.DegreeToRadian(30.0f),
724- (float)device.Viewport.Width / (float)device.Viewport.Height,
725- 1.0f,
726- 1000.0f );
727- // xxx: for w-buffering
728- device.Transform.Projection = Transform_Projection;
729- effect.SetValue("proj", Transform_Projection);
717+ ztex_zbuf = device.CreateDepthStencilSurface(texw, texh, DepthFormat.D16, MultiSampleType.None, 0, false);
730718
731- if (shadow_map_enabled)
732- {
733- Light_Projection = Matrix.PerspectiveFovLH(
734- Geometry.DegreeToRadian(45.0f),
735- 1.0f,
736- 20.0f,
737- 250.0f );
738- effect.SetValue("lightproj", Light_Projection);
739- }
719+ w_scale = (float)devw / texw;
720+ h_scale = (float)devh / texh;
721+ ztex_rect = new Rectangle(0, 0, texw, texh);
722+ }
740723
741- device.SetRenderState(RenderStates.Lighting, false);
742- device.SetRenderState(RenderStates.CullMode, (int)Cull.CounterClockwise);
724+ Transform_Projection = Matrix.PerspectiveFovRH(
725+ Geometry.DegreeToRadian(30.0f),
726+ (float)device.Viewport.Width / (float)device.Viewport.Height,
727+ 1.0f,
728+ 1000.0f);
729+ // xxx: for w-buffering
730+ device.Transform.Projection = Transform_Projection;
731+ effect.SetValue("proj", Transform_Projection);
743732
744- device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
745- device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
746- device.TextureState[0].AlphaArgument2 = TextureArgument.Current;
733+ if (shadow_map_enabled)
734+ {
735+ Light_Projection = Matrix.PerspectiveFovLH(
736+ Geometry.DegreeToRadian(45.0f),
737+ 1.0f,
738+ 20.0f,
739+ 250.0f);
740+ effect.SetValue("lightproj", Light_Projection);
741+ }
747742
748- device.SetRenderState(RenderStates.AlphaBlendEnable, true);
749- device.SetRenderState(RenderStates.SourceBlend, (int)Blend.SourceAlpha);
750- device.SetRenderState(RenderStates.DestinationBlend, (int)Blend.InvSourceAlpha);
751- device.SetRenderState(RenderStates.AlphaTestEnable, true);
752- device.SetRenderState(RenderStates.ReferenceAlpha, 0x08);
753- device.SetRenderState(RenderStates.AlphaFunction, (int)Compare.GreaterEqual);
743+ device.SetRenderState(RenderStates.Lighting, false);
744+ device.SetRenderState(RenderStates.CullMode, (int)Cull.CounterClockwise);
754745
755- vd = new VertexDeclaration(device, TSOSubMesh.ve);
746+ device.TextureState[0].AlphaOperation = TextureOperation.Modulate;
747+ device.TextureState[0].AlphaArgument1 = TextureArgument.TextureColor;
748+ device.TextureState[0].AlphaArgument2 = TextureArgument.Current;
756749
757- //device.RenderState.IndexedVertexBlendEnable = true;
758- }
750+ device.SetRenderState(RenderStates.AlphaBlendEnable, true);
751+ device.SetRenderState(RenderStates.SourceBlend, (int)Blend.SourceAlpha);
752+ device.SetRenderState(RenderStates.DestinationBlend, (int)Blend.InvSourceAlpha);
753+ device.SetRenderState(RenderStates.AlphaTestEnable, true);
754+ device.SetRenderState(RenderStates.ReferenceAlpha, 0x08);
755+ device.SetRenderState(RenderStates.AlphaFunction, (int)Compare.GreaterEqual);
759756
760- /// <summary>
761- /// 全フィギュアを削除します。
762- /// </summary>
763- public void ClearFigureList()
764- {
765- foreach (Figure fig in FigureList)
766- fig.Dispose();
767- FigureList.Clear();
768- SetFigureIndex(0);
769- // free meshes and textures.
770- Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(true));
771- }
757+ vd = new VertexDeclaration(device, TSOSubMesh.ve);
772758
773- /// <summary>
774- /// 選択フィギュアを削除します。
775- /// </summary>
776- public void RemoveSelectedFigure()
777- {
778- Figure fig;
779- if (TryGetFigure(out fig))
780- {
781- fig.Dispose();
782- FigureList.Remove(fig);
783- SetFigureIndex(fig_index-1);
759+ //device.RenderState.IndexedVertexBlendEnable = true;
784760 }
785- fig = null;
786- // free meshes and textures.
787- Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(true));
788- }
789761
790- bool motionEnabled = false;
791-
792- /// <summary>
793- /// モーションの有無
794- /// </summary>
795- public bool MotionEnabled
796- {
797- get
762+ /// <summary>
763+ /// 全フィギュアを削除します。
764+ /// </summary>
765+ public void ClearFigureList()
798766 {
799- return motionEnabled;
767+ foreach (Figure fig in FigureList)
768+ fig.Dispose();
769+ FigureList.Clear();
770+ SetFigureIndex(0);
771+ // free meshes and textures.
772+ Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(true));
800773 }
801- set
774+
775+ /// <summary>
776+ /// 選択フィギュアを削除します。
777+ /// </summary>
778+ public void RemoveSelectedFigure()
802779 {
803- motionEnabled = value;
804-
805- if (motionEnabled)
780+ Figure fig;
781+ if (TryGetFigure(out fig))
806782 {
807- start_ticks = DateTime.Now.Ticks;
808- start_frame_index = frame_index;
783+ fig.Dispose();
784+ FigureList.Remove(fig);
785+ SetFigureIndex(fig_index - 1);
809786 }
787+ fig = null;
788+ // free meshes and textures.
789+ Console.WriteLine("Total Memory: {0}", GC.GetTotalMemory(true));
810790 }
811- }
812791
813- /// <summary>
814- /// シャドウマップの有無
815- /// </summary>
816- public bool ShadowShown = false;
792+ bool motionEnabled = false;
817793
818- /// <summary>
819- /// スプライトの有無
820- /// </summary>
821- public bool SpriteShown = false;
822-
823- /// <summary>
824- /// モーションが有効であるか。
825- /// </summary>
826- [Obsolete("use MotionEnabled", true)]
827- public bool IsMotionEnabled()
828- {
829- return motionEnabled;
830- }
831-
832- /// <summary>
833- /// モーションの有無を切り替えます。
834- /// </summary>
835- [Obsolete("use MotionEnabled", true)]
836- public void SwitchMotionEnabled()
837- {
838- MotionEnabled = ! MotionEnabled;
839- }
840- long start_ticks = 0;
841- int start_frame_index = 0;
842-
843- /// <summary>
844- /// シャドウマップの有無を切り替えます。
845- /// </summary>
846- [Obsolete("use ShadowShown", true)]
847- public void SwitchShadowShown()
848- {
849- ShadowShown = ! ShadowShown;
850- }
851-
852- /// <summary>
853- /// スプライトの有無を切り替えます。
854- /// </summary>
855- [Obsolete("use SpriteShown", true)]
856- public void SwitchSpriteShown()
857- {
858- SpriteShown = ! SpriteShown;
859- }
860-
861- /// <summary>
862- /// フレームを進めるのに用いるデリゲート型
863- /// </summary>
864- public delegate void FrameMovingHandler();
865-
866- /// <summary>
867- /// フレームを進めるハンドラ
868- /// </summary>
869- public FrameMovingHandler FrameMoving;
870-
871- /// <summary>
872- /// 次のシーンフレームに進みます。
873- /// </summary>
874- public void FrameMove()
875- {
876- if (motionEnabled)
794+ /// <summary>
795+ /// モーションの有無
796+ /// </summary>
797+ public bool MotionEnabled
877798 {
878- int frame_len = GetMaxFrameLength();
879- if (frame_len > 0)
799+ get
880800 {
881- long ticks = DateTime.Now.Ticks - start_ticks;
882- long current_frame_index = (long)(start_frame_index + ticks * 0.000006);
883- frame_index = (int)(current_frame_index % frame_len);
884- Debug.Assert(frame_index >= 0);
885- Debug.Assert(frame_index < frame_len);
801+ return motionEnabled;
886802 }
803+ set
804+ {
805+ motionEnabled = value;
887806
888- //フレーム番号を通知する。
889- //camera.SetFrameIndex(frame_index);
807+ if (motionEnabled)
808+ {
809+ start_ticks = DateTime.Now.Ticks;
810+ start_frame_index = frame_index;
811+ }
812+ }
890813 }
891- FrameMove(frame_index);
892814
893- if (FrameMoving != null)
894- FrameMoving();
895- }
815+ /// <summary>
816+ /// シャドウマップの有無
817+ /// </summary>
818+ public bool ShadowShown = false;
896819
897- /// <summary>
898- /// 指定シーンフレームに進みます。
899- /// </summary>
900- /// <param name="frame_index">フレーム番号</param>
901- public void FrameMove(int frame_index)
902- {
903- if (camera.NeedUpdate)
820+ /// <summary>
821+ /// スプライトの有無
822+ /// </summary>
823+ public bool SpriteShown = false;
824+
825+ /// <summary>
826+ /// モーションが有効であるか。
827+ /// </summary>
828+ [Obsolete("use MotionEnabled", true)]
829+ public bool IsMotionEnabled()
904830 {
905- camera.Update();
906- Transform_View = camera.ViewMatrix;
907- // xxx: for w-buffering
908- device.Transform.View = Transform_View;
909- effect.SetValue("view", Transform_View);
831+ return motionEnabled;
910832 }
911833
912- if (shadow_map_enabled)
834+ /// <summary>
835+ /// モーションの有無を切り替えます。
836+ /// </summary>
837+ [Obsolete("use MotionEnabled", true)]
838+ public void SwitchMotionEnabled()
913839 {
914- Figure fig;
915- if (TryGetFigure(out fig))
916- {
917- float scale = 40.0f;
918- Light_View = Matrix.LookAtLH(
919- fig.LightDirection * -scale,
920- new Vector3(0.0f, 5.0f, 0.0f),
921- new Vector3(0.0f, 1.0f, 0.0f));
922- effect.SetValue("lightview", Light_View);
923- }
840+ MotionEnabled = !MotionEnabled;
924841 }
842+ long start_ticks = 0;
843+ int start_frame_index = 0;
925844
926- if (motionEnabled)
845+ /// <summary>
846+ /// シャドウマップの有無を切り替えます。
847+ /// </summary>
848+ [Obsolete("use ShadowShown", true)]
849+ public void SwitchShadowShown()
927850 {
928- //フレーム番号を通知する。
929- foreach (Figure fig in FigureList)
930- fig.SetFrameIndex(frame_index);
851+ ShadowShown = !ShadowShown;
852+ }
931853
932- //device.Transform.World = world_matrix;
933- foreach (Figure fig in FigureList)
934- fig.UpdateBoneMatrices();
854+ /// <summary>
855+ /// スプライトの有無を切り替えます。
856+ /// </summary>
857+ [Obsolete("use SpriteShown", true)]
858+ public void SwitchSpriteShown()
859+ {
860+ SpriteShown = !SpriteShown;
935861 }
936- }
937862
938- private int frame_index = 0;
939- /// <summary>
940- /// フレーム番号
941- /// </summary>
942- public int FrameIndex { get { return frame_index; } set { frame_index = value; } }
863+ /// <summary>
864+ /// フレームを進めるのに用いるデリゲート型
865+ /// </summary>
866+ public delegate void FrameMovingHandler();
943867
944- /// <summary>
945- /// tmo file中で最大のフレーム長さを得ます。
946- /// </summary>
947- /// <returns>フレーム長さ</returns>
948- public int GetMaxFrameLength()
949- {
950- int max = 0;
951- foreach (Figure fig in FigureList)
952- if (fig.Tmo.frames != null && max < fig.Tmo.frames.Length)
953- max = fig.Tmo.frames.Length;
954- return max;
955- }
868+ /// <summary>
869+ /// フレームを進めるハンドラ
870+ /// </summary>
871+ public FrameMovingHandler FrameMoving;
956872
957- /// <summary>
958- /// レンダリングするのに用いるデリゲート型
959- /// </summary>
960- public delegate void RenderingHandler();
873+ /// <summary>
874+ /// 次のシーンフレームに進みます。
875+ /// </summary>
876+ public void FrameMove()
877+ {
878+ if (motionEnabled)
879+ {
880+ int frame_len = GetMaxFrameLength();
881+ if (frame_len > 0)
882+ {
883+ long ticks = DateTime.Now.Ticks - start_ticks;
884+ long current_frame_index = (long)(start_frame_index + ticks * 0.000006);
885+ frame_index = (int)(current_frame_index % frame_len);
886+ Debug.Assert(frame_index >= 0);
887+ Debug.Assert(frame_index < frame_len);
888+ }
961889
962- /// <summary>
963- /// レンダリングするハンドラ
964- /// </summary>
965- public RenderingHandler Rendering;
890+ //フレーム番号を通知する。
891+ //camera.SetFrameIndex(frame_index);
892+ }
893+ FrameMove(frame_index);
966894
967- /// <summary>
968- /// シーンをレンダリングします。
969- /// </summary>
970- public void Render()
971- {
972- device.BeginScene();
973-
974- {
975- Matrix world_view_matrix = world_matrix * Transform_View;
976- Matrix world_view_projection_matrix = world_view_matrix * Transform_Projection;
977- effect.SetValue("wld", world_matrix);
978- effect.SetValue("wv", world_view_matrix);
979- effect.SetValue("wvp", world_view_projection_matrix);
895+ if (FrameMoving != null)
896+ FrameMoving();
980897 }
981898
982- if (shadow_map_enabled)
899+ /// <summary>
900+ /// 指定シーンフレームに進みます。
901+ /// </summary>
902+ /// <param name="frame_index">フレーム番号</param>
903+ public void FrameMove(int frame_index)
983904 {
984- if (ShadowShown)
905+ if (camera.NeedUpdate)
985906 {
986- DrawShadowMap();
907+ camera.Update();
908+ Transform_View = camera.ViewMatrix;
909+ // xxx: for w-buffering
910+ device.Transform.View = Transform_View;
911+ effect.SetValue("view", Transform_View);
987912 }
988- else
913+
914+ if (shadow_map_enabled)
989915 {
990- ClearShadowMap();
916+ Figure fig;
917+ if (TryGetFigure(out fig))
918+ {
919+ float scale = 40.0f;
920+ Light_View = Matrix.LookAtLH(
921+ fig.LightDirection * -scale,
922+ new Vector3(0.0f, 5.0f, 0.0f),
923+ new Vector3(0.0f, 1.0f, 0.0f));
924+ effect.SetValue("lightview", Light_View);
925+ }
991926 }
927+
928+ if (motionEnabled)
929+ {
930+ //フレーム番号を通知する。
931+ foreach (Figure fig in FigureList)
932+ fig.SetFrameIndex(frame_index);
933+
934+ //device.Transform.World = world_matrix;
935+ foreach (Figure fig in FigureList)
936+ fig.UpdateBoneMatrices();
937+ }
992938 }
993939
994- DrawFigure();
940+ private int frame_index = 0;
941+ /// <summary>
942+ /// フレーム番号
943+ /// </summary>
944+ public int FrameIndex { get { return frame_index; } set { frame_index = value; } }
995945
996- if (shadow_map_enabled && SpriteShown)
946+ /// <summary>
947+ /// tmo file中で最大のフレーム長さを得ます。
948+ /// </summary>
949+ /// <returns>フレーム長さ</returns>
950+ public int GetMaxFrameLength()
997951 {
998- DrawSprite();
952+ int max = 0;
953+ foreach (Figure fig in FigureList)
954+ if (fig.Tmo.frames != null && max < fig.Tmo.frames.Length)
955+ max = fig.Tmo.frames.Length;
956+ return max;
999957 }
1000958
1001- if (Rendering != null)
1002- Rendering();
959+ /// <summary>
960+ /// レンダリングするのに用いるデリゲート型
961+ /// </summary>
962+ public delegate void RenderingHandler();
1003963
1004- device.EndScene();
964+ /// <summary>
965+ /// レンダリングするハンドラ
966+ /// </summary>
967+ public RenderingHandler Rendering;
968+
969+ /// <summary>
970+ /// シーンをレンダリングします。
971+ /// </summary>
972+ public void Render()
1005973 {
1006- int ret;
1007- if (! device.CheckCooperativeLevel(out ret))
974+ device.BeginScene();
975+
1008976 {
1009- switch ((ResultCode)ret)
977+ Matrix world_view_matrix = world_matrix * Transform_View;
978+ Matrix world_view_projection_matrix = world_view_matrix * Transform_Projection;
979+ effect.SetValue("wld", world_matrix);
980+ effect.SetValue("wv", world_view_matrix);
981+ effect.SetValue("wvp", world_view_projection_matrix);
982+ }
983+
984+ if (shadow_map_enabled)
985+ {
986+ if (ShadowShown)
1010987 {
1011- case ResultCode.DeviceLost:
1012- Thread.Sleep(30);
1013- return;
1014- case ResultCode.DeviceNotReset:
1015- device.Reset(device.PresentationParameters);
1016- break;
1017- default:
1018- Console.WriteLine((ResultCode)ret);
1019- return;
988+ DrawShadowMap();
1020989 }
990+ else
991+ {
992+ ClearShadowMap();
993+ }
1021994 }
1022- }
1023995
1024- device.Present();
1025- Thread.Sleep(30);
1026- }
996+ DrawFigure();
1027997
1028- void DrawShadowMap()
1029- {
1030- device.SetRenderState(RenderStates.AlphaBlendEnable, false);
998+ if (shadow_map_enabled && SpriteShown)
999+ {
1000+ DrawSprite();
1001+ }
10311002
1032- device.SetRenderTarget(0, ztex_surface);
1033- device.DepthStencilSurface = ztex_zbuf;
1034- device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
1003+ if (Rendering != null)
1004+ Rendering();
10351005
1036- device.VertexDeclaration = vd;
1037- effect.Technique = handle_ShadowMap;
1006+ device.EndScene();
1007+ {
1008+ int ret;
1009+ if (!device.CheckCooperativeLevel(out ret))
1010+ {
1011+ switch ((ResultCode)ret)
1012+ {
1013+ case ResultCode.DeviceLost:
1014+ Thread.Sleep(30);
1015+ return;
1016+ case ResultCode.DeviceNotReset:
1017+ device.Reset(device.PresentationParameters);
1018+ break;
1019+ default:
1020+ Console.WriteLine((ResultCode)ret);
1021+ return;
1022+ }
1023+ }
1024+ }
10381025
1039- foreach (Figure fig in FigureList)
1040- foreach (TSOFile tso in fig.TSOList)
1026+ device.Present();
1027+ Thread.Sleep(30);
1028+ }
1029+
1030+ void DrawShadowMap()
10411031 {
1042- //tso.BeginRender();
1032+ device.SetRenderState(RenderStates.AlphaBlendEnable, false);
10431033
1044- foreach (TSOMesh mesh in tso.meshes)
1045- foreach (TSOSubMesh sub_mesh in mesh.sub_meshes)
1046- {
1047- //device.RenderState.VertexBlend = (VertexBlend)(4 - 1);
1048- device.SetStreamSource(0, sub_mesh.vb, 0, 52);
1034+ device.SetRenderTarget(0, ztex_surface);
1035+ device.DepthStencilSurface = ztex_zbuf;
1036+ device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
10491037
1050- //tso.SwitchShader(sub_mesh);
1051- effect.SetValue(handle_LocalBoneMats, fig.ClipBoneMatrices(sub_mesh));
1038+ device.VertexDeclaration = vd;
1039+ effect.Technique = handle_ShadowMap;
10521040
1053- int npass = effect.Begin(0);
1054- for (int ipass = 0; ipass < npass; ipass++)
1041+ foreach (Figure fig in FigureList)
1042+ foreach (TSOFile tso in fig.TSOList)
10551043 {
1056- effect.BeginPass(ipass);
1057- device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, sub_mesh.vertices.Length - 2);
1058- effect.EndPass();
1044+ //tso.BeginRender();
1045+
1046+ foreach (TSOMesh mesh in tso.meshes)
1047+ foreach (TSOSubMesh sub_mesh in mesh.sub_meshes)
1048+ {
1049+ //device.RenderState.VertexBlend = (VertexBlend)(4 - 1);
1050+ device.SetStreamSource(0, sub_mesh.vb, 0, 52);
1051+
1052+ //tso.SwitchShader(sub_mesh);
1053+ effect.SetValue(handle_LocalBoneMats, fig.ClipBoneMatrices(sub_mesh));
1054+
1055+ int npass = effect.Begin(0);
1056+ for (int ipass = 0; ipass < npass; ipass++)
1057+ {
1058+ effect.BeginPass(ipass);
1059+ device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, sub_mesh.vertices.Length - 2);
1060+ effect.EndPass();
1061+ }
1062+ effect.End();
1063+ }
1064+ //tso.EndRender();
10591065 }
1060- effect.End();
1061- }
1062- //tso.EndRender();
10631066 }
1064- }
10651067
1066- void ClearShadowMap()
1067- {
1068- device.SetRenderTarget(0, ztex_surface);
1069- device.DepthStencilSurface = ztex_zbuf;
1070- device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
1071- }
1068+ void ClearShadowMap()
1069+ {
1070+ device.SetRenderTarget(0, ztex_surface);
1071+ device.DepthStencilSurface = ztex_zbuf;
1072+ device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.White, 1.0f, 0);
1073+ }
10721074
1073- /// config: ほほ赤みの濃さ
1074- public float HohoAlpha { get; set; }
1075+ /// config: ほほ赤みの濃さ
1076+ public float HohoAlpha { get; set; }
10751077
1076- /// config: スクリーン塗りつぶし色
1077- public Color ScreenColor { get; set; }
1078+ /// config: スクリーン塗りつぶし色
1079+ public Color ScreenColor { get; set; }
10781080
1079- /// <summary>
1080- /// UVSCR値を得ます。
1081- /// </summary>
1082- /// <returns></returns>
1083- public Vector4 UVSCR()
1084- {
1085- float x = Environment.TickCount * 0.000002f;
1086- return new Vector4(x, 0.0f, 0.0f, 0.0f);
1087- }
1081+ /// <summary>
1082+ /// UVSCR値を得ます。
1083+ /// </summary>
1084+ /// <returns></returns>
1085+ public Vector4 UVSCR()
1086+ {
1087+ float x = Environment.TickCount * 0.000002f;
1088+ return new Vector4(x, 0.0f, 0.0f, 0.0f);
1089+ }
10881090
1089- /// <summary>
1090- /// フィギュアを描画します。
1091- /// </summary>
1092- protected virtual void DrawFigure()
1093- {
1094- device.SetRenderState(RenderStates.AlphaBlendEnable, true);
1091+ /// <summary>
1092+ /// フィギュアを描画します。
1093+ /// </summary>
1094+ protected virtual void DrawFigure()
1095+ {
1096+ device.SetRenderState(RenderStates.AlphaBlendEnable, true);
10951097
1096- device.SetRenderTarget(0, dev_surface);
1097- device.DepthStencilSurface = dev_zbuf;
1098- device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, ScreenColor, 1.0f, 0);
1098+ device.SetRenderTarget(0, dev_surface);
1099+ device.DepthStencilSurface = dev_zbuf;
1100+ device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, ScreenColor, 1.0f, 0);
10991101
1100- device.VertexDeclaration = vd;
1101- effect.SetValue(handle_HohoAlpha, HohoAlpha);
1102- effect.SetValue(handle_UVSCR, UVSCR());
1103- foreach (Figure fig in FigureList)
1104- {
1105- effect.SetValue(handle_LightDirForced, fig.LightDirForced());
1106- foreach (TSOFile tso in fig.TSOList)
1102+ device.VertexDeclaration = vd;
1103+ effect.SetValue(handle_HohoAlpha, HohoAlpha);
1104+ effect.SetValue(handle_UVSCR, UVSCR());
1105+ foreach (Figure fig in FigureList)
11071106 {
1108- tso.BeginRender();
1107+ effect.SetValue(handle_LightDirForced, fig.LightDirForced());
1108+ foreach (TSOFile tso in fig.TSOList)
1109+ {
1110+ tso.BeginRender();
11091111
1110- foreach (TSOMesh mesh in tso.meshes)
1111- foreach (TSOSubMesh sub_mesh in mesh.sub_meshes)
1112- {
1113- //device.RenderState.VertexBlend = (VertexBlend)(4 - 1);
1114- device.SetStreamSource(0, sub_mesh.vb, 0, 52);
1112+ foreach (TSOMesh mesh in tso.meshes)
1113+ foreach (TSOSubMesh sub_mesh in mesh.sub_meshes)
1114+ {
1115+ //device.RenderState.VertexBlend = (VertexBlend)(4 - 1);
1116+ device.SetStreamSource(0, sub_mesh.vb, 0, 52);
11151117
1116- tso.SwitchShader(sub_mesh);
1117- effect.SetValue(handle_LocalBoneMats, fig.ClipBoneMatrices(sub_mesh));
1118+ tso.SwitchShader(sub_mesh);
1119+ effect.SetValue(handle_LocalBoneMats, fig.ClipBoneMatrices(sub_mesh));
11181120
1119- int npass = effect.Begin(0);
1120- for (int ipass = 0; ipass < npass; ipass++)
1121- {
1122- effect.BeginPass(ipass);
1123- device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, sub_mesh.vertices.Length - 2);
1124- effect.EndPass();
1121+ int npass = effect.Begin(0);
1122+ for (int ipass = 0; ipass < npass; ipass++)
1123+ {
1124+ effect.BeginPass(ipass);
1125+ device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, sub_mesh.vertices.Length - 2);
1126+ effect.EndPass();
1127+ }
1128+ effect.End();
11251129 }
1126- effect.End();
1127- }
1128- tso.EndRender();
1130+ tso.EndRender();
1131+ }
11291132 }
11301133 }
1131- }
11321134
1133- void DrawSprite()
1134- {
1135- device.SetRenderState(RenderStates.AlphaBlendEnable, false);
1135+ void DrawSprite()
1136+ {
1137+ device.SetRenderState(RenderStates.AlphaBlendEnable, false);
11361138
1137- sprite.Transform = Matrix.Scaling(w_scale, h_scale, 1.0f);
1139+ sprite.Transform = Matrix.Scaling(w_scale, h_scale, 1.0f);
11381140
1139- sprite.Begin(0);
1140- sprite.Draw(ztex, ztex_rect, new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White);
1141- sprite.End();
1142- }
1141+ sprite.Begin(0);
1142+ sprite.Draw(ztex, ztex_rect, new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White);
1143+ sprite.End();
1144+ }
11431145
1144- /// <summary>
1145- /// Direct3Dメッシュを描画します。
1146- /// </summary>
1147- /// <param name="mesh">メッシュ</param>
1148- /// <param name="wld">ワールド変換行列</param>
1149- /// <param name="color">描画色</param>
1150- public void DrawMesh(Mesh mesh, Matrix wld, Vector4 color)
1151- {
1152- effect.Technique = "BONE";
1146+ /// <summary>
1147+ /// Direct3Dメッシュを描画します。
1148+ /// </summary>
1149+ /// <param name="mesh">メッシュ</param>
1150+ /// <param name="wld">ワールド変換行列</param>
1151+ /// <param name="color">描画色</param>
1152+ public void DrawMesh(Mesh mesh, Matrix wld, Vector4 color)
1153+ {
1154+ effect.Technique = "BONE";
11531155
1154- Matrix wv = wld * Transform_View;
1155- Matrix wvp = wv * Transform_Projection;
1156+ Matrix wv = wld * Transform_View;
1157+ Matrix wvp = wv * Transform_Projection;
11561158
1157- effect.SetValue("wld", wld);
1158- effect.SetValue("wv", wv);
1159- effect.SetValue("wvp", wvp);
1159+ effect.SetValue("wld", wld);
1160+ effect.SetValue("wv", wv);
1161+ effect.SetValue("wvp", wvp);
11601162
1161- effect.SetValue("ManColor", color);
1163+ effect.SetValue("ManColor", color);
11621164
1163- int npass = effect.Begin(0);
1164- for (int ipass = 0; ipass < npass; ipass++)
1165+ int npass = effect.Begin(0);
1166+ for (int ipass = 0; ipass < npass; ipass++)
1167+ {
1168+ effect.BeginPass(ipass);
1169+ mesh.DrawSubset(0);
1170+ effect.EndPass();
1171+ }
1172+ effect.End();
1173+ }
1174+
1175+ /// <summary>
1176+ /// 内部objectを破棄します。
1177+ /// </summary>
1178+ public void Dispose()
11651179 {
1166- effect.BeginPass(ipass);
1167- mesh.DrawSubset(0);
1168- effect.EndPass();
1180+ foreach (Figure fig in FigureList)
1181+ fig.Dispose();
1182+ if (line != null)
1183+ line.Dispose();
1184+ if (sprite != null)
1185+ sprite.Dispose();
1186+ if (ztex_zbuf != null)
1187+ ztex_zbuf.Dispose();
1188+ if (ztex_surface != null)
1189+ ztex_surface.Dispose();
1190+ if (ztex != null)
1191+ ztex.Dispose();
1192+ if (dev_zbuf != null)
1193+ dev_zbuf.Dispose();
1194+ if (dev_surface != null)
1195+ dev_surface.Dispose();
1196+ if (vd != null)
1197+ vd.Dispose();
1198+ if (effect != null)
1199+ effect.Dispose();
1200+ if (device != null)
1201+ device.Dispose();
11691202 }
1170- effect.End();
1171- }
11721203
1173- /// <summary>
1174- /// 内部objectを破棄します。
1175- /// </summary>
1176- public void Dispose()
1177- {
1178- foreach (Figure fig in FigureList)
1179- fig.Dispose();
1180- if (line != null)
1181- line.Dispose();
1182- if (sprite != null)
1183- sprite.Dispose();
1184- if (ztex_zbuf != null)
1185- ztex_zbuf.Dispose();
1186- if (ztex_surface != null)
1187- ztex_surface.Dispose();
1188- if (ztex != null)
1189- ztex.Dispose();
1190- if (dev_zbuf != null)
1191- dev_zbuf.Dispose();
1192- if (dev_surface != null)
1193- dev_surface.Dispose();
1194- if (vd != null)
1195- vd.Dispose();
1196- if (effect != null)
1197- effect.Dispose();
1198- if (device != null)
1199- device.Dispose();
1200- }
1204+ /// <summary>
1205+ /// 指定パスからPNGFileを読み込みフィギュアを作成します。
1206+ /// </summary>
1207+ /// <param name="source_file">PNGFileのパス</param>
1208+ public PNGSaveFile LoadPNGFile(string source_file)
1209+ {
1210+ PNGSaveFile sav = new PNGSaveFile();
12011211
1202- /// <summary>
1203- /// 指定パスからPNGFileを読み込みフィギュアを作成します。
1204- /// </summary>
1205- /// <param name="source_file">PNGFileのパス</param>
1206- public PNGSaveFile LoadPNGFile(string source_file)
1207- {
1208- PNGSaveFile sav = new PNGSaveFile();
1212+ if (File.Exists(source_file))
1213+ try
1214+ {
1215+ PNGFile png = new PNGFile();
1216+ Figure fig = null;
12091217
1210- if (File.Exists(source_file))
1211- try
1212- {
1213- PNGFile png = new PNGFile();
1214- Figure fig = null;
1218+ png.Hsav += delegate (string type)
1219+ {
1220+ sav.type = type;
1221+ fig = new Figure();
1222+ sav.FigureList.Add(fig);
1223+ };
1224+ png.Pose += delegate (string type)
1225+ {
1226+ sav.type = type;
1227+ };
1228+ png.Scne += delegate (string type)
1229+ {
1230+ sav.type = type;
1231+ };
1232+ png.Cami += delegate (Stream dest, int extract_length)
1233+ {
1234+ byte[] buf = new byte[extract_length];
1235+ dest.Read(buf, 0, extract_length);
12151236
1216- png.Hsav += delegate(string type)
1217- {
1218- sav.type = type;
1219- fig = new Figure();
1220- sav.FigureList.Add(fig);
1221- };
1222- png.Pose += delegate(string type)
1223- {
1224- sav.type = type;
1225- };
1226- png.Scne += delegate(string type)
1227- {
1228- sav.type = type;
1229- };
1230- png.Cami += delegate(Stream dest, int extract_length)
1231- {
1232- byte[] buf = new byte[extract_length];
1233- dest.Read(buf, 0, extract_length);
1237+ List<float> factor = new List<float>();
1238+ for (int offset = 0; offset < extract_length; offset += sizeof(float))
1239+ {
1240+ float flo = BitConverter.ToSingle(buf, offset);
1241+ factor.Add(flo);
1242+ }
1243+ camera.Reset();
1244+ camera.Translation = new Vector3(-factor[0], -factor[1], -factor[2]);
1245+ camera.Angle = new Vector3(-factor[5], -factor[4], -factor[6]);
1246+ };
1247+ png.Lgta += delegate (Stream dest, int extract_length)
1248+ {
1249+ byte[] buf = new byte[extract_length];
1250+ dest.Read(buf, 0, extract_length);
12341251
1235- List<float> factor = new List<float>();
1236- for (int offset = 0; offset < extract_length; offset += sizeof(float))
1237- {
1238- float flo = BitConverter.ToSingle(buf, offset);
1239- factor.Add(flo);
1240- }
1241- camera.Reset();
1242- camera.Translation = new Vector3(-factor[0], -factor[1], -factor[2]);
1243- camera.Angle = new Vector3(-factor[5], -factor[4], -factor[6]);
1244- };
1245- png.Lgta += delegate(Stream dest, int extract_length)
1246- {
1247- byte[] buf = new byte[extract_length];
1248- dest.Read(buf, 0, extract_length);
1252+ List<float> factor = new List<float>();
1253+ for (int offset = 0; offset < extract_length; offset += sizeof(float))
1254+ {
1255+ float flo = BitConverter.ToSingle(buf, offset);
1256+ factor.Add(flo);
1257+ }
12491258
1250- List<float> factor = new List<float>();
1251- for (int offset = 0; offset < extract_length; offset += sizeof(float))
1252- {
1253- float flo = BitConverter.ToSingle(buf, offset);
1254- factor.Add(flo);
1255- }
1259+ Matrix m;
1260+ m.M11 = factor[0];
1261+ m.M12 = factor[1];
1262+ m.M13 = factor[2];
1263+ m.M14 = factor[3];
12561264
1257- Matrix m;
1258- m.M11 = factor[0];
1259- m.M12 = factor[1];
1260- m.M13 = factor[2];
1261- m.M14 = factor[3];
1265+ m.M21 = factor[4];
1266+ m.M22 = factor[5];
1267+ m.M23 = factor[6];
1268+ m.M24 = factor[7];
12621269
1263- m.M21 = factor[4];
1264- m.M22 = factor[5];
1265- m.M23 = factor[6];
1266- m.M24 = factor[7];
1270+ m.M31 = factor[8];
1271+ m.M32 = factor[9];
1272+ m.M33 = factor[10];
1273+ m.M34 = factor[11];
12671274
1268- m.M31 = factor[8];
1269- m.M32 = factor[9];
1270- m.M33 = factor[10];
1271- m.M34 = factor[11];
1275+ m.M41 = factor[12];
1276+ m.M42 = factor[13];
1277+ m.M43 = factor[14];
1278+ m.M44 = factor[15];
12721279
1273- m.M41 = factor[12];
1274- m.M42 = factor[13];
1275- m.M43 = factor[14];
1276- m.M44 = factor[15];
1280+ sav.LightDirection = Vector3.TransformCoordinate(new Vector3(0.0f, 0.0f, -1.0f), m);
1281+ };
1282+ png.Ftmo += delegate (Stream dest, int extract_length)
1283+ {
1284+ sav.Tmo = new TMOFile();
1285+ sav.Tmo.Load(dest);
1286+ };
1287+ png.Figu += delegate (Stream dest, int extract_length)
1288+ {
1289+ fig = new Figure();
1290+ fig.LightDirection = sav.LightDirection;
1291+ fig.Tmo = sav.Tmo;
1292+ sav.FigureList.Add(fig);
12771293
1278- sav.LightDirection = Vector3.TransformCoordinate(new Vector3(0.0f, 0.0f, -1.0f), m);
1279- };
1280- png.Ftmo += delegate(Stream dest, int extract_length)
1281- {
1282- sav.Tmo = new TMOFile();
1283- sav.Tmo.Load(dest);
1284- };
1285- png.Figu += delegate(Stream dest, int extract_length)
1286- {
1287- fig = new Figure();
1288- fig.LightDirection = sav.LightDirection;
1289- fig.Tmo = sav.Tmo;
1290- sav.FigureList.Add(fig);
1294+ byte[] buf = new byte[extract_length];
1295+ dest.Read(buf, 0, extract_length);
12911296
1292- byte[] buf = new byte[extract_length];
1293- dest.Read(buf, 0, extract_length);
1297+ List<float> ratios = new List<float>();
1298+ for (int offset = 0; offset < extract_length; offset += sizeof(float))
1299+ {
1300+ float flo = BitConverter.ToSingle(buf, offset);
1301+ ratios.Add(flo);
1302+ }
1303+ /*
1304+ ◆FIGU
1305+ スライダの位置。値は float型で 0.0 .. 1.0
1306+ 0: 姉妹
1307+ 1: うで
1308+ 2: あし
1309+ 3: 胴まわり
1310+ 4: おっぱい
1311+ 5: つり目たれ目
1312+ 6: やわらか
1313+ */
1314+ if (fig.slider_matrix != null)
1315+ {
1316+ fig.slider_matrix.TallRatio = ratios[0];
1317+ fig.slider_matrix.ArmRatio = ratios[1];
1318+ fig.slider_matrix.LegRatio = ratios[2];
1319+ fig.slider_matrix.WaistRatio = ratios[3];
1320+ fig.slider_matrix.BustRatio = ratios[4];
1321+ fig.slider_matrix.EyeRatio = ratios[5];
1322+ }
12941323
1295- List<float> ratios = new List<float>();
1296- for (int offset = 0; offset < extract_length; offset += sizeof(float))
1297- {
1298- float flo = BitConverter.ToSingle(buf, offset);
1299- ratios.Add(flo);
1300- }
1301- /*
1302- ◆FIGU
1303- スライダの位置。値は float型で 0.0 .. 1.0
1304- 0: 姉妹
1305- 1: うで
1306- 2: あし
1307- 3: 胴まわり
1308- 4: おっぱい
1309- 5: つり目たれ目
1310- 6: やわらか
1311- */
1312- if (fig.slider_matrix != null)
1313- {
1314- fig.slider_matrix.TallRatio = ratios[0];
1315- fig.slider_matrix.ArmRatio = ratios[1];
1316- fig.slider_matrix.LegRatio = ratios[2];
1317- fig.slider_matrix.WaistRatio = ratios[3];
1318- fig.slider_matrix.BustRatio = ratios[4];
1319- fig.slider_matrix.EyeRatio = ratios[5];
1320- }
1324+ //fig.TransformTpo();
1325+ };
1326+ png.Ftso += delegate (Stream dest, int extract_length, byte[] opt1)
1327+ {
1328+ TSOFile tso = new TSOFile();
1329+ tso.Load(dest);
1330+ tso.Row = opt1[0];
1331+ fig.TSOList.Add(tso);
1332+ };
1333+ Debug.WriteLine("loading " + source_file);
1334+ png.Load(source_file);
13211335
1322- //fig.TransformTpo();
1323- };
1324- png.Ftso += delegate(Stream dest, int extract_length, byte[] opt1)
1325- {
1326- TSOFile tso = new TSOFile();
1327- tso.Load(dest);
1328- tso.Row = opt1[0];
1329- fig.TSOList.Add(tso);
1330- };
1331- Debug.WriteLine("loading " + source_file);
1332- png.Load(source_file);
1336+ if (sav.type == "HSAV")
1337+ {
1338+ BMPSaveData data = new BMPSaveData();
13331339
1334- if (sav.type == "HSAV")
1335- {
1336- BMPSaveData data = new BMPSaveData();
1340+ using (Stream stream = File.OpenRead(source_file))
1341+ data.Read(stream);
13371342
1338- using (Stream stream = File.OpenRead(source_file))
1339- data.Read(stream);
1343+ if (fig.slider_matrix != null && data.bitmap.Size == new Size(128, 256))
1344+ {
1345+ fig.slider_matrix.TallRatio = data.GetSliderValue(4);
1346+ fig.slider_matrix.ArmRatio = data.GetSliderValue(5);
1347+ fig.slider_matrix.LegRatio = data.GetSliderValue(6);
1348+ fig.slider_matrix.WaistRatio = data.GetSliderValue(7);
1349+ fig.slider_matrix.BustRatio = data.GetSliderValue(0);
1350+ fig.slider_matrix.EyeRatio = data.GetSliderValue(8);
1351+ }
13401352
1341- if (fig.slider_matrix != null && data.bitmap.Size == new Size(128, 256))
1342- {
1343- fig.slider_matrix.TallRatio = data.GetSliderValue(4);
1344- fig.slider_matrix.ArmRatio = data.GetSliderValue(5);
1345- fig.slider_matrix.LegRatio = data.GetSliderValue(6);
1346- fig.slider_matrix.WaistRatio = data.GetSliderValue(7);
1347- fig.slider_matrix.BustRatio = data.GetSliderValue(0);
1348- fig.slider_matrix.EyeRatio = data.GetSliderValue(8);
1353+ for (int i = 0; i < fig.TSOList.Count; i++)
1354+ {
1355+ TSOFile tso = fig.TSOList[i];
1356+ string file = data.GetFileName(tso.Row);
1357+ if (file != "")
1358+ tso.FileName = Path.GetFileName(file);
1359+ else
1360+ tso.FileName = string.Format("{0:X2}", tso.Row);
1361+ }
1362+ }
13491363 }
1350-
1351- for (int i = 0; i < fig.TSOList.Count; i++)
1364+ catch (Exception ex)
13521365 {
1353- TSOFile tso = fig.TSOList[i];
1354- string file = data.GetFileName(tso.Row);
1355- if (file != "")
1356- tso.FileName = Path.GetFileName(file);
1357- else
1358- tso.FileName = string.Format("{0:X2}", tso.Row);
1366+ Console.WriteLine("Error: " + ex);
13591367 }
1360- }
1368+ return sav;
13611369 }
1362- catch (Exception ex)
1370+
1371+ /// <summary>
1372+ /// バックバッファをBMP形式でファイルに保存します。
1373+ /// </summary>
1374+ /// <param name="file">ファイル名</param>
1375+ public void SaveToBitmap(string file)
13631376 {
1364- Console.WriteLine("Error: " + ex);
1377+ using (Surface sf = device.GetBackBuffer(0, 0, BackBufferType.Mono))
1378+ if (sf != null)
1379+ SurfaceLoader.Save(file, ImageFileFormat.Bmp, sf);
13651380 }
1366- return sav;
1367- }
13681381
1369- /// <summary>
1370- /// バックバッファをBMP形式でファイルに保存します。
1371- /// </summary>
1372- /// <param name="file">ファイル名</param>
1373- public void SaveToBitmap(string file)
1374- {
1375- using (Surface sf = device.GetBackBuffer(0, 0, BackBufferType.Mono))
1376- if (sf != null)
1377- SurfaceLoader.Save(file, ImageFileFormat.Bmp, sf);
1378- }
1382+ /// <summary>
1383+ /// バックバッファをPNG形式でファイルに保存します。
1384+ /// </summary>
1385+ /// <param name="file">ファイル名</param>
1386+ public void SaveToPng(string file)
1387+ {
1388+ using (Surface sf = device.GetBackBuffer(0, 0, BackBufferType.Mono))
1389+ if (sf != null)
1390+ SurfaceLoader.Save(file, ImageFileFormat.Png, sf);
1391+ }
13791392
1380- /// <summary>
1381- /// バックバッファをPNG形式でファイルに保存します。
1382- /// </summary>
1383- /// <param name="file">ファイル名</param>
1384- public void SaveToPng(string file)
1385- {
1386- using (Surface sf = device.GetBackBuffer(0, 0, BackBufferType.Mono))
1387- if (sf != null)
1388- SurfaceLoader.Save(file, ImageFileFormat.Png, sf);
1389- }
1393+ /// <summary>
1394+ /// 球とレイの衝突を見つけます。
1395+ /// </summary>
1396+ /// <param name="sphereRadius">球の半径</param>
1397+ /// <param name="sphereCenter">球の中心位置</param>
1398+ /// <param name="rayStart">光線の発射位置</param>
1399+ /// <param name="rayOrientation">光線の方向</param>
1400+ /// <param name="collisionPoint">衝突位置</param>
1401+ /// <param name="collisionTime">衝突時刻</param>
1402+ /// <returns>衝突したか</returns>
1403+ public static bool DetectSphereRayCollision(float sphereRadius, ref Vector3 sphereCenter, ref Vector3 rayStart, ref Vector3 rayOrientation, out Vector3 collisionPoint, out float collisionTime)
1404+ {
1405+ collisionTime = 0.0f;
1406+ collisionPoint = Vector3.Empty;
13901407
1391- /// <summary>
1392- /// 球とレイの衝突を見つけます。
1393- /// </summary>
1394- /// <param name="sphereRadius">球の半径</param>
1395- /// <param name="sphereCenter">球の中心位置</param>
1396- /// <param name="rayStart">光線の発射位置</param>
1397- /// <param name="rayOrientation">光線の方向</param>
1398- /// <param name="collisionPoint">衝突位置</param>
1399- /// <param name="collisionTime">衝突時刻</param>
1400- /// <returns>衝突したか</returns>
1401- public static bool DetectSphereRayCollision(float sphereRadius, ref Vector3 sphereCenter, ref Vector3 rayStart, ref Vector3 rayOrientation, out Vector3 collisionPoint, out float collisionTime)
1402- {
1403- collisionTime = 0.0f;
1404- collisionPoint = Vector3.Empty;
1408+ Vector3 u = rayStart - sphereCenter;
1409+ float a = Vector3.Dot(rayOrientation, rayOrientation);
1410+ float b = Vector3.Dot(rayOrientation, u);
1411+ float c = Vector3.Dot(u, u) - sphereRadius * sphereRadius;
1412+ if (a <= float.Epsilon)
1413+ //誤差
1414+ return false;
1415+ float d = b * b - a * c;
1416+ if (d < 0.0f)
1417+ //衝突しない
1418+ return false;
1419+ collisionTime = (-b - (float)Math.Sqrt(d)) / a;
1420+ collisionPoint = rayStart + rayOrientation * collisionTime;
1421+ return true;
1422+ }
14051423
1406- Vector3 u = rayStart - sphereCenter;
1407- float a = Vector3.Dot(rayOrientation, rayOrientation);
1408- float b = Vector3.Dot(rayOrientation, u);
1409- float c = Vector3.Dot(u, u) - sphereRadius * sphereRadius;
1410- if (a <= float.Epsilon)
1411- //誤差
1412- return false;
1413- float d = b * b - a * c;
1414- if (d < 0.0f)
1415- //衝突しない
1416- return false;
1417- collisionTime = (-b - (float)Math.Sqrt(d)) / a;
1418- collisionPoint = rayStart + rayOrientation * collisionTime;
1419- return true;
1420- }
1424+ /// <summary>
1425+ /// viewport行列を作成します。
1426+ /// </summary>
1427+ /// <param name="viewport">viewport</param>
1428+ /// <returns>viewport行列</returns>
1429+ public static Matrix CreateViewportMatrix(Viewport viewport)
1430+ {
1431+ Matrix m = Matrix.Identity;
1432+ m.M11 = (float)viewport.Width / 2;
1433+ m.M22 = -1.0f * (float)viewport.Height / 2;
1434+ m.M33 = (float)viewport.MaxZ - (float)viewport.MinZ;
1435+ m.M41 = (float)(viewport.X + viewport.Width / 2);
1436+ m.M42 = (float)(viewport.Y + viewport.Height / 2);
1437+ m.M43 = viewport.MinZ;
1438+ return m;
1439+ }
14211440
1422- /// <summary>
1423- /// viewport行列を作成します。
1424- /// </summary>
1425- /// <param name="viewport">viewport</param>
1426- /// <returns>viewport行列</returns>
1427- public static Matrix CreateViewportMatrix(Viewport viewport)
1428- {
1429- Matrix m = Matrix.Identity;
1430- m.M11 = (float)viewport.Width / 2;
1431- m.M22 = -1.0f * (float)viewport.Height / 2;
1432- m.M33 = (float)viewport.MaxZ - (float)viewport.MinZ;
1433- m.M41 = (float)(viewport.X + viewport.Width / 2);
1434- m.M42 = (float)(viewport.Y + viewport.Height / 2);
1435- m.M43 = viewport.MinZ;
1436- return m;
1437- }
1441+ /// スクリーン位置をワールド座標へ変換します。
1442+ public static Vector3 ScreenToWorld(float screenX, float screenY, float z, Viewport viewport, Matrix view, Matrix proj)
1443+ {
1444+ //スクリーン位置
1445+ Vector3 v = new Vector3(screenX, screenY, z);
14381446
1439- /// スクリーン位置をワールド座標へ変換します。
1440- public static Vector3 ScreenToWorld(float screenX, float screenY, float z, Viewport viewport, Matrix view, Matrix proj)
1441- {
1442- //スクリーン位置
1443- Vector3 v = new Vector3(screenX, screenY, z);
1447+ Matrix inv_m = Matrix.Invert(CreateViewportMatrix(viewport));
1448+ Matrix inv_proj = Matrix.Invert(proj);
1449+ Matrix inv_view = Matrix.Invert(view);
14441450
1445- Matrix inv_m = Matrix.Invert(CreateViewportMatrix(viewport));
1446- Matrix inv_proj = Matrix.Invert(proj);
1447- Matrix inv_view = Matrix.Invert(view);
1451+ //スクリーン位置をワールド座標へ変換
1452+ return Vector3.TransformCoordinate(v, inv_m * inv_proj * inv_view);
1453+ }
14481454
1449- //スクリーン位置をワールド座標へ変換
1450- return Vector3.TransformCoordinate(v, inv_m * inv_proj * inv_view);
1451- }
1455+ /// スクリーン位置をワールド座標へ変換します。
1456+ public Vector3 ScreenToWorld(float screenX, float screenY, float z)
1457+ {
1458+ return ScreenToWorld(screenX, screenY, z, device.Viewport, Transform_View, Transform_Projection);
1459+ }
14521460
1453- /// スクリーン位置をワールド座標へ変換します。
1454- public Vector3 ScreenToWorld(float screenX, float screenY, float z)
1455- {
1456- return ScreenToWorld(screenX, screenY, z, device.Viewport, Transform_View, Transform_Projection);
1457- }
1461+ /// ワールド座標をスクリーン位置へ変換します。
1462+ public static Vector3 WorldToScreen(Vector3 v, Viewport viewport, Matrix view, Matrix proj)
1463+ {
1464+ return Vector3.TransformCoordinate(v, view * proj * CreateViewportMatrix(viewport));
1465+ }
14581466
1459- /// ワールド座標をスクリーン位置へ変換します。
1460- public static Vector3 WorldToScreen(Vector3 v, Viewport viewport, Matrix view, Matrix proj)
1461- {
1462- return Vector3.TransformCoordinate(v, view * proj * CreateViewportMatrix(viewport));
1467+ /// ワールド座標をスクリーン位置へ変換します。
1468+ public Vector3 WorldToScreen(Vector3 v)
1469+ {
1470+ return WorldToScreen(v, device.Viewport, Transform_View, Transform_Projection);
1471+ }
14631472 }
1464-
1465- /// ワールド座標をスクリーン位置へ変換します。
1466- public Vector3 WorldToScreen(Vector3 v)
1467- {
1468- return WorldToScreen(v, device.Viewport, Transform_View, Transform_Projection);
1469- }
14701473 }
1471-}
Show on old repository browser