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

📄 opengldemoview.cpp

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

#include "stdafx.h"
#include "OpenGLDemo.h"

#include "OpenGLDemoDoc.h"
#include "OpenGLDemoView.h"

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

/////////////////////////////////////////////////////////////////////////////
// COpenGLDemoView

IMPLEMENT_DYNCREATE(COpenGLDemoView, CView)

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

/////////////////////////////////////////////////////////////////////////////
// COpenGLDemoView construction/destruction

COpenGLDemoView::COpenGLDemoView()
{
	// TODO: add construction code here

}

COpenGLDemoView::~COpenGLDemoView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// COpenGLDemoView drawing

void COpenGLDemoView::OnDraw(CDC* pDC)
{
	COpenGLDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	double R = 50, i_h = 40;

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

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

	glPushMatrix();
	glColor3f(0.0,1.0,1.0);
	glTranslatef(0.0,-3.0,-8);
	auxWireTeapot(7.0);
	glTranslatef(0.0,14.0,0.0);
	auxSolidTeapot(7.0);
	glPopMatrix();
	//交换前后缓存
	SwapBuffers(wglGetCurrentDC());

	//绘制前景
	glDrawBuffer(GL_FRONT);

/*
	glDrawBuffer(GL_BACK);
	//初始化变换矩阵
//	glLoadIdentity();
	//清除背景颜色
//	glClearColor(1.0,1.0,1.0,1.0f);
//	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glNewList(4,GL_COMPILE_AND_EXECUTE);
	{
		glPushMatrix();
		glColor3f(0.0,0.0,1.0);
//		glTranslatef(0.0,-3.0,-8);
		GLUquadricObj* balltool;
		balltool=gluNewQuadric();
		glPushMatrix();		
		gluCylinder(balltool,R,R,i_h,50,2);
		glTranslatef(0,0,(float)i_h);
		gluQuadricOrientation(balltool,GLU_OUTSIDE);
		gluDisk(balltool,0,R,50,1);
		glTranslatef(0,0,-(float)(i_h+R));
		gluCylinder(balltool,0,R,R,50,2);
		glPopMatrix();		
		glPopMatrix();
	}
	glEndList();
	glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);//将初始化的刀具删除并重新放置在合适的位置
	glDrawBuffer(GL_FRONT);
*/
}

/////////////////////////////////////////////////////////////////////////////
// COpenGLDemoView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// COpenGLDemoView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// COpenGLDemoView message handlers

int COpenGLDemoView::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 COpenGLDemoView::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 COpenGLDemoView::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();
}

⌨️ 快捷键说明

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