#include <Windows.h> using namespace System; using namespace System::Windows::Forms; using namespace System::Drawing; using namespace SlimDX; using namespace SlimDX::Direct3D9; struct ColorVertex { float x, y, z; int color; }; public ref class WndForm : public Form { Direct3D^ direct3D; Device^ device; PresentParameters^ presentParams; VertexBuffer^ vb; public: WndForm() { this->ClientSize = System::Drawing::Size(400, 300); this->StartPosition = FormStartPosition::CenterScreen; this->Text = "D3D Tutorial 4"; } ~WndForm() { this->!WndForm(); } !WndForm() { if (direct3D) delete direct3D; if (device) delete device; if (vb) delete vb; } bool InitD3D() { try { direct3D = gcnew Direct3D(); presentParams = gcnew PresentParameters(); presentParams->Windowed = true; presentParams->DeviceWindowHandle = this->Handle; presentParams->SwapEffect = SwapEffect::Discard; presentParams->BackBufferFormat = Format::X8R8G8B8; device = gcnew Device(direct3D, 0, DeviceType::Hardware, this->Handle, CreateFlags::SoftwareVertexProcessing | CreateFlags::Multithreaded, presentParams); device->SetRenderState(RenderState::CullMode, Cull::None); device->SetRenderState(RenderState::Lighting, false); vb = gcnew VertexBuffer(device, 3 * sizeof(ColorVertex), Usage::WriteOnly, VertexFormat::Position | VertexFormat::Diffuse, Pool::Default); DataStream^ stream = vb->Lock(0, 0, LockFlags::None); ColorVertex vertices[] = { { -1.0f,-1.0f, 0.0f, 0xffff0000, }, { 1.0f,-1.0f, 0.0f, 0xff0000ff, }, { 0.0f, 1.0f, 0.0f, 0xffffffff, }, }; stream->WriteRange(IntPtr(vertices), 3 * sizeof(ColorVertex)); vb->Unlock(); return true; } catch (Direct3D9Exception^) { return false; } } void SetupMatrix() { int dTime = Environment::TickCount % 1000; float angle = dTime * (2.0f * (float)Math::PI) / 1000.0f; device->SetTransform(TransformState::World, Matrix::RotationY(angle)); device->SetTransform(TransformState::View, Matrix::LookAtLH(Vector3(0.0f, 3.0f, -5.0f), Vector3(0.0f, 0.0f, 0.0f), Vector3(0.0f, 1.0f, 0.0f))); device->SetTransform(TransformState::Projection, Matrix::PerspectiveFovLH((float)Math::PI / 4, 1.0f, 1.0f, 100.0f)); } void Render() { if (!device) return; device->Clear(ClearFlags::Target, Color::DarkGray, 1.0f, 0); device->BeginScene(); SetupMatrix(); device->SetStreamSource(0, vb, 0, sizeof(ColorVertex)); device->VertexFormat = VertexFormat::Position | VertexFormat::Diffuse; device->DrawPrimitives(PrimitiveType::TriangleList, 0, 1); device->EndScene(); device->Present(); } }; INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT) { WndForm^ frm = gcnew WndForm(); if (!frm->InitD3D()) { MessageBox::Show("Initializing D3D Device Failed"); return 0; } frm->Show(); while (frm->Created) { frm->Render(); Application::DoEvents(); } delete frm; return 0; }
0 개의 댓글:
댓글 쓰기