单文档OpenGL的初始化

1、配置OpenGL环境

配置OpenGL环境,添加.lib类库

2、添加头文件

在stdafx.h中添加头文件(放到其他文件中也可以)

#include <gl\gl.h>

#include <gl\glu.h>

3、添加变量

public:

         CClientDC *m_pDC;   //Device Context 设备上下文

         HGLRC     m_hRC;    //RenderingContext 着色上下文

4、设定OpenGL风格

   在PreCreateWindow中添加cs.style |= WS_CLIPSIBLINGS |WS_CLIPCHILDREN;

5、初始化

int OnCreate(LPCREATESTRUCT lpCreateStruct)

{

         if (CView::OnCreate(lpCreateStruct)== -1)

                   return -1;

         Init();  //初始化

         return 0;

}

BOOL bSetupPixelFormat()

{

         //填充PIXELFORMATDESCRIPTOR结构

         staticPIXELFORMATDESCRIPTOR pfd =

         {

                   sizeof(PIXELFORMATDESCRIPTOR),        // 像素格式描述符的大小

                   1,                                                                       // 版本号

                   PFD_DRAW_TO_WINDOW|                     // 格式必须支持窗口

                   PFD_SUPPORT_OPENGL|                          // 格式必须支持OpenGL

                   PFD_DOUBLEBUFFER,                                 // 必须支持双缓冲

                   PFD_TYPE_RGBA,                                // 申请RGBA 格式

                   24,                                                                     // 选定色彩深度

                   0, 0, 0, 0, 0, 0,                             // 忽略的色彩位

                   0,                                                                       // Alpha缓存

                   0,                                                                       // 忽略Shift Bit

                   0,                                                                       // 无聚集缓存

                   0, 0, 0, 0,                                      // 忽略聚集位

                   32,                                                                     // 32Z-缓存(深度缓存

                   0,                                                                       // 无模板缓存

                   0,                                                                       // 无辅助缓存

                   PFD_MAIN_PLANE,                             // 主绘图层

                   0,                                                                       // 保留

                   0, 0, 0                                                      // 忽略层遮罩

         };

 

         //设置像素格式

         intpixelformat;

         //Windows 能否找到相应的象素格式了

         if( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )

//GetSafeHdc返回输出设备上下文的句柄

//ChoosePixelFormat根据pixel format descriptor 返回一个和当前device context最佳匹配的 pixel format 

         {

                   MessageBox("选择像素格式失败!");

                   return FALSE;

         }

         //能否设置象素格式?

         if(SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)

         {

                   MessageBox("设置像素格式出错!");

                   return FALSE;

         }

         returnTRUE;

 

}

 

/*初始化*/

void Init()

{

         PIXELFORMATDESCRIPTOR pfd;

        

         m_pDC = new CClientDC(this);  //创建DC

         ASSERT(m_pDC != NULL);

 

         if(!bSetupPixelFormat())    //设置像素格式

                   return;

 

         m_hRC =wglCreateContext(m_pDC->GetSafeHdc());//DC去创建一个RC

         wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC); //RC与当前DC相关联

}

MSDN是这么说的:

1)

The wglCreateContext functioncreates a new OpenGL rendering context, which is suitable for drawing on thedevice referenced by hdc. The renderingcontext has the same pixel format as the device context.

wglCreateContext创建一个和当前的上下文适合的OpenGL rendering context,

2)

A renderingcontext is not the same as a device context. Set the pixel format of the devicecontext before creating a rendering context. For more information on settingthe device context's pixel format, see the SetPixelFormat function.

rendering context创建之前必须确定device contex的像素格式(pixel format

To use OpenGL,you create a rendering context, select it as a thread's current renderingcontext, and then call OpenGL functions. When you are finished with therendering context, you dispose of it by calling the wglDeleteContext function.

即为了能够使用OpenGL,你必须创建渲染上下文(renderingcontext)并把它选为当前的rendering context

 

6、OnSize()窗口改变

void CLidar428LabView::OnSize(UINT nType, int cx, int cy)

//cx即客户区的宽度;cy即客户区的高度

{

         CView::OnSize(nType, cx, cy);

         //TODO: Add your message handler code here

         if(cy > 0)

         {

           glMatrixMode(GL_PROJECTION); //选用投影矩阵

                   glLoadIdentity(); //重置当前指定的矩阵为单位矩阵,

                   glViewport(0,0, cx, cy);   //根据窗口的实时变化重绘窗口 ,即投影窗口的大小

         }       

}

7、响应WM_ERASEBKGND消息

BOOL OnEraseBkgnd(CDC* pDC)

{

         // TODO: Add your message handler code here and/or calldefault

//returnCView::OnEraseBkgnd(pDC);

         return TRUE;

}

当需要重新设置窗口背景时,产生WM_ERASEBKGND消息,处理该消息的默认操作是用当前背景色填充整个窗口。处理方法:注释掉原有的return语句,改为return true;使该函数不执行操作,仅返回true。

8、画图

在OnDraw()中调用绘图函数。

void CLidar428LabView::OnDraw(CDC* pDC)

{

         CLidar428LabDoc* pDoc= GetDocument();

         ASSERT_VALID(pDoc);

         if (!pDoc)

                   return;

         // TODO: 在此处为本机数据添加绘制代码

         DrawScene(); //绘图

}

 

void DrawScene()

{

        

         glClearColor(0.6f,0.6f,1.0f,1.0f);//设置清屏颜色

         glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);        //清除颜色缓冲区和深度缓冲区

         glPushMatrix();//矩阵堆栈函数,和glPopMatrix()相对应

 

         glPointSize(10.0);//设置点的大小

         glBegin(GL_LINES);//说明几何图元为,和glEnd()相对应

         glColor3f(1.0,0.0,0.0);//绘制红色的点

 

         glVertex3f(-0.7,-0.7,0.0);

         glVertex3f(0.7,-0.7,0.0);

 

         glVertex3f(0.7,-0.7,0.0);

         glVertex3f(0.0,0.0,0.0);

 

         glVertex3f(0.0,0.0,0.0);

         glVertex3f(0.7,0.7,0.0);

 

         glVertex3f(0.7,0.7,0.0);

         glVertex3f(-0.7,0.7,0.0);

 

         glVertex3f(-0.7,0.7,0.0);

         glVertex3f(-0.7,-0.7,0.0);

        

         glEnd();  

 

         glPopMatrix();

         glFinish();  

         SwapBuffers(wglGetCurrentDC());//双缓冲

}

红色部分是画图内容,可以随意更改。

9、响应WM_DESTROY消息

    取消DCRC的关联,并且删除DCRC

void CLidar428LabView::OnDestroy()

{

         CView::OnDestroy();

 

         // TODO: Add your message handler code here

         //m_hRC = wglGetCurrentContext();

         wglMakeCurrent(NULL,NULL);  //取消DCRC的关联

         if(m_hRC)      

                   wglDeleteContext(m_hRC);   //删除RC

         if(m_pDC)

                   delete m_pDC;      //删除DC

}

图像 glVertex3f()参数只能小于1,大于1会被裁剪掉。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值