www.pudn.com > Graphicpach.rar > tempView.cpp
// tempView.cpp : implementation of the CTempView class
//
#include "stdafx.h"
#include "temp.h"
#include "tempDoc.h"
#include "tempView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTempView
IMPLEMENT_DYNCREATE(CTempView, CView)
BEGIN_MESSAGE_MAP(CTempView, CView)
//{{AFX_MSG_MAP(CTempView)
ON_WM_CREATE()
ON_WM_DESTROY()
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()
/////////////////////////////////////////////////////////////////////////////
// CTempView construction/destruction
CTempView::CTempView()
{
// TODO: add construction code here
}
CTempView::~CTempView()
{
}
BOOL CTempView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style|=WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
cs.cx=500;
cs.cy=500;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTempView drawing
void CTempView::OnDraw(CDC* pDC)
{
CTempDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
wglMakeCurrent(pDC->m_hDC,m_hRC);
DrawScene(); //用户自定义函数,用于绘制三维场景
wglMakeCurrent(pDC->m_hDC,NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CTempView printing
BOOL CTempView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTempView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTempView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTempView diagnostics
#ifdef _DEBUG
void CTempView::AssertValid() const
{
CView::AssertValid();
}
void CTempView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTempDoc* CTempView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTempDoc)));
return (CTempDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTempView message handlers
int CTempView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
PIXELFORMATDESCRIPTOR pfd= {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,0,0,0,0,0,0,
32,
0,0,
PFD_MAIN_PLANE,
0,
0,0,0 };
CClientDC dc(this);
int pixelFormat=ChoosePixelFormat(dc.m_hDC,&pfd);
BOOL success=SetPixelFormat(dc.m_hDC,pixelFormat,&pfd);
m_hRC=wglCreateContext(dc.m_hDC);
//display();
return 0;
}
void CTempView::OnDestroy()
{
CView::OnDestroy();
wglDeleteContext(m_hRC);
// TODO: Add your message handler code here
}
void CTempView::DrawScene()
{
glClearColor(0.25,0.40,0.70,0.0);
glShadeModel(GL_SMOOTH);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
//glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
//glutWireSphere(1.0,0.5,0.5);
/*glBegin(GL_POLYGON);
glVertex3f(0.25,0.25,0.0);
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.75,0.75,0.0);
glVertex3f(0.25,0.75,0.0);
glEnd();
glFlush();*/
//init();
display();
reshape(m_width,m_height);
}
void CTempView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
RECT rect;
CDC* dc = GetDC();
GetClientRect(&rect);
wglMakeCurrent(dc->m_hDC, m_hRC);
glViewport(0, 0, rect.right, rect.bottom);
wglMakeCurrent(NULL,NULL);
ReleaseDC(dc);
}
void CTempView::display()
{
glPushMatrix();
glutWireSphere(0.2,20,16);
/*glBegin(GL_POLYGON);
glVertex3f(0.25,0.25,0.0);
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.75,0.75,0.0);
glVertex3f(0.25,0.75,0.0);
glEnd();*/
glRotatef((GLfloat)year,0.0,0.1,0.0);
glTranslatef(0.7,0.0,0.0);
glRotatef((GLfloat)day,0.0,0.1,0.0);
glutWireSphere(0.1,10,8);
/*glBegin(GL_POLYGON);
glVertex3f(0.25,0.25,0.0);
glVertex3f(0.75,0.25,0.0);
glVertex3f(0.75,0.75,0.0);
glVertex3f(0.25,0.75,0.0);
glEnd();*/
glPopMatrix();
glutSwapBuffers();
}
BOOL CTempView::SetupView(int cx, int cy)
{
::glViewport(0, 0, cx, cy);
return TRUE;
}
void CTempView::reshape(int w, int h)
{
SetupView(w,h);
glViewport(0,0,(GLsizei)m_width,(GLsizei)m_height);
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ();
gluPerspective(60.0,(GLfloat)m_width/(GLfloat)m_height,1.0,20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
}