Make sure that you can run the " GL01Hello. We also included the GLUT header, which is guaranteed to include " glu. OpenGL is the software interface to graphics hardware. In other words, OpenGL graphic rendering commands issued by your applications could be directed to the graphic hardware and accelerated. The expected output and the coordinates are as follows. Take note that 4 shapes have pure color, and 2 shapes have color blending from their vertices.

Author:Kale Kazranos
Country:Turks & Caicos Islands
Language:English (Spanish)
Published (Last):11 April 2018
PDF File Size:19.19 Mb
ePub File Size:7.61 Mb
Price:Free* [*Free Regsitration Required]

Make sure that you can run the " GL01Hello. We also included the GLUT header, which is guaranteed to include " glu. OpenGL is the software interface to graphics hardware. In other words, OpenGL graphic rendering commands issued by your applications could be directed to the graphic hardware and accelerated. The expected output and the coordinates are as follows. Take note that 4 shapes have pure color, and 2 shapes have color blending from their vertices.

OpenGL operates as a state machine , and maintain a set of state variables such as the foreground color, background color, and many more. In a state machine, once the value of a state variable is set, the value persists until a new value is given. For example, we set the "clearing" background color to black once in initGL. We use this setting to clear the window in the display repeatedly display is called back whenever there is a window re-paint request - the clearing color is not changed in the entire program.

Another example: If we use glColor function to set the current foreground color to "red", then "red" will be used for all the subsequent vertices, until we use another glColor function to change the foreground color.

The ' v ' for vector denotes that the parameters are kept in an array of 2, 3, or 4 elements, and pass into the function as an array pointer. The OpenGL types are defined via typedef in " gl. The initGL is meant for carrying out one-time OpenGL initialization tasks, such as setting the clearing color. The function display is known as a callback event handler. An event handler provides the response to a particular event such as key-press, mouse-click, window-paint. The function display is meant to be the handler for window-paint event.

The OpenGL graphics system calls back display in response to a window-paint request to re-paint the window e. Callback means that the function is invoked by the system, instead of called by the your program. The Display runs when the window first appears and once per subsequent re-paint request. Observe that we included OpenGL graphics rendering code inside the display function, so as to re-draw the entire window when the window first appears and upon each re-paint request.

GLUT provides high-level utilities to simplify OpenGL programming, especially in interacting with the Operating System such as creating a window, handling key and mouse inputs.

The following GLUT functions were used in the above program:. We register display function as the callback handler for window-paint event. That is, display runs when the window first appears and whenever there is a request to re-paint the window. We call the initGL to perform all the one-time initialization operations. In this example, we set the clearing background color once, and use it repeatably in the display function. We then put the program into the event-handling loop, awaiting for events such as window-paint request to trigger off the respective event handlers such as display.

We use glColor function to set the foreground color , and glClearColor function to set the background or clearing color. In display , we set the vertex color via glColor3f for subsequent vertices. In OpenGL, an object is made up of geometric primitives such as triangle, quad, line segment and point.

A primitive is made up of one or more vertices. OpenGL supports the following primitives:. A geometric primitive is defined by specifying its vertices via glVertex function, enclosed within a pair glBegin and glEnd. The vertices are usually specified in float precision. It is because integer is not suitable for trigonometric operations needed to carry out transformations such as rotation.

Precision of float is sufficient for carrying out intermediate operations, and render the objects finally into pixels on screen with resolution of says x, integral precision. All subsequent vertices will have the color of red. Take note that in OpenGL, color and many properties is applied to vertices rather than primitive shapes. The color of the a primitive shape is interpolated from its vertices. For the third quad as follows , the vertices have different color. The color of the quad surface is interpolated from its vertices, resulting in a shades of white to dark gray, as shown in the output.

The following diagram shows the OpenGL 2D Coordinate System, which corresponds to the everyday 2D Cartesian coordinates with origin located at the bottom-left corner. The default OpenGL 2D clipping-area i. This clipping-area is mapped to the viewport on the screen. Viewport is measured in pixels.

Study the above example to convince yourself that the 2D shapes created are positioned correctly on the screen. Try dragging the corner of the window to make it bigger or smaller. Observe that all the shapes are distorted. We can handle the re-sizing of window via a callback handler reshape , which can be programmed to adjust the OpenGL clipping-area according to the window's aspect ratio. Clipping Area : Clipping area refers to the area that can be seen i. The function gluOrtho2D can be used to set the clipping area of 2D orthographic view.

Objects outside the clipping area will be clipped away and cannot be seen. To set the clipping area, we need to issue a series of commands as follows: we first select the so-called projection matrix for operation, and reset the projection matrix to identity. We then choose the 2D orthographic view with the desired clipping area, via gluOrtho2D.

Viewport : Viewport refers to the display area on the window screen , which is measured in pixels in screen coordinates excluding the title bar. The clipping area is mapped to the viewport. We can use glViewport function to configure the viewport. Suppose the the clipping area's left, right, bottom, top is It is obvious that if the aspect ratios for the clipping area and the viewport are not the same, the shapes will be distorted.

Take note that in the earlier example, the windows' size of x has a square shape, with a aspect ratio consistent with the default 2x2 squarish clipping-area. A reshape function, which is called back when the window first appears and whenever the window is re-sized, can be used to ensure consistent aspect ratio between clipping-area and viewport, as shown in the above example.

The graphics sub-system passes the window's width and height, in pixels, into the reshape. We compute the aspect ratio of the new re-sized window, given its new width and height provided by the graphics sub-system to the callback function reshape. We set the viewport to cover the entire new re-sized window, in pixels. We set the aspect ratio of the clipping area to match the viewport. OpenGL has two matrices, a projection matrix which deals with camera projection such as setting the clipping area and a model-view matrix for transforming the objects from their local spaces to the common world space.

We reset the projection matrix via glLoadIdentity. Finally, we invoke gluOrtho2D to set the clipping area with an aspect ratio matching the viewport. In the above main function, we specify the initial window size to x , which is non-squarish. Try re-sizing the window and observe the changes. Note that the reshape runs at least once when the window first appears.

It is then called back whenever the window is re-shaped. On the other hand, the initGL runs once and only once ; and the display runs in response to window re-paint request e. In the above sample, we positioned each of the shapes by defining their vertices with respective to the same origin called world space. It took me quite a while to figure out the absolute coordinates of these vertices.

Instead, we could position each of the shapes by defining their vertices with respective to their own center called model space or local space.

Translation and rotation are parts of so-called model transform , which transform from the objects from the local space or model space to the common world space. OpenGL is operating as a state machine.

That is, once a state is set, the value of the state persists until it is changed. In other words, once the coordinates are translated or rotated, all the subsequent operations will be based on this coordinates. To perform animation e. The graphic system will call back the idle function when there is no other event to be processed.

In the idle function, you could issue glutPostRedisplay command to post a window re-paint request, which in turn will activate display function. Take note that the above is equivalent to registering display as the idle function. Double buffering uses two display buffers to smoothen animation. The next screen is prepared in a back buffer, while the current screen is held in a front buffer.

Once the preparation is done, you can use glutSwapBuffer command to swap the front and back buffers. Double buffering should be used in animation. For static display, single buffering is sufficient. Many graphics hardware always double buffered, so it is hard to see the differences. The following program rotates all the shapes created in our previous example using idle function with double buffering. In the above example, instead of accumulating all the translations and undoing the rotations, we use glPushMatrix to save the current state, perform transformations, and restore the saved state via glPopMatrix.

In the above example, we can also use glLoadIdentity to reset the matrix before the next transformations. We define a global variable called angle to keep track of the rotational angle of all the shapes. We will later use glRotatef to rotate all the shapes to this angle. At the end of each refresh in display , we update the rotational angle of all the shapes. Instead of glFlush which flushes the framebuffer for display immediately, we enable double buffering and use glutSwapBuffer to swap the front- and back-buffer during the VSync for smoother display.

We define an idle function, which posts a re-paint request and invoke display , if there is no event outstanding.


CSc 433/533: OpenGL and GLUT Tutorial

Mark J. Kilgard implemented it to enable the construction of OpenGL applications that are truly window system independent. Thanks to both, you did a great job. Another 3 or 4 lines and you can a keyboard and mouse with your application. GLUT really makes things simple, hence it is very usefull to learn and to build small applications. Although GLUT is not being maintained anymore it still serves its purpose.


OpenGL Tutorial

Spaceball is a registered trademark of Spatial Systems Inc. The authors have taken care in preparation of this documentation but make no expressed or implied warranty of any kind and assumes no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising from the use of information or programs contained herein. The OpenGL programming world owes a tremendous debt to Mr. Mark J. The GLUT library of functions allows an application programmer to create, control, and manipulate windows independent of what operating system the program is running on.

Related Articles