Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

kevlur

Which is the Directx function to load bitmap for height maps

This topic is 5327 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

Hi, could someone tell me which is the function to load bitmap for height maps? Thanks Kev

Share this post


Link to post
Share on other sites
Advertisement
easiest way (use it with a 16 bit bitmap for best results)

open the file using fopen or whatever type of stream open function you prefer, then load the bitmapinfoheader and the bitmap info, then create a mesh from the loaded data.

i may be wrong (i dont know anything about the sample framework, it may be in there) but i dont believe there is any function to directly load a heightmap from a bitmap.

___________________
-Nicholas Anton, Owner RaptorTech
-Admin(at)Raptor85.com

Share this post


Link to post
Share on other sites
I'm reluctant to do this, because this code screams COPY-PASTE ME, but here is code that'll load a bitmap for you.


#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <cstdio>

unsigned char* ReadBitmap( const char* FileName, int& Width, int& Height, int& bpp )
{
BITMAPFILEHEADER Header;
BITMAPINFOHEADER Info;
FILE* fp;
unsigned char* RawData;

fp = fopen( FileName, "rb" );
if( fp== NULL )
{
fclose( fp );
return NULL;
}

//read in the heightmap data

fread( &Header, sizeof(Header), 1, pMap );
fread( &Info, sizeof(Info), 1, pMap );
fseek( pMap, Header.bfOffBits, SEEK_SET );

//notice these are all reference parameters

Width = Info.biWidth;
Height = Info.biHeight;
bpp = Info.biBitCount;

RawData = new unsigned char[Width*Height*bpp];
fread( RawData, 1, Width*Height*bpp, fp );
fclose( fp );

return RawData;
}


This will return a pointer to the bitmap data, and will give you the width, height, and bit depth of the bitmap data via reference parameters. You'll then have to process that into a heightmap.

[edited by - Promit on April 13, 2004 10:07:03 PM]

Share this post


Link to post
Share on other sites
Oh for gods sake, please don't use a new inside the function! Just make it return the data to a user defined pointer and let the function be made queryable so they can make the correct size buffer.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstdio>
void ReadBitmap(unsigned char** RawData, const char* FileName, int* Width, int* Height, int* bpp )
{
BITMAPFILEHEADER Header;
BITMAPINFOHEADER Info;
FILE* fp;
fp = fopen( FileName, "rb" );
if( fp== NULL )
{
fclose( fp );
return;
}
//read in the heightmap data

fread( &Header, sizeof(Header), 1, pMap );
fread( &Info, sizeof(Info), 1, pMap );
fseek( pMap, Header.bfOffBits, SEEK_SET );
//notice these are all reference parameters

if(Width != NULL)
Width = Info.biWidth;
if(Height != NULL)
Height = Info.biHeight;
if(bpp != NULL)
bpp = Info.biBitCount;
if(RawData == NULL)
return;
fread( *RawData, 1, Info.biWidth*Info.biHeight*Info.biBitCount, fp );
fclose( fp );
}


Usage:

int Width=0, Height=0, bpp=0;
unsigned char* BitmapInfo;
//Query for Width, Height and BPP

ReadBitmap(NULL, "somefile.bmp", &Width, &Height, &bpp );
//Create buffer for info

BitmapInfo = new unsigned char[Width*Height*bpp];
//Read in info

ReadBitmap(&BitmapInfo, "somefile.bmp", NULL, NULL, NULL);


Please note I didn't compile this so please don't flame me if something's a bit askew.

@Promit: Don't take it personally, I just have a personal thing with new inside of functions (who ever remembers to delete afterwards?)
EDIT: Promit, how did you get pretty spacing inside your [ source]? I've never been able to do that!

[edited by - ms291052 on April 13, 2004 10:40:58 PM]

Share this post


Link to post
Share on other sites
First off, I don''t see any problem with using new inside a function as long as you make it pretty clear that somebody is going to have to delete it afterwards. Although my version of the code is actually inside a class whch deals with its pointers itself, so there''s no real need. Not to mention I don''t like the querying thing.


As for the pretty spacing, I used the space bar. A lot.

Share this post


Link to post
Share on other sites

  • 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!