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
104 colorKey = -16777216;
\r
105 this.texture = Texture.FromStream( device, stream, this.szテクスチャサイズ.Width, this.szテクスチャサイズ.Height, 1, Usage.None, format, Pool.Managed, Filter.Point, Filter.None, colorKey );
\r
107 this.t頂点バッファの作成( device );
\r
112 throw new CTextureCreateFailedException( "ビットマップからのテクスチャの生成に失敗しました。" );
\r
117 /// <para>空の Managed テクスチャを作成する。</para>
\r
118 /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
119 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
120 /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
\r
121 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
\r
122 /// カラーキーは 0x00000000(透過しない)になる。</para>
\r
124 /// <param name="device">Direct3D9 デバイス。</param>
\r
125 /// <param name="n幅">テクスチャの幅(希望値)。</param>
\r
126 /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
\r
127 /// <param name="format">テクスチャのフォーマット。</param>
\r
128 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
129 public CTexture( Device device, int n幅, int n高さ, Format format )
\r
130 : this( device, n幅, n高さ, format, Pool.Managed )
\r
135 /// <para>指定された画像ファイルから Managed テクスチャを作成する。</para>
\r
136 /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
\r
138 /// <param name="device">Direct3D9 デバイス。</param>
\r
139 /// <param name="strファイル名">画像ファイル名。</param>
\r
140 /// <param name="format">テクスチャのフォーマット。</param>
\r
141 /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
\r
142 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
143 public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する )
\r
144 : this( device, strファイル名, format, b黒を透過する, Pool.Managed )
\r
149 /// <para>空のテクスチャを作成する。</para>
\r
150 /// <para>テクスチャのサイズは、指定された希望サイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
151 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
152 /// <para>テクスチャのテクセルデータは未初期化。(おそらくゴミデータが入ったまま。)</para>
\r
153 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None、
\r
154 /// カラーキーは 0x00000000(透過しない)になる。</para>
\r
156 /// <param name="device">Direct3D9 デバイス。</param>
\r
157 /// <param name="n幅">テクスチャの幅(希望値)。</param>
\r
158 /// <param name="n高さ">テクスチャの高さ(希望値)。</param>
\r
159 /// <param name="format">テクスチャのフォーマット。</param>
\r
160 /// <param name="pool">テクスチャの管理方法。</param>
\r
161 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
162 public CTexture( Device device, int n幅, int n高さ, Format format, Pool pool )
\r
167 this.sz画像サイズ = new Size( n幅, n高さ );
\r
168 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
\r
169 using( Bitmap bitmap = new Bitmap( 1, 1 ) )
\r
171 using( Graphics graphics = Graphics.FromImage( bitmap ) )
\r
173 graphics.FillRectangle( Brushes.Black, 0, 0, 1, 1 );
\r
175 using( MemoryStream stream = new MemoryStream() )
\r
177 bitmap.Save( stream, ImageFormat.Bmp );
\r
178 stream.Seek( 0L, SeekOrigin.Begin );
\r
179 this.texture = Texture.FromStream( device, stream, n幅, n高さ, 1, Usage.None, format, pool, Filter.Point, Filter.None, 0 );
\r
182 this.t頂点バッファの作成( device );
\r
187 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n({0}x{1}, {2})", n幅, n高さ, format ) );
\r
192 /// <para>画像ファイルからテクスチャを生成する。</para>
\r
193 /// <para>利用可能な画像形式は、BMP, JPG, PNG, TGA, DDS, PPM, DIB, HDR, PFM のいずれか。</para>
\r
194 /// <para>テクスチャのサイズは、画像のサイズ以上、かつ、D3D9デバイスで生成可能な最小のサイズに自動的に調節される。
\r
195 /// その際、テクスチャの調節後のサイズにあわせた画像の拡大縮小は行わない。</para>
\r
196 /// <para>その他、ミップマップ数は 1、Usage は None、イメージフィルタは Point、ミップマップフィルタは None になる。</para>
\r
198 /// <param name="device">Direct3D9 デバイス。</param>
\r
199 /// <param name="strファイル名">画像ファイル名。</param>
\r
200 /// <param name="format">テクスチャのフォーマット。</param>
\r
201 /// <param name="b黒を透過する">画像の黒(0xFFFFFFFF)を透過させるなら true。</param>
\r
202 /// <param name="pool">テクスチャの管理方法。</param>
\r
203 /// <exception cref="CTextureCreateFailedException">テクスチャの作成に失敗しました。</exception>
\r
204 public CTexture( Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool )
\r
209 ImageInformation information = ImageInformation.FromFile( strファイル名 );
\r
210 this.sz画像サイズ = new Size( information.Width, information.Height );
\r
211 this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す( device, this.sz画像サイズ );
\r
212 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
213 this.t頂点バッファの作成( device );
\r
218 throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) );
\r
226 /// テクスチャを 2D 画像と見なして描画する。
\r
228 /// <param name="device">Direct3D9 デバイス。</param>
\r
229 /// <param name="x">描画位置(テクスチャの左上位置の X 座標[dot])。</param>
\r
230 /// <param name="y">描画位置(テクスチャの左上位置の Y 座標[dot])。</param>
\r
231 public void t2D描画( Device device, int x, int y )
\r
233 this.t2D描画( device, x, y, new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height ) );
\r
235 public void t2D描画( Device device, int x, int y, Rectangle rc画像内の描画領域 )
\r
237 this.t2D描画( device, x, y, 1f, rc画像内の描画領域 );
\r
239 public void t2D描画( Device device, int x, int y, float depth, Rectangle rc画像内の描画領域 )
\r
241 if( this.texture != null )
\r
243 this.tレンダリングステートの設定( device );
\r
244 if( this.fZ軸中心回転 == 0f )
\r
247 float num2 = -0.5f;
\r
248 float width = rc画像内の描画領域.Width;
\r
249 float height = rc画像内の描画領域.Height;
\r
250 float num5 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
\r
251 float num6 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
\r
252 float num7 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
\r
253 float num8 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
\r
254 int num9 = new Color4( ( (float) this._透明度 ) / 255f, 1f, 1f, 1f ).ToArgb();
\r
255 if( this.cvTransformedColoredVertexies == null )
\r
257 this.cvTransformedColoredVertexies = new TransformedColoredTexturedVertex[ 4 ];
\r
259 this.cvTransformedColoredVertexies[ 0 ].Position = new Vector4( x + num, y + num2, depth, 1f );
\r
260 this.cvTransformedColoredVertexies[ 0 ].Color = num9;
\r
261 this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates = new Vector2( num5, num7 );
\r
262 this.cvTransformedColoredVertexies[ 1 ].Position = new Vector4( ( x + ( width * this.vc拡大縮小倍率.X ) ) + num, y + num2, depth, 1f );
\r
263 this.cvTransformedColoredVertexies[ 1 ].Color = num9;
\r
264 this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates = new Vector2( num6, num7 );
\r
265 this.cvTransformedColoredVertexies[ 2 ].Position = new Vector4( x + num, ( y + ( height * this.vc拡大縮小倍率.Y ) ) + num2, depth, 1f );
\r
266 this.cvTransformedColoredVertexies[ 2 ].Color = num9;
\r
267 this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates = new Vector2( num5, num8 );
\r
268 this.cvTransformedColoredVertexies[ 3 ].Position = new Vector4( ( x + ( width * this.vc拡大縮小倍率.X ) ) + num, ( y + ( height * this.vc拡大縮小倍率.Y ) ) + num2, depth, 1f );
\r
269 this.cvTransformedColoredVertexies[ 3 ].Color = num9;
\r
270 this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates = new Vector2( num6, num8 );
\r
271 device.SetTexture( 0, this.texture );
\r
272 device.VertexFormat = TransformedColoredTexturedVertex.Format;
\r
273 device.DrawUserPrimitives<TransformedColoredTexturedVertex>( PrimitiveType.TriangleStrip, 0, 2, this.cvTransformedColoredVertexies );
\r
277 float num10 = ( ( rc画像内の描画領域.Width % 2 ) == 0 ) ? -0.5f : 0f;
\r
278 float num11 = ( ( rc画像内の描画領域.Height % 2 ) == 0 ) ? -0.5f : 0f;
\r
279 float num12 = ( (float) rc画像内の描画領域.Width ) / 2f;
\r
280 float num13 = ( (float) rc画像内の描画領域.Height ) / 2f;
\r
281 int num1 = rc画像内の描画領域.Width;
\r
282 int num21 = rc画像内の描画領域.Height;
\r
283 float num14 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
\r
284 float num15 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
\r
285 float num16 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
\r
286 float num17 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
\r
287 int num18 = new Color4( ( (float) this._透明度 ) / 255f, 1f, 1f, 1f ).ToArgb();
\r
288 if( this.cvPositionColoredVertexies == null )
\r
290 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
\r
292 this.cvPositionColoredVertexies[ 0 ].Position = new Vector3( -num12 + num10, num13 + num11, depth );
\r
293 this.cvPositionColoredVertexies[ 0 ].Color = num18;
\r
294 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates = new Vector2( num14, num16 );
\r
295 this.cvPositionColoredVertexies[ 1 ].Position = new Vector3( num12 + num10, num13 + num11, depth );
\r
296 this.cvPositionColoredVertexies[ 1 ].Color = num18;
\r
297 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates = new Vector2( num15, num16 );
\r
298 this.cvPositionColoredVertexies[ 2 ].Position = new Vector3( -num12 + num10, -num13 + num11, depth );
\r
299 this.cvPositionColoredVertexies[ 2 ].Color = num18;
\r
300 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates = new Vector2( num14, num17 );
\r
301 this.cvPositionColoredVertexies[ 3 ].Position = new Vector3( num12 + num10, -num13 + num11, depth );
\r
302 this.cvPositionColoredVertexies[ 3 ].Color = num18;
\r
303 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates = new Vector2( num15, num17 );
\r
304 using( DataStream stream = this.vbPositionColoredVertexBuffer.Lock( 0, 0, LockFlags.None ) )
\r
306 stream.WriteRange<PositionColoredTexturedVertex>( this.cvPositionColoredVertexies );
\r
307 this.vbPositionColoredVertexBuffer.Unlock();
\r
309 int num19 = x + ( rc画像内の描画領域.Width / 2 );
\r
310 int num20 = y + ( rc画像内の描画領域.Height / 2 );
\r
311 Vector3 amount = new Vector3( num19 - ( ( (float) device.Viewport.Width ) / 2f ), -( num20 - ( ( (float) device.Viewport.Height ) / 2f ) ), 0f );
\r
312 Matrix matrix = Matrix.Identity * Matrix.Scaling( this.vc拡大縮小倍率 );
\r
313 matrix *= Matrix.RotationZ( this.fZ軸中心回転 );
\r
314 matrix *= Matrix.Translation( amount );
\r
315 device.SetTransform( TransformState.World, matrix );
\r
316 device.SetTexture( 0, this.texture );
\r
317 device.SetStreamSource( 0, this.vbPositionColoredVertexBuffer, 0, PositionColoredTexturedVertex.SizeInBytes );
\r
318 device.VertexFormat = PositionColoredTexturedVertex.Format;
\r
319 device.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 );
\r
325 /// テクスチャを 3D 画像と見なして描画する。
\r
327 public void t3D描画( Device device, Matrix mat )
\r
329 this.t3D描画( device, mat, new Rectangle( 0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height ) );
\r
331 public void t3D描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 )
\r
333 if( this.texture != null )
\r
335 float x = ( (float) rc画像内の描画領域.Width ) / 2f;
\r
336 float y = ( (float) rc画像内の描画領域.Height ) / 2f;
\r
337 int width = rc画像内の描画領域.Width;
\r
338 int height = rc画像内の描画領域.Height;
\r
339 float num3 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width );
\r
340 float num4 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width );
\r
341 float num5 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height );
\r
342 float num6 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height );
\r
343 int num7 = new Color4( ( (float) this._透明度 ) / 255f, 1f, 1f, 1f ).ToArgb();
\r
344 if( this.cvPositionColoredVertexies == null )
\r
346 this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ];
\r
349 this.cvPositionColoredVertexies[ 0 ].Position = new Vector3( -x, y, z );
\r
350 this.cvPositionColoredVertexies[ 0 ].Color = num7;
\r
351 this.cvPositionColoredVertexies[ 0 ].TextureCoordinates = new Vector2( num3, num5 );
\r
352 this.cvPositionColoredVertexies[ 1 ].Position = new Vector3( x, y, z );
\r
353 this.cvPositionColoredVertexies[ 1 ].Color = num7;
\r
354 this.cvPositionColoredVertexies[ 1 ].TextureCoordinates = new Vector2( num4, num5 );
\r
355 this.cvPositionColoredVertexies[ 2 ].Position = new Vector3( -x, -y, z );
\r
356 this.cvPositionColoredVertexies[ 2 ].Color = num7;
\r
357 this.cvPositionColoredVertexies[ 2 ].TextureCoordinates = new Vector2( num3, num6 );
\r
358 this.cvPositionColoredVertexies[ 3 ].Position = new Vector3( x, -y, z );
\r
359 this.cvPositionColoredVertexies[ 3 ].Color = num7;
\r
360 this.cvPositionColoredVertexies[ 3 ].TextureCoordinates = new Vector2( num4, num6 );
\r
361 using( DataStream stream = this.vbPositionColoredVertexBuffer.Lock( 0, 0, LockFlags.None ) )
\r
363 stream.WriteRange<PositionColoredTexturedVertex>( this.cvPositionColoredVertexies );
\r
364 this.vbPositionColoredVertexBuffer.Unlock();
\r
366 this.tレンダリングステートの設定( device );
\r
367 device.SetTransform( TransformState.World, mat );
\r
368 device.SetTexture( 0, this.texture );
\r
369 device.SetStreamSource( 0, this.vbPositionColoredVertexBuffer, 0, PositionColoredTexturedVertex.SizeInBytes );
\r
370 device.VertexFormat = PositionColoredTexturedVertex.Format;
\r
371 device.DrawPrimitives( PrimitiveType.TriangleStrip, 0, 2 );
\r
375 #region [ IDosposable 実装 ]
\r
376 //-----------------
\r
377 public void Dispose()
\r
379 if( !this.bDispose完了済み )
\r
382 if( this.texture != null )
\r
384 this.texture.Dispose();
\r
385 this.texture = null;
\r
389 if( this.vbPositionColoredVertexBuffer != null )
\r
391 this.vbPositionColoredVertexBuffer.Dispose();
\r
392 this.vbPositionColoredVertexBuffer = null;
\r
394 this.bDispose完了済み = true;
\r
397 //-----------------
\r
403 #region [ private ]
\r
404 //-----------------
\r
406 private bool bDispose完了済み;
\r
407 private PositionColoredTexturedVertex[] cvPositionColoredVertexies;
\r
408 private TransformedColoredTexturedVertex[] cvTransformedColoredVertexies;
\r
409 private VertexBuffer vbPositionColoredVertexBuffer;
\r
411 private void tレンダリングステートの設定( Device device )
\r
415 device.SetRenderState( RenderState.AlphaBlendEnable, true );
\r
416 device.SetRenderState( RenderState.SourceBlend, 5 );
\r
417 device.SetRenderState( RenderState.DestinationBlend, 2 );
\r
421 device.SetRenderState( RenderState.AlphaBlendEnable, true );
\r
422 device.SetRenderState( RenderState.SourceBlend, 5 );
\r
423 device.SetRenderState( RenderState.DestinationBlend, 6 );
\r
426 private Size t指定されたサイズを超えない最適なテクスチャサイズを返す( Device device, Size sz指定サイズ )
\r
428 bool flag = ( device.Capabilities.TextureCaps & TextureCaps.NonPow2Conditional ) != 0;
\r
429 bool flag2 = ( device.Capabilities.TextureCaps & TextureCaps.Pow2 ) != 0;
\r
430 bool flag3 = ( device.Capabilities.TextureCaps & TextureCaps.SquareOnly ) != 0;
\r
431 int maxTextureWidth = device.Capabilities.MaxTextureWidth;
\r
432 int maxTextureHeight = device.Capabilities.MaxTextureHeight;
\r
433 Size size = new Size( sz指定サイズ.Width, sz指定サイズ.Height );
\r
434 if( flag2 && !flag )
\r
441 while( num3 <= sz指定サイズ.Width );
\r
442 sz指定サイズ.Width = num3;
\r
448 while( num3 <= sz指定サイズ.Height );
\r
449 sz指定サイズ.Height = num3;
\r
451 if( sz指定サイズ.Width > maxTextureWidth )
\r
453 sz指定サイズ.Width = maxTextureWidth;
\r
455 if( sz指定サイズ.Height > maxTextureHeight )
\r
457 sz指定サイズ.Height = maxTextureHeight;
\r
461 if( size.Width > size.Height )
\r
463 size.Height = size.Width;
\r
466 if( size.Width < size.Height )
\r
468 size.Width = size.Height;
\r
473 private void t頂点バッファの作成( Device device )
\r
475 this.vbPositionColoredVertexBuffer = new VertexBuffer( device, 4 * PositionColoredTexturedVertex.SizeInBytes, Usage.WriteOnly, VertexFormat.None, Pool.Managed );
\r
477 //-----------------
\r