Click here to Skip to main content
15,892,517 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.6K   50.1K   284  
An ATL/STL ActiveX control based on OpenGL library for 3D data visualization
// TorusDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Torus.h"
#include "TorusDlg.h"

#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CTorusDlg dialog

CTorusDlg::CTorusDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTorusDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTorusDlg)
	m_bProjection = FALSE;
	m_nDefformation = 100;
	m_nAmbient = 60;
	m_nAmbMat = 0;
	m_nDiffMat = 0;
	m_nDiffuse = 20;
	m_nEmission = 10;
	m_nShine = 0;
	m_nSpecMat = 0;
	m_nSpecular = 0;
	m_nPosX = 37;
	m_nPosY = 100;
	m_nPosZ = 46;
	m_strAmbient = _T("");
	m_strAmbMat = _T("");
	m_strDiffMat = _T("");
	m_strDiffuse = _T("");
	m_strEmission = _T("");
	m_strShine = _T("");
	m_strSpecMat = _T("");
	m_strSpecular = _T("");
	m_strPosX = _T("");
	m_strPosY = _T("");
	m_strPosZ = _T("");
	m_bFill = FALSE;
	m_bFlat = FALSE;
	m_bLights = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTorusDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTorusDlg)
	DDX_Control(pDX, IDC_COMBO_MODE, m_cmbTrackMode);
	DDX_Check(pDX, IDC_CHECK_PROJ, m_bProjection);
	DDX_Control(pDX, IDC_NTGRAPH3D1, m_Graph3D);
	DDX_Slider(pDX, IDC_DEFFORMATION, m_nDefformation);
	DDX_Slider(pDX, IDC_AMBIENT, m_nAmbient);
	DDX_Slider(pDX, IDC_AMBMAT, m_nAmbMat);
	DDX_Slider(pDX, IDC_DIFFMAT, m_nDiffMat);
	DDX_Slider(pDX, IDC_DIFFUSE, m_nDiffuse);
	DDX_Slider(pDX, IDC_EMISSION, m_nEmission);
	DDX_Slider(pDX, IDC_SHINE, m_nShine);
	DDX_Slider(pDX, IDC_SPECMAT, m_nSpecMat);
	DDX_Slider(pDX, IDC_SPECULAR, m_nSpecular);
	DDX_Slider(pDX, IDC_XPOS, m_nPosX);
	DDX_Slider(pDX, IDC_YPOS, m_nPosY);
	DDX_Slider(pDX, IDC_ZPOS, m_nPosZ);
	DDX_Text(pDX, IDC_AMB_TEXT, m_strAmbient);
	DDX_Text(pDX, IDC_AMBMAT_TEXT, m_strAmbMat);
	DDX_Text(pDX, IDC_DIFFMAT_TEXT, m_strDiffMat);
	DDX_Text(pDX, IDC_DIFFUSE_TEXT, m_strDiffuse);
	DDX_Text(pDX, IDC_EMISSION_TEXT, m_strEmission);
	DDX_Text(pDX, IDC_SHINE_TEXT, m_strShine);
	DDX_Text(pDX, IDC_SPECMAT_TEXT, m_strSpecMat);
	DDX_Text(pDX, IDC_SPECULAR_TEXT, m_strSpecular);
	DDX_Text(pDX, IDC_XPOS_TEXT, m_strPosX);
	DDX_Text(pDX, IDC_YPOS_TEXT, m_strPosY);
	DDX_Text(pDX, IDC_ZPOS_TEXT, m_strPosZ);
	DDX_Check(pDX, IDC_FILL, m_bFill);
	DDX_Check(pDX, IDC_FLAT, m_bFlat);
	DDX_Check(pDX, IDC_LIGHTS, m_bLights);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTorusDlg, CDialog)
	//{{AFX_MSG_MAP(CTorusDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK_PROJ, OnCheckProjection)
	ON_CBN_CLOSEUP(IDC_COMBO_MODE, OnCloseupComboMode)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_FILL, OnFillSurface)
	ON_BN_CLICKED(IDC_FLAT, OnFlatSurface)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTorusDlg message handlers

BOOL CTorusDlg::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
	PlotTorus();
	m_cmbTrackMode.SetCurSel(0);
	SetTimer(1,33,0);
	
	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 CTorusDlg::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 CTorusDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

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



void CTorusDlg::PlotTorus()
{
	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
	m_Graph3D.SetElementSurfaceFill(0,m_bFill);
	m_Graph3D.SetElementSurfaceFlat(0,m_bFlat);
	m_Graph3D.SetLighting(0,m_bLights);

	float a = m_nDefformation/100.0;     

	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 = (a*cos(tj) + 3.0) * cos(ti);
			y = a*sin(tj);
			z = (a*cos(tj) + 3.0) * sin(ti);

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

	m_Graph3D.AutoRange();
}

void CTorusDlg::OnTimer(UINT nIDEvent) 
{
    UpdateData();

	PlotTorus();

	m_Graph3D.SetElementLight(0,TRUE); // Enable Lighting
    m_Graph3D.SetElementLightingAmbient(0,m_nAmbient);
    m_Graph3D.SetElementLightingDiffuse(0,m_nDiffuse);
	m_Graph3D.SetElementLightingSpecular(0,m_nSpecular);
	m_Graph3D.SetElementMaterialAmbient(0,m_nAmbMat);
    m_Graph3D.SetElementMaterialDiffuse(0,m_nDiffMat);
	m_Graph3D.SetElementMaterialSpecular(0,m_nSpecMat);
	m_Graph3D.SetElementMaterialEmission(0,m_nEmission);
	m_Graph3D.SetElementMaterialShinines(0,m_nShine);
	m_Graph3D.SetLightCoordinates(0,(float)m_nPosX,(float)m_nPosY,(float)m_nPosZ);

	CString str;

	str.Format("%d",m_nAmbient);
	m_strAmbient = str;

	str.Format("%d",m_nDiffuse);
	m_strDiffuse = str;

	str.Format("%d",m_nSpecular);
	m_strSpecular = str;

	str.Format("%d",m_nAmbMat);
	m_strAmbMat = str;

	str.Format("%d",m_nDiffMat);
	m_strDiffMat = str;

	str.Format("%d",m_nSpecMat);
	m_strSpecMat = str;

	str.Format("%d",m_nEmission);
	m_strEmission = str;

	str.Format("%d",m_nShine);
	m_strShine = str;

	str.Format("%d",m_nPosX);
	m_strPosX = str;
	str.Format("%d",m_nPosY);
	m_strPosY = str;
	str.Format("%d",m_nPosZ);
	m_strPosZ = str;



	UpdateData(FALSE);

	CDialog::OnTimer(nIDEvent);
}


void CTorusDlg::OnFillSurface() 
{
	UpdateData();
	m_Graph3D.SetElementSurfaceFill(0,m_bFill);
	
}

void CTorusDlg::OnFlatSurface() 
{
	UpdateData();
	m_Graph3D.SetElementSurfaceFlat(0,m_bFlat);
	
}


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