📄 sceneantialiasingview.cpp
字号:
// SceneAntialiasingView.cpp : implementation of the CSceneAntialiasingView class
//
#include "stdafx.h"
#include "SceneAntialiasing.h"
#include "SceneAntialiasingDoc.h"
#include "SceneAntialiasingView.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
# define ACSIZE 8
//定义常量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 displayObjects(void);
//函数声明add up
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiasingView
IMPLEMENT_DYNCREATE(CSceneAntialiasingView, CView)
BEGIN_MESSAGE_MAP(CSceneAntialiasingView, CView)
//{{AFX_MSG_MAP(CSceneAntialiasingView)
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()
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiasingView construction/destruction
CSceneAntialiasingView::CSceneAntialiasingView()
{
//add down
m_pDC = NULL;
//add up
}
CSceneAntialiasingView::~CSceneAntialiasingView()
{
}
BOOL CSceneAntialiasingView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiasingView drawing
void CSceneAntialiasingView::OnDraw(CDC* pDC)
{
CSceneAntialiasingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiasingView printing
BOOL CSceneAntialiasingView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSceneAntialiasingView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSceneAntialiasingView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiasingView diagnostics
#ifdef _DEBUG
void CSceneAntialiasingView::AssertValid() const
{
CView::AssertValid();
}
void CSceneAntialiasingView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSceneAntialiasingDoc* CSceneAntialiasingView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSceneAntialiasingDoc)));
return (CSceneAntialiasingDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiasingView message handlers
int CSceneAntialiasingView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化 OpenGL
//add up
return 0;
}
void CSceneAntialiasingView::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 CSceneAntialiasingView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
}
void CSceneAntialiasingView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
glViewport(0, 0, cx, cy);
//add up
}
//add down
void CSceneAntialiasingView::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 mat_ambient[]={1.0,1.0,1.0,1.0};
GLfloat mat_specular[]={1.0,1.0,1.0,1.0};
GLfloat light_position[]={0.0,0.0,10.0,1.0};
GLfloat lm_ambient[]={0.2,0.2,0.2,1.0};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialf(GL_FRONT,GL_SHININESS,50.0);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lm_ambient);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glClearColor(0.0,0.0,0.0,0.0);
glClearAccum(0.0,0.0,0.0,0.0);
}
BOOL CSceneAntialiasingView::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 CSceneAntialiasingView::DrawScene(void)
{
GLint viewport[4];
int jitter;
glGetIntegerv(GL_VIEWPORT,viewport);
glClear(GL_ACCUM_BUFFER_BIT);
for(jitter=0;jitter<ACSIZE;jitter++)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
accPerspective(50.0,
(GLdouble)viewport[2]/(GLdouble)viewport[3],
1.0,15.0,j8[jitter].x,j8[jitter].y,
0.0,0.0,1.0);
displayObjects();
glAccum(GL_ACCUM,1.0/ACSIZE);
}
glAccum(GL_RETURN,1.0);
// glFlush();
// glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
//add down
void displayObjects(void)
{
//对不同的物体定义不同的材质
GLfloat torus_diffuse[]={0.7,0.7,0.0,1.0};
GLfloat cube_diffuse[]={0.0,0.7,0.7,1.0};
GLfloat sphere_diffuse[]={0.7,0.0,0.7,1.0};
GLfloat octa_diffuse[]={0.7,0.4,0.4,1.0};
glPushMatrix();
glTranslatef(0.0,0.0,-5.0);
glRotatef(30.0,1.0,0.0,0.0);
//绘制Torus
glPushMatrix();
glTranslatef(-0.80,0.35,0.0);
glRotatef(100.0,1.0,0.0,0.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,torus_diffuse);
auxSolidTorus(0.275,0.85);
glPopMatrix();
//绘制Cube
glPushMatrix();
glTranslatef(-0.75,0.50,0.0);
glRotatef(45.0,0.0,0.0,1.0);
glRotatef(45.0,1.0,0.0,0.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,cube_diffuse);
auxSolidCube(1.5);
glPopMatrix();
//绘制Sphere
glPushMatrix();
glTranslatef(0.75,0.60,0.0);
glRotatef(30.0,1.0,0.0,0.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,sphere_diffuse);
auxSolidSphere(1.0);
glPopMatrix();
//绘制Octahedron
glPushMatrix();
glTranslatef(0.70,-0.90,0.25);
glMaterialfv(GL_FRONT,GL_DIFFUSE,octa_diffuse);
auxSolidOctahedron(1.0);
glPopMatrix();
glPopMatrix();
}
//该函数主要完成取景变换的矩阵操作。
//该函数的前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);
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -