Hello and welcome to lecture number 31 in the course Computer Graphics. So, this is going to be our final lecture on the topic. So, far what we have discussed?
We discussed pipeline and then currently we are discussing pipeline implementation. In our earlier lectures we learned about the basic graphics hardware including the graphics input and output devices, the GPU or Graphics Processing Unit and the GPU programming basic idea. Today in our, this last topic we are going to learn about programming or how to write graphics programs that is essentially the software aspect of Computer Graphics.
Now, before we learn to program, we will first start with a basic introduction to graphics software. If you may recollect during our introductory lectures, we had a preliminary introduction but today we are going to recap as well as expand those discussions.
As we have mentioned earlier graphic software are broadly of two types. One is the special purpose packages and the other one is general programming packages.
In the special purpose packages, what we have? These are essentially complete software systems with their own GUIs or user interfaces. For example, painting system here it has its own user interface through which an artist can select objects, select colour, place the objects at desired
position on the Canvas or the screen, change the size of the object, change the shape, also orientation and so on.
And all this, the artist can do by interacting with the user interface. So, there the artist need not know anything about the graphics pipeline or how it is implemented. These are examples of complete software systems or packages. Another example is the CAD package that we have learned about in the introductory lectures, CAD or Computer Aided Design packages. These are primarily used in architecture, medicine, business, engineering and such domains.
The other type of software is the general programming package. Now, here we have libraries, libraries of graphics functions that are provided and we can use those libraries with any programming language such as C, C++, or Java and these functions are mean to perform or rather mean to help a programmer perform pipeline tasks. So, in other words they help the program and implement the pipeline.
An example is OpenGL, which stands for Open Source Graphics Library. Also there are VRML Virtual Reality Modeling Language, Java 3D and so on. So, there are many such libraries provided to implement graphics functions.
Now, these functions are also known as computer graphics application programming interface or CG API. Now, they are essentially a software interface between programming language and the hardware. For example, when we want to write it an application program in a language say C, these library functions allow us to construct and display pictures on the output device. So, without these functions will not be able to do so.
But one thing we should keep in mind is that the graphics functions are typically defined independent of the programming language and that is achieved through a concept called language binding. So, language binding is defined for a particular high-level programming language.
Now, through such binding we get the particular syntax to be used for accessing various graphic functions from that language. So, essentially language binding allows us to use these library functions from inside a program written using a particular language.
Now, each language binding is designed to make the best use of the capabilities there for a particular language and they are designed to handle various syntax issues such as data types, parameter passing and error handling. Now, these specifications or language binding are set by the ISO or International Standard Organization, so we need to know about these standards. We will have a brief introduction to different standards used for computer graphics.
So, what are those standards, software standards that are used in computer graphics?
Now, why we need standard let us try to understand again. When we are writing a program with graphic functions, it may be the case that those programs are moved from one hardware platform to another. Now, how the computer will then understand the program if the platform is changed? There we require standard, without some standards which is essentially a commonly agreed syntax, this movement between platforms will not be possible and we need to rewrite the whole
program. So, essentially we need to start from scratch. So, standard helps us avoid in such situation.
First graphic standard came in 1984, long ago which was known as the graphics kernel system or in short GKS. It was adopted by ISO as well as many other national standard bodies.
Then came a second standard which was developed by extending the GKS, it was called PHIGS, which stands for programmer’s hierarchical interactive graphics standard. ‘PHIGS’, again it was then adopted by the standards organizations worldwide.
Now, around the same time when the other standards were being developed Silicon Graphics Inc or SGI started to ship their workstations meant for graphics with a set of routines or library functions together these are called graphics library for GL.
Subsequently these set of functions or GL become very popular and eventually evolved as the OpenGL in the early 1990s, which had become a de facto graphic standard. Now, this standard is now maintained by the OpenGL architecture review board which is a consortium of representatives from many graphics companies and organizations.
Now, let us try to understand what is there in OpenGL, what functions it provide and how to use those functions.
Let us try to understand OpenGL with respect to one example program. So, this program is shown here, this program is meant to display a straight line on the screen. Now, this has been written by utilizing OpenGL library functions called from C, the C language. Now, let us try to understand the syntax of the program.
So, in order to make use of the library functions, the first thing we should do is to include a header file. Now, this header file contains the library functions, so here we have included it with this statement hash include GL slash glut dot h. Now, what this library name means?
The core library of OpenGL actually does not support input and output operations because those functions were designed to be device independent, whereas support for I/O is or must be device dependent. So, we need to do something about it because we have to display the line on the output which is essentially a device dependent operations.
So, to display we require auxiliary libraries on top of the code library, this is provided by the library GLUT or OpenGL utility toolkit library, ‘GLUT’, GLUT library, that is mentioned in this include statement.
Now, GLUT provides a library of functions for interacting with any screen windowing system essentially any display device and it allows us to setup a display window on our screen, in this window we are going to show the image or whatever we want to display and this display window is essentially a rectangular area which contains the image, that we can do with the help of functions provided in the GLUT library.
Now, whichever library functions we use that are part of GLUT they come with the prefix ‘glut’.
So, essentially these functions provide interface to other device specific window systems that we have already mentioned. So, we can write device independent programs using these GLUT functions and the functions themselves are used to link our program to the particular device.
Also we should note here is that the library GLUT is suitable for graphics operations only and for any other operation we may need to include other header files such as stdio.h or stdlib.h as we do in our regular programs.
Now, let us start with the main function which is shown here, this function and let us try to understand the body of the function. As we said GLUT allows us to create and manage a display window or the screen region on which we want to display the line. So, the first thing that is
required is to initialize GLUT with the statement glutInit as shown here, this is the initialization function that is required at the beginning.
After initialization, we can set various options for the display window using the function glutInitDisplayMode as shown in the second statement. So, what are these options?
Now, these options are provided by symbolic GLUT constants as arguments as shown here, GLUT_SINGLE, GLUT_RGB.
Now, here in this particular function we have used this statement having these two arguments GLUT_SINGLE and GLUT_RGB, they indicate that we are specifying a single refresh buffer to be used for the display window and RGB color mode to be used for selecting color values. GLUT_SINGLE is for the first task single refresh buffer and GLUT_RGB indicates that RGB color mode to be used.
Now, here we should look at the syntax, how this glutInitDisplayMode function is used. In the constant name which provides the options, we have used GLUT as a prefix all caps followed by a underline symbol and then the constant name again all caps as shown here or here this is the particular syntax used to provide arguments. Now, to combine multiple options we are using this logical or operation, to indicate that we want both that is the syntax used to provide the options.
Then we are using the two functions glutInitwindowPosition and glutInitwindowSize. Now, these are used to provide some values that are different than the default values for the window size and position that is already there in the library function. So, if we want to change the values then we need to use these two functions glutInitwindowPosition where is specify the value and glutInitwindowSize where we specify again the size value.
Now, this window position, which position is specifies? It specifies top left corner position of the window. Assuming integer screen coordinate system and assuming origin at the top left corner. These are the assumptions when we specify these values.
Then in case of glutInitwindowSize where we are specifying the size, the first argument specifies width that means 800, second argument specifies height that is 600 and both these values are in pixels, so 800 pixels by 600 pixels. So, we have understood these four functions init, displaymode, windowPosition and windowSize.
Next we create the window and set a caption which is optional using the function Createwindow and the caption is provided within parentheses, but this caption is optional.
The next thing that we do is specify that the picture is to be displayed in the window that is the line. Now, we have to create the line and then we can display it in the window, this creation is done by a separate function which is user defined which we are calling createLine function.
Now, this createLine function is passed as an argument to another glut library function that is glutDisplayFunction which is shown here. This indicates that the line is to be displayed on the window. So, with this function we indicate that we are creating a line which is our image here
that is using the create line function and this line is to be displayed on the window created through these statements. But before we do that certain initializations are required.
And these initializations are performed in the init function shown here. Again this init function is used to make our code look very clean, otherwise we could have used it in a different way and will come back to this function later.
So, in order to keep the code clean and to indicate that we want to display a line on the window we add these two lines init and glutDisplayFunction as shown here.
Now, those are all done but the window is still not on the screen, we need to activate it once the window content is decided, that we do with this function glutMainLoop. Here it activates all display windows created along with their graphic contents. So, this function glutMainLoop actually puts the window with its content on the screen.
This function must be the last one in our program, it puts the program into an infinite loop because the display we want constantly. In this loop the program waits for inputs from devices an input device such as mouse, keyboard, even if there is no input the loop ensures that the picture is displayed till the window is closed. So, since we want the picture to remain on the screen unless there is some input or the window is closed we use the loop and this loop must be at the last statement of the code in main after we create the image and put it on the window.
Now, as we have noted so we explained all these functions that are there in main and all this started with glut indicating that there glut library function except the two functions init and create line. Now, in these two functions we used OpenGL library function rather than glut library functions accordingly their syntax are different.
Each OpenGL function prefixed with GL as we can see in this function init as well as in this create line function. So, here each function is starting with this prefix gl, it indicates that this function is a OpenGL function. Each component word within the function name has first letter capitalized like here C is capitalized in all the cases as you can see Matrix M is capitalized and so on. So, that is the syntax of OpenGL library function starts with gl and component word within this function name has first letter capitalized.
Sometimes some functions may require one or more arguments which are assigned symbolic constants. For example, a parameter name, parameter value or a particular mode, these are all part of the OpenGL library function syntax.
Now, all these constants begin with capital GL all in capital. Each component of the name is written in capital letters and separated by underline symbol, as we have seen in the case of glut constants as well like GL underscore RGB, GL underscore AMBIENT underscore AND underscore DIFFUSE, where everything is in capital separated by underline.
Also the OpenGL functions expect some specific data types. For example, 32 bit integer as a parameter value and these functions use built-in data type names for that.
Each of these names begins with GL and followed by data type name. For example, GLbyte, GLdouble, but this data type name is in lowercase.
So, those are the syntax that are used for using OpenGL library functions. Now, let us try to understand these two functions that we have defined using the OpenGL library functions, one is init, one is create line. So, let us start with init. This is essentially mean to initialize and perform one time parameter settings. In our function we have used three OpenGL library routines or library functions. What they do?
Now, one is glClearColor, the first one with some argument, four arguments are used. This is used to set a background color to our display window and this color is specified with RGB components.
Now, these RGB components are specified in the first three arguments in that order that means this is R, this is G, this is B, with this particular set of values as we all know you will get white as the background color, we can set any background color. For example, if we set all 0, we will get black.
Now, there is also a fourth parameter which we have set as 0.0. Now, this is called alpha value for the specified color and it is used as a blending parameter. In other words it specifies transparency of the color. If we are using value 0.0 that means the color is totally transparent and 1.0 means totally opaque objects. So, it indicates transparency.
Now, here we are displaying a line which is a 2D object. However, OpenGL does not treat 2D objects separately. Now, it treats the 2D pictures as special case of 3D viewing. So, essentially the entire 3D pipeline stages are performed.
So, we need to specify the projection type and other viewing parameters that is done with these two functions glMatrixMode, which is GL_PROJECTION and gluOrtho2D with some arguments.
Now, this function gluOrtho2D here the function is prefixed to GLU rather than GL. So, it indicates that this function belongs to GLU or OpenGL utility another auxiliary library. Earlier we have seen GLUT OpenGL utility toolkit, now we are seeing OpenGL utility another auxiliary library.
And this library provides routines for complex tasks such as setting up of viewing and projection matrices, describing complex objects with line and polygon approximations, processing of surface rendering operations and displaying splines with linear approximations, these are some examples of the complex tasks that are part of the pipeline which are implemented in this OpenGL utility auxiliary library.
Now, together these two functions glMatrixMode and gluOrtho2D specify an orthogonal projection to be used to map the line from view plane to the screen. Now, view plane window specified in terms of lower left and top right corners of the window. So, these arguments specify the lower left and top right corners of the window and during this projection anything outside of this window is clipped out as we have discussed during our pipeline discussion.
Now, let us move to our second function create line. Now, this function is actually creates the line which we want to display. The first line is glClear with some arguments. Now, this function is used to display the window with specified background color. Now, the arguments as you can see an OpenGL symbolic constant indicates bit values in color or the refresh buffer that are to be set to the background color values specified in the function glClearColor function. So, essentially this function indicates what should be the background color of the display window.
Now, OpenGL function also allows us to set the object color with the function glColor3f. So, there are three arguments again they specify the RGB components ‘RGB’, so these two functions are used to set color values to the background as well as to the object.
Now, in the second function this 3f indicates that the three components are specified using floating point values. In other words the values can range between 0.0 to 1.0. So, in this particular case these three values denote green color.
Next we have a piece of code between the two functions glBegin and glEnd, so this indicates the line segment to be drawn between the endpoints provided in the arguments. So, this function essentially creates the line between these two endpoints specified with these two arguments and the functions glVertex2i called twice.
Now, here this 2i in the function as you can guess indicates that the vertices are specified by two integer values denoting the X and Y coordinates, this is quite straightforward.
Now, the first and second endpoints are determined depending on their ordering in the code. So, this will always be treated as the first point because it is appearing before the other one and this will be the second point. So, in the way the code is written the first and second points are determined.
And this function glBegin with this constant GL_LINES as well as the function glEnd indicate that the vertices are line end points.
Now, with all these functions our basic line creation program is ready. One point to be noted here is that these functions may be stored at different locations in the memory depending on the way OpenGL is implemented.
And we need to force the system to process all these functions. This we do with the other function glFlush as shown here. So, this should be again the last line of our picture generation procedure which indicates that all these functions that we have used must be processed one after another.
So, that is how we can create a program using OpenGL. So, in our example we have used OpenGL library in the setting of C language and we have also seen that only OpenGL library is not sufficient, we need to some auxiliary libraries, here we have used GLUT as well as GLU auxiliary libraries, GLUT stands for GL Utility Toolkit which allows us to create the window which is a display dependent operation and GLU allows us to perform other complex tasks which are not there in core OpenGL library.
So, with this we have come to the end of the topic. So, we have learned various things, the graphics hardware including the input output and GPU, also we started with a generic architecture of a graphic system and then learned about various IO and GPU and today we learned about the graphics software, how the softwares are created, different standards and an example program using OpenGL, OpenGL can be used to write any graphics program. Now, with this lecture we have come to the end of the course. So, in the next lecture we will summarize what we have learnt in this course so far.
Whatever I discussed today can we found in this book, you can go through chapter 10, section 10.4 to learn on Graphic Software including the OpenGL example. So, in the last lecture we will summarize our learning so far, so we will see you in the concluding lecture, till then thank you and goodbye.
Log in to save your progress and obtain a certificate in Alison’s free Computer Graphics: Clipping and Scan Conversion online course
Sign up to save your progress and obtain a certificate in Alison’s free Computer Graphics: Clipping and Scan Conversion online course
Please enter you email address and we will mail you a link to reset your password.