81
Graphics, OpenGL, GLUT, GLUI, CUDA OpenCL OpenCV and more! CUDA, OpenCL, OpenCV , and more! CS635 Spring 2010 Daniel G Aliaga Daniel G. Aliaga Department of Computer Science Purdue University

Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Embed Size (px)

Citation preview

Page 1: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Graphics, OpenGL, GLUT, GLUI, CUDA OpenCL OpenCV and more!CUDA, OpenCL, OpenCV, and more!

CS635 Spring 2010

Daniel G AliagaDaniel G. AliagaDepartment of Computer Science

Purdue University

Page 2: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics IComputer Graphics I

• History and applications• Computer Graphics Pipeline• Linear Algebra Review

– Vectors, points– Matrices, transformations

• Representations– Points, lines, polygons, objects, meshes– Textures and images

• Lighting and Shading– Flat, Gouraud, Phong

S d d t i• Some advanced topics– Global illumination– Ray tracing

Antialiasing– Antialiasing(Some slides courtesy of Thomas

Funkhouser and Marcus Magnor)

Page 3: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics IIComputer Graphics II

• OpenGL– Motivation– Graphics context/state– Basic program outline

R d i i i i i• Rendering geometric primitives– Points, lines, polygons

• Lighting and ShadingFl t G d Ph– Flat, Gouraud, Phong

• Texturing Polygons• GLUT

GLUI• GLUI• CUDA and OpenCL• OpenCV

Page 4: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

HistoryHistory• 1950: MIT Whirlwind (CRT) • 1970: Xerox GUI• 1955: Sage, Radar with CRT and 

light pen• 1958: Willy Higinbotham “Tennis”• 1960: MIT “Spacewar” on DEC‐

• 1971: Gouraud shading• 1974: Z‐buffer• 1975: Phong Model• 1979: Eurographics1960: MIT  Spacewar  on DEC

PDP‐1• 1963: Ivan Sutherland’s 

“Sketchpad” (CAD)k b

• 1979: Eurographics • 1981: Apollo Workstation, PC• 1982: Whitted: Ray tracing• 1982: SGI• 1968: Tektronix storage tube

• 1968: Evans & Sutherland’s flight simulators

• 1968: Douglas Engelbart:

1982: SGI • 1984: X Window System• 1984: 1st SGI Workstation• ‐>1995: SGI dominance1968: Douglas Engelbart: 

computer mouse• 1969: ACM SIGGRAPH

• ‐>2003: PC dominance• Today: programmable graphics 

hardware (again)

Page 5: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

ApplicationsApplications

• TrainingTraining

• Education

C id d d i (C )• Computer‐aided design (CAD)

• Scientific Visualization

• E‐commerce

• Computer artComputer art

• Entertainment

Page 6: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Ivan Sutherland (1963) ‐ SKETCHPAD( )

• pop‐up menuspop up menus

• constraint‐based drawing

hi hi l d li• hierarchical modeling

Page 7: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Display hardwareDisplay hardware• vector displays

1963 difi d ill– 1963 – modified oscilloscope– 1974 – Evans and Sutherland Picture System

• raster displays1975 Evans and Sutherland frame buffer– 1975 – Evans and Sutherland frame buffer

– 1980s – cheap frame buffers → bit‐mapped personal computers– 1990s – liquid‐crystal displays → laptops– 2000s – micro‐mirror projectors→ digital cinema2000s  micro mirror projectors → digital cinema– 2010s – high dynamic range displays?

• other– stereo, head‐mounted displays, p y– autostereoscopic displays

Page 8: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Input hardwareInput hardware

• 2D2D– light pen, tablet, mouse, joystick, track ball, touch panel etcpanel, etc.

– 1970s & 80s ‐ CCD analog image sensor + frame grabbergrabber

Page 9: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Input hardwareInput hardware

• 2D2D– light pen, tablet, mouse, joystick, track ball, touch panel etcpanel, etc.

– 1970s & 80s ‐ CCD analog image sensor + frame grabbergrabber

