www.pudn.com > bitmapsave.rar > gl2View.cpp, change:2005-12-07,size:22750b


// gl2View.cpp : implementation of the CGl2View class 
// 
 
#include "stdafx.h" 
#include "gl2.h" 
 
#include "gl2Doc.h" 
#include "gl2View.h" 
 
#include "gl\gl.h" 
#include "gl\glu.h" 
#include "gl\glaux.h" 
#include "math.h" 
#include "Hdibapi.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CGl2View 
 
IMPLEMENT_DYNCREATE(CGl2View, CView) 
 
BEGIN_MESSAGE_MAP(CGl2View, CView) 
	//{{AFX_MSG_MAP(CGl2View) 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_SIZE() 
	ON_WM_TIMER() 
	ON_COMMAND(IDM_PLAY_PAUSE, OnPlayPause) 
	ON_WM_LBUTTONDOWN() 
	ON_COMMAND(ID_FILE_SAVETOBMP_CLIENTBMP, OnFileSavetobmpClientbmp) 
	ON_COMMAND(ID_FILE_SAVETOBMP_RECTANGLEBMP, OnFileSavetobmpRectanglebmp) 
	ON_COMMAND(ID_FILE_SAVETOBMP_WINDOWSBMP, OnFileSavetobmpWindowsbmp) 
	ON_WM_LBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	//}}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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CGl2View construction/destruction 
 
CGl2View::CGl2View() 
{ 
		displayfashion = 0; mutumodel = 1; 
	m_pDC=NULL;m_pOldPalette=NULL; 
	X_Angle=10.0f; 
	 
	Y_Angle=1.0f; 
	Z_Angle=5.0f; 
	m_play=FALSE; 
 
 
		m_lightAmb[0]=0.2f;m_lightAmb[1]=0.2f; 
	 m_lightAmb[2]=0.2f;m_lightAmb[3]=1.0f; 
 
     m_lightDif[0]=1.0f; m_lightDif[1]=1.0f; 
	  m_lightDif[2]=0.3f; m_lightDif[3]=1.0f; 
 
     m_lightSpe[0]=1.0f;m_lightSpe[1]=1.0f; 
	 m_lightSpe[2]=1.0f;m_lightSpe[3]=1.0f; 
	  
     m_lightPos[0]=1.0f;m_lightPos[1]=1.0f; 
	 m_lightPos[2]=1.0f;m_lightPos[3]=0.0f; 
    
	// TODO: add construction code here 
 
} 
 
CGl2View::~CGl2View() 
{ 
} 
 
BOOL CGl2View::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); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CGl2View drawing 
 
void CGl2View::OnDraw(CDC* pDC) 
{ 
	CGl2Doc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
 
     DrawScene(); 
	// TODO: add draw code for native data here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CGl2View printing 
 
BOOL CGl2View::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CGl2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CGl2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CGl2View diagnostics 
 
#ifdef _DEBUG 
void CGl2View::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CGl2View::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CGl2Doc* CGl2View::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGl2Doc))); 
	return (CGl2Doc*)m_pDocument; 
} 
#endif //_DEBUG 
 
 
 
static unsigned char _threeto8[8] =  
{ 
	0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377 
}; 
 
static unsigned char _twoto8[4] =  
{ 
	0, 0x55, 0xaa, 0xff 
}; 
 
static unsigned char _oneto8[2] =  
{ 
	0, 255 
}; 
 
static int defaultOverride[13] =  
{ 
	0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91 
}; 
 
