コード:
#include "DxLib.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
ChangeWindowMode(TRUE);
if (DxLib_Init() != 0) return 0;
SetDrawScreen(DX_SCREEN_BACK);
VECTOR cameraPosision = VGet(0.0f, 0.0f, 0.0f);
VECTOR cameraTarget = VGet(0.0f, 0.0f, 100.0f);
VECTOR cameraUp = VGet(0.0f, 1.0f, 0.0f);
SetupCamera_Perspective(DX_PI_F * 2 / 4);
int xDist = 0;
int yDist = 0;
int xPosOld = 640/2;
int yPosOld = 480/2;
SetMousePoint(xPosOld, yPosOld);
while (ProcessMessage() == 0 && ScreenFlip() == 0 && ClearDrawScreen() == 0) {
{
int xPos, yPos;
GetMousePoint(&xPos, &yPos);
SetMousePoint(xPosOld, yPosOld);
xDist += xPos - xPosOld;
yDist += yPos - yPosOld;
// 真上や真下より後ろには回さない ※1000ピクセルで1周換算
if (yDist < -250) yDist = -250;
if (yDist > 250) yDist = 250;
// 回転行列を作る
MATRIX mat = MGetIdent();
mat = MMult(mat, MGetRotX(yDist * DX_PI_F * 2 / 1000)); // ※1000ピクセルで1周換算
mat = MMult(mat, MGetRotY(xDist * DX_PI_F * 2 / 1000)); // ※1000ピクセルで1周換算
// カメラの向きを回転
VECTOR target = VTransform(cameraTarget, mat);
VECTOR up = VTransform(cameraUp, mat); // ※真上や真下で破綻させないために必要
// ※カメラ位置も原点以外なら要変換
SetCameraPositionAndTargetAndUpVec(cameraPosision, target, up);
}
{
VECTOR v[] = {
VGet(-100.0f, 100.0f, 100.0f),
VGet( 100.0f, 100.0f, 100.0f),
VGet(-100.0f, -100.0f, 100.0f),
VGet( 100.0f, -100.0f, 100.0f),
VGet( 100.0f, 100.0f, -100.0f),
VGet( 100.0f, -100.0f, -100.0f),
VGet(-100.0f, 100.0f, -100.0f),
VGet(-100.0f, -100.0f, -100.0f),
};
int idxs[] = {
0,6,1,4,1,6,0,1,2,3,2,1,1,4,3,5,3,4,4,6,5,7,5,6,6,0,7,2,7,0,2,3,7,5,7,3,
};
int color[] = {
GetColor( 0, 0,255),
GetColor(255,255,255),
GetColor(255,192, 0),
GetColor(255,255, 0),
GetColor(255, 0, 0),
GetColor( 0,255, 0),
};
for (int i=0; i<6*2; i++) {
DrawTriangle3D(v[idxs[i*3+0]], v[idxs[i*3+1]], v[idxs[i*3+2]], color[i/2], TRUE);
}
}
DrawString(0, 0, "終了 : Alt + F4", GetColor(0,0,0));
}
DxLib_End();
return 0;
}