Click here to Skip to main content
15,179,476 members
Articles / Desktop Programming / MFC
Posted 17 Feb 2009


44 bookmarked


Rate me:
Please Sign up or sign in to vote.
4.61/5 (9 votes)
17 Feb 2009CPOL3 min read
Helper class to display 3D data


Ever had the problem to display 3D-related data? E.g. to display a 3D-curve in a Windows dialog window? Don't like to implement a whole DirectX/OpenGL class hierarchy?
There is a solution: The 3DHelper class!

More tools can be found on my homepage.


If you are familiar with 3D programming, you already might have used free/not so free libraries to perform the task of rendering 3D objects onto your screen.
The big (in my opinion!) drawback of that approach is: your code is getting more and more complicated to maintain and even understand. Not to mention portability.

Having just some files to be included into your project, performing all the necessary math-stuff to "get  3D" sounds too nice to be true? Now its real!

The Solution

The aim of the API described in this article is simply to help you to project 3D data into your 2D window.
Given a 3D point (e.g. a point being part of a nurbs-surface, a 3D function value, etc.) the API transforms this point and returns a coordinate pair representing a dot in your window. 

And the best - there is no sophisticated OpenGL/DirectX stuff involved. This is a pure software solution.

Using the Code 

To allow the API to help you, it needs some basic steps to be performed:

  1. Add a variable of type CMF3DHelper to your project.
  2. At start up (and after each resize event) call the CMF3DHelper::Initialize() function of your variable. (see 1.)
    This Initialize() function needs parameters:
    • A pointer to the device context (DC) of your window (used to get the window size)
    • The "cube" representing your data, represented by two vectors: vmin and vmax.
      All of your data points should lay inside that cube. Imagine something like a bounding cube, covering all your data points.
      Example in 2D: you need to display a curve of form y=sin(x) where x is between [-PI] and [+PI]. The result of that function is in range of [-1] up to [+1].
      Therefore you have to define the bounding box (2D!) using two corners:
      (-1, -PI) which is top left and 
      (+1, +PI) similar to bottom right.
    • A flag (bSupportTrackBall) to tell the API whether to perform rotation controlled by mouse or not. 
  3. For each data point to be calculated, call the RenderPoint() function. It returns the corresponding screen(=window) coordinates of your 3D data point.

The best to do is to check 3DHelperDemoSimple project. I reduced the code to the minimum (no checking code) to clarify the steps.
Open the 3DHelperDemoSimple project, and select the OnInitDialog method.

Only the lines below are added to the code generated by the wizard:    

// Initialize the 3D system

The initialize function itself is implemented some lines below.
Steps performed:

  1. Initialize the API.
  2. Start the update timer, used to refresh the mouse (=rotation) information. In our case, the whole scene will be repainted each 25 milliseconds.
// Set up initial 3D system
void CMy3DHelperDemoSimpleDlg::initialize()
	CWnd *pTarget = GetDlgItem(IDC_STATIC_PLACEHOLDER);
	m_c3DHelper.Initialize(pTarget->GetDC(), NeHe::Vector(-2.0f, -2.0f, -2.0f), 
		NeHe::Vector(2.0f,2.0f,2.0f), TRUE);

	// Timer used to refresh "trackball" information
	SetTimer(1, 25, NULL);

Triggered by the timer, every 25 ms the OnTimer function will be entered.
Inside, the trackball position will be updated (UpdateTrackBall, which again needs the current DC as parameter), and the scene will be redrawn (redraw()).