// Windows Default Palette 
static PALETTEENTRY defaultPalEntry[20] =  
{ 
	{ 0,   0,   0,    0 }, 
	{ 0x80,0,   0,    0 }, 
	{ 0,   0x80,0,    0 }, 
	{ 0x80,0x80,0,    0 }, 
	{ 0,   0,   0x80, 0 }, 
	{ 0x80,0,   0x80, 0 }, 
	{ 0,   0x80,0x80, 0 }, 
	{ 0xC0,0xC0,0xC0, 0 }, 
 
	{ 192, 220, 192,  0 }, 
	{ 166, 202, 240,  0 }, 
	{ 255, 251, 240,  0 }, 
	{ 160, 160, 164,  0 }, 
 
	{ 0x80,0x80,0x80, 0 }, 
	{ 0xFF,0,   0,    0 }, 
	{ 0,   0xFF,0,    0 }, 
	{ 0xFF,0xFF,0,    0 }, 
	{ 0,   0,   0xFF, 0 }, 
	{ 0xFF,0,   0xFF, 0 }, 
	{ 0,   0xFF,0xFF, 0 }, 
	{ 0xFF,0xFF,0xFF, 0 } 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
// CGl2View message handlers 
 
int CGl2View::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
	Init(); 
	return 0; 
} 
 
void CGl2View::OnDestroy()  
{ 
 
 
	HGLRC hrc; 
	hrc=::wglGetCurrentContext(); 
	::wglMakeCurrent(NULL,NULL); 
	if(hrc) 
		::wglDeleteContext(hrc); 
	if(m_pOldPalette) 
		m_pDC->SelectPalette(m_pOldPalette,FALSE); 
	if(m_pDC) 
		delete m_pDC; 
	 
	CView::OnDestroy(); 
	 
	// TODO: Add your message handler code here 
	 
} 
 
