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 66 - (show annotations) (download)
Thu Apr 11 12:53:14 2013 UTC (10 years, 11 months ago) by kairera0467
File size: 37512 byte(s)
#31111 DirectShowでの動画再生の実装。ただし上下反転で描画されるのをアプリ側で補正する処理に対応していないので注意。
      実装した箇所はStageClear画面。今回の変更で従来の描画が正常に使えるかは未検証。
#xxxxx RISKY1、ゲージ0時に背景が暗くなるようにした。それに伴い、ゲージマスク、煽り画像もゲージ0で消えるようにした。

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

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