cs & se 233.420 - advanced graphics lecture 2 · 7 lecture 2 photosensitivity • ability to...
TRANSCRIPT
CS & SE 233.420 - ADVANCED GRAPHICS
1
LECTURE 2
TOPICS in IT: ADVANCED GRAPHICS
(CS & SE 233.420)
Lecture 25 Aug 2004
CS & SE 233.420 - ADVANCED GRAPHICS
2
LECTURE 2
Topics
• Image Formation – Electromagnetic spectrum– Light – Human Visual Image processing– Color
• Programming in OpenGL– Background– Programming Structure and naming
conventions
CS & SE 233.420 - ADVANCED GRAPHICS
3
LECTURE 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/ λ
CS & SE 233.420 - ADVANCED GRAPHICS
4
LECTURE 2
Electromagnetic SpectrumWavelength vs Frequency
Increasing in Wavelength (λ)
Increasing in Frequency (f)
CS & SE 233.420 - ADVANCED GRAPHICS
5
LECTURE 2
(Human) Visible Light Spectrum
CS & SE 233.420 - ADVANCED GRAPHICS
6
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
7
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
8
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
9
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
10
LECTURE 2
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)
CS & SE 233.420 - ADVANCED GRAPHICS
11
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
12
LECTURE 2
Human Color Wavelength Sensitivities
• 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
CS & SE 233.420 - ADVANCED GRAPHICS
13
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
14
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
15
LECTURE 2
Commission Internationaled'Eclairage (CIE)
• 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
CS & SE 233.420 - ADVANCED GRAPHICS
16
LECTURE 2
CIE Chromaticity Diagram
• Defines 3 color index in 2 dimensions
• done by projecting the 3D color space onto the plane X+Y+Z=1
CS & SE 233.420 - ADVANCED GRAPHICS
17
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
18
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
19
LECTURE 2
CIE XYZ Color Space
CS & SE 233.420 - ADVANCED GRAPHICS
20
LECTURE 2
CMYK color model
dyecolor
absorbs reflects
Cyan Red Blue and Green
Magenta Green Blue and Red
Yellow Blue Red and Green
Black (K) All None
• 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
CS & SE 233.420 - ADVANCED GRAPHICS
21
LECTURE 2
Other Color Models
• HSV – Hue, Saturation, Value• HLS – Hue, Luminosity, Saturation• Color conversion formula exist to convert
colors between respective color models
CS & SE 233.420 - ADVANCED GRAPHICS
22
LECTURE 2
Programming with OpenGL
• Background• Programming Structure and naming
conventions• Primitives• Attributes and States• Programming in three dimensions• Inputs and Interaction• Working with Callbacks
CS & SE 233.420 - ADVANCED GRAPHICS
23
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
24
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
25
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
26
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
27
LECTURE 2
X, Windows, Apple O/S
Software OrganizationApplication Program
GL
GLX, AGL, or WGL
OpenGL Motif
widget or similar
GLU
GLUT
Software and/or Hardware
CS & SE 233.420 - ADVANCED GRAPHICS
28
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
29
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
30
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
31
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
32
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
33
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
34
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
35
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
36
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
37
LECTURE 2
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?)
CS & SE 233.420 - ADVANCED GRAPHICS
38
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
39
LECTURE 2
OpenGL Function Format
3 float inputs are requiredbelongs to GL library
glVertex3f(x,y,z)function
glVertex3fv(p)
p is a pointer to a 3 Dimensional arrayof floats
CS & SE 233.420 - ADVANCED GRAPHICS
40
LECTURE 2
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,….
CS & SE 233.420 - ADVANCED GRAPHICS
41
LECTURE 2
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.
CS & SE 233.420 - ADVANCED GRAPHICS
42
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
43
LECTURE 2
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
CS & SE 233.420 - ADVANCED GRAPHICS
44
LECTURE 2
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.htm
• 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
CS & SE 233.420 - ADVANCED GRAPHICS
45
LECTURE 2
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