EE TECHNICAL REPORT #CSP-97-001 TRIANGLE FILL ALGORITHM Laurence G. Hassebrook

advertisement

EE TECHNICAL REPORT #CSP-97-001

TRIANGLE FILL ALGORITHM

Laurence G. Hassebrook

Department of Electrical Engineering

University of Kentucky

6/26/2006 1

ABSTRACT

Given a set of vertices formatted as an M N vertex (i.e., *.vtx) matrix form we would like to generate filled triangles to be plotted to a display or print matrix. Unlike the input data, the output is only stored to a MATRIX file for printing or the screen for viewing.

1. PROCESS OVERVIEW

The outer loop loads a single buffer, N long, denoted by v buff

[ ]

as follows:

INITIALIZE ZBUFFER for(m=0;m<M;m++)

{

for(n=0;n<N;n++)

{

READ IN VERTEX m,n: x,y,z: r,g,b

GEOMETRIC TRANSFORM of x,y,z;r,g,b to integer x, y, z and intensity. i = a r a g a b where for black and white all coefficients are equal. v now

= [ x y z i

]

If(m>0)

{

GENERATE TRIANGLE L from v buff

[ n − 1

]

, v b 4

and v now

.

PROCESS TRIANGLE

PLOT POINTS

GENERATE TRIANGLE R from v buff

[ ]

, v buff

[ n − 1

]

, and v now

.

PROCESS TRIANGLE

PLOT POINTS

} v buff

[ n

] v b 4

and v b 4

} v buff

[

N

]

= v now

; Load previous point into buffer. v now

; Load very last point into buffer.

}

1.1 TRIANGLE PROCESSING OVERVIEW

P R O C E S S T R I A N G L E

Each triangle is processed for shading and fill. The Z buffer is continually

C H E C K

B O U N D A R I E S

C H E C K

F I L L updated for all fill points in each triangle while various checks are performed to determine whether or not to plot triangles and whether or not to fill them.

The details are discribed in section 2 for the process shown to the left.

N O

F I L L

F I L L

C H E C K P L O T

S K I P P L O T

N E X T T R I A N G L E

6/26/2006 2

2. GENERATE TRIANGLE L AND R

The process protocol of generating and filling the triangle faces structured to achieve highest numerical efficiency.

2.1 DEFINE L and R Vertices

An identical process is repeated for triangle L and R with the only difference being which vertices are loaded as the triangle.

• For Triangle L (left),

= buff

[ n − 1

]

, B v b 4

and C = v now

.

• For Triangle R (right), now

,

= buff

[ ] and

= buff

[ n − 1

]

.

A diagram of the “L” and “R” triangles with associated vertices is shown in Fig. 1. v buff

[ n − 1

] v buff

[ ]

R

L v b 4 v now

Figure 1: Vertices used in triangle generation.

2.2 CHECK DECISION BOUNDARIES

Given A , B and C , several decisions are made in regards to what or what not to display based on decision boundaries. No points, and therefore no triangles will be plotted:

• if A and B and C intensities are not above a minimum intensity level.

• if A and B and C are not inside the “sea, sky, north, east, west and south” cubical volume.

• if geometrically transformed A and B and C are not inside the crop area where x ll

, y ll

is the lower left corner and x ur

, y ur

is the upper right corner of the cropped area.

The “if” statement structure for A and B and C would be:

6/26/2006 3

CHECK1 if

( min

)

REQUEST NEXT TRIANGLE else GOTO CHECK2

CHECK2 if

( ( x < x west

) ( > x east

) ( < y south

) ( > y north

) ( < sea

) (

> z sky

) ) then REQUEST NEXT TRIANGLE else GOTO CHECK3

CHECK3

GEOMETRIC TRANSFORM OF VERTICES if

( ( x < x or x > ) ( < ) ( > y ur

) )

REQUEST NEXT TRIANGLE else GOTO NO FILL TRIANGLE

2.3 NO FILL TRIANGLES

Given that at least one vertex is within the region to be displayed there is one more aspect to evaluate before beginning the fill process. If A and B and C consist of two adjacent pairs then there is no reason to fill a triangle.

The is two cases of that satisfy this condition. The first case of vertex positions which would not require triangle fill are ones in a right triangle orientation as shown in Fig. 2.

Figure 2: Set of vertex positions that do not require fill.

The second case is where the vertices are in a straight line as shown in Fig. 3.

Figure 3: In line arrangements that do not require fill.

The logical test for the structures in Fig. 2 and Fig. 3 are:

6/26/2006 4

CHECK4 if

( ( x

A

− x and x

A

− x and x

B

− x and y

A

− y and y

A

− y and y

B

− y

C then goto FILL TRIANGLES else GOTO CHECK5

CHECK5 if

( ( x A xB = xC

)

( max

{

, ,

}

≤ 2

)

( min

{

,

)

≤ 1

)

,

}

≥ 1

) )

