![]() |
Multimedia »
General Graphics »
General
Beginner
License: The Code Project Open License (CPOL)
3DHelperBy MikeTheDwarfHelper class to display 3D data |
C++, C++/CLI, Windows, Win32, Visual Studio, MFC, GDI, OpenGL
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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 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.
To allow the API to help you, it needs some basic steps to be performed:
CMF3DHelper to your project. CMF3DHelper::Initialize() function of your variable. (see 1.)Initialize() function needs parameters:
vmin and vmax. bSupportTrackBall) to tell the API whether to perform rotation controlled by mouse or not. 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
initialize();
The initialize function itself is implemented some lines below.
Steps performed:
// 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"
m_c3DHelper.UpdateTrackBall
(GetDlgItem(IDC_STATIC_PLACEHOLDER)->GetDC());
redraw();
SetTimer(1, 25, NULL); // Start timer for next redrawing round
}
CDialog::OnTimer(nIDEvent);
}
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.
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 17 Feb 2009 Editor: Deeksha Shenoy |
Copyright 2009 by MikeTheDwarf Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |