CBlueWindView:
CBlueWindView::CBlueWindView() { // TODO: add construction code here X_Angle=0.0; Y_Angle=0.0; m_translate = 1.0f; m_pDC = NULL; } CBlueWindView::~CBlueWindView() { } BOOL CBlueWindView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs //////////////////////////////////////////////////////////////// //ÉèÖô°¿ÚÀàÐÍ cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS; //////////////////////////////////////////////////////////////// return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CBlueWindView drawing void CBlueWindView::OnDraw(CDC* pDC) { CBlueWindDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC ); SwapBuffers(m_pDC->GetSafeHdc() ); RenderScene(); //äÖȾ³¡¾° } ///////////////////////////////////////////////////////////////////////////// // CBlueWindView message handlers //ÉèÖÃÏñËظñʽ BOOL CBlueWindView::SetupPixelFormat() { PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // pfd½á¹¹µÄ´óС 1, // °æ±¾ºÅ PFD_DRAW_TO_WINDOW | // Ö§³ÖÔÚ´°¿ÚÖлæͼ PFD_SUPPORT_OPENGL | // Ö§³Ö OpenGL PFD_DOUBLEBUFFER, // Ë«»º´æģʽ PFD_TYPE_RGBA, // RGBA ÑÕɫģʽ 24, // 24 λÑÕÉ«Éî¶È 0, 0, 0, 0, 0, 0, // ºöÂÔÑÕɫλ 0, // ûÓзÇ͸Ã÷¶È»º´æ 0, // ºöÂÔÒÆλλ 0, // ÎÞÀÛ¼Ó»º´æ 0, 0, 0, 0, // ºöÂÔÀÛ¼Óλ 32, // 32 λÉî¶È»º´æ 0, // ÎÞÄ£°å»º´æ 0, // ÎÞ¸¨Öú»º´æ PFD_MAIN_PLANE, // Ö÷²ã 0, // ±£Áô 0, 0, 0 // ºöÂÔ²ã,¿É¼ûÐÔºÍËð»ÙÑÚÄ£ }; int pixelformat; pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//Ñ¡ÔñÏñËظñʽ ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd); //ÉèÖÃÏñËظñʽ return TRUE; } //³õʼ»¯openGL³¡¾° BOOL CBlueWindView::InitializeOpenGL(CDC *pDC) { m_pDC = pDC; SetupPixelFormat(); //Éú³É»æÖÆÃèÊö±í m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc()); //Öõ±Ç°»æÖÆÃèÊö±í ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); return TRUE; } //²¶»ñWM_CREATEÏûÏ¢ int CBlueWindView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here //³õʼ»¯OpenGLºÍÉèÖö¨Ê±Æ÷ m_pDC = new CClientDC(this); SetTimer(1, 20, NULL); InitializeOpenGL(m_pDC); ////////////////////////////////////////////////////////////////// Init();//´Ëº¯ÊýÓйØÎÆÀíÓ³ÉäµÄ£¬ÔÚºóÃæ»áÉæ¼°µ½¡£ return 0; } //²¶»ñWM_DESTROYÏûÏ¢ void CBlueWindView::OnDestroy() { CView::OnDestroy(); // TODO: Add your message handler code here //ɾ³ýäÖȾÉÏÏÂÎÄ¡¢¶¨Ê±Æ÷ ::wglMakeCurrent(0,0); ::wglDeleteContext( m_hRC); if ( m_pDC ) { delete m_pDC; } KillTimer(1); } void CBlueWindView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here //Ìí¼Ó´°¿ÚËõ·ÅʱµÄͼÐα任º¯Êý glViewport(0,0,cx,cy); double m_dAspectRatio=double(cx)/double(cy); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(40.0,m_dAspectRatio,0.001f, 10000000.0f); glMatrixMode(GL_MODELVIEW); } // ³¡¾°»æÖÆÓëäÖȾ BOOL CBlueWindView::RenderScene() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); // Ðýת¶ÔÏóÒ»¸ö¸ø¶¨µÄ½Ç¶È glTranslatef(0,0,-3); glRotated(X_Angle,1.0,0.0,0.0); glRotated(Y_Angle,0.0,1.0,0.0); glScalef(m_translate,m_translate,m_translate); GLUquadricObj *pObj; pObj = gluNewQuadric(); glPushAttrib(GL_ALL_ATTRIB_BITS); glDisable(GL_BLEND); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); gluQuadricDrawStyle(pObj,GLU_FILL); gluQuadricNormals(pObj,GLU_SMOOTH); gluQuadricOrientation(pObj,GLU_OUTSIDE); gluQuadricTexture(pObj,GL_TRUE); glColor3f(1.0,1.0,1.0); gluSphere(pObj,1.0,100000,100000); glPopAttrib(); glPopMatrix(); gluDeleteQuadric(pObj); glFinish(); ::SwapBuffers(m_pDC->GetSafeHdc()); //½»»¥»º³åÇø return TRUE; } //äÖȾ³¡¾° ¼ÓÔØͼƬ AUX_RGBImageRec* CBlueWindView::LoadBMP(CString filename) { FILE *File=NULL; if (filename == _T("")) { return NULL; } File=fopen(filename,"r"); if (File) { fclose(File); return auxDIBImageLoad(filename); } return NULL; } //äÖȾ³¡¾° ¼ÓÔØÎÆÀí GLuint CBlueWindView::LoadTexture(CString filename, int Filter, int *ImgX, int *ImgY) { AUX_RGBImageRec *pImage = LoadBMP(filename); if(pImage == NULL) { AfxMessageBox("¶ÁÈ¡ÎÆÀí´íÎó!"); return 0; } GLuint textureID; glEnable(GL_TEXTURE_2D); glPixelStorei(GL_UNPACK_ALIGNMENT,1); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGB,pImage->sizeX,pImage->sizeY,GL_RGB,GL_UNSIGNED_BYTE,pImage->data); *ImgX = pImage->sizeX; *ImgY = pImage->sizeY; delete pImage->data;//GL_COMPRESSED_RGB delete pImage; return textureID; } void CBlueWindView::Init() { int texSizeX,texSizeY; m_EarthtexID=LoadTexture("Earth.bmp",0, &texSizeX, &texSizeY); } void CBlueWindView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default // ¼Ç¼Êó±êµÄλÖà MouseDownPoint=point; // ²¶×½Êó±êµÄÔ˶¯ SetCapture(); CView::OnLButtonDown(nFlags, point); } void CBlueWindView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default // Êó±ê°´ÏÂλÖÃÖÃÓÚÔµã MouseDownPoint=CPoint(0,0); // ½â³ý²¶×½Êó±ê ReleaseCapture(); CView::OnLButtonUp(nFlags, point); } void CBlueWindView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default // ¼ì²âÊÇ·ñ²¶×½Êó±ê if (GetCapture()==this)//µØÇòÐýת { // Ðýת¶ÔÏóÒ»¸ö²½³¤ X_Angle+=double(point.y-MouseDownPoint.y)/3.6; Y_Angle+=double(point.x-MouseDownPoint.x)/3.6; // ¸üг¡¾° Invalidate(FALSE); // ¼ÇÏÂÊó±êλÖà MouseDownPoint=point; }; if(nFlags == MK_RBUTTON)// Êó±êÓÒ¼ü±»°´Ï¡£µØÇòËõ·Å { m_translate *= 1+(point.y-MouseDownPoint.y)/100.0f; if(m_translate <= 0.1f) m_translate = 0.1f; Invalidate(FALSE); } MouseDownPoint=point; CView::OnMouseMove(nFlags, point); } BOOL CBlueWindView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { // TODO: Add your message handler code here and/or call default HCURSOR hCur = LoadCursor( NULL , MAKEINTRESOURCE(32649) ) ; ::SetCursor(hCur); return TRUE; }