s045508View.cpp
资源名称:HeShi.rar [点击查看]
上传用户:bjzhifu888
上传日期:2013-01-22
资源大小:136k
文件大小:11k
源码类别:
STL
开发平台:
Visual C++
- // s045508View.cpp : implementation of the CS045508View class
- //
- #include "stdafx.h"
- #include "s045508.h"
- #include "s045508Doc.h"
- #include "s045508View.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CS045508View
- IMPLEMENT_DYNCREATE(CS045508View, CView)
- BEGIN_MESSAGE_MAP(CS045508View, CView)
- //{{AFX_MSG_MAP(CS045508View)
- ON_WM_CREATE()
- ON_WM_DESTROY()
- ON_WM_SIZE()
- ON_COMMAND(ID_TOOLBAR_xRot_CCW, OnTOOLBARxRotCCW)
- ON_COMMAND(ID_TOOLBAR_xRot_CW, OnTOOLBARxRotCW)
- ON_COMMAND(ID_TOOLBAR_yRot_CCW, OnTOOLBARyRotCCW)
- ON_COMMAND(ID_TOOLBAR_yRot_CW, OnTOOLBARyRotCW)
- ON_COMMAND(ID_TOOLBAR_zRot_CCW, OnTOOLBARzRotCCW)
- ON_COMMAND(ID_TOOLBAR_zRot_CW, OnTOOLBARzRotCW)
- ON_COMMAND(ID_TOOLBAR_XPAN_L, OnToolbarXpanL)
- ON_COMMAND(ID_TOOLBAR_XPAN_R, OnToolbarXpanR)
- ON_COMMAND(ID_TOOLBAR_YPAN_D, OnToolbarYpanD)
- ON_COMMAND(ID_TOOLBAR_YPAN_U, OnToolbarYpanU)
- ON_COMMAND(ID_ZOOM_IN, OnZoomIn)
- ON_COMMAND(ID_Zoom_Out, OnZoomOut)
- ON_COMMAND(ID_SHADE, OnShade)
- ON_COMMAND(ID_Wireframe, OnWireframe)
- ON_WM_MOUSEWHEEL()
- ON_WM_KEYDOWN()
- ON_WM_LBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONUP()
- //}}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()
- /////////////////////////////////////////////////////////////////////////////
- // CS045508View construction/destruction
- CS045508View::CS045508View()
- {
- // TODO: add construction code here
- m_hRC = 0;
- m_pDC = 0;
- xRot=0;
- yRot=0;
- zRot=0;
- xpan=0;
- ypan=0;
- zpan=0;
- ShadeOrWire=0;
- }
- CS045508View::~CS045508View()
- {
- }
- BOOL CS045508View::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CS045508View drawing
- void CS045508View::OnDraw(CDC* pDC)
- {
- CS045508Doc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- SetupViewingTransform();
- RenderScene();
- SwapBuffers(m_pDC->GetSafeHdc());
- }
- /////////////////////////////////////////////////////////////////////////////
- // CS045508View printing
- BOOL CS045508View::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CS045508View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CS045508View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CS045508View diagnostics
- #ifdef _DEBUG
- void CS045508View::AssertValid() const
- {
- CView::AssertValid();
- }
- void CS045508View::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CS045508Doc* CS045508View::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CS045508Doc)));
- return (CS045508Doc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CS045508View message handlers
- BOOL CS045508View::InitializeOpenGL()
- {
- m_pDC = new CClientDC(this); //CDC
- if (m_pDC == NULL) return FALSE;
- if (!SetupPixelFormat()) return FALSE;
- m_hRC =::wglCreateContext(m_pDC->GetSafeHdc()); //HGLRC
- if ((m_hRC) == 0) return FALSE;
- if (!(::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC))) return FALSE; //connection between CDC and HGLRC
- return TRUE;
- }
- int CS045508View::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- InitializeOpenGL();
- glClearColor(0.0f,0.0f,0.0f,0.0f);
- glEnable(GL_DEPTH_TEST);
- Material();
- Lighting();
- return 0;
- }
- void CS045508View::OnDestroy()
- {
- CView::OnDestroy();
- // TODO: Add your message handler code here
- ::wglMakeCurrent(0, 0); // to remove the connection between the CDC and HGLRC
- ::wglDeleteContext(m_hRC);
- if (m_pDC) delete m_pDC; //to free the CDC and HGLRC variables
- }
- void CS045508View::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
- // TODO: Add your message handler code here
- glViewport(0, 0, cx, cy);
- SetupViewingFrustum((double)cx/(double)cy);
- if (cx <= 0 || cy <= 0) return;
- }
- BOOL CS045508View::SetupPixelFormat()
- {
- 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
- 16, // 16-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)
- return FALSE;
- if (!(::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd)))
- return FALSE;
- return TRUE;
- }
- //setup view port
- BOOL CS045508View::SetupViewingFrustum(double aspect)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, aspect, 1, 100.0);
- return TRUE;
- }
- BOOL CS045508View::SetupViewingTransform()
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(xpan, ypan, 10.0+zpan,
- xpan, ypan, 0.0,
- 0.0, 2.0, 0.0); //pan
- glRotatef(yRot, 0, 1, 0); //rotate
- glRotatef(zRot, 0, 0, 1);
- glRotatef(xRot, 1, 0, 0);
- return TRUE;
- }
- //creat texture
- BOOL CS045508View::Lighting()
- {
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- GLfloat light_position[] = {100, 100, 100, 0};
- GLfloat light_diffuse[] = {1, 1, 1, 1};
- GLfloat light_specular[] = {1,1,1,1};
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- return TRUE;
- }
- BOOL CS045508View::Material()
- {
- GLfloat mat_diffuse[] = {0, 1, 0.8f, 1};
- GLfloat mat_specular[] = {0, 0, 1, 1};
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- return TRUE;
- }
- //Draw 3D object
- BOOL CS045508View::RenderScene()
- {
- CS045508Doc *pDoc = GetDocument();
- int n,p;
- n=pDoc->ncon;
- p=3;
- if (pDoc->NormalArray.GetSize() > 0 && pDoc->VertexArray.GetSize() > 0)
- {
- int i=0;
- glShadeModel(GL_FLAT);
- if(ShadeOrWire==0){
- glEnable(GL_LIGHTING);
- glBegin(GL_TRIANGLES);}
- if(ShadeOrWire==1){
- glDisable(GL_LIGHTING);
- glBegin(GL_LINE_STRIP);}
- for (i=1;i<n;i++){
- glNormal3f(pDoc->NormalArray[i].x, pDoc->NormalArray[i].y,
- pDoc->NormalArray[i].z);
- glVertex3f(pDoc->VertexArray[p].x, pDoc->VertexArray[p].y,
- pDoc->VertexArray[p].z);
- glVertex3f(pDoc->VertexArray[p+1].x, pDoc->VertexArray[p+1].y,
- pDoc->VertexArray[p+1].z);
- glVertex3f(pDoc->VertexArray[p+2].x, pDoc->VertexArray[p+2].y,
- pDoc->VertexArray[p+2].z);
- p=p+3; //p+3 when i+1 as 3 vertex vs 1 normal vector
- }
- glEnd();
- }
- return TRUE;
- }
- //followings are rotation,panning, zooming and shading/wire-frame variables
- void CS045508View::OnTOOLBARxRotCCW()
- {
- // TODO: Add your command handler code here
- xRot += 5;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnTOOLBARxRotCW()
- {
- // TODO: Add your command handler code here
- xRot -= 5;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnTOOLBARyRotCCW()
- {
- // TODO: Add your command handler code here
- yRot += 5;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnTOOLBARyRotCW()
- {
- // TODO: Add your command handler code here
- yRot -= 5;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnTOOLBARzRotCCW()
- {
- // TODO: Add your command handler code here
- zRot += 5;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnTOOLBARzRotCW()
- {
- // TODO: Add your command handler code here
- zRot -= 5;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnToolbarXpanL()
- {
- // TODO: Add your command handler code here
- xpan+=1;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnToolbarXpanR()
- {
- // TODO: Add your command handler code here
- xpan-=1;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnToolbarYpanD()
- {
- // TODO: Add your command handler code here
- ypan+=1;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnToolbarYpanU()
- {
- // TODO: Add your command handler code here
- ypan-=1;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnZoomIn()
- {
- // TODO: Add your command handler code here
- zpan-=1;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnZoomOut()
- {
- // TODO: Add your command handler code here
- zpan+=1;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnShade()
- {
- // TODO: Add your command handler code here
- ShadeOrWire=0;
- InvalidateRect(0, FALSE);
- }
- void CS045508View::OnWireframe()
- {
- // TODO: Add your command handler code here
- ShadeOrWire=1;
- InvalidateRect(0, FALSE);
- }
- //MouseWheel ZOOMMING FUNCTION
- BOOL CS045508View::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
- {
- zpan+=(zDelta/60);
- InvalidateRect(0, FALSE);
- return CView::OnMouseWheel(nFlags, zDelta, pt);
- }
- //Direction Key Panning Function
- void CS045508View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- switch(nChar){
- case VK_UP:
- ypan-=1;
- break;
- case VK_DOWN:
- ypan+=1;
- break;
- case VK_LEFT:
- xpan+=1;
- break;
- case VK_RIGHT:
- xpan-=1;
- break;
- default: break;
- }
- InvalidateRect(0, FALSE);
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- //Mouse left button pressed
- void CS045508View::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- m_LeftDownPos = point;
- mouse=1;
- InvalidateRect(0, FALSE);
- CView::OnLButtonDown(nFlags, point);
- }
- //mouse move rotation
- void CS045508View::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if (mouse==1){
- xRot += (float)(point.y - m_LeftDownPos.y) / 3.0f;
- yRot += (float)(point.x - m_LeftDownPos.x) / 3.0f;
- m_LeftDownPos = point;
- InvalidateRect(0, FALSE);
- }
- CView::OnMouseMove(nFlags, point);
- }
- //mouse left button released
- void CS045508View::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- mouse=0;
- InvalidateRect(0, FALSE);
- CView::OnLButtonUp(nFlags, point);
- }
- void CS045508View::OnInitialUpdate()
- {
- CView::OnInitialUpdate();
- // TODO: Add your specialized code here and/or call the base class
- }