Click here to Skip to main content
15,892,480 members
Articles / Desktop Programming / ATL

3D Graph ActiveX Control

Rate me:
Please Sign up or sign in to vote.
4.90/5 (100 votes)
2 Aug 2003MIT3 min read 785.4K   50.1K   284  
An ATL/STL ActiveX control based on OpenGL library for 3D data visualization
// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#include <math.h>
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog

CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemoDlg)
	m_bProjection = FALSE;
	m_bRandom = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_COMBO_MODE, m_cmbTrackMode);
	DDX_Control(pDX, IDC_NTGRAPH3D1, m_Graph3D);
	DDX_Check(pDX, IDC_CHECK_PROJ, m_bProjection);
	DDX_Check(pDX, IDC_CHECK_RANDOM, m_bRandom);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK_PROJ, OnCheckProjection)
	ON_BN_CLICKED(IDC_BUTTON_PROP, OnButtonProperties)
	ON_CBN_CLOSEUP(IDC_COMBO_MODE, OnCloseupComboMode)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_CHECK_RANDOM, OnCheckRandom)
	ON_BN_CLICKED(IDC_COPY_TO_CLIPBOARD, OnCopyToClipboard)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

BOOL CDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_cmbTrackMode.SetCurSel(0);
	UpdateData(FALSE);
	OnCheckRandom() ;
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDemoDlg::OnCheckProjection() 
{
	UpdateData();
	if (m_bProjection) {
		m_Graph3D.SetProjection (1);
	    m_Graph3D.SetCaption ("Orthographic");
	} else {
		m_Graph3D.SetProjection (0);
		m_Graph3D.SetCaption ("Perspective");
	}	
}

void CDemoDlg::OnButtonProperties() 
{
	m_Graph3D.ShowPropertyPages();	
}



void CDemoDlg::OnCloseupComboMode() 
{
    m_Graph3D.SetTrackMode(m_cmbTrackMode.GetCurSel());	
}



void CDemoDlg::OnButton1() 
{
	m_Graph3D.SetCaption ("Torus");
	m_Graph3D.ClearGraph();
	m_Graph3D.AddElement();

    m_Graph3D.SetElementLineColor(0, RGB(255,0,0));
	m_Graph3D.SetElementType(0, 3); // draw surface

	double x,y,z,ti,tj;

	for (int i = 0; i < 41; i++)
	{
	    ti = (i - 20.0)/20.0 * 3.15;
	
		for (int j = 0; j < 41 ; j++) 
		{
		    tj = (j - 20.0)/20.0 * 3.15;

			x = (cos(tj) + 3.0) * cos(ti);
			y = sin(tj);
			z = (cos(tj) + 3.0) * sin(ti);

			m_Graph3D.PlotXYZ(x,y,z,0);
		}
	}

	m_Graph3D.SetRange (-4, 4, -1, 1, -4, 4);
}

void CDemoDlg::OnButton2() 
{
    m_Graph3D.SetCaption ("y(x,z) = sin(x)*cos(z)");
	
	double x,y,z;

	m_Graph3D.ClearGraph();

	m_Graph3D.AddElement();
	
	for (int i = 1; i < 40; i++)
	{
		x = ((i - 20.0) / 20.0) * 3.15;
		
		for (int j = 1; j < 40; j++)
		{
    
			m_Graph3D.AddElement();

			z = ((j - 20.0) / 20.0) * 3.15;

			y = sin(x) * cos(z) + 2.0;

			m_Graph3D.SetElementType(i, 0);
			m_Graph3D.SetElementLineColor(i, RGB(255,0,0));
 			m_Graph3D.PlotXYZ (x, y, z, j);
		} 
	}

	m_Graph3D.SetRange (-4, 4, 1, 3, -4, 4);	
}

void CDemoDlg::OnButton3() 
{

	m_Graph3D.ClearGraph();
	m_Graph3D.SetCaption("y(x,z) = 8*exp{-(x*x + z*z)/4}");

	double data,t[21];
		
	for (int i = 0; i < 21; i++)
		t[i] = (i / 20.0 - 0.5) * 8;

	for (i = 0; i < 21; i++) 
		for (int j = 0; j < 21; j++) 
		{
			data = 8 * exp(-(t[i] * t[i] + t[j] * t[j]) / 4);
			m_Graph3D.AddElement();
			m_Graph3D.SetElementType(i,2);
			m_Graph3D.SetElementPointColor(i, RGB(0,0,255));
            m_Graph3D.SetElementLineColor(i, RGB(255,0,0));
			m_Graph3D.SetElementPointSize(i, 4.5);
			m_Graph3D.PlotXYZ(t[i],data,t[j],i);
		}
	
	m_Graph3D.AutoRange();	
	
}
	


void CDemoDlg::OnTimer(UINT nIDEvent) 
{
	m_Graph3D.SetCaption ("Random");
	
	m_Graph3D.ClearGraph();

	double data;
	int r,g,b;

	
	UpdateData(TRUE);

	for (long i = 0; i < 30; i++)
		for (long j = 0; j < 30; j++)
		{
			data = rand()/32767.0;
			
			r = (int)(rand()/32767.0) * 255;
			g = (int)(rand()/32767.0) * 255;
			b = (int)(rand()/32767.0) * 255;

			m_Graph3D.AddElement();

			m_Graph3D.SetElementType(i,2);

			m_Graph3D.SetElementLineColor (i,RGB(r,g,b));
			
			m_Graph3D.PlotXYZ(i,data,j,i);
			
		}

	m_Graph3D.AutoRange();	

	CDialog::OnTimer(nIDEvent);
}

void CDemoDlg::OnCheckRandom() 
{
	UpdateData(TRUE);

	if (m_bRandom)
		SetTimer(1, 50, NULL);	
	else
		KillTimer(1);
}



void CDemoDlg::OnCopyToClipboard() 
{

	m_Graph3D.CopyToClipboard();
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


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

Comments and Discussions