- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
OpenGLMFCView.cpp
资源名称:OpenGLMFC.rar [点击查看]
上传用户:yanjing
上传日期:2022-06-12
资源大小:36k
文件大小:7k
源码类别:
OpenGL
开发平台:
Visual C++
- // OpenGLMFCView.cpp : implementation of the COpenGLMFCView class
- //
- #include "stdafx.h"
- #include "OpenGLMFC.h"
- #include "OpenGLMFCDoc.h"
- #include "OpenGLMFCView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // COpenGLMFCView
- IMPLEMENT_DYNCREATE(COpenGLMFCView, CScrollView)
- BEGIN_MESSAGE_MAP(COpenGLMFCView, CScrollView)
- //{{AFX_MSG_MAP(COpenGLMFCView)
- ON_WM_DESTROY() //*******************************************
- ON_WM_SIZE() //*******************************************
- ON_WM_CREATE() //*******************************************
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // COpenGLMFCView construction/destruction
- COpenGLMFCView::COpenGLMFCView()
- {
- // TODO: add construction code here
- }
- COpenGLMFCView::~COpenGLMFCView()
- {
- }
- BOOL COpenGLMFCView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CScrollView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // COpenGLMFCView drawing
- void COpenGLMFCView::OnDraw(CDC* pDC)
- {
- COpenGLMFCDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- //*******************************************
- /////////////////////////////////////////////
- wglMakeCurrent(pDC->m_hDC,hglrc);
- drawwithopengl();
- wglMakeCurrent(pDC->m_hDC,NULL);
- SwapBuffers(pDC->m_hDC);
- /////////////////////////////////////////////
- //*******************************************
- }
- void COpenGLMFCView::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- CSize sizeTotal;
- // TODO: calculate the total size of this view
- sizeTotal.cx = sizeTotal.cy = 100;
- SetScrollSizes(MM_TEXT, sizeTotal);
- }
- /////////////////////////////////////////////////////////////////////////////
- // COpenGLMFCView printing
- BOOL COpenGLMFCView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void COpenGLMFCView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void COpenGLMFCView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // COpenGLMFCView diagnostics
- #ifdef _DEBUG
- void COpenGLMFCView::AssertValid() const
- {
- CScrollView::AssertValid();
- }
- void COpenGLMFCView::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
- COpenGLMFCDoc* COpenGLMFCView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLMFCDoc)));
- return (COpenGLMFCDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // COpenGLMFCView message handlers
- BOOL COpenGLMFCView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- // TODO: Add your specialized code here and/or call the base class
- return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
- }
- void COpenGLMFCView::OnDestroy()
- {
- CScrollView::OnDestroy();
- // TODO: Add your message handler code here
- //*******************************************
- /////////////////////////////////////////////
- // delete rending context
- wglDeleteContext(hglrc);
- /////////////////////////////////////////////
- //*******************************************
- }
- void COpenGLMFCView::OnSize(UINT nType, int cx, int cy)
- {
- CScrollView::OnSize(nType, cx, cy);
- // TODO: Add your message handler code here
- //*******************************************
- int w=cx;
- int h=cy;
- GLfloat nRange = 100.0f;
- // prevent divied by aero
- if(h==0)
- h=1;
- // set viewport to windows dimensions
- glViewport(0,0,w,h);
- // reset coordinate system
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- // establish cliping volune (left,right,top,near,far)
- if(w<=h)
- glOrtho(-nRange,nRange,-nRange*h/w,nRange*h/w,-nRange,nRange);
- else
- glOrtho(-nRange*w/h,nRange*w/h,-nRange,nRange,-nRange,nRange);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- //*******************************************
- }
- //*******************************************
- /////////////////////////////////////////////
- void COpenGLMFCView::drawwithopengl(void)
- {
- float z,angle,x,y;
- // clear the window with current clearing color
- glClear(GL_COLOR_BUFFER_BIT);
- glClearColor(0.0,1.0,0.0,1.0);
- // save matrix state and do the rotation
- glPushMatrix();
- // call only once for all remaining points
- glColor3f(1.0,0.0,0.0);
- glBegin(GL_LINES);
- z=0.0f;
- for(angle=0.0f;angle<=3.14159260f;angle+=0.1f)
- {
- // top half of the circle
- x=50.0f*sin(angle);
- y=50.0f*cos(angle);
- glVertex3f(x,y,z);
- // bottom half of the circle
- x=50.0f*sin(angle+3.1415f);
- y=50.0f*cos(angle+3.1415f);
- glVertex3f(x,y,z);
- }
- // done drawing points
- glEnd();
- // restore transformations
- glPopMatrix();
- /*
- // 画填充多边形
- glClearColor(1.0,1.0,1.0,1.0);
- glPolygonMode(GL_FRONT,GL_FILL);
- glTranslatef(90.0,0.0,0.0);
- glColor3f(0.0,0.0,1.0);
- glBegin(GL_POLYGON);
- glVertex2f(10.0,0.0);
- glVertex2f(50.0,0.0);
- glVertex2f(60.0,3.0);
- glVertex2f(0.0,5.0);
- glEnd();
- */
- // flush drawing commands
- glFlush();
- }
- /////////////////////////////////////////////
- //*******************************************
- int COpenGLMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CScrollView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- //*******************************************
- //以下代码不能加入到Create()函数中
- //*******************************************
- /////////////////////////////////////////////
- // initializing the pixel format
- 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,
- };
- // get the dc for the client area
- CClientDC clientdc(this);
- // return the requested pixel format closely matched
- int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
- // set the pixel format
- BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
- // create the rending context
- hglrc=wglCreateContext(clientdc.m_hDC);
- /////////////////////////////////////////////
- //*******************************************
- return 0;
- }