📄 dofview.cpp
字号:
// DofView.cpp : implementation of the CDofView class
//
#include "stdafx.h"
#include "Dof.h"
#include "DofDoc.h"
#include "DofView.h"
//add down加入OpenGL头文件
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up加入OpenGL头文件
//add down
#include "jitter.h"
#include "math.h"
//add up
//函数声明add down
void accPerspective(GLdouble fovy,GLdouble aspect,
GLdouble ear,GLdouble ar,
GLdouble pixdx,GLdouble pixdy,
GLdouble eyedx,GLdouble eyedy,
GLdouble focus);
void accFrustum(GLdouble left,GLdouble right,GLdouble bottom,
GLdouble top,GLdouble ear,GLdouble ar,
GLdouble pixdx,GLdouble pixdy,GLdouble eyedx,
GLdouble eyedy,GLdouble focus);
void renderTeapot (GLfloat x, GLfloat y, GLfloat z,
GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine);
GLuint teapotList;
//函数声明add up
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDofView
IMPLEMENT_DYNCREATE(CDofView, CView)
BEGIN_MESSAGE_MAP(CDofView, CView)
//{{AFX_MSG_MAP(CDofView)
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()
/////////////////////////////////////////////////////////////////////////////
// CDofView construction/destruction
CDofView::CDofView()
{
//add down
m_pDC = NULL;
//add up
}
CDofView::~CDofView()
{
}
BOOL CDofView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CDofView drawing
void CDofView::OnDraw(CDC* pDC)
{
CDofDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CDofView printing
BOOL CDofView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDofView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDofView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDofView diagnostics
#ifdef _DEBUG
void CDofView::AssertValid() const
{
CView::AssertValid();
}
void CDofView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDofDoc* CDofView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDofDoc)));
return (CDofDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDofView message handlers
int CDofView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化 OpenGL
//add up
return 0;
}
void CDofView::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 CDofView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CDofView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
glViewport(0, 0, (GLsizei) cx, (GLsizei) cy);
//add up
}
//add down
void CDofView::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 ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat local_view[] = { 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
glFrontFace (GL_CW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 0.0);
glClearAccum(0.0, 0.0, 0.0, 0.0);
/* make teapot display list */
teapotList = glGenLists(1);
glNewList (teapotList, GL_COMPILE);
auxSolidTeapot(0.5);
glEndList ();
}
BOOL CDofView::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 CDofView::DrawScene(void)
{
int jitter;
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);
glClear(GL_ACCUM_BUFFER_BIT);
for (jitter = 0; jitter < 8; jitter++) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
accPerspective (45.0,
(GLdouble) viewport[2]/(GLdouble) viewport[3],
1.0, 15.0, 0.0, 0.0,
0.33*j8[jitter].x, 0.33*j8[jitter].y, 5.0);
/* ruby, gold, silver, emerald, and cyan teapots */
renderTeapot (-1.1, -0.5, -4.5, 0.1745, 0.01175,
0.01175, 0.61424, 0.04136, 0.04136,
0.727811, 0.626959, 0.626959, 0.6);
renderTeapot (-0.5, -0.5, -5.0, 0.24725, 0.1995,
0.0745, 0.75164, 0.60648, 0.22648,
0.628281, 0.555802, 0.366065, 0.4);
renderTeapot (0.2, -0.5, -5.5, 0.19225, 0.19225,
0.19225, 0.50754, 0.50754, 0.50754,
0.508273, 0.508273, 0.508273, 0.4);
renderTeapot (1.0, -0.5, -6.0, 0.0215, 0.1745, 0.0215,
0.07568, 0.61424, 0.07568, 0.633,
0.727811, 0.633, 0.6);
renderTeapot (1.8, -0.5, -6.5, 0.0, 0.1, 0.06, 0.0,
0.50980392, 0.50980392, 0.50196078,
0.50196078, 0.50196078, .25);
glAccum (GL_ACCUM, 0.125);
}
glAccum (GL_RETURN, 1.0);
SwapBuffers(wglGetCurrentDC());
}
//add up
//add down
//该函数主要完成取景变换的矩阵操作。
//该函数的前6个参数与函数glFrustum()的参数含义相同。
//参数:pixdx、pixdy在象素点反走样中将用到,
// 若不进行反走样,将其设置为0.0。
// eyedx、eyedy是跳动点的深度值,
// 若不考虑深度影响,将其设为0.0。
// focus是视点距焦点物体的距离,focus>0.0。
void accFrustum(GLdouble left,GLdouble right,GLdouble bottom,
GLdouble top,GLdouble ear,GLdouble ar,
GLdouble pixdx,GLdouble pixdy,GLdouble eyedx,
GLdouble eyedy,GLdouble focus)
{
GLdouble xwsize,ywsize;
GLdouble dx,dy;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT,viewport);
xwsize=right-left;
ywsize=top-left;
dx=-(pixdx*xwsize/(GLdouble)viewport[2]+eyedx*ear/focus);
dy=-(pixdy*ywsize/(GLdouble)viewport[3]+eyedy*ear/focus);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(left+dx,right+dx,bottom+dy,top+dy,ear,ar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-eyedx,-eyedy,0.0);
}
//该函数主要实现对accFrustum()函数的调用。
//参数:前4个与函数gluPerspective()的参数意义相同,
// 提供定义取景体所需的数据。
// 后5个参数的意义与函数accFrustum()相同。
void accPerspective(GLdouble fovy,GLdouble aspect,
GLdouble ear,GLdouble ar,
GLdouble pixdx,GLdouble pixdy,
GLdouble eyedx,GLdouble eyedy,
GLdouble focus)
{
GLdouble fov2,left,right,bottom,top;
fov2=((fovy*3.1415926)/180.0)/2.0;
top=ear/(cos(fov2)/sin(fov2));
bottom=-top;
right=top*aspect;
left=-right;
accFrustum(left,right,bottom,top,ear,ar,pixdx,pixdy,eyedx,eyedy,focus);
}
void renderTeapot (GLfloat x, GLfloat y, GLfloat z,
GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
{
GLfloat mat[4];
glPushMatrix();
glTranslatef (x, y, z);
mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;
glMaterialfv (GL_FRONT, GL_AMBIENT, mat);
mat[0] = difr; mat[1] = difg; mat[2] = difb;
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat);
mat[0] = specr; mat[1] = specg; mat[2] = specb;
glMaterialfv (GL_FRONT, GL_SPECULAR, mat);
glMaterialf (GL_FRONT, GL_SHININESS, shine*128.0);
glCallList(teapotList);
glPopMatrix();
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -