CG Module 2 - Part1
CG Module 2 - Part1
• Expanding the determinants, we can write the calculations for the plane
coefficients in the form
• Problem: It is possible that the coordinates defining a polygon facet may not be
contained within a single plane.
• Solution: We can solve this problem by
1. dividing the facet into a set of triangles;
2. or we could find an approximating plane for the vertex list.
Divide the vertex list into subsets, where each subset contains three vertices,
and calculate plane parameters A, B, C, D for each subset and take average.
3. Project the vertex list onto the coordinate planes. Parameter A proportional to
the area of polygon projection on the yz plane. B-xz, C-xy
Front and Back Polygon Faces
• The side of a polygon that faces into the object interior is called the back face,
and the visible, or outward, side is the front face .
• Every polygon is contained within an infinite plane that partitions space into
two regions.
1. Any point that is not on the plane and that is visible to the front face of a
polygon surface section is said to be in front of (or outside) the plane, and,
thus, outside the object.
2. And any point that is visible to the back face of the polygon is behind (or
inside) the plane.
• Plane equations can be used to identify the position of spatial points relative to
the polygon facets of an object.
• For any point (x, y, z) not on a plane with parameters A, B, C, D, we have
Ax + By + Cz + D != 0
• Thus, we can identify the point as either behind or in front of a polygon surface
contained within that plane according to the sign (negative or positive) of
Ax + By + Cz + D:
if Ax + B y + C z + D < 0,
the point (x, y, z) is behind the plane
if Ax + B y + C z + D > 0,
the point (x, y, z) is in front of the plane
These inequalities tests are valid in a right handed cartesian s/m, provided A,B,C,D were
calculated using coordinated positions selected counterclockwise order when viewing the
surface front to back direction.
Consider the unit
cube
To find the normal vector
select any three vertex
positions,V1,V2, and V3,
taken in counterclockwise
order when viewing from
outside the object toward the
inside.
And find A, B, C and D using
the equations already learnt.
We get A=1, B=0, C=0 and
D=-1 therefore N=(1,0,0)
Calculations for your reference..
• Orientation of a polygon surface in space can be
described with the normal vector for the plane containing
that polygon
• The normal vector points in a direction from inside the
plane to the outside; that is, from the back face of the
polygon to the front face.
• Thus, the normal vector for this plane is N = (1, 0, 0),
which is in the direction of the positive x axis.
• That is, the normal vector is pointing from inside the cube
to the outside and is perpendicular to the plane x = 1.
• The elements of a normal vector can also be obtained
using a vector cross product Calculation.
• We have a convex-polygon surface facet and a right-handed Cartesian system, we
again select any three vertex positions,V1,V2, and V3, taken in counterclockwise
order when viewing from outside the object toward the inside.
• Forming two vectors, one from V1 to V2 and the second from V1 to V3, we
calculate N as the vector cross-product:
N = (V2 − V1) × (V3 − V1)
• This generates values for the plane parameters A, B, and C.We can then obtain the
value for parameter D by substituting these values and the coordinates in
Ax + B y + C z + D = 0
• The plane equation can be expressed in vector form using the normal N and the
position P of any point in the plane as
N·P = −D
OpenGL Polygon Fill-Area Functions
• A glVertex function is used to input the coordinates for a single polygon vertex, and a complete polygon is described
with a list of vertices placed between a glBegin/glEnd pair.
• By default, a polygon interior is displayed in a solid color, determined by the current color settings we can fill a
polygon with a pattern and we can display polygon edges as line borders around the interior fill.
• There are six different symbolic constants that we can use as the argument in the glBegin function to describe
polygon fill areas
• In some implementations of OpenGL, the following routine can be more efficient than generating a fill rectangle
using glVertex specifications:
glRect* (x1, y1, x2, y2);
• One corner of this rectangle is at coordinate position (x1, y1), and the opposite corner of the rectangle is at position
(x2, y2).
• Suffix codes for glRect specify the coordinate data type and whether coordinates are to be expressed as array
elements.
• These codes are i (for integer), s (for short), f (for float), d (for double), and v (for vector).
• Example
glRecti (200, 100, 50, 250);
• If we put the coordinate values for this rectangle into arrays, we can generate the same square with the following
code:
int vertex1 [ ] = {200, 100};
int vertex2 [ ] = {50, 250};
1. Polygon
• With the OpenGL primitive constant GL POLYGON, we can display a single polygon fill area.
• Each of the points is represented as an array of (x, y) coordinate values:
glBegin (GL_POLYGON);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glVertex2iv (p6);
glEnd ( );
• A polygon vertex list must contain at least three vertices. Otherwise, nothing is displayed.
• A single convex polygon fill area generated with the primitive constant GL POLYGON.
2. Triangles
• Displays the trianlges.
• Three primitives in triangles, GL_TRIANGLES, GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP
glBegin (GL_TRIANGLES);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p6);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glEnd ( );
• Two unconnected triangles generated with GL_TRIANGLES
• In this case, the first three coordinate points define the vertices for one triangle, the next three points
define the next triangle, and so forth.
• For each triangle fill area, we specify the vertex positions in a counterclockwise order triangle strip
2.1 Four connected triangles generated with GL TRIANGLE STRIP.
glBegin (GL_TRIANGLE_STRIP);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p6);
glVertex2iv (p3);
glVertex2iv (p5);
glVertex2iv (p4);
glEnd ( );
• Assuming that no coordinate positions are repeated in a list of N vertices, we obtain N − 2 triangles in
the strip. Clearly, we must have N ≥ 3 or nothing is displayed.
• Each successive triangle shares an edge with the previously defined triangle, so the ordering of the
vertex list must be set up to ensure a consistent display.
1. First triangle (n = 1) would be listed as having vertices (p1, p2, p6).
2. Second triangle (n = 2) would have vertex ordering (p6, p2, p3).
3. Third triangle (n = 3) would have vertex ordering (p6, p3, p5).
4. Fourth triangle (n = 4) would be listed in the polygon tables with vertex ordering (p5, p3, p4).
2.2 Four connected triangles generated with GL TRIANGLE FAN.
• Another way to generate a set of connected triangles is to use the “fan” Approach
glBegin (GL_TRIANGLE_FAN);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glVertex2iv (p6);
glEnd ( );
• For N vertices, we again obtain N−2 triangles, providing no vertex positions are repeated, and we must list at
least three vertices be specified in the proper order to define front and back faces for each triangle correctly.
1. Triangle 1 is defined with the vertex list (p1, p2, p3);
2. Triangle 2 has the vertex ordering (p1, p3, p4);
3. Triangle 3 has its vertices specified in the order (p1, p4, p5);
4. Triangle 4 is listed with vertices (p1, p5, p6).
3. Quadrilaterals
• OpenGL provides for the specifications of two types of quadrilaterals.
• With the GL QUADS primitive constant and the following list of eight vertices, specified as
two-dimensional coordinate arrays, we can generate the display shown in Figure (a):
glBegin (GL_QUADS);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p3);
glVertex2iv (p4);
glVertex2iv (p5);
glVertex2iv (p6);
glVertex2iv (p7);
glVertex2iv (p8);
glEnd ( );
3.1 GL_QUAD_STRIP
• Rearranging the vertex list in the previous quadrilateral code example and changing the
primitive constant to GL_QUAD_STRIP, we can obtain the set of connected quadrilaterals:
glBegin (GL_QUAD_STRIP);
glVertex2iv (p1);
glVertex2iv (p2);
glVertex2iv (p4);
glVertex2iv (p3);
glVertex2iv (p5);
glVertex2iv (p6);
glVertex2iv (p8);
glVertex2iv (p7);
glEnd ( );
• For a list of N vertices, we obtain N/2− 1 quadrilaterals, providing that N ≥ 4. Thus,
1. First quadrilateral (n = 1) is listed as having a vertex ordering of (p1, p2, p3, p4).
2. Second quadrilateral (n=2) has the vertex ordering (p4, p3, p6, p5).
3. Third quadrilateral (n=3) has the vertex ordering (p5, p6, p7, p8).
Fill-Area Attributes
• We can fill any specified regions, including circles, ellipses, and other objects with curved
boundaries
Fill Styles
• A basic fill-area attribute provided by a general graphics library is the display style of the interior.
• We can display a region with a single color, a specified fill pattern, or in a “hollow” style by
showing only the boundary of the region
• We can also fill selected regions of a scene using various brush styles, color-blending
combinations, or textures.
• For polygons, we could show the edges in different colors, widths, and styles; and we
can select different display attributes for the front and back faces of a region.
• Fill patterns can be defined in rectangular color arrays that list different colors for
different positions in the array.
• An array specifying a fill pattern is a mask that is to be applied to the display area.
• The mask is replicated in the horizontal and vertical directions until the display area is
filled with non overlapping copies of the pattern.
• This process of filling an area with a rectangular pattern is called tiling, and a
rectangular fill pattern is sometimes referred to as a tiling pattern predefined fill
patterns are available in a system, such as the hatch fill patterns
• Hatch fill could be applied to regions by drawing sets of line segments to display either
single hatching or cross hatching
Color-Blended Fill Regions
• Color-blended regions can be
implemented using either
transparency factors to control the
blending of background and object
colors, or using simple logical or
replace operations as shown in
figure
• The linear soft-fill algorithm
repaints an area that was originally
painted by merging a foreground
color F with a single background
color B, where F != B.
• The current color P of each pixel within the area to be refilled is some linear combination of F and B:
P = tF + (1 − t)B
• Where the transparency factor t has a value between 0 and 1 for each pixel.
• For values of t less than 0.5, the background color contributes more to the interior color of the region
than does the fill color.
• If our color values are represented using separate red, green, and blue (RGB) components, each
component of the colors, with
P = (PR, PG, PB), F = (FR, FG, FB), B = (BR, BG, BB) is used
• We can thus calculate the value of parameter t using one of the RGB color components as follows:
Where k = R, G, or B; and Fk != Bk
• When two background colors B1 and B2 are mixed with foreground color F, the resulting pixel color
P is
P = t0F + t1B1 + (1 − t0 − t1)B2
General Scan-Line Polygon-Fill Algorithm
• Where Δx and Δy are the differences between the edge endpoint x and y
coordinate values.
• Thus, incremental calculations of x intercepts along an edge for successive
scan lines can be expressed as
• To perform a polygon fill efficiently, we can first store the polygon boundary in a sorted edge table
that contains all the information necessary to process the scan lines efficiently.
• Proceeding around the edges in either a clockwise or a counterclockwise order, we can use a bucket
sort to store the edges, sorted on the smallest y value of each edge, in the correct scan-line positions.
• Only non horizontal edges are entered into the sorted edge table.
• Each entry in the table for a particular scan line contains the maximum y value for that edge, the x-
intercept value (at the lower vertex) for the edge, and the inverse slope of the edge. For each scan
line, the edges are in sorted order from left to right We process the scan lines from the bottom of the
polygon to its top, producing an active edge list for each scan line crossing the polygon boundaries.
• The active edge list for a scan line contains all edges crossed by that scan line, with iterative
coherence calculations used to obtain the edge intersections
• Implementation of edge-intersection calculations can be facilitated by storing Δx and Δy values in
the sorted edge list
Explain scan line polygon fill algorithm determine the content of the
active edge table to fill the polygon with vertices A(2,4) B(4,6) C(4,1)
y=1 to y=6
OpenGL Fill-Area Attribute Functions
• Polygon edge flags can also be specified in an array that could be combined or associated with a vertex
array.
• The statements for creating an array of edge flags are
glEnableClientState (GL_EDGE_FLAG_ARRAY);
glEdgeFlagPointer (offset, edgeFlagArray);
OpenGL Front-Face Function
• We can label selected surfaces in a scene independently as front or back
with the function
glFrontFace (vertexOrder);
• If we set parameter vertex Order to the OpenGL constant GL_CW, then a
subsequently defined polygon with a clockwise ordering.
• The constant GL_CCW labels a counterclockwise ordering of polygon
vertices as front facing, which is the default ordering, for its vertices is
considered to be front-facing
• Module 2- Part 1 completed, From next
Thank you class Module 2- par 2