📄 mousecontrolview.cpp
字号:
// MouseControlView.cpp : implementation of the CMouseControlView class
//
#include "stdafx.h"
#include "MouseControl.h"
#include "MouseControlDoc.h"
#include "MouseControlView.h"
#include "ArcBall.h"
#include "DlgDrawCube.h"
#include "Figure.h"
#include "Scene.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMouseControlView
IMPLEMENT_DYNCREATE(CMouseControlView, CView)
BEGIN_MESSAGE_MAP(CMouseControlView, CView)
//{{AFX_MSG_MAP(CMouseControlView)
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_COMMAND(IDM_DRAW_CUBE, OnDrawCube)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMouseControlView construction/destruction
static Matrix4fT Transform = { 1.0f, 0.0f, 0.0f, 0.0f, // NEW: Final Transform
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
static Matrix3fT LastRot = { 1.0f, 0.0f, 0.0f, // NEW: Last Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
static Matrix3fT ThisRot = { 1.0f, 0.0f, 0.0f, // NEW: This Rotation
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };
CMouseControlView::CMouseControlView(): m_Scene(this)
{
// TODO: add construction code here
this->m_GLPixelIndex = 0;
this->m_hGLContext = NULL;
//*******************轨迹球参数初始化********************
m_arcball=new ArcBallT(600,800);
isClicked = false;
isRClicked = false;
isDragging = false;
//**********************************************************
}
CMouseControlView::~CMouseControlView()
{
}
BOOL CMouseControlView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMouseControlView drawing
void CMouseControlView::OnDraw(CDC* pDC)
{
CMouseControlDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//m_Scene.DrawGLScene();
//SwapBuffers( wglGetCurrentDC() );
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); // 保存当前的矩阵
glTranslatef(0.0,0.0,-10.0);
glPushMatrix();
glMultMatrixf(Transform.M); // 应用我们的变换矩阵
m_Scene.Display();
glPopMatrix();
glPopMatrix();
SwapBuffers(pDC->m_hDC);
}
/////////////////////////////////////////////////////////////////////////////
// CMouseControlView printing
BOOL CMouseControlView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMouseControlView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMouseControlView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMouseControlView diagnostics
#ifdef _DEBUG
void CMouseControlView::AssertValid() const
{
CView::AssertValid();
}
void CMouseControlView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMouseControlDoc* CMouseControlView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMouseControlDoc)));
return (CMouseControlDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMouseControlView message handlers
bool CMouseControlView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(this->m_GLPixelIndex==0)
{
this->m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
int CMouseControlView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
HWND hWnd = this->GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(this->SetWindowPixelFormat(hDC)==FALSE)
{
return 0;
}
if(this->CreateViewGLContext(hDC)==FALSE)
{
return 0;
}
GLInitView();
/*float initList[] = {0,0,1,0,0,0,0,0.5,0.5,0.5,0.5,0.8,0.8,0.8,0.8};
m_Scene.SetParam(initList);
float LightPos[] = {-1,1,1,0};
m_Scene.SetSceneLight(Scene::LP_POSITION,LightPos);
m_Scene.InitGLScene();*/
return 0;
}
void CMouseControlView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MousePt.s.X=point.x;
MousePt.s.Y=point.y;
m_arcball->click(&MousePt);
LastRot = ThisRot;
isClicked=true;
CView::OnLButtonDown(nFlags, point);
}
void CMouseControlView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
isClicked=false;
CView::OnLButtonUp(nFlags, point);
}
void CMouseControlView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
MousePt.s.X=point.x;
MousePt.s.Y=point.y;
if (isClicked) //如果按住拖动
{
Quat4fT ThisQuat;
m_arcball->drag(&MousePt, &ThisQuat); // 更新轨迹球的变量
Matrix3fSetRotationFromQuat4f(&ThisRot, &ThisQuat); // 计算旋转量
Matrix3fMulMatrix3f(&ThisRot, &LastRot);
Matrix4fSetRotationFromMatrix3f(&Transform, &ThisRot);
InvalidateRect(NULL,FALSE);
}
CView::OnMouseMove(nFlags, point);
}
void CMouseControlView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei width,height;
GLdouble aspect;
width = cx;
height=cy;
if(cy==0)
{
aspect = (GLdouble)width;
}
else
{
aspect = (GLdouble)width/(GLdouble)height;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,aspect,1.0,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//设置轨迹球的边界
m_arcball->setBounds((GLfloat)cx, (GLfloat)cy);
//在后缓冲中绘制图形
glDrawBuffer (GL_BACK);
}
void CMouseControlView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext = NULL;
}
}
bool CMouseControlView::CreateViewGLContext(HDC hDC)
{
this->m_hGLContext = wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{//创建失败
return FALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{//选为当前RC失败
return FALSE;
}
return TRUE;
}
void CMouseControlView::GLInitView()
{
GLfloat fBrightLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
GLfloat fLowLight[] = { 0.8f, 0.8f, 0.8f, 1.0f };
glClearColor(0.0,0.0,0.0,0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
glEnable(GL_LIGHT0);
//glEnable(GL_COLOR_MATERIAL);
//glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}
BOOL CMouseControlView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
//return CView::OnEraseBkgnd(pDC);
return true;
}
void CMouseControlView::OnDrawCube()
{
// TODO: Add your command handler code here
CDlgDrawCube dlgCreateCube;
if (dlgCreateCube.DoModal() == IDOK)
{
float initList[] = {0,0,0,
dlgCreateCube.m_fXLen,dlgCreateCube.m_fYLen,dlgCreateCube.m_fZLen,
(double) dlgCreateCube.m_nColorR / 255,
(double) dlgCreateCube.m_nColorG / 255,
(double) dlgCreateCube.m_nColorB / 255};
m_Scene.NewFigure(Scene::F3D_CUBE,initList);
Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -