290 likes | 454 Views
CGMB214: Introduction to Computer Graphics. Topic 3 Introduction to OpenGL. Objectives. To demonstrate the basic usage of OpenGL To write a simple OpenGL program in C. Introduction. A basic library of functions is provided in OpenGL It helps to specify Graphics primitives Attributes
E N D
CGMB214: Introduction to Computer Graphics Topic 3 Introduction to OpenGL
Objectives To demonstrate the basic usage of OpenGL To write a simple OpenGL program in C
Introduction • A basic library of functions is provided in OpenGL • It helps to specify • Graphics primitives • Attributes • Geometric transformation • Viewing transformation • Etc • OpenGL is designed to be hardware independent, therefore many operations, such as input and output routines are not included in the basic library
Basic OpenGL Syntax Function names in the OpenGL basic library are prefixed with gl Each component word within a function name has its first letter capitalised. E.g. glBegin, glClear, glCopyPixels, glPolygonMode
Basic OpenGL Syntax Arguments, for example parameter name, a value for a parameter or a particular mode begin with uppercase letter GL. The component name comprise of uppercase letter and underscore. E.g. GL_2D, GL_RGB, GL_CCW, GL_POLYGON, GL_AMBIENT_AND_DIFFUSE
Basic OpenGL Syntax Data type in OpenGL starts with capital letters and the remainder is written in lower case. E.g. GLbyte, GLshort, GLint, GLfloat, GLdouble, GLboolean
Related Libraries • OpenGL Utility (GLU) • Provide routines for • Setting up viewing and projection matrices • Describing complex object with lines and polygon approximations • Displaying quadrics and B-splines using linear approximations • Processing the surface rendering operations • Function names start with glu prefix • Open Inventor • Object oriented toolkit written in C++ for interactive 3D applications • OpenGL Extension to the X Window System (GLX) • provides a set of routines that are prefixed with the letter glX
Related Libraries • Apple GL (AGL) • Interface for window-management operations in Apple systems (prefix agl) • Windows-to-OpenGL (WGL) • Prefixed with letter wgl • Presentation Manager to OpenGL (PGL) • An interface for IBM OS/2 • OpenGL Utility Toolkit (GLUT) • Provides library of functions for interacting with any screen-windowing system • Prefixed with letter glut
Header Files • In our graphic program we will need to include the header file for the OpenGL core library • For most application we will also need GLU • We will also need to include the header file for window system • The header file that accesses WGL routines is windows.h • This header file must be listed before the OpenGL and GLU header files because it contains macros needed by the Microsoft Windows version of OpenGL libraries • So the code should look like below # include <windows.h> # include <GL/gl.h> # include <GL/glu.h>
Header Files However if we use GLUT to handle the window-managing operation we don’t need the gl.h and glu.h This is because GLUT will ensure that these header files will be included correctly So the code should look something like below # include <GL/glut.h>
Header Files Other than that we also need to include the header files required by C/C++ E.g. # include <stdio.h> # include <stdlib.h> # include <math.h>
Display-Window Management Using GLUT Initialise GLUT glutInit (&argc, argv); Initialising function Can be substitute with command line argument, but as for now, just leave it as it is glutInit will initialize the GLUT library and negotiate a session with the window system. During this process, glutInit may cause the termination of the GLUT program with an error message to the user if GLUT cannot be properly initialized. glutInit must be called before others.
Display-Window Management Using GLUT Initialise OpenGL on how to set up its frame buffer and colour glutInitDisplayMode (GL_SINGLE|GL_RGB); Initialising display mode function Built in constant which are OR-edtogether. Can be changed based on table below • Frame buffer is a special 2-dimensional array in main memory where the graphical image is stored. OpenGL maintains an enhanced version of the frame buffer with additional information • The system also needs to know how we are representing colors of our general needs in order to determine the depth (number of bits) to assign for each pixel in the frame buffer • The argument to glutInitDisplayMode() is a logical-or (using the operator “|”) of a number of possible options
Display-Window Management Using GLUT • To understand the difference between these two options, we need to know how frame buffer works. In raster graphics systems, whatever is written to the frame buffer is immediately transferred to the display. • This process is repeated frequently (30 – 60 times a second). • To do this a typical approach is to first erase the old contents by setting all the pixels to some background color, i.e. black. After this, the new contents are drawn • However, even though these processes might happen very fast, the process of setting the image to black and then redrawing everything produces a noticeable flicker in the image
Display-Window Management Using GLUT Set the initial location of the window glutInitWindowPosition (int x , int y); Initialising Window Position function Setting the position of the window from upper left corner of the screen
Display-Window Management Using GLUT Initialise the size of the window glutInitWindowSize (int width, int height); Initialising window size function Setting the size of the window
Display-Window Management Using GLUT Open and display a window glutCreateWindow (“string”); Function that open and display a window A character string that will appear on the title bar
Display-Window Management Using GLUT Specify what the display window should contain glutDisplayFunc (function name); Function to specify what to be displayed on the window The name of the function that contains the lines to be executed
Display-Window Management Using GLUT Make sure the window continue running. Should be at the last line Function to specify what to be displayed on the window glutMainLoop(); This should be the last function in the program. It will display the initial graphics and puts the program into an infinite loop that checks for input from devices such as mouse. If the program is not an interactive program, this function will make sure the window is being displayed all the time until the user close the window.
Display-Window Management Using GLUT 300 px 400 px
Display-Window Management Using GLUT To set the background color of the window glClearColor (Red, Green, Blue, Opacity); Function to specify the background color of the window The first 3 parameters determine the value of Red, Green and Blue, respectively. The last parameter is for opaqueness
Display-Window Management Using GLUT To set the color of the primitives glColor3i (Red, Green, Blue); Function to specify the background color of the window The letter ‘i’ is for the data type. ‘i’ is for integer The 3 parameters determine the value of Red, Green and Blue, respectively. The number 3 is for the number of color component, Red, Green and Blue.
Display-Window Management Using GLUT To display 2D picture (because 2D is a special case of 3D) glMatrixMode (Glenum mode); Function to specify the current matrix Can be set to either to define camera, perspective, measuring system etc. glOrtho2D (start x, end x, start y, end y); Function to defines the coordinate reference frame Specify the range of x and y