Texture-mapped trapezoid problems.

Started by
0 comments, last by Fingers_ 17 years, 2 months ago
Hello, I've been working on a '2.5D' game engine for a little bit now, and I've run into a problem with textured trapezoid rendering--this's a problem because my engine will be using textured trapezoids for walls and such. I've gotten trapezoid rendering to work, and I've gotten bitmap loading to work, but putting them together is giving me a headache. Here's what I've got so far:

drwWallTex( s16 bx, s16 ex, s16 bh, s16 eh, s16 my, gfxBitmap *Bmp){

     s16 x, y;
     float ts,bs;
     float xp,yp;
     s16 tl,bl;
     u32 cp;
     
     ts = mthSlope( bx, my-(bh/2), ex, my-(eh/2));
     bs = mthSlope( bx, my+(bh/2), ex, my+(eh/2));
     
     for( x=bx ; x<=ex ; x++ ){
          tl = (my-(bh/2))+((x-bx)*ts);
          bl = (my+(bh/2))+((x-bx)*bs);
          xp = (x-bx)/(ex-bx);
          xp*= Bmp->Width;
          xp = (u16)xp;
          
          for( y = tl ; y <= bl ; y++ ){
               yp = (y-tl)/(bl-tl);
               yp*= Bmp->Height;
               yp = (u16)yp;
               
               cp = yp*Bmp->Width+xp;
               
               drwPoint( x, y, Bmp->Pixels[cp]);
          }
     }
}
Currently, what I do is, I draw the trapezoid one line of vertical pixels at a time, where 'tl' is the top limit, and 'bl' is the bottom limit. 'xp' and 'yp' are 'x percent' and 'y percent'; I'm using these to figure how finished the trapezoid is (e.g. half the lines done, and halfway down on the current line, xp and yp would be 0.50) I'm then multiplying these numbers by the bitmap's width, to get the current texture coordinates. In theory, this should be working, but for some reason, it's not. When I test it with a bitmap of a black stick figure on a white background, I just get a white trapezoid (with the right size, just no texture). If anyone could point out where I'm going wrong, I'd be very grateful. Edit: I've messed around with the bitmap I'm using, and it looks like it just draws the trapezoid the color of the first pixel in the bitmap(coord: (0,0)). Maybe the floating point values can't be converted to u16(unsigned short) values? [Edited by - Lollyn00b on January 28, 2007 3:35:05 PM]
Advertisement
This will cause yp to be zero because these variables are integers and (y-tl) is less than (bl-tl):

yp = (y-tl)/(bl-tl);

What you should do is calculate a delta_yp = (float)Bmp->Height/(bl-tl) and set yp to zero before the inner loop, then add the delta to it each time you loop. Better yet, use fixed-point math. Using floats to compose an index to an array is dangerous.

The one for xp has the same problem (and isn't perspective-correct so the texture will look warped).

This topic is closed to new replies.

Advertisement