Jump to content
  • Advertisement
Sign in to follow this  
chrisb3

OpenGL OpenGL heightmap

This topic is 3892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!