教程1-创建一个窗口

Background

Thanks to Mariano Suligoy you can read this tutorial in Spanish.

The OpenGL spec does not specify any API in order to create and manipulate windows. Modern windowing systems that support OpenGL include a sub-system that provides the binding between an OpenGL context and the windowing system. In the X Window system that interface is called GLX. Microsoft provides WGL (pronounced: Wiggle) for Windows and MacOS has CGL. Working directly with these interfaces in order to create a window in which to display graphics is usually grunt work which is why we use a high level library that abstracts away the fine details. The library we use here is called the 'OpenGL utility library', or GLUT. It provides a simplified API for window management as well as event handling, IO control and a few other services. In addition, GLUT is cross platform which makes portability easier. Alternatives to GLUT include SDL and GLFW.


背景

感谢Mariano Suligoy 让得我们可以读到这个教程。

该篇OpenGL教程没有详细说明任何关于创建操作windows窗口的API。现代支持OpenGL的窗口系统包含一个提供绑定openGL上下文以及windows系统的子系统。在X-window系统下的接口叫做GLX。微软提供了WGL,Mac系统下有CGL。使用这些接口直接工作去创建一个显示图形的窗口是非常简单的工作,这是因为我们使用了高级别的库抽象了经常使用的细节。我们这里使用的库叫做OpenGL工具库(OpenGL utility library或者GLUT)。它为窗口管理、事件处理以及输入输出控制以及其他的一些服务等创建提供了一套简单的API接口。除此之外,GLUT是跨平台的,这样使得移植起来容易很多。GLUT的替代品有SDL或者GLFW。


Source walkthru

源代码攻略

glutInit(&argc, argv);

This call initializes GLUT. The parameters can be provided directly from the command line and include useful options such as '-sync' and '-gldebug' which disable the asynchronous nature of X and automatically checks for GL errors and displays them (respectively).

这个函数调用初始化GLUT。参数可以直接从命令行提供,可以包含有用的选项,像'-sync','-gldebug' 这样,关闭异步默认选项以及检查GL错误并且显示他们。

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

Here we configure some GLUT options. GLUT_DOUBLE enables double buffering (drawing to a background buffer while another buffer is displayed) and the color buffer where most rendering ends up (i.e. the screen). We will usually want these two as well as other options which we will see later.

这里我们配置一些GLUT选项。GLUT_DOUBLE,打开双缓冲(当有缓冲区数据正在显示时,绘图到背景缓冲中),颜色缓冲区大部分渲染结束(例如屏幕)。我们通常会希望这两个以及其他选项,我们之后可以看到。

glutInitWindowSize(1024, 768); 
glutInitWindowPosition(100, 100); 
glutCreateWindow("Tutorial 01");

These calls specify the window parameters and create it. You also have the option to specify the window title.

这些调用指定了窗口参数,然后根据这些参数创建窗口。你也可以去指定窗口标题。

glutDisplayFunc(RenderSceneCB);

Since we are working in a windowing system most of the interaction with the running program occurs via event callback functions. GLUT takes care of interacting with the underlying windowing system and provides us with a few callback options. Here we use just one - a "main" callback to do all the rendering of one frame. This function is continuously called by GLUT internal loop.

因为我们工作在的窗口系统是大多数的运行下的交互发生都会调用回调函数,GLUT关注了窗口系统的基本交互以及提供给我们一些回调选项。这里我们只用了一个“main”回调函数去做了所有的帧渲染。这个函数不停的调用GLUT内部循环。


glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

This is our first encounter with the concept of state in OpenGL. The idea behind state is that rendering is such a complex task that it cannot be treated as a function call that receives a few parameters (and correctly designed functions never receive a lot of parameters). You need to specify shaders, buffers and various flags that affect how rendering will take place. In addition, you would often want to keep the same piece of configuration across several rendering operations (e.g. if you never disable the depth test then there is no point in specifying it for every render call). That is why most of the configuration of rendering operations is done by setting flags and values in the OpenGL state machine and the rendering calls themselves are usually limited to the few parameters that revolve around the number of vertices to draw and their starting offset. After calling a state changing function that particular configuration remains intact until the next call to the same function with a different value. The call above sets the color that will be used when clearing the framebuffer (described later). The color has four channels (RGBA) and it is specified as a normalized value between 0.0 and 1.0.

这是我们遇到的OpenGL中第一个包含状态的概念。


glutMainLoop();

This call passes control to GLUT which now begins its own internal loop. In this loop it listens to events from the windowing system and passes them via the callbacks that we configured. In our case GLUT will only call the function we registered as a display callback (RenderSceneCB) to give us a chace to render the frame.

这个函数传递控制到GLUT,让得程序开始自己内部的循环。在这个循环中,它将监听windows系统事件以及我们配置的回调事件等。在我们的这个例子中,GLUT将只调用我们注册的显示回调函数去重绘制一帧。



glClear(GL_COLOR_BUFFER_BIT); 
glutSwapBuffers();

The only thing we do in our render function is to clear the framebuffer (using the color specified above - try changing it). The second call tells GLUT to swap the roles of the backbuffer and the frontbuffer. In the next round through the render callback we will render into the current frames front buffer and the current backbuffer will be displayed.

在渲染函数中我们做了的唯一一件事情就是清空帧缓冲(使用上面指定的颜色,尝试清空帧缓冲区颜色)。第二个函数告诉GLUT去将前后缓冲区的绘图数据互换使用。在下一个渲染回调过程中,我们将渲染数据到当前的前缓冲区,当前的后缓冲区中的数据将被显示出来。



























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值