Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/FDK17プロジェクト/コード/04.グラフィック/CTexture.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 430 - (show annotations) (download)
Sat Jan 24 06:14:19 2015 UTC (9 years, 2 months ago) by kairera0467
File size: 41611 byte(s)
#34831 とりあえずそれらしい項目を用意。
#34803 修正。
#xxxxx リバース時にワイリングさせた時の座標を調整。
#xxxxx 本家DTXManiaからテクスチャの最適化メソッドを移植。
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 using System.IO;
7 using System.Diagnostics;
8 using SlimDX;
9 using SlimDX.Direct3D9;
10
11 namespace FDK
12 {
13 public class CTexture : IDisposable
14 {
15 // プロパティ
16 public bool b加算合成
17 {
18 get;
19 set;
20 }
21 public float fZ軸中心回転
22 {
23 get;
24 set;
25 }
26 public int n透明度
27 {
28 get
29 {
30 return this._透明度;
31 }
32 set
33 {
34 if( value < 0 )
35 {
36 this._透明度 = 0;
37 }
38 else if( value > 0xff )
39 {
40 this._透明度 = 0xff;
41 }
42 else
43 {
44 this._透明度 = value;
45 }
46 }
47 }
48 public Size szテクスチャサイズ
49 {
50 get;
51 private set;
52 }
53 public Size sz画像サイズ
54 {
55 get;
56 protected set;
57 }
58 public Texture texture
59 {
60 get;
61 private set;
62 }
63 public Format Format
64 {
65 get;
66 protected set;
67 }
68 public Vector3 vc拡大縮小倍率;
69
70 // 画面が変わるたび以下のプロパティを設定し治すこと。
71
72 public static Size sz論理画面 = Size.Empty;
73 public static Size sz物理画面 = Size.Empty;
74 public static Rectangle rc物理画面描画領域 = Rectangle.Empty;
75 /// <summary>
76 /// <para>論理画面を1とする場合の物理画面の倍率。</para>
77 /// <para>論理値×画面比率=物理値。</para>
78 /// </summary>
79 public static float f画面比率 = 1.0f;
80
81 // コンストラクタ
82
83 public CTexture()
84 {
85 this.sz画像サイズ = new Size( 0, 0 );
86 this.szテクスチャサイズ = new Size( 0, 0 );
87 this._透明度 = 0xff;
88 this.texture = null;
89 this.cvPositionColoredVertexies = null;
90 this.b加算合成 = false;
91 this.fZ軸中心回転 = 0f;
92 this.vc拡大縮小倍率 = new Vector3( 1f, 1f, 1f );
93 // this._txData = null;
94 }
95
96 /// <summary>
97 /// <para>指定されたビットマップオブジェクトから Managed テクスチャを作成する。</para>
98 /// <para>テクスチャのサイズは、BITMAP画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
99 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
100 /// <para>その他、ミップマップ数は 1、Usage は None、Pool は Managed、イメージフィルタは Point、ミップマップフィルタは
101 /// None、カラーキーは 0xFFFFFFFF(完全なる黒を透過)になる。</para>
102 /// </summary>
103 /// <param name="device">Direct3D9 デバイス。</param>
104 /// <param name="bitmap">作成元のビットマップ。</param>
105 /// <param name="format">テクスチャのフォーマット。</param>
106 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
107 public CTexture( Device device, Bitmap bitmap, Format format )
108 : this()
109 {
110 try
111 {
112 this.Format = format;
113 this.sz画像サイズ = new Size( bitmap.Width, bitmap.Height );
114 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
115 this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
116
117 using( var stream = new MemoryStream() )
118 {
119 bitmap.Save( stream, ImageFormat.Bmp );
120 stream.Seek( 0L, SeekOrigin.Begin );
121 int colorKey = unchecked( (int) 0xFF000000 );
122 this.texture = Texture.FromStream( device, stream, this.szテクスチャサイズ.Width, this.szテクスチャサイズ.Height, 1, Usage.None, format, poolvar, Filter.Point, Filter.None, colorKey );
123 }
124 }
125 catch ( Exception e )
126 {
127 this.Dispose();
128 throw new CTextureCreateFailedException( "ビットマップからのテクスチャの生成に失敗しました。(" + e.Message + ")" );
129 }
130 }
131
132 /// <summary>
133 /// <para>空の Managed テクスチャを作成する。</para>
134 /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
135 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
136 /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
137 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
138 /// カラーキーは 0x00000000(透過しない)になる。</para>
139 /// </summary>
140 /// <param name="device">Direct3D9 デバイス。</param>
141 /// <param name="n幅">テクスチャの幅(希望値)。</param>
142 /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
143 /// <param name="format">テクスチャのフォーマット。</param>
144 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
145 public CTexture( Device device, int n, int n高さ, Format format )
146 : this( device, n, n高さ, format, Pool.Managed )
147 {
148 }
149
150 /// <summary>
151 /// <para>指定された画像ファイルから Managed テクスチャを作成する。</para>
152 /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
153 /// </summary>
154 /// <param name="device">Direct3D9 デバイス。</param>
155 /// <param name="strファイル名">画像ファイル名。</param>
156 /// <param name="format">テクスチャのフォーマット。</param>
157 /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
158 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
159 public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する )
160 : this( device, strファイル名, format, b黒を透過する, Pool.Managed )
161 {
162 }
163 public CTexture( Device device, byte[] txData, Format format, bool b黒を透過する )
164 : this( device, txData, format, b黒を透過する, Pool.Managed )
165 {
166 }
167 public CTexture( Device device, Bitmap bitmap, Format format, bool b黒を透過する )
168 : this( device, bitmap, format, b黒を透過する, Pool.Managed )
169 {
170 }
171
172 /// <summary>
173 /// <para>空のテクスチャを作成する。</para>
174 /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
175 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
176 /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
177 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
178 /// カラーキーは 0x00000000(透過しない)になる。</para>
179 /// </summary>
180 /// <param name="device">Direct3D9 デバイス。</param>
181 /// <param name="n幅">テクスチャの幅(希望値)。</param>
182 /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
183 /// <param name="format">テクスチャのフォーマット。</param>
184 /// <param name="pool">テクスチャの管理方法。</param>
185 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
186 public CTexture( Device device, int n, int n高さ, Format format, Pool pool )
187 : this( device, n, n高さ, format, pool, Usage.None )
188 {
189 }
190
191 public CTexture( Device device, int n, int n高さ, Format format, Pool pool, Usage usage )
192 : this()
193 {
194 try
195 {
196 this.Format = format;
197 this.sz画像サイズ = new Size( n, n高さ );
198 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
199 this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
200
201 using ( var bitmap = new Bitmap( 1, 1 ) )
202 {
203 using ( var graphics = Graphics.FromImage( bitmap ) )
204 {
205 graphics.FillRectangle( Brushes.Black, 0, 0, 1, 1 );
206 }
207 using ( var stream = new MemoryStream() )
208 {
209 bitmap.Save( stream, ImageFormat.Bmp );
210 stream.Seek( 0L, SeekOrigin.Begin );
211 #if TEST_Direct3D9Ex
212 pool = poolvar;
213 #endif
214 // 中で更にメモリ読み込みし直していて無駄なので、Streamを使うのは止めたいところ
215 this.texture = Texture.FromStream( device, stream, n, n高さ, 1, usage, format, pool, Filter.Point, Filter.None, 0 );
216 }
217 }
218 }
219 catch
220 {
221 this.Dispose();
222 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n({0}x{1}, {2})", n, n高さ, format ) );
223 }
224 }
225
226 /// <summary>
227 /// <para>画像ファイルからテクスチャを生成する。</para>
228 /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
229 /// <para>テクスチャのサイズは、画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
230 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
231 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None になる。</para>
232 /// </summary>
233 /// <param name="device">Direct3D9 デバイス。</param>
234 /// <param name="strファイル名">画像ファイル名。</param>
235 /// <param name="format">テクスチャのフォーマット。</param>
236 /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
237 /// <param name="pool">テクスチャの管理方法。</param>
238 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
239 public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool )
240 : this()
241 {
242 MakeTexture( device, strファイル名, format, b黒を透過する, pool );
243 }
244 public void MakeTexture( Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool )
245 {
246 if ( !File.Exists( strファイル名 ) ) // #27122 2012.1.13 from: ImageInformation では FileNotFound 例外は返ってこないので、ここで自分でチェックする。わかりやすいログのために。
247 throw new FileNotFoundException( string.Format( "ファイルが存在しません。\n[{0}]", strファイル名 ) );
248
249 Byte[] _txData = File.ReadAllBytes( strファイル名 );
250 MakeTexture( device, _txData, format, b黒を透過する, pool );
251 }
252
253 public CTexture( Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool )
254 : this()
255 {
256 MakeTexture( device, txData, format, b黒を透過する, pool );
257 }
258 public void MakeTexture( Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool )
259 {
260 try
261 {
262 var information = ImageInformation.FromMemory( txData );
263 this.Format = format;
264 this.sz画像サイズ = new Size( information.Width, information.Height );
265 this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
266 int colorKey = ( b黒を透過する ) ? unchecked( (int) 0xFF000000 ) : 0;
267 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
268 #if TEST_Direct3D9Ex
269 pool = poolvar;
270 #endif
271 // lock ( lockobj )
272 // {
273 //Trace.TraceInformation( "CTexture() start: " );
274 this.texture = Texture.FromMemory( device, txData, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, colorKey );
275 //Trace.TraceInformation( "CTexture() end: " );
276 // }
277 }
278 catch
279 {
280 this.Dispose();
281 // throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) );
282 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n" ) );
283 }
284 }
285
286 public CTexture( Device device, Bitmap bitmap, Format format, bool b黒を透過する, Pool pool )
287 : this()
288 {
289 MakeTexture( device, bitmap, format, b黒を透過する, pool );
290 }
291 public void MakeTexture( Device device, Bitmap bitmap, Format format, bool b黒を透過する, Pool pool )
292 {
293 try
294 {
295 this.Format = format;
296 this.sz画像サイズ = new Size( bitmap.Width, bitmap.Height );
297 this.rc全画像 = new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height );
298 int colorKey = ( b黒を透過する ) ? unchecked( (int) 0xFF000000 ) : 0;
299 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
300 #if TEST_Direct3D9Ex
301 pool = poolvar;
302 #endif
303 //Trace.TraceInformation( "CTExture() start: " );
304 unsafe // Bitmapの内部データ(a8r8g8b8)を自前でゴリゴリコピーする
305 {
306 int tw =
307 #if TEST_Direct3D9Ex
308 288; // 32の倍数にする(グラフによっては2のべき乗にしないとダメかも)
309 #else
310 this.sz画像サイズ.Width;
311 #endif
312 #if TEST_Direct3D9Ex
313 this.texture = new Texture( device, tw, this.sz画像サイズ.Height, 1, Usage.Dynamic, format, Pool.Default );
314 #else
315 this.texture = new Texture( device, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool );
316 #endif
317 BitmapData srcBufData = bitmap.LockBits( new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height ), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb );
318 DataRectangle destDataRectangle = texture.LockRectangle( 0, LockFlags.Discard ); // None
319 #if TEST_Direct3D9Ex
320 byte[] filldata = null;
321 if ( tw > this.sz画像サイズ.Width )
322 {
323 filldata = new byte[ (tw - this.sz画像サイズ.Width) * 4 ];
324 }
325 for ( int y = 0; y < this.sz画像サイズ.Height; y++ )
326 {
327 IntPtr src_scan0 = (IntPtr) ( (Int64) srcBufData.Scan0 + y * srcBufData.Stride );
328 destDataRectangle.Data.WriteRange( src_scan0, this.sz画像サイズ.Width * 4 );
329 if ( tw > this.sz画像サイズ.Width )
330 {
331 destDataRectangle.Data.WriteRange( filldata );
332 }
333 }
334 #else
335 IntPtr src_scan0 = (IntPtr) ( (Int64) srcBufData.Scan0 );
336 destDataRectangle.Data.WriteRange( src_scan0, this.sz画像サイズ.Width * 4 * this.sz画像サイズ.Height );
337 #endif
338 texture.UnlockRectangle( 0 );
339 bitmap.UnlockBits( srcBufData );
340 }
341 //Trace.TraceInformation( "CTExture() End: " );
342 }
343 catch
344 {
345 this.Dispose();
346 // throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) );
347 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n" ) );
348 }
349 }
350 // メソッド
351
352 /// <summary>
353 /// テクスチャを 2D 画像と見なして描画する。
354 /// </summary>
355 /// <param name="device">Direct3D9 デバイス。</param>
356 /// <param name="x">描画位置(テクスチャの左上位置の X 座標[dot])。</param>
357 /// <param name="y">描画位置(テクスチャの左上位置の Y 座標[dot])。</param>
358 public void t2D描画( Device device, int x, int y )
359 {
360 this.t2D描画( device, x, y, 1f, this.rc全画像 );
361 }
362 public void t2D描画( Device device, int x, int y, Rectangle rc画像内の描画領域 )
363 {
364 this.t2D描画( device, x, y, 1f, rc画像内の描画領域 );
365 }
366 public void t2D描画( Device device, float x, float y )
367 {
368 this.t2D描画( device, (int)x, (int)y, 1f, this.rc全画像 );
369 }
370 public void t2D描画( Device device, float x, float y, Rectangle rc画像内の描画領域 )
371 {
372 this.t2D描画( device, (int)x, (int)y, 1f, rc画像内の描画領域 );
373 }
374 public void t2D描画( Device device, int x, int y, float depth, Rectangle rc画像内の描画領域 )
375 {
376 if (this.texture == null)
377 return;
378
379 this.tレンダリングステトの設定( device );
380
381 if( this.fZ軸中心回転 == 0f )
382 {
383 #region [ (A) 回転なし ]
384 //-----------------
385 float f補正値X = -0.5f; // -0.5 は座標とピクセルの誤差を吸収するための座標補正値。(MSDN参照)
386 float f補正値Y = -0.5f; //
387 float w = rc画像内の描画領域.Width;
388 float h = rc画像内の描画領域.Height;
389 float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
390 float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
391 float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
392 float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
393 this.color4.Alpha = ( (float) this._透明度 ) / 255f;
394 int color = this.color4.ToArgb();
395
396 if( this.cvTransformedColoredVertexies == null )
397 this.cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[ 4 ];
398
399 // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
400
401 this.cvTransformedColoredVertexies[ 0 ].Position.X = x + f補正値X;
402 this.cvTransformedColoredVertexies[ 0 ].Position.Y = y + f補正値Y;
403 this.cvTransformedColoredVertexies[ 0 ].Position.Z = depth;
404 this.cvTransformedColoredVertexies[ 0 ].Position.W = 1.0f;
405 this.cvTransformedColoredVertexies[ 0 ].Color = color;
406 this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.X = fU;
407 this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
408
409 this.cvTransformedColoredVertexies[ 1 ].Position.X = ( x + ( w * this.vc拡大縮小倍率.X ) ) + f補正値X;
410 this.cvTransformedColoredVertexies[ 1 ].Position.Y = y + f補正値Y;
411 this.cvTransformedColoredVertexies[ 1 ].Position.Z = depth;
412 this.cvTransformedColoredVertexies[ 1 ].Position.W = 1.0f;
413 this.cvTransformedColoredVertexies[ 1 ].Color = color;
414 this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.X = fU;
415 this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
416
417 this.cvTransformedColoredVertexies[ 2 ].Position.X = x + f補正値X;
418 this.cvTransformedColoredVertexies[ 2 ].Position.Y = ( y + ( h * this.vc拡大縮小倍率.Y ) ) + f補正値Y;
419 this.cvTransformedColoredVertexies[ 2 ].Position.Z = depth;
420 this.cvTransformedColoredVertexies[ 2 ].Position.W = 1.0f;
421 this.cvTransformedColoredVertexies[ 2 ].Color = color;
422 this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.X = fU;
423 this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
424
425 this.cvTransformedColoredVertexies[ 3 ].Position.X = ( x + ( w * this.vc拡大縮小倍率.X ) ) + f補正値X;
426 this.cvTransformedColoredVertexies[ 3 ].Position.Y = ( y + ( h * this.vc拡大縮小倍率.Y ) ) + f補正値Y;
427 this.cvTransformedColoredVertexies[ 3 ].Position.Z = depth;
428 this.cvTransformedColoredVertexies[ 3 ].Position.W = 1.0f;
429 this.cvTransformedColoredVertexies[ 3 ].Color = color;
430 this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.X = fU;
431 this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
432
433 device.SetTexture( 0, this.texture );
434 device.VertexFormat = TransformedColoredTexturedVertex.Format;
435 device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 0, 2, this.cvTransformedColoredVertexies );
436 //-----------------
437 #endregion
438 }
439 else
440 {
441 #region [ (B) 回転あり ]
442 //-----------------
443 float f補正値X = ( ( rc画像内の描画領域.Width % 2 ) == 0 ) ? -0.5f : 0f; // -0.5 は座標とピクセルの誤差を吸収するための座標補正値。(MSDN参照)
444 float f補正値Y = ( ( rc画像内の描画領域.Height % 2 ) == 0 ) ? -0.5f : 0f; // 3D(回転する)なら補正はいらない。
445 float f中央X = ( (float) rc画像内の描画領域.Width ) / 2f;
446 float f中央Y = ( (float) rc画像内の描画領域.Height ) / 2f;
447 float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
448 float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
449 float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
450 float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
451 this.color4.Alpha = ( (float) this._透明度 ) / 255f;
452 int color = this.color4.ToArgb();
453
454 if( this.cvPositionColoredVertexies == null )
455 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
456
457 // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
458
459 this.cvPositionColoredVertexies[ 0 ].Position.X = -f中央X + f補正値X;
460 this.cvPositionColoredVertexies[ 0 ].Position.Y = f中央Y + f補正値Y;
461 this.cvPositionColoredVertexies[ 0 ].Position.Z = depth;
462 this.cvPositionColoredVertexies[ 0 ].Color = color;
463 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = fU;
464 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
465
466 this.cvPositionColoredVertexies[ 1 ].Position.X = f中央X + f補正値X;
467 this.cvPositionColoredVertexies[ 1 ].Position.Y = f中央Y + f補正値Y;
468 this.cvPositionColoredVertexies[ 1 ].Position.Z = depth;
469 this.cvPositionColoredVertexies[ 1 ].Color = color;
470 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = fU;
471 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
472
473 this.cvPositionColoredVertexies[ 2 ].Position.X = -f中央X + f補正値X;
474 this.cvPositionColoredVertexies[ 2 ].Position.Y = -f中央Y + f補正値Y;
475 this.cvPositionColoredVertexies[ 2 ].Position.Z = depth;
476 this.cvPositionColoredVertexies[ 2 ].Color = color;
477 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = fU;
478 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
479
480 this.cvPositionColoredVertexies[ 3 ].Position.X = f中央X + f補正値X;
481 this.cvPositionColoredVertexies[ 3 ].Position.Y = -f中央Y + f補正値Y;
482 this.cvPositionColoredVertexies[ 3 ].Position.Z = depth;
483 this.cvPositionColoredVertexies[ 3 ].Color = color;
484 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = fU;
485 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
486
487 int n描画領域内X = x + ( rc画像内の描画領域.Width / 2 );
488 int n描画領域内Y = y + ( rc画像内の描画領域.Height / 2 );
489 var vc3移動量 = new Vector3( n描画領域内X - ( ( (float) device.Viewport.Width ) / 2f ), -( n描画領域内Y - ( ( (float) device.Viewport.Height ) / 2f ) ), 0f );
490
491 var matrix = Matrix.Identity * Matrix.Scaling( this.vc拡大縮小倍率 );
492 matrix *= Matrix.RotationZ( this.fZ軸中心回転 );
493 matrix *= Matrix.Translation( vc3移動量 );
494 device.SetTransform( TransformState.World, matrix );
495
496 device.SetTexture( 0, this.texture );
497 device.VertexFormat = PositionColoredTexturedVertex.Format;
498 device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies );
499 //-----------------
500 #endregion
501 }
502 }
503 public void t2D上下反転描画( Device device, int x, int y )
504 {
505 this.t2D上下反転描画( device, x, y, 1f, this.rc全画像 );
506 }
507 public void t2D上下反転描画( Device device, int x, int y, Rectangle rc画像内の描画領域 )
508 {
509 this.t2D上下反転描画( device, x, y, 1f, rc画像内の描画領域 );
510 }
511 public void t2D上下反転描画( Device device, int x, int y, float depth, Rectangle rc画像内の描画領域 )
512 {
513 if( this.texture == null )
514 throw new InvalidOperationException( "テクスチャは生成されていません。" );
515
516 this.tレンダリングステトの設定( device );
517
518 float fx = x * CTexture.f画面比率 + CTexture.rc物理画面描画領域.X - 0.5f; // -0.5 は座標とピクセルの誤差を吸収するための座標補正値。(MSDN参照)
519 float fy = y * CTexture.f画面比率 + CTexture.rc物理画面描画領域.Y - 0.5f; //
520 float w = rc画像内の描画領域.Width * this.vc拡大縮小倍率.X * CTexture.f画面比率;
521 float h = rc画像内の描画領域.Height * this.vc拡大縮小倍率.Y * CTexture.f画面比率;
522 float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
523 float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
524 float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
525 float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
526 this.color4.Alpha = ( (float) this._透明度 ) / 255f;
527 int color = this.color4.ToArgb();
528
529 if( this.cvTransformedColoredVertexies == null )
530 this.cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[ 4 ];
531
532 // 以下、マネージドオブジェクトの量産を抑えるため new は使わない。
533
534 this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.X = fU; // 左上 → 左下
535 this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
536 this.cvTransformedColoredVertexies[ 0 ].Position.X = fx;
537 this.cvTransformedColoredVertexies[ 0 ].Position.Y = fy;
538 this.cvTransformedColoredVertexies[ 0 ].Position.Z = depth;
539 this.cvTransformedColoredVertexies[ 0 ].Position.W = 1.0f;
540 this.cvTransformedColoredVertexies[ 0 ].Color = color;
541
542 this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.X = fU; // 右上 → 右下
543 this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
544 this.cvTransformedColoredVertexies[ 1 ].Position.X = fx + w;
545 this.cvTransformedColoredVertexies[ 1 ].Position.Y = fy;
546 this.cvTransformedColoredVertexies[ 1 ].Position.Z = depth;
547 this.cvTransformedColoredVertexies[ 1 ].Position.W = 1.0f;
548 this.cvTransformedColoredVertexies[ 1 ].Color = color;
549
550 this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.X = fU; // 左下 → 左上
551 this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
552 this.cvTransformedColoredVertexies[ 2 ].Position.X = fx;
553 this.cvTransformedColoredVertexies[ 2 ].Position.Y = fy + h;
554 this.cvTransformedColoredVertexies[ 2 ].Position.Z = depth;
555 this.cvTransformedColoredVertexies[ 2 ].Position.W = 1.0f;
556 this.cvTransformedColoredVertexies[ 2 ].Color = color;
557
558 this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.X = fU; // 右下 → 右上
559 this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
560 this.cvTransformedColoredVertexies[ 3 ].Position.X = fx + w;
561 this.cvTransformedColoredVertexies[ 3 ].Position.Y = fy + h;
562 this.cvTransformedColoredVertexies[ 3 ].Position.Z = depth;
563 this.cvTransformedColoredVertexies[ 3 ].Position.W = 1.0f;
564 this.cvTransformedColoredVertexies[ 3 ].Color = color;
565
566 device.SetTexture( 0, this.texture );
567 device.VertexFormat = TransformedColoredTexturedVertex.Format;
568 device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvTransformedColoredVertexies );
569 }
570 public void t2D上下反転描画( Device device, Point pt )
571 {
572 this.t2D上下反転描画( device, pt.X, pt.Y, 1f, this.rc全画像 );
573 }
574 public void t2D上下反転描画( Device device, Point pt, Rectangle rc画像内の描画領域 )
575 {
576 this.t2D上下反転描画( device, pt.X, pt.Y, 1f, rc画像内の描画領域 );
577 }
578 public void t2D上下反転描画( Device device, Point pt, float depth, Rectangle rc画像内の描画領域 )
579 {
580 this.t2D上下反転描画( device, pt.X, pt.Y, depth, rc画像内の描画領域 );
581 }
582
583 public static Vector3 t論理画面座標をワルド座標へ変換する(int x, int y)
584 {
585 return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3((float)x, (float)y, 0f));
586 }
587 public static Vector3 t論理画面座標をワルド座標へ変換する(float x, float y)
588 {
589 return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3(x, y, 0f));
590 }
591 public static Vector3 t論理画面座標をワルド座標へ変換する(Point pt論理画面座標)
592 {
593 return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3(pt論理画面座標.X, pt論理画面座標.Y, 0.0f));
594 }
595 public static Vector3 t論理画面座標をワルド座標へ変換する(Vector2 v2論理画面座標)
596 {
597 return CTexture.t論理画面座標をワルド座標へ変換する(new Vector3(v2論理画面座標, 0f));
598 }
599 public static Vector3 t論理画面座標をワルド座標へ変換する(Vector3 v3論理画面座標)
600 {
601 return new Vector3(
602 (v3論理画面座標.X - (CTexture.sz論理画面.Width / 2.0f)) * CTexture.f画面比率,
603 (-(v3論理画面座標.Y - (CTexture.sz論理画面.Height / 2.0f)) * CTexture.f画面比率),
604 v3論理画面座標.Z);
605 }
606
607 /// <summary>
608 /// テクスチャを 3D 画像と見なして描画する。
609 /// </summary>
610 public void t3D描画( Device device, Matrix mat )
611 {
612 this.t3D描画( device, mat, this.rc全画像 );
613 }
614 public void t3D描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 )
615 {
616 if( this.texture == null )
617 return;
618
619 float x = ( (float) rc画像内の描画領域.Width ) / 2f;
620 float y = ( (float) rc画像内の描画領域.Height ) / 2f;
621 float z = 0.0f;
622 float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
623 float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
624 float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
625 float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
626 this.color4.Alpha = ( (float) this._透明度 ) / 255f;
627 int color = this.color4.ToArgb();
628
629 if( this.cvPositionColoredVertexies == null )
630 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
631
632 // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
633
634 this.cvPositionColoredVertexies[ 0 ].Position.X = -x;
635 this.cvPositionColoredVertexies[ 0 ].Position.Y = y;
636 this.cvPositionColoredVertexies[ 0 ].Position.Z = z;
637 this.cvPositionColoredVertexies[ 0 ].Color = color;
638 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = fU;
639 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
640
641 this.cvPositionColoredVertexies[ 1 ].Position.X = x;
642 this.cvPositionColoredVertexies[ 1 ].Position.Y = y;
643 this.cvPositionColoredVertexies[ 1 ].Position.Z = z;
644 this.cvPositionColoredVertexies[ 1 ].Color = color;
645 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = fU;
646 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
647
648 this.cvPositionColoredVertexies[ 2 ].Position.X = -x;
649 this.cvPositionColoredVertexies[ 2 ].Position.Y = -y;
650 this.cvPositionColoredVertexies[ 2 ].Position.Z = z;
651 this.cvPositionColoredVertexies[ 2 ].Color = color;
652 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = fU;
653 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
654
655 this.cvPositionColoredVertexies[ 3 ].Position.X = x;
656 this.cvPositionColoredVertexies[ 3 ].Position.Y = -y;
657 this.cvPositionColoredVertexies[ 3 ].Position.Z = z;
658 this.cvPositionColoredVertexies[ 3 ].Color = color;
659 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = fU;
660 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
661
662 this.tレンダリングステトの設定( device );
663
664 device.SetTransform( TransformState.World, mat );
665 device.SetTexture( 0, this.texture );
666 device.VertexFormat = PositionColoredTexturedVertex.Format;
667 device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies );
668 }
669
670 public void t3D左上基準描画( Device device, Matrix mat )
671 {
672 this.t3D左上基準描画( device, mat, this.rc全画像 );
673 }
674 /// <summary>
675 /// ○覚書
676 /// SlimDX.Matrix mat = SlimDX.Matrix.Identity;
677 /// mat *= SlimDX.Matrix.Translation( x, y, z );
678 /// 「mat =」ではなく「mat *=」であることを忘れないこと。
679 /// </summary>
680 public void t3D左上基準描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 )
681 {
682 //とりあえず補正値などは無し。にしても使う機会少なさそうだなー・・・・
683 if( this.texture == null )
684 return;
685
686 float x = 0.0f;
687 float y = 0.0f;
688 float z = 0.0f;
689 float fU = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
690 float fU = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
691 float fV = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
692 float fV = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
693 this.color4.Alpha = ( (float) this._透明度 ) / 255f;
694 int color = this.color4.ToArgb();
695
696 if( this.cvPositionColoredVertexies == null )
697 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
698
699 // #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。
700
701 this.cvPositionColoredVertexies[ 0 ].Position.X = -x;
702 this.cvPositionColoredVertexies[ 0 ].Position.Y = y;
703 this.cvPositionColoredVertexies[ 0 ].Position.Z = z;
704 this.cvPositionColoredVertexies[ 0 ].Color = color;
705 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = fU;
706 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = fV;
707
708 this.cvPositionColoredVertexies[ 1 ].Position.X = x;
709 this.cvPositionColoredVertexies[ 1 ].Position.Y = y;
710 this.cvPositionColoredVertexies[ 1 ].Position.Z = z;
711 this.cvPositionColoredVertexies[ 1 ].Color = color;
712 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = fU;
713 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = fV;
714
715 this.cvPositionColoredVertexies[ 2 ].Position.X = -x;
716 this.cvPositionColoredVertexies[ 2 ].Position.Y = -y;
717 this.cvPositionColoredVertexies[ 2 ].Position.Z = z;
718 this.cvPositionColoredVertexies[ 2 ].Color = color;
719 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = fU;
720 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = fV;
721
722 this.cvPositionColoredVertexies[ 3 ].Position.X = x;
723 this.cvPositionColoredVertexies[ 3 ].Position.Y = -y;
724 this.cvPositionColoredVertexies[ 3 ].Position.Z = z;
725 this.cvPositionColoredVertexies[ 3 ].Color = color;
726 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = fU;
727 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = fV;
728
729 this.tレンダリングステトの設定( device );
730
731 device.SetTransform( TransformState.World, mat );
732 device.SetTexture( 0, this.texture );
733 device.VertexFormat = PositionColoredTexturedVertex.Format;
734 device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies );
735 }
736
737 #region [ IDisposable 実装 ]
738 //-----------------
739 public void Dispose()
740 {
741 if( !this.bDispose完了済み )
742 {
743 // テクスチャの破棄
744 if( this.texture != null )
745 {
746 this.texture.Dispose();
747 this.texture = null;
748 }
749
750 this.bDispose完了済み = true;
751 }
752 }
753 //-----------------
754 #endregion
755
756
757 // その他
758
759 #region [ private ]
760 //-----------------
761 private int _透明度;
762 private bool bDispose完了済み;
763 private PositionColoredTexturedVertex[] cvPositionColoredVertexies;
764 protected TransformedColoredTexturedVertex[] cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[]
765 {
766 new TransformedColoredTexturedVertex(),
767 new TransformedColoredTexturedVertex(),
768 new TransformedColoredTexturedVertex(),
769 new TransformedColoredTexturedVertex(),
770 };
771 private const Pool poolvar = // 2011.4.25 yyagi
772 #if TEST_Direct3D9Ex
773 Pool.Default;
774 #else
775 Pool.Managed;
776 #endif
777 // byte[] _txData;
778 static object lockobj = new object();
779
780 private void tレンダリングステトの設定( Device device )
781 {
782 if( this.b加算合成 )
783 {
784 device.SetRenderState( RenderState.AlphaBlendEnable, true );
785 device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5
786 device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.One ); // 2
787 }
788 else
789 {
790 device.SetRenderState( RenderState.AlphaBlendEnable, true );
791 device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5
792 device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.InverseSourceAlpha ); // 6
793 }
794 }
795 private Size t指定されたサイズを超えない最適なテクスチャサイズを返す( Device device, Size sz指定サイズ )
796 {
797 bool b条件付きでサイズは2の累乗でなくてもOK = ( device.Capabilities.TextureCaps & TextureCaps.NonPow2Conditional ) != 0;
798 bool bサイズは2の累乗でなければならない = ( device.Capabilities.TextureCaps & TextureCaps.Pow2 ) != 0;
799 bool b正方形でなければならない = ( device.Capabilities.TextureCaps & TextureCaps.SquareOnly ) != 0;
800 int n最大幅 = device.Capabilities.MaxTextureWidth;
801 int n最大高 = device.Capabilities.MaxTextureHeight;
802 var szサイズ = new Size( sz指定サイズ.Width, sz指定サイズ.Height );
803
804 if( bサイズは2の累乗でなければならない && !b条件付きでサイズは2の累乗でなくてもOK )
805 {
806 // 幅を2の累乗にする
807 int n = 1;
808 do
809 {
810 n *= 2;
811 }
812 while( n <= sz指定サイズ.Width );
813 sz指定サイズ.Width = n;
814
815 // 高さを2の累乗にする
816 n = 1;
817 do
818 {
819 n *= 2;
820 }
821 while( n <= sz指定サイズ.Height );
822 sz指定サイズ.Height = n;
823 }
824
825 if( sz指定サイズ.Width > n最大幅 )
826 sz指定サイズ.Width = n最大幅;
827
828 if( sz指定サイズ.Height > n最大高 )
829 sz指定サイズ.Height = n最大高;
830
831 if( b正方形でなければならない )
832 {
833 if( szサイズ.Width > szサイズ.Height )
834 {
835 szサイズ.Height = szサイズ.Width;
836 }
837 else if( szサイズ.Width < szサイズ.Height )
838 {
839 szサイズ.Width = szサイズ.Height;
840 }
841 }
842
843 return szサイズ;
844 }
845
846
847 // 2012.3.21 さらなる new の省略作戦
848
849 protected Rectangle rc全画像; // テクスチャ作ったらあとは不変
850 protected Color4 color4 = new Color4( 1f, 1f, 1f, 1f ); // アルファ以外は不変
851 //-----------------
852 #endregion
853 }
854 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26