#13
by qwea » 8年前
流れをぶった切る形で申し訳ないのですが、
自分のゲームでは行列を使って頂点を計算しています。
計算する関数はDXLIBが用意してくれています。
以下サンプルを乗せました。参考になりましたら幸いです。
コード:
#include "DxLib.h"
#define WIDTH 50
#define HEIGHT 50
int g_image_handle = -1;
// 回転描画
void Draw3DRot(float _x, float _y, float _x_rot, float _y_rot, float _z_rot)
{
VECTOR m_pos[4] = {
{-WIDTH/2, -HEIGHT/2, 0.f}, // 左上頂点
{WIDTH/2, -HEIGHT/2, 0.f}, // 右上頂点
{WIDTH/2, HEIGHT/2, 0.f}, // 右下頂点
{-WIDTH/2, HEIGHT/2, 0.f}, // 左下頂点
};
MATRIX transMat, rotMat, mulMat;
CreateTranslationMatrix(&transMat, _x, _y, 0.f);
CreateRotationYXZMatrix(&rotMat, _x_rot, _y_rot, _z_rot);
// 単位行列
CreateIdentityMatrix(&mulMat);
// 回転させる
CreateMultiplyMatrix(&mulMat, &mulMat, &rotMat);
// 移動させる
CreateMultiplyMatrix(&mulMat, &mulMat, &transMat);
// 各頂点に行列を適用
for(int i = 0; i < 4; i++)
{
VectorTransform(&m_pos[i], &m_pos[i], &mulMat);
}
DrawModiGraphF(m_pos[0].x, m_pos[0].y, m_pos[1].x, m_pos[1].y, m_pos[2].x, m_pos[2].y, m_pos[3].x, m_pos[3].y, g_image_handle, TRUE);
}
// WinMain
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
float ang = 0;
// ウインドウモードで起動
ChangeWindowMode( TRUE );
// ライブラリの初期化
if( DxLib_Init() < 0 )
return -1;
// 描画先を裏画面にする
SetDrawScreen( DX_SCREEN_BACK );
g_image_handle = LoadGraph("test.png");
// ESCキーが押されるか、ウインドウが閉じられるまでループ
while( ProcessMessage() == 0 && CheckHitKey( KEY_INPUT_ESCAPE ) == 0 )
{
// 画面をクリア
ClearDrawScreen();
float x = 640/7/2;
float w = 640/7;
float y = 480/2;
Draw3DRot(x+w*0, y, ang, 0, 0);
Draw3DRot(x+w*1, y, 0, ang, 0);
Draw3DRot(x+w*2, y, 0, 0, ang);
Draw3DRot(x+w*3, y, ang, ang, 0);
Draw3DRot(x+w*4, y, ang, 0, ang);
Draw3DRot(x+w*5, y, 0, ang, ang);
Draw3DRot(x+w*6, y, ang, ang, ang);
ang -= 0.05f;
// 裏画面の内容を表画面に反映
ScreenFlip();
}
// ライブラリの後始末
DxLib_End() ;
// ソフト終了
return 0 ;
}
流れをぶった切る形で申し訳ないのですが、
自分のゲームでは行列を使って頂点を計算しています。
計算する関数はDXLIBが用意してくれています。
以下サンプルを乗せました。参考になりましたら幸いです。
[code]
#include "DxLib.h"
#define WIDTH 50
#define HEIGHT 50
int g_image_handle = -1;
// 回転描画
void Draw3DRot(float _x, float _y, float _x_rot, float _y_rot, float _z_rot)
{
VECTOR m_pos[4] = {
{-WIDTH/2, -HEIGHT/2, 0.f}, // 左上頂点
{WIDTH/2, -HEIGHT/2, 0.f}, // 右上頂点
{WIDTH/2, HEIGHT/2, 0.f}, // 右下頂点
{-WIDTH/2, HEIGHT/2, 0.f}, // 左下頂点
};
MATRIX transMat, rotMat, mulMat;
CreateTranslationMatrix(&transMat, _x, _y, 0.f);
CreateRotationYXZMatrix(&rotMat, _x_rot, _y_rot, _z_rot);
// 単位行列
CreateIdentityMatrix(&mulMat);
// 回転させる
CreateMultiplyMatrix(&mulMat, &mulMat, &rotMat);
// 移動させる
CreateMultiplyMatrix(&mulMat, &mulMat, &transMat);
// 各頂点に行列を適用
for(int i = 0; i < 4; i++)
{
VectorTransform(&m_pos[i], &m_pos[i], &mulMat);
}
DrawModiGraphF(m_pos[0].x, m_pos[0].y, m_pos[1].x, m_pos[1].y, m_pos[2].x, m_pos[2].y, m_pos[3].x, m_pos[3].y, g_image_handle, TRUE);
}
// WinMain
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
float ang = 0;
// ウインドウモードで起動
ChangeWindowMode( TRUE );
// ライブラリの初期化
if( DxLib_Init() < 0 )
return -1;
// 描画先を裏画面にする
SetDrawScreen( DX_SCREEN_BACK );
g_image_handle = LoadGraph("test.png");
// ESCキーが押されるか、ウインドウが閉じられるまでループ
while( ProcessMessage() == 0 && CheckHitKey( KEY_INPUT_ESCAPE ) == 0 )
{
// 画面をクリア
ClearDrawScreen();
float x = 640/7/2;
float w = 640/7;
float y = 480/2;
Draw3DRot(x+w*0, y, ang, 0, 0);
Draw3DRot(x+w*1, y, 0, ang, 0);
Draw3DRot(x+w*2, y, 0, 0, ang);
Draw3DRot(x+w*3, y, ang, ang, 0);
Draw3DRot(x+w*4, y, ang, 0, ang);
Draw3DRot(x+w*5, y, 0, ang, ang);
Draw3DRot(x+w*6, y, ang, ang, ang);
ang -= 0.05f;
// 裏画面の内容を表画面に反映
ScreenFlip();
}
// ライブラリの後始末
DxLib_End() ;
// ソフト終了
return 0 ;
}
[/code]