Click here to Skip to main content
Licence GPL3
First Posted 26 Mar 2009
Views 12,960
Downloads 283
Bookmarked 10 times

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

By | 26 Mar 2009 | Article
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:

/// <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: 

/// <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)

About the Author

ManelF



Spain Spain

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat job Pinmemberjoseph74it9:15 13 Jun '09  
GeneralNice Clean Interface PinmemberJeffrey Walton12:22 26 Mar '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 26 Mar 2009
Article Copyright 2009 by ManelF
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid