![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 45
| Float To 3 unsigned chars I want to be able to convert a float to 3 unsigned chars (as in part of the float in each unsigned char) and also be able to compare the float to the equivalent value of the 3 unsigned chars. float will be between 0 and -1.0 I hope that makes sense :S Many Thanks, Last edited by appleGuy; 07-03-2009 at 12:23 PM. |
| appleGuy is offline | |
| | #2 |
| Registered User Join Date: Mar 2007
Posts: 333
| Each part of the decimal in to an unsigned char? Try reading the float in to a string, and then pull out the part you want in to a single char.
__________________ home page (new layout) |
| scwizzo is offline | |
| | #3 | |
| Registered User Join Date: Aug 2006
Posts: 45
| Quote:
-1.0 = 255, 255, 255 -0.x = ???, ???, ??? 0.0 = 0, 0, 0 | |
| appleGuy is offline | |
| | #5 | |
| Registered User Join Date: Aug 2006
Posts: 45
| Quote:
I am trying to implement a z buffer algorithm for a software renderer. Pixels come in with a particular depth between 0.0 and -1.0. I want to be able to store these depths (for later comparisons) in a depth buffer that is really an image consisting of 3 unsigned chars per pixel (RGB). A Pixel In The rendered image has the same pixel location as its depth in the depth buffer, therefore I have 3 * unsigned chars to play with | |
| appleGuy is offline | |
| | #6 |
| int x = *((int *) NULL); Join Date: Jul 2003 Location: Banks of the River Styx
Posts: 891
| I'm not entirely sure what you're trying to do here, but if your floats are in the range of [-1, 0], then you could scale that to [0, 2^24-1], and then store than in an integer. Then store 8 of the 24 bits in one of your colors. If you had a 32bit image, you could probably stuff the entire float in, since an IEEE float is 32bit. But this whole thing feels like you're pushing a square peg into a round hole.
__________________ long time; /* know C? */ Unprecedented performance: Nothing ever ran this slow before. Any sufficiently advanced bug is indistinguishable from a feature. Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31. The best way to accelerate an IBM is at 9.8 m/s/s. recursion (re - cur' - zhun) n. 1. (see recursion) |
| Cactus_Hugger is offline | |
| | #7 | |
| Registered User Join Date: Mar 2004
Posts: 535
| Quote:
The formula for linear interpolation is f(x) = f(a) + (b-a)/(x-a) * (f(b)-f(a)) If a = 0.0, b = -1.0, f(a) = 0, f(b) = 255 it could go something like Code: const double a = 0.0;
const double b = -1.0;
const double fa = 0.0;
const double fb = 255.0;
double x;
unsigned char pixelval;
.
.
.
/* get a value of x from somewhere. Presumably can be anything from a to b */
pixelval = (fa + (x-a)/(b-a)*(fb-fa))+0.5; /* round the positive float to the "nearest" integer */
D. Last edited by Dave Evans; 07-03-2009 at 02:19 PM. | |
| Dave Evans is offline | |
| | #8 |
| Registered User Join Date: Mar 2007
Posts: 333
| Why wouldn't something as easy as this work? Code: unsigned char pixel = depth*(-255.0);
__________________ home page (new layout) |
| scwizzo is offline | |
| | #9 | |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| Quote:
The bad thing though is that in order to compare depths later, you have to reconstruct the int value, which is additional overhead. If you multiplied by -65535 instead and only used two of the bytes to store it, then you have less work to reconstruct the value later and the result should be approximately as good. You could cast the address of those 3 bytes as a pointer to short, mask off the lowest bit of the pointer, and then access it directly as a nicely-aligned short, for better performance. For that matter, why don't you just treat it as an array of unsigned shorts directly, completely ignoring the upper one third of the bytes? I have my own hobby software renderer, so I have a pretty good idea of what is faster most of the time.
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger | |
| iMalc is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C in UNIX environment; external bubble sort doesn't work | stn0091 | C Programming | 2 | 05-13-2009 03:25 PM |
| rotating around object (looat) | jabka | Game Programming | 13 | 06-18-2008 05:02 PM |
| Debug Error Really Quick Question | GCNDoug | C Programming | 1 | 04-23-2007 12:05 PM |
| Please STICKY this- vital to MSVC 6 dev - BASETSD.h | Bubba | Game Programming | 11 | 03-15-2005 09:22 AM |
| error declaration terminated incorrectly help | belfour | C++ Programming | 7 | 11-25-2002 09:07 PM |