Computer graphic -- Programming with OpenGL 2
OpenGL OpenGL (Open Graphics Library) a cross-language, multi-platform API for rendering 2D and 3D computer graphics. The API is typically used to interact with a GPU, to achieve hardware-accelerated rendering. OpenGL was developed by Silicon Graphics Inc. in 1992 and is widely used in CAD, virtual reality, scientific visualization, information visualization, flight simulation, and video games. OpenGL is managed by the non-profit technology consortium Khronos Group. 2
Documentation OpenGL's popularity is partially due to the quality of its official documentation. The OpenGL ARB released a series of manuals along with the specification which have been updated to track changes in the API. These are almost universally known by the colors of their covers: The Red Book OpenGL Programming Guide, 7th edition. ISBN 0-321-55262-8 A readable tutorial and reference book this is a 'must have' book for OpenGL programmers. The Green Book OpenGL Programming for the X Window System. ISBN 0-201-48359-9 A book about X11 interfacing and GLUT. The Blue Book OpenGL Reference manual, 4th edition. ISBN 0-321-17383-X Essentially a hard-copy printout of the man pages for OpenGL. Includes a poster-sized fold-out diagram showing the structure of an idealised OpenGL implementation. The Alpha Book (white cover) OpenGL Programming for Windows 95 and Windows NT. ISBN 0-201-40709-4 A book about interfacing OpenGL with Microsoft Windows. Then, for OpenGL 2.0 and beyond: The Orange Book OpenGL Shading Language, 3rd edition. ISBN 0-321-63763-1 A readable tutorial and reference book for GLSL. 3
History http://en.wikipedia.org/wiki/opengl 4
OpenGL Webpage http://www.opengl.org/ Wiki http://www.opengl.org/wiki/main_page Function http://www.opengl.org/wiki/category:core_ap I_Reference 5
Basic GL Operation (x,y,z) (RGB) Pixel (x,y,z, RGB) 6
24-bit true color 8 0 1 0 0 1 0 1 1 Bitplane registers 0 1 0 0 1 0 1 1 8 bit DAC Color Guns Blue 75 pixel 8 1 0 1 0 1 1 0 0 Green 172 8 bit DAC 8 DAC: digital-to-analog converter 0 0 0 0 1 0 1 0 8 bit DAC Red 10 CRT Raster Frame Buffer 7
Basic GL Operation Commands enter the GL on the left. Some commands specify geometric objects to be drawn while others control how the objects are handled by the various stages. Commands are effectively sent through a processing pipeline. Pixel (x,y,z, RGB) 8
Basic GL Operation Geometric Shading The first stage operates on geometric primitives described by vertices: points, line segments, and polygons. In this stage vertices may be transformed and lit, followed by assembly into geometric primitives, which may optionally be used by the next stage, i.e., geometry shading, to generate new primitives. 9
Basic GL Operation primitive assembly The final resulting primitives are clipped to a clip volume in preparation for the next stage, rasterization. 10
Basic GL Operation Rasterization The rasterizer produces a series of framebuffer addresses and values using a two dimensional description of a point, line segment, or polygon. 11
Basic GL Operation Fragment Each fragment is fed to the next stage that performs operations on individual fragments before they finally alter the framebuffer. These operations include conditional updates into the framebuffer based on incoming and previously stored depth values (to effect depth buffering), blending of incoming fragment colors with stored colors, as well as masking and other logical operations on fragment values. 12
Basic GL Operation Framebuffer Values may also be read back from the framebuffer or copied from one portion of the framebuffer to another. These transfers may include some type of decoding or encoding. This ordering is meant only as a tool for describing the GL, not as a strict rule of how the GL is implemented, and we present it only as a means to organize the various operations of the GL. 13
14
Compute shader A compute shader is a programmable shader stage that expands Microsoft Direct3D 11 beyond graphics programming. The compute shader technology is also known as the DirectCompute technology. Like other programmable shaders (vertex and geometry shaders for example), a compute shader is designed and implemented with HLSL but that is just about where the similarity ends. provide high-speed general purpose computing and takes advantage of the large numbers of parallel processors on the graphics processing unit (GPU). provide memory sharing and thread synchronization features to allow more effective parallel programming methods. 15
OpenGL 4.3 OpenGL 4.3 Core Profile Specification http://www.opengl.org/registry/doc/glspec43.c ore.20120806.pdf 16
OpenGL Shading The OpenGL Shading Language is actually several closely related languages. These languages are used to create shaders for each of the programmable processors contained in the OpenGL processing pipeline. Vertex tessellation control tessellation evaluation geometry fragment compute processors Document http://www.opengl.org/documentation/glsl/ 17
Vertex Processor The vertex processor is a programmable unit that operates on incoming vertices and their associated data. vertex shaders Compilation units written in the OpenGL Shading Language run on this processor. vertex shader executable When a set of vertex shaders are successfully compiled and linked, they result in a vertex shader executable that runs on the vertex processor. 18
Tessellation Control Processor The tessellation control processor is a programmable unit that operates on a patch of incoming vertices and their associated data, emitting a new output patch. tessellation control shaders Compilation units written in the OpenGL Shading Language to run on this processor are called tessellation control shaders. tessellation control shader executable When a set of tessellation control shaders are successfully compiled and linked, they result in a tessellation control shader executable that runs on the tessellation control processor. 19
Tessellation Evaluation Processor The tessellation evaluation processor is a programmable unit that evaluates the position and other attributes of a vertex generated by the tessellation primitive generator, using a patch of incoming vertices and their associated data. tessellation evaluation shaders Compilation units written in the OpenGL Shading Language to run on this processor are called tessellation evaluation shaders. tessellation evaluation shader executable When a set of tessellation evaluation shaders are successfully compiled and linked, they result in a tessellation evaluation shader executable that runs on the tessellation evaluation processor. 20
Geometry Processor The geometry processor is a programmable unit that operates on data for incoming vertices for a primitive assembled after vertex processing and outputs a sequence of vertices forming output primitives. geometry shaders Compilation units written in the OpenGL Shading Language to run on this processor are called geometry shaders. geometry shader executable When a set of geometry shaders are successfully compiled and linked, they result in a geometry shader executable that runs on the geometry processor. 21
Fragment Processor The fragment processor is a programmable unit that operates on fragment values and their associated data. fragment shaders Compilation units written in the OpenGL Shading Language to run on this processor are called fragment shaders. fragment shader executable When a set of fragment shaders are successfully compiled and linked, they result in a fragment shader executable that runs on the fragment processor. 22
Compute Processor The compute processor is a programmable unit that operates independently from the other shader processors. compute shaders Compilation units written in the OpenGL Shading Language to run on this processor are called compute shaders. compute shader executable When a set of compute shaders are successfully compiled and linked, they result in a compute shader executable that runs on the compute processor. provide high-speed general purpose computing and takes advantage of the large numbers of parallel processors on the graphics processing unit (GPU). 23
24
Demo E:\course-CG\2011\froblins.mov 25
Basics of GLUT 26
Basics of GLUT Why GLUT? a popular library for OpenGL programmers, because it standardizes and simplifies window and event management. ported atop a variety of OpenGL implementations, including both the X Window System and Microsoft Windows NT. 27
Initializing and Creating a Window Before open a window, you must specify its characteristics: single-buffered or double-buffered store colors as RGBA values or color indices Where should it appear on your display? Answers: glutinit(), glutinitdisplaymode(), glutinitwindowsize(), glutinitwindowposition(), glutcreatewindow(). 28
glutinit() void glutinit (int argc, char **argv); glutinit() should be called before any other GLUT routine, because it initializes the GLUT library. 29
glutinitdisplaymode() void glutinitdisplaymode(unsigned int mode); Specify a display mode RGBA or color-index (GLUT_RGBA or GLUT_INDEX) single- or double-buffered (GLUT_SINGLE or GLUT_DOUBLE) specify that the window have an associated depth, stencil, and/or accumulation buffer. GLUT_DEPTH, GLUT_STENCIL, or GLUT_ACCUM. 30
glutinitwindowsize() void glutinitwindowsize(int width, int height); height width 31
glutinitwindowposition() void glutinitwindowposition(int x, int y); Requests windows to have an initial size and position. The arguments (x, y) indicate the location of a corner of the window 32
glutcreatewindow() int glutcreatewindow(char *name); Opens a window with characteristics display mode, width, height The string name appear in title bar. 33
glutdisplayfunc() void glutdisplayfunc(void (*func)(void)) redrawn the contents of the window. initially opened pop and damage 34
glutreshapefunc() void glutreshapefunc (void (*func)(int width, int height)); resizedor moved the window. Func: a pointer to a function that expects two arguments, the new width and height of a window. 35
glutkeyboardfunc() void glutkeyboardfunc(void (*func)(unsigned int key, int x, int y); Call the function, func, when a key that generates an ASCII character is pressed. The key callback parameter is the generated ASCII value. The x and y callback parameters indicate the location of the mouse (in window-relative coordinates) when the key was pressed. 36
glutmousefunc() void glutmousefunc(void (*func)(int button, int state, int x, int y)); Call the function, func, when a mouse button is pressed or released. The button callback parameter is one of GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. The state callback parameter is either GLUT_UP or GLUT_DOWN, depending upon whether the mouse has been released or pressed. The x and y callback parameters indicate the location (in windowrelative coordinates) of the mouse when the event occurred. 37
glutmotionfunc() void glutmotionfunc(void (*func)(int x, int y)); Call the function, func, when the mouse pointer moves within the window while one or more mouse buttons is pressed. The x and y callback parameters indicate the location (in window-relative coordinates) of the mouse when the event occurred. 38
glutpostredisplay() void glutpostredisplay(void); Marksthe current window as needing to be redrawn. 39
glutsetcolor() void glutsetcolor(glint index, GLfloat red, GLfloat green, GLfloat blue); Load the index in the color map Index: red, green, and blue. normalized to [0.0,1.0]. 40
glutidlefunc() void glutidlefunc(void (*func)(void)); Call the function, func, if no other events are pending. For example, when the event loop would otherwise be idle. This is particularly useful for continuous animation or other background processing. If NULL (zero) is passed in, execution of func is disabled. 41
glutmainloop() void glutmainloop(void); After all the setup is completed, GLUT programs enter an event processing loop, never to return. Registered callback functions will be called when the corresponding events instigate them. 42
Transformations and Timers 43
Transformations and Timers Transformations: View a geometric model in any orientation in 3D space gltranslatef(0.0f, 0.0f, -5.0f); //Move forward 5 units glrotatef (30.0f, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis glpushmatrix(); //Save the transformations performed thus far glpopmatrix(); //Undo the move to the center of the trapezoid glmatrixmode(gl_modelview); //Switch to the drawing perspective gluttimerfunc(25, update, 0); //Add a timer Model Rotation Model Scaling 44
Color 45
Color glcolor3f(0.5f, 0.0f, 0.8f); Green (R G B) 1 Yellow Black Cyan Blue 1 0 1 Megenta (0.5f, 0.0f, 0.8f) White Red Blue Color map Color cube Green Red Color palette 46
Lighting Lighting is 5% of light setup and 95% of revisions and adjustments. Jeremy Birn Digital Lighting and Rendering 47
Imaging function I=f(ρ, n, s) I: radiosity (intensity) ρ: albedo N: unit normal S: source vector Lighting magnitude proportional to intensity of the source Lambertian model I=ρ n s N ρ S 48
Lighting Light source Diffuse Light Ambient light Specular Light N ρ S 49
Lighting- source Diffuse Light: shines upon an object indirectly. affected by the distance and angle between the surface and the light, unaffected by the viewers position or view angle. Add spotlight by OpenGL GLfloat lightcolor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5) GLfloat lightpos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8) gllightfv(gl_light0, GL_DIFFUSE, lightcolor0); gllightfv(gl_light0, GL_POSITION, lightpos0); 50
Lighting- source Ambient light: light that is considered to be everywhere. no source, Numerous large light sources are generally positioned in such a way Add ambient light by OpenGL GLfloat ambientcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; gllightmodelfv(gl_light_model_ambient, ambientcolor); Diffuse Light Diffuse and Ambient Lighting 51
Lighting- source Specular Light: refered to as Specular Highlight, it highlights an object with a reflective color. Add specular light by OpenGL GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0}; gllightfv(gl_light0, GL_SPECULAR, specular0); Diffuse Light Diffuse and Ambient Lighting Diffuse, Ambient and Specular Lighting 52
Lighting- Attenuation Attenuation with distance generates a softer and more realistic image: d is the distance computed by OpenGL a = GL_CONSTANT_ATTENUATION (default 1.0) b = GL_LINEAR_ATTENUATION (default 0.0) c = GL_QUADRATIC_ATTENUATION (default 0.0) Default is no attenuation: a=1, b=0, c=0 53
Lighting-Function specify light source parameters 54
Lighting-Source example specify light source parameters I = I amb +I diff +I spec = I a K a + I d K d N L + I s K s (R V) n GLfloat position0[] = {1.0, 1.0, 1.0, 0.0}; GLfloat diffuse0[] = {1.0, 0.0, 0.0, 1.0}; // I d term - Red GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0}; // I s term - White GLfloat ambient0[] = {0.1, 0.1, 0.1, 1.0}; // I a term - Gray glenable(gl_lighting); glenable(gl_light0); gllightfv(gl_light0, GL_POSITION, position0); gllightfv(gl_light0, GL_DIFFUSE, diffuse0); gllightfv(gl_light0, GL_SPECULAR, specular0); gllightfv(gl_light0, GL_AMBIENT, ambient0); 55
Lighting- Normal Normal : A face's normal is a vector that is perpendicular to the face. glnormal3f(1.0f, 0.0f, 0.0f); tell OpenGL the "normals" of the different shapes in our scene. OpenGL Programming Guide or The Red Book : http://unreal.srk.fer.hr/theredbook/ Appendix E : Calculating Normal Vectors N ρ S 56
Lighting - Material Once lighting is enabled, colors are no longer used N ρ S 57
Lighting - Material Main functions for OpenGL: 58
Lighting-source example specify a material I = I amb +I diff +I spec = I a K a + I d K d N L + I s K s (R V) n param: GL_AMBIENT // K a term GL_DIFFUSE // K d term GL_SPECULAR // K s term GL_SHININESS // exponent n GL_AMBIENT_AND_DIFFUSE // K a = K d GL_EMISSION // No light calculations // simulates sources 59
Lighting - Material 60
Demo SIGGRAPH 2012 : Emerging Technologies http://www.youtube.com/watch?v=bgndaw2aghi More about Siggraph2012 http://www.youtube.com/user/acmsiggraph 61
Reference Course Resources OpenGL on Sigraph2010 http://www.opengl-redbook.com/s2010 http://s2012.siggraph.org/ OpenGL Video Tutorial: http://www.videotutorialsrock.com/opengl_tutorial/what _is_opengl/text.php Nehe http://nehe.gamedev.net/ OpenGl with Java http://jerome.jouvie.free.fr/opengl/index.php 62
On-Line Resources http://www.opengl.org start here; up to date specification and lots of sample code online man pages for all OpenGL functions http://www.mesa3d.org/ Brian Paul s Mesa 3D http://www.cs.utah.edu/~narobins/ope ngl.html very special thanks to Nate Robins for the OpenGL Tutors source code for tutors available here! 63
Books OpenGL Programming Guide, 7 th Edition The OpenGL Shading Language, 3 rd Edition Interactive Computer Graphics: A top-down approach with OpenGL, 5 th Edition OpenGL Programming for the X Window System OpenGL: A Primer 3 rd Edition OpenGL Distilled OpenGL Programming on Mac OS X 64
The end of this lecture! 65