840 likes | 1.06k Views
OpenGL 1 Angel: Chapter 2 OpenGL Programming and Reference Guides, Angel, other sources. CSCI 6360/4360. Introduction. Angel’s (and others) “top down” approach … vs. the “old school” approach OpenGL is pretty complicated …
E N D
OpenGL 1Angel: Chapter 2OpenGL Programming and Reference Guides, Angel, other sources. CSCI 6360/4360
Introduction • Angel’s (and others) “top down” approach …vs. the “old school” approach • OpenGL is pretty complicated … • Especially, in that programmer has access to “inner workings” of (graphics) system • Rather in contrast to pedagogic, and limited access, environments • Below is single engine aircraft, next, look out for the ejection button …
. • .
Overview • The world of computer graphics …which you’ve just seen … • Development of the OpenGL API • … and why things are not as easy these days, OGL (free) resources • OpenGL Architecture • OpenGL as a state machine and geometry processing machine • Functions • Types • Formats • Simple viewing • Coordinate systems, orthographic viewing • Practical things: • Color, Viewports • A first program • Event loop, compiler things, first assignment
Early History of Graphics APIs(pre-OpenGL pre-history) • IFIPS (1973) formed two committees to come up with a standard graphics API • Graphical Kernel System (GKS) - 2D but contained good workstation model • Core - Both 2D and 3D • GKS adopted as IS0 and later ANSI standard (1980s) • GKS not easily extended to 3D (GKS-3D) • Far behind hardware development • Programmers Hierarchical Graphics System (PHIGS) • 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)
SGI, GL and OpenGL • Silicon Graphics (SGI) revolutionized graphics workstation by implementing the pipeline in hardware (1982) • Had to be able to program it! • To access system, application programmers used a library called “GL” • Relatively simple to program 3d interactive applications • Success of GL lead to OpenGL (1992), a platform-independent API • Easy to use • Close enough to the hardware to get excellent performance • Focus on rendering • Omitted windowing and input to avoid window system dependencies
OpenGL Evolution • Controlled by an Architectural Review Board (ARB) • (see opengl.org for all you could want) • Members include Microsoft, Nvidia, HP, 3DLabs, IBM,……. • Relatively stable (present version 4.3) • Evolution reflects new hardware capabilities • 3D texture mapping and texture objects • Vertex shader programs • Allows for platform specific features through extensions • OpenGL continues to evolve • Reflects, in large part, hardware evolution … see below: • “Software migrates to hardware …” • E.g., for cg, from routines in main memory to firmware on graphics controller • Now, “hardware (or firmware) migrates to software!” • E.g., cg programmable shaders – now firmware not firm! • I.e., it’s all programmable
OpenGL Evolution • “Software migrates to hardware …” • Cg, from routines in main memory to firmware on graphics controller • Now, “hardware (or firmware) migrates to software!” • Cg programmable shaders – now firmware not firm! • I.e., it’s all programmable
OpenGL Evolution(Version History – Focus on Shaders – and this matters for you …) • 1.0, 1992, “Pioneers” – now, didn’t have to buy SGI’s to use! • 1995, Microsoft DirectX competes, only recently OpenGL support in MS Windows • 2.0, 2004, first version for shader programming • 3.0, 2008, objects not bound to states • No new functionality for “old” (graphics) object system • Old = immediate mode + fixed functionality (non-shader mode) • Left old functionality there, but as deprecated (supported, but by context) • All fixed-function vertex and fragment processing • Direct-mode rendering, using glBegin and glEnd • Display lists • Indexed-color rendering targets • Angel examples require 3.2 • Will see what version your computer supports! • 4.0, 2010, nothing major, supported on latest Intel laptop CPU/GPU IC • H 4000 -- 4 cores, 100+ pipelines • 4.1, 2010, and 4.2, 2011, add geometry shaders and tessellator
OpenGL Evolution(Version History – Focus on Shaders – and this matters for you …) • So, you find yourself at the “time of change” for computer graphics programming • From “old” immediate mode OpenGL to “new” shader based • E.g., as noted this 6th edition of Angel, is the first to provide it • AND, the programs in the text require OpenGL version 3.2 to execute! • E.g., recent versions of OpenGL are also shader based (next slide) • More, as we progress in the next couple of weeks
Other Versions of OpenGL, Direct X • OpenGL ES • Embedded systems, e.g., phones, e.g., http://developer.android.com/guide/topics/graphics/opengl.html • Version 1.0 simplified OpenGL 2.1 • Version 2.0 simplified OpenGL 3.1 • Shader based • WebGL • Javascript implementation of ES 2.0 • Supported on newer browsers • Direct X • Windows only • Advantages: • Better control of resources, Access to high level functionality • Disadvantages: • New versions not backward compatible, Windows only • Recent advances in shaders leading to convergence with OpenGL
OpenGL, Resources • Below contains much useful, and free information • www.opengl.org • Tutorials • Many available online • Note that most are “old” OpenGL, vs. shader based … though both should ultimately be mastered • “OpenGL Documentation Overview”: • OpenGL is the industry's most widely used, supported and best documented 2D/3D graphics API making it inexpensive & easy to obtain information on implementing OpenGL in hardware and software. There are numerous books, tutorials, online coding examples, coding seminars, and classes that document the API, Extensions, Utility Libraries, and Platform Specific Implementations. • At opengl.org there are free books – details follow • The OpenGL Reference Manual • The OpenGL Programming Guide
Free Books from OpenGL.org • The “Red Book” • The OpenGL Programming Guide 4th Edition: The Official Guide to Learning OpenGL Version 4.3, 8th Ed. • All the new stuff, ~$50 • at http://www.opengl.org/ • Top level menu: Documentation – OpenGl …. Guide • Earlier versions (v. 1.1) available online: • “limited, but useful” … • http://www.opengl.org/documentation/red_book/ • Pointers change over time • For these pointers: • The OpenGL "Redbook" v1.1 HTML format • http://www.glprogramming.com/red/ • Sample Programs for the OpenGL "Redbook" v1.1 • http://www.opengl.org/resources/code/samples/redbook/
Free Books from OpenGL.org • The “Blue Book” • The OpenGL Reference Manual 4th Edition The Official Reference Document to OpenGL, Version 1.4 • Old, 2004, but useful for non-shader programming • Earlier versions available online: • Pointers change … • http://www.opengl.org/documentation/blue_book/ • For these pointers: • http://www.glprogramming.com/blue/ • http://www.cs.utk.edu/~vose/c-stuff/opengl/
OpenGL … finally • Commands and syntax • Libraries • Yet more about pipelines and software architecture
OpenGL Libraries • To call the functions that are the OpenGL API, use libraries • Will need to put on computer, and access (#include), to develop programs • OpenGL core library • #include, e.g., gl.h • OpenGL Utility Library (GLU) • Provides functionality in OpenGL core but avoids having to rewrite code • OpenGL Utility Toolkit (GLUT) • Interfaces with window system …. • GLX for X window systems • WGL for Windows • AGL for Macintosh • Provides window system independent implementation • For class, freeglut and GLEW (OpenGL extension Wrangler) • … “wrangler” … not too reassuring … • Have instructions for use with compiler on class web site
Software OrganizationFocus on GL, GLU, GLUT application program OpenGL Motif widget or similar GLUT GLX, AGLor WGL GLU GL X, Win32, Mac O/S software and/or hardware • Below shows relationship among libraries • GLU “on top of” GL, • GLUT uses GLU and GL for graphics • Controls operating and window systems primarily through GLX • Application program can call GLUT, GLU, GL
OpenGL:“Software Interface to Graphics Hardware” • OpenGL is a software interface to graphics hardware – now, at any point! • Software layer with API: • Excellent synopsis in Angel’s Appendix D • ~200 core functions - next slide • specify objects and operations – again, at any point in the pipeline • ~50 in library (auxiliary) commands – next slide • glu, glut make life simple • also, can use ogl with straight Windows (the windowing system) • … and build your own shaders, …
OpenGL:“Software Interface to Graphics Hardware” • OpenGL is a software interface to graphics hardware – at any point! • Software layer with API: • Excellent synopsis in Angel’s Appendix D • ~200 core functions - next slide • specify objects and operations – again, at any point in the pipeline • ~50 in library (auxiliary) commands – next slide • glu, glut make life simple • also, can use ogl with straight Windows (the windowing system) • … and build your own shaders, …
FYI - OpenGL:“Software Interface to Graphics Hardware” Immediate Mode • Per Vertex • Operations & • Primitive • Assembly Polynomial Evaluator DisplayList Per Fragment Operations Frame Buffer CPU Rasterization Texture Memory Pixel Operations • Another view, showing pipeline (more later)
From online Reference • http://www.glprogramming.com/blue/
GLUT – OpenGL Utility Toolkit • OpenGL Utility Toolkit (GLUT) • Provides functionality common to all window systems • Open a window • Get input from mouse and keyboard • Menus • Event-driven • Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform • E.g., no scroll bars • But, it will work for us • Lots of tutorials online • Much more next time • Will also look at qt
“Types” of OpenGL Functions • Again, OGL API implemented as functions (below after Angel): • Primitives • Points • Line Segments • Polygons • Attributes • Viewing • Transformations • Viewing • Modeling • Input (GLUT) • Control (GLUT) • Query
OpenGL: Primitives and States • Older, pre - 2.0 and 3.0 OpenGL is a “state machine” • Some “state”, e.g., color is set and remains in that state until changed • Everything drawn is in the color set • When program starts, default values are set for everything • Color, camera position and orientation, projection type, etc. • OpenGL functions are of two types: 1.Primitive generating • Can cause output if primitive is visible • How vertices are processed and appearance of primitive are controlled by the state 2. State changing • Transformation functions • E.g, viewing, projections • Attribute functions • E.g., color
Note: Lack of Object Orientation • BTW, OpenGL is not object oriented • So there are multiple functions for a given logical function • Below differ in 2d vs. 3d and type of arguments, integer vs. fp (example next slides) • glVertex3f • glVertex2i • glVertex3dv • Underlying storage mode is the same • Easy to create overloaded functions in C++, but issue is efficiency • As usual, alternative software layers exist
OpenGL Function Format function name dimensions glVertex3f(x,y,z) x,y,zare floats belongs to GL library glVertex3fv(p) pis a pointer to an array/vector • Function format: <lib><function name><ndims><type> ( <parameters> )
OGLese – Naming Conventions • E.g., glVertex3f (x, y, z) • Suffixes and types (like MS Windows API): • Suffix Data Type Typical C Type OpenGL Type • b 8-bit integer signed char GLbyte • s 16-bit integer short GLshort • i 32-bit integer long GLint, GLsizei • f 32-bit floating-point float GLfloat, GLclampf • d 64-bit floating-point double GLdouble,GLclampd • ub 8-bit unsigned integer unsigned char GLubyte, GLboolean • us 16-bit unsigned integer unsigned short GLushort • ui 32-bit unsigned integer unsigned long GLuint, GLenum • Argument expectation convention (name of function gives args): • glVertex2i(1, 3); • glVertex2f(1.0, 3.0); • Also, vector arguments common: • glColor3f(1.0, 0.0, 0.0); • float color_array[] = {1.0, 0.0, 0.0}; • glColor3fv(color_array);
OpenGL Include Files: “#defines” • Well, this is C, so lexics and semantics conflated • Most constants are defined in include files • gl.h, glu.h and glut.h • Note #include glut.h automatically includes the others • Do have a look at the *.h files, quite easy in MS compiler • Examples • glBegin(GL_POLYGON) • glClear(GL_COLOR_BUFFER_BIT) • include files also define OpenGL data types: • GLfloat, GLdouble,….
Coordinate Systems • Units in glVertex are determined by application • called object or problem or model coordinates • Viewing specifications are also in object coordinates • Size of viewing volume that determines what will appear in the image • Internally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates • OpenGL also uses some internal representations that usually not visible to application – next slide
Coordinate Systems in Viewing • Coordinate Systems in the Graphics Pipeline • OCS – object coordinate system • WCS – world coordinate system • VCS – viewing coordinate system • CCS – clipping coordinate system • NDCS - normalized device coordinate system • DCS – device coordinate system • And images are formed on the image plane
Placing & Aiming the OpenGL (synthetic) Camera • Natural to position camera in world space as if real camera 1. Identify the eye point where the camera is located 2. Identify the look-at point that we wish to appear in the center of our view 3. Identify an up-vector vector oriented upwards in our final image • Specify camera configuration with: • gluLookAt(ex, ey, ez, ax, ay, az, ux, uy, uz) • 3 camera vectors: lookFrom (ex, ey, ez), lookAt (ax, ay, az), vUp (ux, uy, uz)
OpenGL Viewing: The Camera Analogy(and, briefly, transformations to create coordinate systems) • Synthetic camera in OGL • 1.Setting up tripod and pointing camera at scene: • viewing transformation • 2.Arranging scene viewed into desired composition: • modeling transformation • 3.Choosing camera lens or adjusting zoom: • projection transformation • 4.Determining how large and what part final view: • viewport transformation
Again, Viewing – gluLookAtgluLookAt(ex, ey, ez, ax, ay, az, ux, uy, uz) • Real handy (about as useful as anything): • gluLookAt (x, y, z, //cop – where the camera is, center of projection x, y, z // any pt - where camera looking, viewpoint z, y, z); // up vector (camera twist) • Most Simply: • Center of projection: just x, y, z • Viewpoint is specified by eyex, eyey, and eyez. • “Where looking” is specified as atx, aty, and atz, • any point along line of sight • Up vector: upx, upy, and upz indicate which direction is up
Example - gluLookAt • gluLookAt (GLdouble eyex, GLdouble eyey, Gldouble eyez, //cop GLdouble atx, GLdouble aty,GLdouble atz, // pt on los GLdouble upx,GLdouble upy,GLdouble upz); // up vec. • gluLookAt (4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0, 2.0, -1.0) • Shows camera twist
BTW, OpenGL Camera Defaults • First examples do not move camera, so default/ “opening state” • Need to know, lest may not see anything! • E.g., object placed “on” camera • OpenGL places a camera at origin in world/object space pointing in negative z direction • Default view volume is a box centered at the origin with a side of length 2
Also, Orthographic Viewing is Default z=0 z=0 • Orthographic vs. Perspective Viewing • Later will examine projection on view plane for differing distances • Angel suggests thinking of orthographic view as camera at infinite/far distance w/telephoto lens • Or, no vanishing point • Or, …. • In the default orthographic view, points are projected forward along the z axis onto the plane z=0
Viewports • More later • Do not have use the entire window for the image • E.g., glViewport(x,y,w,h) • Values in pixels (window coordinates)
It’s (almost) all about Polygons(recall, modeling topics last week) • Have discussed tractability, interactivity and selection of image models • Not physical • Leads to using “good enough” (for the task) representation • Much of challenge of cg lies in representing the analog world on a digital device • E.g., approximation of circle as series of straight lines • Though some surfaces and objects can be described mathematically, e.g., sphere, most cannot, e.g., crocodile • Approximation for objects is typically polygon mesh Wireframe Ray tracing Polygons – Flat shading Polygons - Smooth shading
Polygon Representations • More is always better (for polygon count and photorealism) • Below, 20, 80, 320 polygons for sphere • Sampling vs. discrete • Tesselation • Fair amount of detail in using meshes, etc. • Usually, library routines or tools create
Polygon Mesh – More is Better • Maya (en.9jcg.com/comm_pages/blog_content-art-51.htm) • 940 and ~1m polygons • But they are still polygons!
Example: Draw cube from faces(quick look) void colorcube( ) { polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); } Vertices are ordered to obtain correct outward facing normals 5 6 2 1 7 4 0 3
Representing a Mesh(quick look) v5 • Consider a simple “mesh” • Already 8 nodes and 12 edges • 5 interior polygons • 6 interior (shared) edges • Each vertex has location vi = (xiyizi) • How to efficiently store for use is a significant data structure question • Hence, the large number of representations in OpenGL e2 v6 e3 e9 v8 e8 v4 e1 e11 e10 v7 e4 e7 e12 v1 v3 e6 e5 v2
OpenGL Primitives(quick look) • Can Specify individual vertices and polygons, as below, or a zillion other ways • In fact “it’s all about polygons”, so efficient representations needed 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(); }
Polygons and OpenGL • OpenGL will only display polygons correctly that are: • Simple: edges cannot cross – a, not b • Convex: All points on line segment between two points in a polygon are also in the polygon – not b • Flat: all vertices are in same plane • User program checks if above true • Program vs. OpenGL checks for efficiency • OpenGL will produce output if these conditions are violated, • … but it may not be what is desired • All triangles satisfy all conditions … used extensively
Attributes • Attributes are part of the OpenGL state and determine the appearance of objects • Color (points, lines, polygons) • Size and width (points, lines) • Stipple pattern (lines, polygons) • Polygon mode • Display as filled: solid color or stipple pattern • Display edges • Display vertices