[SDL] Texture mapping
Bob Pendleton
bob at pendleton.com
Thu Nov 18 07:59:44 PST 2004
On Wed, 2004-11-17 at 21:42, John Silicon wrote:
> On Wednesday 17 November 2004 03:16 pm, Scott Harper wrote:
> > Mein Mein wrote:
> > > Isn't there a nicer way to do this?? some kind of clipping in the texture
> > > or something.... somthing that doesn't slow my program down ofcourse :)
> >
> > Division is always slow on computers. They can add and multiply all you
> > could possibly want -- but subtraction and division are gonna be slow.
> > Try pre-setting the amount you want:
>
> Most (all?) C compilers in use today are going to do the 54.f/64.f calculation
> at compile time and hard-code the result into the two orginal glTexCoord2f
> calls.
>
> In fact, the following code is going to hurt performance more than the
> original, because it requires assembly instructions to push the float
> shownSize onto the stack. If, however, you were to throw a "const" in there,
> the result should be the same as the original code.
Compilers that do reasonable constant folding will actually treat
shownSize as a constant no matter how it is declared and only
materialize it as a variable if it occurs on the left hand side of the
assignment operator.
I wonder how much time we could all save if every person who programs
actually took the time to read a book on compilers?
Bob Pendleton
>
> The only way I can think of to make this more efficient, is to change the
> first and last glTexCoord2f calls that use only 0 or 1 into glTexCoord2s(),
> but the speed difference there is negligible, unless you're using this code
> fairly often, or if you need the time to do alot of other things.
>
> > float shownSize = 54f / 64f;
> > ...
> > glBegin(GL_QUADS);
> > glTexCoord2f(0, 0); glVertex2i(x, y);
> > glTexCoord2f(shownSize, 0); glVertex2i(x+w, y);
> > glTexCoord2f(shownSize, 1); glVertex2i(x+w, y+h);
> > glTexCoord2f(0, 1); glVertex2i(x, y+h);
> > glEnd();
> >
> > Goes back to the old days of programming when answers to hard
> > mathematical computations were stored in lookup tables rather than
> > calculated on the fly. (Okay, not exactly the same thing.) It's a
> > great tool to use! I mean, REALLY great!
>
> Lookup tables are used in cases where it will be more efficient (in preportion
> to the loss in precision) to compute an integer offset, and load from that
> offset, than to compute a precise result.
>
> >
> > --Scott
> >
> > PS: In looking back, I realize that you didn't mention having slowdowns
> > already... But still this technique can help make things easier to look
> > at and immediately understand what's going on in the code.
> >
> > Also, why not make a texture struct (or object or something -- I use
> > Java, so I'm not familiar with what's best in C++) and inside you can
> > store the texture id, and the left, top, right, and bottom sides of your
> > texture in floats. This would help for a tile-based solution where you
> > load ONE texture which is broken into smaller parts.
> >
> > Anyway, my $.02.
> >
> > _______________________________________________
> > SDL mailing list
> > SDL at libsdl.org
> > http://www.libsdl.org/mailman/listinfo/sdl
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
--
+--------------------------------------+
+ Bob Pendleton: writer and programmer +
+ email: Bob at Pendleton.com +
+ blog: www.Stonewolf.net +
+ web: www.GameProgrammer.com +
+--------------------------------------+
More information about the SDL
mailing list