void CGl2View::OnSize(UINT nType, int cx, int cy)  
{ 
 
 
 
    if(cy>0) 
	{ 
		if((m_oldRect.right>cx)||(m_oldRect.bottom>cy)) 
			RedrawWindow(); 
		m_oldRect.right=cx; 
		m_oldRect.bottom=cy; 
		glMatrixMode(GL_PROJECTION); 
		glLoadIdentity(); 
		     glFrustum(-1.0,1.0,-1.0,1.0,3.0,7.0); 
			 glViewport(0,0,cx,cy); 
	} 
 
 
 
 
	CView::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	 
} 
void CGl2View::Init() 
{ 
	PIXELFORMATDESCRIPTOR pfd; 
	int n; 
	HGLRC hrc; 
	m_pDC=new CClientDC(this); 
	ASSERT(m_pDC!=NULL); 
	if(!bSetupPixelFormat()) return; 
		 
	n = ::GetPixelFormat(m_pDC->GetSafeHdc()); 
    ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd); 
	CreateRGBPalette();	 
    hrc=wglCreateContext(m_pDC->GetSafeHdc()); 
	wglMakeCurrent(m_pDC->GetSafeHdc(),hrc); 
 
 
 
	GetClientRect(&m_oldRect); 
	glClearDepth(1.0f); 
	glEnable(GL_DEPTH_TEST); 
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
 
} 
BOOL CGl2View::bSetupPixelFormat() 
{ 
		// define default desired video mode (pixel format) 
	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 
        32,                             // 32-bit z-buffer 
        0,                              // no stencil buffer 
        0,                              // no auxiliary buffer 
        PFD_MAIN_PLANE,                 // main layer 
        0,                              // reserved 
        0, 0, 0                         // layer masks ignored 
    }; 
	// let the user change some parameters if he wants 
 
    int pixelformat; 
	if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 ) 
    { 
        AfxMessageBox("ChoosePixelFormat failed"); 
        return FALSE; 
    } 
 
	// try to set this video mode     
	if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE) 
    { 
		// the requested video mode is not available so get a default one 
        pixelformat = 1;	 
		if (DescribePixelFormat(m_pDC->GetSafeHdc(), pixelformat, sizeof(PIXELFORMATDESCRIPTOR), &pfd)==0) 
		{ 
			// neither the requested nor the default are available: fail 
			AfxMessageBox("SetPixelFormat failed (no OpenGL compatible video mode)"); 
			return FALSE; 
		} 
    } 
 
    return TRUE; 
 
} 
void CGl2View::DrawScene() 
{ 
 
	static BOOL bBusy=FALSE; 
	if(bBusy) return; 
	bBusy=TRUE; 
 
	glClearColor(1.0f,1.0f,1.0f,1.0f); 
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
	 
	glColor3f(1.0f,0.2f,0.5f); 
     
	glMatrixMode(GL_MODELVIEW); 
	glLoadIdentity(); 
	 
	glTranslatef(0.0f,0.0f,-4.5f); 
	CreatLights(); 
 
		 
	 
     
	glPushMatrix(); 
	  glRotatef(X_Angle,1.0f,0.0f,0.0f); 
 
	  glRotatef(Y_Angle,0.0f,1.0f,0.0f); 
	  glRotatef(Z_Angle,0.0f,0.0f,1.0f); 
	   
	   
     DrawCube(); 
 
//	   glBegin(GL_POLYGON); 
//	          glVertex3f(-0.5f,0.5f,0.5f); 
//			  glVertex3f(-0.5f,-0.5f,0.5f); 
//			  glVertex3f(0.5f,-0.5f,0.5f); 
//			  glVertex3f(0.5f,0.5f,0.5f); 
//       glEnd(); 
// 
//	   glBegin(GL_POLYGON); 
//	          glVertex3f(-0.5f,-0.5f,0.5f); 
//			  glVertex3f(-0.5f,0.5f,0.5f); 
//			  glVertex3f(-0.5f,0.5f,-0.5f); 
//			  glVertex3f(-0.5f,-0.5f,-0.5f); 
// 
//       glEnd(); 
// 
// 
//	   glBegin(GL_POLYGON); 
//	          glVertex3f(-0.5f,-0.5f,0.5f); 
//			  glVertex3f(-0.5f,0.5f,0.5f); 
//			  glVertex3f(-0.5f,0.5f,-0.5f); 
//			  glVertex3f(-0.5f,-0.5f,-0.5f); 
//       glEnd(); 
// 
//       glBegin(GL_POLYGON); 
//	          glVertex3f(0.5f,-0.5f,0.5f); 
//			  glVertex3f(0.5f,-0.5f,-0.5f); 
//			  glVertex3f(0.5f,0.5f,-0.5f); 
//			  glVertex3f(0.5f,0.5f,0.5f); 
// 
//       glEnd(); 
// 
// 
//	   glBegin(GL_POLYGON); 
//	          glVertex3f(-0.5f,-0.5f,0.5f); 
//			  glVertex3f(0.5f,-0.5f,0.5f); 
//			  glVertex3f(0.5f,-0.5f,-0.5f); 
//			  glVertex3f(-0.5f,-0.5f,-0.5f); 
//       glEnd(); 
//			     
// 
//	   glBegin(GL_POLYGON); 
//	          glVertex3f(-0.5f,0.5f,0.5f); 
//			  glVertex3f(0.5f,0.5f,0.5f); 
//			  glVertex3f(0.5f,0.5f,-0.5f); 
//			  glVertex3f(-0.5f,0.5f,-0.5f); 
//       glEnd(); 
 
	   glPopMatrix(); 
		glFinish(); 
 
		SwapBuffers(wglGetCurrentDC()); 
		bBusy=FALSE; 
 
 
	 
 
	 
 
} 
void CGl2View::GLTextOut(GLuint id,const char * const textstring) 
{ 
	if ( 0 == id || 0 == textstring ) 
		{ 
    		return; 
		} 
 
	GLsizei size = strlen( textstring ); 
 
	::glListBase( id ); 
	::glCallLists( size, GL_UNSIGNED_BYTE, (const GLvoid*)textstring );  
 
} 
void CGl2View::CreateRGBPalette() 
{ 
    PIXELFORMATDESCRIPTOR pfd; 
    LOGPALETTE *pPal; 
    int n, i; 
 
	// get the initially choosen video mode 
	n = ::GetPixelFormat(m_pDC->GetSafeHdc()); 
    ::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd); 
 
	// if is an indexed one... 
    if (pfd.dwFlags & PFD_NEED_PALETTE) 
    { 
        // ... construct an equilibrated palette (3 red bits, 3 green bits, 2 blue bits) 
        // NOTE: this code is integrally taken from MFC example Cube 
		n = 1 << pfd.cColorBits; 
        pPal = (PLOGPALETTE) new char[sizeof(LOGPALETTE) + n * sizeof(PALETTEENTRY)]; 
 
        ASSERT(pPal != NULL); 
 
        pPal->palVersion = 0x300; 
        pPal->palNumEntries = n; 
        for (i=0; i<n; i++) 
        { 
            pPal->palPalEntry[i].peRed=ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift); 
            pPal->palPalEntry[i].peGreen=ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift); 
            pPal->palPalEntry[i].peBlue=ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift); 
            pPal->palPalEntry[i].peFlags=0; 
        } 
 
		// fix up the palette to include the default Windows palette 
        if ((pfd.cColorBits == 8)                           && 
            (pfd.cRedBits   == 3) && (pfd.cRedShift   == 0) && 
            (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) && 
            (pfd.cBlueBits  == 2) && (pfd.cBlueShift  == 6) 
           ) 
        { 
			for (i = 1 ; i <= 12 ; i++) 
                pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i]; 
        } 
 
        m_cPalette.CreatePalette(pPal); 
        delete pPal; 
 
		// set the palette 
        m_pOldPalette=m_pDC->SelectPalette(&m_cPalette, FALSE); 
        m_pDC->RealizePalette(); 
    } 
} 
 
