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

📄 depthmaskview.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// DepthMaskView.cpp : implementation of the CDepthMaskView class
//

#include "stdafx.h"
#include "DepthMask.h"

#include "DepthMaskDoc.h"
#include "DepthMaskView.h"

//add down
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up

//add down定义常量
GLboolean eyePosition=GL_FALSE;
//add up定义常量
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDepthMaskView

IMPLEMENT_DYNCREATE(CDepthMaskView, CView)

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

/////////////////////////////////////////////////////////////////////////////
// CDepthMaskView construction/destruction

CDepthMaskView::CDepthMaskView()
{
	//add down
	m_pDC = NULL;
	//add up

}

CDepthMaskView::~CDepthMaskView()
{
}

BOOL CDepthMaskView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	//add down
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
	//add up

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDepthMaskView drawing

void CDepthMaskView::OnDraw(CDC* pDC)
{
	CDepthMaskDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	//add down
	DrawScene();
	//add up
}

/////////////////////////////////////////////////////////////////////////////
// CDepthMaskView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDepthMaskView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDepthMaskView message handlers

int CDepthMaskView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	 
	//add down
	Init(); //初始化OpenGL
	//add up
	
	return 0;
}

void CDepthMaskView::OnDestroy() 
{
	//add down
	HGLRC   hrc;

	hrc = ::wglGetCurrentContext();

	::wglMakeCurrent(NULL,  NULL);

	if (hrc)
		::wglDeleteContext(hrc);

	if (m_pDC)
		delete m_pDC;
	//add up

	CView::OnDestroy();
	
	
}

void CDepthMaskView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	//add down
	int w=cx;
	int h=cy;
	//避免除数为0
	if(h==0) h=1;

	//设置视口与窗口匹配
	glViewport(0,0,w,h);

	//重新设置坐标系统
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	//建立透视投影
	gluPerspective(30.0,(GLfloat)w/(GLfloat)h,1.0,20.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	//add up
}

//add down
void CDepthMaskView::Init()
{
	PIXELFORMATDESCRIPTOR pfd;
	int         n;
	HGLRC       hrc;

	m_pDC = new CClientDC(this);

	ASSERT(m_pDC != NULL);

	if (!bSetupPixelFormat())
		return;

	n = ::GetPixelFormat(m_pDC->GetSafeHdc());
	::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);

	hrc = wglCreateContext(m_pDC->GetSafeHdc());
	wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);

	//定义材质和光照
	GLfloat mat_ambient[]={0.0,0.0,0.0,0.15};
	GLfloat mat_specular[]={1.0,1.0,1.0,0.15};
	GLfloat mat_shininess[]={15.0};
	glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
	glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
	glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glDepthFunc(GL_LEQUAL);
	//进行深度测试
	glEnable(GL_DEPTH_TEST);
}

BOOL CDepthMaskView::bSetupPixelFormat()
{
	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("ChoosePixelFormat failed");
		return FALSE;
	}

	if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
	{
		MessageBox("SetPixelFormat failed");
		return FALSE;
	}

	return TRUE;
}

void CDepthMaskView::DrawScene(void)
{
	GLfloat position[]={0.0,0.0,1.0,1.0};
	GLfloat mat_torus[]={0.75,0.75,0.0,1.0};
	GLfloat mat_cylinder[]={0.0,0.75,0.75,0.15};

	//清除颜色缓冲区和深度缓冲区
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLightfv(GL_LIGHT0,GL_POSITION,position);
	glPushMatrix();

	if(eyePosition)
		gluLookAt(0.0,0.0,9.0,0.0,0.0,0.0,0.0,1.0,0.0);
	else
		gluLookAt(0.0,0.0,-9.0,0.0,0.0,0.0,0.0,1.0,0.0);

	glPushMatrix();
	glTranslatef(-0.5f,-0.5f,-1.0f);
	glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_torus);
	glRotatef(230.0f,1.0f,0.5f,0.0f);
	auxSolidCone(1.0f,2.0f);
	glPopMatrix();
	glEnable(GL_BLEND);
	glDepthMask(GL_FALSE);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE);
	glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_cylinder);
	glTranslatef(-0.6f,-0.5f,-1.0f);
	auxSolidSphere(1.3f);
	glDepthMask(GL_TRUE);
	glDisable(GL_BLEND);
	glPopMatrix();

	//刷新屏幕,绘制图形
	glFinish();
	SwapBuffers(wglGetCurrentDC());
}


void CDepthMaskView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//add down
	if(eyePosition)
		eyePosition=GL_FALSE;
	else
		eyePosition=GL_TRUE;

	//刷新屏幕,重新绘制图形
	DrawScene();
	//add up
	CView::OnLButtonDown(nFlags, point);
}

BOOL CDepthMaskView::OnEraseBkgnd(CDC* pDC) 
{
	
	return CView::OnEraseBkgnd(pDC);
}

⌨️ 快捷键说明

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