OpenGL Tutorial: (2) Creating an OpenGL Window
来源:互联网 发布:大智慧股票行情软件 编辑:程序博客网 时间:2024/05/16 06:01
接上文(《OpenGL Tutorial: (1) Setting up OpenGL with Visual Studio》)。
创建默认的OpenGL窗口
先给出一个能运行的例子,它创建一个默认的OpenGL窗口并给它填色。如果press 键那么程序退出。
// Sample.c : Defines the entry point for the console application.//#include "stdafx.h"#include <GL\glew.h>#include <GL\freeglut.h>#include <vld.h>static void keyboard (unsigned char key, int x, int y);static void renderScene (void);int main (int argc, char* argv[]){ glutInit (&argc, argv); glutCreateWindow ("GLUT Test"); glutKeyboardFunc (&keyboard); glutDisplayFunc (&renderScene); glutMainLoop (); return 0;}static void keyboard (unsigned char key, int x, int y){ switch (key) { case '\x1B': exit (EXIT_SUCCESS); break; }}static void renderScene (){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (0.5f, 0.0f, 0.5f); glBegin (GL_POLYGON); glVertex2f (-0.5f, -0.5f); glVertex2f (0.5f, -0.5f); glVertex2f (0.5f, 0.5f); glVertex2f (-0.5f, 0.5f); glEnd (); glFlush ();}
上面这段代码中,创建OpenGL窗口就用到了这么一句:
glutCreateWindow ("GLUT Test");
它当然可以创建OpenGL窗口,但是没有任何的额外的参数,完全由着OpenGL的默认行为。
Screenshots:
控制窗口的位置和大小
把main函数改成:
int main (int argc, char* argv[]){ glutInit (&argc, argv); glutInitWindowPosition (200, 100); glutInitWindowSize (600, 400); glutCreateWindow ("OpenGL Window"); glutKeyboardFunc (&keyboard); glutDisplayFunc (&display); glutMainLoop (); return 0;}
其中初始化和创建OpenGL窗口的语句是:
glutInitWindowPosition (200, 100); glutInitWindowSize (600, 400); glutCreateWindow ("OpenGL Window");
运行。这时候你会发现创建出来的OpenGL窗口已经不是一个随意的大小和位置,而是你所期望的结果:
Screenshot:
初始化显示模式 glutInitDisplayMode
把初始化和创建OpenGL窗口的语句改成这样的:
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition (200, 100); glutInitWindowSize (600, 400); glutCreateWindow ("OpenGL Window");
要关注一下这一句:
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- GLUT_DEPTH: 告诉OpenGL,我们需要一个z轴。
- GLUT_DOUBLE: 告诉OpenGL,我们启用双缓冲区。
- GLUT_RGBA: 告诉OpenGL,我们使用Red, Green, Blue, and Alpha的方式来分配32位的Framebuffer。
看一下运行结果:
是怎么一回事呢?恰恰是因为我们设置了GLUT_DOUBLE的结果。
改一下renderScene函数:
static void renderScene (){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (0.5f, 0.0f, 0.5f); glBegin (GL_POLYGON); glVertex2f (-0.5f, -0.5f); glVertex2f (0.5f, -0.5f); glVertex2f (0.5f, 0.5f); glVertex2f (-0.5f, 0.5f); glEnd (); glFlush (); glutSwapBuffers ();}
注意我们加了这一句:
glutSwapBuffers ();
这一句的作用是交换缓冲区,就是在设置GLUT_DOUBLE时创建的那两个缓冲区。刚刚我们执行的结果是空白,实际上图像画在了“后台”的那个缓冲区,现在我们把它置换到前台来。
执行结果:
你看,这个熟悉的紫色矩形框又能看到了。
为什么要设置双缓冲区呢?因为,当我们画复杂的图形的时候,绘制的过程可能导致屏幕闪烁,如果我们先让它在“后台”的缓冲区里画着,画好了以后再置换到“前台”的缓冲区里来,这样就消除了闪烁了。
今天先写到这里,后面我们再来详细解释。
参考文章
- 《CREATING AN OPENGL WINDOW》
附:系列文章索引
- OpenGL Tutorial: (1) Setting up OpenGL with Visual Studio
- OpenGL Tutorial: (2) Creating an OpenGL Window
- OpenGL Tutorial: (2) Creating an OpenGL Window
- opengl tutorial
- OpenGL Tutorial
- Tutorial 2: Creating a Framework and Window
- 【译】TetroGL: An OpenGL Game Tutorial in C++ for Win32 Platforms - Part 2 (下)
- 【译】TetroGL: An OpenGL Game Tutorial in C++ for Win32 Platforms - Part 2 (上)
- OpenGL Window
- window opengl
- Creating Anaglyphs using OpenGL
- OpenGL: An OpenGL GUI Library
- OpenGL Tutorial 2 : The first triangle
- NeHe OpenGL Tutorial
- Cube Map OpenGL Tutorial
- OpenGL Water Tutorial
- OpenGL ES Tutorial
- 笔记:OpenGL Tutorial 02
- An OpenGL GUI Library
- MFC Tutorial Part 1 - Creating a Window
- Java 文件上传 JQ 文件上传
- 创建一个字符设备驱动
- PHP常用函数总结
- Linux VGA驱动移植实验
- 如何编译Apache Hadoop2.2.0源代码
- OpenGL Tutorial: (2) Creating an OpenGL Window
- leecode(1)Two Sum
- 利用python画微分方程(组)的数值曲线
- 马上去行动马上去体验
- 在浏览器中运行applet
- Web开发教程--如何让图片在div中垂直居中
- Thinkphp3.2.2多语言包实现
- leetcode--Insertion Sort List
- Perl Learning 1 Scalar Data