C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2009, 12:19 PM   #1
Registered User
 
Join Date: Aug 2006
Posts: 45
Float To 3 unsigned chars

Hi,

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   Reply With Quote
Old 07-03-2009, 12:30 PM   #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   Reply With Quote
Old 07-03-2009, 12:36 PM   #3
Registered User
 
Join Date: Aug 2006
Posts: 45
Quote:
Originally Posted by scwizzo View Post
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.
Its not the literal displayed values... ill try to give an example


-1.0 = 255, 255, 255

-0.x = ???, ???, ???

0.0 = 0, 0, 0
appleGuy is offline   Reply With Quote
Old 07-03-2009, 12:39 PM   #4
Student
 
legit's Avatar
 
Join Date: Aug 2008
Location: UK -> Newcastle
Posts: 156
Is there a relation between the float value and the values that you want to extract?
__________________
MSDN <- Programmers Haven!
legit is offline   Reply With Quote
Old 07-03-2009, 12:45 PM   #5
Registered User
 
Join Date: Aug 2006
Posts: 45
Quote:
Originally Posted by legit View Post
Is there a relation between the float value and the values that you want to extract?
OK, I will explain what i am trying to do...

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   Reply With Quote
Old 07-03-2009, 01:01 PM   #6
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
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   Reply With Quote
Old 07-03-2009, 02:06 PM   #7
Registered User
 
Join Date: Mar 2004
Posts: 535
Quote:
Originally Posted by appleGuy View Post
Its not the literal displayed values... ill try to give an example


-1.0 = 255, 255, 255

-0.x = ???, ???, ???

0.0 = 0, 0, 0
You want to interpolate each rgb value.

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 */
If all of the min pixel values are zero and all of the max pixel values are 255, then you call this function once and assign the same calculated pixel value to r, g, and b.

D.

Last edited by Dave Evans; 07-03-2009 at 02:19 PM.
Dave Evans is offline   Reply With Quote
Old 07-03-2009, 06:23 PM   #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);
Since you know the values are between 0 and -1 then the pixel value will be anything between 0 and 255, and can be later referenced to decide what the depth originally was.
__________________
home page (new layout)
scwizzo is offline   Reply With Quote
Old 07-04-2009, 03:02 PM   #9
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
Quote:
Originally Posted by Cactus_Hugger View Post
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.
What Cactus has described here is the way to go. Multiply by -16777215 and cast to int and then you have a number from 0x000000 to 0xFFFFFF. Done.
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:04 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22