Why isn't my terrain being modified?

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.
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Why isn't my terrain being modified?

Post by pwierz »

Hi,
I'm trying to modify my ISceneTerrainNode on the fly and it's not working. Can someone explain to why it isn't working? My code is attempt to just modify the height and turn it white but it's showing up black and flat. Thanks. Is there still that limit on terrain sizes of 129x129?

Code: Select all


	scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode("black.bmp",
		0,					// parent node
		-1,					// node id
		core::vector3df(0.f, 0.f, 0.f),		// position
		core::vector3df(0.f, 0.f, 0.f),		// rotation
		core::vector3df(1.0f,1.0f,1.0f),	// scale
		video::SColor ( 255, 0, 0, 0 ),	// vertexColor
		5,					// maxLOD
		scene::ETPS_17,				// patchSize
		4					// smoothFactor
		);
	terrain->setMaterialFlag(video::EMF_LIGHTING, false);

	
	scene::CDynamicMeshBuffer* buffer = new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
	terrain->getMeshBufferForLOD(*buffer, 0);
	video::S3DVertex2TCoords* data = (video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();
	
	for(int i=0; i < 1025; i++){
		for(int j=0; j<1025; j++)
		{
			data[1025*i + j].Pos.Z = j; 
			data[1025*i + j].Color = video::SColor(255,255,255,255);
		}
	}
	buffer->drop(); // When done drop the buffer again.
	
	terrain->setScale( terrain->getScale() );
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

First of all, I was under the impression that y was vertical movement. Second, you'll most likely have to modify ALL the levels of LOD.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It doesn't work because you are getting a copy of the mesh data. Modifying a copy of something does not affect the original. If you want to modify the original, you should probably use the getMesh() method.

Travis
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

Thanks for the help. I changed the section that gets the data to

Code: Select all

	scene::IMesh* buffer = terrain->getMesh();
	video::S3DVertex2TCoords* data = (video::S3DVertex2TCoords*)buffer->getMeshBuffer(0)->getVertices();
And still no dice. Any other thoughts? Thanks again for the help.
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

First of all, I was under the impression that y was vertical movement.
Thanks. That changed the elevation. But the color is not changing. It remains whatever color I make it upon creation.
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Hmm. Have you tried changing the colors of the terrain's SMaterial?
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

Lonesome Ducky wrote:Hmm. Have you tried changing the colors of the terrain's SMaterial?
I wanted to do it on a per pixel basis. Mostly color code it according to height.
Grumpy
Posts: 77
Joined: Wed Dec 30, 2009 7:17 pm
Location: Montana, Usa

Post by Grumpy »

Where is the tutorial for this ???
the tutorial 12. terrain rendering does not go into any detail what or how anything is to be loaded. The above code by pwierz documented is the only hint of whats going.
Or am I going in the wrong direction and should be looking for scene editing tutorials ???

Tim
code happens
pwierz
Posts: 59
Joined: Sun Aug 20, 2006 3:32 pm

Post by pwierz »

Grumpy wrote:Where is the tutorial for this ???
the tutorial 12. terrain rendering does not go into any detail what or how anything is to be loaded. The above code by pwierz documented is the only hint of whats going.
Or am I going in the wrong direction and should be looking for scene editing tutorials ???

Tim
There really isn't a tutorial for what I am trying to do beyond tut 12. I just sort of put it together by playing with the system. In the meantime, does anyone out there have any info on why the colors of the vertices aren't changing?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I think you need to alter the RenderBuffer in this case. Vertex attributes are not copied from the original mesh to the RenderBuffer after creation.
wowdevver24
Posts: 26
Joined: Mon Jan 04, 2010 8:02 pm

Post by wowdevver24 »

right here is some code that generates a texture based upon height in irrlicht, you would just regenerate the texture every time the height has been modified (works out not that expensive as in-game editing would be less likely to occur and over a much reduced time-span)

Code by Frosty Topaz:

Code: Select all

ITexture *generateTerrainTexture(IVideoDriver *driver, ITerrainSceneNode *terrain)
{
   // Create the texture we'll use on the terrain
   // NOTE: The texture is 128 by 128 while the terrain is 129 by 129.  There are a couple of reasons
   // for this.  First it's better for texture sizes to be a power of 2.  Second this size lets us work
   // with the texture and terrain together as if each pair of triangles on the terrain is one pixel on
   // the texture.  It also gives us a bit of room when calculating slopes.
   ITexture *texture = driver->addTexture(dimension2d<s32>(128,128), "texture", ECF_A8R8G8B8);

   // Check if the colour format is what we expect.  IVideoDriver::addTexture() does not have to
   // use the colour format you specify so it must be checked.  Obviously it would be better to
   // generate a texture for all the colour formats but for this example I wanted to keep the whole
   // process simple.
   if (texture->getColorFormat() != ECF_A8R8G8B8)
      return NULL;

   // Lock the texture we created earlier so we can work on the data
   u32 *texData = static_cast<u32 *>(texture->lock());

   // Base color of black
   u32 color = 0xff000000;

   // For each point on the texture
   for (int x = 0; x < 128; x++)
   {
      for (int y = 0; y < 128; y++)
      {
         // NOTE: X and Y for the texture are X and Z respectively for the terrain.  I've tried to
         // keep my variable naming consistant based on whether the variable is referring more to the
         // terrain scene node or the texture.  But most things refer to both so Y and Z are used
         // somewhat interchangably.

         // Grab the scale of the terrain
         f32 xScale = terrain->getScale().X;
         f32 yScale = terrain->getScale().Y;
         f32 zScale = terrain->getScale().Z;

         // Calculate the slopes in the x and y directions
         // Another option would be to find the normal vector for the area being worked on
         // and using the y component as the slope.  This would also let you skip the next step
         // as the y component of the normal would always be positive.
         f32 xSlope = terrain->getHeight(x*xScale, y*zScale) - terrain->getHeight(x*xScale + 1.0f, y*zScale);
         f32 ySlope = terrain->getHeight(x*xScale, y*zScale) - terrain->getHeight(x*xScale, y*zScale + 1.0f);
         
         // Calculate the absolute values of the slopes
         xSlope = abs_<f32>(xSlope);
         ySlope = abs_<f32>(ySlope);

         // Calculate the red color based on slope
         // Steeper = more red
         s16 red = static_cast<s16>((xSlope > ySlope ? xSlope : ySlope) * 128);

         // Calculate the green value based on height
         // Lower = more green
         s16 green = 255 - static_cast<u8>(terrain->getHeight(x*xScale, y*zScale) / yScale);

         // Just set the blue
         s16 blue = 0x00;

         // NOTE: You can change the way the colours are calculated to adjust the results
         // but you should make sure to leave in the clamping I have applied so the colour
         // compilation below will work.
         clampColours(red, green, blue);

         // Build the colour and set it in the correct place
         texData[x+128*y] = color + (red << 16) + (green << 8) + blue;
      }
   }

   // Unlock the texture so it can be used
   texture->unlock();

   // Return our pointer to the newely created texture.
   return texture;
} 
this may need to be slightly modified but it works and works from the heightmap the terrain is generated from so could easily be modified to work with verticies
Remember all information is contextual and if I do not understand the context I cannot gras the information
wowdevver24
Posts: 26
Joined: Mon Jan 04, 2010 8:02 pm

Post by wowdevver24 »

oh in short what I meant was it's easier to work with textures than per vertex colours, what are there like 65000+
Remember all information is contextual and if I do not understand the context I cannot gras the information
instinct
Posts: 87
Joined: Sat May 10, 2008 3:42 pm

Post by instinct »

Code: Select all

video::SColor ( 255, 0, 0, 0 ),   // vertexColor 
shouldnt this be 255,255,255,255 if you want it white? :P
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

wowdevver24 wrote:oh in short what I meant was it's easier to work with textures than per vertex colours, what are there like 65000+
Oh yes, textures only have 512x512 or 1024x1024 pixel, that's... oh wait :shock:
:roll:
wowdevver24
Posts: 26
Joined: Mon Jan 04, 2010 8:02 pm

Post by wowdevver24 »

am I wrong again hybrid, maths seems to say I am lol, but what about outputting the generated textures and checking for their existence first eh :wink: this would then allow for dynamic texture creation with a cache to speed things up.

Can you set sub-mesh textures (as in set texture on a collection of verticies) because if so you could make this algorithm take the count of verticies, the partial mesh and generate terrain only for different parts, like chunks.

Alternatively you could work with much smaller textures and chunks say 32x32 heightmap data needing a 128x128 texture (much smaller than 1024x1024) and works out more efficient to re-texture.

just some random thoughts on the subject
Remember all information is contextual and if I do not understand the context I cannot gras the information
Post Reply