/*★ Direct 3D で Torus を描画 前田 稔 ★*/ #using #using #using #using #using #using using namespace System; using namespace System::Windows::Forms; using namespace System::Drawing; using namespace Microsoft::DirectX; using namespace Microsoft::DirectX::Direct3D; ref class D3DModel : public System::Windows::Forms::Form { Device ^_device; // rendering device Mesh ^_mesh; // mesh object bool pause; // rendering flag public: // Constructor D3DModel() { _device = nullptr; _mesh = nullptr; pause = false; // Window のサイズを設定 this->ClientSize = System::Drawing::Size(400,400); // caption の設定 this->Text = "Direct3D Model"; } bool InitializeGraphics() { PresentParameters ^presentParams = gcnew PresentParameters(); presentParams->Windowed = true; presentParams->SwapEffect = SwapEffect::Discard; presentParams->EnableAutoDepthStencil = true; presentParams->AutoDepthStencilFormat = DepthFormat::D16; // Create the D3DDevice _device = gcnew Device(0, DeviceType::Hardware, this, CreateFlags::SoftwareVertexProcessing, presentParams); if (_device==nullptr) { MessageBox::Show("Create Direct3D Device Error."); return false; } _device->DeviceReset += gcnew System::EventHandler(this, &D3DModel::OnResetDevice); this->OnResetDevice(_device, nullptr); pause = false; return true; } void OnResetDevice(Object^ sender, EventArgs^ e) { _device->RenderState->ZBufferEnable = true; // Turn on the zbuffer _device->RenderState->Lighting = true; // make sure lighting is enabled //☆ トーラスメッシュの生成 _mesh = Mesh::Torus(_device, 16.0f, 40.0f, 32, 32); } void SetupMatrices() { _device->Transform->World = Matrix::RotationY(Environment::TickCount / 1000.0f); _device->Transform->View = Matrix::LookAtLH(Vector3(0.0f, 3.0f, -200.0f), Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f)); _device->Transform->Projection = Matrix::PerspectiveFovLH((float)(Math::PI/4), 1.0f, 1.0f, 500.0f); } void SetupLights() { System::Drawing::Color col = System::Drawing::Color::White; Direct3D::Material mtrl = Direct3D::Material(); mtrl.Diffuse = col; mtrl.Ambient = col; _device->Material = mtrl; _device->Lights[0]->Type = LightType::Directional; _device->Lights[0]->Diffuse = System::Drawing::Color::Yellow; //☆ ライトの座標を設定する _device->Lights[0]->Direction = Vector3(50.0f, -40.0f, 100.0f); _device->Lights[0]->Enabled = true; _device->RenderState->Ambient = System::Drawing::Color::FromArgb(0x202020); } public: // OnPaint() から呼ばれる描画処理 void Render() { if (_device==nullptr) return; if (pause) return; _device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, System::Drawing::Color::Blue, 1.0f, 0); _device->BeginScene(); // Rendering of scene objects can happen here SetupLights(); SetupMatrices(); _mesh->DrawSubset(0); _device->EndScene(); _device->Present(); } // Window の描画 OnPaint() を override virtual void OnPaint(PaintEventArgs ^e) override { this->Render(); // Render on painting } // OnKeyPress() を override virtual void OnKeyPress(System::Windows::Forms::KeyPressEventArgs ^e) override { if ((int)e->KeyChar == (int)System::Windows::Forms::Keys::Escape) this->Close(); // Esc was pressed } // OnResize() を override virtual void OnResize(System::EventArgs ^e) override { pause = ((this->WindowState == FormWindowState::Minimized) || !this->Visible); } }; //☆ Main() メソッド int main() { D3DModel ^frm; frm = gcnew D3DModel(); if (frm) { if (!frm->InitializeGraphics()) { MessageBox::Show("Could not initialize Direct3D."); return 0; } frm->Show(); // メッセージループ while(frm->Created) { frm->Render(); Application::DoEvents(); } } return 0; }