Professional Documents
Culture Documents
CHAPTER6 RASTERIZATION
Line Segments Clipping
Cohen-Sutherland Algorithm
b0b1b2b3
b0 = 1 if y > ymax, 0 otherwise
b1 = 1 if y < ymin, 0 otherwise
b2 = 1 if x > xmax, 0 otherwise
b3 = 1 if x < xmin, 0 otherwise
outcode(A) = outcode(B) = 0Accept line segment
outcode (C) = 0, outcode(D) ≠ 0 Location of 1 in outcode(D)
determines which edge to intersect with
outcode(E) logically ANDed with outcode(F) (bitwise) ≠ 0 out size
GH and IJ: same outcodes, neither zero but logical AND yields zero Shorten line segment by intersecting with
one of sides of window
Cohen Sutherland in 3DUse 6-bit outcodes
Liang-Barsky Clipping
p(α) = (1-α)p1+ αp2 , 1 ≥ α ≥ 0
1> α4 > α3 > α2 > α1 > 0 Intersect right, top, left, bottom: shorten
1> α4 > α2 > α3 > α1 > 0 Intersect right, left, top, bottom: reject
Plane-Line Intersections
p(a)=(1-a)p1 +ap2
n.(p(a)-p0 )=0
a=(n*(p0-p1))/(n*( p2-p1))
Polygon Clipping
Clipping as a Black Box
Nonconvextessellation
Hidden Surface Removal :Object-space approach: use pair-wise testing between polygons (objects)
Bresenham
∆x, ∆y, 2∆y, 2 ∆y- 2∆x
p0= 2∆y- ∆x
k=0
for(0-k){
If pk < 0{
Plot(xk+1, yk)
pk+1 = pk + 2∆y
}else
Plot(xk+1, yk+1)
pk+1 = pk + 2∆y- 2∆x
}
Polygon rasterization
spatial resolution (n x m)
depth k
Image Formats
Reading the Header
Reading the Data
Scaling the Image Data
Texture Mapping
I ntroduce Mapping Methods
Texture Mapping
Environment Mapping
Bump Mapping
Coordinate Systems
Parametric coordinates
Texture coordinates
Object or World Coordinates
Window Coordinates
Two-part mapping
Map the texture to a simple intermediate surface
Map from intermediate object to actual object
Normals from intermediate to actual
Normals from actual to intermediate
Vectors from center of intermediate
Applying Textures I
1. specify the texture
Loan Nguyen BKIT08.net Page 4
read or generate image
assign to texture
Glubyte my_texels[512][512];// Define a texture image from an array of texels in CPU memory
enable texturing
glEnable(GL_TEXTURE_2D)
glTexImage2D( target, level, components,w, h, border, format, type, texels );
// Define Image as a Texture
gluScaleImage( format, w_in, h_in,type_in, *data_in, w_out, h_out,type_out, *data_out );
// If dimensions of image are not powers of 2
2. assign texture coordinates to vertices
Proper mapping function is left to application
3. specify texture parameters
Wrapping parameters determine what happens if s and t are outside the (0,1) range
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP )
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )
Filter modes allow us to use area averaging instead of point samples
Magnification and Minification
Blending Equation
Define source and destination blending factors for each RGBA component
s = [sr, sg, sb, sα]
d = [dr, dg, db, dα]
Suppose that the source and destination colors are
b = [br, bg, bb, bα]
c = [cr, cg, cb, cα]
Blend as
c’ = [br sr+ cr dr, bg sg+ cg dg , bb sb+ cb db , bα sα+ cα dα ]
glEnable(GL_BLEND)
glBlendFunc(source_factor, destination_factor)
//GL_ZERO, GL_ONE
//GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
//GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA
Ex:
1.opaque background color (R0,G0,B0,1)
2. want to blend in a translucent polygon with color (R 1,G1,B1,α1)
3. Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as the source and destination blending
factors R’1 = α1 R1 +(1- α1) R0, ……
Fog:
Blend source color Cs and fog color Cf by Cs’=f Cs + (1-f) Cf
GLfloat fcolor[4] = {……}:
glEnable(GL_FOG);
glFogf(GL_FOG_MODE, GL_EXP);
Antialiasing
Use average area α1+α2-α1α2 as blending factor
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLSL
Use:
gl_Position
gl_ProjectionMatrix
gl_ModelViewMartrix
gl_Vertex
gl_FrontColor
Data Types
void main(void)
{
gl_FragColor = gl_Color;
}
//Vertex Shader
const vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
varying vec3 color_out;
void main(void)
{
gl_Position =gl_ModelViewProjectionMatrix*gl_Vertex;
color_out = red;
}
GLuint myProgObj;
myProgObj = glCreateProgram();
/* define shader objects here */
glUseProgram(myProgObj);
glLinkProgram(myProgObj);
//Shader Reader
#include <stdio.h>
char* readShaderSource(const char* shaderFile)
{
FILE* fp = fopen(shaderFile, "r");
char* buf;
long size;
if(fp == NULL) return(NULL);
fseek(fp, 0L, SEEK_END); /* end of file */
size = ftell(fp);
fseek(fp , )L, SEEK_SET); /* start of file */
buf = (char*) malloc(statBuf.st_size + 1 * sizeof(char));
//Particle System
uniform vec3 init_vel;
uniform float g, m, t;
Samplers
Provides access to a texture object
Defined for 1, 2, and 3 dimensional textures and for cube maps
//In shader:
uniform sampler2D myTexture;
Vec2 texcoord;
Vec4 texcolor = texture2D(mytexture, texcoord);
//In application:
texMapLocation =
glGetUniformLocation(myProg,“myTexture”);
glUniform1i(texMapLocation, 0);
/* assigns to texture unit 0 */