www.pudn.com > Spere.rar > SpereView.cpp
// SpereView.cpp : implementation of the CSpereView class
//
#include "stdafx.h"
#include "Spere.h"
#include "SpereDoc.h"
#include "SpereView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSpereView
IMPLEMENT_DYNCREATE(CSpereView, CView)
BEGIN_MESSAGE_MAP(CSpereView, CView)
//{{AFX_MSG_MAP(CSpereView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_CANCELMODE()
ON_WM_CAPTURECHANGED()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CSpereView construction/destruction
CSpereView::CSpereView()
{
// TODO: add construction code here
}
CSpereView::~CSpereView()
{
}
BOOL CSpereView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSpereView drawing
void CSpereView::OnDraw(CDC* pDC)
{
CSpereDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
this->SetWindowPixelFormat();
m_hGLContext = wglCreateContext(m_hDC);
wglMakeCurrent(m_hDC, m_hGLContext);
DrawSpere();
SwapBuffers(wglGetCurrentDC());
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CSpereView printing
BOOL CSpereView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSpereView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSpereView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSpereView diagnostics
#ifdef _DEBUG
void CSpereView::AssertValid() const
{
CView::AssertValid();
}
void CSpereView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSpereDoc* CSpereView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSpereDoc)));
return (CSpereDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSpereView message handlers
void CSpereView::DrawSpere()
{
CRect rect;
float h=(float)((float)rect.Width())/((float)rect.Height());
this->GetClientRect(&rect);
m_fAngleX = 0;
m_fAngleY = 45;
m_fAngleZ = 10;
wglMakeCurrent(m_hDC, m_hGLContext);
glClearColor(140.0f/255, 195.0f/255, 245.0f/155, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0, 0, 0);
glRotatef(m_fAngleX, 1.0, 0.0, 0.0);
glRotatef(m_fAngleY, 0.0, 1.0, 0.0);
glRotatef(m_fAngleZ, 0.0, 0.0, 1.0);
glScalef(1, 1,1);
GLUquadricObj* q = gluNewQuadric();
gluQuadricDrawStyle(q, GLU_LINE);
gluSphere(q, 0.75, 16, 16);
gluDeleteQuadric(q);
glPopMatrix();
//glFinish();
}
BOOL CSpereView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
void CSpereView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
// TODO: Add your specialized code here and/or call the base class
CView::CalcWindowRect(lpClientRect, nAdjustType);
}
int CSpereView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
hWnd=GetSafeHwnd();
m_hDC=::GetDC(hWnd);
return 0;
}
void CSpereView::SetWindowPixelFormat()
{ PIXELFORMATDESCRIPTOR pfd;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); // Size of this pfd
pfd.nVersion = 1; // Version number : must be 1
pfd.dwFlags = PFD_DRAW_TO_WINDOW | // Support window
PFD_SUPPORT_OPENGL | // Support OpenGL
PFD_DOUBLEBUFFER | // Double buffered
PFD_STEREO_DONTCARE; // Support either monoscopic or stereoscopic
pfd.iPixelType = PFD_TYPE_RGBA; // RGBA type
pfd.cColorBits = 32; // Specifies the number of color bitplanes in each color buffer
pfd.cRedBits = 8; // Specifies the number of red bitplanes in each RGBA color buffer
pfd.cRedShift = 16; // Specifies the shift count for red bitplanes in each RGBA color buffer
pfd.cGreenBits = 8; // Specifies the number of green bitplanes in each RGBA color buffer
pfd.cGreenShift = 8; // Specifies the shift count for green bitplanes in each RGBA color buffer
pfd.cBlueBits = 8; // Specifies the number of blue bitplanes in each RGBA color buffer
pfd.cBlueShift = 0; // Specifies the shift count for blue bitplanes in each RGBA color buffer
pfd.cAlphaBits = 0; // Specifies the number of alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported
pfd.cAlphaShift = 0; // Specifies the shift count for alpha bitplanes in each RGBA color buffer. Alpha bitplanes are not supported
pfd.cAccumBits = 64; // Specifies the total number of bitplanes in the accumulation buffer
pfd.cAccumRedBits = 16; // Specifies the number of red bitplanes in the accumulation buffer
pfd.cAccumGreenBits = 16; // Specifies the number of green bitplanes in the accumulation buffer
pfd.cAccumBlueBits = 16; // Specifies the number of blue bitplanes in the accumulation buffer
pfd.cAccumAlphaBits = 0; // Specifies the number of alpha bitplanes in the accumulation buffer
pfd.cDepthBits = 32; // Specifies the depth of the depth (z-axis) buffer
pfd.cStencilBits = 8; // Specifies the depth of the stencil buffer
pfd.cAuxBuffers = 0; // Specifies the number of auxiliary buffers. Auxiliary buffers are not supported
pfd.iLayerType = PFD_MAIN_PLANE; // Ignored. Earlier implementations of OpenGL used this member, but it is no longer used
pfd.bReserved = 0; // Specifies the number of overlay and underlay planes
pfd.dwLayerMask = 0; // Ignored. Earlier implementations of OpenGL used this member, but it is no longer used
pfd.dwVisibleMask = 0; // Specifies the transparent color or index of an underlay plane
pfd.dwDamageMask = 0; // Ignored. Earlier implementations of OpenGL used this member, but it is no longer used
int m_GLPixelIndex = ChoosePixelFormat(m_hDC, &pfd);// Attempts to match an appropriate pixel format supported by a device context to a given pixel format specification
if(m_GLPixelIndex == 0) // Choose default
{
m_GLPixelIndex = 1;
if(DescribePixelFormat(m_hDC, m_GLPixelIndex, // Obtains information about the pixel format identified by iPixelFormat of the device associated with hdc
sizeof(PIXELFORMATDESCRIPTOR), &pfd)==0)
return ;
}
if(!SetPixelFormat(m_hDC, m_GLPixelIndex, &pfd)) //Sets the pixel format of the specified device context to the format specified by the iPixelFormat index
return ;
}
void CSpereView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(145, (cy == 0) ? 1.0 : cx / (double)cy, 0, 15.0);
glMatrixMode(GL_MODELVIEW);
}
void CSpereView::OnCancelMode()
{
CView::OnCancelMode();
// TODO: Add your message handler code here
}
void CSpereView::OnCaptureChanged(CWnd *pWnd)
{
// TODO: Add your message handler code here
CView::OnCaptureChanged(pWnd);
}