Download PowerPoint

Document related concepts

InfiniteReality wikipedia , lookup

Spatial anti-aliasing wikipedia , lookup

PostScript wikipedia , lookup

Theme Hospital wikipedia , lookup

General-purpose computing on graphics processing units wikipedia , lookup

Intel GMA wikipedia , lookup

Color Graphics Adapter wikipedia , lookup

Rendering (computer graphics) wikipedia , lookup

Comparison of OpenGL and Direct3D wikipedia , lookup

BSAVE (bitmap format) wikipedia , lookup

Free and open-source graphics device driver wikipedia , lookup

OpenGL wikipedia , lookup

Apple II graphics wikipedia , lookup

Tektronix 4010 wikipedia , lookup

Mesa (computer graphics) wikipedia , lookup

Framebuffer wikipedia , lookup

Video card wikipedia , lookup

2.5D wikipedia , lookup

Waveform graphics wikipedia , lookup

Molecular graphics wikipedia , lookup

Graphics processing unit wikipedia , lookup

Transcript
• Welcome!
• Today:
– Administrative stuff.
– Introductory remarks.
– Voting on course arrangements.
– Basic graphics + OpenGL revision or curves &
surfaces.
COMP9018 - Advanced Graphics
Administrivia
• Web page:
– http://www.cse.unsw.edu.au/~cs9018
– Includes a notice board + message board.
– Tim & I will read/contribute regularly.
– Also lots of links and stuff.
COMP9018 - Advanced Graphics
Administrivia
• People
– Waleed (that's me) ([email protected])
– Tim (the smart guy) ([email protected])
– Who to e-mail:
•
•
•
•
•
•
Enrolments: waleed
Software problems: waleed
Web issues: waleed
Lecture material: whoever presented it
Tricky questions: lambert
Lecturer-in-charge/complex admin: lambert
COMP9018 - Advanced Graphics
Attitude to the course
• Graphics is HUGE!
• No one person can grasp all of it
• Tim & I know stuff but we don't know it
all: guides rather than experts
COMP9018 - Advanced Graphics
Fourth year
• This is a fourth year subject/graduate
subject
• So rules are a little different.
– Smaller numbers =
• more flexibility
• higher standard (we hope)
• more interactivity
COMP9018 - Advanced Graphics
Things we’d like you to do
• Common sense:
–
–
–
–
–
–
–
Doing the best you can in the subject
Respecting fellow students
Shutting up when you're not supposed to talk
Participating
Doing the preparation/homework we ask you to
Not cheating (we'll come down HARD on cheats)
Actually showing up to appointments/not
expecting us to be available all the time
COMP9018 - Advanced Graphics
Some basic rules
• Questions allowed at any time ... but raise
your hands and wait for us to ask
• Open to suggestions
• Academic etiquette: You’re allowed to
borrow or copy, but when you do,
ACKNOWLEDGE it.
COMP9018 - Advanced Graphics
Anybody scared?
• Can discontinue without failure until end
of week 4 (I think)
COMP9018 - Advanced Graphics
Run through handout
• See handouts
COMP9018 - Advanced Graphics
Voting issues
• Vote on lots of critical things
• ... but you can't vote people out of the
course
• Course isn't going to get smaller and
smaller as time goes on, 'til last person
gets an HD
COMP9018 - Advanced Graphics
Issue 1
• Language preference
– C/C++ vs Java
– Any other bizarre languages? Fortran, Perl,
Python, Haskell?
• Examples in both?
• Assignments: both allowed
COMP9018 - Advanced Graphics
Issue 2
• Last time had a local games company give
a seminar/demo
• Couldn’t organise film person; but will try
again this year … if you are interested
• Vote?
COMP9018 - Advanced Graphics
Issue 3
• Syllabus material
• Rough outline of what was covered last
time
• Any topics from the list people feel
strongly about?
• What to drop to include?
COMP9018 - Advanced Graphics
Week 13 demo night
•
•
•
•
•
Put it in your calendars
No lectures week 13
From 5pm to 10pm
Can bring in own equipment if you like
Do you guys want me to ask outsiders
(e.g. industry people) to attend?
COMP9018 - Advanced Graphics
Text
• Graphics has 2 properties:
– Diverse
– Fast-moving
• Generally means textbooks get out of
date quickly
• The Net is a very good resource.
• See links on Web page for online info.
COMP9018 - Advanced Graphics
… but if you really want …
• Textbooks
– Computer Graphics: Principles & Practice
Brown et al. Known as “Old Testament”
– Advanced Animation & Rendering Techniques
by Watt & Watt. Known as “New Testament”
– OpenGL Programming Guide by OpenGL ARB.
Known as the “Red Book” or “Lego book”.
Available online.
COMP9018 - Advanced Graphics
Labs
• Have hardware acceleration (TNT2 cards -- not
great, but better than nothing)
• Linux is official platform (not by choice, really)
• For compatibility: Only use standard Java
libraries + gl4java; for C/C++: standard
libraries + OpenGL + GLU + GLUT + MUI ...
must compile with gcc
• If you want something else, let us know.
COMP9018 - Advanced Graphics
Project possibilities
• Write your own game
• Write your own modeller
• Extend an open source program with
some feature
• Make a short CG movie
• But be careful with your time budget!
Don’t want you to fail other subjects
(almost happened last time)
COMP9018 - Advanced Graphics
Examples from last time
• Some examples
– Movie scenes:
• Lord of the Rings + modelling tools (4 ppl).
• Story of a fly.
– Games:
• 3D pool (2 ppl).
• 3D tetris
• 3D multiplayer networked robot/shooter/transfomer game
(2ppl).
• Halflife level modelled on Ground & Basement of CSE.
• Global thermonuclear war with Ronald Raygun,
COMP9018 - Advanced Graphics
More examples
– Interactive applications
•
•
•
•
•
Origami tutor
CT-Scan lung visualiser
Photo-etching animation.
Artificial creatures lab
Lego modeller
– Screen savers:
• Rollercoaster generator.
• Rubik’s snake.
COMP9018 - Advanced Graphics
Special resources
• Generally
– Increased disk quota (+ 20Mb)
– Increased IP quota (+ 200Mb)
– Trying to arrange special access to bongo lab.
COMP9018 - Advanced Graphics
Project-specific
• With access to bongo:
– 6 Maya floating licenses.
– VirTools.
– May be able to arrange other tools, but may
be difficult.
COMP9018 - Advanced Graphics
Special resources
• Available on request
• Good for the project
• Two clusters:
– vina: 18 machines, 1x866MHz, 256MB RAM,
Linux
– tines: 17 machines, 2x450MHz, 512MB RAM,
Linux
COMP9018 - Advanced Graphics
Software available
•
•
•
•
GLUT 3.7
OpenInventor
POVRay
Blue Moon Rendering Tools (RenderMan
compatible, radiosity support)
• Java3D
• Blender
COMP9018 - Advanced Graphics
Break time
• After break, revision of basic graphics +
OpenGL
or
• Get straight into curves and surfaces.
• Vote!!
COMP9018 - Advanced Graphics
Graphics Revision
• What is graphics?
• About anything computer generated that
you see:
– In print
– On a monitor
– On television
– At the movies
– At art exhibitions
COMP9018 - Advanced Graphics
Usual representation
• Most common representation of an image
is a bitmap: A 2D array of pixels
• Each pixel holds a description of the
colour of that point
• The bitmap that holds the current image
on display is called the frame buffer
COMP9018 - Advanced Graphics
Representing colour
• Usual way is as a combination of red green and
blue; but monochrome is also possible
• Different approaches:
– Fixed set of colours (e.g. CGA, EGA). Colour
represented by a bit value
– Each pixel stores an index into an array of
customisable colours (e.g. VGA) aka colour index
mode
– Each pixel directly represents its colour directly
(e.g. HighColor, TrueColor) aka RGB mode.
COMP9018 - Advanced Graphics
Objects
• Primitive objects in graphics:
– Points: A single coordinate.
– Lines: usually a parametric representation (a
+ tb; 0 < t < 1) or a pair of coordinates.
– Polygons: A sequence of points. Usually talk
of closed polygons (end is same as
beginning)
COMP9018 - Advanced Graphics
But first ... what is a polygon?
• Sequence of points enclosing a plane.
Concave polygon
Convex polygon
• In 3D, we use polygons that only have one face
... there is no "back" of a polygon.
• How do you decide which way a polygon is
facing?
COMP9018 - Advanced Graphics
Using the normal
• If not specified, use the right hand rule.
D
B
C
C
A
A
D
Into the page
COMP9018 - Advanced Graphics
B
Out of the page
Types of polygon
• Nasty polygons:
– Non-planar: Not all vertices are in the same plane.
– Self-intersecting: Polygons that intersect
themselves are problematic.
– Concave: Have internal angles greater than 180
degrees. Complicate lots of things.
• We'll assume we don't have any of these.
COMP9018 - Advanced Graphics
Transformations
• Frequently want to represent "doing" things to
objects. Doing things = "transformations".
• Many useful transformations can be represented
as matrices applied to vectors
• Homogeneous matrices are used so that
translations can be represented as matrices.
• Homogenous means 1 more dimension than
underlying space: 3x3 for 2D, 4x4 for 3D
COMP9018 - Advanced Graphics
Why homogenous?
• Allows us to represent translations as a
matrix multiply -- making almost all
interesting modelling transforms
COMP9018 - Advanced Graphics
Example transforms
•
•
•
•
•
•
Rotate about origin in 2D
Scale
Translate
Rotation about y axis in 3D?
Perspective
etc etc etc
COMP9018 - Advanced Graphics
Window to Viewport mapping
• Maps part of the 2D plane of interest onto
the monitor
• Usually includes an inversion because
monitors are upside down
• Except with OpenGL, which maintains a
Cartesian representation
COMP9018 - Advanced Graphics
Clipping
• Finding bits outside the screen
• Why?
– Speed ... don't render what we don't need to
render
– Avoid overwriting random bits of memory
COMP9018 - Advanced Graphics
Graphics pipelines - 2D
• Start with description of 2D objects (lines,
points, polygon)
• Apply transformations to position them
• Apply window-to-viewport mapping
• Clip
• Rasterise
COMP9018 - Advanced Graphics
OpenGL
• Provides a general platform independent
graphics API that's a rich but thin and
efficient layer over the hardware.
• About 150 commands
• For real-time 2D and 3D point, line and
polygon based rendering
• Also does 3D lighting and texturing
COMP9018 - Advanced Graphics
What OpenGL doesn't do
• Doesn't deal with windows or I/O
• Doesn't handle curves or curved surfaces
• Doesn't have a file format or handle
complicated objects - only points lines and
polygons
• Doesn't create image files
COMP9018 - Advanced Graphics
Companion libraries to
OpenGL
• GLU (GL utility): Does tesselation of curved
surfaces, some more flexible projections, etc
• GLUT (GL utility toolkit): A platformindependent window and I/O system (also now
includes MUI - micro user interface for menus,
dials etc)
• WGL, GLX: Platform-specific window and I/O
systems
• OpenInventor: Provides high-level scene graph
libraries
COMP9018 - Advanced Graphics
OpenGL and Java
• Too slow to have an OpenGL
implementation in Java.
• So usual technique: use a Java native
interface to OpenGL and provide a Java
interface to it.
• We’ll be using GL4Java
COMP9018 - Advanced Graphics
Why not use Java3D?
•
•
•
•
•
Java3D is a high-level scene graph library
More like OpenInventor than OpenGL
Java3D is implemented on top of OpenGL!
Have a look at Java3D later
Grrr ...
COMP9018 - Advanced Graphics
More about OpenGL
• Funky thing about it: It's basically a state
machine - i.e. another computer
• Most commands don't produce visible
output - they set up the state for what
happens between glBegin and glEnd
commands
• Interesting stuff happens between the
glBegin and the glEnd
COMP9018 - Advanced Graphics
Open GL + C/Java
• Anatomy of a simple OpenGL program in
C/GLUT - hello.c
• ... from the OpenGL Programmer's Guide
• Java’s pretty much the same.
COMP9018 - Advanced Graphics
The matrix stack
• How OpenGL does transformations
• A perfect fit for scene graph traversal
• How matrix stacks work
COMP9018 - Advanced Graphics
Basic operations
• glMatrixMode(Mode)sets the matrix stack
we want to use. For now, only concerned
with GL_MODELVIEW.
• glPushMatrix()copies current top of matrix
stack and pushes on top of stack.
• glPopMatrix()pops off the top of the stack
• glLoadIdentity()loads identity matrix onto
top of stack
COMP9018 - Advanced Graphics
How matrix stack is used.
• When you do glTranslatef(...)it actually
postmultiplies by the matrix on top of
stack and puts it back on top of the stack.
• Means that last matrix op is done first.
COMP9018 - Advanced Graphics
Example - On blackboard
COMP9018 - Advanced Graphics
I
• Initialise stack with
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
• Scale by 2
glScaled(1.0, 2.0, 1.0);
• Rotate around x by 90
glRotated(90, 1, 0, 0);
• Push on stack
copy
glPushMatrix();
S(2)
S(2).R(90)
S(2).R(90)
S(2).R(90)
Example - cont'd
S(2).R(90)
.Tr(a)
• Translate by a
S(2).R(90)
glTranslatef(a.x, a.y, a.z);
• Draw a point on screen.
S(2).R(90)
Tr(a)
glBegin(GL_POINTS);
S(2).R(90)
glVertex3f(1,1,1);
glEnd();
would actually draw at S(2).R(90).Tr(a).(1,1,1)
• Pop off stack
S(2).R(90)
glPopMatrix();
COMP9018 - Advanced Graphics
The scene graph
• A representation of the scene
COMP9018 - Advanced Graphics
Generalisation: A scene graph
• Can represent all objects in a scene as a
Each node contains
graph
World
geometry
TIEFighter
Xwing1
R2D2 Pilot Torpedo
Left Arm Head
COMP9018 - Advanced Graphics
Each edge stores
a transformation
Scene graph vs Scene tree
• Previous was a scene tree. Can easily
generalise to scene graph, e.g.
World
TIEFighter
Xwing1
Xwing2
R2D2 Pilot Torpedo
Left Arm Head
COMP9018 - Advanced Graphics
Why is this cool?
• Because it allows us to have libraries of
objects.
• It's hierarchical, so we can have objects
within objects.
• When we change the transform one node
in the scene graph, all its children move,
too! Good for animation.
COMP9018 - Advanced Graphics
Example 2: Arm
• Arm-> Forearm -> Hand -> Fingers
• Transforming arm also moves forearm,
hands, fingers.
• Transforming forearm also transforms
hands & fingers.
COMP9018 - Advanced Graphics
Scene graphs and the matrix
stack
• Can use matrix stack for traversing the scene
graph
• Algorithm:
– Start at root node. Render this node's geometry
– For each child:
•
•
•
•
Push the stack
Apply the modelling transform
Render the node (including children)
Pop the stack
COMP9018 - Advanced Graphics
The scene graph
• Why post multiply?
• Because previous algorithm works with it;
and it represents the common case.
COMP9018 - Advanced Graphics
Viewing scene
•
•
•
•
•
•
Lots of pieces.
Projection (3D only)
Camera setting
Clipping
Visible surface determination (mostly 3D)
Window to viewport mapping
COMP9018 - Advanced Graphics
Projection
• Two types:
• Parallel: All objects are the same size,
regardless of distance
• Perspective: Further away objects are
smaller
• Both can be represented as matrices
COMP9018 - Advanced Graphics
Viewing
• How set camera's position?
• Lots of solid maths, but at end of the day,
you need:
– A position for the camera (view reference
point)
– A view-plane normal
– An up vector
• Use this to construct the viewing matrix.
COMP9018 - Advanced Graphics
Clipping
• 2D case is easy
• 3D: Have to find the "view volume". Area
that can be seen. Also usually define a
near and far plane.
• See demo viewvolpar.wrl and
viewvolpersp.wrl
COMP9018 - Advanced Graphics
Visible surface determination
• Have to decide what goes in front of what.
• Three common approaches in polygon
rendering:
– Depth sorting
– BSP Trees
– Z Buffers
• Other approaches like raytracing handle it
automatically.
COMP9018 - Advanced Graphics
The OpenGL approach
• Viewing transform: Goes on bottom of
modelview stack.
• Projection & clipping combined essentially, a direct description of the view
volume in camera coordinate system.
• Visible surface determination: do-ityourself, or Z buffer.
COMP9018 - Advanced Graphics
OpenGL Viewing Transform
• gluLookat(eyex, eyey, eyez,
lookx, looky, lookz,
upx, upy, upz)
• Eye position = VRP
• VPN = eye -look
• up = up
• Remember: Goes at BOTTOM of modelview
stack
• Think of it as an extra root node.
COMP9018 - Advanced Graphics
View volume definition
• Depends on projection.
• Several functions for defining:
• glOrtho
– parallel projection is also known as orthographic
projection
– glOrtho(left, right, bottom, top, near, far)
• glFrustum
– The truncated pyramid is called a frustum
– glFrustum(left, right, bottom, top, near, far)
COMP9018 - Advanced Graphics
Where do they go?
• A special stack for the projection matrix
called GL_PROJECTION.
• Usually only two deep.
• So usual code is
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(...)
COMP9018 - Advanced Graphics
Convenience functions
• GLU library has some convenient variants:
• gluOrtho2D(left, right, bottom, top) =
glOrtho(left, right, bottom, top, -1, 1).
• Proof: See Mesa source code.
• gluPerspective(fovy, aspect, near, far)
• Maths behind it: OpenGL source code.
COMP9018 - Advanced Graphics
What do projections do?
• The transform the view volume into the
canonical view volume.
• Canonical = standard.
• CVV is the standard cube.
• Why? Easy to clip against; can be done in
hardware.
COMP9018 - Advanced Graphics
Window->Viewport
•
•
•
•
Window to viewport has its own matrix stack
glViewport(x, y, width, height)
WARNING: Viewport is not inverted as in java!
x, y are the lower left corners of the viewport
rectangle in pixels.
• Note: in OpenGL, win->view does NOT kill the z
values; the z values are kept for the z buffer.
COMP9018 - Advanced Graphics
Z buffering
• Set as part of the window characteristics.
• Handled by the windowing system; e.g.
GLUT or Java.
• Called the depth buffer in OpenGL
terminology.
• Must be cleared after every rendered
frame.
COMP9018 - Advanced Graphics
Lighting and shading
• Different models.
– Ambient
– Diffuse (uses Lambert's law)
– Specular (includes diffuse component - uses
Phong illumination eqtn)
COMP9018 - Advanced Graphics
Different ways of rendering a
scene
•
•
•
•
Polygon rendering (like OpenGL)
Ray tracing (like POVRay)
Radiosity
Ray-trace/Radiosity hybrids (like POVRay
[experimental] and BMRT)
• Polygon is the only realtime option right
now.
COMP9018 - Advanced Graphics
Polygon rendering ... cont'd
• Focus on polygon rendering for a while.
• How to shade polygons?
– Flat shading: one normal per polygon, shade
whole polygon one colour
– Gouraud shading: one normal per vertex
(comes from model), interpolate colours
between vertices
– Phong shading: one normal per vertex,
interpolate normals between vertices
COMP9018 - Advanced Graphics
Light source and material
• Lighting equations depend on:
– Light source intensities
– Material properties
COMP9018 - Advanced Graphics
Light sources in OpenGL
• glLightf(lightid,property,value)
• All purpose lighting function
• lightnum is the id of the light (GL_LIGHT0 to
GL_LIGHT7, but may be more).
• Settable properties include:
–
–
–
–
Light position (note! Modelview matrix applies!)
Diffuse intensity, ambient intensity, specular intensity
Light attenuation with distance
Spotlight setting
COMP9018 - Advanced Graphics
Global properties
• Global lighting is controlled by the
glLightModel() function
• Properties include:
– Ambient light
– Two-sided lighting
– Local viewer
COMP9018 - Advanced Graphics
Material properties
•
•
•
•
glMaterial(face, property, value)
Applies to all subsequent values
face: front or back side of polygon
Properties:
– ambient, diffuse, specular and emissive
colour components
– shininess (Phong exponent)
COMP9018 - Advanced Graphics
Other important functions
• For polygons, must define normals. So
need glNormal() function
• Just like material properties: normal
applies until you change it.
COMP9018 - Advanced Graphics
Texture mapping
• Using images to add surface detail
• Example: logopoly.wrl
• Problem: How to "map" texture space to
polygon's surface.
• Solution: Use texture coordinates for every
vertex and apply all that transformation stuff.
• More detail on texture mapping in Advanced
OpenGL lecture.
COMP9018 - Advanced Graphics
Variants of texture mapping
• Procedural textures instead of images.
• 3D vs 2D textures.
• Using textures to model
– Surface perturbations: Bump map.
– Reflections: Environment map.
• Textures are the "hot" area in graphics.
Incredible number of nifty hacks possible
using textures and variants.
COMP9018 - Advanced Graphics
Aliasing
• Whenever we try to capture a highfrequency (i.e. detail) reality with a lowfrequency sample.
• Examples: Jaggies, texture collapse, etc.
• Approaches to fix: prefiltering,
postfiltering, adaptive supersampling,
jitter, mipmapping, bilinear interpolation,
trilinear interpolation
COMP9018 - Advanced Graphics
Brains full yet?
• Good!
• Want something interesting to read?
• Subject Web page: Readings - 19 Pitfalls
to avoid when programming OpenGL; by
Mark Kilgard: Person who wrote GLUT,
and a couple of other OpenGL books, now
works at nVidia.
COMP9018 - Advanced Graphics