Page 10: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Input hardwareInput hardware

• 2D2D– light pen, tablet, mouse, joystick, track ball, touch panel etcpanel, etc.

– 1970s & 80s ‐ CCD analog image sensor + frame grabbergrabber

– 1990s & 2000’s ‐ CMOS digital sensor + in‐camera processingp g

Page 11: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

High Dynamic Range ImagingHigh Dynamic Range Imaging

• negative film  =  130:1  (7 stops)

• paper prints    =  46:1

• combine multiple exposures =  250,000:1  (18 stops)

[Debevec97]

[Nayar00]

Page 12: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Input hardwareInput hardware

• 2D– light pen, tablet, mouse, joystick, track ball, touch panel, etc.– 1970s & 80s ‐ CCD analog image sensor + frame grabber– 1990s & 2000’s ‐ CMOS digital sensor + in‐camera processingg p g

→ high‐dynamic range (HDR) imaging• 3D

– 1980s ‐ 3D trackers– 1990s ‐ active rangefinders

• 4D and higher– multiple camerasmultiple cameras– multi‐arm gantries

Page 13: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

RenderingRendering

• 1960s ‐ the visibility problem1960s  the visibility problem– Roberts (1963), Appel (1967) ‐ hidden‐line algorithmsalgorithms

– Warnock (1969), Watkins (1970) ‐ hidden‐surface algorithmsalgorithms

– Sutherland (1974) ‐ visibility = sorting

Page 14: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

• 1960s ‐ the visibility problemy p– Roberts (1963), Appel (1967) ‐ hidden‐line algorithms– Warnock (1969), Watkins (1970) ‐ hidden‐surface algorithmsalgorithms

– Sutherland (1974) ‐ visibility = sorting

• 1970s ‐ raster graphics– Gouraud (1971) ‐ diffuse lightingPhong (1974) specular lighting– Phong (1974) ‐ specular lighting

– Blinn (1974) ‐ curved surfaces, texture– Crow (1977) ‐ anti‐aliasing

Page 15: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

• 1960s ‐ the visibility problem– Roberts (1963), Appel (1967) ‐ hidden‐line algorithms– Warnock (1969), Watkins (1970) ‐ hidden‐surface algorithmsg

– Sutherland (1974) ‐ visibility = sorting

• 1970s ‐ raster graphics1970s  raster graphics– Gouraud (1971) ‐ diffuse lighting– Phong (1974) ‐ specular lighting

li ( ) d f– Blinn (1974) ‐ curved surfaces, texture– Catmull (1974) ‐ Z‐buffer hidden‐surface algorithm– Crow (1977) ‐ anti‐aliasing( ) g

Page 16: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

• early 1980s ‐ global illuminationearly 1980s  global illumination– Whitted (1980) ‐ ray tracing

Goral Torrance et al (1984) Cohen (1985)– Goral, Torrance et al. (1984), Cohen (1985) ‐radiosity

– Kajiya (1986) ‐ the rendering equation– Kajiya (1986) ‐ the rendering equation

Page 17: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

• early 1980s ‐ global illuminationearly 1980s  global illumination– Whitted (1980) ‐ ray tracing

– Goral, Torrance et al. (1984), Cohen (1985) ‐ radiosityGoral, Torrance et al. (1984), Cohen (1985)  radiosity

– Kajiya (1986) ‐ the rendering equation

• late 1980s ‐ photorealism– Cook (1984) ‐ shade trees( )

– Perlin (1985) ‐ shading languages

– Hanrahan and Lawson (1990) ‐ RenderMan

Page 18: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

• early 1990s ‐ non‐photorealistic renderingearly 1990s  non photorealistic rendering– Drebin et al. (1988), Levoy (1988) ‐ volume renderingrendering

– Haeberli (1990) ‐ impressionistic paint programs

– Salesin et al (1994‐) ‐ automatic pen‐and‐ink– Salesin et al. (1994‐) ‐ automatic pen‐and‐ink illustration