unsigned char CGl2View::ComponentFromIndex(int i, UINT nbits, UINT shift) 
{ 
    unsigned char val; 
 
    val = (unsigned char) (i >> shift); 
    switch (nbits)  
	{ 
 
    case 1: 
        val &= 0x1; 
        return _oneto8[val]; 
    case 2: 
        val &= 0x3; 
        return _twoto8[val]; 
    case 3: 
        val &= 0x7; 
        return _threeto8[val]; 
 
    default: 
        return 0; 
    } 
} 
 
void CGl2View::OnTimer(UINT nIDEvent)  
{ 
	m_fAngleY+=10.f; 
	m_fAngleX+=1.0f; 
	m_fAngleZ+=5.0f; 
	Invalidate(FALSE); 
	// TODO: Add your message handler code here and/or call default 
	 
	CView::OnTimer(nIDEvent); 
} 
 
void CGl2View::OnPlayPause()  
{ 
	m_play=m_play?FALSE:TRUE; 
	if(m_play) 
		SetTimer(1,45,NULL); 
	else 
		KillTimer(1); 
	// TODO: Add your command handler code here 
	 
} 
void CGl2View::DrawCube() 
{ 
	 
 
 
 
	glPushMatrix(); 
	     glRotatef(30.0f,1.0f,0.0f,0.0f); 
 
         glRotatef(20.0f,0.0f,1.0f,0.0f); 
 
	     glScalef(1.2f,1.2f,1.2f); 
	      
		 double dNormal[3]; 
 /////////////////////////////在前面 
		  
		 double dPoint1[3]={-0.5,0.5,0.5}; 
	     double dPoint2[3]={-0.5,-0.5,0.5}; 
	     double dPoint3[3]={0.5,-0.5,0.5}; 
		 double dPoint4[3]={0.5,0.5,0.5}; 
		 CalculateNormal(dPoint1,dPoint2,dPoint3,dNormal); 
		  
         glBegin(GL_POLYGON); 
		      glNormal3dv(dNormal); 
 
	          glVertex3f(-0.5f,0.5f,0.5f); 
			  glVertex3f(-0.5f,-0.5f,0.5f); 
			  glVertex3f(0.5f,-0.5f,0.5f); 
			  glVertex3f(0.5f,0.5f,0.5f); 
       glEnd(); 
           
//////////////////////////////在右面 
 
//	      dPoint1[3]={0.5,0.5,0.5}; 
//	      dPoint2[3]={0.5,-0.5,0.5}; 
//	      dPoint3[3]={0.5,-0.5,-0.5}; 
//		  dPoint4[3]={0.5,0.5,-0.5}; 
 
	   dPoint1[0]=0.5;dPoint1[1]=0.5;dPoint1[2]=0.5; 
	   dPoint2[0]=0.5;dPoint2[1]=-0.5;dPoint2[2]=0.5; 
	   dPoint3[0]=0.5;dPoint3[1]=-0.5;dPoint3[2]=-0.5; 
	   dPoint4[0]=0.5;dPoint4[1]=0.5;dPoint4[2]=-0.5; 
 
 
		 CalculateNormal(dPoint1,dPoint2,dPoint3,dNormal); 
         glBegin(GL_POLYGON);  
              glNormal3dv(dNormal); 
 
	          glVertex3f(0.5f,0.5f,0.5f); 
			  glVertex3f(0.5f,-0.5f,0.5f); 
			  glVertex3f(0.5f,-0.5f,-0.5f); 
			  glVertex3f(0.5f,0.5f,-0.5f); 
 
       glEnd(); 
 
///////////////////////在后面 
//          dPoint1[3]={0.5,0.5,-0.5}; 
//	      dPoint2[3]={0.5,-0.5,-0.5}; 
//	      dPoint3[3]={-0.5,-0.5,-0.5}; 
//		  dPoint4[3]={-0.5,0.5,-0.5}; 
 
 
       dPoint1[0]=0.5;dPoint1[1]=0.5;dPoint1[2]=-0.5; 
	   dPoint2[0]=0.5;dPoint2[1]=-0.5;dPoint2[2]=-0.5; 
	   dPoint3[0]=-0.5;dPoint3[1]=-0.5;dPoint3[2]=-0.5; 
	   dPoint4[0]=-0.5;dPoint4[1]=0.5;dPoint4[2]=-0.5; 
		 CalculateNormal(dPoint1,dPoint2,dPoint3,dNormal); 
         glBegin(GL_POLYGON); 
	          glNormal3dv(dNormal); 
 
	          glVertex3f(0.5f,0.5f,-0.5f); 
			  glVertex3f(0.5f,-0.5f,-0.5f); 
			  glVertex3f(-0.5f,-0.5f,-0.5f); 
			  glVertex3f(-0.5f,0.5f,-0.5f); 
       glEnd(); 
////////////////////////在左面 
//	     dPoint1[3]={-0.5,0.5,-0.5}; 
//	     dPoint2[3]={-0.5,-0.5,-0.5}; 
//	      dPoint3[3]={-0.5,-0.5,0.5}; 
//		  dPoint4[3]={-0.5,0.5,0.5}; 
	    dPoint1[0]=-0.5;dPoint1[1]=0.5;dPoint1[2]=-0.5; 
	   dPoint2[0]=-0.5;dPoint2[1]=-0.5;dPoint2[2]=-0.5; 
	   dPoint3[0]=-0.5;dPoint3[1]=-0.5;dPoint3[2]=0.5; 
	   dPoint4[0]=-0.5;dPoint4[1]=0.5;dPoint4[2]=0.5; 
		 CalculateNormal(dPoint1,dPoint2,dPoint3,dNormal); 
 
       glBegin(GL_POLYGON); 
	          glNormal3dv(dNormal); 
 
	          glVertex3f(-0.5f,0.5f,-0.5f); 
			  glVertex3f(-0.5f,-0.5f,-0.5f); 
			  glVertex3f(-0.5f,-0.5f,0.5f); 
			  glVertex3f(-0.5f,0.5f,0.5f); 
 
       glEnd(); 
 
///////////////////////在下面 
 
//	     dPoint1[3]={-0.5,-0.5,0.5}; 
//	     dPoint2[3]={-0.5,-0.5,-0.5}; 
//	     dPoint3[3]={0.5,-0.5,-0.5}; 
//		 dPoint4[3]={0.5,-0.5,-0.5}; 
	    dPoint1[0]=-0.5;dPoint1[1]=-0.5;dPoint1[2]=0.5; 
	   dPoint2[0]=-0.5;dPoint2[1]=-0.5;dPoint2[2]=-0.5; 
	   dPoint3[0]=0.5;dPoint3[1]=-0.5;dPoint3[2]=-0.5; 
	   dPoint4[0]=0.5;dPoint4[1]=-0.5;dPoint4[2]=-0.5; 
		 CalculateNormal(dPoint1,dPoint2,dPoint3,dNormal); 
	   glBegin(GL_POLYGON); 
	          glNormal3dv(dNormal); 
 
	          glVertex3f(-0.5f,-0.5f,0.5f); 
			  glVertex3f(-0.5f,-0.5f,-0.5f); 
			  glVertex3f(0.5f,-0.5f,-0.5f); 
			  glVertex3f(0.5f,-0.5f,-0.5f); 
       glEnd(); 
			     
///////////////////在上面 
//	     dPoint1[3]={-0.5,0.5,-0.5}; 
//	     dPoint2[3]={-0.5,0.5,0.5}; 
//	      dPoint3[3]={0.5,0.5,0.5}; 
//		  dPoint4[3]={0.5,0.5,-0.5}; 
	    dPoint1[0]=-0.5;dPoint1[1]=0.5;dPoint1[2]=-0.5; 
	   dPoint2[0]=-0.5;dPoint2[1]=0.5;dPoint2[2]=0.5; 
	   dPoint3[0]=0.5;dPoint3[1]=0.5;dPoint3[2]=0.5; 
	   dPoint4[0]=0.5;dPoint4[1]=0.5;dPoint4[2]=-0.5; 
		 CalculateNormal(dPoint1,dPoint2,dPoint3,dNormal); 
	   glBegin(GL_POLYGON); 
	          glNormal3dv(dNormal); 
			   
	          glVertex3f(-0.5f,0.5f,-0.5f); 
			  glVertex3f(-0.5f,0.5f,0.5f); 
			  glVertex3f(0.5f,0.5f,0.5f); 
			  glVertex3f(0.5f,0.5f,-0.5f); 
       glEnd(); 
 
	 glPopMatrix(); 
	 
 
	 
 
	 
 
} 
void CGl2View::CalculateNormal(GLdouble* dVertex1,GLdouble* dVertex2,GLdouble* dVertex3,GLdouble* dNormal) 
{ 
 
    GLdouble dVector1[3],dVector2[3]; 
	dVector1[0]=dVertex2[0]-dVertex1[0]; 
	dVector1[1]=dVertex2[1]-dVertex1[1]; 
	dVector1[2]=dVertex2[2]-dVertex1[2]; 
 
	dVector2[0]=dVertex3[0]-dVertex1[0]; 
	dVector2[1]=dVertex3[1]-dVertex1[1]; 
	dVector2[2]=dVertex3[2]-dVertex1[2]; 
 
	dNormal[0]=dVector1[1]*dVector2[2]-dVector1[2]*dVector2[1]; 
	dNormal[1]=dVector1[2]*dVector2[0]-dVector1[0]*dVector2[2]; 
	dNormal[2]=dVector1[0]*dVector2[1]-dVector1[1]*dVector2[0]; 
 
	double dNormalLength=sqrt(dNormal[0]*dNormal[0]+dNormal[1]*dNormal[1]+dNormal[2]*dNormal[2]); 
     
	if(dNormalLength!=0.0) 
	{ 
		dNormal[0]=dNormal[0]/dNormalLength; 
		dNormal[1]=dNormal[1]/dNormalLength; 
		dNormal[2]=dNormal[2]/dNormalLength; 
 
	} 
	else 
	{ 
		dNormal[0]=0.0; 
		dNormal[1]=0.0; 
		dNormal[2]=0.0; 
 
	} 
 
 
} 
void CGl2View::CreatLights() 
{ 
	glLightfv(GL_LIGHT0,GL_AMBIENT,m_lightAmb); 
	glLightfv(GL_LIGHT0,GL_DIFFUSE,m_lightDif); 
	glLightfv(GL_LIGHT0,GL_SPECULAR,m_lightSpe); 
 
	glLightfv(GL_LIGHT0,GL_POSITION,m_lightPos); 
	glEnable(GL_LIGHTING); 
	glEnable(GL_LIGHT0); 
} 
 
