📄 sceneantialiastingview.cpp
字号:
// SceneAntialiastingView.cpp : implementation of the CSceneAntialiastingView class
//
#include "stdafx.h"
#include "SceneAntialiasting.h"
#include "SceneAntialiastingDoc.h"
#include "SceneAntialiastingView.h"
//add down加入OpenGL头文件
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up加入OpenGL头文件
//add down
#include <stdlib.h>
#include <math.h>
#include "jitter.h"
//add up
//定义常量add down
# define ACSIZE 8
//定义常量add up
//函数声明add down
//函数声明add up
#define PI_ 3.14159265358979323846
void accFrustum(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble znear, GLdouble zfar, GLdouble pixdx,
GLdouble pixdy, GLdouble eyedx, GLdouble eyedy, GLdouble focus);
void accPerspective(GLdouble fovy, GLdouble aspect,
GLdouble znear, GLdouble zfar, GLdouble pixdx, GLdouble pixdy,
GLdouble eyedx, GLdouble eyedy, GLdouble focus);
void displayObjects(void);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiastingView
IMPLEMENT_DYNCREATE(CSceneAntialiastingView, CView)
BEGIN_MESSAGE_MAP(CSceneAntialiastingView, CView)
//{{AFX_MSG_MAP(CSceneAntialiastingView)
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()
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiastingView construction/destruction
CSceneAntialiastingView::CSceneAntialiastingView()
{
// TODO: add construction code here
//add down
m_pDC = NULL;
//add up
}
CSceneAntialiastingView::~CSceneAntialiastingView()
{
}
BOOL CSceneAntialiastingView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiastingView drawing
void CSceneAntialiastingView::OnDraw(CDC* pDC)
{
CSceneAntialiastingDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiastingView printing
BOOL CSceneAntialiastingView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSceneAntialiastingView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSceneAntialiastingView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiastingView diagnostics
#ifdef _DEBUG
void CSceneAntialiastingView::AssertValid() const
{
CView::AssertValid();
}
void CSceneAntialiastingView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSceneAntialiastingDoc* CSceneAntialiastingView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSceneAntialiastingDoc)));
return (CSceneAntialiastingDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSceneAntialiastingView message handlers
int CSceneAntialiastingView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化 OpenGL
//add up
return 0;
}
void CSceneAntialiastingView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
// TODO: Add your message handler code here
}
BOOL CSceneAntialiastingView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CView::OnEraseBkgnd(pDC);
}
void CSceneAntialiastingView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
CDC*pdc=GetDC();
wglMakeCurrent(pdc->m_hDC,hrc);
int width=cx;
int height=cy;
if(!height)return;
glViewport(0, 0, (GLint)width,(GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(cx<=cy)
glOrtho(-2.25,2.25,-2.25*cy/cx,2.25*cy/cx,-10.0,10.0);
else
glOrtho(-2.25*cy/cx,2.25*cy/cx,-2.25,2.25,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
//add up
}
//add down
void CSceneAntialiastingView::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 CSceneAntialiastingView::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 CSceneAntialiastingView::DrawScene(void)
{
wglMakeCurrent(m_pDC->m_hDC,hrc);
CSceneAntialiastingDoc*pDoc=GetDocument();
ASSERT_VALID(pDoc);
Init();
GLint viewport[4];
int jitter;
glGetIntegerv(GL_VIEWPORT,viewport);
if(!viewport[3])
return;
glClear(GL_ACCUM_BUFFER_BIT);
for(jitter=0;jitter<ACSIZE;jitter++)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
accPerspective(50.0,(double)viewport[2]/(double)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);
glFlush();
auxSwapBuffers();
}
glAccum(GL_RETURN,1.0);
glFlush();
auxSwapBuffers();
wglMakeCurrent(m_pDC->m_hDC,NULL);
SwapBuffers(m_pDC->m_hDC);
}
//add up
void accFrustum(GLdouble left, GLdouble right, GLdouble bottom,
GLdouble top, GLdouble znear, GLdouble zfar, 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 - bottom;
dx = -(pixdx*xwsize/(GLdouble) viewport[2] + eyedx*znear/focus);
dy = -(pixdy*ywsize/(GLdouble) viewport[2] + eyedy*znear/focus);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum (left + dx, right + dx, bottom + dy, top + dy, znear, zfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (-eyedx, -eyedy, 0.0);
}
/* accPerspective()
*
* The first 4 arguments are identical to the gluPerspective() call.
* pixdx and pixdy are anti-alias jitter in pixels.
* Set both equal to 0.0 for no anti-alias jitter.
* eyedx and eyedy are depth-of field jitter in pixels.
* Set both equal to 0.0 for no depth of field effects.
*
* focus is distance from eye to plane in focus.
* focus must be greater than, but not equal to 0.0.
*
* Note that accPerspective() calls accFrustum().
*/
void accPerspective(GLdouble fovy, GLdouble aspect,
GLdouble znear, GLdouble zfar, GLdouble pixdx, GLdouble pixdy,
GLdouble eyedx, GLdouble eyedy, GLdouble focus)
{
GLdouble fov2,left,right,bottom,top;
fov2 = ((fovy*PI_) / 180.0) / 2.0;
top = znear / (cos(fov2) / sin(fov2));
bottom = -top;
right = top * aspect;
left = -right;
accFrustum (left, right, bottom, top, znear, zfar,
pixdx, pixdy, eyedx, eyedy, focus);
}
//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);
glPushMatrix();
glTranslatef(-0.80,0.35,0.0);
glRotatef(100.0,1.0,0.0,0.0);
glMaterialfv(GL_FRONT,GL_DIFFUSE,torus_diffuse);
auxSolidCube(1.0);
glPopMatrix();
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);
auxSolidTorus(0.275,0.85);
glPopMatrix();
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();
glPushMatrix();
glTranslatef(0.70,-0.90,0.25);
glMaterialfv(GL_FRONT,GL_DIFFUSE,octa_diffuse);
auxSolidOctahedron(1.0);
glPopMatrix();
glPopMatrix();
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -