Tutorial 1 - Create a window(创建窗口)

Background

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

Source walkthru

glutInit(&argc, argv);

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

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

Here we configure some GLUT options. GLUT_DOUBLE enables doublebuffering (drawing to a background buffer while another buffer isdisplayed) and the color buffer where most rendering ends up (i.e. thescreen). We will usually want these two as well as other options whichwe will see later.

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

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

glutDisplayFunc(RenderSceneCB);

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

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

This is our first encounter with the concept of state in OpenGL. Theidea behind state is that rendering is such a complex task that itcannot 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 effect howrendering will take place. In addition, you would often want to keepthe same piece of configuration across several rendering operations(e.g. if you never disable the depth test then there is no point inspecifying it for every render call). That is why most of theconfiguration of rendering operations is done by setting flags andvalues in the OpenGL state machine and the rendering calls themselvesare usually limited to the few parameters that revolve around thenumber of vertices to draw and their starting offset. After calling astate changing function that particular configuration remains intactuntil the next call to the same function with a different value. Thecall above sets the color that will be used when clearing theframebuffer (described later). The color has four channels (RGBA) andit is specified as a normalized value between 0.0 and 1.0.

glutMainLoop();

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

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 calltells GLUT to swap the roles of the backbuffer and the frontbuffer. Inthe next round through the render callback we will render into thecurrent frames front buffer and the current backbuffer will bedisplayed.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值