– Meier (1996) ‐ painterly renderingMeier (1996)  painterly rendering

Page 19: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

• early 1990s ‐ non‐photorealistic renderingearly 1990s  non photorealistic rendering– Drebin et al. (1988), Levoy (1988) ‐ volume renderingrendering

– Haeberli (1990) ‐ impressionistic paint programs

– Salesin et al (1994‐) ‐ automatic pen‐and‐ink– Salesin et al. (1994‐) ‐ automatic pen‐and‐ink illustration

– Meier (1996) ‐ painterly renderingMeier (1996)  painterly rendering

Page 20: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics PipelineComputer Graphics Pipeline• How do we create a rendering such as this?

Page 21: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics PipelineComputer Graphics Pipeline• Design the scene (technical drawing in “wireframe”)

Page 22: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• Apply perspective transformations to the scene geometry for a virtual 

camera

Page 23: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• Hidden lines removed and colors added

Page 24: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• Geometric primitives filled with constant color

Page 25: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• View‐independent lighting model added

Page 26: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• View‐dependent lighting model added

Page 27: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• Texture mapping: pictures are wrapped around objects

Page 28: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics Pipeline• Reflections, shadows, and bumpy surfaces

Page 29: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Computer Graphics PipelineComputer Graphics Pipeline

Modeling Transformation

Geometric Primitives

T f i t 3D ld di t tModeling Transformation

Lighting

Transform into 3D world coordinate system

Simulate illumination and reflectance

Viewing Transformation Transform into 3D camera coordinate system

Clipping Clip primitives outside camera’s view

Projection Transformation

Scan Conversion

Transform into 2D camera coordinate system

Draw pixels (incl. texturing, hidden surface…)Scan Conversion

Image

p ( g, )

Page 30: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Linear Algebra ReviewLinear Algebra Review

• Why do we need it?Why do we need it?– Modeling transformation

• Move “objects” into place relative to a world originMove  objects  into place relative to a world origin

– Viewing transformation• Move “objects” into place relative to cameraMove  objects  into place relative to camera

– Perspective transformation• Project “objects” onto image planeProject  objects  onto image plane

Page 31: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

TransformationsTransformations

• Most popular transformations in graphicsMost popular transformations in graphics– Translation

Rotation– Rotation

– Scale

P j ti– Projection

• In order to use a single matrix for all, we use h dihomogeneous coordinates…

Page 32: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Transformations

Page 33: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Transformations

Page 34: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

TransformationsTransformations

Perspective projection

Page 35: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

RepresentationsRepresentations

• How are the objects described in a computer?How are the objects described in a computer?– Points (or vertices)

Lines– Lines

– Triangles

P l– Polygons

– Curved surfaces, etc.

F i– Functions

Page 36: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

RepresentationsRepresentations

• How are the geometric primitives grouped?How are the geometric primitives grouped?– “Polygon soup”

Vertex array/triangle strip– Vertex‐array/triangle‐strip

– Mesh

Page 37: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

RepresentationsRepresentations

• What information is needed per vertex?What information is needed per vertex?– Position

Normal– Normal

– Color

T t di t– Texture coordinates…

Page 38: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

RepresentationsRepresentations

• What information is needed per geometricWhat information is needed per geometric primitive?– Color– Color

– Normal

Material properties (e g textures )– Material properties (e.g. textures…)

Page 39: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Texture Mapping• Map a “texture” onto the surface of an object

– Wood, marble, or any “pattern”

Page 40: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Texture MappingTexture Mapping• A texture is a two‐dimensional array of “texels”, indexed by a (u,v) texture 

coordinate

• At each screen pixel, a texel can be used to substitute a geometric primitives surface color

Page 41: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Texture MappingTexture Mapping

Page 42: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Texture Mapping

Page 43: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Lighting and ShadingLighting and Shading

• Light sources– Point light

• Models an omnidirectional light source (e.g., a bulb)– Directional lightDirectional light

