PDA

View Full Version : Type conversion



Orzech
11-24-2004, 02:26 PM
Hello!

I have a 2D vector of floats representing a position. It's important that the numbers describing the position need to be rounded to integer values when I use them in drawing. Which method would be faster :

1) convert floats to ints at the level of C code

iX = (int) fX;
iY = (int) fY;2) pass floats as arguments to glVertex2i() and hope that OpenGL does the rounding better than pure C does

glVertex2i(fX, fY)Which one seems more efficient? Maybe there is some other - better - solution?

Thanks in advance! :)

Bob
11-25-2004, 05:16 AM
Assuming you use iX and iY from 1) and pass them to glVertex2i, then there's no difference between the two. In the second code, the floats are converted to integers before they are passed to the function. So it's just a matter of explicitly storing the temporary integer values (as in 1) or let the compiler deal with the temporary copies (as in 2). A decent optimizing compiler will generate the same code for both cases.

Orzech
11-25-2004, 06:10 AM
Putting it shortly - no difference. :) That's all I wanted to know.

Thanks!

Relic
11-25-2004, 07:02 AM
Small nitpick: For negative vertex coordinates this might not be what you want.

From Microsoft's C++ help:
"When an object of floating type is converted to an integral type, the fractional part is truncated. No rounding takes place in the conversion process. Truncation means that a number like 1.3 is converted to 1, and 1.3 is converted to 1."