Coding - Texture Mapping - Implementation of SaiR00S/EI.

Scenergy #02
TEXTURE MAPPING-ESPECIALLY 4 SCENERGY


(C) 1999 SaiR00S/EI


   The essence of the linear texture mapping
is a linear interpolation
coordinates of the polygon and their corresponding
texture coordinates.

   As the landfill is most often
selected triangle, although described
following algorithm is suitable for any
landfill.

   Thus, the polygon is a triangle,
all of whose vertices have a pair of coordinates
(X, Y) and related textural
coordinates (U, V). By polygon, we
move from top to bottom and from left to right,
ie when moving from top to bottom we
increase at each vertical step
Y coordinate of the triangle at
simultaneous linear interpolation
X coordinate and texture coordinates (in
That will have the contour of the polygon and
corresponding contour triangle
texture), and when moving from left to right,
increasing coordinate of the X-coordinate of
the left border of the contour to the right to
unit, perform linear interpolation
texture and set the coordinates Xi, Yj
color value from the texture coordinates
Up, Vq.

   The first thing to do - it
sort the coordinates of the triangle
vmeste with texture coordinates in
ascending order of the coordinates of Y, ie


   If vert1.y> vert2.y then

          swap (vert1, vert2);

   If vert2.y> vert3.y then

          swap (vert2, vert3);

   If vert3.y> vert1.y then

          swap (vert3, vert1);


   Or (Lexis STORM'a):


        LD HL, (VERT1); Take X, Y coordinates
        LD DE, (VERT2); you have a triangle

        LD BC, (VERT3);

        EXX

        LD HL, (VERT1 +2); Take textural

        LD DE, (VERT2 +2); coordinates U, V

        LD BC, (VERT3 +2);

        EXX

        LD A, D

        CP H

        JR NC, NOSWAP1

        EX DE, HL

        EXX

        EX DE, HL

        EXX
NOSWAP1 LD A, B

        CP H

        JR NC, NOSWAP2

        LD A, L, L, C, C, A

        LD A, H, H, B, B, A

        EXX

        LD A, L, L, C, C, A

        LD A, H, H, B, B, A

        EXX
NOSWAP2 LD A, B

        CP D

        JR NC, NOSWAP3

        LD A, E, E, C, C, A

        LD A, D, D, B, B, A

        EXX

        LD A, E, E, C, C, A

        LD A, D, D, B, B, A

        EXX
NOSWAP3 LD (VERT1), HL

        LD (VERT2), DE

        LD (VERT3), BC

        EXX

        LD (VERT1 +2), HL

        LD (VERT2 +2), DE

        LD (VERT3 +2), BC

        EXX



   The result is a picture
shown in the figure. In this case,
inflection point is on the left
respectively, in the left half
triangle have 2 faces, and the right -
one.


                vert1
,,

              ,,

             ,,

            ,,

           ,,
H1,,

         ,,

        ,,

       ,, H

      ,,
 vert2,

       'U,

           'U,

               'U,
H2 'u,

                        'U,

                              u,

       <W> 'u
'' U

                                     vert3
Ha below:

     H - Maximum height of the triangle

     H1 - the height of ribs (vert1, vert2);

     H2 - the height of ribs (vert2, vert3);

     W - maximum width of the triangle

          triangle of X. Determined

          it follows:


   t = (vert2.y-vert1.y) / (vert3.y-vert1.y)

   w = (vert1.x + t * (vert3.x-vert1.x))-vert2.x


   If H and / or W are zero, then
Draw a triangle is not necessary.

   Next, we determine which side
is an inflection point in the triangle.
This is done checking the sign of W.
If this value is positive, then the point
inflection - the left is negative - on the right.


   If w> 0 then "point" on left

          else "point" on right


   Then perform a linear interpolation
X coordinate of the triangle and texture
coordinates U, V. Interpolation can be
conduct the formula below, or
by Brezenhemu - as you like, but
the second option more quickly (although, for small
number of polygons that we have to
Spectrum, it's almost not noticeable).


   So, if the inflection point on the left (W> 0);
then you need to draw two faces to the left and one
on the right:

Building a left-hand side of the triangle:


   h1 = vert2.y-vert1.y

   DeltaLeftX1 = (vert2.x-vert1.x) / h1

   DeltaLeftU1 = (vert2.u-vert1.u) / h1

   DeltaLeftV1 = (vert2.v-vert1.v) / h1

   h2 = vert3.y-vert2.y

   DeltaLeftX2 = (vert3.x-vert2.x) / h2

   DeltaLeftU2 = (vert3.u-vert2.u) / h2

   DeltaLeftV2 = (vert3.v-vert2.v) / h2

   X: = vert1.x;

   For i = 0 to h1 do

    Begin

      TableLeftX [i]: = X;

      TableU [i]: = U;

      TableV [i]: = V;

      X: = X + DeltaLeftX;

      U: = U + DeltaLeftU;

      V: = V + DeltaLeftV;

     End;

    For i = h1 to h2 do

     Begin

      TableLeftX [i]: = X;

      TableU [i]: = U;

      TableV [i]: = V;

      X: = X + DeltaLeftX2;

      U: = U + DeltaLeftU2;

      V: = V + DeltaLeftV2;

     End;

Construct the right-hand side of the triangle:


    h = vert3.y-vert1.y;

    DeltaRightX = (vert3.x-vert1.x) / h;

    For i = 0 to h do

     Begin

      TableRightX [i]: = X;

      X: = X + DeltaRightX;

     End;


   Here:

     TableLeftX [] - the table containing the X

                    coordinates of the left half
                    guilty of a triangle;

     TableRightX [] - the same for the right position
                    guilt;

     TableU [] - the table containing the U

                    coordinates of the left half
                    treuglnika guilt;

     TableV [] - the same for V-coordinates

     DeltaLeftX - constant interpolation

                    (On the ZX should be considered
                    Camping with the fixed point 8.8);

     DeltaRightX - same thing.


   If the inflection point is on the right
(W <0), then all the same, only the left
Draw one edge of the triangle, and
right - two.


   Next we define the steps in texture,
which are constant for all
landfill (to ZX assume them fixed
point 8.8, and with the sign!):


   DeltaU = (t * (u3-u1) + (u1-u2)) / w

   DeltaV = (t * (v3-v1) + (v1-v2)) / w



  Further, immediately draw a polygon:

 i: = 0;
 For Y: = vert1.y to (vert1.y + H) do
 Begin

  U: = TableU [i];

  V: = TableV [i];

  For X: = TableLeftX [i] to TableRigtX [i] do

  Begin

    {Puts an end to the coordinate (X, Y) color

     from the texture coordinate (U, V)}

   PutPixel (X, Y, Texture [U, V])

   X: = X +1;

   U: = U + DeltaU;

   V: = V + DeltaV

  End;

  i: = i +1
 End;



   The inner loop would look like
follows (by the way, perhaps
faster ...):


        LD A, H; V-coordinate in the texture

        ADD HL, DE; to calculate the following
                 ; Ordinate V

        EXX

        LD B, A

        LD C, H; U-coordinate in the texture

        ADD HL, DE; to calculate the following
                 ; Ordinate U

        LD A, (BC); take texel

        EXX

        LD (BC), A; put texel in bu
                 , Fer

        INC C; increasing X-coordinates
                 ; That



   It is recalled that all (almost)
calculations are performed with the fixed point 8.8,
For example:

       HL = HL, ie, H contains an integer part
number, and L - fractional.


    Well, like all good luck in the coding.