200 likes | 213 Views
Learn about OpenGL, a high-quality graphics rendering API independent of devices, windows, and operating systems. This introductory guide covers setting up OpenGL, syntax basics including functions, data types, and command formats.
E N D
OpenGL – Part One Introduction
What is OpenGL • An application programming interface (API) • A (low-level) Graphics rendering API • Generate high-quality color images composed of geometric and image primitives • Display device independent • Window system independent • Operating system independent • OpenGL: Open Graphics Library • GLU: OpenGL Toolkit • GLUT: OpenGL Utility Toolkit
OpenGL Basics • OpenGL’s primary function – Rendering • Rendering – converting geometric/mathematical object descriptions into frame buffer values • OpenGL can render: • Geometric primitives • Bitmaps and Images (Raster primitives)
Setting up OpenGL • Open this website: http://www.xmission.com/~nate/glut.html • Click over the link glut-3.7.6-bin.zip (117 KB) and download the file. • Unzip the file • Do following copies: • copy "glut.h" to C:\Program Files\Microsoft Visual Studio\VC98\Include\GL (Visual C++ include directory) • copy "glut32.lib" to c:\Program Files\Microsoft Visual Studio\VC98\lib (Visual C++ library directory) • copy "glut32.dll" to C:\WINNT\SYSTEM32, where your system files are located.
Basic OpenGL syntax • Functions begin with gl • Constants and function arguments and data types begin with GL • Constant words are written in capital letters and separated using ( _ ) underscore • Like: GL_2D, GL_POLYGON • OpenGl use special built in data types to work with all environments: • GLbyte • Glint • GLfloat • GLdouble • GLboolean
Header Files • Use the following header files in your program: • # include <windows.h> • # include <GL/gl.h> • # include <GL/glu.h> • # include <GL/glut.h> • You may need other header files for c++ command like: • # include <iostream> • # include <cmath>
Basic Functions • 1. glutInit( &argc, argv ); • Is used to initialize the glut library. • 2. glutCreateWindow( “Type a name for window here” ); • Is used to creates a top-level window . • 3. glutMainLoop( ); • Must be the last one in our program. • Display the initial graphics • Put the program in an infinite loop that check for input from devices such as mouse or keyboard.
Basic Functions (cont.) • 4/5. glutInitWindowPosition (…), glutInitWindowSize(…) • Is used to set the initial window position and size respectively • void glutInitWindowSize(int width, int height); • void glutInitWindowPosition(int x, int y); • width • Width in pixels. • height • Height in pixels. • x • Window X location in pixels. • y • Window Y location in pixels.
Basic Functions (cont.) • 6. glutInitDisplayMode (…); • Is used to initialze the display mode • May have more than one mode. • Modes examples: • GLUT_SINGLE, used to select a single buffered window • GLUT_RGB, used to specify color mode • glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
Basic Functions (cont.) • Primitives are specified using 7. glBegin( primType ) ; // define your primitives here 8. glEnd( ); • primType:GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, … • glVertix2i(x value, y value); // integer values • glVertix2f(x value, y value); // float values • Used to display point (x, y)
Basic Functions (cont.) • 9. glMatrixMode (GL_PROJECTION) • Specifies that the used matrix is the projection matrix • 10. gluOrtho2D (left , right , bottom , top ) • Used to display part from an image that specified by left/right/bottom/top values • See the figure in the next slide.
Basic Functions (cont.) • The bordered part is to be displayed.
Basic Functions (cont.) • 11. glFlush( ); • To ensures that the drawing commands are actually executed rather than stored in a buffer. • 12. glutDisplayFunction(... write your fn name here …); • To call the function in which you drawn shapes. • i.e glutDisplayFunction ( drawLine);, where drawLine is the function that you wrote to display a line
Color Functions • 13. glColor3f( red value, green value, blue value) ; • Used to specify the wanted color • Has three float parameter; • 14. glClearColor( red value, green value, blue value, alpha value) ; • Used to specify the initial background color. • Has four float parameters • Alpha value is used to determine the color of two overlapped objects
Color Functions (cont.) • 15. glClear ( GL_COLOR_BUFFER_BIT); • Used to set the bit value in the color buffer (refresh buffer) to the color indicated in the glClearColor function. • Colors that can be used after 0/1 combination for red, green and blue values appear in the next slide.
Example The following page contains a complete C++/OpenGL program to draw simple triangle