• Models an omnidirectional light source at infinity– Spot light

• Models a point light with directionModels a point light with direction

• Light modelAmbient light– Ambient light

– Diffuse reflection – Specular reflection

Page 44: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Lighting and ShadingLighting and Shading• Diffuse reflection

– Lambertian model

Page 45: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Lighting and ShadingLighting and Shading• Specular reflection

– Phong model

Page 46: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Lighting and ShadingLighting and Shading

Page 47: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Lighting and ShadingLighting and Shading

…shadows?

Page 48: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Advanced Topics: Global IlluminationAdvanced Topics: Global Illumination

Page 49: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Advanced Topics: Ray tracingAdvanced Topics: Ray tracing

Page 50: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Advanced Topics: Antialiasing

Page 51: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

OpenGLOpenGL

• Software interface to graphics hardwareSoftware interface to graphics hardware• ~150 distinct commands• Hardware‐independent and widely supported• Hardware‐independent and widely supported

– To achieve this, no windowing tasks are included

• GLU (Graphics Library Utilities)• GLU (Graphics Library Utilities)– Provides some higher‐level modeling features such as curved surfaces, objects, etc.such as curved surfaces, objects, etc.

• Open Inventor (old)– A higher‐level object‐oriented software packageA higher level object oriented software package

Page 52: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

OpenGL OnlineOpenGL Online

• Programming Guide v1 1 (“Red book”)Programming Guide v1.1 ( Red book )– http://www.glprogramming.com/red/

• Reference Manual v1.1 (“Blue book”)// / /– http://www.glprogramming.com/blue/

• Current version is >2.0

Page 53: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

OpenGLOpenGL

• Rendering parametersRendering parameters– Lighting, shading, lots of little details…

• Texture informationTexture information– Texture data, mapping strategies

• Matrix transformationsMatrix transformations– Projection– Model viewModel view– (Texture)– (Color)(Color)

Page 54: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Matrix TransformationsMatrix Transformations

Page 55: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Matrix TransformationsMatrix Transformations

• Each of modelview and projection matrix is a 4x4 matrix• OpenGL functions

– glMatrixMode(…)– glLoadIdentity(…)g oad de t ty( )– glLoadMatrixf(…)– glMultMatrix(…)– glTranslate(…)glTranslate(…)– glScale(…)– glRotate(…)

– glPushMatrix()– glPopMatrix()

Page 56: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Matrix TransformationsMatrix Transformations{

………glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMultMatrixf(N); /* apply transformation */glMultMatrixf(M); /* apply transformation M */ glMultMatrixf(L); /* apply transformation L */ glBegin(GL_POINTS); glVertex3f(v); /* draw transformed vertex v */glVertex3f(v); /* draw transformed vertex v */glEnd(); ………

}

= draw transformed point “N(M(Lv))”

Page 57: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Modelview Transformations

lR t t f(45 0 0 1)glRotatef(45,0,0,1)

3 ( ) lS l f(2 0 5 1 0)glTranslate3f(tx,ty,tz) glScalef(2,-0.5,1.0)

Page 58: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Modelview TransformationsModelview Transformations

glRotatef(d,rx,ry,rz);

glTranslate3f(tx,ty,tz);

glTranslate3f(tx,ty,tz);

glRotatef(d,rx,ry,rz);

Page 59: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Modelview TransformationsModelview Transformationsvoid pilotView{GLdouble planex, GLdouble planey, GLdouble

l GLd bl ll GLd bl it h GLd bl h di )planez, GLdouble roll, GLdouble pitch, GLdouble heading) {

glRotated(roll, 0.0, 0.0, 1.0); glRotated(pitch, 0.0, 1.0, 0.0);glRotated(heading, 1.0, 0.0, 0.0); glTranslated(-planex, -planey, -planez);

}

void polarView{GLdouble distance, GLdouble twist, GLdouble elevation, GLdouble azimuth)

