Sign in to follow this  
Cleese852

Reading RAW 16 bit

Recommended Posts

I'm trying to read a .raw file containing the heightdata for my terrain. I succeeded in reading a 8bit raw file, but I want more height precision so I'd like to read a 16 bit file. This is what I got for 8 bit version: // Vector containing height for each vertex in terrain std::vector<unsigned char> in( row * col ); // Read the stuff std::ifstream inFile; inFile.open(filename.c_str(), ios_base::binary); inFile.read((char*)&in[0], (streamsize)in.size()); inFile.close(); And what I'm trying to do for 16 bit: // Vector containing height for each vertex in terrain std::vector<unsigned short> in( row * col ); // Open the file. std::wifstream inFile; inFile.open(filename.c_str(), ios_base::binary); inFile.read((wchar_t*)&in[0], (streamsize)in.size()); inFile.close(); Is this the right way to read raw 16bit because my heights seems screwed up?

Share this post


Link to post
Share on other sites
If I want to read from a ifstream you need a pointer to char like:

inFile.read((char*)&in[0], (streamsize)in.size());

But if I want to read 16 bit, which is 2 bytes I need wchar_t* and so I need to use a wifstream in order to do:

inFile.read((wchar_t*)&in[0], (streamsize)in.size());

Am I correct?

Share this post


Link to post
Share on other sites
No, you still read 16Bit values using ifsteam. You can think of that (char*) cast as casting to (byte*). You just change the amount of data you read (i.e. in.size() * 2 because you want to read in.size() * 2 bytes of data).

Edit: Well, in case that didn't make to much sense, lets try it this way: ifstream.read expects a byte array to write and the amount of bytes to read. It doesn't care what those bytes are used for later on.

Share this post


Link to post
Share on other sites
First of all there was (apparently) something wrong with my raw file. There was an alpha channel which made my terrain look like pins shooting out of the ground.
But with this line of code and an ifstream:
inFile.read((char*)&in[0], (streamsize)in.size() * 2);
I got heightdata for each vertex. But the final result was screwed by that alpha channel. Now I got rid of that and I have now smooth heigth on my terrain, but only for one half of it, the other half is flat.
So am I not reading enough of the raw file?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this