www.pudn.com > DEM_TDS.rar > GLEnabledView.cpp, change:2007-07-30,size:11032b


// GLEnabledView.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "GLEnabledView.h" 
#include "MainFrm.h" 
#include "PointAttribData.h" 
#include "math.h" 
#define PI 3.14159265 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CGLEnabledView 
 
IMPLEMENT_DYNCREATE(CGLEnabledView, CView) 
 
CGLEnabledView::CGLEnabledView() 
{ 
	draw_3D=true; 
	moving=true; 
} 
 
CGLEnabledView::~CGLEnabledView() 
{ 
} 
 
 
BEGIN_MESSAGE_MAP(CGLEnabledView, CView) 
	//{{AFX_MSG_MAP(CGLEnabledView) 
	ON_WM_CREATE() 
	ON_WM_DESTROY() 
	ON_WM_SIZE() 
	ON_WM_ERASEBKGND() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CGLEnabledView drawing 
 
BOOL CGLEnabledView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	return true; 
	//	return CView::OnEraseBkgnd(pDC); 
} 
 
 
void CGLEnabledView::OnDraw(CDC* pDC) 
{ 
	CDocument* pDoc = GetDocument(); 
	// TODO: add draw code here 
		// 准备一个标志信号 
	static BOOL 	bBusy = FALSE; 
	if(bBusy) return;		//如果忙,则返回 
	bBusy = TRUE;			//置标志为忙,下面开始场景绘制 
 
 
	// 获取设备描述表 
	wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 
 
	// 清除颜色缓冲区和深度缓冲区 
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
 
	// 调用场景绘制函数 
	if(draw_3D)OnDrawGL(); 
 
	// 完成图形的绘制 
	glFinish(); 
 
	// 启用双缓冲 
	SwapBuffers(m_pDC->GetSafeHdc()); 
	 
	// 绘制完毕,置标志为空闲 
	bBusy = FALSE; 
 
	// 释放OpenGL绘制描述表 
    wglMakeCurrent(NULL,NULL); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CGLEnabledView diagnostics 
 
#ifdef _DEBUG 
void CGLEnabledView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CGLEnabledView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CGLEnabledView message handlers 
 
int CGLEnabledView::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
 
	InitializeOpenGL(); 
	return 0; 
} 
 
void CGLEnabledView::OnDestroy()  
{ 
	// 获得设备描述表的OpenGL绘制描述表 
    wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 
	// 释放绘制描述表 
	if (m_hRC!=NULL) ::wglDeleteContext(m_hRC); 
	if (m_hPalette) DeleteObject(m_hPalette);//释放调色板 
	// 释放Windows设备描述表 
	if(m_pDC) delete m_pDC; 
	KillTimer(0); 
	CView::OnDestroy(); 
	// TODO: Add your message handler code here 
} 
 
void CGLEnabledView::OnSize(UINT nType, int cx, int cy)  
{ 
	CView::OnSize(nType, cx, cy); 
	 
	// TODO: Add your message handler code here 
	if ( 0 < cx && 0 < cy ) 
	{ 
	// 更新客户区,计算窗口的比例 
		m_ClientRect.right = cx; 
		m_ClientRect.bottom = cy; 
		m_dAspectRatio=double(cx)/double(cy); 
 
	// 获取OpenGL绘制描述表 
		wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 
	// 设置视口变换 
		glViewport(0,0,cx,cy); 
 
	// 设置透视变换 
 		glPushMatrix(); 
		glMatrixMode(GL_PROJECTION); 
		glLoadIdentity(); 
 
		gluPerspective(50,m_dAspectRatio,0.1f, 10000.0f); 
//		glOrtho(-5*m_dAspectRatio,5*m_dAspectRatio,-5,5,0,100); 
		glTranslatef(0.0f,0.0f,-10.f); 
		gluLookAt(0,0,1,0,0,0,0,1,0); 
		glMatrixMode(GL_MODELVIEW); 
		glPopMatrix(); 
 
		glDrawBuffer (GL_BACK); 
		glEnable(GL_DEPTH_TEST); 
 
	glEnable(GL_LIGHTING); 
	// 释放OpenGL绘制描述表 
		wglMakeCurrent(NULL,NULL); 
	// 更新窗口内容	 
		Invalidate(TRUE); 
	} 
} 
 
BOOL CGLEnabledView::PreCreateWindow(CREATESTRUCT& cs)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
//////////////////////////////////////////////////////////////////////////// 
//设置窗口风格 
	//OpenGL要求 
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; 
	//多文档应用程序要求 
	cs.lpszClass = AfxRegisterWndClass(CS_OWNDC | CS_HREDRAW | CS_VREDRAW); 
///////////////////////////////////////////////////////////////////////////// 
	 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////// 
//	                  设置逻辑调色板 
////////////////////////////////////////////////////////////////////// 
void CGLEnabledView::SetLogicalPalette(void) 
{ 
    struct 
    { 
        WORD Version; 
        WORD NumberOfEntries; 
        PALETTEENTRY aEntries[256]; 
    } logicalPalette = { 0x300, 256 }; 
 
	BYTE reds[] = {0, 36, 72, 109, 145, 182, 218, 255}; 
	BYTE greens[] = {0, 36, 72, 109, 145, 182, 218, 255}; 
	BYTE blues[] = {0, 85, 170, 255};

    for (int colorNum=0; colorNum<256; ++colorNum)
    {
        logicalPalette.aEntries[colorNum].peRed =
            reds[colorNum & 0x07];
        logicalPalette.aEntries[colorNum].peGreen =
            greens[(colorNum >> 0x03) & 0x07];
        logicalPalette.aEntries[colorNum].peBlue =
            blues[(colorNum >> 0x06) & 0x03];
        logicalPalette.aEntries[colorNum].peFlags = 0;
    }

    m_hPalette = CreatePalette ((LOGPALETTE*)&logicalPalette);
}


//////////////////////////////////////////////////////////
//						初始化openGL场景
//////////////////////////////////////////////////////////
BOOL CGLEnabledView::InitializeOpenGL()
{ 
	m_pDC = new CClientDC(this); 
	// 初始化OpenGL 
	SetupPixelFormat();
	//生成绘制描述表
	m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
	//置当前绘制描述表
	::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); 
 
	glClearColor(1,1,1,0); 
	glShadeModel(GL_SMOOTH); 
	glPolygonMode(GL_FRONT,GL_FILL); 
	glPolygonMode(GL_BACK,GL_FILL); 
	glEnable(GL_LIGHT0); 
	// Gouraud 
	BuildTexture(); 
    BuildList(); 
 
 	// 释放OpenGL绘制描述表    
	wglMakeCurrent(NULL,NULL); 
	return TRUE;
}

//////////////////////////////////////////////////////////
//						设置像素格式
//////////////////////////////////////////////////////////
BOOL CGLEnabledView::SetupPixelFormat()
{
	PIXELFORMATDESCRIPTOR pfd = { 
	    sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
	    1,                                // 版本号 
	    PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
	    PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
	    PFD_DOUBLEBUFFER| 
		PFD_STEREO_DONTCARE,                 // 双缓存模式 
	    PFD_TYPE_RGBA,                    // RGBA 颜色模式 
	    32,                               // 24 位颜色深度 
	    8, 16, 8, 8, 8, 0,                 // 忽略颜色位 
	    0,                                // 没有非透明度缓存 
	    0,                                // 忽略移位位 
	    64,                                // 无累加缓存 
	    16, 16, 16, 0,                       // 忽略累加位 
	    32,                               // 32 位深度缓存     
	    8,                                // 无模板缓存 
	    0,                                // 无辅助缓存 
	    PFD_MAIN_PLANE,                   // 主层 
	    0,                                // 保留 
	    0, 0, 0                           // 忽略层,可见性和损毁掩模 
	}; 	
	int pixelformat;
	pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);//选择像素格式
	::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd);	//设置像素格式
	if(pfd.dwFlags & PFD_NEED_PALETTE)
		SetLogicalPalette();	//设置逻辑调色板
	return TRUE;
} 
 
void CGLEnabledView::OnDrawGL()
{ 

} 
 
void CGLEnabledView::BuildList() 
{ 
	::glNewList(2,GL_COMPILE); 
	glShadeModel(GL_SMOOTH); 
 
	float x = 1.5; 
	float xt = 5.0f; 
	glBegin(GL_POLYGON); 
//		glNormal3d(0.0,1.0,1.0); 
		glColor3ub(255,0,0); 
		glVertex3d( x,  x, x); 
		glColor3ub(0,255,0); 
		glVertex3d( x, -x, x); 
		glColor3ub(0,0,255); 
		glVertex3d(-x, -x, x); 
		glColor3ub(255,255,0); 
		glVertex3d(-x,  x, x); 
	glEnd(); 
 
	glBegin(GL_POLYGON); 
//		glNormal3d(0.0,0.0,-1.0); 
		glColor3ub(255,0,0); 
		glVertex3d( -x,  x, -x); 
		glColor3ub(0,255,0); 
		glVertex3d( -x, -x, -x); 
		glColor3ub(0,0,255); 
		glVertex3d(  x, -x, -x); 
		glColor3ub(255,255,0); 
		glVertex3d(  x,  x, -x); 
	glEnd(); 
 
 
	glBegin(GL_POLYGON); 
//		glNormal3d(1.0,0.0,0.0); 
		glColor3ub(255,0,0); 
		glVertex3d( x, x, x); 
		glColor3ub(0,255,0); 
		glVertex3d( x, x, -x); 
		glColor3ub(0,0,255); 
		glVertex3d( x, -x, -x); 
		glColor3ub(255,255,0); 
		glVertex3d( x, -x, x); 
	glEnd(); 
 
	glBegin(GL_POLYGON); 
//		glNormal3d(-1.0,0.0,0.0); 
		glColor3ub(255,0,0); 
		glVertex3d( -x, x, x); 
		glColor3ub(0,255,0); 
		glVertex3d( -x, x, -x); 
		glColor3ub(0,0,255); 
		glVertex3d( -x, -x, -x); 
		glColor3ub(255,255,0); 
		glVertex3d( -x, -x, x); 
	glEnd(); 
 
	glBegin(GL_POLYGON); 
//		glNormal3d(0.0,-1.0,0.0); 
		glColor3ub(255,0,0); 
		glVertex3d( -x, -x, x); 
		glColor3ub(0,255,0); 
		glVertex3d( x, -x, x); 
		glColor3ub(0,0,255); 
		glVertex3d( x, -x, -x); 
		glVertex3d( -x, -x, -x); 
	glEnd(); 
 
 
	glBegin(GL_POLYGON); 
//		glNormal3d(0.0,1.0,0.0); 
		glColor3ub(255,0,0); 
		glVertex3d( -x, x, x); 
		glColor3ub(0,255,0); 
		glVertex3d( x, x, x); 
		glColor3ub(0,0,255); 
		glVertex3d( x, x, -x); 
		glColor3ub(255,255,0); 
		glVertex3d( -x, x, -x); 
	glEnd(); 
	::glEndList(); 
 
	::glNewList(3, GL_COMPILE); 
     float mat_diffuse[]={(float)0.1,(float)0.5,(float)0.8,(float)1.0};  
     float mat_emission[]={(float)0.3,(float)0.2,(float)0.2,(float)0.0};  
 
     glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);   
     glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);  
	 auxSolidTeapot(1.0);// 用 辅 助 库 函 数 画 茶 壶。 
	::glEndList(); 
 
	::glNewList(7,GL_COMPILE); 
	{ 
    GLUquadricObj *quadObj= gluNewQuadric(); 
    gluQuadricTexture(quadObj,GL_TRUE);         
    glBindTexture(GL_TEXTURE_2D,texture[0]); 
	glColor3d(1.0,1.0,1.0); 
    gluSphere(quadObj,1,20,20); 
    gluDeleteQuadric(quadObj); 
	} 
	::glEndList(); 
 
	::glNewList(8,GL_COMPILE); 
	{ 
    GLUquadricObj *quadObj = gluNewQuadric(); 
    gluQuadricTexture(quadObj,GL_TRUE);         
    glBindTexture(GL_TEXTURE_2D,texture[1]); 
	glColor3d(1.0,1.0,1.0); 
    gluSphere(quadObj,1,20,20); 
    gluDeleteQuadric(quadObj); 
	} 
	::glEndList(); 
 
	::glNewList(9,GL_COMPILE); 
	glBegin(GL_POINTS);           
    for(int i=0; i<360;i++)glVertex3f(cos(2*PI/360*i),sin(2*PI/360*i),0); 
    glEnd(); 
	::glEndList(); 
} 
 
 
void CGLEnabledView::BuildTexture() 
{ 
	glGenTextures(2,texture);   
 
	glBindTexture(GL_TEXTURE_2D,texture[0]);    
	AUX_RGBImageRec   *earthpImage   =   NULL;    
	earthpImage=auxDIBImageLoad("earth.bmp");    
	if (earthpImage==NULL) return;    
 	gluBuild2DMipmaps(GL_TEXTURE_2D,  GL_RGBA ,earthpImage->sizeX,earthpImage->sizeY,    
	GL_RGB, GL_UNSIGNED_BYTE,earthpImage->data);    
	free(earthpImage->data);    
	free(earthpImage); 
 
	glBindTexture(GL_TEXTURE_2D,texture[1]);    
	AUX_RGBImageRec   *moonpImage   =   NULL;    
	moonpImage=auxDIBImageLoad("moon.bmp");    
	if (moonpImage==NULL) return;    
 	gluBuild2DMipmaps(GL_TEXTURE_2D,  GL_RGBA ,moonpImage->sizeX,moonpImage->sizeY,    
	GL_RGB, GL_UNSIGNED_BYTE,moonpImage->data);    
	free(moonpImage->data);    
	free(moonpImage); 
}