The Programmer’s Interface • Programmer sees the graphics system through an interface: the Application Programmer Interface (API)
Introduction to OpenGL Prof. George Wolberg Dept. of Computer Science City College of New York
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
API Contents
2
Object Specification • Most APIs support a limited set of primitives including
• Functions that specify what we need to form an image
- Points (1D object) - Line segments (2D objects) - Polygons (3D objects) - Some curves and surfaces
- Objects - Viewer - Light Source(s) - Materials
• Quadrics • Parametric polynomial
• Other information - Input from devices such as mouse and keyboard - Capabilities of system Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
3
Example
• All are defined through locations in space or vertices Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
4
Camera Specification • Six degrees of freedom
type of object location of vertex
- Position of center of lens - Orientation
glBegin(GL_POLYGON) glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glEnd( );
• Lens • Film size • Orientation of film plane
end of object definition
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
5
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
6
1
Following the Pipeline: Transformations
Lights and Materials • Types of lights
• Much of the work in the pipeline is in converting object representations from one coordinate system to another
- Point sources vs distributed sources - Spot lights - Near and far sources - Color properties
- World coordinates - Camera coordinates - Screen coordinates
• Material properties - Absorption: color properties - Scattering
• Every change of coordinates is equivalent to a matrix transformation
• Diffuse • Specular
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
7
Clipping
8
Projection
• Just as a real camera cannot “see” the whole world, the virtual camera can only see part of the world space
• Must carry out the process that combines the 3D viewer with the 3D objects to produce the 2D image
- Objects that are not within this volume are said to be clipped out of the scene
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
- Perspective projections: all projectors meet at the center of projection - Parallel projection: projectors are parallel, center of projection is replaced by a direction of projection
9
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
10
Rasterization • If an object is visible in the image, the appropriate pixels in the frame buffer must be assigned colors
Programming with OpenGL Part 1: Background
- Vertices assembled into objects - Effects of lights and materials must be determined - Polygons filled with interior colors/shades - Must have also determined which objects are in front (hidden surface removal) Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
Prof. George Wolberg Dept. of Computer Science City College of New York
11
2
Objectives
Early History of APIs • IFIPS (1973) formed two committees to come up with a standard graphics API
• Development of the OpenGL API • OpenGL Architecture
- Graphical Kernel System (GKS)
- OpenGL as a state machine
• 2D but contained good workstation model
• Functions
- Core • Both 2D and 3D
- Types - Formats
- GKS adopted as IS0 and later ANSI standard (1980s)
• Simple program
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
• GKS not easily extended to 3D (GKS-3D) • Far behind hardware development 13
PHIGS and X
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
14
SGI and GL
• Programmers Hierarchical Graphics System (PHIGS)
• Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) • To use the system, application programmers used a library called GL • With GL, it was relatively simple to program three dimensional interactive applications
- Arose from CAD community - Database model with retained graphics (structures)
• X Window System - DEC/MIT effort - Client-server architecture with graphics
• PEX combined the two - Not easy to use (all the defects of each) Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
15
OpenGL
16
OpenGL Evolution • Controlled by an Architectural Review Board (ARB)
• The success of GL lead to OpenGL in 1992, a platform-independent API that was
- Members include SGI, Microsoft, Nvidia, HP, 3DLabs,IBM,……. - Relatively stable (present version 1.4)
- Easy to use - Close enough to the hardware to get excellent performance - Focused on rendering - Omitted windowing and input to avoid window system dependencies Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
• Evolution reflects new hardware capabilities – 3D texture mapping and texture objects – Vertex programs
- Allows for platform specific features through extensions - See www.opengl.org for up-to-date info 17
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
18
3
OpenGL Libraries
GLUT
• OpenGL core library
• OpenGL Utility Toolkit (GLUT)
- OpenGL32 on Windows - GL on most Unix/Linux systems
- Provides functionality common to all window systems
• OpenGL Utility Library (GLU)
• • • •
- Provides functionality in OpenGL core but avoids having to rewrite code
• Links with window system
- Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform
- GLX for X window systems - WGL for Windows - AGL for Macintosh
• Slide bars
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
19
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
Software Organization
X, Win32, Mac O/S
Geometric pipeline
Immediate Mode
Polynomial Evaluator
GLUT
GLX, AGL or WGL
20
OpenGL Architecture
application program OpenGL Motif widget or similar
Open a window Get input from mouse and keyboard Menus Event-driven
Per Vertex Operations & Primitive Assembly
GLU GL
CPU
Display List
Rasterization
software and/or hardware
Per Fragment Operations
Frame Buffer
Texture Memory Pixel Operations
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
21
OpenGL Functions
22
OpenGL State
• Primitives
• OpenGL is a state machine • OpenGL functions are of two types
- Points - Line Segments - Polygons
- Primitive generating • Can cause output if primitive is visible • How vertices are processed and appearance of primitive are controlled by the state
• Attributes • Transformations
- State changing
- Viewing - Modeling
• Transformation functions • Attribute functions
• Control • Input (GLUT) Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
23
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
24
4
Lack of Object Orientation
OpenGL function format
• OpenGL is not object oriented so that there are multiple functions for a given logical function, e.g. glVertex3f, glVertex2i, glVertex3dv,…..
function name glVertex3f(x,y,z)
• Underlying storage mode is the same • Easy to create overloaded functions in C++ but issue is efficiency
belongs to GL library
x,y,z are floats
glVertex3fv(p) p is a pointer to an array Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
25
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
OpenGL #defines
A Simple Program
• Most constants are defined in the include files gl.h, glu.h and glut.h
Generate a square on a solid background
26
- Note #include should automatically include the others - Examples -glBegin(GL_POLYGON) -glClear(GL_COLOR_BUFFER_BIT)
• include files also define OpenGL data types: Glfloat, Gldouble,…. Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
27
simple.c
28
Event Loop
#include void mydisplay(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5,-0.5); glVertex2f(-0.5, 0.5); glVertex2f( 0.5, 0.5); glVertex2f( 0.5,-0.5); glEnd(); glFlush(); } int main(int argc, char** argv){ glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop(); } Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
• Note that the program defines a display callback function named mydisplay - Every glut program must have a display callback - The display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is opened - The main function ends with the program entering an event loop
29
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
30
5
Defaults
Notes on compilation
•simple.c is too simple
• See website and ftp for examples • Unix/Linux
• Makes heavy use of state variable default values for
- Include files usually in …/include/GL - Compile with –lglut –lglu –lgl loader flags - May have to add –L flag for X libraries - Mesa implementation included with most linux distributions - Check web for latest versions of Mesa and glut
- Viewing - Colors - Window parameters
• Next version will make the defaults more explicit Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
31
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
32
Compilation on Windows • Visual C++ - Get glut.h, glut32.lib and glut32.dll from web - Create a console application - Add opengl32.lib, glut32.lib, glut32.lib to project settings (under link tab)
• Borland C similar • Cygwin (linux under Windows)
Programming with OpenGL Part 2: Complete Programs Prof. George Wolberg Dept. of Computer Science City College of New York
- Can use gcc and similar makefile to linux - Use –lopengl32 –lglu32 –lglut32 flags Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
33
Objectives
Program Structure • Most OpenGL programs have a similar structure that consists of the following functions
• Refine the first program - Alter the default values - Introduce a standard program structure
-main():
• Simple viewing
• defines the callback functions • opens one or more windows with the required properties • enters event loop (last executable statement)
- Two-dimensional viewing as a special case of three-dimensional viewing
-init(): sets the state variables
• Fundamental OpenGL primitives • Attributes
• viewing • Attributes
- callbacks • Display function • Input and window functions
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
35
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
36
6
Simple.c revisited
main.c includes gl.h
#include
• In this version, we will see the same output but have defined all the relevant state values through function calls with the default values • In particular, we set
int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); define window properties glutDisplayFunc(mydisplay);
- Colors - Viewing conditions - Window properties
display callback set OpenGL state
init(); glutMainLoop(); }
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
37
GLUT functions
black clear color void init() opaque window { glClearColor (0.0, 0.0, 0.0, 1.0);
- RGB color - Single buffering - Properties logically ORed together
fill with white glColor3f(1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); viewing volume } 39
Coordinate Systems
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
40
OpenGL Camera
• The units of in glVertex are determined by the application and are called world or problem coordinates • The viewing specifications are also in world coordinates and it is the size of the viewing volume that determines what will appear in the image • Internally, OpenGL will convert to camera coordinates and later to screen coordinates Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
38
init.c
•glutInit allows application to get command line arguments and initializes system •gluInitDisplayMode requests properties of the window (the rendering context)
•glutWindowSize in pixels •glutWindowPosition from top-left corner of display •glutCreateWindow create window with title “simple” •glutDisplayFunc display callback •glutMainLoop enter infinite event Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003 loop
enter event loop
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
41
• OpenGL places a camera at the origin pointing in the negative z direction • The default viewing volume is a box centered at the origin with a side of length 2
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
42
7
Orthographic Viewing
Transformations and Viewing • In OpenGL, the projection is carried out by a projection matrix (transformation) • There is only one set of transformation functions so we must set the matrix mode first
In the default orthographic view, points are projected forward along the z axis onto the plane z=0
glMatrixMode (GL_PROJECTION) z=0
• Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume
z=0
glLoadIdentity (); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
43
Two- and threedimensional viewing
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
44
mydisplay.c
• In glOrtho(left, right, bottom, top, near, far) the near and far distances are measured from the camera • Two-dimensional vertex commands place all vertices in the plane z=0 • If the application is in two dimensions, we can use the function gluOrtho2D(left, right,bottom,top) • In two dimensions, the view or clipping volume becomes a clipping window Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
45
OpenGL Primitives
void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5,-0.5); glVertex2f(-0.5, 0.5); glVertex2f( 0.5, 0.5); glVertex2f( 0.5,-0.5); glEnd(); glFlush(); } Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
46
Example: Drawing an Arc • Given a circle with radius r, centered at (x,y), draw an arc of the circle that sweeps out an angle θ.
GL_POINTS
GL_POLYGON GL_LINES
( x, y ) = ( x0 + r cosθ , y0 + r sin θ ),
GL_LINE_STRIP
for 0 ≤ θ ≤ 2π .
GL_LINE_LOOP
GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP
GL_TRIANGLE_FAN
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
47
Angel: Interactive Computer Graphics 3E © Addison-Wesley 2003
48
8
The Line Strip Primitive
Polygon Issues
void drawArc(float x, float y, float r, float t0, float sweep) { float t, dt; /* angle */ int n = 30; /* # of segments */ int i; t = t0 * PI/180.0; /* radians */ dt = sweep * PI/(180*n); /* increment */ glBegin(GL_LINE_STRIP); for(i=0; i 0) { for(j=0; j