|開発環境|Microsoft Visual C++ 2010 Express (SP1)| |実行環境|Microsoft Windows XP Home Edition (SP3)| |プロジェクトの種類|空の CLR プロジェクト| |プロジェクト名|ClrD3D| 参考 -[[三角形を描く>http://www.atelier-blue.com/program/mdirectx/3d/3d01-06.htm]] ClrD3D_7.cpp #highlight(cpp){{ /* プロジェクトへの参照の追加(参照タブ) C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0 Microsoft.DirectX.dll Microsoft.DirectX.Direct3D.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::Windows::Forms; using namespace Microsoft::DirectX; using namespace Microsoft::DirectX::Direct3D; #define RGB(r,g,b) (((r)<<16) | ((g)<<8) | (b)) ref class MainForm : Form { private: Device^ dev; VertexBuffer^ vb; public: MainForm() { MinimumSize = Drawing::Size(160, 120); MaximizeBox = false; ClientSize = Drawing::Size(640, 480); Text = "ClrD3D"; } bool DXInitialize() { try { PresentParameters^ pp = gcnew PresentParameters; pp->Windowed = true; pp->SwapEffect = SwapEffect::Discard; dev = gcnew Device(0, DeviceType::Hardware, this, CreateFlags::HardwareVertexProcessing, pp); CreateVertex(); return true; } catch (Exception^) { return false; } } void Render() { if (dev == nullptr) return; dev->Clear(ClearFlags::Target, Color::Black, 1.0f, 0); dev->BeginScene(); dev->SetStreamSource(0, vb, 0); dev->VertexFormat = CustomVertex::TransformedColored::Format; dev->DrawPrimitives(PrimitiveType::TriangleFan, 0, 360); dev->EndScene(); dev->Present(); } private: void CreateVertex() { vb = gcnew VertexBuffer(CustomVertex::TransformedColored::typeid, 362, dev, (Usage)0, CustomVertex::TransformedColored::Format, Pool::Managed); GraphicsStream^ gs = vb->Lock(0, 0, (LockFlags)0); // 頂点データ array<CustomVertex::TransformedColored>^ v = gcnew array<CustomVertex::TransformedColored>(362); v[0].X = 250; v[0].Y = 250; v[0].Z = 0.5f; v[0].Rhw = 1; v[0].Color = Color::White.ToArgb(); for (int n = 0; n <= 360; ) { double d = n * Math::PI / 180; double dX = 250 + 200 * Math::Sin(d); double dY = 250 - 200 * Math::Cos(d); int nColor; int nInc = 0xff * (n % 60) / 60; int nDec = 0xff - nInc; switch ((n/60) % 6) { case 0: // 0-59,360 緑→水 nColor = RGB(0, 0xff, nInc); break; case 1: // 60-119 水→青 nColor = RGB(0, nDec, 0xff); break; case 2: // 120-179 青→紫 nColor = RGB(nInc, 0, 0xff); break; case 3: // 180-239 紫→赤 nColor = RGB(0xff, 0, nDec); break; case 4: // 240-299 赤→黄 nColor = RGB(0xff, nInc, 0); break; case 5: // 300-359 黄→緑 nColor = RGB(nDec, 0xff, 0); break; } n++; v[n].X = (float)dX; v[n].Y = (float)dY; v[n].Z = 0.5f; v[n].Rhw = 1; v[n].Color = nColor; } gs->Write(v); vb->Unlock(); } }; 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; } }}