您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > OPENGL学习课件1
基于VisualC++OpenGL编程一、概述OpenGL是一个功能强大的三维图形库GL:GRAPHICLIBRARYVC++中提供三个OpenGL的函数库(opengl32.lib,glu32.lib,glaux.lib),分别表示OpenGL库、实用库和辅助库它们相应的头文件是gl.h、glu.h及glaux.h。1、OpenGL图形操作步骤:1)根据基本图形单元建立景物模型。2)把景物模型放在合适的位置,并且设置视点(viewpoint)。3)确定光照条件、纹理粘贴方式等。4)光栅化OpenGL库函数分为六类:(1)核心库前缀为gl用于常规、核心的图形处理(2)实用库前缀为glu用于较为复杂的操作(坐标变换、茶壶)(3)辅助库前缀为aux用于绘制简单三维物体及窗口管理(4)工具库前缀为glut用于基于窗口绘制(对话框、定时器)(5)专用库前缀为wgl用于连接OpenGL和Windows(6)Win32API无前缀用于处理像素格式和双桢缓存2、OpenGL开发库基本组成3、OpenGL绘图基本知识OPENGL函数、变量命名准则变量后缀类型C类型OpenGL类型s16-bitintshortGLshorti32-bitintlongGLint,GLsizeif32-bitfloatfloatGLfloatd64-bitfloatdoubleGLdouble例如:GLdoublex,GLfloaty,GLintz;1.0f实际就是1.0,一般写成1.0fOPENGL函数、变量命名准则函数后缀函数参数类型作为函数后缀例如:glVertex2i(2,4)glVertex3f(2.0,4.0,6.0)表明是opengl基本函数(gl-)是绘点的函数(-Vertex-)是两个整型参数(-2i)OPENGL基本库的绘图方法glBegin(类型);glColor3f(...);glVertex(...)glColor3f(...);glVertex(...);...glEnd();由“类型”指定画什么平面:GL_POINTS单个顶点集GL_LINES多组线,2点一条线GL_POLYGON单个简单多边形GL_TRIANGLES绘制三角形GL_LINE_LOOP闭合折线GL_QUADS绘制四边形几何图元类型glBegin()和glEnd()之间可以调用的函数glVertex//设置顶点坐标如:glVertex2f(0.0,1.0);glColor//设置当前颜色如:glColor3f(1.0,0.0,0.0);glNormal//设置法向坐标如:glNormal3f(1.0,0.0,0.0)//法线向量glMaterial//设置材质例如:绘制五边形glBegin(GL_POLYGON);//多边形glVertex2f(0.0,1.0);//顶点glColor3f(1.0,0.0,0.0);//点的颜色glVertex2f(2.0,3.0);glVertex2f(6.0,8.0);glVertex2f(3.0,10.0);glVertex2f(1.0,3.0);glEnd();一几何变换•1.voidglTranslated(GLdoublex,GLdoubley,GLdoublez);voidglTranslatef(GLfloatx,GLfloaty,GLfloatz);•目标沿XYZ轴平移xyz平移变换实例glTranslated(GLdoublex,GLdoubley,GLdoublez);glTranslatef(0.0f,0.0f,4.0f);目标沿Z轴平移4voidglRotated(GLdoubleangle,GLdoublex,GLdoubley,GLdoublez);voidglRotatef(GLfloatangle,GLfloatx,GLfloaty,GLfloatz);•目标分别以XYZ轴为轴逆时针旋转xyz旋转变换实例voidglRotated(GLdoubleangle,GLdoublex,GLdoubley,GLdoublez);glRotated(10,0.0f,1.0f,0.0f);目标分别以Y轴为轴逆时针旋转10度voidglScaled(GLdoublex,GLdoubley,GLdoublez);voidglScalef(GLfloatx,GLfloaty,GLfloatz);目标在XYZ方向缩放,缩放因子为xyz缩放变换实例voidglScaled(GLdoublex,GLdoubley,GLdoublez);glScalef(0.5,2.0,4.0);目标在XYZ方向缩放,缩放因子为0.5244、VC中OpenGL编程总体步骤:1、添加、引用OpenGL相关库、头文件2、用AppWizard创建应用程序框架3、用ClassWizard添加成员函数:WM_CREATE、WM_SIZE、WM_DESTROY,改写PreCreateWindow函数;4、设置像素格式,创建绘制描述表。5、绘制场景5、VC中OpenGL编程具体步骤:1)添加opengl32.libglu32.libglaux.lib复制相关头文件、库到VC安装路径下具体操作:选择“工程”下的“设置”项,单击Link标签,选择General目录,在Object/LibraryModules的编辑框2)#includeGL/gl.h#includeGL/glu.h#includeGL/glaux.h加入到视图类源文件中3)PreCreateWindow(CREATESTRUCT&cs)中代码BOOLCGLSample1View::PreCreateWindow(CREATESTRUCT&cs){cs.style=cs.style|WS_CLIPCHILDREN|WS_CLIPSIBLINGS;returnCView::PreCreateWindow(cs);}4)用类向导添加WM_CREATEWM_SIZEWM_DESTROY消息函数;HGLRCm_hRC;//绘制描述表CDC*m_pDC;//设备描述表voidInitOpenGL(CDC*pDC);//初始化voidSetupPixelFormat(void);//设置像素voidRenderScene();//绘制场景m_pDC=pDC;SetupPixelFormat();//生成绘制描述表m_hRC=::wglCreateContext(m_pDC-GetSafeHdc());//置当前绘制描述表wglMakeCurrent(m_pDC-GetSafeHdc(),m_hRC);glEnable(GL_DEPTH_TEST);//深度测试5)在视图类头文件中添加:voidSetupPixelFormat(void);//设置像素PIXELFORMATDESCRIPTORpfd={sizeof(PIXELFORMATDESCRIPTOR),//pfd结构的大小1,//版本号PFD_DRAW_TO_WINDOW|//支持在窗口中绘图PFD_SUPPORT_OPENGL|//支持OpenGLPFD_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//忽略层,可见性和损毁掩模};intpixelformat;//选择像素格式pixelformat=ChoosePixelFormat(m_pDC-GetSafeHdc(),&pfd);//设置像素格式SetPixelFormat(m_pDC-GetSafeHdc(),pixelformat,&pfd);HGLRCm_hRC;//绘制描述表CDC*m_pDC;//设备描述表voidInitOpenGL(CDC*pDC);//初始化voidSetupPixelFormat(void);//设置像素voidRenderScene();//绘制场景glClearColor(1.0,1.0,1.0,0.0);//清除背景色glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//清除屏幕及深度缓存glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f);//左移1.5单位,并移入屏幕6.0//绘制三角形glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);//设置当前色为红色glVertex3f(0.0f,1.0f,0.0f);//上顶点HGLRCm_hRC;//绘制描述表CDC*m_pDC;//设备描述表voidInitOpenGL(CDC*pDC);//初始化voidSetupPixelFormat(void);//设置像素voidRenderScene();//绘制场景glColor3f(0.0f,1.0f,0.0f);//设置当前色为绿色glVertex3f(-1.0f,-1.0f,0.0f);//左下HGLRCm_hRC;//绘制描述表CDC*m_pDC;//设备描述表voidInitOpenGL(CDC*pDC);//初始化voidSetupPixelFormat(void);//设置像素voidRenderScene();//绘制场景glColor3f(0.0f,0.0f,1.0f);//设置当前色为蓝色glVertex3f(1.0f,-1.0f,0.0f);//右下glEnd();//三角形绘制结束glTranslatef(1.0f,0.0f,0.0f);//右移1单位HGLRCm_hRC;//绘制描述表CDC*m_pDC;//设备描述表voidInitOpenGL(CDC*pDC);//初始化voidSetupPixelFormat(void);//设置像素voidRenderScene();//绘制场景glTranslatef(3.0f,0.0f,0.0f);//右移3单位glColor3f(0.5f,0.5f,1.0f);//一次性将当前色设置为蓝色glBegin(GL_QUADS);//绘制正方形glVertex3f(-1.0f,1.0f,0.0f);//左上glVertex3f(1.0f,1.0f,0.0f);//右上glVertex3f(1.0f,-1.0f,0.0f);//左下glVertex3f(-1.0f,-1.0f,0.0f);//右下glEnd();//正方形绘制结束SwapBuffers(m_pDC-m_hDC);//交互缓冲区6)响应WM_CREATE消息VoidC**View::OnCreate(){//…..m_pDC=newCClientDC(this);InitOpenGL(m_pDC);//…}7)响应WM_SIZE消息VoidC**View::OnSize(UINTnType,intcx,intcy){CView::OnSize(nType,cx,cy);glViewport(0,0,cx,cy);//定义视口}glMatrixMode(GL_PROJECTION);//选择投影矩阵glLoadIdentity();//重置投影矩阵//设置视口的大小gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);//选择模型观察矩阵glLoadIdentity();//重置模型观察矩阵8)在OnDraw函数中绘制OpenGL场景{RenderScene();//绘制场景}9)响应WM_DESTROY消息{wglDeleteConte
本文标题:OPENGL学习课件1
链接地址:https://www.777doc.com/doc-3956098 .html