void CGl2View::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
 
	if (!m_bSaveRectBMP) 
	{ 
		MouseDownPoint=point; 
		// capture mouse movements even outside window borders 
		SetCapture(); 
	} 
	else 
	{ 
		c_down = point; 
		c_last = point; 
		Zoom_in_First = point;        //保存矩形区域的左上角坐标 
		SetCapture();                 //设置输入焦点 
		::SetCursor ( AfxGetApp()->LoadStandardCursor( IDC_SIZENWSE ) ); //改变鼠标光标状态 
	} 
	// TODO: Add your message handler code here and/or call default 
	 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CGl2View::OnFileSavetobmpClientbmp()  
{ 
	m_nSaveBMPFlag = BMPCLIENT; 
	SaveToBMP();	 
	// TODO: Add your command handler code here 
	 
} 
 
void CGl2View::OnFileSavetobmpRectanglebmp()  
{ 
	m_nSaveBMPFlag = BMPRECT; 
	m_bSaveRectBMP = TRUE; 
	// TODO: Add your command handler code here 
	 
} 
 
void CGl2View::OnFileSavetobmpWindowsbmp()  
{ 
	m_nSaveBMPFlag = BMPWINDOW; 
	SaveToBMP(); 
	// TODO: Add your command handler code here 
	 
} 
void CGl2View::SaveToBMP() 
{ 
	GetDIB(); 
	if( hDIB == NULL ) 
	{ 
		MessageBox("HDIB handle is NULL, save abort !!!"); 
		return; 
	} 
	CFileDialog  dilg(FALSE, NULL, "*.bmp", OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, 
					  "Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*||"); 
												    
	dilg.m_ofn.lpstrTitle="保存位图对话框"; 
	if( dilg.DoModal() != IDOK ) 
	{ 
		FreeDIB(); 
		return; 
	} 
	CString pszPathName = dilg.GetPathName(); 
 
	CFile file; 
	CFileException fe; 
 
	if (!file.Open(pszPathName, CFile::modeCreate | 
	  CFile::modeReadWrite | CFile::shareExclusive, &fe)) 
	{ 
		FreeDIB(); 
		MessageBox("不能保存为此位图 <" + pszPathName + "> !!!"); 
		return; 
	} 
 
	BOOL bSuccess = FALSE; 
	TRY 
	{ 
		BeginWaitCursor(); 
	  //bSuccess = ::SaveDIB(hDIB, (HANDLE)file.m_hFile); 
	    bSuccess=::SaveDIB(hDIB,(HANDLE)file.m_hFile); 
		file.Close(); 
	} 
	CATCH (CException, eSave) 
	{ 
		file.Abort(); // will not throw an exception 
		EndWaitCursor(); 
		FreeDIB(); 
		MessageBox("不能保存此位图文件 <" + pszPathName + "> !!!"); 
		return; 
	} 
	END_CATCH 
	EndWaitCursor(); 
	if (!bSuccess)   
	{ 
		FreeDIB(); 
		MessageBox("不能保存为此位图 <" + pszPathName + "> !!!"); 
	} 
 
	FreeDIB(); 
} 
void CGl2View::GetDIB() 
{ 
	if(hDIB != NULL) 
	{ 
		::GlobalUnlock(hDIB); 
		::GlobalFree(hDIB); 
		hDIB = NULL; 
	} 
	switch(m_nSaveBMPFlag) 
	{ 
	case BMPWINDOW: 
		hDIB = ::CopyWindowToDIB(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), m_nSaveBMPFlag); 
		break; 
	case BMPCLIENT: 
		hDIB = ::CopyWindowToDIB(GetSafeHwnd(), m_nSaveBMPFlag); 
		break; 
	case BMPRECT: 
		hDIB = ::CopyRectToDIB(GetSafeHwnd(), &m_Rect); 
		break; 
	} 
} 
 
