Transcript Document

COMPUTER
G RAPH I C S
Computer graphic
-- Programming with OpenGL 2
Jie chen
COMPUTER
G RAPH I C S
What is new
• Website:
http://www.ee.oulu.fi/~jiechen/Course.htm
• Lecture slides for 4rd are online now
2
Jie chen
COMPUTER
G RAPH I C S
OpenGL rendering pipeline
---Order of Operations
3
Jie chen
COMPUTER
G RAPH I C S
Order of Operations
• OpenGL rendering pipeline has a similar order of
operations, a series of processing stages.
• This ordering is not a strict rule of how OpenGL
is implemented but provides a reliable guide for
predicting what OpenGL will do.
4
Jie chen
COMPUTER
G RAPH I C S
Order of Operations
• How does OpenGL take to processing data?
– Geometric data (vertices, lines, and polygons) follow
the path through the row of boxes that includes
evaluators and per-vertex operations, while pixel data
(pixels, images, and bitmaps) are treated differently
for part of the process.
– Both types of data undergo the same final steps
(rasterization and per-fragment operations) before the
final pixel data is written into the framebuffer.
5
Jie chen
COMPUTER
G RAPH I C S
Display Lists
• All data, whether it describes geometry or pixels,
can be saved in a display list for current or later use.
– The alternative to retaining data in a display list is
processing the data immediately - also known as
immediate mode.
• When a display list is executed, the retained data
is sent from the display list just as if it were sent by
the application in immediate mode.
6
Jie chen
COMPUTER
G RAPH I C S
Evaluators
• All geometric primitives (e.g., point
, line
or
polygon ) are eventually described by vertices.
• Parametric curves
and surfaces
may be
initially described by control points and polynomial
functions called basis functions.
– Evaluators provide a method to derive the vertices used to
represent the surface from the control points.
– The method is a polynomial mapping, which can produce
surface normal, texture coordinates, colors, and spatial
coordinate values from the control points.
7
Jie chen
COMPUTER
G RAPH I C S
Per-Vertex Operations
• Converting the vertex data into primitives (e.g.,
point
, line
or polygon ).
• If advanced features are enabled, this stage is
even busier.
– Generate and transform texture coordinates.
– Perform the lighting calculations using the
transformed vertex, surface normal, light source
position, material properties, and other lighting
information to produce a color value.
8
Jie chen
COMPUTER
G RAPH I C S
Primitive Assembly
• Primitive assembly differs, depending on
whether the primitive is a point, a line, or
a polygon.
– If flat shading is enabled, the colors or indices
of all the vertices in a line or polygon are set to
the same value.
– If special clipping planes are defined and
enabled, they're used to clip primitives of all
three types (point, line, or polygon).
• Finally, points, lines, and polygons are
rasterized to fragments.
9
Jie chen
COMPUTER
G RAPH I C S
Pixel Operations
• Pixels from host memory are first unpacked into the
proper number of components.
• Next, the data is scaled, biased, and processed using a
pixel map.
• Pixel-transfer operations (scale, bias, mapping, and
clamping) are performed If pixel data is read from the
framebuffer.
• The pixel copy operation is similar to a combination of
the unpacking and transfer operations, and only a single
pass is made through the transfer operations.
10
Jie chen
COMPUTER
G RAPH I C S
Texture Memory
• Texture image data can be specified from framebuffer
memory, as well as processor memory.
• All or a portion of a texture image may be replaced.
• Texture data may be stored in texture objects, which can
be loaded into texture memory.
• If there are too many texture objects to fit into texture
memory at the same time, the textures that have the
highest priorities remain in the texture memory.
11
Jie chen
COMPUTER
G RAPH I C S
Fragment Operations
• Operations
– Generating a texel (texture element,
also texture pixel) )
– Performing the fog calculations
– Antialiasing.
– Scissoring, the alpha test, the stencil test, and
the depth-buffer test.
– performing blending test if in RGBA mode.
– Dithering and logical operation.
• The fragment is then masked by a color mask
or an index mask, and drawn into the
appropriate buffer.
12
Jie chen
COMPUTER
G RAPH I C S
From buffer to image
Bitplane
registers
8
Color Guns
01001011
8 bit DAC
Blue 75
8
10101100
8 bit DAC
Green 172
8
DAC: digital-to-analog converter
00001010
8 bit DAC
Red 10
Frame Buffer
Jie chen
13
COMPUTER
G RAPH I C S
Basics of GLUT
14
Jie chen
COMPUTER
G RAPH I C S
Basics of GLUT
• Why GLUT?
– GLUT has become a popular library for
OpenGL programmers, because it
standardizes and simplifies window and event
management.
– GLUT has been ported atop a variety of
OpenGL implementations, including both the
X Window System and Microsoft Windows NT.
15
Jie chen
COMPUTER
G RAPH I C S
Initializing and Creating a Window
• Before you can open a window, you must specify
its characteristics:
– Should it be single-buffered or double-buffered?
– Should it store colors as RGBA values or as color
indices?
– Where should it appear on your display?
• To specify the answers to these questions, call
–
–
–
–
–
glutInit(),
glutInitDisplayMode(),
glutInitWindowSize(),
glutInitWindowPosition(),
glutCreateWindow().
16
Jie chen
COMPUTER
G RAPH I C S
glutInit()
• void glutInit (int argc,
char **argv);
– glutInit() should be called
before any other GLUT
routine, because it
initializes the GLUT library.
17
Jie chen
COMPUTER
G RAPH I C S
glutInitDisplayMode()
• void
glutInitDisplayMode(unsigned
int mode);
• Specifies a display mode (such
as RGBA or color-index, or
single- or double-buffered).
• You can also specify that the
window have an associated
depth, stencil, and/or
accumulation buffer.
– The mask argument is a bitwise
ORed combination
• GLUT_RGBA or GLUT_INDEX,
GLUT_SINGLE or GLUT_DOUBLE,
– and any of the buffer-enabling flags:
• GLUT_DEPTH, GLUT_STENCIL, or
GLUT_ACCUM.
18
Jie chen
COMPUTER
G RAPH I C S
glutInitWindowSize()
• void glutInitWindowSize(int width, int height);
height
width
19
Jie chen
COMPUTER
G RAPH I C S
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,
relative to the entire
display.
20
Jie chen
COMPUTER
G RAPH I C S
glutCreateWindow()
• int glutCreateWindow(char *name);
• Opens a window with previously set
characteristics (display mode, width,
height, and so on).
• The string name appear in title bar.
• The window is not initially displayed
until glutMainLoop() is entered.
• The value returned is a unique integer
identifier for the window. This identifier
can be used for controlling and
rendering to multiple windows (each
with an OpenGL rendering context)
from the same application.
21
Jie chen
COMPUTER
G RAPH I C S
glutDisplayFunc()
• void glutDisplayFunc(void
(*func)(void))
• Call the Specified the
function whenever the
contents of the window
need to be redrawn.
• The contents of the window
may need to be redrawn
when the window is
– initially opened,
– popped and window damage
is exposed
22
Jie chen
COMPUTER
G RAPH I C S
glutReshapeFunc()
• void glutReshapeFunc
(void (*func)(int width, int
height));
• Call this function whenever
the window is resized or
moved.
• The argument func is a
pointer to a function that
expects two arguments, the
new width and height of a
window.
23
Jie chen
COMPUTER
G RAPH I C S
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 windowrelative coordinates) when the
key was pressed.
24
Jie chen
COMPUTER
G RAPH I C S
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.
25
Jie chen
COMPUTER
G RAPH I C S
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.
26
Jie chen
COMPUTER
G RAPH I C S
glutPostRedisplay()
• void glutPostRedisplay(void);
– Marks the current
window as needing to be
redrawn.
27
Jie chen
COMPUTER
G RAPH I C S
glutSetColor()
• void glutSetColor(GLint
index, GLfloat red, GLfloat
green, GLfloat blue);
• Loads the index in the
color map
– Index: with the given red,
green, and blue values.
– These values for RGB are
normalized to lie in the range
[0.0,1.0].
28
Jie chen
COMPUTER
G RAPH I C S
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.
29
Jie chen
COMPUTER
G RAPH I C S
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.
30
Jie chen
COMPUTER
G RAPH I C S
Transformations and Timers
• Demo
• Transformations
– glTranslatef(0.0f, 0.0f, -5.0f);
• //Move forward 5 units
– glPushMatrix();
• //Save the transformations performed thus far
– glPopMatrix();
• //Undo the move to the center of the trapezoid
– glRotatef 30.0f, 0.0f, 0.0f, 1.0f);
• //Rotate about the z-axis
– glMatrixMode(GL_MODELVIEW);
• //Switch to the drawing perspective
– glutTimerFunc(25, update, 0);
• //Add a timer
31
Jie chen
Model Rotation
Model Scaling
COMPUTER
G RAPH I C S
Color
• Demo
• glColor3f(0.5f, 0.0f, 0.8f);
Green
Blue
Yellow
White
Cyan
Red
Black
Megenta
Blue
R=0.5*256
G= 0.0f*256
B= 0.8f*256
Red
(0.5f, 0.0f, 0.8f)
Color cube
Jie chen
Green
Color palette
Color map
32
COMPUTER
G RAPH I C S
Lighting
• Lighting is 5% of light setup and 95% of
revisions and adjustments.
Jeremy Birn
Digital Lighting and Rendering
33
Jie chen
COMPUTER
G RAPH I C S
Lighting
• Imaging function
N
– I=f(ρ, n, s)
•
•
•
•
S
I: radiosity
ρ: albedo
x
N: unit normal
S: source vector (magnitude proportional to
intensity of the source)
• Lambertian model
– I=ρ×n×s
34
Jie chen
COMPUTER
G RAPH I C S
Lighting
• Light source
– Diffuse Light
– Ambient light
– Specular Light
N
S
x
35
Jie chen
COMPUTER
G RAPH I C S
Lighting- source
• Demo
• 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);
36
Jie chen
COMPUTER
G RAPH I C S
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);
Jie chen
Diffuse Light
Diffuse and Ambient
Lighting
Diffuse, Ambient and
Specular Lighting
37
COMPUTER
G RAPH I C S
Lighting- source
• Specular Light: refered to as Specular Highlight,
• it highlights an object with a reflective color.
• Add ambient light by OpenGL
– GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0};
– glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
Jie chen
Diffuse Light
Diffuse and Ambient
Lighting
Diffuse, Ambient and
Specular Lighting
38
COMPUTER
G RAPH I C S
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
39
Jie chen
COMPUTER
G RAPH I C S
Lighting-Function
• specifying light source parameters
40
Jie chen
COMPUTER
G RAPH I C S
Lighting-Source example
• specifying light source parameters
I = Iamb+Idiff+Ispec= Ia Ka + Id Kd N⋅L + Is Ks (R⋅V)n
–
–
–
–
GLfloat position0[] = {1.0, 1.0, 1.0, 0.0};
GLfloat diffuse0[] = {1.0, 0.0, 0.0, 1.0}; // Id term - Red
GLfloat specular0[] = {1.0, 1.0, 1.0, 1.0}; // Is term - White
GLfloat ambient0[] = {0.1, 0.1, 0.1, 1.0}; // Ia 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);
41
Jie chen
COMPUTER
G RAPH I C S
Lighting- Normal
N
S
• Normal : A face's normal is a vector
that is perpendicular to the face.
• glNormal3f(1.0f, 0.0f, 0.0f);
x
– telling 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
42
Jie chen
COMPUTER
G RAPH I C S
Lighting - Material
N
S
• Once lighting is enabled, colors are
no longer used
x
43
Jie chen
COMPUTER
G RAPH I C S
Lighting - Material
• Main functions for OpenGL:
44
Jie chen
COMPUTER
G RAPH I C S
Lighting-source example
• specifying a Material
I = Iamb+Idiff+Ispec= Ia Ka + Id Kd N⋅L + Is Ks (R⋅V)n
– param:
•
•
•
•
•
•
GL_AMBIENT
// Ka term
GL_DIFFUSE
// Kd term
GL_SPECULAR // Ks term
GL_SHININESS // exponent n
GL_AMBIENT_AND_DIFFUSE // Ka = Kd
GL_EMISSION
// No light calculations
// simulates sources
45
Jie chen
COMPUTER
G RAPH I C S
Lighting - Material
•
46
Jie chen
COMPUTER
G RAPH I C S
Homework
• link
47
Jie chen
COMPUTER
G RAPH I C S
• The end of this lecture!
48
Jie chen