OpenGL heightmap

Started by
6 comments, last by RobbieS 16 years, 5 months ago
I'm trying to load a .raw heightmap in linux openGL/C++. However when I try to run the program i'm getting a segmentation fault. He is the code which I got from a textbook, i've tried to find a solution by myself but I really just don't know where to start. This is for a quite important project so if anyone can help me :)
#include "terrain.h"
bool CTERRAIN::LoadHeightMap( char* szFilename, int iSize )
{
 FILE* pFile;
 if( m_heightData.m_pucData ) UnloadHeightMap( );
 m_heightData.m_pucData= new unsigned char [iSize*iSize];
 if( m_heightData.m_pucData==NULL )
 {
  printf( "Could not allocate memory for%s\n", szFilename );
  return false;
 }

 fread( m_heightData.m_pucData, 1, iSize*iSize, pFile );
 fclose( pFile );

 m_heightData.m_iSize = iSize;
 m_iSize = m_heightData.m_iSize;

 printf( "Loaded %s\n", szFilename );
 return true;
}


bool CTERRAIN::UnloadHeightMap( void )
{
 if( m_heightData.m_pucData )
 {
  delete[] m_heightData.m_pucData;
  m_heightData.m_iSize= 0;
 }
 printf( "Successfully unloaded the height map\n" );
 return true;
}
Advertisement
There is nothing really wrong with that code, except that it doesn't check whether the right amount of data was actually read - try replacing this:
 fread( m_heightData.m_pucData, 1, iSize*iSize, pFile ); 

with this:
if( fread( m_heightData.m_pucData, 1, iSize*iSize, pFile ) != iSize*iSize ){printf( "Could not read data from %s\n", szFilename );return false;}

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
There is nothing really wrong with that code, except that it doesn't check whether the right amount of data was actually read - try replacing this:
 fread( m_heightData.m_pucData, 1, iSize*iSize, pFile ); 

with this:
*** Source Snippet Removed ***


Thank you very much for the quick help, but the problem remains.

I'm calling it with:

CBRUTE_FORCE bruteForce;
bruteForce.LoadHeightMap( "height128.RAW", 128 );

CTERRAIN is my base terrain class which is the parent of CBRUTE_FORCE.
The heightmap was supplied with the textbook.
Hi,

so does the fault actually occure inside this code block ?

You could run your program with a debugger and see which line causes the error.

Regards

[edit]

I hope that you set m_heightData.m_pucData to NULL before trying to load

[Edited by - kauna on November 21, 2007 7:29:15 AM]
Quote:Original post by kauna
Hi,

so does the fault actually occure inside this code block ?

You could run your program with a debugger and see which line causes the error.

Regards

[edit]

I hope that you set m_heightData.m_pucData to NULL before trying to load


If I call Unload Heightmap, it runs fine in that it sees that there isn't one, and prints a message. So the fault must be with Load Heightmap as everything else in terrain.c++ works.

Setting m_heightData.m_pucData to NULL didn't change anything, did you want me to initialise it to NULL (that's what I tried)?


I will try and find a debugger, thanks again for the replies so far!
Is this line correct...

m_iSize = m_heightData.m_iSize;

shouldn't it be

m_heightData.m_iSize = m_iSize;
?

Scratch that... :)
"Game Maker For Life, probably never professional thou." =)
Looks like you read from the uninitialized pFile variable without opening the file.

add something like:

pFile = fopen(szFilename, "rb");


Be sure to check the return value in case the file doesn't exist or you don't have permission to open it. (Note that "rb" is the windows version, linux might be different).
Quote:Original post by Solias
Looks like you read from the uninitialized pFile variable without opening the file.

add something like:

pFile = fopen(szFilename, "rb");


Be sure to check the return value in case the file doesn't exist or you don't have permission to open it. (Note that "rb" is the windows version, linux might be different).


Beaten to the solution. I think this is it. Also, a small niggle. If new fails, are you using an overloaded 'new' operator that doesn't throw an exception? Because if not, C++ doesn't return NULL for failed allocs.

This topic is closed to new replies.

Advertisement