Skip to main content
GameDev.net gamedev.net
🔒 Locked

floating point conversion / c++

Started by nini Aug 7, 2006 at 11:35 PM 4 replies 550+ views
Original Post
nini
nini
hi all , i'm writting a 3DS reader , at this time all goes fine except that when i render the model i have artifact in the placement of the texture and i wonder if my code is clear for float conversion here is pseudocode of what i do : Read all the file data into BYTE *data; then : float u; float v; for(int i=0;i<numTexCoord;i++) { u = *((float*)data[0]); v = *((float*)data[4]); data+=8; // increment pointer by 8 byte (2 floating point number) } Is this the right way ? thankx in advance for your help.
blarn
blarn
If data[0] and data[4] are pointers to float's, then what you wrote should work.
But are you sure you don't mean

u = (float)data[0];
v = (float)data[4];
haegarr
haegarr
If I understood right: Data is a pointer to bytes, BYTE *data, then data[0] is a byte. What you wanted is the address of that byte, casted to a pointer to float
(float*)(&data[0])
and at last copy the float. In summary
u = *((float*)(&data[0]));
v = *((float*)(&data[4]));
data += 8;

You can alternatively use data as a pointer directly, like so:
u = *((float*)data);
v = *((float*)(data+4));
data += 8;

Of course, this works only if no endian conversion is needed.
nini
nini
Quote:
Original post by haegarr
Of course, this works only if no endian conversion is needed.


this is interesting me , i fill the data pointer with the standard fread function from stdio.h , and i don't know how 3DS store floating point numbers...

here is a screen of the pbm i encounter with the uvmap of a 3DS model
but it seems not to be the conversion the pbm because i have dumped texcoord from runtime and from the ascii file version of 3DS model and they seem to be the same... so it must be something else...

thanx for your help..

Image Hosted by ImageShack.us
nini
nini
that's okay , a guy have the good answer about this gotcha , in fact i'm using deVIL for texture loading and the bitmap is stored in memory in the inverse so i must flip the v value eg 1-v ....
thanx a lot for your interest...
Zahlman
Zahlman
So you have the file read into a char (ok, ok, BYTE) array, and you want to interpret a sequence of four bytes in the array as a float value?

In portable (sort of), modern C++, this is spelled:

float u;float v;std::copy(data, data + 4, reinterpret_cast<BYTE*>(&u));std::copy(data, data + 4, reinterpret_cast<BYTE*>(&v));


You shouldn't try to convert the byte location into pointer-to-float and then assign to a float from that address, because the 'data' array isn't necessarily aligned in memory, and you can't necessarily read a float from an unaligned address. But the hardware is always at least byte-aligned, so you can interpret the other way: interpret the destination float as a character-array location, and copy character by character across. It's really not that slow: architectures these days are designed for pipelining this sort of thing :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.