Click here to Skip to main content
15,891,423 members
Articles / Desktop Programming / MFC

Mouse Selection in OpenGL Scene

Rate me:
Please Sign up or sign in to vote.
4.82/5 (20 votes)
25 Oct 2002CPOL3 min read 249.1K   12.5K   82  
One another approach for picking objects with the mouse in OpenGL scene.
// MouseSelDoc.cpp : implementation of the CMouseSelDoc class
//

#include "stdafx.h"
#include "MouseSel.h"

#include "MouseSelDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMouseSelDoc

IMPLEMENT_DYNCREATE(CMouseSelDoc, CDocument)

BEGIN_MESSAGE_MAP(CMouseSelDoc, CDocument)
END_MESSAGE_MAP()


// CMouseSelDoc construction/destruction

CMouseSelDoc::CMouseSelDoc()
{
	// TODO: add one-time construction code here

}

CMouseSelDoc::~CMouseSelDoc()
{
}

BOOL CMouseSelDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

		InitDoc();
		m_count=50;
		double x=-100;
		double y=0;
		double z=0;
		for( int i=1;i<=m_count; i++)
		{
			m_spheres = new CSphere(x,y,z,m_RedColor,m_GreenColor
									,m_BlueColor,m_dRadius);

			m_SphereList.AddTail(m_spheres);
			x=x+5;		
		}

	return TRUE;
}




// CMouseSelDoc serialization

void CMouseSelDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
	ar<<m_SpeedTranslation;
	ar<<m_SpeedRotation;
	ar<<m_xRotation;
	ar<<m_yRotation;
	ar<<m_zRotation;
	ar<<m_xyRotation;
	ar<<m_xTranslation;
	ar<<m_yTranslation;
	ar<<m_zTranslation;
	}
	else
	{

	ar>>m_SpeedTranslation;
	ar>>m_SpeedRotation;
	ar>>m_xRotation;
	ar>>m_yRotation;
	ar>>m_zRotation;
	ar>> m_xyRotation;
	ar>>m_xTranslation;
	ar>>m_yTranslation;
	ar>>m_zTranslation;


	}
	m_SphereList.Serialize(ar);
}


// CMouseSelDoc diagnostics

#ifdef _DEBUG
void CMouseSelDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMouseSelDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG
void CMouseSelDoc::DeleteContents()
{
	while (!m_SphereList.IsEmpty())
	{
		delete m_SphereList.RemoveHead();
	}
	CDocument::DeleteContents();
}

BOOL CMouseSelDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;

	// TODO:  Add your specialized creation code here

	return TRUE;
}
void CMouseSelDoc::RenderScene()
{
	POSITION pos = m_SphereList.GetHeadPosition();
	while (pos != NULL)
	{
     m_spheres = m_SphereList.GetNext(pos);
	glGetIntegerv(GL_VIEWPORT, m_spheres->m_viewport);
    glGetDoublev(GL_MODELVIEW_MATRIX, m_spheres->m_modelMatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, m_spheres->m_projMatrix);
	gluProject(m_spheres->m_xc,m_spheres->m_yc,m_spheres->m_zc,m_spheres->m_modelMatrix, 
				m_spheres->m_projMatrix,m_spheres->m_viewport,
				&m_spheres->m_winx,&m_y,&m_spheres->m_winz);
	m_spheres->m_winy=m_spheres->m_viewport[3] - (GLint) m_y - 1;
	m_spheres->Draw(); 

	}

}

void CMouseSelDoc::InitDoc()
{
	m_RedColor=0.0;
	m_GreenColor=0.0;
	m_BlueColor=1.0;
	m_dRadius=1.0;
	m_xRotation = 0.0f;
	m_yRotation = 0.0f;
	m_zRotation = 0.0f;

	m_xTranslation = 0.0f;
	m_yTranslation = 0.0f;
	m_zTranslation = -15.0f;

	m_SpeedRotation = 1.0f / 3.0f;
	m_SpeedTranslation = 1.0f / 50.0f;

	m_xyRotation = TRUE;

}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Bosnia and Herzegovina Bosnia and Herzegovina
Bahrudin Hrnjica holds a Ph.D. degree in Technical Science/Engineering from University in Bihać.
Besides teaching at University, he is in the software industry for more than two decades, focusing on development technologies e.g. .NET, Visual Studio, Desktop/Web/Cloud solutions.

He works on the development and application of different ML algorithms. In the development of ML-oriented solutions and modeling, he has more than 10 years of experience. His field of interest is also the development of predictive models with the ML.NET and Keras, but also actively develop two ML-based .NET open source projects: GPdotNET-genetic programming tool and ANNdotNET - deep learning tool on .NET platform. He works in multidisciplinary teams with the mission of optimizing and selecting the ML algorithms to build ML models.

He is the author of several books, and many online articles, writes a blog at http://bhrnjica.net, regularly holds lectures at local and regional conferences, User groups and Code Camp gatherings, and is also the founder of the Bihac Developer Meetup Group. Microsoft recognizes his work and awarded him with the prestigious Microsoft MVP title for the first time in 2011, which he still holds today.

Comments and Discussions