今回は画像を回転させます
演出やギミック、特に2Dシューティングゲームなどでは必須の処理でしょう


今回使うファイルは次のようになってます。どぞー(-ω-)つ旦
・&ref(direct3d.h);
・&ref(direct3d.cpp);
・&ref(texture.h);
・&ref(texture.cpp);
・&ref(sprite.h);
・&ref(sprite.cpp);
・&ref(rotate.cpp);(WinMain関数があるアプリケーション本体)
今回使う画像はこちら
・&ref(car.bmp);

zipでほしい人はこちら
・&ref(rotate.zip);

sprite.hで回転情報を追加します

 #pragma once
 
 // Direct3Dの各種ヘッダーのインクルードが必要なため
 // すでにまとめてあるヘッダーをインクルードする
 #include "direct3d.h"
 
 struct AnimationNum{
	unsigned int numU;
	unsigned int numV;
 };
 
 // スプライトクラス(2D板ポリゴン)
 class Sprite
 {
 public:
	// 板ポリゴン頂点情報
	struct Vertex{
		float x,y,z;// 3次元座標
		float rhw;	// 2D変換済みフラグ
		float u,v;	// UV座標
	};
	// FVF(柔軟な頂点構造体宣言)フラグ
	static const DWORD SPRITE_FVF = D3DFVF_XYZRHW | D3DFVF_TEX1;
	
	// スプライト位置
	D3DXVECTOR2 pos;
	// スプライトサイズ
	int width;
	int height;
	// UVの分割数
	unsigned int divU;
	unsigned int divV;
	// UVの番号
	unsigned int numU;
	unsigned int numV;
 ///////////////////////////////////////////////////////
	// 回転値(ラジアン)
	float rotate;
 ///////////////////////////////////////////////////////
 
	// コンストラクタ
	Sprite();
	// デストラクタ
	~Sprite();
 
	void SetPos(float x,float y);
	void SetWidth(int Width,int Height);
 ///////////////////////////////////////////////////////
	void SetRotate(float Rotate);
 ///////////////////////////////////////////////////////
 
	void SetDivide(unsigned int DivU,unsigned int DivV);
	void SetUVNum(unsigned int NumU,unsigned int NumV);
	void Draw(IDirect3DDevice9* pDevice3D,IDirect3DTexture9* pTexture,bool isTurn = false);
 };


sprite.cppで追加のメンバ関数の実装とDrawメンバ関数の改良をします
頂点情報は回転してから指定位置に平行移動するという計算になっていることに注意してください

 #include "sprite.h"
 
 // コンストラクタ
 Sprite::Sprite()
 {
	pos.x = pos.y = 0.0f;
	width = 0;
	height = 0;
	divU = 1;
	divV = 1;
	numU = 0;
	numV = 0;
	rotate = 0.0f;
 }
 
 // 18回と同じなので略
 
 void Sprite::SetRotate(float Rotate)
 {
	rotate = Rotate;
 }
 
 // 18回と同じなので略
 
 void Sprite::Draw(IDirect3DDevice9* pDevice3D,IDirect3DTexture9* pTexture,bool isTurn)
 {
	// 頂点情報セット
	Vertex vtx[4]={
		{ (float) width/2,(float)-height/2, 0.0f, 1.0f,(isTurn ? (float)numU/divU : (float)(numU + 1)/divU), (float)numV/divV},
		{ (float) width/2,(float) height/2, 0.0f, 1.0f,(isTurn ? (float)numU/divU : (float)(numU + 1)/divU), (float)(numV + 1)/divV}, 
		{ (float)-width/2,(float)-height/2, 0.0f, 1.0f,(isTurn ? (float)(numU + 1)/divU : (float)numU/divU), (float)numV/divV}, 
		{ (float)-width/2,(float) height/2, 0.0f, 1.0f,(isTurn ? (float)(numU + 1)/divU : (float)numU/divU), (float)(numV + 1)/divV} 
	};
 
	for(int i = 0;i < 4;++i){
		float x = vtx[i].x * cosf(rotate) - vtx[i].y * sinf(rotate);
		float y = vtx[i].x * sinf(rotate) + vtx[i].y * cosf(rotate);
		vtx[i].x = x + pos.x;
		vtx[i].y = y + pos.y;
	}
	// テクスチャセット
	pDevice3D->SetTexture(0,pTexture);
	// 頂点構造体宣言セット
	pDevice3D->SetFVF(SPRITE_FVF);
	// スプライト描画
	pDevice3D->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,2,vtx,sizeof(Vertex));
 }

使うときはrotate.cppで回転情報をセットします


 ///////////////////////////////////////////////////////////////////////////////////////////////
 
	////////////////////////////////////
	//	Direct3Dの初期化
	////////////////////////////////////
	Direct3D direct3d;
	direct3d.Create(hWnd,WINDOW_WIDTH,WINDOW_HEIGHT);
 
	////////////////////////////////
	// テクスチャ作成
	////////////////////////////////
	Texture cartex;
	cartex.Load(direct3d.pDevice3D,_T("car.bmp"));
 
	////////////////////////////////
	// スプライト作成
	////////////////////////////////
	Sprite sprite;
 
	sprite.SetPos(200,200);
	sprite.SetWidth(128,128);
	sprite.SetRotate(D3DX_PI/4.0f);
 
 ///////////////////////////////////////////////////////////////////////////////////////////////

今回の実行結果は次のようになります
回転してます
&ref(rotate.png);

前:DirectX講座19回
次:DirectX講座21回

#vote( ミッションコンプリート[3],馬鹿なの?死ぬの?[23],トゥットルゥ〜[6]);
#vote( ミッションコンプリート[4],馬鹿なの?死ぬの?[23],トゥットルゥ〜[6]);



トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS