#include
//#include
//#include
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glaux.lib")
//opengl32.lib glu32.lib glaux.lib odbc32.lib odbccp32.lib
//实现步骤
void COpenGLExpView::OnDestroy()
{
HGLRC hrc;
//撤消时间步
KillTimer(1);
//获取着色描述表句柄
hrc = ::wglGetCurrentContext();
//释放着色描述表句柄
::wglMakeCurrent(NULL, NULL);
//删除着色描述表句柄
if (hrc)
::wglDeleteContext(hrc);
//删除CClientDC指针
if (m_pDC)
delete m_pDC;
CView::OnDestroy();
}
void COpenGLExpView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此添加消息处理程序代码
//add down
int w=cx;
int h=cy;
//避免除数为0
if(h==0) h=1;
//设置视口与窗口匹配
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
//重新设置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立正交变换下的剪切体
if (cx <= cy)
glOrtho (-3.5, 3.5, -3.5*(GLfloat)cy/(GLfloat)cx,
3.5*(GLfloat)cy/(GLfloat)cx ,-3.5, 3.5);
else
glOrtho (-3.5*(GLfloat)cx/(GLfloat)cy,
3.5*(GLfloat)cx/(GLfloat)cy, -3.5, 3.5, -3.5, 3.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void COpenGLExpView::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);
//创建着色描述表
hrc = wglCreateContext(m_pDC->GetSafeHdc());
//设置着色描述表
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
//使景物模型能接受光源照射
glEnable(GL_LIGHTING);
}
BOOL COpenGLExpView::bSetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), //pdf结构的长度
1, //版本号
PFD_DRAW_TO_WINDOW | //输出到屏幕
PFD_SUPPORT_OPENGL | //支持openGL函数
PFD_DOUBLEBUFFER, //支持双缓存计数
PFD_TYPE_RGBA, //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 //
};
int pixelformat;
//选择像素格式
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
//设置像素格式
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
void COpenGLExpView::DrawScene(void)
{
static BOOL bBusy = FALSE;
static GLfloat wAngleY = 10.0f;
static GLfloat wAngleX = 1.0f;
static GLfloat wAngleZ = 5.0f;
if(bBusy)
return;
bBusy = TRUE;
//将初始的背景色设置为黑色
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//清除颜色缓存和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
//实现正方体的旋转变换
glRotatef(wAngleX, 1.0f, 0.0f, 0.0f);
glRotatef(wAngleY, 0.0f, 1.0f, 0.0f);
glRotatef(wAngleZ, 0.0f, 0.0f, 1.0f);
wAngleX += 1.0f;
wAngleY += 10.0f;
wAngleZ += 5.0f;
//绘制正方体曲线
auxWireCube(2.0);
glPopMatrix ();
//刷新显示
glFinish();
SwapBuffers(wglGetCurrentDC());
bBusy = FALSE;
}
void COpenGLExpView::OnUpdateStop(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
pCmdUI->SetCheck(m_nPlay==0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void COpenGLView::OnDraw(CDC* pDC)
{
COpenGLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: 在此处为本机数据添加绘制代码
//清除背景
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 变换
EyePointChange();
//添加光源和材料属性
AddLight();
//绘制球体
auxSolidSphere(100);
// 刷新显示
glFinish();
//如果使用的是双缓冲区则交换缓冲区
SwapBuffers(pDC->GetSafeHdc());
}
void COpenGLView::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);
hrc = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
GLfloat mat_colormap[] = { 16.0, 47.0, 3682.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 10.0, 4.0, 1.0, -3.0 };
glClearColor (0.0, 1.0, 1.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_colormap);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
BOOL COpenGLView::bSetupPixelFormat()
{
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
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
void COpenGLView::DrawScene(void)
{
//清除颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auxSolidSphere (1.0);
glFinish();
SwapBuffers(wglGetCurrentDC());
}
int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
start();
return 0;
}
void COpenGLView::OnDestroy()
{
CView::OnDestroy();
// TODO: 在此添加消息处理程序代码
end();
}
void COpenGLView::OnLight1()
{
// TODO: 在此添加命令处理程序代码
if(m_bLight1){
m_bLight1=FALSE;
}
else{
m_bLight1=TRUE;
}
Invalidate();
}
void COpenGLView::OnUpdateLight1(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
pCmdUI->SetCheck(m_bLight1==TRUE);
}
void COpenGLView::OnLight2()
{
// TODO: 在此添加命令处理程序代码
if(m_bLight2){
m_bLight2=FALSE;
}
else{
m_bLight2=TRUE;
}
Invalidate();
}
void COpenGLView::OnUpdateLight2(CCmdUI *pCmdUI)
{
// TODO: 在此添加命令更新用户界面处理程序代码
pCmdUI->SetCheck(m_bLight2==TRUE);
}
void COpenGLView::start()
{
//定义PIXELFORMATDESCRIPTOR结构体
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // pdf结构的长度
1, // 版本号
PFD_DRAW_TO_WINDOW | // 输出到屏幕
PFD_SUPPORT_OPENGL| //支持openGL函数
PFD_DOUBLEBUFFER, // 支持双缓存计数
PFD_TYPE_RGBA, // 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 clientDC(this);
int pixelformat = ChoosePixelFormat(clientDC.m_hDC,&pfd);
BOOL success=SetPixelFormat(clientDC.m_hDC,pixelformat,&pfd);
//创建和设置着色描述表
m_hRC=wglCreateContext(clientDC.m_hDC);
wglMakeCurrent(this->GetDC()->GetSafeHdc(), m_hRC);
//清除深度缓存的值
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
//指明模型取景矩阵
glMatrixMode(GL_MODELVIEW);
//初始化取景矩阵
glLoadIdentity();
}
void COpenGLView::end()
{
// TODO: Add your command handler code here
HGLRC hrc;
//获取着色描述表句柄
hrc=::wglGetCurrentContext();
//释放着色描述表句柄
::wglMakeCurrent(NULL,NULL);
if(hrc)
//删除着色描述表句柄
::wglDeleteContext(m_hRC);
}
void COpenGLView::EyePointChange()
{
//设置模型取景变换
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//设置观察点
gluLookAt(30,30,60,0,0,0,-1,-1,1.414);
//设置投影变换
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//设置可视空间
glOrtho(-150,150,-150,150,-180,180);
//设置可视区域
glViewport(0,0,400,400);
}
void COpenGLView::AddLight()
{
//光照
GLfloat mat_diffuse[4]={1,0.5,0.5,1.0};
GLfloat mat_specular[4]={1.0,1.0,1.0,1.0};
GLfloat mat_shininess[1]={100.0};
//光源1
if(m_bLight1==TRUE){
GLfloat light_position0[4]={0,500,500,0};
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
glEnable(GL_LIGHT0);
}
else{
glDisable(GL_LIGHT0);
}
//光源2
if(m_bLight2==TRUE){
GLfloat light_position1[4]={1000,-1000,1000,0};
GLfloat mat_diffuse1[4]={0.5,0.5,1.0,1.0};
glLightfv(GL_LIGHT1,GL_DIFFUSE,mat_diffuse1);
glLightfv(GL_LIGHT1,GL_SPECULAR,mat_specular);
glLightfv(GL_LIGHT1,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
glEnable(GL_LIGHT1);
}
else{
glDisable(GL_LIGHT1);
}
glEnable(GL_LIGHTING);
//设置光照效果
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
//设置深度缓存的参照值
glDepthFunc(GL_LESS);
//设置光源的其他性质
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}