Optimized routine to fill rectangle with bitmap?

Started by
5 comments, last by Ilici 19 years, 6 months ago
I'm trying to find the best (fastest) way to fill a screen rectangle given: X = Screen Lower Left X Y = Screen Lower Left Y W = Rectangle WIdth in pixels H = Rectangle Height in pixels with a bitmap from a texture given: TX = Texture Lower Left X TY = Texture Lower Left Y TW = Texture width in pixels TH = Texture height in pixels Here is what I have right now:

void lugBitmap::Fill(int x, int y, int w, int h, int tx, int ty, int tw, int th)
{
     // repeat fill actions across an down...
     register int ex, ey;
     register int dx, dy;
     register int sx = x;
     
     ex = x + w;		// end x pos
     ey = y + h;		// end y pos
     dy = th;			// Y shift per iteration
     
     glMatrixMode(GL_TEXTURE);
     glScalef(Width,Height,1.0);
     glMatrixMode(GL_MODELVIEW);
     
     glBegin(GL_QUADS);     
     while (y < ey)
     {
          dx = tw;			// X shift per iteration
          if ((ey-y) < dy) dy = ey - y;	// clip a the end if needed
          
          x = sx;
          while (x < ex)
          {
               if ((ex - x) < dx) dx = ex - x;	// clip at the end if needed
               
              		glTexCoord2i(tx,ty);
              	glVertex2i(x,y);
              		glTexCoord2f(tx+dx,ty);
              	glVertex2i(x+dx,y);
              		glTexCoord2f(tx+dx,ty+dy);
              	glVertex2i(x+dx,y+dy);
              		glTexCoord2f(tx,ty+dy);
              	glVertex2i(x,y+dy);
              	x += dx;
          }
          y += dy;
     }     
     glEnd();
     
     glMatrixMode(GL_TEXTURE);
     glLoadIdentity();
}

Advertisement
[looksaround] Why aren't you just drawing a single quad then?
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
How do you make a single quad repeat just part of a texture?

Quote:Original post by Ranger_One
How do you make a single quad repeat just part of a texture?


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

Then just specify texture cords larger than 1.0f.
Quote:glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);


Won't that repeat the entire texture? I need to repeat just a slice of it (a skin for a GUI)...

Ahh ic. I didn't understand before.
Erm if you're cutting out parts of textures for skins etc I recommend using pixel precision and/or not building mipmaps. Otherwise I foresee many problems with scaling causing bleeding from nearby components in the skins.
Personally I perfer to seperate textures where I can & use them individually so this kind of thing doesn't happen (makes coding like that alot easier too)

Erm yeah that code probally looks fine. I'm kinda tired for complex thought right now so can't really analysis it now
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Poke around your texture coordinates:

to render a quad repeating a part of the texture like in your original post:

x, y - repeated rectangle's position
w,h - repetead rectangle's size
tw, th - texture size
qw, qh - quad size, assuming you want a pixel in the texture to be a pixel onscreen

glTexCoord2i(x, y) x and y should be in the texture's rectangle
glTexCoord2i((x + w) % (tw - x) + x, y)

and the same for Y

draw it out on paper to see if it works and why

This topic is closed to new replies.

Advertisement