⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openglmovieview.cpp

📁 vc++程序设计与技巧中第8章“图形图像编程”部分的VC++原代码
💻 CPP
字号:
// OpenGLMovieView.cpp : implementation of the COpenGLMovieView class
//

#include "stdafx.h"
#include "OpenGLMovie.h"

#include "OpenGLMovieDoc.h"
#include "OpenGLMovieView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COpenGLMovieView

IMPLEMENT_DYNCREATE(COpenGLMovieView, CView)

BEGIN_MESSAGE_MAP(COpenGLMovieView, CView)
	//{{AFX_MSG_MAP(COpenGLMovieView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_TIMER()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// COpenGLMovieView construction/destruction

COpenGLMovieView::COpenGLMovieView()
{
	// TODO: add construction code here
	m_RotateAngle = 0;
}

COpenGLMovieView::~COpenGLMovieView()
{
}

BOOL COpenGLMovieView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// COpenGLMovieView drawing

void COpenGLMovieView::OnDraw(CDC* pDC)
{
	COpenGLMovieDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	if(m_hglrc)
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hglrc);
	else
		return;

	//指定在后台缓存中绘制
	glDrawBuffer(GL_BACK);

	//初始化变换矩阵
	glLoadIdentity();
	//清除背景颜色
	glClearColor(1.0,1.0,1.0,1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
	//进行旋转变换
	glRotatef(m_RotateAngle,1.0,1.0,1.0);
	glColor3f(0.0,1.0,0.0);
	auxWireSphere(15.0);

	glPopMatrix();

	//结束整个绘制
	glFinish();

	//交换前后缓存
	SwapBuffers(wglGetCurrentDC());

	//绘制前景
	glDrawBuffer(GL_FRONT);
}

/////////////////////////////////////////////////////////////////////////////
// COpenGLMovieView printing

BOOL COpenGLMovieView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void COpenGLMovieView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void COpenGLMovieView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// COpenGLMovieView diagnostics

#ifdef _DEBUG
void COpenGLMovieView::AssertValid() const
{
	CView::AssertValid();
}

void COpenGLMovieView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

COpenGLMovieDoc* COpenGLMovieView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLMovieDoc)));
	return (COpenGLMovieDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// COpenGLMovieView message handlers

int COpenGLMovieView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	int         n;

    m_pDC = new CClientDC(this);
    ASSERT(m_pDC != NULL);

	//	初始化像素格式
    static PIXELFORMATDESCRIPTOR pfd = 
	{
        sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
        1,                              // version number
        PFD_DRAW_TO_WINDOW   |          // support window
          PFD_SUPPORT_OPENGL |          // support OpenGL
          PFD_DOUBLEBUFFER,             // double buffered
        PFD_TYPE_RGBA,		            // RGBA type
        24,                             // 24-bit color depth
        0, 0, 0, 0, 0, 0,               // color bits ignored
        0,                              // no alpha buffer
        0,                              // shift bit ignored
        0,                              // no accumulation buffer
        0, 0, 0, 0,                     // accum bits ignored
        32,                             // 32-bit z-buffer
        0,                              // no stencil buffer
        0,                              // no auxiliary buffer
        PFD_MAIN_PLANE,                 // main layer
        0,                              // reserved
        0, 0, 0                         // layer masks ignored
    };

	//	选择像素格式
    int pixelformat;
    if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
    {
        MessageBox("选择像素格式失败!");
        return -1;
    }

	//	设置像素格式
    if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
    {
        MessageBox("设置像素格式失败!");
        return -1;
    }
	
    n = ::GetPixelFormat(m_pDC->GetSafeHdc());
    ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);

	//	创建绘制情景对象
    m_hglrc = wglCreateContext(m_pDC->GetSafeHdc());
	//	选择绘制情景对象
    wglMakeCurrent(m_pDC->GetSafeHdc(), m_hglrc);

	//	启用深度测试
	glEnable(GL_DEPTH_TEST);

	//	设置时间间隔
	SetTimer(0,5,NULL);
	return 0;
}

void COpenGLMovieView::OnDestroy() 
{
	CView::OnDestroy();
	
	// TODO: Add your message handler code here
	//	将当前绘图情景对象赋空
    ::wglMakeCurrent(NULL,  NULL);

	//	删除当前绘图情景对象并释放所占内存
    if (m_hglrc)
	{
        ::wglDeleteContext(m_hglrc);
		m_hglrc = NULL;
	}

    if (m_pDC)
        delete m_pDC;
}

void COpenGLMovieView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	//	设置当前绘图设备
	if(m_hglrc)
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hglrc);
	else
		return;

	//	设置视口大小
	glViewport(0,0,cx,cy);
	//	设置变换模式为投影变换
	glMatrixMode(GL_PROJECTION);
	//	初始化投影变换矩阵
	glLoadIdentity();
	//	根据窗口大小设置调整正射投影矩阵
	if(cx<=cy)
		glOrtho(-20.0,20.0,-20.0*(GLfloat)cy/(GLfloat)cx,
			20.0*(GLfloat)cy/(GLfloat)cx,-50.0,50.0);
	else
		glOrtho(-20.0*(GLfloat)cx/(GLfloat)cy,
			20.0*(GLfloat)cx/(GLfloat)cy,-20.0,20.0,-50.0,50.0);
	//	设置变换模式为模型变换
	glMatrixMode(GL_MODELVIEW);
	//	初始化模型变换矩阵
	glLoadIdentity();
}

void COpenGLMovieView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_RotateAngle += 5;
	if(m_RotateAngle > 360)
		m_RotateAngle = 0;
	InvalidateRect(NULL,FALSE);
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -