www.pudn.com > testproj.rar > testprojView.cpp
// testprojView.cpp : implementation of the CTestprojView class
//
#include "stdafx.h"
#include "testproj.h"
#include "testprojDoc.h"
#include "testprojView.h"
#include "dlgTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTestprojView
IMPLEMENT_DYNCREATE(CTestprojView, CView)
BEGIN_MESSAGE_MAP(CTestprojView, CView)
//{{AFX_MSG_MAP(CTestprojView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_COMMAND(IDM_Test, OnTest)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CTestprojView construction/destruction
CTestprojView::CTestprojView()
{
// TODO: add construction code here
}
CTestprojView::~CTestprojView()
{
}
BOOL CTestprojView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTestprojView drawing
void CTestprojView::OnDraw(CDC* pDC)
{
CTestprojDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(m_hglrc)
wglMakeCurrent(m_pDC->GetSafeHdc(),m_hglrc);
else
return;
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calcul
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(0.0f,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW )
glBegin(GL_TRIANGLES); // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Right Of Triangle (Front)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,-1.0f, 1.0f); // Left Of Triangle (Right)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Right Of Triangle (Right)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,-1.0f, -1.0f); // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f, -1.0f); // Right Of Triangle (Back)
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f); // Left Of Triangle (Left)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f); // Right Of Triangle (Left)
glEnd();
glFlush ();
SwapBuffers(::wglGetCurrentDC());
}
/////////////////////////////////////////////////////////////////////////////
// CTestprojView printing
BOOL CTestprojView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTestprojView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTestprojView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTestprojView diagnostics
#ifdef _DEBUG
void CTestprojView::AssertValid() const
{
CView::AssertValid();
}
void CTestprojView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTestprojDoc* CTestprojView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestprojDoc)));
return (CTestprojDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestprojView message handlers
int CTestprojView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
/////////////新增//////////////
int n;
m_pDC=new CClientDC(this);
ASSERT(m_pDC!=NULL);
//初始化像素格式
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 depth
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("选择像素格式失败!");
return -1;
}
//设置像素格式
if(SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd)==FALSE)
{
MessageBox("设置像素格式失败!");
return -1;
}
n=::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(),n,sizeof(pfd),&pfd);
//创建绘制情景对象
m_hglrc=wglCreateContext(m_pDC->GetSafeHdc());
//选择绘制情景对象
wglMakeCurrent(m_pDC->GetSafeHdc(),m_hglrc);
/////////////新增完//////////////
// TODO: Add your specialized creation code here
return 0;
}
void CTestprojView::OnDestroy()
{
CView::OnDestroy();
if(m_hglrc)
::wglDeleteContext(m_hglrc);
if(m_pDC)
delete m_pDC;
// TODO: Add your message handler code here
}
void CTestprojView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CSize size(cx,cy);
double aspect;
aspect=(cy==0)?(double)size.cx:(double)size.cx/(double)size.cy;
glViewport(0,0,size.cx,size.cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,aspect,1,15.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
}
BOOL CTestprojView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CView::OnEraseBkgnd(pDC);
// return true;
}
void CTestprojView::OnTest()
{
// TODO: Add your command handler code here
CdlgTest test;
test.DoModal();
}