Sign in to follow this  
TDP owns

Can't convert bin. ascii to float

Recommended Posts

Im trying to create a OpenGL heightmap loader. Using: Code::Blocks 1.0 /GNU GCC I modified the OpenGL example that came with C::B. The heightmap itself is a 768x768 grayscale binary file. The problem is, that the converted float equals 0., it should more like 0.0 (0x0 on the heightmap). anyway heres my code: -------------------------------------------------------------------------- /************************** * Includes * **************************/ #include <windows.h> #include <gl/gl.h> #include <fstream> #include <string.h> using namespace std; /************************** * Function Declarations * **************************/ LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC); void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC); void LoadFile(); float HM[767][767]; /************************** * WinMain ... -------------------------------------------------------------------------- void LoadFile() { char Buffer [589823]; int x; int y; float z; long k; char* t; char bfr [6]; //589824 bytes (0 - not counted) 768x768 ifstream File("hm.b",ios::in | ios::binary); File.read (Buffer, 589823); File.close(); for ( y = 0; y< 767; y++ ) { for (x = 0; x < 767;x++) { z = (float)Buffer[k]; HM [x] [y] = z; k++; } } gcvt (HM[767][767],6,bfr); t = (char*)bfr; MessageBox(NULL,t,"t",MB_OK); // is there any other way to check variable values? } -------------------------------------------------------------------------- wth am i doing wrong?

Share this post


Link to post
Share on other sites
wha?
character array?
im converting the characters in the array one by one to floats,
then putting them in a 2d array.
or am i missing something?

if im doing anything stupid then plz tell me, because for me it looks fine and should work...

Share this post


Link to post
Share on other sites
Actually it doesn't work,
atleast i got some non-zero values to work with.
Im quite sure the problem lies in the converting,
so i've got some serious testing to do.

Anyway thanks for the help.

Edit:
LOL, never use a flat heightmap for testing...
The loader works fine, could be more accurate though.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDP owns
Initial value, zero i guess.
Im using the k as a offset for reading through the ascii string and
increasing it by one each x-loop


There is no default initial value in C/C++. Unless you initialize the variable manually, you don't know the value of k. To make my statement simple, k is probably not 0 at the beginning of the loop.

There are also numerous other errors in the code, as well as some style problems.

1) don't use litteral constants - use symbolic constant instead:
int HM[768]|768]; // bad
char Buffer [589823]; // bad
--
const int HM_SIZE = 768; // far better
int HM[HM_SIZE][HM_SIZE];

Your buffer size is wrong. You want to read 768x768 pixels. Thus, you have to read 768*768=589824 bytes, not 589823. I guess that you there is some kind of misunderstanding in the way you see arrays.
When you declare an array, you declare its whole size. When you use an array, don't forget that the index of its first element is 0. For example, if you have to create an array that will contain 4 integers, you declare it as:
int array[4];

But if you want to use it, you can only access to indices 0 to 3 (0,1,2,3 => 4 indices).
Same problem: when converting your bytes to floats, the loop maximum indices are wrong. To loop over 768 pixels, you must loop from 0 to 767 included. Or, if you don't want to include the last value (common idiom), then the stop condition is "<768" (or better "<HM_SIZE").

Your code shoudl read:

const int HM_SIZE = 768;
float HM[HM_SIZE][HM_SIZE];

void LoadFile()
{
// char Buffer [589823]; // NEVER DO THAT. It allocates an array ON THE STACK
// which is supposedly a limited resource.
std::vector<char> buffer(HM_SIZE*MH_SIZE); // allocates a buffer
long k = 0;

ifstream File("hm.b",ios::in | ios::binary);
File.read (&buffer.front(), buffer.size());
File.close();

for (int y = 0; y< HM_SIZE; y++ )
{
for (int x = 0; x < HM_SIZE;x++)
{
float z = (float)buffer[k];
// there is a simpler way to do this, which does not need you to
// maintain a 'k' variable: float z = (float)buffer[y*HM_SIZE+x];
HM [x] [y] = z;
k++;
}
}
}


To answer your very last question: yes, there is other ways to inspect variable values. The best one is probably the debugger which is integrated to your IDE.

HTH,

Share this post


Link to post
Share on other sites
Thx, your post was very helpful.
It kinda seems logical now...
C++/VB differences are starting to confuse me...
I've got to do lots of learning about this stuff, maybe should have done a hello world first or smth...

Share this post


Link to post
Share on other sites
Quote:
Original post by TDP owns
Thx, your post was very helpful.
It kinda seems logical now...
C++/VB differences are starting to confuse me...
I've got to do lots of learning about this stuff, maybe should have done a hello world first or smth...


Then I suggest you to have a look to the running C++ Workshop (see sig for link). The only thing you have to do is to buy a cheap book (you can probably find a used one if you don't want to pay the full price), follow the chapters and ask if there is something you don't understand. We'll be glad to help.

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