Download presentation
Presentation is loading. Please wait.
1
OpenGL A Brief Overview
2
What is OpenGL? It is NOT a programming language.
It is a Graphics Rendering API consisting of a set of functions with a well defined interface. Whenever we say that a program is OpenGL-based or OpenGL applications, we mean that it is written in some programming language (such as C/C++ or Java) that makes calls to one or more of OpenGL libraries.
3
OpenGL & Alternative Based on GL (graphics library) by Silicon Graphics Inc. (SGI) ( Advantages: Runs on everything, including smart phones (OpenGL/ES) Alternatives: Microsoft’s Direct3D – limited to MS-Windows ( Sun’s Java3D – slower, implemented on top of OpenGL (
4
Useful Websites and Books
Official Site Non official sites BOOKS OpenGL Red Book ( & OpenGL Blue Book (
5
Library Layers OpenGL = GL + GLU
APP EXT GLUT OpenGL = GL + GLU Basic low-level GL routines implemented using OS graphics routines Time-saving higher-level GLU routines implemented using GL routines GLUT opens and manages OpenGL windows and adds helper functions OpenGL Extensions provide direct device-dependent access to hardware GLU GL OS Driver HW
6
OpenGL API Functions OpenGL contains over 200 functions
Primitive functions: define the elements (e.g. points, lines, polygons, etc.) Attribute functions: control the appearance of primitives (e.g. colors, line types, light source, textures, etc.) Viewing functions: determine the properties of camera. Handle transformations. Windowing functions: not part of core OpenGL (in GLUT) Other functions
7
Libraries and Headers Library Name Library File Header File Note
OpenGL opengl32.lib (Win) -lgl (UNIX) gl.h “core” library Auxiliary library glu32.lib (Win) -lglu (UNIX) glu.h handles a variety of accessory functions Utility toolkit glut32.lib (Win) -lglut (UNIX) glut.h glaux.h window management & others
8
Function Naming Conventions
glColor3f(…) library name, command name, # of arguments, argument type gl: OpenGL glu: GLU glut: GLUT f: the argument is float type i: the argument is integer type v: the argument requires a vector
9
A Sample Program 1 2 3 4 #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glut.h> void main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutCreateWindow (“My First Program"); myinit (); glutDisplayFunc ( display ); glutReshapeFunc ( resize ); glutKeyboardFunc ( key ); glutMainLoop (); } 1 2 3 4
10
1. Initialize & Create Window
Initiate a GLUT window… void main (int argc, char **argv) { glutInit (&argc, argv); // GLUT initialization glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // display model glutInitWindowSize (500, 500); // set window size glutCreateWindow (“My First Program”); // create window …… }
11
2. Initialize OpenGL State
Set up the canvas for drawing, including the background color and view void myinit(void) { glClearColor(1.0, 1.0, 1.0, 1.0); // background color glColor3f(1.0, 0.0, 0.0); // line color glMatrixMode(GL_PROJECTION); // set up viewing: glLoadIdentity(); // load identity matrix gluOrtho2D(0.0, 500.0, 0.0, 500.0); // specify Orthographic view glMatrixMode(GL_MODELVIEW); // go back to MV matrix }
12
3. Register Callback Functions
Event handling void main (int argc, char **argv) { …… glutDisplayFunc ( display ); // display callback glutReshapeFunc ( resize ); // window resize callback glutKeyboardFunc ( key ); // keyboard callback } Event Loops and Callback Functions Interactive programs react to the events such as mouse or keyboard events and window events. Callback Function - Routine to call when something happens (eg. window resize, redraw, user input, etc) GLUT uses a callback mechanism to do its event processing
13
(side note) GLUT Callback Functions
Contents of window need to be refreshed glutDisplayFunc() Window is resized or moved glutReshapeFunc() Key action glutKeyboardFunc() Mouse button action glutMouseFunc() Mouse moves while a button is pressed glutMotionFunc() Mouse moves regardless of mouse button state glutPassiveMouseFunc() Called when nothing else is going on glutIdleFunc()
14
3.1 Rendering Callback Here is the place you do the drawing!!!
void display( void ) { int k; glClear(GL_COLOR_BUFFER_BIT); for( k=0; k<5000; k++) …… } One of the most important callbacks is the glutDisplayFunc() callback. This callback is called when the window needs to be refreshed. It’s here that you’d do all of your OpenGL rendering.
15
3.2 Window Resize Callback
void resize(int w, int h) { …… display(); } It’s called when the window is resized or moved
16
3.3 Keyboard Input Callback
void key( unsigned char mkey, int x, int y ) { switch( mkey ) case ‘q’ : exit( EXIT_SUCCESS ); break; …… } Above is a simple example of a user input callback. In this case, the routine was registered to receive keyboard input. GLUT supports user input through a number of devices including the keyboard, mouse, dial and button boxes and spaceballs.
17
4. Event Process Loop This is where your application receives events, and schedules when callback functions are called void main (int argc, char **argv) { …… glutMainLoop(); } Enter the main event processing loop. This is where your application receives events, and schedules when callback functions are called.
18
Viewport Coordinates Physical per-pixel integer coordinates
(HRES-1, VRES-1) (0,VRES-1) Physical per-pixel integer coordinates Also called screen or device coordinates glViewport(x,y,w,h) x,y – lower left pixel (integers) w – width h – height Sometimes (0,0) is in the upper left corner (e.g. for mouse input) (0,0) (HRES-1,0)
19
Window Coordinates Logical, mathematical floating-point coordinates
(1,1) (-1,1) Logical, mathematical floating-point coordinates glOrtho(l,r,b,t,n,f) (for 3D) left, right, bottom, top near, far: limits depth For 2D use gluOrtho2D(l,r,b,t) calls glOrtho(l,r,b,t,-1,1) To use per-pixel coordinates, call: gluOrtho2D(-.5,-.5,w-.5,h-.5); (-1,-1) (1,-1)
20
2D Geometric Primitives
Primitives – fundamental entities such as point and polygons Basic types of geometric primitives Points Line segments Polygons
21
2D Geometric Primitives
GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_FAN GL_POLYGON GL_QUADS All geometric primitives are specified by vertices
22
Specifying Geometric Primitives
glBegin( type ); glVertex*(…); …… glEnd(); OpenGL organizes vertices into primitives based upon which type is passed into glBegin(). The possible types are: GL_POINTS GL_LINE_STRIP GL_LINES GL_LINE_LOOP GL_POLYGON GL_TRIANGLE_STRIP GL_TRIANGLES GL_TRIANGLE_FAN GL_QUADS GL_QUAD_STRIP type determines how vertices are combined
23
An Example void drawSquare (GLfloat *color) {
glColor3fv( color ); // sets the color of the square glBegin(GL_POLYGON); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glVertex2f ( 1.1, 1.1 ); glVertex2f ( 0.0, 1.0 ); glEnd(); glFlush(); // force the renderer to output the results }
24
Points (1,1) (-1,1) glBegin(GL_POINTS); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
25
Lines (1,1) (-1,1) glBegin(GL_LINES); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
26
Line Strip (1,1) (-1,1) glBegin(GL_LINE_STRIP); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
27
Line Loop (1,1) (-1,1) glBegin(GL_LINE_LOOP); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
28
Polygon OpenGL only supports convex polygons
(and really only triangles) (1,1) (-1,1) glBegin(GL_POLYGON); glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1) (1,-1)
29
Quads (1,1) (-1,1) glBegin(GL_QUADS); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
30
Quads glBegin(GL_QUADS); glVertex2f(-.6,1.); glVertex2f(-.6,.6);
glEnd(); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.);
31
Quads (1,1) (-1,1) (-1,1) glBegin(GL_QUADS); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
32
Triangles (1,1) (-1,1) glBegin(GL_TRIANGLES); glVertex2f(-.6,1.);
glEnd(); (-1,-1) (1,-1)
33
Triangles (1,1) (-1,1) glBegin(GL_TRIANGLES); glVertex2f(-.6,1.);
… glEnd(); (-1,-1) (1,-1)
34
Triangle Strip (1,1) (-1,1) glBegin(GL_TRIANGLE_STRIP);
glVertex2f(-.6,1.); glVertex2f(-.6,.6); glVertex2f(-.2,.6); glVertex2f(-.2,-.6); glVertex2f(-.6,-.6); glVertex2f(-.6,-1.); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glVertex2f(.2,-.6); glVertex2f(.2,.6); glVertex2f(.6,.6); glVertex2f(.6,1.); glEnd(); (-1,-1) (1,-1)
35
Triangle Strip First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous two vertices (1,1) (-1,1) glBegin(GL_TRIANGLE_STRIP); glVertex2f(-.6,1.); glVertex2f(.6,1.); glVertex2f(-.2,.6); glVertex2f(.2,.6); glVertex2f(-.2,-.6); glVertex2f(.2,-.6); glVertex2f(.6,-1.); glVertex2f(.6,-.6); glEnd(); … (-1,-1) (1,-1)
36
First two vertices prime the pump, then every new vertex creates a triangle connecting it to the previous vertex and the first vertex Triangle Fan (1,1) (-1,1) glBegin(GL_TRIANGLE_FAN); glVertex2f(-.2,.6); glVertex2f(-.6,.6); glVertex2f(-.6,1.); glVertex2f(.6,1.); glVertex2f(.2,.6); glVertex2f(.2,-.6); glVertex2f(-.2,-.6); glEnd(); … (-1,-1) (1,-1)
37
Assigning Color glColor3f(0,0,1); glBegin(GL_POLYGON);
glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glBegin(GL_POLYGON); glColor3f(0,1,0); glVertex2f(-1,1); glColor3f(0,0,1); glVertex2f(-1,-1); glColor3f(1,0,0); glVertex2f(1,-1); glEnd(); glColor3f(1,0,0); glBegin(GL_POLYGON); glVertex2f(-1,1); glVertex2f(-1,-1); glVertex2f(1,-1); glEnd(); glColor3f(0,0,0); glBegin(GL_LINE_LOOP);
38
Graphics Pipeline of OpenGL
Model Coords Model Xform World Coords Viewing Xform Viewing Coords Perspective Distortion Still Clip Coords Clipping Clip Coords Homogeneous Divide Window Coordinates Window to Viewport Viewport Coordinates
39
Coordinate Systems and Transformation
Identify which matrix we wish to alter Set the matrix to an identity matrix Alter the matrix to form the desired matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
40
Coordinate Systems and Transformation
glMatrixMode(GLenum mode); Identify which matrix we wish to alter – The mode is usually GL_MODELVIEW, GL_PROJECTION glLoadIdentity(); Set the current matrix to an identity matrix
41
Transformations and Camera Analogy
Modeling transformation Positioning and moving the model. Viewing transformation Positioning and aiming camera in the world. Projection transformation Adjusting the lens of the camera. Viewport transformation Enlarging or reducing the physical photograph. .
42
Transformations in OpenGL
Transformations are specified by matrix operations. Desired transformation can be obtained by a sequence of simple transformations that can be concatenated together. Transformation matrix is usually represented by 4x4 matrix (homogeneous coordinates). Provides matrix stacks for each type of supported matrix to store matrices.
43
Specifying Operations (1)
Translation glTranslate {fd} (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that translates an object by the given x, y, z.
44
Specifying Operations (2)
Scaling glScale {fd} (TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that scales an object by the given x, y, z.
45
Specifying Operations (3)
Rotation glRotate {fd} (TYPE angle, TYPE x, TYPE y, TYPE z) Multiplies the current matrix by a matrix that rotates an object in a counterclockwise direction about the ray from origin through the point by the given x, y, z. The angle parameter specifies the angle of rotation in degrees.
46
Order of Transformations
The transformation matrices appear in reverse order to that in which the transformations are applied. In OpenGL, the transformation specified most recently is the one applied first.
47
Viewing-Modeling Transformation
If given an object, and we want to render it from a viewpoint, what information do we have to have? Viewing position Which way I am looking at Which way is “up” …..
48
Default Viewing +X +Z +Y By default, the camera is at the origin, looking down the negative z-axis In the default position, the camera is at the origin, looking down the negative z-axis If you get this wrong, you may see nothing in your image.
49
Where are we and what are we looking at?
y y Loot at (atx, aty, atz) View-up vector (upx, upy, upz) x x Model z z Eyepoint (eyex, eyey, eyez)
50
Viewing in OpenGL Look-At Function
gluLookAt (eyex, eyey, eyez, atx, aty, atz, upx, upy, upz ) Defines a viewing matrix and multiplies the current matrix by it. Alters the ModelView matrix.
51
Projection Transformation
Projection & Viewing Volume Projection Transformation Viewpoint Transformation
52
Perspective Projection Volume
Far-plane: zFar Near-plane: zNear Viewing volume h w aspect ratio = w/h y z x fovy
53
Perspective Projection Commands
glFrustum( left, right, bottom, top, zNear, zFar ) Creates a matrix for a perspective viewing frustum and multiplies the current matrix by it. Alters the Projection matrix.
54
Perspective Projection Commands
gluPerspective( fovy, aspect, zNear, zFar ) Alternative to glFrustum(..). Creates a matrix for an perspective viewing frustum and multiplies the current matrix by it. Alters the Projection matrix. Note: fovy is the field of view (fov) angle between the top and bottom planes of the clipping volume. aspect is the aspect ratio
55
Enabling GL Features Features – lighting, hidden-surface removal, texture mapping, etc… Each feature will slow down the rendering process. We can enable/disable each feature individually void glEnable(GLenum feature) void glDisable(GLenum feature) E.g. glEnable(GL_LIGHTING);
56
Enabling GL Features GL_ALPHA_TEST GL_BLEND GL_CLIP_PLANE i
If enabled, do alpha testing. GL_AUTO_NORMAL If enabled, generate normal vectors when either GL_MAP2_VERTEX_3 or GL_MAP2_VERTEX_4 is used to generate vertices. See glMap2. GL_BLEND If enabled, blend the incoming RGBA color values with the values in the color buffers.. GL_CLIP_PLANE i If enabled, clip geometry against user-defined clipping plane i.. GL_COLOR_LOGIC_OP If enabled, apply the currently selected logical operation to the incoming RGBA color and color buffer values. GL_COLOR_MATERIAL If enabled, have one or more material parameters track the current color. GL_COLOR_TABLE If enabled, preform a color table lookup on the incoming RGBA color values. GL_CONVOLUTION_1D If enabled, perform a 1D convolution operation on incoming RGBA color values. GL_CONVOLUTION_2D If enabled, perform a 2D convolution operation on incoming RGBA color values. ……
57
Double Buffering Use two buffers: front buffer and back buffer to guarantee the displaying of a fully redrawn buffer image glutSwapBuffers(); Replace glFlush() by glutSwapBuffer() in the display callback if using double buffering
58
Advanced Topics Lighting Texture Mapping Plotting Implicit Functions
Shadows Fog Picking (object selection) GUI (glut pop-up menus, glui library) Reference links given on slide 3
61
main function
62
OpenGL header files and libraries
63
GLUI interface libraries
65
Acknowledgment Thanks for materials from Dr. John C. Hart
Dr. Eugene Zhang Dr. Mike Bailey
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.