void CMy3DHelperDemoSimpleDlg::OnTimer(UINT nIDEvent) 
	// Timer is used to refresh trackball information
	if(nIDEvent == 1) {
		KillTimer(nIDEvent); // Prevent timer "overlapping"
		SetTimer(1, 25, NULL); // Start timer for next redrawing round

Redraw again is trivial. Just take your data point per point, call RenderPoint() for each of them and display the result using e.g. SetPixel() function.

The 3DHelperDemo project implements a more complex example.

Points of Interest 

I decided not to implement the whole math stuff by myself (think that is not a unique task...). So I have taken some matrix/vector code from NeHe (a GREAT site related to OpenGL, got my 5!).

To get a better idea on how OpenGL is implemented, I took a closer look at the free mesa implementation. This is a must for everyone interested in 3D implementation! 


Any feedback would be appreciated!

See more tools and updates on SoftwareHive.


  • 2009/02/14 - Initial release


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Business Analyst UPC AT B2B customers
Austria Austria
No Biography provided

Comments and Discussions

QuestionMemory deallocation problem. Pin
mrsilver9-Apr-19 2:57
Membermrsilver9-Apr-19 2:57 
GeneralReading Pin
mateusz.matyaszek8-Jun-10 23:24
Membermateusz.matyaszek8-Jun-10 23:24 
GeneralRe: Reading Pin
MikeTheDwarf9-Jun-10 2:14
MemberMikeTheDwarf9-Jun-10 2:14 
GeneralRe: Reading Pin
mateusz.matyaszek22-Jun-10 22:34
Membermateusz.matyaszek22-Jun-10 22:34 
GeneralRe: Reading Pin
MikeTheDwarf23-Jun-10 1:59
MemberMikeTheDwarf23-Jun-10 1:59 
Questionuse in Pin
GeneralBiSoN28-Feb-09 0:21
MemberGeneralBiSoN28-Feb-09 0:21 
AnswerRe: use in Pin
MikeTheDwarf1-Mar-09 22:12
MemberMikeTheDwarf1-Mar-09 22:12 
GeneralRe: use in [modified] Pin
GeneralBiSoN4-Mar-09 9:14
MemberGeneralBiSoN4-Mar-09 9:14 
YES you are right, a complete conversion to is a performance DESASTER!
Thats why there is NOTHING really usefull for VB in the whole WWW.

BUT i can a little bit C#, the fusion of the POWER from c++ and the easynes from VB
Now i'm working on it to modify this Article to work like your code.

Rendering terrains with Managed DirectX[^]

But its REALLY hard because i have not the Brain for 3D-Movement.
Your code has ALL I NEED in movement and performance, the C# project is editable for me.

To make a fusion is impossible (for me) i think.

So i have to play(deal) with it...!

May be you have a look at this article^^

Its a Read Only Property in the universe that COOL-STUFF starts with "C" Laugh | :laugh: .

modified on Wednesday, March 4, 2009 2:27 PM

GeneralRe: use in Pin
MikeTheDwarf4-Mar-09 9:34
MemberMikeTheDwarf4-Mar-09 9:34 
GeneralRe: use in Pin
GeneralBiSoN5-Mar-09 6:16
MemberGeneralBiSoN5-Mar-09 6:16 
GeneralRe: use in Pin
MikeTheDwarf5-Mar-09 9:27
MemberMikeTheDwarf5-Mar-09 9:27 
GeneralSounds nice Pin
john wallis17-Feb-09 12:20
Memberjohn wallis17-Feb-09 12:20 
GeneralRe: Sounds nice Pin
MikeTheDwarf18-Feb-09 0:47
MemberMikeTheDwarf18-Feb-09 0:47 
GeneralRe: Sounds nice Pin
john wallis18-Feb-09 13:22
Memberjohn wallis18-Feb-09 13:22 
GeneralRe: Sounds nice Pin
john wallis18-Feb-09 13:36
Memberjohn wallis18-Feb-09 13:36 
AnswerRe: Sounds nice Pin
MikeTheDwarf19-Feb-09 7:54
MemberMikeTheDwarf19-Feb-09 7:54 
JokeRe: Sounds nice Pin
john wallis20-Feb-09 1:34
Memberjohn wallis20-Feb-09 1:34 
GeneralRe: Sounds nice Pin
MikeTheDwarf20-Feb-09 6:46
MemberMikeTheDwarf20-Feb-09 6:46 

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.