void CGl2View::FreeDIB() 
{ 
	::GlobalUnlock(hDIB); 
	::GlobalFree(hDIB); 
	hDIB = NULL; 
} 
 
void CGl2View::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	if (!m_bSaveRectBMP) 
	{ 
		// forget where we clicked 
		MouseDownPoint=CPoint(0,0); 
		// release mouse capture 
		ReleaseCapture(); 
	} 
	else 
	{ 
		Zoom_in_Last = point;           //保存矩形区域的右下角坐标 
        ::SetCursor ( AfxGetApp()->LoadStandardCursor( IDC_ARROW ) ); //改变鼠标光标状态 
        ReleaseCapture();               //释放鼠标输入焦点 
		m_bSaveRectBMP = FALSE; 
		m_Rect.left	  = Zoom_in_First.x + 1; 
		m_Rect.top    = Zoom_in_First.y + 1; 
		m_Rect.right  = Zoom_in_Last.x  - 1; 
		m_Rect.bottom = Zoom_in_Last.y  - 1; 
		c_down.x = c_down.y = 0; 
		c_last.x = c_last.y = 0; 
		SaveToBMP(); 
	} 
	// TODO: Add your message handler code here and/or call default 
	 
	CView::OnLButtonUp(nFlags, point); 
} 
 
