www.pudn.com > 3dsMFCRender.rar > 3dsView.cpp, change:1998-03-21,size:6061b


// 3dsView.cpp : implementation of the C3dsView class 
// 
 
#include "stdafx.h" 
#include "3ds.h" 
 
#include "3dsDoc.h" 
#include "3dsView.h" 
#include <mmsystem.h>		//for timing 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// C3dsView 
 
IMPLEMENT_DYNCREATE(C3dsView, CView) 
 
BEGIN_MESSAGE_MAP(C3dsView, CView) 
	//{{AFX_MSG_MAP(C3dsView) 
	ON_WM_CREATE() 
	ON_WM_SIZE() 
	ON_COMMAND(ID_SCENE_ANIMER, OnSceneAnimer) 
	ON_WM_DESTROY() 
	ON_WM_RBUTTONDOWN() 
	ON_WM_RBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	ON_WM_LBUTTONUP() 
	ON_WM_LBUTTONDOWN() 
	ON_COMMAND(ID_SCENE_BOX, OnSceneBox) 
	ON_COMMAND(ID_SCENE_DISPLIST, OnSceneDisplist) 
	ON_COMMAND(ID_SCENE_PLAY, OnScenePlay) 
	ON_COMMAND(ID_SCENE_REVERSE, OnSceneReverse) 
	ON_COMMAND(ID_SCENE_STOP, OnSceneStop) 
	ON_COMMAND(ID_SCENE_ADVANCE, OnSceneAdvance) 
	ON_COMMAND(ID_SCENE_MOVESTART, OnSceneMovestart) 
	ON_COMMAND(ID_SCENE_MOVEEND, OnSceneMoveend) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// C3dsView construction/destruction 
 
C3dsView::C3dsView() 
{ 
} 
 
C3dsView::~C3dsView() 
{ 
} 
 
BOOL C3dsView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// C3dsView drawing 
 
void C3dsView::OnDraw(CDC* pDC) 
{ 
	 
	//C3dsDoc* pDoc = GetDocument(); 
	//ASSERT_VALID(pDoc); 
	m_GL.RenderScene(); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// C3dsView diagnostics 
 
#ifdef _DEBUG 
void C3dsView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void C3dsView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
C3dsDoc* C3dsView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(C3dsDoc))); 
	return (C3dsDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// C3dsView message handlers 
 
int C3dsView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	m_pDC = new CClientDC(this);;  
	m_GL.InitializeOpenGL(m_pDC); 
	m_bAnim = FALSE; 
	m_AnimSpeed = 30; 
	m_ElapsedTime = 0; 
	m_previousElapsedTime = 0; 
	mouseprevpoint.x = 0; 
	mouseprevpoint.y = 0; 
	mouserightdown = FALSE; 
	mouseleftdown = FALSE; 
	strcpy(oldtitle,(LPCTSTR)GetDocument()->GetTitle()); 
	currentframe = 0; 
	return 0; 
} 
 
void C3dsView::OnSize(UINT nType, int cx, int cy)  
{ 
	CView::OnSize(nType, cx, cy); 
		 
	m_GL.Resize(cx, cy); 
	 
} 
 
void C3dsView::OnSceneAnimer()  
{ 
	if(m_bAnim)  
	{ 
		GetDocument()->SetTitle( oldtitle ); 
		m_bAnim = FALSE;	 
	} 
	else 
	{ 
		m_bAnim = TRUE;	 
	} 
} 
 
void C3dsView::SceneAnimate() 
{ 
	static DWORD count	   = 0; 
	static DWORD accumulated = 0; 
 
	m_ElapsedTime = ::timeGetTime(); 
	 
	// Check if enough time has gone by to draw the next frame 
	if( glElapsedTimeRender() >= (1000/m_AnimSpeed) && m_bAnim ) //m_AnimSpeed in FPS 
	{	 
		m_GL.RenderScene(); 
		accumulated += glElapsedTimeRender(); 
		count++; 
		if( count >= 10) 
		{ 
			sprintf(titlebuffer," %.0f FPS", (1000.0f / accumulated) * count ); 
			GetDocument()->SetTitle( titlebuffer ); 
			count = 0; 
			accumulated = 0; 
		} 
		//Reset the timecounter 
		m_previousElapsedTime = m_ElapsedTime; 
		currentframe = m_GL.AdvanceAnim(); 
	} 
} 
 
