MainForm的: // MainFrm.cpp : implementation of the CMainFrame class //#include "stdafx.h" #include "3DGlasses.h"#include "MainFrm.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif///////////////////////////////////////////////////////////////////////////// // CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code ! ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP()static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, };///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destructionCMainFrame::CMainFrame() { // TODO: add member initialization code here
-------------------------------------------------------------------------
View的:
// 3DGlassesView.cpp : implementation of the CMy3DGlassesView class
//#include "stdafx.h"
#include "3DGlasses.h"#include "3DGlassesDoc.h"
#include "3DGlassesView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMy3DGlassesViewIMPLEMENT_DYNCREATE(CMy3DGlassesView, CView)BEGIN_MESSAGE_MAP(CMy3DGlassesView, CView)
//{{AFX_MSG_MAP(CMy3DGlassesView)
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_DESTROY()
//}}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()/////////////////////////////////////////////////////////////////////////////
// CMy3DGlassesView construction/destructionCMy3DGlassesView::CMy3DGlassesView()
{
// TODO: add construction code here
my_pDC = NULL;
}CMy3DGlassesView::~CMy3DGlassesView()
{
}BOOL CMy3DGlassesView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style = WS_CHILD|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
//设置WS_CLIPCHILDREN|WS_CLIPSIBLINGS是为了定义OpenGL窗口不覆盖兄弟窗口与子窗口而设置的
return CView::PreCreateWindow(cs);
}/////////////////////////////////////////////////////////////////////////////
// CMy3DGlassesView drawingvoid CMy3DGlassesView::OnDraw(CDC* pDC)
{
CMy3DGlassesDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
static BOOL bBusy = FALSE;
if(bBusy) return;
bBusy = TRUE;
//背景色,没有下列语句时背景为黑色
glClearColor(0.2f,0.2f,0.5f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//将背景置空
glMatrixMode(GL_MODELVIEW); //启动模型矩阵
glLoadIdentity(); //初始化矩阵
//自己的图形
mydraw();
//完成绘制
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy = FALSE;
}/////////////////////////////////////////////////////////////////////////////
// CMy3DGlassesView printingBOOL CMy3DGlassesView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}void CMy3DGlassesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}void CMy3DGlassesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}/////////////////////////////////////////////////////////////////////////////
// CMy3DGlassesView diagnostics#ifdef _DEBUG
void CMy3DGlassesView::AssertValid() const
{
CView::AssertValid();
}void CMy3DGlassesView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}CMy3DGlassesDoc* CMy3DGlassesView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy3DGlassesDoc)));
return (CMy3DGlassesDoc*)m_pDocument;
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CMy3DGlassesView message handlersBOOL CMy3DGlassesView::mypixelformat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
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
};
int pixelformat; //在创建渲染描述表之前必须创造像素格式。
//以下程序用来选择最匹配的硬件像素格式
if((pixelformat = ChoosePixelFormat(my_pDC->GetSafeHdc(),&pfd))==0)
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(my_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}void CMy3DGlassesView::myfirst()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
my_pDC = new CClientDC(this);
ASSERT(my_pDC != NULL);
if(!mypixelformat())
return;
//以上调用mypixelformat()函数,完成像素格式设置
//下面为测试像素格式
n = ::GetPixelFormat(my_pDC->GetSafeHdc());
::DescribePixelFormat(my_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);
hrc = wglCreateContext(my_pDC->GetSafeHdc()); //创建
wglMakeCurrent(my_pDC->GetSafeHdc(),hrc); //当前化渲染描述表 GetClientRect(&my_oldRect); //获取客户区大小
glClearDepth(1.0f); //清除深度缓存
glEnable(GL_DEPTH_TEST); //启动深度测试
glMatrixMode(GL_MODELVIEW); //启动模型矩阵
glLoadIdentity(); //初始化矩阵
}void CMy3DGlassesView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if(cy>0)
{
my_oldRect.right = cx;
my_oldRect.bottom = cy;
glMatrixMode(GL_PROJECTION); //启动透视矩阵
glLoadIdentity(); //初始化
//视场调节
glFrustum(-1.0,1.0,-1.0*cy/cx,1.0*cy/cx,5.0,9.0); //定义空间范围
glViewport(0,0,cx,cy); //定义显示范围
}
RedrawWindow(); //显示更新
}void CMy3DGlassesView::mydraw()
{}int CMy3DGlassesView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
myfirst();
// TODO: Add your specialized creation code here
return 0;
}void CMy3DGlassesView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
HGLRC hrc;
hrc = ::wglGetCurrentContext(); //获取当前渲染描述表
::wglMakeCurrent(NULL,NULL);
if(hrc)
::wglDeleteContext(hrc);
if(my_pDC)
delete my_pDC;
//删除渲染描述表和绑定的设备描述表
}
// MainFrm.cpp : implementation of the CMainFrame class
//#include "stdafx.h"
#include "3DGlasses.h"#include "MainFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMainFrameIMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destructionCMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}CMainFrame::~CMainFrame()
{
}int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
} if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
} // TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar); return 0;
}BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs return TRUE;
}/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext *pContext)
{
// create a splitter with 1 row, 2 columns
if (!m_wndSplitter.CreateStatic(this, 1, 2))
{
TRACE0("Failed to CreateStaticSplitter\n");
return FALSE;
} // add the first splitter pane - the default view in column 0
if (!m_wndSplitter.CreateView(0, 0,
RUNTIME_CLASS(CControlDlgView), CSize(130, 50), pContext))
{
TRACE0("Failed to create first pane\n");
return FALSE;
} // add the second splitter pane - an input view in column 1
if (!m_wndSplitter.CreateView(0, 1,
RUNTIME_CLASS(CMy3DGlassesView), CSize(0, 0), pContext))
{
TRACE0("Failed to create second pane\n");
return FALSE;
} // activate the input view
// SetActiveView((CView*)m_wndSplitter.GetPane(0,1)); return TRUE;
}