19,469
社区成员
发帖
与我相关
我的任务
分享
CMy001View::CMy001View()
{
// TODO: add construction code here
xMax= 100;
xMin=-100;
yMax= 100;
yMin=-100;
zMax=500;
zMin=-50;
m_bInMouseRotate=0;
m_LeftDownPos=(0,0);
m_xRotate=0;
m_yRotate=0;
m_zRotate=0;
m_xMouseRotation=0;
m_yMouseRotation=0;
m_zMouseRotation=0;
}
void CMy001View::OnDraw(CDC* pDC)
{
CMy001Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
HDC hdc;
HGLRC rc;
glDrawBuffer(GL_BACK);
//GetCurrent(hdc, rc);//Store current rendering and device contexts
//MakeActive();//Make view's rendering context current
::glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
SetupOrtho();
::glPushMatrix();
gluLookAt(200.0,200.0,200.0,0.0,0.0,0.0, 0.0,0.0,1.0);//定义观察坐标系
glBegin(GL_LINES);//世界坐标系
glColor3f(0.0,0.0,1.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 60.0, 0.0, 0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 60.0, 0.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 0.0, 60.0);
glEnd();
DoMouseRotate();//坐标旋转变换
glBegin(GL_LINES);//模型坐标系
glColor3f(0.0,0.0,1.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 60.0, 0.0, 0.0);
glColor3f(0.0,1.0,0.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 60.0, 0.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 0.0, 60.0);
glEnd();
RenderScene(); //画一个正方体
//在这里添加绘图的函数
::glPopMatrix();
// Tell OpenGL to flush its pipeline
::glFinish();
// Now Swap the buffers
if ( FALSE == ::SwapBuffers(m_pDC->GetSafeHdc()))
return;
}
/////////////////////////////////////////////////////////////////////////////
// CMy001View printing
void CMy001View::DoMouseRotate()
{
glRotatef(m_xMouseRotation, 1.0, 0.0, 0.0);
glRotatef(m_yMouseRotation, 0.0, 1.0, 0.0);
glRotatef(m_zMouseRotation, 0.0, 0.0, 1.0);
}
void CMy001View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bool==1)
{
trackball_MouseDown(point);
}
CView::OnLButtonDown(nFlags, point);
}
void CMy001View::trackball_MouseDown(CPoint point)
{
m_LeftDownPos = point;//将该值赋给变量m_LeftDownPos
SetCapture();
::SetCursor(LoadCursor(NULL, IDC_SIZEALL));
m_bInMouseRotate=TRUE;//////////////////
}
void CMy001View::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bInMouseRotate==true)
{
trackball_MouseMove(point);//获得鼠标点的坐标,并调用trackball_MouseMove()
}
CView::OnMouseMove(nFlags, point);
}
void CMy001View::trackball_MouseMove(CPoint point)
{
ASSERT(GetCapture()==this);
::SetCursor(LoadCursor(NULL, IDC_SIZEALL));
if(m_bInMouseRotate==true)
{
if(m_xRotate==1)
{
m_xMouseRotation -= (float)(m_LeftDownPos.x - point.x)/3.0f;
}
if(m_yRotate==1)
{
m_yMouseRotation -= (float)(m_LeftDownPos.x - point.x)/3.0f;
}
if(m_zRotate==1)
{
m_zMouseRotation -= (float)(m_LeftDownPos.x - point.x)/3.0f;
}
m_LeftDownPos = point;
}
InvalidateRect(NULL,FALSE);
}
void CMy001View::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bInMouseRotate==true)
{
trackball_MouseUp(point);//获得鼠标点的坐标,并调用trackball_MouseUp()
}
CView::OnLButtonUp(nFlags, point);
}
void CMy001View::trackball_MouseUp(CPoint point)
{
//m_LeftDownPos = CPoint(0,0); // forget where we clicked
//m_LeftUpPos=point;
ReleaseCapture();
m_bInMouseRotate=FALSE;
}
void CMy001View::OnxRotate()
{
// TODO: Add your command handler code here
m_xRotate=1;
m_yRotate=0;
m_zRotate=0;
m_bool=1;
}
void CMy001View::OnyRotate()
{
// TODO: Add your command handler code here
m_yRotate=1;
m_xRotate=0;
m_zRotate=0;
m_bool=1;
}
void CMy001View::OnzRotate()
{
// TODO: Add your command handler code here
m_zRotate=1;
m_xRotate=0;
m_yRotate=0;
m_bool=1;
}
void CMy001View::RenderScene()
{
cube();
}
void CMy001View::cube()
{
glBegin(GL_LINES);//画一个正方体
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 30.0, 0.0, 0.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 30.0, 0.0);
glVertex3f( 30.0, 30.0, 0.0);
glVertex3f( 30.0, 0.0, 0.0);
glVertex3f( 30.0, 30.0, 0.0);
glVertex3f( 0.0, 30.0, 0.0);
glVertex3f( 0.0, 0.0, 0.0);
glVertex3f( 0.0, 0.0, 30.0);
glVertex3f( 30.0, 0.0, 0.0);
glVertex3f( 30.0, 0.0, 30.0);
glVertex3f( 0.0, 30.0, 0.0);
glVertex3f( 0.0, 30.0, 30.0);
glVertex3f( 30.0, 30.0, 0.0);
glVertex3f( 30.0, 30.0, 30.0);
glVertex3f( 0.0, 0.0, 30.0);
glVertex3f( 30.0, 0.0, 30.0);
glVertex3f( 0.0, 0.0, 30.0);
glVertex3f( 0.0, 30.0, 30.0);
glVertex3f( 30.0, 30.0, 30.0);
glVertex3f( 0.0, 30.0, 30.0);
glVertex3f( 30.0, 30.0, 30.0);
glVertex3f( 30.0, 0.0, 30.0);
glEnd();
}