[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
DenTut21-Pas

Today is my 21st birthday, so I decided it would be the perfect time to finish up this trainer which I have been meaning to send out for weeks. It's on texure mapping. I know, I know, I said light sourcing, then gourad, then texture mapping, but I got enough mail (a deluge in fact ;) telling me to do texure mapping...
I'll be using the code from Tut 20 quite extensively, so make sure you know whats going on in there... well, on with the show!
BTW, I've improved my web page quite a bit... give it a visit, I want to really ramp up that hit count :)
Firstly, I am cheating. The texture mapping I am going to show you is not perspective-correct, with clever divides for z-placement etc. This method looks almost as good and is quite a bit faster too.
Secondly, you will find it all rather easy. The reason for this is that it's all rather simple. I first made the routine by sitting down with some paper and a pencil and had it on the machine in a few hours. A while later when people on the net started discussing their methods, they were remarkably similar.
Let me show you what I mean.
Let us assume you have a texture of 128x128 (a straight array of bytes [0..127, 0..127]) which you want to map onto the side of a polygon. The problem of course being that the polygon can be all over the place, with one side longer then the other etc.
Our first step is to make sure we know which end is up... let me demonstrate...
If you think back to our tutorial on polygons, you will remember we draw it scanline by scanline. We do texture mapping the same way.
Lets look at that picture again :
The clever bit, and the entire key to texture mapping, is making the logical leap that precisely half way between Point 1 and Point 2 (b), we are at [64,0] in our texture. (a) is in the same manner at [0,64].
That's it. All we need to know per y scanline is : The starting position on the x axis of the polgon line The position on the x in the texture map referenced by that point The position on the y in the texture map referenced by that point
The ending position on the x axis of the polgon line The position on the x in the texture map referenced by that point The position on the y in the texture map referenced by that point
Let me give you an example. Let's sat that (a) and (b) from the above picture are on the same y scanline. We know that the x of that scanline is (say) 100 pixels at the start and 200 pixels at the end, making it's width 100 pixels.
We know that on the left hand side, the texture is at [0,64], and at the right hand side, the texture is at [64,0]. In 100 pixels we have to traverse our texture from [0,64] to [64,0].
Assume at the start we have figured out the starting and ending points in the texture
Do the above for all the scanlines, and you have a texture mapped polygon! It's that simple.
We find our beginning and ending positions in the usual fasion. We know that Point 1 is [0,0]. We know that Point 2 is [127,0]. We know the number of scanlines on the y axis between Point 1 and Point 2.
Repeat for all four sides, and you have the six needed variables per scanline.
When people write me, they often refer to my "tutes". This stems back to Mark Feldman calling them such in the PCGPE. I always though a "tute" was something you did with your car to gain someones attention. I dunno, maybe its an Australian thing
I have been coding almost exclusively in C/C++ for the past year or so. Sorry guys, thats all they will pay me for ;) Anyway, the trainers will continue to be in Pascal for ease of understanding by beginners, but if someone (*ahem* Snowman) doesn't start converting them to C soon, I will do it myself. He also corrected any mistakes I made while he was converting, so I'd prefer he did it (sort of a proofreader after release...)
Send me presents! It's my birthday!
Byeeeee..... - Denthor 16-04-96
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
DenTut21-Pas
ÕÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͸
³ W E L C O M E ³
³ To the VGA Trainer Program ³ ³
³ By ³ ³
³ DENTHOR of ASPHYXIA ³ ³ ³
ÔÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ; ³ ³
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ³
ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
--==[ PART 21 ]==--
- Introduction
Today is my 21st birthday, so I decided it would be the perfect time to finish up this trainer which I have been meaning to send out for weeks. It's on texure mapping. I know, I know, I said light sourcing, then gourad, then texture mapping, but I got enough mail (a deluge in fact ;) telling me to do texure mapping...
I'll be using the code from Tut 20 quite extensively, so make sure you know whats going on in there... well, on with the show!
BTW, I've improved my web page quite a bit... give it a visit, I want to really ramp up that hit count :)
- Free Direction Texture Mapping
Firstly, I am cheating. The texture mapping I am going to show you is not perspective-correct, with clever divides for z-placement etc. This method looks almost as good and is quite a bit faster too.
Secondly, you will find it all rather easy. The reason for this is that it's all rather simple. I first made the routine by sitting down with some paper and a pencil and had it on the machine in a few hours. A while later when people on the net started discussing their methods, they were remarkably similar.
Let me show you what I mean.
Let us assume you have a texture of 128x128 (a straight array of bytes [0..127, 0..127]) which you want to map onto the side of a polygon. The problem of course being that the polygon can be all over the place, with one side longer then the other etc.
Our first step is to make sure we know which end is up... let me demonstrate...
1
+
/ \
/ \
4 + + 2
\ /
\ /
+
3
Let us say that the above is the chosen polygon. We have decided that point
1 is the top left, point 3 is bottom right. This means that1 - 2 is the top of the texture 2 - 3 is the right of the texture 3 - 4 is the bottom of the texture 4 - 1 is the left of the textureThe same polygon, but rotated :
3
+
/ \
/ \
2 + + 4
\ /
\ /
+
1
Although the positions of the points are different, point 1 is still the
top left of our texture.- How to put it to screen
If you think back to our tutorial on polygons, you will remember we draw it scanline by scanline. We do texture mapping the same way.
Lets look at that picture again :
1
+
a / \ b
/ \
4 + + 2
\ /
\ /
+
3
We know that point 1 is at [0,0] in our texture. Point 2 is at [127,0],
Point 3 is at [127,127], and Point 4 is at [0,127].The clever bit, and the entire key to texture mapping, is making the logical leap that precisely half way between Point 1 and Point 2 (b), we are at [64,0] in our texture. (a) is in the same manner at [0,64].
That's it. All we need to know per y scanline is : The starting position on the x axis of the polgon line The position on the x in the texture map referenced by that point The position on the y in the texture map referenced by that point
The ending position on the x axis of the polgon line The position on the x in the texture map referenced by that point The position on the y in the texture map referenced by that point
Let me give you an example. Let's sat that (a) and (b) from the above picture are on the same y scanline. We know that the x of that scanline is (say) 100 pixels at the start and 200 pixels at the end, making it's width 100 pixels.
We know that on the left hand side, the texture is at [0,64], and at the right hand side, the texture is at [64,0]. In 100 pixels we have to traverse our texture from [0,64] to [64,0].
Assume at the start we have figured out the starting and ending points in the texture
textureX = 0;
textureY = 64;
textureEndX = 64;
textureEndY = 0;
dx := (TextureEndX-TextureX)/(maxx-minx);
dy := (TextureEndY-TextureY)/(maxx-minx);
for loop1 := minx to maxx do BEGIN
PutPixel (loop1, ypos, texture [textureX, textureY], VGA);
textureX = textureX + dx;
textureY = textureY + dy;
END;
Do the above for all the scanlines, and you have a texture mapped polygon! It's that simple.
We find our beginning and ending positions in the usual fasion. We know that Point 1 is [0,0]. We know that Point 2 is [127,0]. We know the number of scanlines on the y axis between Point 1 and Point 2.
textureDX = 127/abs (point2.y - point1.y)We run though all the y scanlines, starting from [0,0] and adding the above formula to the X every time. When we hit the last scanline, we will be at point [127,0] in the texure.
Repeat for all four sides, and you have the six needed variables per scanline.
- In closing
When people write me, they often refer to my "tutes". This stems back to Mark Feldman calling them such in the PCGPE. I always though a "tute" was something you did with your car to gain someones attention. I dunno, maybe its an Australian thing
I have been coding almost exclusively in C/C++ for the past year or so. Sorry guys, thats all they will pay me for ;) Anyway, the trainers will continue to be in Pascal for ease of understanding by beginners, but if someone (*ahem* Snowman) doesn't start converting them to C soon, I will do it myself. He also corrected any mistakes I made while he was converting, so I'd prefer he did it (sort of a proofreader after release...)
Send me presents! It's my birthday!
Byeeeee..... - Denthor 16-04-96
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
