開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 空の CLR プロジェクト
プロジェクト名 ClrD3D

ClrD3D_24.cpp
/*
#24 メッシュとカメラ移動
 
プロジェクトへの参照の追加(参照タブ)
C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0
Microsoft.DirectX.dll
Microsoft.DirectX.Direct3D.dll
Microsoft.DirectX.Direct3DX.dll
*/
#pragma comment(linker, "/subsystem:windows /entry:main")
 
// アセンブリ
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
 
// 名前空間
using namespace System;
using namespace System::Drawing;
using namespace System::Threading;
using namespace System::Windows::Forms;
using namespace Microsoft::DirectX;
using namespace Microsoft::DirectX::Direct3D;
 
ref class MainForm : Form
{
private:
	Device^ dev;
	PresentParameters^ pp;
	Mesh^ mesh;
	float fDist;	// 原点との距離
	float fAlt;	// 高度
	float fCamDir;	// カメラの左右角度
 
	Direct3D::Font^ font;
	int nSec;
	int nFps;
	int nFrame;
public:
	MainForm()
	{
		MinimumSize = Drawing::Size(160, 120);
		MaximizeBox = false;
		ClientSize = Drawing::Size(640, 480);
		Text = "ClrD3D";
		KeyDown += gcnew KeyEventHandler(this, &MainForm::OnKeyDown);
	}
 
	bool DXInitialize()
	{
		try {
			pp = gcnew PresentParameters;
			pp->Windowed = true;
			pp->SwapEffect = SwapEffect::Discard;
			pp->AutoDepthStencilFormat = DepthFormat::D16;
			pp->EnableAutoDepthStencil = true;
 
			dev = gcnew Device(0, DeviceType::Hardware, this,
				CreateFlags::HardwareVertexProcessing, pp);
			CreateMesh();
			CreateFont();
			fDist = 10.0f;
			fAlt = 5.0f;
			fCamDir = 0.0f;
			return true;
		}
		catch (Exception^) {
			return false;
		}
	}
 
	void Render()
	{
		if (dev == nullptr) return;
		if (WindowState == FormWindowState::Minimized) return;
		nFrame++;
		if (nSec != DateTime::Now.Second) {
			nSec = DateTime::Now.Second;
			nFps = nFrame;
			nFrame = 0;
		}
		dev->RenderState->ZBufferEnable = true;
		dev->RenderState->ZBufferWriteEnable = true;
		// カメラの設定
		float fTargetX = (float)Math::Sin(fCamDir) * fDist;
		float fTargetZ = (float)Math::Cos(fCamDir) * fDist - fDist;
		dev->Transform->View = Matrix::LookAtLH(
			Vector3(0.0f, fAlt,-fDist),
			Vector3(fTargetX, 0.0f,  fTargetZ),
			Vector3(0.0f, 1.0f,  0.0f));
		dev->Transform->Projection = Matrix::PerspectiveFovLH((float)Math::PI / 4,
			(float)ClientSize.Width / (float)ClientSize.Height, 1.0f, 1000.0f);
 
		// ライトの設定
		dev->Lights[0]->Direction = Vector3::Normalize(Vector3(-1, -2, 3));
		dev->Lights[0]->Type = LightType::Directional;
		dev->Lights[0]->Diffuse = Color::FromArgb(0xff,0xff,0xff);
		dev->Lights[0]->Ambient = Color::FromArgb(0x28,0x28,0x28);
		dev->Lights[0]->Enabled = true;
		dev->Lights[0]->Update();
 
		dev->Clear(ClearFlags::Target | ClearFlags::ZBuffer, 0x7f7fff, 1.0f, 0);
		dev->BeginScene();
 
		// メッシュ描画
		for (int n = -10; n <= 10; n++) {
			DrawMesh(ColorValue(1.0f, 0.0f, 1.0f), Vector3(4.0f*n, 0, 0));
			DrawMesh(ColorValue(1.0f, 1.0f, 0.0f), Vector3(4.0f*n, 0, 4));
			DrawMesh(ColorValue(0.0f, 1.0f, 1.0f), Vector3(4.0f*n, 0, 8));
		}
 
		font->DrawText(nullptr, "fps=" + nFps, 0, 0, Color::White);
		dev->EndScene();
		try {
			dev->Present();
		}
		catch (DeviceLostException^) {
			ResetDevice();
		}
	}
private:
	void CreateMesh()
	{
		mesh = Mesh::Box(dev, 2, 2, 2);
	}
 
	void DrawMesh(ColorValue color, Vector3 v)
	{
		Material mat;
		mat.AmbientColor = color;
		mat.DiffuseColor = color;
		dev->Material = mat;
		dev->Transform->World = Matrix::Translation(v);
		mesh->DrawSubset(0);
	}
 
	void CreateFont()
	{
		FontDescription fd;
		fd.Height = 20;
		fd.FaceName = "MS ゴシック";
		font = gcnew Direct3D::Font(dev, fd);
	}
 
	void ResetDevice()
	{
		int hResult;
		if (!dev->CheckCooperativeLevel(hResult)) {
			switch (hResult) {
			case ResultCode::DeviceLost:
				Thread::Sleep(10);
				break;
			case ResultCode::DeviceNotReset:
				dev->Reset(pp);
				break;
			}
		}
	}
 
	void OnKeyDown(Object^ sender, KeyEventArgs^ e)
	{
		switch (e->KeyCode) {
		case Keys::W:
			fDist -= 0.5f;
			break;
		case Keys::S:
			fDist += 0.5f;
			break;
		case Keys::Up:
			fAlt += 0.5f;
			break;
		case Keys::Down:
			fAlt -= 0.5f;
			break;
		case Keys::Left:
			fCamDir -= 5 * (float)Math::PI / 180;
			break;
		case Keys::Right:
			fCamDir += 5 * (float)Math::PI / 180;
			break;
		case Keys::Escape:
			Close();
			break;
		}
	}
};
 
int main()
{
	MainForm^ form = gcnew MainForm;
	if (!form->DXInitialize()) {
		MessageBox::Show("Direct3Dの初期化に失敗しました。", "ClrD3D");
		return 0;
	}
	form->Show();
	while (form->Created) {
		form->Render();
		Application::DoEvents();
	}
	return 0;
}
 
最終更新:2012年11月06日 20:49