资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,*,*,vc,环境下,OpenGL,编程:,Win32,应用编程,OpenGL,学习资源网站:,百度,nehe,教程,一、程序主流程,程序主进程:,WinMain,创建窗口,CreateGLWindow,进入主窗口消息循环,退出,二、,OpenGL,窗口创建及初始化,程序主进程:,WinMain,创建窗口,CreateGLWindow,进入主窗口消息循环,退出,注册窗口类:,RegisterClass,显示设置:,ChangeDisplaySettings,调整窗口大小:,AdjustWindowRectEx,创建:,CreateWindowEx,选择,像素格式,:ChoosePixelFormat,设置,像素格式,:,SetPixelFormat,创建,OpenGL,渲染上下文:,wglCreateContext,激活,OpenGL,渲染上下文:,wglMakeCurrent,显示窗口:,ShowWindow,调整,OpenGL,视口大小:,ReSizeGLScene,OpenGL,绘制选项初始化:,InitGL,HDC,hDC,=NULL;,/,基本图形设备接口上下文,选择和设置,像素格式,时,使用,HGLRC,hRC,=NULL;,/,永久渲染上下文,创建和激活,OpenGL,渲染上下文时使用,HWND,hWnd,=NULL;/,窗口句柄,HINSTANCE,hInstance,;/,应用实例句柄,三、调整,OpenGL,视口,glViewport(0,0,width,height);/,设置当前视口大小,glMatrixMode(GL_PROJECTION,);/,选择投影矩阵模式,glLoadIdentity,();/,重置投影矩阵,/,计算,OpenGL,观察窗口参数:视角、宽高比、近裁剪面、,/,远裁剪面,gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);,glMatrixMode(GL_MODELVIEW,);/,选择模型矩阵模式,glLoadIdentity,();/,重置模型矩阵,四、,OpenGL,绘制选项初始化,glShadeModel(GL_SMOOTH,);/打开平滑阴影:glClearColor(0.0f,0.0f,0.0f,0.5f);/黑色背景glClearDepth(1.0f);/,深度缓存初始化glEnable(GL_DEPTH_TEST,);/,打开深度测试,glDepthFunc(GL_LEQUAL,);/深度测试类型,,,GL_LEQUAL,:,当前深度值小于或等于深度缓存中的深度值时进行绘制,glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);/,高质量投影计算,五、,OpenGL,图形绘制,glClear(GL_COLOR_BUFFER_BIT,|GL_DEPTH_BUFFER_BIT);/,初始化颜色缓存和深度缓存,glLoadIdentity,();glTranslatef(-1.5f,0.0f,-6.0f);glRotatef(rtri,0.0f,1.0f,0.0f);,glBegin(GL_TRIANGLES,);/,绘制四菱锥,glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);,glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);,glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);,glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);,glColor3f(0.0f,0.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);,glColor3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);,glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);,glColor3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);,glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);,glColor3f(1.0f,0.0f,0.0f);glVertex3f(0.0f,1.0f,0.0f);,glColor3f(0.0f,0.0f,1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);,glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);,glEnd,();,/,绘制立方体,glLoadIdentity,();glTranslatef(1.5f,0.0f,-7.0f);glRotatef(rquad,1.0f,1.0f,1.0f);,glBegin(GL_QUADS,);,glColor3f(0.0f,1.0f,0.0f);,glVertex3f(1.0f,1.0f,-1.0f);glVertex3f(-1.0f,1.0f,-1.0f);,glVertex3f(-1.0f,1.0f,1.0f);glVertex3f(1.0f,1.0f,1.0f);,glColor3f(1.0f,0.5f,0.0f);,glVertex3f(1.0f,-1.0f,1.0f);glVertex3f(-1.0f,-1.0f,1.0f);,glVertex3f(-1.0f,-1.0f,-1.0f);glVertex3f(1.0f,-1.0f,-1.0f);,glColor3f(1.0f,0.0f,0.0f);,glVertex3f(1.0f,1.0f,1.0f);glVertex3f(-1.0f,1.0f,1.0f);,glVertex3f(-1.0f,-1.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);,glColor3f(1.0f,1.0f,0.0f);,glVertex3f(1.0f,-1.0f,-1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);,glVertex3f(-1.0f,1.0f,-1.0f);glVertex3f(1.0f,1.0f,-1.0f);,glColor3f(0.0f,0.0f,1.0f);,glVertex3f(-1.0f,1.0f,1.0f);glVertex3f(-1.0f,1.0f,-1.0f);,glVertex3f(-1.0f,-1.0f,-1.0f);glVertex3f(-1.0f,-1.0f,1.0f);,glColor3f(1.0f,0.0f,1.0f);,glVertex3f(1.0f,1.0f,-1.0f);glVertex3f(1.0f,1.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);glVertex3f(1.0f,-1.0f,-1.0f);,glEnd,();,/,调整旋转角度,rtri,+=0.2f;,rquad,-=0.15f;,return TRUE;,/,绘制结束,六、主窗口消息循环,程序主进程:,WinMain,创建窗口,CreateGLWindow,进入主窗口消息循环,退出,主窗口消息处理,OpenGL,场景绘制,切换缓存:,SwapBuffers,
展开阅读全文