www.pudn.com > testproj.rar > dlgTest.cpp
// dlgTest.cpp : implementation file
//
#include "stdafx.h"
#include "testproj.h"
#include "dlgTest.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CdlgTest dialog
CdlgTest::CdlgTest(CWnd* pParent /*=NULL*/)
: CDialog(CdlgTest::IDD, pParent)
{
//{{AFX_DATA_INIT(CdlgTest)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CdlgTest::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CdlgTest)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CdlgTest, CDialog)
//{{AFX_MSG_MAP(CdlgTest)
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CdlgTest message handlers
bool CdlgTest::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd;
pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR); //size of this pfd
pfd.nVersion=1; //version number
pfd.dwFlags=PFD_DRAW_TO_WINDOW| //support window
PFD_SUPPORT_OPENGL| //support opengl
PFD_DOUBLEBUFFER| //double buffered
PFD_STEREO_DONTCARE;
pfd.iPixelType=PFD_TYPE_RGBA; //RGBA type
pfd.cColorBits=32; //32-bit color depth
pfd.cRedBits=8;
pfd.cRedShift=16;
pfd.cGreenBits=8;
pfd.cGreenShift=8;
pfd.cBlueBits=8;
pfd.cBlueShift=0;
pfd.cAlphaBits=0;
pfd.cAlphaShift=0;
pfd.cAccumBits=64;
pfd.cAccumRedBits=16;
pfd.cAccumBlueBits=16;
pfd.cAccumGreenBits=16;
pfd.cDepthBits=32;
pfd.cStencilBits=8;
pfd.cAuxBuffers=0;
pfd.iLayerType=PFD_MAIN_PLANE;
pfd.bReserved=0;
pfd.dwLayerMask=0;
pfd.dwVisibleMask=0;
pfd.dwDamageMask=0;
int pixelformat;
if((pixelformat=ChoosePixelFormat(hDC,&pfd))==0)
{
pixelformat=1;
if(DescribePixelFormat(hDC,pixelformat,sizeof(PIXELFORMATDESCRIPTOR),&pfd)==0)
{
MessageBox("选择像素格式失败!");
return FALSE;
}
}
//设置像素格式
if(SetPixelFormat(hDC,pixelformat,&pfd)==FALSE)
{
MessageBox("设置像素格式失败!");
return FALSE;
}
return TRUE;
}
void CdlgTest::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(GLdouble)cx/(GLdouble)cy,1,10.0);
glMatrixMode(GL_MODELVIEW);
// TODO: Add your message handler code here
}
bool CdlgTest::CreateViewGLContext(HDC hDC)
{
m_rc=wglCreateContext(hDC);
if(m_rc==NULL)
return FALSE;
//选择绘制情景对象
if(wglMakeCurrent(hDC,m_rc)==FALSE)
return FALSE;
return TRUE;
}
void CdlgTest::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
HWND hWnd=GetSafeHwnd();
HDC hDC=::GetDC(hWnd);
wglMakeCurrent(hDC,m_rc);
/* if (!LoadGLTextures()) // Jump To Texture Loading Routine ( NEW )
{
return; // If Texture Didn't Load Return FALSE
}*/
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDrawBuffer(GL_BACK);
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(5,5,5,0,0,0,0,1,0);
glColor3f(0,0,0);
auxSolidSphere(1.0f);
glFinish();
SwapBuffers(wglGetCurrentDC());
glDrawBuffer(GL_FRONT);
// Do not call CDialog::OnPaint() for painting messages
}
int CdlgTest::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
HWND hWnd=GetSafeHwnd();
HDC hDC=::GetDC(hWnd);
if(SetWindowPixelFormat(hDC)==FALSE)
return 0;
if(CreateViewGLContext(hDC)==FALSE)
return 0;
// glClearColor(0.0f,0.75f,0.75f,1.0f);
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK,GL_LINE);
glShadeModel(GL_FLAT);
glEnable(GL_NORMALIZE);
glClearDepth(1.0);
return 0;
}