{ glTranslated(0.0, 0.0, -distance); glRotated(-twist, 0.0, 0.0, 1.0); glRotated(-elevation, 1.0, 0.0, 0.0); glRotated(azimuth, 0.0, 0.0, 1.0);

}}

Page 60: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Projection TransformationsProjection Transformations

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);

Page 61: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Projection TransformationsProjection Transformations

void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble GLd bl f )near, GLdouble far);

Page 62: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Projection TransformationsProjection Transformations

void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);

void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

Page 63: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Matrix TransformationsMatrix Transformations

draw_wheel_and_bolts() {

long i; draw_wheel(); for(i=0;i<5;i++){

glPushMatrix(); glRotatef(72.0*i,0.0,0.0,1.0); glTranslatef(3.0,0.0,0.0); draw_bolt(); glPopMatrix();

}}

Page 64: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Simple OpenGL ProgramSimple OpenGL Program{

<I iti li O GL t t ><Initialize OpenGL state>

<Load and define textures>

<Specify lights and shading parameters>

<Load projection matrix>

For each frame

<Load model view matrix><Draw primitives>

End frame}}

Page 65: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Simple ProgramSimple Program#include <GL/gl.h> main() (){

InitializeAWindowPlease(); glMatrixMode(GL_PROJECTION);glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glClearColor (0.0, 0.0, 0.0, 0.0); gglClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslate3f(1.0, 1.0, 1.0):glBegin(GL_POLYGON);

glVertex3f (0.25, 0.25, 0.0);glVertex3f (0.75, 0.25, 0.0);glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0);

glEnd(); glFlush(); UpdateTheWindowAndCheckForEvents();

}

Page 66: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

GLUTGLUT

• = Graphics Library Utility Toolkit p y y– Adds functionality such as windowing operations to OpenGL

• Event‐based callback interface– Display callbackp y– Resize callback– Idle callbackK b d llb k– Keyboard callback

– Mouse movement callback – Mouse button callbackMouse button callback

Page 67: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Simple OpenGL + GLUT ProgramSimple OpenGL + GLUT Program#include <…> KeyCallback()

{DisplayCallback(){

<Clear window><Load Projection matrix><Load Modelview matrix>

{<Handle key presses>

}

MouseMovementCallback{

<Draw primitives>(<Swap buffers>)

}

IdleCallback()

<Handle mouse movement>}

MouseButtonsCallback{

{<Do some computations><Maybe force a window refresh>

}

<Handle mouse buttons>}

Main(){

KeyCallback(){

<Handle key presses>}

<Initialize GLUT and callbacks><Create a window><Initialize OpenGL state>

<Enter main event loop>}}

Page 68: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Simple OpenGL + GLUT ProgramSimple OpenGL + GLUT Program#include <GL/gl.h>#include <GL/glu.h>

void reshape (int w, int h){/g

#include <GL/glut.h>

void init(void) {glClearColor (0.0, 0.0, 0.0, 0.0);

{glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION);glLoadIdentity ();glFrustum (‐1.0, 1.0, ‐1.0, 1.0, 1.5, 20.0);glMatrixMode (GL_MODELVIEW);g ( )

glShadeModel (GL_FLAT);}

void display(void){

g ( _ )}

int main(int argc, char** argv){glutInit(&argc, argv);

glClear (GL_COLOR_BUFFER_BIT);glColor3f (1.0, 1.0, 1.0);glLoadIdentity (); gluLookAt (0, 0, 5, 0, 0, 0, 0, 1, 0);glScalef (1.0, 2.0, 1.0);  

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100);glutCreateWindow (argv[0]);init ();

glutWireCube (1.0);glFlush ();

}

glutDisplayFunc(display); glutReshapeFunc(reshape);glutMainLoop();return 0;

}

Page 69: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Example Program with LightingExample Program with Lighting#include <GL/gl.h>#include <GL/glu.h>#i l d GL/ l t h

void reshape (int w, int h){

lVi t (0 0 (GL i i) (GL i i) h)#include <GL/glut.h>

void init(void) {GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };GLfloat mat_shininess[] = { 50.0 };GLfloat light position[] = { 1.0, 1.0, 1.0, 0.0 };

glViewport (0, 0, (GLsizei) w, (GLsizei) h);glMatrixMode (GL_PROJECTION);glLoadIdentity();if (w <= h)glOrtho (‐1.5, 1.5, ‐1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, ‐10.0, 10.0);

elseGLfloat light_position[]   { 1.0, 1.0, 1.0, 0.0 };glClearColor (0.0, 0.0, 0.0, 0.0);glShadeModel (GL_SMOOTH);

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);glLightfv(GL_LIGHT0, GL_POSITION, light_position);

elseglOrtho (‐1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, ‐1.5, 1.5, ‐10.0, 10.0);

glMatrixMode(GL_MODELVIEW);glLoadIdentity();

}

i t i (i t h ** )glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL_DEPTH_TEST);

}void display(void){

int main(int argc, char** argv){glutInit(&argc, argv);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | 

GLUT_DEPTH);glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100);{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glutSolidSphere (1.0, 20, 16);glFlush ();

}

glutCreateWindow (argv[0]);init ();glutDisplayFunc(display); glutReshapeFunc(reshape);glutMainLoop();return 0;

}}

Page 70: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Simple OpenGL + GLUT ProgramSimple OpenGL + GLUT Program

Page 71: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

GLUI• = Graphics Library User Interface

Page 72: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

GLUI• = Graphics Library User Interface

Page 73: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

GLUI• = Graphics Library User Interface

Page 74: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

Alternatives graphics pipeline?Alternatives graphics pipeline?

• Traditional pipeline…okad t o a p pe e…o• Parallel pipeline

– Cluster of PCs?– Cluster of PS3?– What must be coordinated? What changes? What are 

h b l k ?the bottlenecks?

Sort first vs Sort last pipeline– Sort‐first vs. Sort‐last pipeline• PixelFlow• Several hybrid designs

Page 75: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

What can you do with a graphics lpipeline?

• Uhm graphicsUhm…graphics

Page 76: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

What can you do with a graphics lpipeline?

• Uhm graphicsUhm…graphics

• Paperweight?

Page 77: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

What can you do with a graphics lpipeline?

• Uhm graphicsUhm…graphics

• Paperweight?

• How about large number crunching tasks?• How about general (parallelizable) tasks?

Page 78: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

CUDA and OpenCLCUDA and OpenCL

• NVIDIA defined “CUDA” (new)NVIDIA defined  CUDA  (new)– Compute Unified Device Architecture

http://www nvidia com/object/cuda home html#– http://www.nvidia.com/object/cuda_home.html#

Kh ’ d fi d “O CL” ( )• Khrono’s group defined “OpenCL” (newer)– Open Standard for Parallel Programming of H t S tHeterogeneous Systems

– http://www.khronos.org/opencl/

Page 79: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

CUDA ExampleCUDA Example

• Rotate a 2D image by an angleRotate a 2D image by an angle

O th CPU (PC)– On the CPU (PC)• simple‐tex.pdf

– On the GPU (graphics card)i l t k l df• simple‐tex‐kernel.pdf

Page 80: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

OpenCL ExampleOpenCL Example

• Compute a Fast Fourier TransformCompute a Fast Fourier Transform

O th CPU (PC)– On the CPU (PC)• cl‐cpu.pdf

– On the GPU (graphics card)• cl‐gpu pdf• cl‐gpu.pdf

Page 81: Graphics, OpenGL, GLUT, GLUI, CUDA, OpenCL,  · PDF fileGraphics, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, and more! ... • 3D – 1980s ‐3D ... Simple OpenGL + GLUT Program

OpenCVOpenCV

• A library for computer‐vision related softwareA library for computer vision related software

• Derived from research work and high‐performance code from Intelperformance code from Intel

• http://opencv.willowgarage.com/wiki/– e.g., find fundamental matrix