( ( and y A = y B = yC

)

( max

{ }

≤ 2

)

( min

{ }

≥ 1

) ) then PLOT POINTS else goto FILL TRIANGLE

2.4 FILL TRIANGLE

In filling the triangle we would like to be numerically efficient, minimize the amount of necessary RAM, minimize hard drive access and fill in all pixels within the triangle region. The quality of the fill represents an interpolation between the vertices of both z values and intensity values. For color systems, each color would be interpolated as the intensity is for this black and white system. The idea is to select one of the axes, x or y, as a reference dimension and represent all other dimensions as a function of the reference dimension. Hidden surface capability is achieved by a “z buffer” described later. Since the maximum dimensions for display are typically the screen we will use the y dimension as the reference one.

For every triangle plotted the minimum and maximum x values are initialized once as: x min

[ ] = ∞ , x max

[ ] = −∞ for m = 0 1 K M y

− 1 )

The variables for the A, B and C vertices are x x

A

,

, y

A

,

, z

A

, and

, and i i

A x

B

C

, y

B

C

, z

B

C

, and i

B

C

A line is formed for segments a, b and c shown in Fig. 4.

A

6/26/2006 c b

B a

Figure 4: Vertices and edges of triangle.

C

5

For segment “a” the x values are mapped by first determining the minimum and maximum y values as m min

= {

,

C

} and m max

=

{

C

}

For the other variables we determine the slope and intercept such that x

B

= a y

B

+ b x and x

C

= a y + b x

From the above equations we find a x

=

(

( x

B

− x y

B

− y

C

C

)

) and b x

= x

B

− a y

B

The same procedure is used for “z” and “i” such that a z

=

( z

B

− a i

= i

B

− i

C z

C

/

) (

( ) ( y

B

− y

C y

B

− y

C

)

)

and

and b z

= z

B

− a y

B b i

= i

B

− a y

Given the coefficients for all the dimensions as functions of y, three array pairs are loaded with values. These arrays are: x min

[ ]

, max

[ ]

, = K M y

− 1 )

[ ] [ ] = K M y

− 1 )

[ ] [ ] = K M y

− 1 )

The three arrays are loaded with an outer loop over all three edges of the triangle as follows:

6/26/2006 6

FILL ARRAY PAIRS for(e=0;e<3;e++)

{

Use edge a for e=0, b for e=1 and c for e=2. m m

for min max

(

=

=

{

{ e 1 e 1 e 2

, e

}

2

} m = m min

; ≤ max

; m + +

)

{ x = + x

CHECK FOR x BEING MORE LEFT THAN ANY PREVIOUS POINT AT y=m

if

( x ≤ x min

[ ] )

{ x min m = x

[ ]

[ ]

[ ]

=

= a m b a m b i z

}

CHECK FOR x BEING MORE RIGHT THAN ANY PREVIOUS POINT AT y=m

if

( x ≥ x max

[ ] )

{ x max

[ ]

[ ]

[ ]

=

=

= x a m b a m b i z

}

}

}

2.5 PLOT OUT ARRAYS

Loop through y values and plot out arrays.

6/26/2006 7

for

( m = 0; m < ; + +

)

{

if

( x min

[ ] ≤ x max

[ ] )

{

Interpolate “z” and “i” such that

if

( x min

[ ]

≠ x max

[ ] )

{ a a i z

=

=

(

(

[ ] [ ]

[ ] [ ] )

/

)

/

( x

( x max max

[ ] m m

− x x min min

[ ] m m

)

)

and

and b i b z

=

= i m

[ ]

[ ] −

− a x a x max max

[ ] m

}

else

{

Single point to be plotted such that a z

= 0 and b z

= z m a i

= 0 and b i

=

[ ]

[ ]

}

PLOT OUT Z AND INTENSITY

for

( n = x min

[ ]

; ≤ x max

[ ]

; + +

)

{

= y m z a n b z i = a n b i

PLOT POINTS

}

}

}

3. PLOT POINTS

For every point being plotted there are three final checks prior to plotting. The first check is that the point intensity exceeds a minimum threshold level. The second check is that the z value of the point is in front of any previous points. The third check determines whether the point is in the cropped area. The ordering of which check is performed first depends on the likelihood of rejection as well the numerical cost of checking the point. For example, the intensity is a single byte value so it requires little computation so it is performed first. The z value may require an access to a file if the zbuffer is not stored in RAM so it is performed second. The cropping area requires

4 comparisons but rarily occures so it is done last. A more specific or even dynamic ordering could be obtained if the numerical costs and likelihood statistics were known. A logical sequence is shown as follows:

6/26/2006 8

CHECKA if

( min

)

RETURN WITHOUT PLOTTING else GOTO CHECKB

CHECKB if

( ( x < x or x >

) (

<

) (

> y ur

) ) else GOTO CHECKC

CHECKC if

(

≥ buffer

)

RETURN WITHOUT PLOTTING

PLOT x,y,z,i point

RETURN WITHOUT PLOTTING

6/26/2006 9

Download