/*
プロジェクトへの参照の追加(参照タブ)
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;
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";
}
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();
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 fCamPosX = (float)Math::Sin(Environment::TickCount / 600.0f) * 8.0f;
float fCamPosZ = (float)Math::Cos(Environment::TickCount / 600.0f) * 8.0f;
dev->Transform->View = Matrix::LookAtLH(
Vector3(fCamPosX, 3.0f, fCamPosZ),
Vector3(0.0f, 0.0f, 0.0f),
Vector3(0.0f, 1.0f, 0.0f));
dev->Transform->Projection = Matrix::PerspectiveFovLH((float)Math::PI / 4,
(float)ClientSize.Width / ClientSize.Height, 3.0f, 100.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();
// マテリアルの設定
Material mat;
dev->Clear(ClearFlags::Target | ClearFlags::ZBuffer, 0x7f7fff, 1.0f, 0);
dev->BeginScene();
// シアン
mat.AmbientColor = ColorValue(0.0f, 1.0f, 1.0f);
mat.DiffuseColor = ColorValue(0.0f, 1.0f, 1.0f);
dev->Material = mat;
dev->Transform->World =
Matrix::RotationY(Environment::TickCount / 3000.0f) *
Matrix::Translation(0, 0, -3);
mesh->DrawSubset(0);
// 白
mat.AmbientColor = ColorValue(1.0f, 1.0f, 1.0f);
mat.DiffuseColor = ColorValue(1.0f, 1.0f, 1.0f);
dev->Material = mat;
dev->Transform->World = Matrix::RotationX(Environment::TickCount / 3000.0f);
mesh->DrawSubset(0);
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 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;
}
}
}
};
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;
}