📄 simulateview.cpp
字号:
//X axies with red color
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(5.0, 0.0, 1000.0);
//Y axies with red color
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 5.0, 1000.0);
//Z axies with red color
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 1000.0);
glEnd();
glEnable(GL_LIGHTING);*/
//Rotate along X,Y,Z
glRotatef(m_fRotateAngleWithX, 1.0, 0.0, 0.0);
glRotatef(m_fRotateAngleWithY, 0.0, 1.0, 0.0);
glRotatef(m_fRotateAngleWithZ, 0.0, 0.0, 1.0);
//Scale along X,Y,Z
glScalef(m_fScale, m_fScale, m_fScale);
glPushAttrib(GL_LIGHTING_BIT);
//Set up the colors for the cylinder. The cylinder has
//a blue surface and green interior.
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, vflBlueAmbient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, vflBlueDiffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vflBlueSpecular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
if(m_bTransforming)
glDisable(GL_LIGHTING);
int Index = pDoc->GetNumTriMeshes();
while(Index--)
(pDoc->GetTriMesh(Index))->Show(m_bSmooth);
glPopAttrib();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(m_nCXOld <= m_nCYOld)
glOrtho(-50.0,
50.0,
-50.0 * (GLfloat)(m_nCYOld)/(GLfloat)(m_nCXOld),
50.0 * (GLfloat)(m_nCYOld)/(GLfloat)(m_nCXOld),
0.,
100000000.);
else
glOrtho(-50.0 * (GLfloat)(m_nCXOld)/(GLfloat)(m_nCYOld),
50.0 * (GLfloat)(m_nCXOld)/(GLfloat)(m_nCYOld),
-50.0,
50.0,
0.,
100000000.);
glMatrixMode(GL_MODELVIEW);
glFlush();
SwapBuffers(wglGetCurrentDC());
}
BOOL CSimulateView::CreateViewGLContext(HDC hDC)
{
m_hrc = wglCreateContext(hDC);
if(m_hrc == NULL)
return FALSE;
if(wglMakeCurrent(hDC, m_hrc) == FALSE)
return FALSE;
return TRUE;
}
BOOL CSimulateView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(m_GLPixelIndex == 0) // Choose default
{
m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
return FALSE;
}
if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))
return FALSE;
return TRUE;
}
void CSimulateView::OnContextMenu(CWnd* pWnd, CPoint point)
{
// TODO: Add your message handler code here
CSimulateDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int Index = pDoc->GetNumTriMeshes();
if(Index <= 0)
return;
else
{
m_MainMenu.RemoveMenu(5, MF_BYPOSITION);
for(int i = m_TranMenu.GetMenuItemCount(); --i>=0;)
m_TranMenu.RemoveMenu(i, MF_BYPOSITION);
UINT nTranMenuID = 550;
if(m_CurrentTransformDirection == TRANALONG_X)
{
m_TranMenu.AppendMenu(MF_CHECKED | MF_STRING, nTranMenuID++, "X");
m_TranMenu.AppendMenu(MF_STRING, nTranMenuID++, "Y");
m_TranMenu.AppendMenu(MF_STRING, nTranMenuID++, "Z");
}
else if(m_CurrentTransformDirection == TRANALONG_Y)
{
m_TranMenu.AppendMenu(MF_STRING, nTranMenuID++, "X");
m_TranMenu.AppendMenu(MF_CHECKED | MF_STRING, nTranMenuID++, "Y");
m_TranMenu.AppendMenu(MF_STRING, nTranMenuID++, "Z");
}
else if(m_CurrentTransformDirection == TRANALONG_Z)
{
m_TranMenu.AppendMenu(MF_STRING, nTranMenuID++, "X");
m_TranMenu.AppendMenu(MF_STRING, nTranMenuID++, "Y");
m_TranMenu.AppendMenu(MF_CHECKED | MF_STRING, nTranMenuID++, "Z");
}
m_MainMenu.AppendMenu(MF_POPUP | MF_STRING, (unsigned int)(m_TranMenu.GetSafeHmenu()), "Transform");
m_MainMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x, point.y, this, NULL);
}
}
BOOL CSimulateView::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bRet = CView::OnCommand(wParam, lParam);
UINT nID = LOWORD(wParam); //menu item ID
int nCode = HIWORD(wParam); //notification code
CTransformSetting TransformSetting;
if(bRet == FALSE && nCode == 0)
{
switch(nID)
{
case 500:
//if move item selected
m_nTransformMode = MOVE;
break;
case 501:
//if rotate item selected
m_nTransformMode = ROTATE;
break;
case 502:
//if scale item selected
m_nTransformMode = SCALE;
break;
case 503:
//if Transform step setting item selected
TransformSetting.m_fMovementStep = m_fMovementStep;
TransformSetting.m_fRotationStep = m_fRotationStep;
TransformSetting.m_fScaleFactor = m_fScaleFactor;
UpdateData(FALSE);
if(TransformSetting.DoModal() == IDOK)
{
m_fMovementStep = TransformSetting.m_fMovementStep;
m_fRotationStep = TransformSetting.m_fRotationStep;
m_fScaleFactor = TransformSetting.m_fScaleFactor;
}
break;
case 550:
//if tranform along x selected
m_CurrentTransformDirection = TRANALONG_X;
break;
case 551:
//if tranform along x selected
m_CurrentTransformDirection = TRANALONG_Y;
break;
case 552:
//if tranform along x selected
m_CurrentTransformDirection = TRANALONG_Z;
break;
}
bRet = TRUE;
}
return bRet;
}
void CSimulateView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
RECT Rect;
switch(m_nTransformMode)
{
case NOCHOICE:
CView::OnLButtonDown(nFlags, point);
break;
case MOVE:
m_PointOld = point;
SetCapture();
m_bTransforming = TRUE;
GetClientRect(&Rect);
ClientToScreen(&Rect);
::ClipCursor(&Rect);
CView::OnLButtonDown(nFlags, point);
break;
case ROTATE:
m_PointOld = point;
SetCapture();
m_bTransforming = TRUE;
GetClientRect(&Rect);
ClientToScreen(&Rect);
::ClipCursor(&Rect);
CView::OnLButtonDown(nFlags, point);
break;
case SCALE:
m_PointOld = point;
SetCapture();
m_bTransforming = TRUE;
GetClientRect(&Rect);
ClientToScreen(&Rect);
::ClipCursor(&Rect);
CView::OnLButtonDown(nFlags, point);
break;
}
}
void CSimulateView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bTransforming)
{
if(m_nTransformMode == SCALE)
{
if(point.y < m_PointOld.y)
m_fScale = m_fScale * (1. + m_fScaleFactor);
else if(point.y > m_PointOld.y)
m_fScale = m_fScale * (1. - m_fScaleFactor);
m_PointOld = point;
DrawScene();
}
else if(m_nTransformMode == ROTATE)
{
float fRotateAngle = 0.0f;
if(point.y < m_PointOld.y)
fRotateAngle = -1. * m_fRotationStep;
else if(point.y > m_PointOld.y)
fRotateAngle = m_fRotationStep;
if(m_CurrentTransformDirection == TRANALONG_X)
{
m_fRotateAngleWithX = m_fRotateAngleWithX + fRotateAngle;
if(m_fRotateAngleWithX > 360.0)
m_fRotateAngleWithX = m_fRotateAngleWithX - 360.0;
else if(m_fRotateAngleWithX < 0.0)
m_fRotateAngleWithX = m_fRotateAngleWithX + 360.0;
}
else if(m_CurrentTransformDirection == TRANALONG_Y)
{
m_fRotateAngleWithY = m_fRotateAngleWithY + fRotateAngle;
if(m_fRotateAngleWithY > 360.0)
m_fRotateAngleWithY = m_fRotateAngleWithY - 360.0;
else if(m_fRotateAngleWithY < 0.0)
m_fRotateAngleWithY = m_fRotateAngleWithY + 360.0;
}
else if(m_CurrentTransformDirection == TRANALONG_Z)
{
m_fRotateAngleWithZ = m_fRotateAngleWithZ + fRotateAngle;
if(m_fRotateAngleWithZ > 360.0)
m_fRotateAngleWithZ = m_fRotateAngleWithZ - 360.0;
else if(m_fRotateAngleWithZ < 0.0)
m_fRotateAngleWithZ = m_fRotateAngleWithZ + 360.0;
}
m_PointOld = point;
DrawScene();
}
else if(m_nTransformMode == MOVE)
{
float fMoveDistance = 0.0f;
if(m_CurrentTransformDirection == TRANALONG_X)
{
if(point.x < m_PointOld.x)
fMoveDistance = -1.0 * m_fMovementStep;
else if(point.x > m_PointOld.x)
fMoveDistance = m_fMovementStep;
m_fShiftWithX = m_fShiftWithX + fMoveDistance;
}
else if(m_CurrentTransformDirection == TRANALONG_Y)
{
if(point.y < m_PointOld.y)
fMoveDistance = m_fMovementStep;
else if(point.y > m_PointOld.y)
fMoveDistance = -1.0 * m_fMovementStep;
m_fShiftWithY = m_fShiftWithY + fMoveDistance;
}
else if(m_CurrentTransformDirection == TRANALONG_Z)
//Because the projection direction is -Z
//So we couldn't change the Z corrdinate interactively;
m_fShiftWithZ = m_fShiftWithZ;
m_PointOld = point;
DrawScene();
}
}
CView::OnMouseMove(nFlags, point);
}
void CSimulateView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bTransforming)
{
m_bTransforming = FALSE;
::ReleaseCapture();
::ClipCursor(NULL);
m_nTransformMode = NOCHOICE;
glEnable(GL_LIGHTING);
DrawScene();
}
CView::OnLButtonUp(nFlags, point);
}
void CSimulateView::ResetSetting()
{
m_CurrentTransformDirection = TRANALONG_X;
m_bTransforming = FALSE;
m_nTransformMode = NOCHOICE;
m_fShiftWithX = 0.0f;
m_fShiftWithY = 0.0f;
m_fShiftWithZ = 0.0f;
m_fRotateAngleWithX = 0.0f;
m_fRotateAngleWithY = 0.0f;
m_fRotateAngleWithZ = 0.0f;
m_fScale = 1.0f;
m_fMovementStep = 1.0;
m_fRotationStep = 2.5;
m_fScaleFactor = 0.05;
m_Eye.m_fX = 0.0;
m_Eye.m_fY = 0.0;
m_Eye.m_fZ = 1000.0;
m_Center.m_fX = 0.0;
m_Center.m_fY = 0.0;
m_Center.m_fZ = 0.0;
m_Up.m_fX = 0.0;
m_Up.m_fY = 1.0;
m_Up.m_fZ = 0.0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -