| 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 |
return; |
| 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 f左U値 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width ); |
| 383 |
float f右U値 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width ); |
| 384 |
float f上V値 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height ); |
| 385 |
float f下V値 = ( (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 = f左U値; |
| 400 |
this.cvTransformedColoredVertexies[ 0 ].TextureCoordinates.Y = f上V値; |
| 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 = f右U値; |
| 408 |
this.cvTransformedColoredVertexies[ 1 ].TextureCoordinates.Y = f上V値; |
| 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 = f左U値; |
| 416 |
this.cvTransformedColoredVertexies[ 2 ].TextureCoordinates.Y = f下V値; |
| 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 = f右U値; |
| 424 |
this.cvTransformedColoredVertexies[ 3 ].TextureCoordinates.Y = f下V値; |
| 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 f左U値 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width ); |
| 441 |
float f右U値 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width ); |
| 442 |
float f上V値 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height ); |
| 443 |
float f下V値 = ( (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 = f左U値; |
| 457 |
this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = f上V値; |
| 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 = f右U値; |
| 464 |
this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = f上V値; |
| 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 = f左U値; |
| 471 |
this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = f下V値; |
| 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 = f右U値; |
| 478 |
this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = f下V値; |
| 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 |
|
| 497 |
public static Vector3 t論理画面座標をワールド座標へ変換する(int x, int y) |
| 498 |
{ |
| 499 |
return CTexture.t論理画面座標をワールド座標へ変換する(new Vector3((float)x, (float)y, 0f)); |
| 500 |
} |
| 501 |
public static Vector3 t論理画面座標をワールド座標へ変換する(float x, float y) |
| 502 |
{ |
| 503 |
return CTexture.t論理画面座標をワールド座標へ変換する(new Vector3(x, y, 0f)); |
| 504 |
} |
| 505 |
public static Vector3 t論理画面座標をワールド座標へ変換する(Point pt論理画面座標) |
| 506 |
{ |
| 507 |
return CTexture.t論理画面座標をワールド座標へ変換する(new Vector3(pt論理画面座標.X, pt論理画面座標.Y, 0.0f)); |
| 508 |
} |
| 509 |
public static Vector3 t論理画面座標をワールド座標へ変換する(Vector2 v2論理画面座標) |
| 510 |
{ |
| 511 |
return CTexture.t論理画面座標をワールド座標へ変換する(new Vector3(v2論理画面座標, 0f)); |
| 512 |
} |
| 513 |
public static Vector3 t論理画面座標をワールド座標へ変換する(Vector3 v3論理画面座標) |
| 514 |
{ |
| 515 |
return new Vector3( |
| 516 |
(v3論理画面座標.X - (CTexture.sz論理画面.Width / 2.0f)) * CTexture.f画面比率, |
| 517 |
(-(v3論理画面座標.Y - (CTexture.sz論理画面.Height / 2.0f)) * CTexture.f画面比率), |
| 518 |
v3論理画面座標.Z); |
| 519 |
} |
| 520 |
|
| 521 |
/// <summary> |
| 522 |
/// テクスチャを 3D 画像と見なして描画する。 |
| 523 |
/// </summary> |
| 524 |
public void t3D描画( Device device, Matrix mat ) |
| 525 |
{ |
| 526 |
this.t3D描画( device, mat, this.rc全画像 ); |
| 527 |
} |
| 528 |
public void t3D描画( Device device, Matrix mat, Rectangle rc画像内の描画領域 ) |
| 529 |
{ |
| 530 |
if( this.texture == null ) |
| 531 |
return; |
| 532 |
|
| 533 |
float x = ( (float) rc画像内の描画領域.Width ) / 2f; |
| 534 |
float y = ( (float) rc画像内の描画領域.Height ) / 2f; |
| 535 |
float z = 0.0f; |
| 536 |
float f左U値 = ( (float) rc画像内の描画領域.Left ) / ( (float) this.szテクスチャサイズ.Width ); |
| 537 |
float f右U値 = ( (float) rc画像内の描画領域.Right ) / ( (float) this.szテクスチャサイズ.Width ); |
| 538 |
float f上V値 = ( (float) rc画像内の描画領域.Top ) / ( (float) this.szテクスチャサイズ.Height ); |
| 539 |
float f下V値 = ( (float) rc画像内の描画領域.Bottom ) / ( (float) this.szテクスチャサイズ.Height ); |
| 540 |
this.color4.Alpha = ( (float) this._透明度 ) / 255f; |
| 541 |
int color = this.color4.ToArgb(); |
| 542 |
|
| 543 |
if( this.cvPositionColoredVertexies == null ) |
| 544 |
this.cvPositionColoredVertexies = new PositionColoredTexturedVertex[ 4 ]; |
| 545 |
|
| 546 |
// #27122 2012.1.13 from: 以下、マネージドオブジェクト(=ガベージ)の量産を抑えるため、new は使わず、メンバに値を1つずつ直接上書きする。 |
| 547 |
|
| 548 |
this.cvPositionColoredVertexies[ 0 ].Position.X = -x; |
| 549 |
this.cvPositionColoredVertexies[ 0 ].Position.Y = y; |
| 550 |
this.cvPositionColoredVertexies[ 0 ].Position.Z = z; |
| 551 |
this.cvPositionColoredVertexies[ 0 ].Color = color; |
| 552 |
this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.X = f左U値; |
| 553 |
this.cvPositionColoredVertexies[ 0 ].TextureCoordinates.Y = f上V値; |
| 554 |
|
| 555 |
this.cvPositionColoredVertexies[ 1 ].Position.X = x; |
| 556 |
this.cvPositionColoredVertexies[ 1 ].Position.Y = y; |
| 557 |
this.cvPositionColoredVertexies[ 1 ].Position.Z = z; |
| 558 |
this.cvPositionColoredVertexies[ 1 ].Color = color; |
| 559 |
this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.X = f右U値; |
| 560 |
this.cvPositionColoredVertexies[ 1 ].TextureCoordinates.Y = f上V値; |
| 561 |
|
| 562 |
this.cvPositionColoredVertexies[ 2 ].Position.X = -x; |
| 563 |
this.cvPositionColoredVertexies[ 2 ].Position.Y = -y; |
| 564 |
this.cvPositionColoredVertexies[ 2 ].Position.Z = z; |
| 565 |
this.cvPositionColoredVertexies[ 2 ].Color = color; |
| 566 |
this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.X = f左U値; |
| 567 |
this.cvPositionColoredVertexies[ 2 ].TextureCoordinates.Y = f下V値; |
| 568 |
|
| 569 |
this.cvPositionColoredVertexies[ 3 ].Position.X = x; |
| 570 |
this.cvPositionColoredVertexies[ 3 ].Position.Y = -y; |
| 571 |
this.cvPositionColoredVertexies[ 3 ].Position.Z = z; |
| 572 |
this.cvPositionColoredVertexies[ 3 ].Color = color; |
| 573 |
this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.X = f右U値; |
| 574 |
this.cvPositionColoredVertexies[ 3 ].TextureCoordinates.Y = f下V値; |
| 575 |
|
| 576 |
this.tレンダリングステートの設定( device ); |
| 577 |
|
| 578 |
device.SetTransform( TransformState.World, mat ); |
| 579 |
device.SetTexture( 0, this.texture ); |
| 580 |
device.VertexFormat = PositionColoredTexturedVertex.Format; |
| 581 |
device.DrawUserPrimitives( PrimitiveType.TriangleStrip, 2, this.cvPositionColoredVertexies ); |
| 582 |
} |
| 583 |
|
| 584 |
#region [ IDosposable 実装 ] |
| 585 |
//----------------- |
| 586 |
public void Dispose() |
| 587 |
{ |
| 588 |
if( !this.bDispose完了済み ) |
| 589 |
{ |
| 590 |
// テクスチャの破棄 |
| 591 |
if( this.texture != null ) |
| 592 |
{ |
| 593 |
this.texture.Dispose(); |
| 594 |
this.texture = null; |
| 595 |
} |
| 596 |
|
| 597 |
this.bDispose完了済み = true; |
| 598 |
} |
| 599 |
} |
| 600 |
//----------------- |
| 601 |
#endregion |
| 602 |
|
| 603 |
|
| 604 |
// その他 |
| 605 |
|
| 606 |
#region [ private ] |
| 607 |
//----------------- |
| 608 |
private int _透明度; |
| 609 |
private bool bDispose完了済み; |
| 610 |
private PositionColoredTexturedVertex[] cvPositionColoredVertexies; |
| 611 |
private TransformedColoredTexturedVertex[] cvTransformedColoredVertexies; |
| 612 |
private const Pool poolvar = // 2011.4.25 yyagi |
| 613 |
#if TEST_Direct3D9Ex |
| 614 |
Pool.Default; |
| 615 |
#else |
| 616 |
Pool.Managed; |
| 617 |
#endif |
| 618 |
// byte[] _txData; |
| 619 |
static object lockobj = new object(); |
| 620 |
|
| 621 |
private void tレンダリングステートの設定( Device device ) |
| 622 |
{ |
| 623 |
if( this.b加算合成 ) |
| 624 |
{ |
| 625 |
device.SetRenderState( RenderState.AlphaBlendEnable, true ); |
| 626 |
device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5 |
| 627 |
device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.One ); // 2 |
| 628 |
} |
| 629 |
else |
| 630 |
{ |
| 631 |
device.SetRenderState( RenderState.AlphaBlendEnable, true ); |
| 632 |
device.SetRenderState( RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha ); // 5 |
| 633 |
device.SetRenderState( RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.InverseSourceAlpha ); // 6 |
| 634 |
} |
| 635 |
} |
| 636 |
private Size t指定されたサイズを超えない最適なテクスチャサイズを返す( Device device, Size sz指定サイズ ) |
| 637 |
{ |
| 638 |
bool b条件付きでサイズは2の累乗でなくてもOK = ( device.Capabilities.TextureCaps & TextureCaps.NonPow2Conditional ) != 0; |
| 639 |
bool bサイズは2の累乗でなければならない = ( device.Capabilities.TextureCaps & TextureCaps.Pow2 ) != 0; |
| 640 |
bool b正方形でなければならない = ( device.Capabilities.TextureCaps & TextureCaps.SquareOnly ) != 0; |
| 641 |
int n最大幅 = device.Capabilities.MaxTextureWidth; |
| 642 |
int n最大高 = device.Capabilities.MaxTextureHeight; |
| 643 |
var szサイズ = new Size( sz指定サイズ.Width, sz指定サイズ.Height ); |
| 644 |
|
| 645 |
if( bサイズは2の累乗でなければならない && !b条件付きでサイズは2の累乗でなくてもOK ) |
| 646 |
{ |
| 647 |
// 幅を2の累乗にする |
| 648 |
int n = 1; |
| 649 |
do |
| 650 |
{ |
| 651 |
n *= 2; |
| 652 |
} |
| 653 |
while( n <= sz指定サイズ.Width ); |
| 654 |
sz指定サイズ.Width = n; |
| 655 |
|
| 656 |
// 高さを2の累乗にする |
| 657 |
n = 1; |
| 658 |
do |
| 659 |
{ |
| 660 |
n *= 2; |
| 661 |
} |
| 662 |
while( n <= sz指定サイズ.Height ); |
| 663 |
sz指定サイズ.Height = n; |
| 664 |
} |
| 665 |
|
| 666 |
if( sz指定サイズ.Width > n最大幅 ) |
| 667 |
sz指定サイズ.Width = n最大幅; |
| 668 |
|
| 669 |
if( sz指定サイズ.Height > n最大高 ) |
| 670 |
sz指定サイズ.Height = n最大高; |
| 671 |
|
| 672 |
if( b正方形でなければならない ) |
| 673 |
{ |
| 674 |
if( szサイズ.Width > szサイズ.Height ) |
| 675 |
{ |
| 676 |
szサイズ.Height = szサイズ.Width; |
| 677 |
} |
| 678 |
else if( szサイズ.Width < szサイズ.Height ) |
| 679 |
{ |
| 680 |
szサイズ.Width = szサイズ.Height; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
return szサイズ; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
// 2012.3.21 さらなる new の省略作戦 |
| 689 |
|
| 690 |
private Rectangle rc全画像; // テクスチャ作ったらあとは不変 |
| 691 |
private Color4 color4 = new Color4( 1f, 1f, 1f, 1f ); // アルファ以外は不変 |
| 692 |
//----------------- |
| 693 |
#endregion |
| 694 |
} |
| 695 |
} |