📄 orthjitterview.cpp
字号:
// OrthJitterView.cpp : implementation of the COrthJitterView class
//
#include "stdafx.h"
#include "OrthJitter.h"
#include "OrthJitterDoc.h"
#include "OrthJitterView.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 displayObjects(void);
//函数声明add up
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COrthJitterView
IMPLEMENT_DYNCREATE(COrthJitterView, CView)
BEGIN_MESSAGE_MAP(COrthJitterView, CView)
//{{AFX_MSG_MAP(COrthJitterView)
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()
/////////////////////////////////////////////////////////////////////////////
// COrthJitterView construction/destruction
COrthJitterView::COrthJitterView()
{
//add down
m_pDC = NULL;
//add up
}
COrthJitterView::~COrthJitterView()
{
}
BOOL COrthJitterView::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);
}
/////////////////////////////////////////////////////////////////////////////
// COrthJitterView drawing
void COrthJitterView::OnDraw(CDC* pDC)
{
COrthJitterDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// COrthJitterView printing
BOOL COrthJitterView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void COrthJitterView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void COrthJitterView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// COrthJitterView diagnostics
#ifdef _DEBUG
void COrthJitterView::AssertValid() const
{
CView::AssertValid();
}
void COrthJitterView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
COrthJitterDoc* COrthJitterView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COrthJitterDoc)));
return (COrthJitterDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// COrthJitterView message handlers
int COrthJitterView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化 OpenGL
//add up
return 0;
}
void COrthJitterView::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 COrthJitterView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void COrthJitterView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
glViewport(0, 0, cx, cy);
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 COrthJitterView::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 COrthJitterView::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 COrthJitterView::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);
//注意:4.5是全局坐标上、下,左、右之间的距离,
//下面的公式将小数的象素运动转换到全局坐标下。
glPushMatrix();
glTranslatef(j8[jitter].x*4.5/viewport[2],
j8[jitter].y*4.5/viewport[3],0.0);
displayObjects();
glPopMatrix();
glAccum(GL_ACCUM,1.0/ACSIZE);
}
glAccum(GL_RETURN,1.0);
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);
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();
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();
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 + -