int C3dsView::glElapsedTimeRender() 
{ 
	return m_ElapsedTime - m_previousElapsedTime; 
} 
 
void C3dsView::OnDestroy()  
{ 
	CView::OnDestroy(); 
	m_bAnim = FALSE; 
} 
 
BOOL C3dsView::OpenFile(LPCTSTR lpszPathName) 
{ 
	char* file = new char[strlen(lpszPathName)]; 
	strcpy(file, lpszPathName);	 
	 
	if(m_GL.Open3dsFile(file)) 
	{ 
		strcpy(oldtitle,(LPCTSTR)GetDocument()->GetTitle()); 
		return TRUE; 
	} 
	return FALSE; 
} 
 
void C3dsView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	SetCapture( ); 
	mouserightdown = TRUE; 
	mouseprevpoint.x = point.x; 
	mouseprevpoint.y = point.y; 
	CView::OnRButtonDown(nFlags, point); 
} 
 
void C3dsView::OnRButtonUp(UINT nFlags, CPoint point)  
{ 
	ReleaseCapture( ); 
	mouserightdown = FALSE; 
	m_GL.SetCamPos(2, (point.y - mouseprevpoint.y) , TRUE, TRUE); 
 
	CView::OnRButtonUp(nFlags, point); 
} 
 
void C3dsView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	 
	if(mouserightdown) 
	{ 
		m_GL.SetCamPos(2, -(point.y - mouseprevpoint.y) , TRUE); 
	} 
	else if(mouseleftdown) 
	{	 
		m_GL.SetSceneRot(0, (point.y - mouseprevpoint.y) , TRUE); 
		m_GL.SetSceneRot(2, (point.x - mouseprevpoint.x) , TRUE); 
	} 
	CView::OnMouseMove(nFlags, point); 
 
	mouseprevpoint.x = point.x; 
	mouseprevpoint.y = point.y; 
} 
 
void C3dsView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	ReleaseCapture( ); 
	mouseleftdown = FALSE; 
	m_GL.SetSceneRot(0, (point.y - mouseprevpoint.y) , TRUE, TRUE); 
	m_GL.SetSceneRot(2, (point.x - mouseprevpoint.x) , TRUE, TRUE); 
 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void C3dsView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	SetCapture( ); 
	mouseleftdown = TRUE; 
	mouseprevpoint.x = point.x; 
	mouseprevpoint.y = point.y; 
 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void C3dsView::OnSceneBox()  
{ 
	m_GL.SetBoundingBoxMode(); 
	m_GL.RenderScene(); 
} 
 
void C3dsView::OnSceneDisplist()  
{ 
	m_GL.setDisplayListMode(); 
	m_GL.RenderScene(); 
} 
 
void C3dsView::OnScenePlay()  
{ 
	m_bAnim = TRUE; 
} 
 
void C3dsView::OnSceneReverse()  
{ 
	currentframe = 	m_GL.AdvanceAnim(1,-1); 
	m_GL.RenderScene(); 
} 
 
void C3dsView::OnSceneStop()  
{ 
	GetDocument()->SetTitle( oldtitle ); 
	m_bAnim = FALSE;	 
} 
 
void C3dsView::OnSceneAdvance()  
{ 
	currentframe = m_GL.AdvanceAnim(1,1); 
	m_GL.RenderScene(); 
} 
 
void C3dsView::OnSceneMovestart()  
{ 
	currentframe = m_GL.AdvanceAnim(1,1,-2); 
	m_GL.RenderScene(); 
} 
 
void C3dsView::OnSceneMoveend()  
{ 
	currentframe = m_GL.AdvanceAnim(1,1,-3);	 
	m_GL.RenderScene(); 
}