📄 stencilview.cpp
字号:
// StencilView.cpp : implementation of the CStencilView class
//
#include "stdafx.h"
#include "Stencil.h"
#include "StencilDoc.h"
#include "StencilView.h"
//add down加入OpenGL头文件
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up加入OpenGL头文件
//定义常量add down
# define YELLOWMAT 1
# define BLUEMAT 2
//定义常量add up
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStencilView
IMPLEMENT_DYNCREATE(CStencilView, CView)
BEGIN_MESSAGE_MAP(CStencilView, CView)
//{{AFX_MSG_MAP(CStencilView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
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()
/////////////////////////////////////////////////////////////////////////////
// CStencilView construction/destruction
CStencilView::CStencilView()
{
//add down
m_pDC = NULL;
//add up
}
CStencilView::~CStencilView()
{
}
BOOL CStencilView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CStencilView drawing
void CStencilView::OnDraw(CDC* pDC)
{
CStencilDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CStencilView printing
BOOL CStencilView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CStencilView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CStencilView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CStencilView diagnostics
#ifdef _DEBUG
void CStencilView::AssertValid() const
{
CView::AssertValid();
}
void CStencilView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CStencilDoc* CStencilView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStencilDoc)));
return (CStencilDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStencilView message handlers
int CStencilView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化 OpenGL
//add up
return 0;
}
void CStencilView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
}
BOOL CStencilView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
void CStencilView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
if(cy > 0)
{
glViewport(0, 0, cx, cy);
if((m_oldRect.right > cx) || (m_oldRect.bottom > cy))
RedrawWindow();
m_oldRect.right = cx;
m_oldRect.bottom = cy;
glClear(GL_STENCIL_BUFFER_BIT);
//创建一个钻石形的模版区域
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.0,3.0,-3.0,3.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glStencilFunc(GL_ALWAYS,0x1,0x1);
glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);
glBegin(GL_QUADS);
glVertex3f(-1.0,0.0,0.0);
glVertex3f(0.0,1.0,0.0);
glVertex3f(1.0,0.0,0.0);
glVertex3f(0.0,-1.0,0.0);
glEnd();
//定义为透视投影
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLdouble)cx/cy,3.0,7.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
}
//add up
}
//add down
void CStencilView::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);
//根据客户区的初始值定透视投影
GetClientRect(&m_oldRect);
//定义光线的颜色和角度等。
GLfloat yellow_diffuse[]={0.7,0.7,0.0,1.0};
GLfloat yellow_specular[]={1.0,1.0,1.0,1.0};
GLfloat blue_diffuse[]={0.1,0.1,0.7,1.0};
GLfloat blue_specular[]={0.1,1.0,1.0,1.0};
GLfloat position_one[]={1.0,1.0,1.0,0.0};
glNewList(YELLOWMAT,GL_COMPILE);
glMaterialfv(GL_FRONT,GL_DIFFUSE,yellow_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,yellow_specular);
glMaterialf(GL_FRONT,GL_SHININESS,64.0);
glEndList();
glNewList(BLUEMAT,GL_COMPILE);
glMaterialfv(GL_FRONT,GL_DIFFUSE,blue_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,blue_specular);
glMaterialf(GL_FRONT,GL_SHININESS,45.0);
glEndList();
glLightfv(GL_LIGHT0,GL_POSITION,position_one);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glClearStencil(0x0);
glEnable(GL_STENCIL_TEST);
}
BOOL CStencilView::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 CStencilView::DrawScene(void)
{
//使用黑色清屏
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//使用当前定义的清屏颜色,清除颜色缓存和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//在模版为1处绘制蓝色球
glStencilFunc(GL_EQUAL,0x1,0x1);
glCallList(BLUEMAT);
auxSolidSphere(0.5);
//在模版不是1处绘制蓝色球
glStencilFunc(GL_NOTEQUAL,0x1,0x1);
glStencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
glPushMatrix();
glRotatef(45.0,0.0,0.0,1.0);
glRotatef(45.0,0.0,1.0,0.0);
glCallList(YELLOWMAT);
auxSolidTorus(0.275,0.85);
glPushMatrix();
glRotatef(90.0,1.0,0.0,0.0);
auxSolidTorus(0.275,0.85);
glPopMatrix();
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -