Introduction to Computer Graphics CS 445 / 645

advertisement
UBI 516 Advanced Computer Graphics
Graphics Output Primitives
( Chapter 3 )
Screen & Window Coordinates
Setting a pixel
(0,0) Screen coordinates
• glBegin(GL_POINTS);
glVertex2f(px,py);
glEnd();
Setting Orthogonal Camera
• glMatrixMode(GL_PROJECTION);
y
glLoadIdentity();
gluOrtho2D(xmin,xmax,ymin,ymax);
y
x
(399,299)
xmax,ymax
(px,py)
(0,0)
xmin,ymin
x
(799,599)
Curve=Polyline
GLU has many polynomials that are defined with
discrete point sets (like spheres, cylinders)
...
Pixels and Object Geometry
Pixel
Line
Rectangle
Drawing a Circle
Midpoint and modified midpoint algorithm.
Polygon Fill
Convex
Splitting
concave
polygon
Concave
E5
E4
<180o
E6
>180o
E1
E3
E2
(E1xE2)z>0
(E2xE3)z>0
(E3xE4)z<0
(E4xE5)z>0
(E5xE6)z>0
E5
E4
E6
E3
E1
E2
Slitting of a Polygon into Triangles
OpenGL guarentees correct rendering of polygons only if they
are convex. A triangle is a convex polygon.
But a polygon which has >3 vertices can be concave.
So, we must tesselate a given polygon into
triangles.The GLU library has a tesselator.
Inside-Outside Testing
We want fill inside of a polygon with constant color.
Flat and convex polygons are guarenteed to be
rendered correctly by OpenGL.
For nonflat polygons:
• We can work with their projections.
• Or we can use first three vertices to determine a flat plane
to use for the interrior.
For nonsimple flat polygons, we must decide how to
determine whether a given point is inside or
outside of the polygon (Jordan theorem).
Inside-Outside Testing
Odd-even rule (easy implementation)
Non-zero winding rule
= crossing, odd parity rule, even-odd rule
Clocwise edges
Crossed odd edges : inside point
Counterclocwise edges : –1
Crossed even edges : outside point
Don’t fill while total 0
–1
–1
+1
: +1
+1
Polygon Tables
VERTEX TABLE
EDGE TABLE
V1:
V2:
V3:
V4:
V5:
E1 :
E2 :
E3 :
E4 :
E5 :
E6 :
x1
x2
x3
x4
x5
y1
y2
y3
y4
y5
z1
z2
z3
z4
z5
V 1,
V 2,
V 3,
V 3,
V 4,
V 5,
POLYGON-SURFACE
TABLE
V2
V3
V1
V4
V5
V1
S1 : E1, E 2, E3
S2 : E 3, E4, E5, E6
V1
E6
E1
E3
S1
E2
V2
S2
V3
V5
E5
E4
V4
Vertex Arrays
typedef GLint vertex3[3];
vertex3 pt[8] = { (0,0,0), (0,1,0), (1,0,0), (1,1,0),
(0,0,1), (0,1,1), (1,0,1), (1,1,1) }
void quad(GLint n1, GLint n2, GLint n3, GLint n4) {
glBegin(GL_QUADS);
glVertx3iv( pt[ n1 ]); glVertx3iv( pt[ n2 ]);
glVertx3iv( pt[ n3 ]); glVertx3iv( pt[ n4 ]);
}
}
void cube( ) {
quad( 6,2,3,7 ); quad( 5,1,0,4 ); quad( 7,3,1,5 );
quad( 4,0,2,6 ); quad( 2,0,1,3 ); quad( 7,5,4,6 );
}
4
5
6
7
0
2
1
3
Plane Equations
(x,y,z)
Surface Normal
Surface (plane) equation :
Ax+By+Cz+D=0
y
V1
N = (V1-V2)x(V1-V3)
= [ A B C]T
V2
V3
If P is any point on the plane :
x
N•P+D = 0
N•P = -D
z
OpenGL: Front/Back Rendering
Each polygon has two sides,
front and back (Example)
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
OpenGL can render the two differently
The ordering of vertices in the list determines which is
the front side:
• When looking at the front side, the vertices go counterclockwise
– This is basically the right-hand rule
Culling
For convex objects, such as the cube, we could simply remove all
the faces pointing away from viewer.
• glEnable(GL_CULL);
Normally, if we use only the z-buffer algorithm, we pass 6n
polygons through the pipeline.
If we enable culling, only 3n polygons can pass through
the pipeline.
OpenGL Bitmap Functions
OpenGL Pixel Operations
glReadPixels( xmin, ymin, width, height, dataFormat, dataType, array);
glEnable( GL_COLOR_LOGIC_OP );
glLogicOp( LogicOp );
glDrawPixels( width, height, dataFormat, dataType, array );
dataFormat :
dataType :
LogicOp :
GL_RGB, GL_LUMINANCE, ...
GL_UNSIGNED_BYTE,
GL_COPY, GL_AND, GL_XOR, ...
with GL_COPY_INVERTED
Bitmap Fonts
Text in computer graphics is problematic.
• OpenGL : no text primitive (use window primitives)
• GLUT : minimal support
glRasterPosition2i(x1, y1); // First Character Position
glutBitmapCharacter(font, character);
glRasterPosition2i(x2, y2); // Second Character Position
glutBitmapCharacter(font, character);
Font:
GLUT_BITMAP_TIMES_ROMAN_10,
GLUT_STROKE_ROMAN, ...
Text
Stroke Text
• Use vertices to define line segments or curves that
outline each character.
Advantages:
• It is defined in the same way as other objects.
• It can be manipulated as other objects (rotate, make
bigger, smaller, etc.)
Disadvantages:
• Take up too much memory and
processing time
• Slow
Text
Raster Text
• Characters are defined as rectangles of bits
called bit blocks.
• Each bit block defines a single character by
the pattern of 0 and 1 bits in the block.
Text
Advantages:
• Simple and fast
• A raster character can be placed in the frame
buffer rapidly by a bit-block-transfer operation
(butblt)
• Does not take up too much memory and
processing time
Disadvantages:
• It can not be defined in the same way as other objects.
• It can not be manipulated as other objects.
Display Lists
Command block for often used objects.
Creation : GLuint blockNum;
blockNum = glGenLists( 1 );
glNewList( blockNum, GL_COMPILE );
... // OpenGL commands
glEndList( );
Execution: glCallList( blockNum );
Deletion:
glDeleteLists( blockNum, 1 );
UBI 516 Advanced omputer Graphics
Attributes of Graphics Primitives
( Chapter 4 )
OpenGL is a State Machine
Use current attribute, change it, use new attribute.
time1 :
glBegin( ... );
...
// Present color ?
glEnd( );
time2 :
glColor3fv( Red );
time3 :
glBegin( ... ); ... glEnd( );
// New color is red
Light
Visible Light in Electromagnetic Spectrum
red
FM
AM
104
green
yellow
106
infrared
microwave
108
1010
1012
blue
ultraviolet
visible
1014
1016
1018
violet
X-ray
1020
frequency
(Hz)
Color
C() : Strenght of wavelength  in the
color (energy distribution)
Additive color model
• C() = Sum of 3 color’s energy
Three-color theory
Blue
• C = T1 R + T2 G + T3 B
= Desired color
• T1, T2, T3 (Tristimulus Values) :
The numbers that specify
the values of R, G, B
M
C
Red
Y
Green
Human Visual System
Human Visual System
cones : red, green, blue receptors
sensitivy curve of a cone : Si()
brain perception values
three-color theory works for human eye
• (Ared, Agreen, Ablue)
three parameter of color
C ( )
Color Model
Color model
• Colors are different on a CRT, a film or a printer.
• Color models : RGB, CMY, YIQ, CIE, …
Color gamut
• The ranges of color produced by given system.
Color solid (= color cube) (Example)
• Color model based on three primary colors.
• Three primary color are three axis of a cube.
• Color gamut is set of all points on the cube.
RGB color model
Red, Green, Blue
• Tri-stimulus theory.
• Additive system : Adding ligths
to blank points.
• RGB cube has 3 axis (0 to 1).
Green
C
Y
Blue
M
Red
CMY, CMYK color model
Hard copy systems : printing, paiting ...
Yellow
G
• Subtractive system : Adding
colors to white surface (ex:paper)
R
• Convertion CMY to RGB, and RGB to CMY.
Cyan
B
Magenta
CMYK color model : K (black) for cartridge saving.
• Normally, cyan + magenta + yellow = black
• CMY can be used for dark gray.
• But, (K) ink will be used for black.
 C  1  R 
     
M  1  G ,
     

Y 
 
1
 
B

 R  1  C 
     
G  1  M
     

B
 
1
 
Y 

RGB vs. CMY
RGB color system
CMY color system
Additive primaries
Subtractive primiaries
• Adding ligths
For monitor, datashows
• R, G, B lights.
Graphics system generally
use RGB color system
• Adding color pigments
For printer, plotter ...
• C, M, Y inks.
Direct color system
Video card characteristics :
• Each pixel stored with RGB values
on frame buffer
• If frame buffer stores n bit for
every values
– 2n × 2n × 2n colors = 23n colors
• 3n = 24 : true color system
• 8 bit for every RGB values (0~255).
• Frame buffer wold have over 3MB of memory that would have to
be redisplay at video rates.
Direct color system
OpenGL functions
• Video card has 3n system
• OpenGL always use 0.0 ~ 1.0 values for red, green, blue.
• It means, OpenGL is independent of hardware.
glColor*();
• For setting colors.
Direct color system
RGBA color model
• RGB + A (alpha channel = opacity value)
• A = 1.0 is for invisible objects.
glColor4f(red, green, blue, alpha);
Clearing system to color
(i.e. pure white with alpha value of 0.0)
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);.
Indexed color system
If the frame buffer is limited in depth (i.e.
each pixel is only 8-bits deep), then we
can select colors by interpreting our
limited depth pixels as indices, rather
than as color values.
The indices point to a color-lookup table.
Example:
• For a frame buffer that has k-bits per pixel,
each pixel index is a value from 0 to 2k-1.
• Assume that the display can display
colors with an accuracy of m
(2m reds, 2m greens, 2m blues).
color-lookup table
Indexed color system
Why indexed color system ?
• Image and frame buffer size is reduced.
• Image file format : GIF, BMP use this system.
OpenGL functions :
• Sets current color to LUT[index].
glIndexi(index);
• Change color at LUT[index].
glutSetColor(index, red, green, blue);
OpenGL Blending
// destination
Draw the rectangle
(dr, dg, db, da)
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA,
GL_SRC_ALPHA);
// source
Draw the triangle
transparent
(sr, sg, sb, sa)
Don’t forget disabling !
glDisable(GL_BLEND);
opaque
Point Attributes
Point color & size :
Pixel size = 1
Line Attributes
Line color, width & style :
glLineWidth ( width );
glLineStipple ( repeatFactor, pattern );
// 0x1c47 for dash-dot pattern
// binary: 1110001000111
glEnable ( GL_LINE_STIPPLE );
...
glDisable ( GL_LINE_STIPPLE );
See page 192 for example code.
Line Attributes
Implementation of line width :
Smooth Lines ?
Fill-Area Attributes
Hollow, solid or patterned.
GLubyte fillPattern[ ] = {0xff,0,0xff,0,...}
glPolygonStipple ( fillPattern );
glEnable ( GL_POLYGON_STIPPLE );
...
glDisable ( GL_POLYGON_STIPPLE );
Smoothed Fill Patterns
OpenGL Polygon Modes
glPolygonMode( face, displayMode ); (Example)
Face: GL_FRONT, GL_BACK, GL_FRON_AND_BACK
Display Modes:
GL_POINT
GL_LINE
GL_FILL
(Only edge points)
(Wireframe)
(Default)
glFrontFace( faceOrder ); // GL_CW or GL_CCW
glEdgeFlag( flag ); // GL_TRUE or GL_FALSE for line mode
General Scan-Line Polygon-Fill Algorithm
Consider the following polygon:
D
B
C
A
E
F
How do we know whether a given pixel on the
scanline is inside or outside the polygon?
Polygon Rasterization
Inside-Outside Points
Point a, c : +1
Point b, d : -1
if zero then fill
For edge E ?
Do not changes
For edges A and C?
Flood-Fill Algorithm
Set pixel to fill color value until bounds.
• An interior point (x, y)
• A boundary color
• A fill color
Fill color
Boundary color
Interior point (x, y)
4-connected vs. 8-connected
Start point
4-connected
8-connected
Flood-Fill Algorithm
Rough algorithm for 4-connected case
void flood_fill4(int x, int y, Color fill, Color boundary) {
Color current = getPixel(x, y);
if (current  boundary && current  fill) {
setPixel(x, y, fill);
flood_fill4(x+1, y, fill, boundary); // recursion !
flood_fill4(x–1, y, fill, boundary);
flood_fill4(x, y+1, fill, boundary);
flood_fill4(x, y–1, fill, boundary);
}
}
Character Attributes
Anti-aliasing
Aliasing
Information loss due to low-frequency sampling
Jagged or stair-step appearance
Anti-aliasing
1
2
1
2
4
2
1 2 1
Subpixel
weighting
mask
Modified Bresenham’s line algorithm :
We put a pixels to yk, and lightened pixel yk-1 and yk+1.
OpenGL Anti-aliasing
In RGB display mode
glEnable(mode);
GL_POINT_SMOOTH
GL_LINE_SMOOTH
GL_POLYGON_SMOOTH
Also type these :
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA);
Push/Pop Attributes
glPushAttrib( mode );
Modes:
GL_ALL_ATTRIB_BITS
GL_POINT_BIT
GL_LINE_BIT
GL_POLYGON_BIT
glPopAttrib( );
Current Style
glPushAttrib()
// Change attribute
glPopAttrib()
Download