Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail:...
Transcript of Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail:...
![Page 1: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/1.jpg)
Shading in OpenGL
Shandong University Software College
Instructor: Zhou Yuanfeng E-mail: [email protected]
![Page 2: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/2.jpg)
2
Objectives
• Introduce the OpenGL shading functions•Discuss polygonal shading
Flat
Smooth
Gouraud
![Page 3: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/3.jpg)
3
Steps in OpenGL shading
1. Enable shading and select model
2. Specify normals
3. Specify material properties
4. Specify lights
![Page 4: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/4.jpg)
4
Normals
• In OpenGL the normal vector is part of the state
• Set by glNormal*()glNormal3f(x, y, z);glNormal3fv(p);
• Usually we want to set the normal to have unit length so cosine calculations are correct
Length can be affected by transformations
Note that scaling does not preserved lengthglEnable(GL_NORMALIZE) allows for auto-
normalization at a performance penalty
![Page 5: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/5.jpg)
5
Normal for Triangle
p0
p1
p2
nPlane n · (p - p0 ) = 0
n = (p2 - p0 ) × (p1 - p0 )
normalize n n/ |n|
p
Note that right-hand rule determines outward face
![Page 6: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/6.jpg)
6
Enabling Shading
• Shading calculations are enabled byglEnable(GL_LIGHTING)
Once lighting is enabled, glColor() ignored glEnable(GL_COLOR_MATERIAL);
• Must enable each light source individuallyglEnable(GL_LIGHTi) i=0,1…..
• Can choose light model parametersglLightModeli(parameter, GL_TRUE)
•GL_LIGHT_MODEL_LOCAL_VIEWER • do not use simplifying distant viewer assumption in
calculation (infinite viewer)•GL_LIGHT_MODEL_TWO_SIDED • shades both sides of polygons independently
![Page 7: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/7.jpg)
7
Defining a Point Light Source
• For each light source, we can set an RGBA for the diffuse, specular, and ambient components, and for the position
GLfloat diffuse0[]={1.0, 0.0, 0.0, 1.0};GLfloat ambient0[]={1.0, 0.0, 0.0, 1.0};GLfloat specular0[]={1.0, 0.0, 0.0, 1.0};Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};
glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightv(GL_LIGHT0, GL_POSITION, light0_pos);glLightv(GL_LIGHT0, GL_AMBIENT, ambient0);glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0);glLightv(GL_LIGHT0, GL_SPECULAR, specular0);
![Page 8: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/8.jpg)
8
Distance and Direction
• The source colors are specified in RGBA
• The position is given in homogeneous coordinates
If w = 1.0, we are specifying a finite location
If w = 0.0, we are specifying a parallel source with the given direction vector
• The coefficients in the distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms). Change bya = 0.80;glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a);
1/(a+bdL+cdL2)
![Page 9: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/9.jpg)
9
Spotlights
•Use glLightv to set Direction GL_SPOT_DIRECTION Cutoff GL_SPOT_CUTOFF Attenuation GL_SPOT_EXPONENT
• Proportional to cos
![Page 10: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/10.jpg)
10
Global Ambient Light
•Ambient light depends on color of light sources
A red light in a white room will cause a red ambient term that disappears when the light is turned off
•OpenGL also allows a global ambient term that is often helpful for testingglLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)
![Page 11: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/11.jpg)
11
glLightfv(light, pname, param)
![Page 12: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/12.jpg)
12
LIGHT_TYPE_SPOT(Demo Ogl-lighting)
• case LIGHT_TYPE_SPOT: {• GLfloat diffuse_light2[] = { 1.0f, 1.0f, 1.0f, 1.0f };• GLfloat position_light2[] = { 2.0f*x, 2.0f*y, 2.0f*z, 1.0f };• GLfloat spotDirection_light2[] = { x, y, z };• GLfloat constantAttenuation_light2[] = { 1.0f };• glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse_light2 );• glLightfv( GL_LIGHT2, GL_POSITION, position_light2 );• glLightfv( GL_LIGHT2, GL_SPOT_DIRECTION,
spotDirection_light2 );• glLightfv( GL_LIGHT2, GL_CONSTANT_ATTENUATION,
constantAttenuation_light2 );• glLightf( GL_LIGHT2, GL_SPOT_CUTOFF, 45.0f );• glLightf( GL_LIGHT2, GL_SPOT_EXPONENT, 25.0f );• }
• case LIGHT_TYPE_SPOT: {• GLfloat diffuse_light2[] = { 1.0f, 1.0f, 1.0f, 1.0f };• GLfloat position_light2[] = { 2.0f*x, 2.0f*y, 2.0f*z, 1.0f };• GLfloat spotDirection_light2[] = { x, y, z };• GLfloat constantAttenuation_light2[] = { 1.0f };• glLightfv( GL_LIGHT2, GL_DIFFUSE, diffuse_light2 );• glLightfv( GL_LIGHT2, GL_POSITION, position_light2 );• glLightfv( GL_LIGHT2, GL_SPOT_DIRECTION,
spotDirection_light2 );• glLightfv( GL_LIGHT2, GL_CONSTANT_ATTENUATION,
constantAttenuation_light2 );• glLightf( GL_LIGHT2, GL_SPOT_CUTOFF, 45.0f );• glLightf( GL_LIGHT2, GL_SPOT_EXPONENT, 25.0f );• }
![Page 13: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/13.jpg)
13
Moving Light Sources (Demo)
•Light sources are geometric objects whose positions or directions are affected by the model-view matrix
•Depending on where we place the position (direction) setting function, we can
Move the light source(s) with the object(s)
Fix the object(s) and move the light source(s)
Fix the light source(s) and move the object(s)
Move the light source(s) and object(s) independently
![Page 14: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/14.jpg)
14
• void display(GLint spin){ GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; glPushMatrix(); glTranslatef(0.0, 0.0, -5.0); glPushMatrix(); glRotated((GLdouble) spin, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION,
light_position); glPopMatrix(); auxSolidTorus(0.275, 0.85); glPopMatrix(); glFlush();
• } // 旋转光源
• void display(GLint spin){ GLfloat light_position[] = { 0.0, 0.0, 1.5, 1.0 }; glPushMatrix(); glTranslatef(0.0, 0.0, -5.0); glPushMatrix(); glRotated((GLdouble) spin, 1.0, 0.0, 0.0); glLightfv(GL_LIGHT0, GL_POSITION,
light_position); glPopMatrix(); auxSolidTorus(0.275, 0.85); glPopMatrix(); glFlush();
• } // 旋转光源
![Page 15: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/15.jpg)
15
Material Properties
• Material properties are also part of the OpenGL state and match the terms in the modified Phong model
• Set by glMaterialv()
GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};GLfloat shine = 100.0glMaterialf(GL_FRONT, GL_AMBIENT, ambient);glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);glMaterialf(GL_FRONT, GL_SPECULAR, specular);glMaterialf(GL_FRONT, GL_SHININESS, shine);
![Page 16: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/16.jpg)
16
Front and Back Faces
• The default is shade only front faces which works correctly for convex objects
• If we set two sided lighting, OpenGL will shade both sides of a surface
• Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf
back faces not visible back faces visible
![Page 17: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/17.jpg)
17
Emissive Term
•We can simulate a light source in OpenGL by giving a material an emissive component
•This component is unaffected by any sources or transformations
GLfloat emission[] = 0.0, 0.3, 0.3, 1.0);glMaterialf(GL_FRONT, GL_EMISSION, emission);
![Page 18: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/18.jpg)
18
Transparency
•Material properties are specified as RGBA values
•The A value can be used to make the surface translucent
•The default is that all surfaces are opaque regardless of A
•Later we will enable blending and use this feature
![Page 19: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/19.jpg)
19
Efficiency
• Because material properties are part of the state, if we change materials for many surfaces, we can affect performance
• We can make the code cleaner by defining a material structure and setting all materials during initialization
• We can then select a material by a pointer
typedef struct materialStruct { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shineness;} MaterialStruct;
![Page 20: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/20.jpg)
20
IMPLEMENTATION I
![Page 21: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/21.jpg)
21
Objectives
• Introduce basic implementation strategies•Clipping •Scan conversion
![Page 22: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/22.jpg)
22
Overview
•At end of the geometric pipeline, vertices have been assembled into primitives
•Must clip out primitives that are outside the view frustum
Algorithms based on representing primitives by lists of vertices
•Must find which pixels can be affected by each primitive
Fragment generation Rasterization or scan conversion
![Page 23: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/23.jpg)
23
Required Tasks
•Clipping•Rasterization or scan conversion• We can get the pixels based on vertices
•Some tasks deferred until fragement processing
Hidden surface removal
Antialiasing
ProjectionFragmentsClippingShading
Surface hiddenTextureTransparency
![Page 24: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/24.jpg)
24
Rasterization Meta Algorithms
•Consider two approaches to rendering a scene with opaque objects
•For every pixel, determine which object that projects on the pixel is closest to the viewer and compute the shade of this pixel
Ray tracing paradigm
•For every object, determine which pixels it covers and shade these pixels
Pipeline approach Must keep track of depths
![Page 25: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/25.jpg)
25
Clipping
• 2D against clipping window• 3D against clipping volume• Easy for line segments polygons• Hard for curves and text
Convert to lines and polygons first
![Page 26: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/26.jpg)
26
Clipping 2D Line Segments
•Brute force approach: compute intersections with all sides of clipping window
Inefficient: one division per intersection
![Page 27: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/27.jpg)
27
Cohen-Sutherland Algorithm
• Idea: eliminate as many cases as possible without computing intersections
•Start with four lines that determine the sides of the clipping window
x = xmaxx = xmin
y = ymax
y = ymin
![Page 28: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/28.jpg)
28
The Cases
• Case 1: both endpoints of line segment inside all four lines
Draw (accept) line segment as is
• Case 2: both endpoints outside all lines and on same side of a line
Discard (reject) the line segment
x = xmaxx = xmin
y = ymax
y = ymin
![Page 29: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/29.jpg)
29
The Cases
•Case 3: One endpoint inside, one outside Must do at least one intersection
•Case 4: Both outside May have part inside
Must do at least one intersection
x = xmaxx = xmin
y = ymax
![Page 30: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/30.jpg)
30
Defining Outcodes
•For each endpoint, define an outcode
•Outcodes divide space into 9 regions•Computation of outcode requires at most 4 subtractions (8 times for two endpoint)
b0b1b2b3
b0 = 1 if y > ymax, 0 otherwiseb1 = 1 if y < ymin, 0 otherwiseb2 = 1 if x > xmax, 0 otherwiseb3 = 1 if x < xmin, 0 otherwise
![Page 31: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/31.jpg)
Outcodes application
•Case I: o1 , o2
•AB: o1 = o2 = 0
31
![Page 32: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/32.jpg)
Outcodes application
•Case II: o1 , o2
•CD: o1 ≠ 0, o2 = 0
32
![Page 33: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/33.jpg)
Outcodes application
•Case III: o1 , o2
•EF: o1 & o2 ≠ 0
33
![Page 34: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/34.jpg)
Outcodes application
•Case IV: o1 , o2
•GH and IJ: o1 & o2 = 0
•Compute the intersection points;•Re-compute the code of intersection points.
34
![Page 35: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/35.jpg)
35
The Cohen-Sutherland Line-Clipping Algorithm (2)
• To perform trivial and reject tests, we extend the edges of the clip rectangle to divide the plane into nine regions, and encode each region a 4-bit code.
• If both 4-bit codes of the endpoint are zero, the line is Trivially Accepted
• If both endpoints lie in the outside halfplane of a particular edge, the line is Trivially rejected, and the logical AND of the endpoints is not zero.
![Page 36: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/36.jpg)
36
The Cohen-Sutherland Line-Clipping Algorithm(3)
• Compute the codes of both endpoints and check for trivial acceptance and rejection.
• If neither test is successful, find an endpoint that lies outside, and then test the code to find the edge that is crossed and to determine the corresponding intersection point.
• Clip off the line segment from the outside endpoint to the intersection point by replacing the outside endpoint with the intersection point, and compute the code of this new endpoint to prepare for the next iteration.
![Page 37: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/37.jpg)
37
Efficiency
• In many applications, the clipping window is small relative to the size of the entire data base
Most line segments are outside one or more side of the window and can be eliminated based on their outcodes
• Inefficiency when code has to be reexecuted for line segments that must be shortened in more than one step
![Page 38: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/38.jpg)
38
Cohen Sutherland in 3D
• Use 6-bit outcodes
• When needed, clip line segment against planes
![Page 39: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/39.jpg)
39
Parametric Lines and Intersections
For L1
x=x0l1 + t(x1l1 – x0l1)y=y0l1 + t(y1l1 – y0l1)For L2
x=x0l2 + t(x1l2 – x0l2)y=y0l2 + t(y1l2 – y0l2)
The Intersection Point:
x0l1 + t1 (x1l1 – x0l1) = x0l2 + t2 (x1l2 – x0l2) y0l1 + t1 (y1l1 – y0l1) = y0l2 + t2 (y1l2 – y0l2)
![Page 40: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/40.jpg)
40
Clipping Lines
• Clipping Lines by Solving Simultaneous Equations
Two sets of simultaneous equations of this parametric form can be solved for parameters tedge for the edge and tline for the line segment.
The value of tedge and tline can be checked to see whether both lie in [0,1]; if they do, the intersection point is a true clip-rectangle intersection.
Shortcoming: considerable calculation and testing
![Page 41: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/41.jpg)
41
A Parametric Line-Clipping Algorithm(1) –Liang-Barsky
1.For each clip edge (to upright rectangle), we can easily calculate a value t corresponding to the intersection point between the edge and the line segment.
2.We get four values of in t total.
3.Intersections can be classified as PE or PL ( How ?) PE: Point Entering the clipping rectangle
PL: Point Leaving the clipping rectangle
![Page 42: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/42.jpg)
42
A Parametric Line-Clipping Algorithm(2) –Liang-Barsky
3.on the basis of the angle between P0P1 and the normal of the edge.
< 90, PL (Ni . D >0 )
>90, PE (Ni . D <0 )
4.Divide t into two groups({t0’, t0
”} {t1’, t1
”}) according to its corresponding intersection belongs to PE or PL.
5.We get t0 = max (t0
’, t0”,0)
t1 = min(t1’, t1
”,1)If t0 < t1 , the line segment between [t0 , t1]
is visible, otherwise, the whole line is invisible
![Page 43: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/43.jpg)
43
A Parametric Line-Clipping Algorithm(3) –Liang-Barsky
• precalculate Ni for each edge• for (each line segment to be clipped){
If (P0 = P1 )• Line is degenerate so clip as a point;
else{• te =0; tl =1;• for (each candidate intersection with a clip edge){
– if (Ni * D !=0) { // ignore edges parallel to line• calculate t;• use sign of Ni * D to categorize as PE or PL;• if (PE) te =max(te. t);• if (PL) tl =min(tl. t);
– }• }• if (te > tl ) return nil;• else return P(te) and P(tl) as true clip intersections;
}• }
![Page 44: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/44.jpg)
44
Advantages
•Can accept/reject as easily as with Cohen-Sutherland
•Using values of , we do not have to use algorithm recursively as with C-S
•Extends to 3D
![Page 45: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/45.jpg)
45
Clipping and Normalization
•General clipping in 3D requires intersection of line segments against arbitrary plane
•Example: oblique view
![Page 46: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/46.jpg)
46
Plane-Line Intersections
)(
)(
12
1
ppn
ppna o
0n)))(((( 1210 PPaPP
![Page 47: Shading in OpenGL Shandong University Software College Instructor: Zhou Yuanfeng E-mail: yuanfeng.zhou@gmail.com.](https://reader035.fdocuments.in/reader035/viewer/2022062217/5697bfba1a28abf838ca04f9/html5/thumbnails/47.jpg)
47
Normalized Form
before normalization after normalization
Normalization is part of viewing (pre clipping)but after normalization, we clip against sides ofright parallelepiped
Typical intersection calculation now requires onlya floating point subtraction, e.g. is x > xmax
top view