Click here to Skip to main content
15,867,832 members
Articles / Multimedia / DirectX

Windows Forms, Managed DirectX and C++. All In One

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
26 Mar 2009GPL31 min read 39.2K   1K   11   2
This project shows with an example how to combine Managed C++, Managed DirectX and Windows Forms.
Simple_XYZ_Point_Viewer.JPG

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:

C++
/// <summary>
/// Render the 3D points vector.
/// </summary>   
//Clear the backbuffer and color
device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, 
		System::Drawing::Color::Black, 1.0f, 0);
        
//Begin the scene
device->BeginScene();

// View definitions		
Vector3 CameraPos(Bmaxx, Bmaxy, Bmaxz);
Vector3 Center((Bmaxx+Bminx)/2, (Bmaxy+Bminy)/2,(Bmaxz+Bminz)/2);
CameraPos = CameraPos + (CameraPos-Center);

// View
device->Transform->World = 
	Matrix::RotationZ(Environment::TickCount / 2500.0f); // a rotating world
device->Transform->Projection = 
	Matrix::OrthoLH(8.0f, 8.0f, 0.1f, 1000.0f); // field of view
device->Transform->View = 
	Matrix::LookAtLH(CameraPos,Center,  Vector3(0.0f, 0.0f, 1.0f)); // point of view

//Lighting
device->RenderState->Lighting = false;
device->RenderState->ZBufferEnable = true;

// Point size
device->SetRenderState(Direct3D::RenderStates::PointSize, 2.0f);
		
// Rendering of scene objects
device->VertexFormat = CustomVertex::PositionColored::Format;
device->DrawUserPrimitives(PrimitiveType::PointList, points->Length, points);

//End the scene
device->EndScene();
device->Present();		 

The data of the array points is extracted from a DataGridView using this simple procedure: 

C++
/// <summary>
/// Update points vector from dataGridView1.
/// </summary>
try // using try to control data conversion errors
{
    // actualize points
    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 ) 
{
    // Cell data format error
    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

  • Version 1.0 24-03-2009

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGreat job Pin
joseph74it13-Jun-09 9:15
joseph74it13-Jun-09 9:15 
GeneralNice Clean Interface Pin
Jeffrey Walton26-Mar-09 12:22
Jeffrey Walton26-Mar-09 12:22 
Hi Manuel,

The program looks nice. You might consider going over the article formatting, such as removing the extraneous white space in the code's left marging. Others might rough you up for it.

Jeff

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.