TOPICS in IT: ADVANCED GRAPHICS (CS & SE 233.420) Lecture 2 5 Aug 2004 1
Topics Image Formation Electromagnetic spectrum Light Human Visual Image processing Color Programming in OpenGL Background Programming Structure and naming conventions 2
Electromagnetic Spectrum The surface of Earth bathed in electromagnetic radiation from sun Electromagnetic energy is created by a vibration that produces wave that carry the energy Waves are ordered in the electromagnetic spectrum according to their wave length (λ) Longest (radio) Shortest (gamma) Frequency (f) refers to the number of waves a vibration creates during a period of time Wavelength and frequency are inversely related f = 1/ λ 3
Electromagnetic Spectrum Wavelength vs Frequency Increasing in Wavelength (λ) Increasing in Frequency (f) 4
(Human) Visible Light Spectrum 5
Light Detection Advantages Travels rapidly (300,000 km/sec) and in straight lines Surfaces reflect and absorb various wavelengths of light depending on physical and chemical properties Light therefore contains a great deal of information about environment Useful for many survival needs Allows detection of predators, prey at a distance without direct physical contact 6
Photosensitivity Ability to detect different intensities of diffuse illumination Present in many plants and most animals, Found in single celled animals, skin of many simple organisms, and in specialized visual organs, Light sensitivity underlies daily rhythm of activities 7
Vision Sensing changes in illumination that are rapid in time and localized in space Image forming eyes found in annelids (segmented worms) mollusks (snails, scallops, squid etc.) arthropods (spiders, crabs, insects etc.) and vertebrates 8
Visual Acuity and Sensitivity Acuity Expressed as angular resolving power Humans can resolve 1' (one minute) of arc, full moon 30' arc diameter, Some birds of prey, such as eagles can resolve 20" of arc Sensitivity how an eye responds to light In complete darkness, human eye can respond to single photon of light, stimulation of only 10-15 cells by single photons in a small patch of retina is sufficient to produce a conscious percept in a human observer 9
Human (Vertebrate) Vision Retina The photosensitive part of the eye Specialized layer of photoreceptor cells and neurons at back of eye Image formed on retina by refraction of light by cornea and lens Efferent control systems Ciliary muscles stretch lens to alter focus setting Iris controls amount of light admitted to eye (f-stop) 10
Rods and Cones The retina is largely composed of two types of cells, called rods and cones Rods Specialised for night vision can be activated by a single photon but they produce lower acuity vision, however. Cones Specialised for day vision, are concentrated at the fovea and provide high acuity vision Confer color vision to primates, birds, reptiles, and most fish 11
Human Color Wavelength Humans have 3 types of cone pigment that are maximally sensitive to either red, green or blue Color perception results from the simultaneous stimulation of the 3 cone types Colorblindness results from a deficiency of one cone type Sensitivities 12
Additive and Subtractive Color Additive color Form a color by adding amounts of three primaries Used by CRTs, projection systems, positive film Primaries are Red (R), Green (G), Blue (B) Subtractive color Form a color by filtering white light with cyan (C), Magenta (M), and Yellow (Y) filters Light-material interactions Used in Printing and Negative film 13
Color Matching In order to define the perceptual 3D space in a "standard" way A set of experiments can (and have been) carried by having observers try and match color of a given wavelength, lambda, Done by mixing three other pure wavelengths, such as R=700nm, G=546nm, and B=436nm A problem exists, because sometimes the red light needs to be added to the target before a match can be achieved. This is shown on the graph by having its intensity, R, take on a negative value. 14
Commission Internationale Defined three new hypothetical light sources, x, y, and z, which yield positive matching curves Given a spectrum and wish to find the corresponding X, Y, and Z quantities Done by integrating the product of the spectral power and each of the three matching curves over all wavelengths. The weights X,Y,Z form the three-dimensional CIE XYZ space d'eclairage (CIE) 15
CIE Chromaticity Diagram Defines 3 color index in 2 dimensions done by projecting the 3D color space onto the plane X+Y+Z=1 16
Color Gamuts The chromaticity diagram can be used to compare the "gamuts" of various possible output devices Such as monitors and printers Note that a color printer cannot reproduce all the colors visible on a color monitor 17
RGB Color Cube The additive color model used for computer graphics is represented by the RGB color cube, R, G, and B represent the colors produced by red, green and blue phosphours, respectively. 18
CIE XYZ Color Space 19
CMYK color model Used by printers Green paper is green because it reflects green and absorbs other wavelengths To produce blue, one would mix cyan and magenta inks both reflect blue while each absorbing one of green and red Black ink is used to ensure that a high quality black can always be printed, and is often referred to as to K dye color absorbs reflects Cyan Red Blue and Green Magenta Green Blue and Red Yellow Blue Red and Green Black (K) All None 20
Other Color Models HSV Hue, Saturation, Value HLS Hue, Luminosity, Saturation Color conversion formula exist to convert colors between respective color models 21
Programming with OpenGL Background Programming Structure and naming conventions Primitives Attributes and States Programming in three dimensions Inputs and Interaction Working with Callbacks 22
SGI and GL Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) To access the system, application programmers used a library called IRIS GL With GL, it was relatively simple to program three dimensional interactive applications 23
OpenGL The success of GL lead to OpenGL (1992) a platform-independent API that was Easy to use Close enough to the hardware to get excellent performance Available in a number of languages OS-independent and hardware-independent Focus on rendering Aimed at 2-D/3-D scenes made of polygons (and lines and points). Omitted windowing and input to avoid window system dependencies Not as good for 2-D windows/text/gui-style graphics 24
OpenGL Evolution Controlled by an Architectural Review Board (ARB) Members include SGI, Microsoft, NVidia, HP, 3DLabs, IBM, Apple, Dell, Intel, Matrox, Sun, HP,. Relatively stable Evolution reflects new hardware capabilities 3D texture mapping and texture objects Vertex programs Allows for platform specific features through extensions 25
OpenGL Design The interface with the graphics hardware. Designed for efficient implementation in hardware. Particular OpenGL implementations may be partially or totally software. We deal with OpenGL via function calls (or commands). No global variables. Most OpenGL functions have few parameters. C/C++ header: <GL/gl.h> But you make lots of function calls. No complex data types. OpenGL is function-call intensive. Think: advantages/disadvantages. 26
Software Organization Application Program OpenGL Motif widget or similar GLUT GLX, AGL, or WGL X, Windows, Apple O/S GLU GL Software and/or Hardware 27
OpenGL is Procedural OpenGL is a procedural rather than descriptive interface. In order to get a rendering of a red sphere the programmer must specify the appropriate sequence of commands Set up the camera view and modelling transformations Draw the geometry for a sphere with a red color. etc. Disadvantage of using a procedural interface The application must specify all of the operations in exacting detail and in the correct sequence to get the desired result. Advantage of this approach It allows great flexibility in the process of generating the image Application is free to trade-off rendering speed and image quality by changing the steps through which the image is drawn. 28
OpenGL is Not Pixel Exact This means that two different OpenGL implementations are very unlikely to render exactly the same image. This allows OpenGL to be implemented across a range of hardware platforms. If the specification were too exact, it would limit the kinds of hardware acceleration that could be used; limiting its usefulness as a standard. In practice, the lack of exactness need not be a burden -- unless you plan to build a rendering farm from a diverse set of machines. 29
OpenGL State OpenGL is a state machine OpenGL functions are of two types Primitive generating (drawing) Can cause output if primitive is visible How vertices are processed and appearance of primitive are controlled by the state Set and return state values Transformation functions Attribute functions So: all drawn objects are composed of primitives. The properties of these are attributes, which are determined by OpenGL states. 30
OpenGL Libraries OpenGL Core Library OpenGL32 on Windows GL on most unix/linux systems OpenGL Utility Library (GLU) Provides functionality in OpenGL core but avoids having to rewrite code Additional functions & types for various graphics operations. Designed to be implemented in software; calls GL. C/C++ header: <GL/glu.h>. Links with window system GLX or X window systems WGL or Windows AGL for Macintosh 31
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 32
GLUT Functions glutinit allows application to get command line arguments and initializes system gluinitdisplaymode requests properties for the window (the rendering context) RGB color Single buffering Properties logically ORed together glutwindowsize in pixels glutwindowposition from top-left corner of display glutcreatewindow create window glutdisplayfunc display callback glutmainloop enter infinite event loop 33
OpenGL Extensions Functionality that anyone can add to OpenGL. OpenGL specifies rules that extensions are to follow. May be system-dependent. We will not use any extensions. 34
OpenGL Naming Conventions Functions Begin with gl, words capitalized & run together Example: glclearcolor Can include type information. For example, the 2d in glvertex2d indicates two parameters of type GLdouble. Constants Begin with GL, all upper-case, _ between words Example: GL_TRIANGLE_STRIP Types Begin with GL, next word not capitalized, all words run together Example: GLdouble 35
Related Package Naming Conventions Related packages use similar conventions. GLU Function: gluscaleimage Constant: GLU_TESS_ERROR Type: GLUtesselatorObj GLUT Function: glutinitdisplaymode Constant: GLUT_MIDDLE_BUTTON 36
OpenGL Types OpenGL defines its own types, which have the same (minimum) precision on all systems. Some of these: GLint: at least 32-bit integer GLfloat: at least 32-bit floating-point GLdouble: at least 64-bit floating-point and others So, for example, GLdouble is probably the same as double, but may not be. Converting (say) a GLdouble to a double is fine. But be careful when tossing around GLdouble * and double *. (Why?) 37
Lack of Object Orientation OpenGL is not object oriented so that there are multiple functions for a given logical function As a result, some OpenGL commands have several forms allowing for different types. For example, glvertex* can take 2, 3, or 4 parameters of many different types. Function glvertex2d takes 2 parameters of type GLdouble. Function glvertex3f takes 3 parameters of type GLfloat. Function glvertex3fv ( v for vector ) takes a single parameter of type GLfloat * (should be a pointer to an array of 3 GLfloat s). Underlying storage mode is the same 38
OpenGL Function Format belongs to GL library glvertex3f(x,y,z) 3 float inputs are required function glvertex3fv(p) p is a pointer to a 3 Dimensional array of floats 39
OpenGL #defines Most constants are defined in the include files gl.h, glu.h and glut.h #include <glut.h> should automatically include the others Examples glbegin(gl_polygon) glclear(gl_color_buffer_bit) include files also define OpenGL datatypes: GLfloat, GLdouble,. 40
glflush() It is important to note that OpenGL commands are not necessarily executed as soon as they are issued. It is necessary to call the command glflush() to ensure that all previously issued commands are executed. glflush() is generally called at the end of a sequence of drawing commands to ensure all objects in the scene are drawn. 41
Program Structure Most OpenGL programs have a similar structure that consists of the following functions main(): defines the callback functions opens one or more windows with the required properties enters event loop (last executable statement) init(): sets the state variables Viewing Attributes callbacks Display function Input and window functions 42
Event Loop 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 43
References The Electro Magnetic Spectrum Tutorial, http://www.emk.gazi.edu.tr/ems.htm What is RF Radiation?, http://www.rfsafe.com/research/rf_radiation/what_is_rf/emf_spectrum.ht m The Physics Classroom, The Electromagnetic and Visible Spectra, http://www.glenbrook.k12.il.us/gbssci/phys/class/light/u12l2a.html Light Sources, http://graphics.lcs.mit.edu/~mcmillan/comp136/lecture23/lights.html The Visual System, http://www.geocities.com/medinotes/visual_system.htm The Visual System I & II, Dr. Paul Patton, http://soma.npa.uiuc.edu/courses/bio303. Computer Graphics Color Models, CSC 418: Colour Representation, Michiel van de Panne, University of Toronto, http://www.dgp.toronto.edu/people/van/courses/csc418/colour.html 44
References http://www.eecs.tulane.edu/ www/terry/opengl/changing_state.html www.inf.pucrs.br/~flash/tcg/aulas/oglfaq/viewing.htm - www.cs.uaf.edu/~cs381/slides/20030910ogl.ppt http://www.dgp.toronto.edu/~ah/csc418/fall_2001/tut/ogl_draw.html http://isg.cs.tcd.ie/dingliaj/3d4/lab2_p2.html http://www.opengl.org/about/overview.html http://www.winnetmag.com/article/articleid/3581/3581.html http://www.csee.umbc.edu/~ebert/691/au00/notes/c1_viewing.html www.inf.ufrgs.br/~comba/gh-files/talk1.pdf http://www.dgp.toronto.edu/~hertzman/courses/csc418/winter_2004/not es/lecture/pipeline.html Ed Angel Lecture notes (Lectures 4, 5, 6, 7, 8 and 9) Open GL web page www.opengl.org 45