GOURAUD - Likbez in 3D-shading (shading algorithm faces by GOURAUD).

Buzz #17
(C) 1999 SaiR00S/EI


    The aim of this paper (and, perhaps, a series of articles, 
if you All this seems not without interest) is to conduct

small literacy classes in the 3D-shading, for me, softly
for instance, got sayings on this issue in the style:
"Pah-fa-a, aa-and hde moge? etc.

    Okay, enough about sad things, we go directly to the subject
today's story.

    Gouraud shading - a method of filling 3D
surface based on Lambertovskoy lighting model, and
which is its refinement (for details, anyone interested,
read about it in the literature on this topic or Clarify
the matter to full-time meeting with me for a couple of 
beers:). Well duck Now, with this model, light, light intensity,

incident on the plane determined by the angle between the light
and the vector normal to the surface. Bare podnapryagshis in
mathematics, we recall that the cosine of the angle between the 
two vectors defined as follows:


cos (AB) = (Xa * Xb + Ya * Yb + Za * Zb) / (| A | * | B |)

 where Xa, Xb, Ya, Yb, Za, Zb - the coordinates of all vectors
                         ditch A and B;

     | A |, | B | - the lengths of these vectors are computed

               quite simple:), as summa goemetricheskaya to
               ordinates Xa .. Za:


               | A | = sqrt (Xa ^ 2 + Ya ^ 2 + Za ^ 2),

               Similarly, we find | B |.


    In relation to Gouraud, it gives us information on how to
what color to paint over the ground.

    So what, do we need for the formation of surface
method of Gouraud:

    - Firstly - the coordinates of the vertices of the projected
landfill (we'll use the triangle as it is
the most simple, fast and it is the most mathematically correct,
For its three vertices can not lie in different planes, which
can not be said about other types of polygons);

    - Secondly - this is the value of light intensity in each 
of the vertices.



    To learn how to project the top of the plane, I
I will not tell, but on how to get the value
light intensity at the vertices, I shall detail.
Thus, for each face 3D object known, we have a normal. In each
top of the object known, we raiseth normal, which is the sum
normals of the faces surrounding the vertex. Then, we normalize
received normal, and taking any of the coordinate
light intensity about the axis formed by this
coordinate and the origin, we get the color value
this point.

    The following picture will help a little bit to understand 
the reading. 


                  (X1, Y1), a1

                    /

                  / \ A1 .. a3 - values
                / Radiation colors

              / / Points with coordinates
            / Coordinate of X1-X3,
(X2, Y2), a2 / \ Y1-Y3.

          \

            \ \

              \

                \ \

                  \

                    \ \

                      \

                        \ \

                          \

                            \ \

                           (X3, Y3), a3


    Further, linearly interpolated color from a1 to a2, a2 
​​from to a3, and from a1 to a3, we get the color values ​​for 
each vertical line. Who are not lazy, interpolation can spend

by straight-line equation:


                          Y = A * X + B,

resulting in a small brake. Much more convenient
interpolate on Brezenhemu (this you will get rid of the 
division). 

    After this interpoliruete color on each horizontal
line and at the same time draws a polygon:


           LD A, H; Writes a value of A
                     ; Of color

           ADD HL, DE; interpolate the color

                     ; On the horizontal

                     ; Line

           LD (BC), A; throw color

                     ; Buffer

           INC C; Proceed to the next
                     ; Yuschey point.


    That's it! Get a nice (as far as it can be beautiful
on the Spectrum) Gouraud shading.

    Before you throw it all kodit can be viewed as
it looks at VAV'a in Refresh (the part where a bagel Dangle
against the background of the eye), or on the AMIGA (although 
there is a net Gouraud rarity, all Phong, yes Phong ...) in the 
last demah from Ramses, but with Texture Mapping'om (strongly 
recommended view all of the best demos for 97-98 years, so for 
general development ...).


    For sim goodbye, success on the demoscene!