Introduction
This article presents a very clean and simple method to render in 3D using Managed DirectX, Windows Forms and C++ (Managed C++ in this case). The application is a simple 3D Point viewer where the points are obtained from a DataGridView
that allows user editing and multiline Copy/Paste.
The November 2008 DirectX SDK download includes complete support for MC++ development: all DirectX API has been fully translated to MC++. This has strong implications for those who use C++ language and want .NET functionality and DirectX functionality.
Background
This article assumes the reader has some knowledge of using Microsoft Visual Studio, a Managed C++ background and some basic concepts about Windows Forms.
Using the Code
The project is created with Visual Studio 2008 and you will also need the DirectX SDK November 2008 installed.
Please, remember to check if the references Microsoft.DirectX
and Microsoft.DirectX.Direct3D
are included in the project (see Project-> Properties-> Common Properties -> FrameWork and References).
The next code is a cut extracted from the Render
routine in the example program. The data source for the 3D points rendering is an array of points called points
(of type array<CustomVertex::PositionColored, 1>
). Before rendering the points, it is necessary to setup the viewing parameters as follows:
device->Clear(ClearFlags::Target | ClearFlags::ZBuffer,
System::Drawing::Color::Black, 1.0f, 0);
device->BeginScene();
Vector3 CameraPos(Bmaxx, Bmaxy, Bmaxz);
Vector3 Center((Bmaxx+Bminx)/2, (Bmaxy+Bminy)/2,(Bmaxz+Bminz)/2);
CameraPos = CameraPos + (CameraPos-Center);
device->Transform->World =
Matrix::RotationZ(Environment::TickCount / 2500.0f); device->Transform->Projection =
Matrix::OrthoLH(8.0f, 8.0f, 0.1f, 1000.0f); device->Transform->View =
Matrix::LookAtLH(CameraPos,Center, Vector3(0.0f, 0.0f, 1.0f));
device->RenderState->Lighting = false;
device->RenderState->ZBufferEnable = true;
device->SetRenderState(Direct3D::RenderStates::PointSize, 2.0f);
device->VertexFormat = CustomVertex::PositionColored::Format;
device->DrawUserPrimitives(PrimitiveType::PointList, points->Length, points);
device->EndScene();
device->Present();
The data of the array points
is extracted from a DataGridView
using this simple procedure:
try {
for (i = 0; i < points->Length; i++)
{
points[i].X = (float)System::Convert::ToDouble(dataGridView1[0,i]->Value);
points[i].Y = (float)System::Convert::ToDouble(dataGridView1[1,i]->Value);
points[i].Z = (float)System::Convert::ToDouble(dataGridView1[2,i]->Value);
points[i].Color = System::Drawing::Color::Red.ToArgb();
}
}
catch ( Exception^ exp )
{
MessageBox::Show( "Cell data format error: Row("+ i +")," +
exp->Message, "Error", MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
return;
}
Points of Interest
- Mixing C++, DirectX and Windows Forms
History