void CGl2View::OnMouseMove(UINT nFlags, CPoint point)  
{ 
 
	CGl2Doc* pDoc=GetDocument(); 
	ASSERT_VALID(pDoc); 
	if (!m_bSaveRectBMP) 
	{ 
	 
//			// check if we have captured the mouse 
			if (GetCapture()==this) 
			{ 
				// increment the object rotation angles 
				X_Angle-=double(point.y-MouseDownPoint.y)/3.6; 
			    Y_Angle+=double(point.x-MouseDownPoint.x)/3.6; 
				Z_Angle+=double(point.x-MouseDownPoint.x)/3.6;//double(point.x+point.y-MouseDownPoint.x-MouseDownPoint.y)/3.6; 
				// remember the mouse point 
				MouseDownPoint=point; 
				 
			};Invalidate(FALSE); 
//		case 2: 
//			if(GetCapture()==this) 
//			{ 
//				X_Trans+=double(point.x-MouseDownPoint.x)/64; 
//				Y_Trans-=double(point.y-MouseDownPoint.y)/64; 
//				MouseDownPoint=point; 
//			}; 
 
	}//endif 
	else  
	{ 
		if (GetCapture() == this) 
		{ 
    		CClientDC dc(this); 
    		OnPrepareDC(&dc); 
    		dc.DPtoLP(&point); 
    		//下面是象皮条拉出一个矩形框 
    	    CRect rect(c_down.x, c_down.y, c_last.x, c_last.y); 
    	    rect.NormalizeRect(); 
    	    dc.DrawFocusRect(rect); 
    	    rect.SetRect(c_down.x, c_down.y, point.x, point.y); 
    	    rect.NormalizeRect(); 
			dc.DrawFocusRect(rect); 
    		c_last = point; 
		}//endif 
	}//endelse 
	// TODO: Add your message handler code here and/or call default 
	 
	CView::OnMouseMove(nFlags, point); 
}