2 using System.Collections.Generic;
\r
4 using System.Drawing;
\r
5 using System.Drawing.Imaging;
\r
8 using SlimDX.Direct3D9;
\r
12 public class CTexture : IDisposable
\r
21 public float fZ軸中心回転
\r
38 else if( value > 0xff )
\r
48 public Size szテクスチャサイズ
\r
58 public Texture texture
\r
63 public Vector3 vc拡大縮小倍率;
\r
70 this.sz画像サイズ = new Size( 0, 0 );
\r
71 this.szテクスチャサイズ = new Size( 0, 0 );
\r
73 this.texture = null;
\r
74 this.vbPositionColoredVertexBuffer = null;
\r
75 this.cvPositionColoredVertexies = null;
\r
78 this.vc拡大縮小倍率 = new Vector3( 1f, 1f, 1f );
\r
82 /// <para>指定されたビットマップオブジェクトから Managed テクスチャを作成する。</para>
\r
83 /// <para>テクスチャのサイズは、BITMAP画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
84 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
85 /// <para>その他、ミップマップ数は 1、Usage は None、Pool は Managed、イメージフィルタは Point、ミップマップフィルタは
\r
86 /// None、カラーキーは 0xFFFFFFFF(完全なる黒を透過)になる。</para>
\r
88 /// <param name="device">Direct3D9 デバイス。</param>
\r
89 /// <param name="bitmap">作成元のビットマップ。</param>
\r
90 /// <param name="format">テクスチャのフォーマット。</param>
\r
91 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
92 public CTexture( Device device, Bitmap bitmap, Format format )
\r
97 this.sz画像サイズ = new Size( bitmap.Width, bitmap.Height );
\r
98 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
\r
99 using( MemoryStream stream = new MemoryStream() )
\r
101 bitmap.Save( stream, ImageFormat.Bmp );
\r
102 stream.Seek( 0L, SeekOrigin.Begin );
\r
105 int colorKey = (int) 0xFF000000; // -16777216;
\r
106 this.texture = Texture.FromStream( device, stream, this.szテクスチャサイズ.Width, this.szテクスチャサイズ.Height, 1, Usage.None, format, poolvar, Filter.Point, Filter.None, colorKey );
\r
109 this.t頂点バッファの作成( device );
\r
114 throw new CTextureCreateFailedException( "ビットマップからのテクスチャの生成に失敗しました。" );
\r
119 /// <para>空の Managed テクスチャを作成する。</para>
\r
120 /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
121 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
122 /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
\r
123 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
\r
124 /// カラーキーは 0x00000000(透過しない)になる。</para>
\r
126 /// <param name="device">Direct3D9 デバイス。</param>
\r
127 /// <param name="n幅">テクスチャの幅(希望値)。</param>
\r
128 /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
\r
129 /// <param name="format">テクスチャのフォーマット。</param>
\r
130 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
131 public CTexture( Device device, int n幅, int n高さ, Format format )
\r
132 : this( device, n幅, n高さ, format, Pool.Managed )
\r
137 /// <para>指定された画像ファイルから Managed テクスチャを作成する。</para>
\r
138 /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
\r
140 /// <param name="device">Direct3D9 デバイス。</param>
\r
141 /// <param name="strファイル名">画像ファイル名。</param>
\r
142 /// <param name="format">テクスチャのフォーマット。</param>
\r
143 /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
\r
144 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
145 public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する )
\r
146 : this( device, strファイル名, format, b黒を透過する, Pool.Managed )
\r
151 /// <para>空のテクスチャを作成する。</para>
\r
152 /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
153 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
154 /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
\r
155 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
\r
156 /// カラーキーは 0x00000000(透過しない)になる。</para>
\r
158 /// <param name="device">Direct3D9 デバイス。</param>
\r
159 /// <param name="n幅">テクスチャの幅(希望値)。</param>
\r
160 /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
\r
161 /// <param name="format">テクスチャのフォーマット。</param>
\r
162 /// <param name="pool">テクスチャの管理方法。</param>
\r
163 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
164 public CTexture( Device device, int n幅, int n高さ, Format format, Pool pool )
\r
165 : this( device, n幅, n高さ, format, pool, Usage.None )
\r
168 public CTexture( Device device, int n幅, int n高さ, Format format, Pool pool, Usage usage )
\r
173 this.sz画像サイズ = new Size( n幅, n高さ );
\r
174 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
\r
175 using ( Bitmap bitmap = new Bitmap( 1, 1 ) )
\r
177 using ( Graphics graphics = Graphics.FromImage( bitmap ) )
\r
179 graphics.FillRectangle( Brushes.Black, 0, 0, 1, 1 );
\r
181 using ( MemoryStream stream = new MemoryStream() )
\r
183 bitmap.Save( stream, ImageFormat.Bmp );
\r
184 stream.Seek( 0L, SeekOrigin.Begin );
\r
185 #if TEST_Direct3D9Ex
\r
188 // 中で更にメモリ読み込みし直していて無駄なので、Streamを使うのは止めたいところ
\r
189 this.texture = Texture.FromStream( device, stream, n幅, n高さ, 1, usage, format, pool, Filter.Point, Filter.None, 0 );
\r
192 this.t頂点バッファの作成( device );
\r
197 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n({0}x{1}, {2})", n幅, n高さ, format ) );
\r
202 /// <para>画像ファイルからテクスチャを生成する。</para>
\r
203 /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
\r
204 /// <para>テクスチャのサイズは、画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
205 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
206 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None になる。</para>
\r
208 /// <param name="device">Direct3D9 デバイス。</param>
\r
209 /// <param name="strファイル名">画像ファイル名。</param>
\r
210 /// <param name="format">テクスチャのフォーマット。</param>
\r
211 /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
\r
212 /// <param name="pool">テクスチャの管理方法。</param>
\r
213 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
214 public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool )
\r
219 ImageInformation information = ImageInformation.FromFile( strファイル名 );
\r
220 this.sz画像サイズ = new Size( information.Width, information.Height );
\r
221 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
\r
222 #if TEST_Direct3D9Ex
\r
225 this.texture = Texture.FromFile( device, strファイル名, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, b黒を透過する ? -16777216 : 0 );
\r
226 this.t頂点バッファの作成( device );
\r
231 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) );
\r
239 /// テクスチャを 2D 画像と見なして描画する。
\r
241 /// <param name="device">Direct3D9 デバイス。</param>
\r
242 /// <param name="x">描画位置(テクスチャの左上位置の X 座標[dot])。</param>
\r
243 /// <param name="y">描画位置(テクスチャの左上位置の Y 座標[dot])。</param>
\r
244 public void t2D描画( Device device, int x, int y )
\r
246 this.t2D描画( device, x, y, new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height ) );
\r
248 public void t2D描画( Device device, int x, int y, Rectangle rc画像内の描画領域 )
\r
250 this.t2D描画( device, x, y, 1f, rc画像内の描画領域 );
\r
252 public void t2D描画( Device device, int x, int y, float depth, Rectangle rc画像内の描画領域 )
\r
254 if( this.texture != null )
\r
256 this.tレンダリングステートの設定( device );
\r
257 if( this.fZ軸中心回転 == 0f )
\r
260 float num2 = -0.5f;
\r
261 float width = rc画像内の描画領域.Width;
\r
262 float height = rc画像内の描画領域.Height;
\r
263 float num5 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
\r
264 float num6 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
\r
265 float num7 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
\r
266 float num8 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
\r
267 int color4 = new Color4( ( (float) this._透明度 ) / 255f, 1f, 1f, 1f ).ToArgb();
\r
268 if( this.cvTransformedColoredVertexies == null )
\r
270 this.cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[ 4 ];
\r
272 this.cvTransformedColoredVertexies[ 0 ].Position = new Vector4( x + num, y + num2, depth, 1f );
\r
273 this.cvTransformedColoredVertexies[ 0 ].Color = color4;
\r
274 this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates = new Vector2( num5, num7 );
\r
275 this.cvTransformedColoredVertexies[ 1 ].Position = new Vector4( ( x + ( width * this.vc拡大縮小倍率.X ) ) + num, y + num2, depth, 1f );
\r
276 this.cvTransformedColoredVertexies[ 1 ].Color = color4;
\r
277 this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates = new Vector2( num6, num7 );
\r
278 this.cvTransformedColoredVertexies[ 2 ].Position = new Vector4( x + num, ( y + ( height * this.vc拡大縮小倍率.Y ) ) + num2, depth, 1f );
\r
279 this.cvTransformedColoredVertexies[ 2 ].Color = color4;
\r
280 this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates = new Vector2( num5, num8 );
\r
281 this.cvTransformedColoredVertexies[ 3 ].Position = new Vector4( ( x + ( width * this.vc拡大縮小倍率.X ) ) + num, ( y + ( height * this.vc拡大縮小倍率.Y ) ) + num2, depth, 1f );
\r
282 this.cvTransformedColoredVertexies[ 3 ].Color = color4;
\r
283 this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates = new Vector2( num6, num8 );
\r
284 device.SetTexture( 0, this.texture );
\r
285 device.VertexFormat = TransformedColoredTexturedVertex.Format;
\r
286 device.DrawUserPrimitives<TransformedColoredTexturedVertex>( PrimitiveType.TriangleStrip, 0, 2, this.cvTransformedColoredVertexies );
\r
290 float num10 = ( ( rc画像内の描画領域.Width % 2 ) == 0 ) ? -0.5f : 0f;
\r
291 float num11 = ( ( rc画像内の描画領域.Height % 2 ) == 0 ) ? -0.5f : 0f;
\r
292 float num12 = ( (float) rc画像内の描画領域.Width ) / 2f;
\r
293 float num13 = ( (float) rc画像内の描画領域.Height ) / 2f;
\r
294 int num1 = rc画像内の描画領域.Width;
\r
295 int num21 = rc画像内の描画領域.Height;
\r
296 float num14 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
\r
297 float num15 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
\r
298 float num16 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
\r
299 float num17 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
\r
300 int color4 = new Color4( ( (float) this._透明度 ) / 255f, 1f, 1f, 1f ).ToArgb();
\r
301 if( this.cvPositionColoredVertexies == null )
\r
303 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
\r
305 this.cvPositionColoredVertexies[ 0 ].Position = new Vector3( -num12 + num10, num13 + num11, depth );
\r
306 this.cvPositionColoredVertexies[ 0 ].Color = color4;
\r
307 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates = new Vector2( num14, num16 );
\r
308 this.cvPositionColoredVertexies[ 1 ].Position = new Vector3( num12 + num10, num13 + num11, depth );
\r
309 this.cvPositionColoredVertexies[ 1 ].Color = color4;
\r
310 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates = new Vector2( num15, num16 );
\r
311 this.cvPositionColoredVertexies[ 2 ].Position = new Vector3( -num12 + num10, -num13 + num11, depth );
\r
312 this.cvPositionColoredVertexies[ 2 ].Color = color4;
\r
313 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates = new Vector2( num14, num17 );
\r
314 this.cvPositionColoredVertexies[ 3 ].Position = new Vector3( num12 + num10, -num13 + num11, depth );
\r
315 this.cvPositionColoredVertexies[ 3 ].Color = color4;
\r
316 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates = new Vector2( num15, num17 );
\r
317 using( DataStream stream = this.vbPositionColoredVertexBuffer.Lock( 0, 0, LockFlags.NoOverwrite ) ) // LockFlags.None
\r
319 stream.WriteRange<PositionColoredTexturedVertex>( this.cvPositionColoredVertexies );
\r
320 this.vbPositionColoredVertexBuffer.Unlock();
\r
322 int num19 = x + ( rc画像内の描画領域.Width / 2 );
\r
323 int num20 = y + ( rc画像内の描画領域.Height / 2 );
\r
324 Vector3 amount = new Vector3( num19 - ( ( (float) device.Viewport.Width ) / 2f ), -( num20 - ( ( (float) device.Viewport.Height ) / 2f ) ), 0f );
\r
325 Matrix matrix = Matrix.Identity * Matrix.Scaling( this.vc拡大縮小倍率 );
\r
326 matrix *= Matrix.RotationZ( this.fZ軸中心回転 );
\r
327 matrix *= Matrix.Translation( amount );
\r
328 device.SetTransform( TransformState.World, matrix );
\r
329 device.SetTexture( 0, this.texture );
\r
330 device.SetStreamSource( 0, this.vbPositionColoredVertexBuffer, 0, PositionColoredTexturedVertex.SizeInBytes );
\r
331 device.VertexFormat = PositionColoredTexturedVertex.Format;
\r
332 device.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 );
\r
338 /// テクスチャを 3D 画像と見なして描画する。
\r
340 public void t3D描画( Device device, Matrix mat )
\r
342 this.t3D描画( device, mat, new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height ) );
\r
344 public void t3D描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 )
\r
346 if( this.texture != null )
\r
348 float x = ( (float) rc画像内の描画領域.Width ) / 2f;
\r
349 float y = ( (float) rc画像内の描画領域.Height ) / 2f;
\r
350 int width = rc画像内の描画領域.Width;
\r
351 int height = rc画像内の描画領域.Height;
\r
352 float num3 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
\r
353 float num4 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
\r
354 float num5 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
\r
355 float num6 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
\r
356 int color4 = new Color4( ( (float) this._透明度 ) / 255f, 1f, 1f, 1f ).ToArgb();
\r
357 if( this.cvPositionColoredVertexies == null )
\r
359 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
\r
362 this.cvPositionColoredVertexies[ 0 ].Position = new Vector3( -x, y, z );
\r
363 this.cvPositionColoredVertexies[ 0 ].Color = color4;
\r
364 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates = new Vector2( num3, num5 );
\r
365 this.cvPositionColoredVertexies[ 1 ].Position = new Vector3( x, y, z );
\r
366 this.cvPositionColoredVertexies[ 1 ].Color = color4;
\r
367 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates = new Vector2( num4, num5 );
\r
368 this.cvPositionColoredVertexies[ 2 ].Position = new Vector3( -x, -y, z );
\r
369 this.cvPositionColoredVertexies[ 2 ].Color = color4;
\r
370 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates = new Vector2( num3, num6 );
\r
371 this.cvPositionColoredVertexies[ 3 ].Position = new Vector3( x, -y, z );
\r
372 this.cvPositionColoredVertexies[ 3 ].Color = color4;
\r
373 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates = new Vector2( num4, num6 );
\r
374 using( DataStream stream = this.vbPositionColoredVertexBuffer.Lock( 0, 0, LockFlags.NoOverwrite ) ) // LockFlags.None
\r
376 stream.WriteRange<PositionColoredTexturedVertex>( this.cvPositionColoredVertexies );
\r
377 this.vbPositionColoredVertexBuffer.Unlock();
\r
379 this.tレンダリングステートの設定( device );
\r
380 device.SetTransform( TransformState.World, mat );
\r
381 device.SetTexture( 0, this.texture );
\r
382 device.SetStreamSource( 0, this.vbPositionColoredVertexBuffer, 0, PositionColoredTexturedVertex.SizeInBytes );
\r
383 device.VertexFormat = PositionColoredTexturedVertex.Format;
\r
384 device.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 );
\r
388 #region [ IDosposable 実装 ]
\r
389 //-----------------
\r
390 public void Dispose()
\r
392 if( !this.bDispose完了済み )
\r
395 if( this.texture != null )
\r
397 this.texture.Dispose();
\r
398 this.texture = null;
\r
402 if( this.vbPositionColoredVertexBuffer != null )
\r
404 this.vbPositionColoredVertexBuffer.Dispose();
\r
405 this.vbPositionColoredVertexBuffer = null;
\r
407 this.bDispose完了済み = true;
\r
410 //-----------------
\r
416 #region [ private ]
\r
417 //-----------------
\r
419 private bool bDispose完了済み;
\r
420 private PositionColoredTexturedVertex[] cvPositionColoredVertexies;
\r
421 private TransformedColoredTexturedVertex[] cvTransformedColoredVertexies;
\r
422 private VertexBuffer vbPositionColoredVertexBuffer;
\r
423 private const Pool poolvar = // 2011.4.25 yyagi
\r
424 #if TEST_Direct3D9Ex
\r
431 private void tレンダリングステートの設定( Device device )
\r
435 device.SetRenderState( RenderState.AlphaBlendEnable, true );
\r
436 device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5
\r
437 device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.One ); // 2
\r
441 device.SetRenderState( RenderState.AlphaBlendEnable, true );
\r
442 device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5
\r
443 device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.InverseSourceAlpha ); // 6
\r
446 private Size t指定されたサイズを超えない最適なテクスチャサイズを返す( Device device, Size sz指定サイズ )
\r
448 bool flag = ( device.Capabilities.TextureCaps & TextureCaps.NonPow2Conditional ) != 0;
\r
449 bool flag2 = ( device.Capabilities.TextureCaps & TextureCaps.Pow2 ) != 0;
\r
450 bool flag3 = ( device.Capabilities.TextureCaps & TextureCaps.SquareOnly ) != 0;
\r
451 int maxTextureWidth = device.Capabilities.MaxTextureWidth;
\r
452 int maxTextureHeight = device.Capabilities.MaxTextureHeight;
\r
453 Size size = new Size( sz指定サイズ.Width, sz指定サイズ.Height );
\r
454 if( flag2 && !flag )
\r
461 while( num3 <= sz指定サイズ.Width );
\r
462 sz指定サイズ.Width = num3;
\r
468 while( num3 <= sz指定サイズ.Height );
\r
469 sz指定サイズ.Height = num3;
\r
471 if( sz指定サイズ.Width > maxTextureWidth )
\r
473 sz指定サイズ.Width = maxTextureWidth;
\r
475 if( sz指定サイズ.Height > maxTextureHeight )
\r
477 sz指定サイズ.Height = maxTextureHeight;
\r
481 if( size.Width > size.Height )
\r
483 size.Height = size.Width;
\r
486 if( size.Width < size.Height )
\r
488 size.Width = size.Height;
\r
493 private void t頂点バッファの作成( Device device )
\r
495 this.vbPositionColoredVertexBuffer = new VertexBuffer( device, 4 * PositionColoredTexturedVertex.SizeInBytes, Usage.WriteOnly, VertexFormat.None, poolvar );
\r
497 //-----------------
\r