BOOL C3Dview::InitializeOpenGL()
{
//Get a DC for the Client Area
m_pDC = new CClientDC(this);
//Failure to Get DC
if(m_pDC == NULL)
{
//::MessageBox("Error Obtaining DC");
return FALSE;
}
//Failure to set the pixel format
if(!SetupPixelFormat())
{
return FALSE;
}
//Create Rendering Context
m_hRC = ::wglCreateContext (m_pDC->GetSafeHdc ());
//Failure to Create Rendering Context
if(m_hRC == 0)
{
// MessageBox("Error Creating RC");
return FALSE;
}
//Make the RC Current
if(::wglMakeCurrent (m_pDC->GetSafeHdc (), m_hRC)==FALSE)
{
// MessageBox("Error making RC Current");
return FALSE;
}
//Specify Black as the clear color
::glClearColor(0.2f,0.2f,0.2f,0.0f);//设置背景色
//Specify the back of the buffer as clear depth
::glClearDepth(1.0f);
//Enable Depth Testing
::glEnable(GL_DEPTH_TEST);
int C3Dview::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
InitializeOpenGL();
//CRoom1App *pApp = (CRoom1App *)AfxGetApp();
m_tipBtnCtr.Create(this, TTS_ALWAYSTIP);
m_tipBtnCtr.AddTool(this);
m_tipBtnCtr.Activate(TRUE);
m_tipBtnCtr.SetDelayTime(100);//delay time
//m_tipBtnCtr.SetTipTextColor(RGB(0,0,255));
//m_tipBtnCtr.SetTipBkColor( RGB(200,200,0));
m_tipBtnCtr.SetMaxTipWidth(200);
BuildFont();
init0();
return 0;
}
void C3Dview::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(::wglMakeCurrent (0,0) == FALSE)
{
MessageBox(_T("Could not make RC non-current"));
}
//Delete the rendering context
if(::wglDeleteContext (m_hRC)==FALSE)
{
MessageBox(_T("Could not delete RC"));
}
//Delete the DC
if(m_pDC)
{
delete m_pDC;
}
//Set it to NULL
m_pDC = NULL;
KillFont();
//AfxMessageBox("haha");
}
BOOL C3Dview::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CView::OnEraseBkgnd(pDC);
}
void C3Dview::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CView::OnSize(nType, cx, cy);
GLdouble aspect_ratio; // width/height ratio
// TODO: Add your message handler code here
if ( 0 >= cx || 0 >= cy )
{
return;
}
// select the full client area
::glViewport(0, 0, cx, cy);
// compute the aspect ratio
// this will keep all dimension scales equal
aspect_ratio = (GLdouble)cx/(GLdouble)cy;
// select the projection matrix and clear it
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
::gluPerspective(45.0f, aspect_ratio, .01f*BS, 200.0f*BS);//画三维
// switch back to the modelview matrix and clear it
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
}
void C3Dview::init0()
{
//自定义函数
}
void C3Dview::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
rx-=1.0f;
Invalidate();