First step to heightmap

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
knightoflight
Posts: 199
Joined: Sun Aug 24, 2003 5:47 pm
Location: Germany

First step to heightmap

Post by knightoflight »

I changed Nikos createHillPlaneMesh to get a heightmap, my first try.
For example i use a 256x256 bmp-map from other examples (from http://ohad.visual-i.com). I get the heightmap from heap like Niko wrote in the old forum, with a texture.
But the result looks like in legoland. The land is in steps, why ? I dont really understand the vertex-mathematics :-(
Can someone or Niko please explain ?



IAnimatedMesh* CGeometryCreator::createHillPlaneMesh(const c8* name,video::ITexture* texture, const core::dimension2d<f32>& tileSize,
video::SMaterial* material,
const core::dimension2d<f32>& textureRepeatCount)
{
core::dimension2d<s32> tileCount;
short mapheight;


s16 *p = (s16*)texture->lock();

tileCount = texture->getDimension();
s32 pitch = texture->getPitch() / 2;

SMeshBuffer* buffer = new SMeshBuffer();
SMesh* mesh = new SMesh();
video::S3DVertex vtx;
vtx.Color.set(255,255,255,255);
vtx.Normal.set(0,1,0);


float halfX = (tileSize.Width * tileCount.Width) / 2;
float halfY = (tileSize.Height * tileCount.Height) / 2;

// create vertices

s32 x = 0;
s32 y = 0;

core::dimension2d<f32> tx;
tx.Width = 1.0f / (tileCount.Width / textureRepeatCount.Width);
tx.Height = 1.0f / (tileCount.Height / textureRepeatCount.Height);


// texture->lock();
for (x=0; x<tileCount.Width; ++x)
for (y=0; y<tileCount.Height; ++y)
{
vtx.Pos.set(tileSize.Width * x - halfX, 0, tileSize.Height * y - halfY);
vtx.TCoords.set(-(f32)x * tx.Width, (f32)y * tx.Height);

//if (hillHeight)
mapheight=p[y*pitch + x];
vtx.Pos.Y = mapheight/32;

buffer->Vertices.push_back(vtx);
}
texture->unlock();
// create indices

... // the rest is unchanged to the original
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Re: First step to heightmap

Post by niko »

I think this could be the problem:
knightoflight wrote: mapheight=p[y*pitch + x];
vtx.Pos.Y = mapheight/32;

In the first line, you get the color value in the texture. The color is stored in as a 16 bit value, like this ARRRRRGGGGGBBBBB (one character for each bit). You are interpreting all bits together as one value, but I think you really want just one color value. For example the red value. You can get it by using the function in SColor.h: (http://irrlicht.sourceforge.net/docu/SC ... ource.html) getRed(p[y*pitch+x]);

In the second line, you divide the height by 32. Don't do this now, because getRed() will only return a value between 0 and 32.

Hope it helps. :)
knightoflight
Posts: 199
Joined: Sun Aug 24, 2003 5:47 pm
Location: Germany

Re: First step to heightmap

Post by knightoflight »

>... it by using the function in SColor.h: ...In the second line, you divide the > height by 32. Don't do this now,...

My sword for you my king (and ash on my head ) - but the terrain is still legoland (should i send the jpg-pic ?)
Changing to get one color with
mapheight=p[y*pitch+x] &0x1F;
leads to the same. But now i understand, the problems are not their magical shining vertexes and normals from the irrlicht engine, so its not for this forum...
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

post an image, would be interesting. You mean it does still not look like legoland because there are now only 32 height values? (Yes, yes I know, time to get some support for 24 or 32 bit textures.. :) )
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

I'm not sure if this is related, but a current possible defect in addHillPlaneMesh may be the culprit.

The TileCount is off by 1.

So, if you want a 1x1 tile, you need to specify 2x2. If you want a 16x16, you have to specify 17x17. I haven't looked at the irrlicht code for it, but I'd wonder if the legacy addHillPlane code still in your Heightmap procedure wasn't causing your verticies to match up incorrectly.

Code: Select all

irr::core::dimension2d tilesize = irr::core::dimension2d(256.0f,256.0f);
//Here be problem
irr::core::dimension2d tilecount = irr::core::dimension2d(2,2);
irr::f32 hillheight = 0.0f;
irr::core::dimension2d hillcount = irr::core::dimension2d(0.0f,0.0f);
irr::core::dimension2d texturerepeat = irr::core::dimension2d(8.0f,8.0f);
lbMesh = smgr->addHillPlaneMesh("field",tilesize,tilecount,0,hillheight,hillcount,texturerepeat);
I'll log this defect to the Sourceforge page.
Crud, how do I do this again?
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Ah, thanx, didn't know the bug. I fixed it in my version here. (Just added (1,1) to tileCount inside the method.)
knightoflight
Posts: 199
Joined: Sun Aug 24, 2003 5:47 pm
Location: Germany

Re: first step to heightmap

Post by knightoflight »

okay, like Sire Anonymous wrote in the old forum, i should change to RAW-picture and load direct without texture (and without lost colours). anyway better for the future if i want to store things like trees and so on behind in the file,
thank for the help
Post Reply