Texture Mapping (Walls only)
by PeyloW

I have been testing and trying to do texture mapping and I finaly found out the basics and got it to work. So I have first written down the mathimatical basics and then a easy to understand basic code. Hope you find it useful, and if you do then please write me a note and don't forget to give me some credit in your production...

First you got your picture in memory, which I describe like this:

            Xm
        +--------+
        |        |
        |        | Ym
        |        |
        +--------+

And this picture shall be put on this polygon:

  X1,Y1    
    +---___ x2,y2
    |      ---+
    |         |
    |         |
    |   ___---+
    +---    X3,Y3
  X4,Y4          

Now you only have to do this procedure every pixel in the polygon:

PLOT X, Y, POINT(Xp, Yp)

Well what are Xp and Yp then? Well you only have to use these two formulas:

      Xm*(X-X1)
Xp = -----------
        X2 -x1


               (Y2-Y1)*(X-X1)
       Ym*(y- ----------------)
                   X2 - x1
Yp = -----------------------------
               X*(Y3-Y2-Y4+Y1)
       Y4-Y2+ -----------------
                   X2 - x1

As a little hint I could say that you better do this in assembler and that you draw the polygon in columns not by lines. And make some kind of delta values and just add in the inner loops.

Well I maybe shall help you get started with a little test in Basic. You maybe don't recognise the formula but if you don't then you better not try to code any Texture mapping at all!!!

FOR X = x1 TO X2
  Xp = (Xm * (X - x1)) / (X2 - x1)
  Ys = Y1 + ((y2 - Y1) / (X2 - x1) * (X - x1))
  Ye = Y4 + ((Y3 - Y4) / (X2 - x1) * (X - x1))
    FOR Y = Ys TO Ye
      Yp = (Ym * (Y - Y1)) / (Ye - Ys)
      PLOT X, Y, POINT(Xp, Yp)
    NEXT Y
NEXT X

Another hint to make the original picture in a length and width of 32, 64 128 or something equaly easy to ROL instead of MULU.

Then I in my own code (On ST) got one seperate precalculated code for every colour to plot, and one extra for transparent picture. (Windows) And the transparent cod is one line long and includes a RTS so guess what P-time that one takes!?!

Well that's it, good luck from PeyloW of T.O.Y.S.

Greetz goes to:
TaM, Thanx for all but coding
Me, Say helo to Josefin.
AIO (Pseudo), Thanx for all muzix.
Hydrogen, Hey there.
Eagle, Havent seen you for a long...
OB, I look forward to SUB STAION.
MF, Min sladd tack !!!
And to all others...

My address if you need it ?!?

PeyloW of T.O.Y.S.
Greby
59042 Horn
Sweden

And phone #:
0494-30404
(I love a long chat but only 18-21)

See ya...

Discuss this article in the forums


Date this article was posted to GameDev.net: 7/16/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
Texture Mapping

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!