Create terrain from code

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Revan1985
Posts: 89
Joined: Tue May 08, 2007 4:11 pm
Location: Italy

Create terrain from code

Post by Revan1985 »

Hi all, i need to create a casual terrain from code, and not from a mesh, or heightmap...
there is a method?
or i need to create a terrain with a heightmap/mesh, and than modify it :?:
i want to be able to create from code a terrain of 512x512 or 4096x4096 if i want...

thx for the responses :wink:
CPU: AMD PHENOMII X6 1090T BE 3,2GHZ
RAM : OCZ 8GB 2*4GB DDR3 LOW VOLTAGE 1333
VGA: GeForce GTX680 2GB
HD : 500GB + 500GB + 2x1TB Raid Edition + 500GB External
Motherboard: ASUS CROSSHAIR FORMULA 4 890FX AM3
PSU: Corsair 750W
CPU Cooling: Katana 2
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Just use the hillplane mesh with no height and alter the vertices at each position.
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

You could also create a raw image in memory change the pixels in the image and then create the terrain from the raw image. This would give you a terrain mesh with LOD.

I know 1.5 supports bigger meshes but I don't know if it supports them that big, 4096 x 4096 is going to be enormous, perhaps you should consider something that is tiled or a terrain like Arras Shifting Tiled Terrain node.
Revan1985
Posts: 89
Joined: Tue May 08, 2007 4:11 pm
Location: Italy

Post by Revan1985 »

how can i create and manipulate a raw image in irrlicht?!? :oops:
CPU: AMD PHENOMII X6 1090T BE 3,2GHZ
RAM : OCZ 8GB 2*4GB DDR3 LOW VOLTAGE 1333
VGA: GeForce GTX680 2GB
HD : 500GB + 500GB + 2x1TB Raid Edition + 500GB External
Motherboard: ASUS CROSSHAIR FORMULA 4 890FX AM3
PSU: Corsair 750W
CPU Cooling: Katana 2
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

Sorry this was me getting mixed up a bit, the standard terrain node does not have this. My IrrlichtWrapper project contains a custom Tiled Terrain Node, which I changed to pass in an IImage instead of creating it in the function call. You could try to use my node but it is from Irrlicht 1.4.2 and I doubt it will work in version 1.5

You could modify your own Irrlicht and track down the code segement: -

Code: Select all

	//! Initializes the terrain data.  Loads the vertices from the heightMapFile
	bool CTerrainSceneNode::loadHeightMap( io::IReadFile* file, video::SColor vertexColor, s32 smoothFactor )
	{
		if( !file )
			return false;

		u32 startTime = os::Timer::getRealTime();
		video::IImage* heightMap = SceneManager->getVideoDriver()->createImageFromFile( file );
And knock out the createImageFromFile call and instead create it yourself and pass it in as a parameter. Then you can create your own IImage with an: -

Code: Select all

driver->createImage(  format, dimension2d< s32 >( x, y ) );
and then set the pixels yourself. My apologies for the confusion.
Revan1985
Posts: 89
Joined: Tue May 08, 2007 4:11 pm
Location: Italy

Post by Revan1985 »

I've Solved...

here my code for the casual generation from file [i use the terrainscenenode, so i can use the lod ^^']

cterrain.h

Code: Select all

#include "stdafx.h"

using namespace irr;
//using namespace core;

class CTerrain
{
protected:
private:
	video::IImage* m_pImage;
	scene::ITerrainSceneNode* m_pTerrain;
	IrrlichtDevice* m_pDevice;
public:
	CTerrain();
	~CTerrain();
	bool Init(IrrlichtDevice* device);
	void GenerateCasual();
};
cterrain.cpp

Code: Select all

#include "stdafx.h"
#include "CTerrain.h"

CTerrain::CTerrain()
{
}
/* */
CTerrain::~CTerrain()
{
}
/* */
bool CTerrain::Init(IrrlichtDevice* device)
{
	srand((unsigned)time(0));
	m_pDevice = device;
	return true;
}
/* */
void CTerrain::GenerateCasual()
{
	m_pImage = m_pDevice->getVideoDriver()->createImage(video::ECF_A8R8G8B8, core::dimension2d<s32>(128, 128));
	m_pImage->fill(video::SColor(255, 0, 0, 0));

	for(int counter = 0; counter < 8; ++counter)
	{
		s32 x  = rand() % 128;
		s32 y  = rand() % 128;

		for(int i = 0; i < 5000; ++i)
		{
			s32 v = rand() % 4;

			switch(v)
			{
			case 0:
				if(++x > 127){ --x; }
				break;
			case 1:
				if(--x < 0){ ++x; }
				break;
			case 2:
				if(++x > 127){ --y; }
				break;
			case 3:
				if(--y < 0){ ++y; }
				break;
			}

			video::SColor color = m_pImage->getPixel(x, y);

			u32 red = color.getRed();
			u32 blue = color.getBlue();
			u32 green = color.getGreen();

			color.setRed((red <= 256) ? red += 16 : red);
			color.setBlue((blue <= 256) ? blue += 16: blue);
			color.setGreen((green <= 256) ? green += 16 : green);

			m_pImage->setPixel(x, y, color);
		}
	}

	//io::IWriteFile* file = m_pDevice->getFileSystem()->createAndWriteFile("heightmap.jpg");
	m_pDevice->getVideoDriver()->writeImageToFile(m_pImage, "heightmap.jpg");
	//file->drop();

	m_pTerrain = m_pDevice->getSceneManager()->addTerrainSceneNode("heightmap.jpg", 0, -1,
		core::vector3df(0.0f, 0.0f, 0.0f), core::vector3df(0.0f, 0.0f, 0.0f), core::vector3df(40.0f, 4.4f, 40.0f),
		video::SColor(255, 255, 255, 255), 5, scene::ETPS_17, 4);

	m_pTerrain->setMaterialFlag(video::EMF_LIGHTING, false);
	m_pTerrain->setMaterialFlag(video::EMF_WIREFRAME, !m_pTerrain->getMaterial(0).Wireframe);
	m_pTerrain->setMaterialFlag(video::EMF_POINTCLOUD, false);
}
stdafx.h

Code: Select all

#include <Irrlicht.h>

#ifdef _MSC_VER
	#pragma comment(lib, "Irrlicht.lib")
#endif

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <ctime>

i believe you can found this usefull :wink:
CPU: AMD PHENOMII X6 1090T BE 3,2GHZ
RAM : OCZ 8GB 2*4GB DDR3 LOW VOLTAGE 1333
VGA: GeForce GTX680 2GB
HD : 500GB + 500GB + 2x1TB Raid Edition + 500GB External
Motherboard: ASUS CROSSHAIR FORMULA 4 890FX AM3
PSU: Corsair 750W
CPU Cooling: Katana 2
Post Reply